| 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/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
| 11 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 12 #include "media/audio/audio_output_controller.h" | 11 #include "media/audio/audio_output_controller.h" |
| 13 #include "media/base/limits.h" | 12 #include "media/base/limits.h" |
| 14 | 13 |
| 15 namespace media { | 14 namespace media { |
| 16 | 15 |
| 17 // Takes care of invoking the render callback on the audio thread. | 16 // Takes care of invoking the render callback on the audio thread. |
| 18 // An instance of this class is created for each capture stream in | 17 // An instance of this class is created for each capture stream in |
| 19 // OnStreamCreated(). | 18 // OnStreamCreated(). |
| (...skipping 13 matching lines...) Expand all Loading... |
| 33 | 32 |
| 34 private: | 33 private: |
| 35 AudioRendererSink::RenderCallback* render_callback_; | 34 AudioRendererSink::RenderCallback* render_callback_; |
| 36 scoped_ptr<AudioBus> input_bus_; | 35 scoped_ptr<AudioBus> input_bus_; |
| 37 scoped_ptr<AudioBus> output_bus_; | 36 scoped_ptr<AudioBus> output_bus_; |
| 38 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); | 37 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); |
| 39 }; | 38 }; |
| 40 | 39 |
| 41 AudioOutputDevice::AudioOutputDevice( | 40 AudioOutputDevice::AudioOutputDevice( |
| 42 scoped_ptr<AudioOutputIPC> ipc, | 41 scoped_ptr<AudioOutputIPC> ipc, |
| 43 const scoped_refptr<base::MessageLoopProxy>& io_loop) | 42 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 44 : ScopedLoopObserver(io_loop), | 43 : ScopedTaskRunnerObserver(io_task_runner), |
| 45 callback_(NULL), | 44 callback_(NULL), |
| 46 ipc_(ipc.Pass()), | 45 ipc_(ipc.Pass()), |
| 47 state_(IDLE), | 46 state_(IDLE), |
| 48 play_on_start_(true), | 47 play_on_start_(true), |
| 49 session_id_(-1), | 48 session_id_(-1), |
| 50 stopping_hack_(false) { | 49 stopping_hack_(false) { |
| 51 CHECK(ipc_); | 50 CHECK(ipc_); |
| 52 | 51 |
| 53 // The correctness of the code depends on the relative values assigned in the | 52 // The correctness of the code depends on the relative values assigned in the |
| 54 // State enum. | 53 // State enum. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 74 } | 73 } |
| 75 | 74 |
| 76 AudioOutputDevice::~AudioOutputDevice() { | 75 AudioOutputDevice::~AudioOutputDevice() { |
| 77 // The current design requires that the user calls Stop() before deleting | 76 // The current design requires that the user calls Stop() before deleting |
| 78 // this class. | 77 // this class. |
| 79 DCHECK(audio_thread_.IsStopped()); | 78 DCHECK(audio_thread_.IsStopped()); |
| 80 } | 79 } |
| 81 | 80 |
| 82 void AudioOutputDevice::Start() { | 81 void AudioOutputDevice::Start() { |
| 83 DCHECK(callback_) << "Initialize hasn't been called"; | 82 DCHECK(callback_) << "Initialize hasn't been called"; |
| 84 message_loop()->PostTask(FROM_HERE, | 83 task_runner()->PostTask(FROM_HERE, |
| 85 base::Bind(&AudioOutputDevice::CreateStreamOnIOThread, this, | 84 base::Bind(&AudioOutputDevice::CreateStreamOnIOThread, this, |
| 86 audio_parameters_)); | 85 audio_parameters_)); |
| 87 } | 86 } |
| 88 | 87 |
| 89 void AudioOutputDevice::Stop() { | 88 void AudioOutputDevice::Stop() { |
| 90 { | 89 { |
| 91 base::AutoLock auto_lock(audio_thread_lock_); | 90 base::AutoLock auto_lock(audio_thread_lock_); |
| 92 audio_thread_.Stop(base::MessageLoop::current()); | 91 audio_thread_.Stop(base::MessageLoop::current()); |
| 93 stopping_hack_ = true; | 92 stopping_hack_ = true; |
| 94 } | 93 } |
| 95 | 94 |
| 96 message_loop()->PostTask(FROM_HERE, | 95 task_runner()->PostTask(FROM_HERE, |
| 97 base::Bind(&AudioOutputDevice::ShutDownOnIOThread, this)); | 96 base::Bind(&AudioOutputDevice::ShutDownOnIOThread, this)); |
| 98 } | 97 } |
| 99 | 98 |
| 100 void AudioOutputDevice::Play() { | 99 void AudioOutputDevice::Play() { |
| 101 message_loop()->PostTask(FROM_HERE, | 100 task_runner()->PostTask(FROM_HERE, |
| 102 base::Bind(&AudioOutputDevice::PlayOnIOThread, this)); | 101 base::Bind(&AudioOutputDevice::PlayOnIOThread, this)); |
| 103 } | 102 } |
| 104 | 103 |
| 105 void AudioOutputDevice::Pause() { | 104 void AudioOutputDevice::Pause() { |
| 106 message_loop()->PostTask(FROM_HERE, | 105 task_runner()->PostTask(FROM_HERE, |
| 107 base::Bind(&AudioOutputDevice::PauseOnIOThread, this)); | 106 base::Bind(&AudioOutputDevice::PauseOnIOThread, this)); |
| 108 } | 107 } |
| 109 | 108 |
| 110 bool AudioOutputDevice::SetVolume(double volume) { | 109 bool AudioOutputDevice::SetVolume(double volume) { |
| 111 if (volume < 0 || volume > 1.0) | 110 if (volume < 0 || volume > 1.0) |
| 112 return false; | 111 return false; |
| 113 | 112 |
| 114 if (!message_loop()->PostTask(FROM_HERE, | 113 if (!task_runner()->PostTask(FROM_HERE, |
| 115 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) { | 114 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) { |
| 116 return false; | 115 return false; |
| 117 } | 116 } |
| 118 | 117 |
| 119 return true; | 118 return true; |
| 120 } | 119 } |
| 121 | 120 |
| 122 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) { | 121 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) { |
| 123 DCHECK(message_loop()->BelongsToCurrentThread()); | 122 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 124 if (state_ == IDLE) { | 123 if (state_ == IDLE) { |
| 125 state_ = CREATING_STREAM; | 124 state_ = CREATING_STREAM; |
| 126 ipc_->CreateStream(this, params, session_id_); | 125 ipc_->CreateStream(this, params, session_id_); |
| 127 } | 126 } |
| 128 } | 127 } |
| 129 | 128 |
| 130 void AudioOutputDevice::PlayOnIOThread() { | 129 void AudioOutputDevice::PlayOnIOThread() { |
| 131 DCHECK(message_loop()->BelongsToCurrentThread()); | 130 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 132 if (state_ == PAUSED) { | 131 if (state_ == PAUSED) { |
| 133 ipc_->PlayStream(); | 132 ipc_->PlayStream(); |
| 134 state_ = PLAYING; | 133 state_ = PLAYING; |
| 135 play_on_start_ = false; | 134 play_on_start_ = false; |
| 136 } else { | 135 } else { |
| 137 play_on_start_ = true; | 136 play_on_start_ = true; |
| 138 } | 137 } |
| 139 } | 138 } |
| 140 | 139 |
| 141 void AudioOutputDevice::PauseOnIOThread() { | 140 void AudioOutputDevice::PauseOnIOThread() { |
| 142 DCHECK(message_loop()->BelongsToCurrentThread()); | 141 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 143 if (state_ == PLAYING) { | 142 if (state_ == PLAYING) { |
| 144 ipc_->PauseStream(); | 143 ipc_->PauseStream(); |
| 145 state_ = PAUSED; | 144 state_ = PAUSED; |
| 146 } | 145 } |
| 147 play_on_start_ = false; | 146 play_on_start_ = false; |
| 148 } | 147 } |
| 149 | 148 |
| 150 void AudioOutputDevice::ShutDownOnIOThread() { | 149 void AudioOutputDevice::ShutDownOnIOThread() { |
| 151 DCHECK(message_loop()->BelongsToCurrentThread()); | 150 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 152 | 151 |
| 153 // Close the stream, if we haven't already. | 152 // Close the stream, if we haven't already. |
| 154 if (state_ >= CREATING_STREAM) { | 153 if (state_ >= CREATING_STREAM) { |
| 155 ipc_->CloseStream(); | 154 ipc_->CloseStream(); |
| 156 state_ = IDLE; | 155 state_ = IDLE; |
| 157 } | 156 } |
| 158 | 157 |
| 159 // We can run into an issue where ShutDownOnIOThread is called right after | 158 // We can run into an issue where ShutDownOnIOThread is called right after |
| 160 // OnStreamCreated is called in cases where Start/Stop are called before we | 159 // OnStreamCreated is called in cases where Start/Stop are called before we |
| 161 // get the OnStreamCreated callback. To handle that corner case, we call | 160 // get the OnStreamCreated callback. To handle that corner case, we call |
| 162 // Stop(). In most cases, the thread will already be stopped. | 161 // Stop(). In most cases, the thread will already be stopped. |
| 163 // | 162 // |
| 164 // Another situation is when the IO thread goes away before Stop() is called | 163 // Another situation is when the IO thread goes away before Stop() is called |
| 165 // in which case, we cannot use the message loop to close the thread handle | 164 // in which case, we cannot use the message loop to close the thread handle |
| 166 // and can't rely on the main thread existing either. | 165 // and can't rely on the main thread existing either. |
| 167 base::AutoLock auto_lock_(audio_thread_lock_); | 166 base::AutoLock auto_lock_(audio_thread_lock_); |
| 168 base::ThreadRestrictions::ScopedAllowIO allow_io; | 167 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 169 audio_thread_.Stop(NULL); | 168 audio_thread_.Stop(NULL); |
| 170 audio_callback_.reset(); | 169 audio_callback_.reset(); |
| 171 stopping_hack_ = false; | 170 stopping_hack_ = false; |
| 172 } | 171 } |
| 173 | 172 |
| 174 void AudioOutputDevice::SetVolumeOnIOThread(double volume) { | 173 void AudioOutputDevice::SetVolumeOnIOThread(double volume) { |
| 175 DCHECK(message_loop()->BelongsToCurrentThread()); | 174 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 176 if (state_ >= CREATING_STREAM) | 175 if (state_ >= CREATING_STREAM) |
| 177 ipc_->SetVolume(volume); | 176 ipc_->SetVolume(volume); |
| 178 } | 177 } |
| 179 | 178 |
| 180 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegate::State state) { | 179 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegate::State state) { |
| 181 DCHECK(message_loop()->BelongsToCurrentThread()); | 180 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 182 | 181 |
| 183 // Do nothing if the stream has been closed. | 182 // Do nothing if the stream has been closed. |
| 184 if (state_ < CREATING_STREAM) | 183 if (state_ < CREATING_STREAM) |
| 185 return; | 184 return; |
| 186 | 185 |
| 187 // TODO(miu): Clean-up inconsistent and incomplete handling here. | 186 // TODO(miu): Clean-up inconsistent and incomplete handling here. |
| 188 // http://crbug.com/180640 | 187 // http://crbug.com/180640 |
| 189 switch (state) { | 188 switch (state) { |
| 190 case AudioOutputIPCDelegate::kPlaying: | 189 case AudioOutputIPCDelegate::kPlaying: |
| 191 case AudioOutputIPCDelegate::kPaused: | 190 case AudioOutputIPCDelegate::kPaused: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 204 default: | 203 default: |
| 205 NOTREACHED(); | 204 NOTREACHED(); |
| 206 break; | 205 break; |
| 207 } | 206 } |
| 208 } | 207 } |
| 209 | 208 |
| 210 void AudioOutputDevice::OnStreamCreated( | 209 void AudioOutputDevice::OnStreamCreated( |
| 211 base::SharedMemoryHandle handle, | 210 base::SharedMemoryHandle handle, |
| 212 base::SyncSocket::Handle socket_handle, | 211 base::SyncSocket::Handle socket_handle, |
| 213 int length) { | 212 int length) { |
| 214 DCHECK(message_loop()->BelongsToCurrentThread()); | 213 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 215 #if defined(OS_WIN) | 214 #if defined(OS_WIN) |
| 216 DCHECK(handle); | 215 DCHECK(handle); |
| 217 DCHECK(socket_handle); | 216 DCHECK(socket_handle); |
| 218 #else | 217 #else |
| 219 DCHECK_GE(handle.fd, 0); | 218 DCHECK_GE(handle.fd, 0); |
| 220 DCHECK_GE(socket_handle, 0); | 219 DCHECK_GE(socket_handle, 0); |
| 221 #endif | 220 #endif |
| 222 DCHECK_GT(length, 0); | 221 DCHECK_GT(length, 0); |
| 223 | 222 |
| 224 if (state_ != CREATING_STREAM) | 223 if (state_ != CREATING_STREAM) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 247 audio_callback_.get(), socket_handle, "AudioOutputDevice", true); | 246 audio_callback_.get(), socket_handle, "AudioOutputDevice", true); |
| 248 state_ = PAUSED; | 247 state_ = PAUSED; |
| 249 | 248 |
| 250 // We handle the case where Play() and/or Pause() may have been called | 249 // We handle the case where Play() and/or Pause() may have been called |
| 251 // multiple times before OnStreamCreated() gets called. | 250 // multiple times before OnStreamCreated() gets called. |
| 252 if (play_on_start_) | 251 if (play_on_start_) |
| 253 PlayOnIOThread(); | 252 PlayOnIOThread(); |
| 254 } | 253 } |
| 255 | 254 |
| 256 void AudioOutputDevice::OnIPCClosed() { | 255 void AudioOutputDevice::OnIPCClosed() { |
| 257 DCHECK(message_loop()->BelongsToCurrentThread()); | 256 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 258 state_ = IPC_CLOSED; | 257 state_ = IPC_CLOSED; |
| 259 ipc_.reset(); | 258 ipc_.reset(); |
| 260 } | 259 } |
| 261 | 260 |
| 262 void AudioOutputDevice::WillDestroyCurrentMessageLoop() { | 261 void AudioOutputDevice::WillDestroyCurrentMessageLoop() { |
| 263 LOG(ERROR) << "IO loop going away before the audio device has been stopped"; | 262 LOG(ERROR) << "IO loop going away before the audio device has been stopped"; |
| 264 ShutDownOnIOThread(); | 263 ShutDownOnIOThread(); |
| 265 } | 264 } |
| 266 | 265 |
| 267 // AudioOutputDevice::AudioThreadCallback | 266 // AudioOutputDevice::AudioThreadCallback |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 int input_channels = audio_parameters_.input_channels(); | 318 int input_channels = audio_parameters_.input_channels(); |
| 320 if (input_bus_ && input_channels > 0) { | 319 if (input_bus_ && input_channels > 0) { |
| 321 render_callback_->RenderIO( | 320 render_callback_->RenderIO( |
| 322 input_bus_.get(), output_bus_.get(), audio_delay_milliseconds); | 321 input_bus_.get(), output_bus_.get(), audio_delay_milliseconds); |
| 323 } else { | 322 } else { |
| 324 render_callback_->Render(output_bus_.get(), audio_delay_milliseconds); | 323 render_callback_->Render(output_bus_.get(), audio_delay_milliseconds); |
| 325 } | 324 } |
| 326 } | 325 } |
| 327 | 326 |
| 328 } // namespace media. | 327 } // namespace media. |
| OLD | NEW |