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.h" | 7 #include "base/message_loop_proxy.h" |
8 #include "media/audio/audio_manager_base.h" | |
9 #include "media/audio/virtual_audio_input_stream.h" | 8 #include "media/audio/virtual_audio_input_stream.h" |
10 | 9 |
11 namespace media { | 10 namespace media { |
12 | 11 |
13 // static | |
14 VirtualAudioOutputStream* VirtualAudioOutputStream::MakeStream( | |
15 AudioManagerBase* manager, const AudioParameters& params, | |
16 base::MessageLoopProxy* message_loop, VirtualAudioInputStream* target) { | |
17 return new VirtualAudioOutputStream(manager, params, message_loop, target); | |
18 } | |
19 | |
20 VirtualAudioOutputStream::VirtualAudioOutputStream( | 12 VirtualAudioOutputStream::VirtualAudioOutputStream( |
21 AudioManagerBase* manager, const AudioParameters& params, | 13 const AudioParameters& params, base::MessageLoopProxy* message_loop, |
22 base::MessageLoopProxy* message_loop, VirtualAudioInputStream* target) | 14 VirtualAudioInputStream* target, const AfterCloseCallback& after_close_cb) |
23 : audio_manager_(manager), message_loop_(message_loop), callback_(NULL), | 15 : params_(params), message_loop_(message_loop), |
24 params_(params), target_input_stream_(target), volume_(1.0f), | 16 target_input_stream_(target), after_close_cb_(after_close_cb), |
25 attached_(false) { | 17 callback_(NULL), volume_(1.0f) { |
| 18 DCHECK(params_.IsValid()); |
| 19 DCHECK(message_loop_); |
| 20 DCHECK(target); |
26 } | 21 } |
27 | 22 |
28 VirtualAudioOutputStream::~VirtualAudioOutputStream() { | 23 VirtualAudioOutputStream::~VirtualAudioOutputStream() { |
29 DCHECK(!callback_); | 24 DCHECK(!callback_); |
30 DCHECK(!attached_); | |
31 } | 25 } |
32 | 26 |
33 bool VirtualAudioOutputStream::Open() { | 27 bool VirtualAudioOutputStream::Open() { |
34 DCHECK(message_loop_->BelongsToCurrentThread()); | 28 DCHECK(message_loop_->BelongsToCurrentThread()); |
35 return true; | 29 return true; |
36 } | 30 } |
37 | 31 |
38 void VirtualAudioOutputStream::Start(AudioSourceCallback* callback) { | 32 void VirtualAudioOutputStream::Start(AudioSourceCallback* callback) { |
39 DCHECK(message_loop_->BelongsToCurrentThread()); | 33 DCHECK(message_loop_->BelongsToCurrentThread()); |
40 DCHECK(!attached_); | 34 DCHECK(!callback_); |
41 callback_ = callback; | 35 callback_ = callback; |
42 target_input_stream_->AddOutputStream(this, params_); | 36 target_input_stream_->AddOutputStream(this, params_); |
43 attached_ = true; | |
44 } | 37 } |
45 | 38 |
46 void VirtualAudioOutputStream::Stop() { | 39 void VirtualAudioOutputStream::Stop() { |
47 DCHECK(message_loop_->BelongsToCurrentThread()); | 40 DCHECK(message_loop_->BelongsToCurrentThread()); |
48 DCHECK(attached_); | 41 if (callback_) { |
49 callback_ = NULL; | 42 callback_ = NULL; |
50 target_input_stream_->RemoveOutputStream(this, params_); | 43 target_input_stream_->RemoveOutputStream(this, params_); |
51 attached_ = false; | 44 } |
52 } | 45 } |
53 | 46 |
54 void VirtualAudioOutputStream::Close() { | 47 void VirtualAudioOutputStream::Close() { |
55 DCHECK(message_loop_->BelongsToCurrentThread()); | 48 DCHECK(message_loop_->BelongsToCurrentThread()); |
56 audio_manager_->ReleaseOutputStream(this); | 49 |
| 50 Stop(); |
| 51 |
| 52 // 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 |
| 54 // destroyed during Run(). |
| 55 if (!after_close_cb_.is_null()) { |
| 56 const AfterCloseCallback cb = after_close_cb_; |
| 57 after_close_cb_.Reset(); |
| 58 cb.Run(this); |
| 59 } |
57 } | 60 } |
58 | 61 |
59 void VirtualAudioOutputStream::SetVolume(double volume) { | 62 void VirtualAudioOutputStream::SetVolume(double volume) { |
| 63 DCHECK(message_loop_->BelongsToCurrentThread()); |
60 volume_ = volume; | 64 volume_ = volume; |
61 } | 65 } |
62 | 66 |
63 void VirtualAudioOutputStream::GetVolume(double* volume) { | 67 void VirtualAudioOutputStream::GetVolume(double* volume) { |
| 68 DCHECK(message_loop_->BelongsToCurrentThread()); |
64 *volume = volume_; | 69 *volume = volume_; |
65 } | 70 } |
66 | 71 |
67 double VirtualAudioOutputStream::ProvideInput(AudioBus* audio_bus, | 72 double VirtualAudioOutputStream::ProvideInput(AudioBus* audio_bus, |
68 base::TimeDelta buffer_delay) { | 73 base::TimeDelta buffer_delay) { |
69 DCHECK(message_loop_->BelongsToCurrentThread()); | 74 DCHECK(message_loop_->BelongsToCurrentThread()); |
70 DCHECK(callback_); | 75 DCHECK(callback_); |
71 | 76 |
72 int frames = callback_->OnMoreData(audio_bus, AudioBuffersState()); | 77 int frames = callback_->OnMoreData(audio_bus, AudioBuffersState()); |
73 if (frames < audio_bus->frames()) | 78 if (frames < audio_bus->frames()) |
74 audio_bus->ZeroFramesPartial(frames, audio_bus->frames() - frames); | 79 audio_bus->ZeroFramesPartial(frames, audio_bus->frames() - frames); |
75 | 80 |
76 return frames > 0 ? volume_ : 0; | 81 return frames > 0 ? volume_ : 0; |
77 } | 82 } |
78 | 83 |
79 } // namespace media | 84 } // namespace media |
OLD | NEW |