OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "media/audio/audio_output_device.h" | 5 #include "media/audio/audio_output_device.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "media/audio/audio_output_controller.h" | 11 #include "media/audio/audio_output_controller.h" |
12 #include "media/audio/audio_util.h" | 12 #include "media/audio/audio_util.h" |
13 #include "media/audio/shared_memory_util.h" | 13 #include "media/audio/shared_memory_util.h" |
14 #include "media/base/limits.h" | |
14 | 15 |
15 namespace media { | 16 namespace media { |
16 | 17 |
17 // Takes care of invoking the render callback on the audio thread. | 18 // Takes care of invoking the render callback on the audio thread. |
18 // An instance of this class is created for each capture stream in | 19 // An instance of this class is created for each capture stream in |
19 // OnStreamCreated(). | 20 // OnStreamCreated(). |
20 class AudioOutputDevice::AudioThreadCallback | 21 class AudioOutputDevice::AudioThreadCallback |
21 : public AudioDeviceThread::Callback { | 22 : public AudioDeviceThread::Callback { |
22 public: | 23 public: |
23 AudioThreadCallback(const AudioParameters& audio_parameters, | 24 AudioThreadCallback(const AudioParameters& audio_parameters, |
25 int input_channels, | |
24 base::SharedMemoryHandle memory, | 26 base::SharedMemoryHandle memory, |
25 int memory_length, | 27 int memory_length, |
26 AudioRendererSink::RenderCallback* render_callback); | 28 AudioRendererSink::RenderCallback* render_callback); |
27 virtual ~AudioThreadCallback(); | 29 virtual ~AudioThreadCallback(); |
28 | 30 |
29 virtual void MapSharedMemory() OVERRIDE; | 31 virtual void MapSharedMemory() OVERRIDE; |
30 | 32 |
31 // Called whenever we receive notifications about pending data. | 33 // Called whenever we receive notifications about pending data. |
32 virtual void Process(int pending_data) OVERRIDE; | 34 virtual void Process(int pending_data) OVERRIDE; |
33 | 35 |
34 private: | 36 private: |
35 AudioRendererSink::RenderCallback* render_callback_; | 37 AudioRendererSink::RenderCallback* render_callback_; |
36 scoped_ptr<AudioBus> audio_bus_; | 38 scoped_ptr<AudioBus> input_bus_; |
39 scoped_ptr<AudioBus> output_bus_; | |
37 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); | 40 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); |
38 }; | 41 }; |
39 | 42 |
40 AudioOutputDevice::AudioOutputDevice( | 43 AudioOutputDevice::AudioOutputDevice( |
41 AudioOutputIPC* ipc, | 44 AudioOutputIPC* ipc, |
42 const scoped_refptr<base::MessageLoopProxy>& io_loop) | 45 const scoped_refptr<base::MessageLoopProxy>& io_loop) |
43 : ScopedLoopObserver(io_loop), | 46 : ScopedLoopObserver(io_loop), |
47 input_channels_(0), | |
44 callback_(NULL), | 48 callback_(NULL), |
45 ipc_(ipc), | 49 ipc_(ipc), |
46 stream_id_(0), | 50 stream_id_(0), |
47 play_on_start_(true), | 51 play_on_start_(true), |
48 is_started_(false) { | 52 is_started_(false) { |
49 CHECK(ipc_); | 53 CHECK(ipc_); |
50 } | 54 } |
51 | 55 |
52 void AudioOutputDevice::Initialize(const AudioParameters& params, | 56 void AudioOutputDevice::Initialize(const AudioParameters& params, |
53 RenderCallback* callback) { | 57 RenderCallback* callback) { |
54 CHECK_EQ(0, stream_id_) << | 58 CHECK_EQ(0, stream_id_) << |
55 "AudioOutputDevice::Initialize() must be called before Start()"; | 59 "AudioOutputDevice::Initialize() must be called before Start()"; |
56 | 60 |
57 CHECK(!callback_); // Calling Initialize() twice? | 61 CHECK(!callback_); // Calling Initialize() twice? |
58 | 62 |
59 audio_parameters_ = params; | 63 audio_parameters_ = params; |
60 callback_ = callback; | 64 callback_ = callback; |
61 } | 65 } |
62 | 66 |
67 void AudioOutputDevice::InitializeIO(const AudioParameters& params, | |
68 int input_channels, | |
69 RenderCallback* callback) { | |
70 DCHECK_GE(input_channels, 0); | |
71 DCHECK_LT(input_channels, limits::kMaxChannels); | |
72 input_channels_ = input_channels; | |
73 Initialize(params, callback); | |
74 } | |
75 | |
63 AudioOutputDevice::~AudioOutputDevice() { | 76 AudioOutputDevice::~AudioOutputDevice() { |
64 // The current design requires that the user calls Stop() before deleting | 77 // The current design requires that the user calls Stop() before deleting |
65 // this class. | 78 // this class. |
66 CHECK_EQ(0, stream_id_); | 79 CHECK_EQ(0, stream_id_); |
67 } | 80 } |
68 | 81 |
69 void AudioOutputDevice::Start() { | 82 void AudioOutputDevice::Start() { |
70 DCHECK(callback_) << "Initialize hasn't been called"; | 83 DCHECK(callback_) << "Initialize hasn't been called"; |
71 message_loop()->PostTask(FROM_HERE, | 84 message_loop()->PostTask(FROM_HERE, |
72 base::Bind(&AudioOutputDevice::CreateStreamOnIOThread, this, | 85 base::Bind(&AudioOutputDevice::CreateStreamOnIOThread, this, |
73 audio_parameters_)); | 86 audio_parameters_, input_channels_)); |
74 } | 87 } |
75 | 88 |
76 void AudioOutputDevice::Stop() { | 89 void AudioOutputDevice::Stop() { |
77 { | 90 { |
78 base::AutoLock auto_lock(audio_thread_lock_); | 91 base::AutoLock auto_lock(audio_thread_lock_); |
79 audio_thread_.Stop(MessageLoop::current()); | 92 audio_thread_.Stop(MessageLoop::current()); |
80 } | 93 } |
81 | 94 |
82 message_loop()->PostTask(FROM_HERE, | 95 message_loop()->PostTask(FROM_HERE, |
83 base::Bind(&AudioOutputDevice::ShutDownOnIOThread, this)); | 96 base::Bind(&AudioOutputDevice::ShutDownOnIOThread, this)); |
(...skipping 14 matching lines...) Expand all Loading... | |
98 return false; | 111 return false; |
99 | 112 |
100 if (!message_loop()->PostTask(FROM_HERE, | 113 if (!message_loop()->PostTask(FROM_HERE, |
101 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) { | 114 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) { |
102 return false; | 115 return false; |
103 } | 116 } |
104 | 117 |
105 return true; | 118 return true; |
106 } | 119 } |
107 | 120 |
108 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) { | 121 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params, |
122 int input_channels) { | |
109 DCHECK(message_loop()->BelongsToCurrentThread()); | 123 DCHECK(message_loop()->BelongsToCurrentThread()); |
110 // Make sure we don't create the stream more than once. | 124 // Make sure we don't create the stream more than once. |
111 DCHECK_EQ(0, stream_id_); | 125 DCHECK_EQ(0, stream_id_); |
112 if (stream_id_) | 126 if (stream_id_) |
113 return; | 127 return; |
114 | 128 |
115 stream_id_ = ipc_->AddDelegate(this); | 129 stream_id_ = ipc_->AddDelegate(this); |
116 ipc_->CreateStream(stream_id_, params); | 130 ipc_->CreateStream(stream_id_, params, input_channels); |
117 } | 131 } |
118 | 132 |
119 void AudioOutputDevice::PlayOnIOThread() { | 133 void AudioOutputDevice::PlayOnIOThread() { |
120 DCHECK(message_loop()->BelongsToCurrentThread()); | 134 DCHECK(message_loop()->BelongsToCurrentThread()); |
121 if (stream_id_ && is_started_) | 135 if (stream_id_ && is_started_) |
122 ipc_->PlayStream(stream_id_); | 136 ipc_->PlayStream(stream_id_); |
123 else | 137 else |
124 play_on_start_ = true; | 138 play_on_start_ = true; |
125 } | 139 } |
126 | 140 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 // the IPC layer should have closed the shared memory and socket handles | 221 // the IPC layer should have closed the shared memory and socket handles |
208 // for us and not invoked the callback. The basic assertion is that when | 222 // for us and not invoked the callback. The basic assertion is that when |
209 // stream_id_ is 0 the AudioOutputDevice instance is not registered as a | 223 // stream_id_ is 0 the AudioOutputDevice instance is not registered as a |
210 // delegate and hence it should not receive callbacks. | 224 // delegate and hence it should not receive callbacks. |
211 DCHECK(stream_id_); | 225 DCHECK(stream_id_); |
212 | 226 |
213 base::AutoLock auto_lock(audio_thread_lock_); | 227 base::AutoLock auto_lock(audio_thread_lock_); |
214 | 228 |
215 DCHECK(audio_thread_.IsStopped()); | 229 DCHECK(audio_thread_.IsStopped()); |
216 audio_callback_.reset(new AudioOutputDevice::AudioThreadCallback( | 230 audio_callback_.reset(new AudioOutputDevice::AudioThreadCallback( |
217 audio_parameters_, handle, length, callback_)); | 231 audio_parameters_, input_channels_, handle, length, callback_)); |
218 audio_thread_.Start(audio_callback_.get(), socket_handle, | 232 audio_thread_.Start(audio_callback_.get(), socket_handle, |
219 "AudioOutputDevice"); | 233 "AudioOutputDevice"); |
220 | 234 |
221 // We handle the case where Play() and/or Pause() may have been called | 235 // We handle the case where Play() and/or Pause() may have been called |
222 // multiple times before OnStreamCreated() gets called. | 236 // multiple times before OnStreamCreated() gets called. |
223 is_started_ = true; | 237 is_started_ = true; |
224 if (play_on_start_) | 238 if (play_on_start_) |
225 PlayOnIOThread(); | 239 PlayOnIOThread(); |
226 } | 240 } |
227 | 241 |
228 void AudioOutputDevice::OnIPCClosed() { | 242 void AudioOutputDevice::OnIPCClosed() { |
229 ipc_ = NULL; | 243 ipc_ = NULL; |
230 } | 244 } |
231 | 245 |
232 void AudioOutputDevice::WillDestroyCurrentMessageLoop() { | 246 void AudioOutputDevice::WillDestroyCurrentMessageLoop() { |
233 LOG(ERROR) << "IO loop going away before the audio device has been stopped"; | 247 LOG(ERROR) << "IO loop going away before the audio device has been stopped"; |
234 ShutDownOnIOThread(); | 248 ShutDownOnIOThread(); |
235 } | 249 } |
236 | 250 |
237 // AudioOutputDevice::AudioThreadCallback | 251 // AudioOutputDevice::AudioThreadCallback |
238 | 252 |
239 AudioOutputDevice::AudioThreadCallback::AudioThreadCallback( | 253 AudioOutputDevice::AudioThreadCallback::AudioThreadCallback( |
240 const AudioParameters& audio_parameters, | 254 const AudioParameters& audio_parameters, |
255 int input_channels, | |
241 base::SharedMemoryHandle memory, | 256 base::SharedMemoryHandle memory, |
242 int memory_length, | 257 int memory_length, |
243 AudioRendererSink::RenderCallback* render_callback) | 258 AudioRendererSink::RenderCallback* render_callback) |
244 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length), | 259 : AudioDeviceThread::Callback(audio_parameters, |
260 input_channels, | |
261 memory, | |
262 memory_length), | |
245 render_callback_(render_callback) { | 263 render_callback_(render_callback) { |
246 } | 264 } |
247 | 265 |
248 AudioOutputDevice::AudioThreadCallback::~AudioThreadCallback() { | 266 AudioOutputDevice::AudioThreadCallback::~AudioThreadCallback() { |
249 } | 267 } |
250 | 268 |
251 void AudioOutputDevice::AudioThreadCallback::MapSharedMemory() { | 269 void AudioOutputDevice::AudioThreadCallback::MapSharedMemory() { |
252 shared_memory_.Map(TotalSharedMemorySizeInBytes(memory_length_)); | 270 shared_memory_.Map(TotalSharedMemorySizeInBytes(memory_length_)); |
253 DCHECK_EQ(memory_length_, AudioBus::CalculateMemorySize(audio_parameters_)); | 271 |
254 audio_bus_ = AudioBus::WrapMemory(audio_parameters_, shared_memory_.memory()); | 272 // Calculate output and input memory size. |
273 int output_memory_size = AudioBus::CalculateMemorySize(audio_parameters_); | |
274 int frames = audio_parameters_.frames_per_buffer(); | |
275 int input_memory_size = | |
276 AudioBus::CalculateMemorySize(input_channels_, frames); | |
277 | |
278 int io_size = output_memory_size + input_memory_size; | |
henrika (OOO until Aug 14)
2012/09/11 21:44:48
Do we need all the added space in this section?
| |
279 | |
280 DCHECK_EQ(memory_length_, io_size); | |
281 | |
282 output_bus_ = | |
283 AudioBus::WrapMemory(audio_parameters_, shared_memory_.memory()); | |
284 | |
285 if (input_channels_ > 0) { | |
286 // The input data is after the output data. | |
287 char* input_data = | |
288 static_cast<char*>(shared_memory_.memory()) + output_memory_size; | |
289 input_bus_ = | |
290 AudioBus::WrapMemory(input_channels_, frames, input_data); | |
291 } | |
255 } | 292 } |
256 | 293 |
257 // Called whenever we receive notifications about pending data. | 294 // Called whenever we receive notifications about pending data. |
258 void AudioOutputDevice::AudioThreadCallback::Process(int pending_data) { | 295 void AudioOutputDevice::AudioThreadCallback::Process(int pending_data) { |
259 if (pending_data == kPauseMark) { | 296 if (pending_data == kPauseMark) { |
260 memset(shared_memory_.memory(), 0, memory_length_); | 297 memset(shared_memory_.memory(), 0, memory_length_); |
261 SetActualDataSizeInBytes(&shared_memory_, memory_length_, 0); | 298 SetActualDataSizeInBytes(&shared_memory_, memory_length_, 0); |
262 return; | 299 return; |
263 } | 300 } |
264 | 301 |
265 // Convert the number of pending bytes in the render buffer | 302 // Convert the number of pending bytes in the render buffer |
266 // into milliseconds. | 303 // into milliseconds. |
267 int audio_delay_milliseconds = pending_data / bytes_per_ms_; | 304 int audio_delay_milliseconds = pending_data / bytes_per_ms_; |
268 | 305 |
269 TRACE_EVENT0("audio", "AudioOutputDevice::FireRenderCallback"); | 306 TRACE_EVENT0("audio", "AudioOutputDevice::FireRenderCallback"); |
270 | 307 |
271 // Update the audio-delay measurement then ask client to render audio. Since | 308 // Update the audio-delay measurement then ask client to render audio. Since |
272 // |audio_bus_| is wrapping the shared memory the Render() call is writing | 309 // |output_bus_| is wrapping the shared memory the Render() call is writing |
273 // directly into the shared memory. | 310 // directly into the shared memory. |
274 size_t num_frames = render_callback_->Render( | 311 size_t num_frames = audio_parameters_.frames_per_buffer(); |
275 audio_bus_.get(), audio_delay_milliseconds); | 312 |
313 if (input_bus_.get() && input_channels_ > 0) { | |
314 render_callback_->RenderIO(input_bus_.get(), | |
henrika (OOO until Aug 14)
2012/09/11 21:44:48
Why not num_frames = render_callback->?
| |
315 output_bus_.get(), | |
316 audio_delay_milliseconds); | |
317 } else { | |
318 num_frames = render_callback_->Render(output_bus_.get(), | |
319 audio_delay_milliseconds); | |
320 } | |
276 | 321 |
277 // Let the host know we are done. | 322 // Let the host know we are done. |
278 // TODO(dalecurtis): Technically this is not always correct. Due to channel | 323 // TODO(dalecurtis): Technically this is not always correct. Due to channel |
279 // padding for alignment, there may be more data available than this. We're | 324 // padding for alignment, there may be more data available than this. We're |
280 // relying on AudioSyncReader::Read() to parse this with that in mind. Rename | 325 // relying on AudioSyncReader::Read() to parse this with that in mind. Rename |
281 // these methods to Set/GetActualFrameCount(). | 326 // these methods to Set/GetActualFrameCount(). |
282 SetActualDataSizeInBytes( | 327 SetActualDataSizeInBytes( |
283 &shared_memory_, memory_length_, | 328 &shared_memory_, memory_length_, |
284 num_frames * sizeof(*audio_bus_->channel(0)) * audio_bus_->channels()); | 329 num_frames * sizeof(*output_bus_->channel(0)) * output_bus_->channels()); |
285 } | 330 } |
286 | 331 |
287 } // namespace media. | 332 } // namespace media. |
OLD | NEW |