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/virtual_audio_output_stream.h" | 5 #include "media/audio/virtual_audio_output_stream.h" |
6 | 6 |
7 #include "base/message_loop/message_loop_proxy.h" | 7 #include "base/logging.h" |
8 #include "media/audio/virtual_audio_input_stream.h" | 8 #include "media/audio/virtual_audio_input_stream.h" |
9 | 9 |
10 namespace media { | 10 namespace media { |
11 | 11 |
12 VirtualAudioOutputStream::VirtualAudioOutputStream( | 12 VirtualAudioOutputStream::VirtualAudioOutputStream( |
13 const AudioParameters& params, base::MessageLoopProxy* message_loop, | 13 const AudioParameters& params, VirtualAudioInputStream* target, |
14 VirtualAudioInputStream* target, const AfterCloseCallback& after_close_cb) | 14 const AfterCloseCallback& after_close_cb) |
15 : params_(params), message_loop_(message_loop), | 15 : params_(params), target_input_stream_(target), |
16 target_input_stream_(target), after_close_cb_(after_close_cb), | 16 after_close_cb_(after_close_cb), callback_(NULL), volume_(1.0f) { |
17 callback_(NULL), volume_(1.0f) { | |
18 DCHECK(params_.IsValid()); | 17 DCHECK(params_.IsValid()); |
19 DCHECK(message_loop_); | |
20 DCHECK(target); | 18 DCHECK(target); |
| 19 |
| 20 // VAOS can be constructed on any thread, but will DCHECK that all |
| 21 // AudioOutputStream methods are called from the same thread. |
| 22 thread_checker_.DetachFromThread(); |
21 } | 23 } |
22 | 24 |
23 VirtualAudioOutputStream::~VirtualAudioOutputStream() { | 25 VirtualAudioOutputStream::~VirtualAudioOutputStream() { |
24 DCHECK(!callback_); | 26 DCHECK(!callback_); |
25 } | 27 } |
26 | 28 |
27 bool VirtualAudioOutputStream::Open() { | 29 bool VirtualAudioOutputStream::Open() { |
28 DCHECK(message_loop_->BelongsToCurrentThread()); | 30 DCHECK(thread_checker_.CalledOnValidThread()); |
29 return true; | 31 return true; |
30 } | 32 } |
31 | 33 |
32 void VirtualAudioOutputStream::Start(AudioSourceCallback* callback) { | 34 void VirtualAudioOutputStream::Start(AudioSourceCallback* callback) { |
33 DCHECK(message_loop_->BelongsToCurrentThread()); | 35 DCHECK(thread_checker_.CalledOnValidThread()); |
34 DCHECK(!callback_); | 36 DCHECK(!callback_); |
35 callback_ = callback; | 37 callback_ = callback; |
36 target_input_stream_->AddOutputStream(this, params_); | 38 target_input_stream_->AddOutputStream(this, params_); |
37 } | 39 } |
38 | 40 |
39 void VirtualAudioOutputStream::Stop() { | 41 void VirtualAudioOutputStream::Stop() { |
40 DCHECK(message_loop_->BelongsToCurrentThread()); | 42 DCHECK(thread_checker_.CalledOnValidThread()); |
41 if (callback_) { | 43 if (callback_) { |
| 44 target_input_stream_->RemoveOutputStream(this, params_); |
42 callback_ = NULL; | 45 callback_ = NULL; |
43 target_input_stream_->RemoveOutputStream(this, params_); | |
44 } | 46 } |
45 } | 47 } |
46 | 48 |
47 void VirtualAudioOutputStream::Close() { | 49 void VirtualAudioOutputStream::Close() { |
48 DCHECK(message_loop_->BelongsToCurrentThread()); | 50 DCHECK(thread_checker_.CalledOnValidThread()); |
49 | 51 |
50 Stop(); | 52 Stop(); |
51 | 53 |
52 // If a non-null AfterCloseCallback was provided to the constructor, invoke it | 54 // If a non-null AfterCloseCallback was provided to the constructor, invoke it |
53 // here. The callback is moved to a stack-local first since |this| could be | 55 // here. The callback is moved to a stack-local first since |this| could be |
54 // destroyed during Run(). | 56 // destroyed during Run(). |
55 if (!after_close_cb_.is_null()) { | 57 if (!after_close_cb_.is_null()) { |
56 const AfterCloseCallback cb = after_close_cb_; | 58 const AfterCloseCallback cb = after_close_cb_; |
57 after_close_cb_.Reset(); | 59 after_close_cb_.Reset(); |
58 cb.Run(this); | 60 cb.Run(this); |
59 } | 61 } |
60 } | 62 } |
61 | 63 |
62 void VirtualAudioOutputStream::SetVolume(double volume) { | 64 void VirtualAudioOutputStream::SetVolume(double volume) { |
63 DCHECK(message_loop_->BelongsToCurrentThread()); | 65 DCHECK(thread_checker_.CalledOnValidThread()); |
64 volume_ = volume; | 66 volume_ = volume; |
65 } | 67 } |
66 | 68 |
67 void VirtualAudioOutputStream::GetVolume(double* volume) { | 69 void VirtualAudioOutputStream::GetVolume(double* volume) { |
68 DCHECK(message_loop_->BelongsToCurrentThread()); | 70 DCHECK(thread_checker_.CalledOnValidThread()); |
69 *volume = volume_; | 71 *volume = volume_; |
70 } | 72 } |
71 | 73 |
72 double VirtualAudioOutputStream::ProvideInput(AudioBus* audio_bus, | 74 double VirtualAudioOutputStream::ProvideInput(AudioBus* audio_bus, |
73 base::TimeDelta buffer_delay) { | 75 base::TimeDelta buffer_delay) { |
74 DCHECK(message_loop_->BelongsToCurrentThread()); | 76 // Note: This method may be invoked on any one thread, depending on the |
| 77 // platform. |
75 DCHECK(callback_); | 78 DCHECK(callback_); |
76 | 79 |
77 const int frames = callback_->OnMoreData(audio_bus, AudioBuffersState()); | 80 const int frames = callback_->OnMoreData(audio_bus, AudioBuffersState()); |
78 if (frames < audio_bus->frames()) | 81 if (frames < audio_bus->frames()) |
79 audio_bus->ZeroFramesPartial(frames, audio_bus->frames() - frames); | 82 audio_bus->ZeroFramesPartial(frames, audio_bus->frames() - frames); |
80 | 83 |
81 return frames > 0 ? volume_ : 0; | 84 return frames > 0 ? volume_ : 0; |
82 } | 85 } |
83 | 86 |
84 } // namespace media | 87 } // namespace media |
OLD | NEW |