1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/media/audio_input_sync_writer.h"
9 #include "base/memory/shared_memory.h"
13 AudioInputSyncWriter::AudioInputSyncWriter(
14 base::SharedMemory
* shared_memory
,
15 int shared_memory_segment_count
)
16 : shared_memory_(shared_memory
),
17 shared_memory_segment_count_(shared_memory_segment_count
),
18 current_segment_id_(0) {
19 DCHECK_GT(shared_memory_segment_count
, 0);
20 DCHECK_EQ(shared_memory
->requested_size() % shared_memory_segment_count
, 0u);
21 shared_memory_segment_size_
=
22 shared_memory
->requested_size() / shared_memory_segment_count
;
25 AudioInputSyncWriter::~AudioInputSyncWriter() {}
27 // TODO(henrika): Combine into one method (including Write).
28 void AudioInputSyncWriter::UpdateRecordedBytes(uint32 bytes
) {
29 socket_
->Send(&bytes
, sizeof(bytes
));
32 uint32
AudioInputSyncWriter::Write(const void* data
,
36 uint8
* ptr
= static_cast<uint8
*>(shared_memory_
->memory());
37 ptr
+= current_segment_id_
* shared_memory_segment_size_
;
38 media::AudioInputBuffer
* buffer
=
39 reinterpret_cast<media::AudioInputBuffer
*>(ptr
);
40 buffer
->params
.volume
= volume
;
41 buffer
->params
.size
= size
;
42 buffer
->params
.key_pressed
= key_pressed
;
43 memcpy(buffer
->audio
, data
, size
);
45 if (++current_segment_id_
>= shared_memory_segment_count_
)
46 current_segment_id_
= 0;
51 void AudioInputSyncWriter::Close() {
55 bool AudioInputSyncWriter::Init() {
56 socket_
.reset(new base::CancelableSyncSocket());
57 foreign_socket_
.reset(new base::CancelableSyncSocket());
58 return base::CancelableSyncSocket::CreatePair(socket_
.get(),
59 foreign_socket_
.get());
64 bool AudioInputSyncWriter::PrepareForeignSocketHandle(
65 base::ProcessHandle process_handle
,
66 base::SyncSocket::Handle
* foreign_handle
) {
67 ::DuplicateHandle(GetCurrentProcess(), foreign_socket_
->handle(),
68 process_handle
, foreign_handle
,
69 0, FALSE
, DUPLICATE_SAME_ACCESS
);
70 return (*foreign_handle
!= 0);
75 bool AudioInputSyncWriter::PrepareForeignSocketHandle(
76 base::ProcessHandle process_handle
,
77 base::FileDescriptor
* foreign_handle
) {
78 foreign_handle
->fd
= foreign_socket_
->handle();
79 foreign_handle
->auto_close
= false;
80 return (foreign_handle
->fd
!= -1);
85 } // namespace content