Ignore non-active fullscreen windows for shelf state.
[chromium-blink-merge.git] / content / browser / renderer_host / media / audio_input_sync_writer.cc
blob369e0a87f20ab6760f62dba82a792cd776f61429
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"
7 #include <algorithm>
9 #include "base/memory/shared_memory.h"
11 namespace content {
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,
33 uint32 size,
34 double volume,
35 bool key_pressed) {
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;
48 return size;
51 void AudioInputSyncWriter::Close() {
52 socket_->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());
62 #if defined(OS_WIN)
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);
73 #else
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);
83 #endif
85 } // namespace content