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"
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"
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
21 class AudioOutputDevice::AudioThreadCallback
22 : public AudioDeviceThread::Callback
{
24 AudioThreadCallback(const AudioParameters
& audio_parameters
,
25 base::SharedMemoryHandle memory
,
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
;
36 AudioRendererSink::RenderCallback
* render_callback_
;
37 scoped_ptr
<AudioBus
> output_bus_
;
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
),
51 stopping_hack_(false),
52 current_switch_request_id_(0) {
55 // The correctness of the code depends on the relative values assigned in the
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
,
66 DCHECK(!callback_
) << "Calling InitializeWithSessionId() twice?";
67 DCHECK(params
.IsValid());
68 audio_parameters_
= params
;
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
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(¤t_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,
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)
124 if (!task_runner()->PostTask(FROM_HERE
,
125 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread
, this, volume
))) {
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());
157 play_on_start_
= false;
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());
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
) {
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_
);
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
)
226 // TODO(miu): Clean-up inconsistent and incomplete handling here.
227 // http://crbug.com/180640
229 case AUDIO_OUTPUT_IPC_DELEGATE_STATE_PLAYING
:
230 case AUDIO_OUTPUT_IPC_DELEGATE_STATE_PAUSED
:
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();
249 void AudioOutputDevice::OnStreamCreated(
250 base::SharedMemoryHandle handle
,
251 base::SyncSocket::Handle socket_handle
,
253 DCHECK(task_runner()->BelongsToCurrentThread());
254 DCHECK(base::SharedMemory::IsHandleValid(handle
));
256 DCHECK(socket_handle
);
258 DCHECK_GE(socket_handle
, 0);
260 DCHECK_GT(length
, 0);
262 if (state_
!= CREATING_STREAM
)
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
277 base::AutoLock
auto_lock(audio_thread_lock_
);
281 DCHECK(audio_thread_
.IsStopped());
282 audio_callback_
.reset(new AudioOutputDevice::AudioThreadCallback(
283 audio_parameters_
, handle
, length
, callback_
));
285 audio_callback_
.get(), socket_handle
, "AudioOutputDevice", true);
288 // We handle the case where Play() and/or Pause() may have been called
289 // multiple times before OnStreamCreated() gets called.
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(¤t_switch_callback_
).Run(
301 SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE
);
303 current_switch_callback_
= callback
;
304 current_switch_request_id_
++;
307 void AudioOutputDevice::OnOutputDeviceSwitched(
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_
) {
317 base::ResetAndReturn(¤t_switch_callback_
).Run(result
);
320 void AudioOutputDevice::OnIPCClosed() {
321 DCHECK(task_runner()->BelongsToCurrentThread());
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
,
337 AudioRendererSink::RenderCallback
* render_callback
)
338 : AudioDeviceThread::Callback(audio_parameters
, memory
, memory_length
, 1),
339 render_callback_(render_callback
),
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_
));
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_
;
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.