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/process_util.h"
10 #include "base/shared_memory.h"
14 AudioInputSyncWriter::AudioInputSyncWriter(
15 base::SharedMemory
* shared_memory
,
16 int shared_memory_segment_count
)
17 : shared_memory_(shared_memory
),
18 shared_memory_segment_count_(shared_memory_segment_count
),
19 current_segment_id_(0) {
20 DCHECK_GT(shared_memory_segment_count
, 0);
21 DCHECK_EQ(shared_memory
->requested_size() % shared_memory_segment_count
, 0u);
22 shared_memory_segment_size_
=
23 shared_memory
->requested_size() / shared_memory_segment_count
;
26 AudioInputSyncWriter::~AudioInputSyncWriter() {}
28 // TODO(henrika): Combine into one method (including Write).
29 void AudioInputSyncWriter::UpdateRecordedBytes(uint32 bytes
) {
30 socket_
->Send(&bytes
, sizeof(bytes
));
33 uint32
AudioInputSyncWriter::Write(
34 const void* data
, uint32 size
, double volume
) {
35 uint8
* ptr
= static_cast<uint8
*>(shared_memory_
->memory());
36 ptr
+= current_segment_id_
* shared_memory_segment_size_
;
37 media::AudioInputBuffer
* buffer
=
38 reinterpret_cast<media::AudioInputBuffer
*>(ptr
);
39 buffer
->params
.volume
= volume
;
40 buffer
->params
.size
= size
;
41 memcpy(buffer
->audio
, data
, size
);
43 if (++current_segment_id_
>= shared_memory_segment_count_
)
44 current_segment_id_
= 0;
49 void AudioInputSyncWriter::Close() {
53 bool AudioInputSyncWriter::Init() {
54 socket_
.reset(new base::CancelableSyncSocket());
55 foreign_socket_
.reset(new base::CancelableSyncSocket());
56 return base::CancelableSyncSocket::CreatePair(socket_
.get(),
57 foreign_socket_
.get());
62 bool AudioInputSyncWriter::PrepareForeignSocketHandle(
63 base::ProcessHandle process_handle
,
64 base::SyncSocket::Handle
* foreign_handle
) {
65 ::DuplicateHandle(GetCurrentProcess(), foreign_socket_
->handle(),
66 process_handle
, foreign_handle
,
67 0, FALSE
, DUPLICATE_SAME_ACCESS
);
68 return (*foreign_handle
!= 0);
73 bool AudioInputSyncWriter::PrepareForeignSocketHandle(
74 base::ProcessHandle process_handle
,
75 base::FileDescriptor
* foreign_handle
) {
76 foreign_handle
->fd
= foreign_socket_
->handle();
77 foreign_handle
->auto_close
= false;
78 return (foreign_handle
->fd
!= -1);
83 } // namespace content