Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / media / audio / audio_output_device.cc
blob3213400ff344b277eaf8f3c5de4dfccdbbce619e
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
3 // found in the LICENSE file.
5 #include "media/audio/audio_output_device.h"
7 #include <string>
9 #include "base/callback_helpers.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "base/time/time.h"
12 #include "base/trace_event/trace_event.h"
13 #include "media/audio/audio_output_controller.h"
14 #include "media/base/limits.h"
16 namespace media {
18 // Takes care of invoking the render callback on the audio thread.
19 // An instance of this class is created for each capture stream in
20 // OnStreamCreated().
21 class AudioOutputDevice::AudioThreadCallback
22 : public AudioDeviceThread::Callback {
23 public:
24 AudioThreadCallback(const AudioParameters& audio_parameters,
25 base::SharedMemoryHandle memory,
26 int memory_length,
27 AudioRendererSink::RenderCallback* render_callback);
28 ~AudioThreadCallback() override;
30 void MapSharedMemory() override;
32 // Called whenever we receive notifications about pending data.
33 void Process(uint32 pending_data) override;
35 private:
36 AudioRendererSink::RenderCallback* render_callback_;
37 scoped_ptr<AudioBus> output_bus_;
38 uint64 callback_num_;
39 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback);
42 AudioOutputDevice::AudioOutputDevice(
43 scoped_ptr<AudioOutputIPC> ipc,
44 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
45 : ScopedTaskRunnerObserver(io_task_runner),
46 callback_(NULL),
47 ipc_(ipc.Pass()),
48 state_(IDLE),
49 play_on_start_(true),
50 session_id_(-1),
51 stopping_hack_(false),
52 current_switch_request_id_(0) {
53 CHECK(ipc_);
55 // The correctness of the code depends on the relative values assigned in the
56 // State enum.
57 static_assert(IPC_CLOSED < IDLE, "invalid enum value assignment 0");
58 static_assert(IDLE < CREATING_STREAM, "invalid enum value assignment 1");
59 static_assert(CREATING_STREAM < PAUSED, "invalid enum value assignment 2");
60 static_assert(PAUSED < PLAYING, "invalid enum value assignment 3");
63 void AudioOutputDevice::InitializeWithSessionId(const AudioParameters& params,
64 RenderCallback* callback,
65 int session_id) {
66 DCHECK(!callback_) << "Calling InitializeWithSessionId() twice?";
67 DCHECK(params.IsValid());
68 audio_parameters_ = params;
69 callback_ = callback;
70 session_id_ = session_id;
73 void AudioOutputDevice::Initialize(const AudioParameters& params,
74 RenderCallback* callback) {
75 InitializeWithSessionId(params, callback, 0);
78 AudioOutputDevice::~AudioOutputDevice() {
79 // The current design requires that the user calls Stop() before deleting
80 // this class.
81 DCHECK(audio_thread_.IsStopped());
83 // The following makes it possible for |current_switch_callback_| to release
84 // its bound parameters in the correct thread instead of implicitly releasing
85 // them in the thread where this destructor runs.
86 if (!current_switch_callback_.is_null()) {
87 base::ResetAndReturn(&current_switch_callback_).Run(
88 SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE);
92 void AudioOutputDevice::Start() {
93 DCHECK(callback_) << "Initialize hasn't been called";
94 task_runner()->PostTask(FROM_HERE,
95 base::Bind(&AudioOutputDevice::CreateStreamOnIOThread, this,
96 audio_parameters_));
99 void AudioOutputDevice::Stop() {
101 base::AutoLock auto_lock(audio_thread_lock_);
102 audio_thread_.Stop(base::MessageLoop::current());
103 stopping_hack_ = true;
106 task_runner()->PostTask(FROM_HERE,
107 base::Bind(&AudioOutputDevice::ShutDownOnIOThread, this));
110 void AudioOutputDevice::Play() {
111 task_runner()->PostTask(FROM_HERE,
112 base::Bind(&AudioOutputDevice::PlayOnIOThread, this));
115 void AudioOutputDevice::Pause() {
116 task_runner()->PostTask(FROM_HERE,
117 base::Bind(&AudioOutputDevice::PauseOnIOThread, this));
120 bool AudioOutputDevice::SetVolume(double volume) {
121 if (volume < 0 || volume > 1.0)
122 return false;
124 if (!task_runner()->PostTask(FROM_HERE,
125 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) {
126 return false;
129 return true;
132 void AudioOutputDevice::SwitchOutputDevice(
133 const std::string& device_id,
134 const GURL& security_origin,
135 const SwitchOutputDeviceCB& callback) {
136 DVLOG(1) << __FUNCTION__ << "(" << device_id << ")";
137 task_runner()->PostTask(
138 FROM_HERE, base::Bind(&AudioOutputDevice::SwitchOutputDeviceOnIOThread,
139 this, device_id, security_origin, callback));
142 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) {
143 DCHECK(task_runner()->BelongsToCurrentThread());
144 if (state_ == IDLE) {
145 state_ = CREATING_STREAM;
146 ipc_->CreateStream(this, params, session_id_);
150 void AudioOutputDevice::PlayOnIOThread() {
151 DCHECK(task_runner()->BelongsToCurrentThread());
152 if (state_ == PAUSED) {
153 TRACE_EVENT_ASYNC_BEGIN0(
154 "audio", "StartingPlayback", audio_callback_.get());
155 ipc_->PlayStream();
156 state_ = PLAYING;
157 play_on_start_ = false;
158 } else {
159 play_on_start_ = true;
163 void AudioOutputDevice::PauseOnIOThread() {
164 DCHECK(task_runner()->BelongsToCurrentThread());
165 if (state_ == PLAYING) {
166 TRACE_EVENT_ASYNC_END0(
167 "audio", "StartingPlayback", audio_callback_.get());
168 ipc_->PauseStream();
169 state_ = PAUSED;
171 play_on_start_ = false;
174 void AudioOutputDevice::ShutDownOnIOThread() {
175 DCHECK(task_runner()->BelongsToCurrentThread());
177 // Close the stream, if we haven't already.
178 if (state_ >= CREATING_STREAM) {
179 ipc_->CloseStream();
180 state_ = IDLE;
183 // We can run into an issue where ShutDownOnIOThread is called right after
184 // OnStreamCreated is called in cases where Start/Stop are called before we
185 // get the OnStreamCreated callback. To handle that corner case, we call
186 // Stop(). In most cases, the thread will already be stopped.
188 // Another situation is when the IO thread goes away before Stop() is called
189 // in which case, we cannot use the message loop to close the thread handle
190 // and can't rely on the main thread existing either.
191 base::AutoLock auto_lock_(audio_thread_lock_);
192 base::ThreadRestrictions::ScopedAllowIO allow_io;
193 audio_thread_.Stop(NULL);
194 audio_callback_.reset();
195 stopping_hack_ = false;
198 void AudioOutputDevice::SetVolumeOnIOThread(double volume) {
199 DCHECK(task_runner()->BelongsToCurrentThread());
200 if (state_ >= CREATING_STREAM)
201 ipc_->SetVolume(volume);
204 void AudioOutputDevice::SwitchOutputDeviceOnIOThread(
205 const std::string& device_id,
206 const GURL& security_origin,
207 const SwitchOutputDeviceCB& callback) {
208 DCHECK(task_runner()->BelongsToCurrentThread());
209 DVLOG(1) << __FUNCTION__ << "(" << device_id << "," << security_origin << ")";
210 if (state_ >= CREATING_STREAM) {
211 SetCurrentSwitchRequest(callback);
212 ipc_->SwitchOutputDevice(device_id, security_origin,
213 current_switch_request_id_);
214 } else {
215 callback.Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED);
219 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegateState state) {
220 DCHECK(task_runner()->BelongsToCurrentThread());
222 // Do nothing if the stream has been closed.
223 if (state_ < CREATING_STREAM)
224 return;
226 // TODO(miu): Clean-up inconsistent and incomplete handling here.
227 // http://crbug.com/180640
228 switch (state) {
229 case AUDIO_OUTPUT_IPC_DELEGATE_STATE_PLAYING:
230 case AUDIO_OUTPUT_IPC_DELEGATE_STATE_PAUSED:
231 break;
232 case AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR:
233 DLOG(WARNING) << "AudioOutputDevice::OnStateChanged(ERROR)";
234 // Don't dereference the callback object if the audio thread
235 // is stopped or stopping. That could mean that the callback
236 // object has been deleted.
237 // TODO(tommi): Add an explicit contract for clearing the callback
238 // object. Possibly require calling Initialize again or provide
239 // a callback object via Start() and clear it in Stop().
240 if (!audio_thread_.IsStopped())
241 callback_->OnRenderError();
242 break;
243 default:
244 NOTREACHED();
245 break;
249 void AudioOutputDevice::OnStreamCreated(
250 base::SharedMemoryHandle handle,
251 base::SyncSocket::Handle socket_handle,
252 int length) {
253 DCHECK(task_runner()->BelongsToCurrentThread());
254 DCHECK(base::SharedMemory::IsHandleValid(handle));
255 #if defined(OS_WIN)
256 DCHECK(socket_handle);
257 #else
258 DCHECK_GE(socket_handle, 0);
259 #endif
260 DCHECK_GT(length, 0);
262 if (state_ != CREATING_STREAM)
263 return;
265 // We can receive OnStreamCreated() on the IO thread after the client has
266 // called Stop() but before ShutDownOnIOThread() is processed. In such a
267 // situation |callback_| might point to freed memory. Instead of starting
268 // |audio_thread_| do nothing and wait for ShutDownOnIOThread() to get called.
270 // TODO(scherkus): The real fix is to have sane ownership semantics. The fact
271 // that |callback_| (which should own and outlive this object!) can point to
272 // freed memory is a mess. AudioRendererSink should be non-refcounted so that
273 // owners (WebRtcAudioDeviceImpl, AudioRendererImpl, etc...) can Stop() and
274 // delete as they see fit. AudioOutputDevice should internally use WeakPtr
275 // to handle teardown and thread hopping. See http://crbug.com/151051 for
276 // details.
277 base::AutoLock auto_lock(audio_thread_lock_);
278 if (stopping_hack_)
279 return;
281 DCHECK(audio_thread_.IsStopped());
282 audio_callback_.reset(new AudioOutputDevice::AudioThreadCallback(
283 audio_parameters_, handle, length, callback_));
284 audio_thread_.Start(
285 audio_callback_.get(), socket_handle, "AudioOutputDevice", true);
286 state_ = PAUSED;
288 // We handle the case where Play() and/or Pause() may have been called
289 // multiple times before OnStreamCreated() gets called.
290 if (play_on_start_)
291 PlayOnIOThread();
294 void AudioOutputDevice::SetCurrentSwitchRequest(
295 const SwitchOutputDeviceCB& callback) {
296 DCHECK(task_runner()->BelongsToCurrentThread());
297 DVLOG(1) << __FUNCTION__;
298 // If there is a previous unresolved request, resolve it as obsolete
299 if (!current_switch_callback_.is_null()) {
300 base::ResetAndReturn(&current_switch_callback_).Run(
301 SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE);
303 current_switch_callback_ = callback;
304 current_switch_request_id_++;
307 void AudioOutputDevice::OnOutputDeviceSwitched(
308 int request_id,
309 SwitchOutputDeviceResult result) {
310 DCHECK(task_runner()->BelongsToCurrentThread());
311 DCHECK(request_id <= current_switch_request_id_);
312 DVLOG(1) << __FUNCTION__
313 << "(" << request_id << ", " << result << ")";
314 if (request_id != current_switch_request_id_) {
315 return;
317 base::ResetAndReturn(&current_switch_callback_).Run(result);
320 void AudioOutputDevice::OnIPCClosed() {
321 DCHECK(task_runner()->BelongsToCurrentThread());
322 state_ = IPC_CLOSED;
323 ipc_.reset();
326 void AudioOutputDevice::WillDestroyCurrentMessageLoop() {
327 LOG(ERROR) << "IO loop going away before the audio device has been stopped";
328 ShutDownOnIOThread();
331 // AudioOutputDevice::AudioThreadCallback
333 AudioOutputDevice::AudioThreadCallback::AudioThreadCallback(
334 const AudioParameters& audio_parameters,
335 base::SharedMemoryHandle memory,
336 int memory_length,
337 AudioRendererSink::RenderCallback* render_callback)
338 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length, 1),
339 render_callback_(render_callback),
340 callback_num_(0) {}
342 AudioOutputDevice::AudioThreadCallback::~AudioThreadCallback() {
345 void AudioOutputDevice::AudioThreadCallback::MapSharedMemory() {
346 CHECK_EQ(total_segments_, 1);
347 CHECK(shared_memory_.Map(memory_length_));
348 DCHECK_EQ(memory_length_, AudioBus::CalculateMemorySize(audio_parameters_));
350 output_bus_ =
351 AudioBus::WrapMemory(audio_parameters_, shared_memory_.memory());
354 // Called whenever we receive notifications about pending data.
355 void AudioOutputDevice::AudioThreadCallback::Process(uint32 pending_data) {
356 // Convert the number of pending bytes in the render buffer into milliseconds.
357 int audio_delay_milliseconds = pending_data / bytes_per_ms_;
359 callback_num_++;
360 TRACE_EVENT1("audio", "AudioOutputDevice::FireRenderCallback",
361 "callback_num", callback_num_);
363 // When playback starts, we get an immediate callback to Process to make sure
364 // that we have some data, we'll get another one after the device is awake and
365 // ingesting data, which is what we want to track with this trace.
366 if (callback_num_ == 2) {
367 TRACE_EVENT_ASYNC_END0("audio", "StartingPlayback", this);
370 // Update the audio-delay measurement then ask client to render audio. Since
371 // |output_bus_| is wrapping the shared memory the Render() call is writing
372 // directly into the shared memory.
373 render_callback_->Render(output_bus_.get(), audio_delay_milliseconds);
376 } // namespace media.