OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/audio_device.h" | 5 #include "content/renderer/audio_device.h" |
6 | 6 |
7 #include "base/singleton.h" | 7 #include "base/singleton.h" |
8 #include "chrome/common/render_messages.h" | 8 #include "chrome/common/render_messages.h" |
9 #include "chrome/common/render_messages_params.h" | |
10 #include "chrome/renderer/render_thread.h" | 9 #include "chrome/renderer/render_thread.h" |
| 10 #include "content/common/audio_messages.h" |
11 #include "media/audio/audio_util.h" | 11 #include "media/audio/audio_util.h" |
12 | 12 |
13 scoped_refptr<AudioMessageFilter> AudioDevice::filter_; | 13 scoped_refptr<AudioMessageFilter> AudioDevice::filter_; |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // AudioMessageFilterCreator is intended to be used as a singleton so we can | 17 // AudioMessageFilterCreator is intended to be used as a singleton so we can |
18 // get access to a shared AudioMessageFilter. | 18 // get access to a shared AudioMessageFilter. |
19 // Example usage: | 19 // Example usage: |
20 // AudioMessageFilter* filter = AudioMessageFilterCreator::SharedFilter(); | 20 // AudioMessageFilter* filter = AudioMessageFilterCreator::SharedFilter(); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 // Make sure we don't call Start() more than once. | 69 // Make sure we don't call Start() more than once. |
70 DCHECK_EQ(0, stream_id_); | 70 DCHECK_EQ(0, stream_id_); |
71 if (stream_id_) | 71 if (stream_id_) |
72 return false; | 72 return false; |
73 | 73 |
74 // Lazily create the message filter and share across AudioDevice instances. | 74 // Lazily create the message filter and share across AudioDevice instances. |
75 filter_ = AudioMessageFilterCreator::SharedFilter(); | 75 filter_ = AudioMessageFilterCreator::SharedFilter(); |
76 | 76 |
77 stream_id_ = filter_->AddDelegate(this); | 77 stream_id_ = filter_->AddDelegate(this); |
78 | 78 |
79 ViewHostMsg_Audio_CreateStream_Params params; | 79 AudioParameters params; |
80 params.params.format = AudioParameters::AUDIO_PCM_LOW_LATENCY; | 80 params.format = AudioParameters::AUDIO_PCM_LOW_LATENCY; |
81 params.params.channels = channels_; | 81 params.channels = channels_; |
82 params.params.sample_rate = static_cast<int>(sample_rate_); | 82 params.sample_rate = static_cast<int>(sample_rate_); |
83 params.params.bits_per_sample = 16; | 83 params.bits_per_sample = 16; |
84 params.params.samples_per_packet = buffer_size_; | 84 params.samples_per_packet = buffer_size_; |
85 | 85 |
86 filter_->Send( | 86 filter_->Send(new AudioHostMsg_CreateStream(0, stream_id_, params, true)); |
87 new ViewHostMsg_CreateAudioStream(0, stream_id_, params, true)); | |
88 | 87 |
89 return true; | 88 return true; |
90 } | 89 } |
91 | 90 |
92 bool AudioDevice::Stop() { | 91 bool AudioDevice::Stop() { |
93 if (stream_id_) { | 92 if (stream_id_) { |
94 OnDestroy(); | 93 OnDestroy(); |
95 return true; | 94 return true; |
96 } | 95 } |
97 return false; | 96 return false; |
98 } | 97 } |
99 | 98 |
100 void AudioDevice::OnDestroy() { | 99 void AudioDevice::OnDestroy() { |
101 // Make sure we don't call destroy more than once. | 100 // Make sure we don't call destroy more than once. |
102 DCHECK_NE(0, stream_id_); | 101 DCHECK_NE(0, stream_id_); |
103 if (!stream_id_) | 102 if (!stream_id_) |
104 return; | 103 return; |
105 | 104 |
106 filter_->RemoveDelegate(stream_id_); | 105 filter_->RemoveDelegate(stream_id_); |
107 filter_->Send(new ViewHostMsg_CloseAudioStream(0, stream_id_)); | 106 filter_->Send(new AudioHostMsg_CloseStream(0, stream_id_)); |
108 stream_id_ = 0; | 107 stream_id_ = 0; |
109 if (audio_thread_.get()) { | 108 if (audio_thread_.get()) { |
110 socket_->Close(); | 109 socket_->Close(); |
111 audio_thread_->Join(); | 110 audio_thread_->Join(); |
112 } | 111 } |
113 } | 112 } |
114 | 113 |
115 void AudioDevice::OnRequestPacket(AudioBuffersState buffers_state) { | 114 void AudioDevice::OnRequestPacket(AudioBuffersState buffers_state) { |
116 // This method does not apply to the low-latency system. | 115 // This method does not apply to the low-latency system. |
117 NOTIMPLEMENTED(); | 116 NOTIMPLEMENTED(); |
118 } | 117 } |
119 | 118 |
120 void AudioDevice::OnStateChanged( | 119 void AudioDevice::OnStateChanged(AudioStreamState state) { |
121 const ViewMsg_AudioStreamState_Params& state) { | |
122 // Not needed in this simple implementation. | 120 // Not needed in this simple implementation. |
123 NOTIMPLEMENTED(); | 121 NOTIMPLEMENTED(); |
124 } | 122 } |
125 | 123 |
126 void AudioDevice::OnCreated( | 124 void AudioDevice::OnCreated( |
127 base::SharedMemoryHandle handle, uint32 length) { | 125 base::SharedMemoryHandle handle, uint32 length) { |
128 // Not needed in this simple implementation. | 126 // Not needed in this simple implementation. |
129 NOTIMPLEMENTED(); | 127 NOTIMPLEMENTED(); |
130 } | 128 } |
131 | 129 |
(...skipping 19 matching lines...) Expand all Loading... |
151 | 149 |
152 socket_.reset(new base::SyncSocket(socket_handle)); | 150 socket_.reset(new base::SyncSocket(socket_handle)); |
153 // Allow the client to pre-populate the buffer. | 151 // Allow the client to pre-populate the buffer. |
154 FireRenderCallback(); | 152 FireRenderCallback(); |
155 | 153 |
156 // TODO(crogers): we could optionally set the thread to high-priority | 154 // TODO(crogers): we could optionally set the thread to high-priority |
157 audio_thread_.reset( | 155 audio_thread_.reset( |
158 new base::DelegateSimpleThread(this, "renderer_audio_thread")); | 156 new base::DelegateSimpleThread(this, "renderer_audio_thread")); |
159 audio_thread_->Start(); | 157 audio_thread_->Start(); |
160 | 158 |
161 filter_->Send(new ViewHostMsg_PlayAudioStream(0, stream_id_)); | 159 filter_->Send(new AudioHostMsg_PlayStream(0, stream_id_)); |
162 } | 160 } |
163 | 161 |
164 void AudioDevice::OnVolume(double volume) { | 162 void AudioDevice::OnVolume(double volume) { |
165 // Not needed in this simple implementation. | 163 // Not needed in this simple implementation. |
166 NOTIMPLEMENTED(); | 164 NOTIMPLEMENTED(); |
167 } | 165 } |
168 | 166 |
169 // Our audio thread runs here. | 167 // Our audio thread runs here. |
170 void AudioDevice::Run() { | 168 void AudioDevice::Run() { |
171 int pending_data; | 169 int pending_data; |
172 while (sizeof(pending_data) == socket_->Receive(&pending_data, | 170 while (sizeof(pending_data) == socket_->Receive(&pending_data, |
173 sizeof(pending_data)) && | 171 sizeof(pending_data)) && |
174 pending_data >= 0) { | 172 pending_data >= 0) { |
175 FireRenderCallback(); | 173 FireRenderCallback(); |
176 } | 174 } |
177 } | 175 } |
178 | 176 |
179 void AudioDevice::FireRenderCallback() { | 177 void AudioDevice::FireRenderCallback() { |
180 if (callback_) { | 178 if (callback_) { |
181 // Ask client to render audio. | 179 // Ask client to render audio. |
182 callback_->Render(audio_data_, buffer_size_); | 180 callback_->Render(audio_data_, buffer_size_); |
183 | 181 |
184 // Interleave, scale, and clip to int16. | 182 // Interleave, scale, and clip to int16. |
185 int16* output_buffer16 = static_cast<int16*>(shared_memory_data()); | 183 int16* output_buffer16 = static_cast<int16*>(shared_memory_data()); |
186 media::InterleaveFloatToInt16(audio_data_, output_buffer16, buffer_size_); | 184 media::InterleaveFloatToInt16(audio_data_, output_buffer16, buffer_size_); |
187 } | 185 } |
188 } | 186 } |
OLD | NEW |