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" |
(...skipping 29 matching lines...) Expand all Loading... |
40 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); | 40 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); |
41 }; | 41 }; |
42 | 42 |
43 AudioOutputDevice::AudioOutputDevice( | 43 AudioOutputDevice::AudioOutputDevice( |
44 AudioOutputIPC* ipc, | 44 AudioOutputIPC* ipc, |
45 const scoped_refptr<base::MessageLoopProxy>& io_loop) | 45 const scoped_refptr<base::MessageLoopProxy>& io_loop) |
46 : ScopedLoopObserver(io_loop), | 46 : ScopedLoopObserver(io_loop), |
47 input_channels_(0), | 47 input_channels_(0), |
48 callback_(NULL), | 48 callback_(NULL), |
49 ipc_(ipc), | 49 ipc_(ipc), |
50 stream_id_(0), | 50 state_(IDLE), |
51 play_on_start_(true), | 51 play_on_start_(true), |
52 is_started_(false), | |
53 stopping_hack_(false) { | 52 stopping_hack_(false) { |
54 CHECK(ipc_); | 53 CHECK(ipc_); |
| 54 stream_id_ = ipc_->AddDelegate(this); |
55 } | 55 } |
56 | 56 |
57 void AudioOutputDevice::Initialize(const AudioParameters& params, | 57 void AudioOutputDevice::Initialize(const AudioParameters& params, |
58 RenderCallback* callback) { | 58 RenderCallback* callback) { |
59 CHECK_EQ(0, stream_id_) << | 59 DCHECK(!callback_) << "Calling Initialize() twice?"; |
60 "AudioOutputDevice::Initialize() must be called before Start()"; | |
61 | |
62 CHECK(!callback_); // Calling Initialize() twice? | |
63 | |
64 audio_parameters_ = params; | 60 audio_parameters_ = params; |
65 callback_ = callback; | 61 callback_ = callback; |
66 } | 62 } |
67 | 63 |
68 void AudioOutputDevice::InitializeIO(const AudioParameters& params, | 64 void AudioOutputDevice::InitializeIO(const AudioParameters& params, |
69 int input_channels, | 65 int input_channels, |
70 RenderCallback* callback) { | 66 RenderCallback* callback) { |
71 DCHECK_GE(input_channels, 0); | 67 DCHECK_GE(input_channels, 0); |
72 DCHECK_LT(input_channels, limits::kMaxChannels); | 68 DCHECK_LT(input_channels, limits::kMaxChannels); |
73 input_channels_ = input_channels; | 69 input_channels_ = input_channels; |
74 Initialize(params, callback); | 70 Initialize(params, callback); |
75 } | 71 } |
76 | 72 |
77 AudioOutputDevice::~AudioOutputDevice() { | 73 AudioOutputDevice::~AudioOutputDevice() { |
78 // The current design requires that the user calls Stop() before deleting | 74 // The current design requires that the user calls Stop() before deleting |
79 // this class. | 75 // this class. |
80 CHECK_EQ(0, stream_id_); | 76 DCHECK(audio_thread_.IsStopped()); |
| 77 |
| 78 if (ipc_) |
| 79 ipc_->RemoveDelegate(stream_id_); |
81 } | 80 } |
82 | 81 |
83 void AudioOutputDevice::Start() { | 82 void AudioOutputDevice::Start() { |
84 DCHECK(callback_) << "Initialize hasn't been called"; | 83 DCHECK(callback_) << "Initialize hasn't been called"; |
85 message_loop()->PostTask(FROM_HERE, | 84 message_loop()->PostTask(FROM_HERE, |
86 base::Bind(&AudioOutputDevice::CreateStreamOnIOThread, this, | 85 base::Bind(&AudioOutputDevice::CreateStreamOnIOThread, this, |
87 audio_parameters_, input_channels_)); | 86 audio_parameters_, input_channels_)); |
88 } | 87 } |
89 | 88 |
90 void AudioOutputDevice::Stop() { | 89 void AudioOutputDevice::Stop() { |
(...skipping 25 matching lines...) Expand all Loading... |
116 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) { | 115 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) { |
117 return false; | 116 return false; |
118 } | 117 } |
119 | 118 |
120 return true; | 119 return true; |
121 } | 120 } |
122 | 121 |
123 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params, | 122 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params, |
124 int input_channels) { | 123 int input_channels) { |
125 DCHECK(message_loop()->BelongsToCurrentThread()); | 124 DCHECK(message_loop()->BelongsToCurrentThread()); |
126 // Make sure we don't create the stream more than once. | 125 if (state_ == IDLE) { |
127 DCHECK_EQ(0, stream_id_); | 126 state_ = CREATING_STREAM; |
128 if (stream_id_) | 127 ipc_->CreateStream(stream_id_, params, input_channels); |
129 return; | 128 } |
130 | |
131 stream_id_ = ipc_->AddDelegate(this); | |
132 ipc_->CreateStream(stream_id_, params, input_channels); | |
133 } | 129 } |
134 | 130 |
135 void AudioOutputDevice::PlayOnIOThread() { | 131 void AudioOutputDevice::PlayOnIOThread() { |
136 DCHECK(message_loop()->BelongsToCurrentThread()); | 132 DCHECK(message_loop()->BelongsToCurrentThread()); |
137 if (stream_id_ && is_started_) | 133 if (state_ == PAUSED) { |
138 ipc_->PlayStream(stream_id_); | 134 ipc_->PlayStream(stream_id_); |
139 else | 135 state_ = PLAYING; |
| 136 play_on_start_ = false; |
| 137 } else { |
140 play_on_start_ = true; | 138 play_on_start_ = true; |
| 139 } |
141 } | 140 } |
142 | 141 |
143 void AudioOutputDevice::PauseOnIOThread(bool flush) { | 142 void AudioOutputDevice::PauseOnIOThread(bool flush) { |
144 DCHECK(message_loop()->BelongsToCurrentThread()); | 143 DCHECK(message_loop()->BelongsToCurrentThread()); |
145 if (stream_id_ && is_started_) { | 144 if (state_ == PLAYING) { |
146 ipc_->PauseStream(stream_id_); | 145 ipc_->PauseStream(stream_id_); |
147 if (flush) | 146 if (flush) |
148 ipc_->FlushStream(stream_id_); | 147 ipc_->FlushStream(stream_id_); |
| 148 state_ = PAUSED; |
149 } else { | 149 } else { |
150 // Note that |flush| isn't relevant here since this is the case where | 150 // Note that |flush| isn't relevant here since this is the case where |
151 // the stream is first starting. | 151 // the stream is first starting. |
152 play_on_start_ = false; | |
153 } | 152 } |
| 153 play_on_start_ = false; |
154 } | 154 } |
155 | 155 |
156 void AudioOutputDevice::ShutDownOnIOThread() { | 156 void AudioOutputDevice::ShutDownOnIOThread() { |
157 DCHECK(message_loop()->BelongsToCurrentThread()); | 157 DCHECK(message_loop()->BelongsToCurrentThread()); |
158 | 158 |
159 // Make sure we don't call shutdown more than once. | 159 // Make sure we don't call shutdown more than once. |
160 if (stream_id_) { | 160 if (state_ >= CREATING_STREAM) { |
161 is_started_ = false; | 161 ipc_->CloseStream(stream_id_); |
162 | 162 state_ = IDLE; |
163 if (ipc_) { | |
164 ipc_->CloseStream(stream_id_); | |
165 ipc_->RemoveDelegate(stream_id_); | |
166 } | |
167 | |
168 stream_id_ = 0; | |
169 } | 163 } |
170 | 164 |
171 // We can run into an issue where ShutDownOnIOThread is called right after | 165 // We can run into an issue where ShutDownOnIOThread is called right after |
172 // OnStreamCreated is called in cases where Start/Stop are called before we | 166 // OnStreamCreated is called in cases where Start/Stop are called before we |
173 // get the OnStreamCreated callback. To handle that corner case, we call | 167 // get the OnStreamCreated callback. To handle that corner case, we call |
174 // Stop(). In most cases, the thread will already be stopped. | 168 // Stop(). In most cases, the thread will already be stopped. |
175 // | 169 // |
176 // Another situation is when the IO thread goes away before Stop() is called | 170 // Another situation is when the IO thread goes away before Stop() is called |
177 // in which case, we cannot use the message loop to close the thread handle | 171 // in which case, we cannot use the message loop to close the thread handle |
178 // and can't rely on the main thread existing either. | 172 // and can't rely on the main thread existing either. |
179 base::AutoLock auto_lock_(audio_thread_lock_); | 173 base::AutoLock auto_lock_(audio_thread_lock_); |
180 base::ThreadRestrictions::ScopedAllowIO allow_io; | 174 base::ThreadRestrictions::ScopedAllowIO allow_io; |
181 audio_thread_.Stop(NULL); | 175 audio_thread_.Stop(NULL); |
182 audio_callback_.reset(); | 176 audio_callback_.reset(); |
183 stopping_hack_ = false; | 177 stopping_hack_ = false; |
184 } | 178 } |
185 | 179 |
186 void AudioOutputDevice::SetVolumeOnIOThread(double volume) { | 180 void AudioOutputDevice::SetVolumeOnIOThread(double volume) { |
187 DCHECK(message_loop()->BelongsToCurrentThread()); | 181 DCHECK(message_loop()->BelongsToCurrentThread()); |
188 if (stream_id_) | 182 if (state_ >= PAUSED) |
189 ipc_->SetVolume(stream_id_, volume); | 183 ipc_->SetVolume(stream_id_, volume); |
190 } | 184 } |
191 | 185 |
192 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegate::State state) { | 186 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegate::State state) { |
193 DCHECK(message_loop()->BelongsToCurrentThread()); | 187 DCHECK(message_loop()->BelongsToCurrentThread()); |
194 | 188 |
195 // Do nothing if the stream has been closed. | 189 // Do nothing if the stream has been closed. |
196 if (!stream_id_) | 190 if (state_ < CREATING_STREAM) |
197 return; | 191 return; |
198 | 192 |
199 if (state == AudioOutputIPCDelegate::kError) { | 193 if (state == AudioOutputIPCDelegate::kError) { |
200 DLOG(WARNING) << "AudioOutputDevice::OnStateChanged(kError)"; | 194 DLOG(WARNING) << "AudioOutputDevice::OnStateChanged(kError)"; |
201 // Don't dereference the callback object if the audio thread | 195 // Don't dereference the callback object if the audio thread |
202 // is stopped or stopping. That could mean that the callback | 196 // is stopped or stopping. That could mean that the callback |
203 // object has been deleted. | 197 // object has been deleted. |
204 // TODO(tommi): Add an explicit contract for clearing the callback | 198 // TODO(tommi): Add an explicit contract for clearing the callback |
205 // object. Possibly require calling Initialize again or provide | 199 // object. Possibly require calling Initialize again or provide |
206 // a callback object via Start() and clear it in Stop(). | 200 // a callback object via Start() and clear it in Stop(). |
207 if (!audio_thread_.IsStopped()) | 201 if (!audio_thread_.IsStopped()) |
208 callback_->OnRenderError(); | 202 callback_->OnRenderError(); |
209 } | 203 } |
210 } | 204 } |
211 | 205 |
212 void AudioOutputDevice::OnStreamCreated( | 206 void AudioOutputDevice::OnStreamCreated( |
213 base::SharedMemoryHandle handle, | 207 base::SharedMemoryHandle handle, |
214 base::SyncSocket::Handle socket_handle, | 208 base::SyncSocket::Handle socket_handle, |
215 int length) { | 209 int length) { |
216 DCHECK(message_loop()->BelongsToCurrentThread()); | 210 DCHECK(message_loop()->BelongsToCurrentThread()); |
217 #if defined(OS_WIN) | 211 #if defined(OS_WIN) |
218 DCHECK(handle); | 212 DCHECK(handle); |
219 DCHECK(socket_handle); | 213 DCHECK(socket_handle); |
220 #else | 214 #else |
221 DCHECK_GE(handle.fd, 0); | 215 DCHECK_GE(handle.fd, 0); |
222 DCHECK_GE(socket_handle, 0); | 216 DCHECK_GE(socket_handle, 0); |
223 #endif | 217 #endif |
224 | 218 |
225 // We should only get this callback if stream_id_ is valid. If it is not, | 219 if (state_ != CREATING_STREAM) |
226 // the IPC layer should have closed the shared memory and socket handles | 220 return; |
227 // for us and not invoked the callback. The basic assertion is that when | |
228 // stream_id_ is 0 the AudioOutputDevice instance is not registered as a | |
229 // delegate and hence it should not receive callbacks. | |
230 DCHECK(stream_id_); | |
231 | 221 |
232 // We can receive OnStreamCreated() on the IO thread after the client has | 222 // We can receive OnStreamCreated() on the IO thread after the client has |
233 // called Stop() but before ShutDownOnIOThread() is processed. In such a | 223 // called Stop() but before ShutDownOnIOThread() is processed. In such a |
234 // situation |callback_| might point to freed memory. Instead of starting | 224 // situation |callback_| might point to freed memory. Instead of starting |
235 // |audio_thread_| do nothing and wait for ShutDownOnIOThread() to get called. | 225 // |audio_thread_| do nothing and wait for ShutDownOnIOThread() to get called. |
236 // | 226 // |
237 // TODO(scherkus): The real fix is to have sane ownership semantics. The fact | 227 // TODO(scherkus): The real fix is to have sane ownership semantics. The fact |
238 // that |callback_| (which should own and outlive this object!) can point to | 228 // that |callback_| (which should own and outlive this object!) can point to |
239 // freed memory is a mess. AudioRendererSink should be non-refcounted so that | 229 // freed memory is a mess. AudioRendererSink should be non-refcounted so that |
240 // owners (WebRtcAudioDeviceImpl, AudioRendererImpl, etc...) can Stop() and | 230 // owners (WebRtcAudioDeviceImpl, AudioRendererImpl, etc...) can Stop() and |
241 // delete as they see fit. AudioOutputDevice should internally use WeakPtr | 231 // delete as they see fit. AudioOutputDevice should internally use WeakPtr |
242 // to handle teardown and thread hopping. See http://crbug.com/151051 for | 232 // to handle teardown and thread hopping. See http://crbug.com/151051 for |
243 // details. | 233 // details. |
244 base::AutoLock auto_lock(audio_thread_lock_); | 234 base::AutoLock auto_lock(audio_thread_lock_); |
245 if (stopping_hack_) | 235 if (stopping_hack_) |
246 return; | 236 return; |
247 | 237 |
248 DCHECK(audio_thread_.IsStopped()); | 238 DCHECK(audio_thread_.IsStopped()); |
249 audio_callback_.reset(new AudioOutputDevice::AudioThreadCallback( | 239 audio_callback_.reset(new AudioOutputDevice::AudioThreadCallback( |
250 audio_parameters_, input_channels_, handle, length, callback_)); | 240 audio_parameters_, input_channels_, handle, length, callback_)); |
251 audio_thread_.Start(audio_callback_.get(), socket_handle, | 241 audio_thread_.Start(audio_callback_.get(), socket_handle, |
252 "AudioOutputDevice"); | 242 "AudioOutputDevice"); |
| 243 state_ = PAUSED; |
253 | 244 |
254 // We handle the case where Play() and/or Pause() may have been called | 245 // We handle the case where Play() and/or Pause() may have been called |
255 // multiple times before OnStreamCreated() gets called. | 246 // multiple times before OnStreamCreated() gets called. |
256 is_started_ = true; | |
257 if (play_on_start_) | 247 if (play_on_start_) |
258 PlayOnIOThread(); | 248 PlayOnIOThread(); |
259 } | 249 } |
260 | 250 |
261 void AudioOutputDevice::OnIPCClosed() { | 251 void AudioOutputDevice::OnIPCClosed() { |
| 252 DCHECK(message_loop()->BelongsToCurrentThread()); |
| 253 state_ = IPC_CLOSED; |
262 ipc_ = NULL; | 254 ipc_ = NULL; |
263 } | 255 } |
264 | 256 |
265 void AudioOutputDevice::WillDestroyCurrentMessageLoop() { | 257 void AudioOutputDevice::WillDestroyCurrentMessageLoop() { |
266 LOG(ERROR) << "IO loop going away before the audio device has been stopped"; | 258 LOG(ERROR) << "IO loop going away before the audio device has been stopped"; |
267 ShutDownOnIOThread(); | 259 ShutDownOnIOThread(); |
268 } | 260 } |
269 | 261 |
270 // AudioOutputDevice::AudioThreadCallback | 262 // AudioOutputDevice::AudioThreadCallback |
271 | 263 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 // TODO(dalecurtis): Technically this is not always correct. Due to channel | 334 // TODO(dalecurtis): Technically this is not always correct. Due to channel |
343 // padding for alignment, there may be more data available than this. We're | 335 // padding for alignment, there may be more data available than this. We're |
344 // relying on AudioSyncReader::Read() to parse this with that in mind. Rename | 336 // relying on AudioSyncReader::Read() to parse this with that in mind. Rename |
345 // these methods to Set/GetActualFrameCount(). | 337 // these methods to Set/GetActualFrameCount(). |
346 SetActualDataSizeInBytes( | 338 SetActualDataSizeInBytes( |
347 &shared_memory_, memory_length_, | 339 &shared_memory_, memory_length_, |
348 num_frames * sizeof(*output_bus_->channel(0)) * output_bus_->channels()); | 340 num_frames * sizeof(*output_bus_->channel(0)) * output_bus_->channels()); |
349 } | 341 } |
350 | 342 |
351 } // namespace media. | 343 } // namespace media. |
OLD | NEW |