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 OutputDevice
* AudioOutputDevice::GetOutputDevice() {
136 void AudioOutputDevice::SwitchOutputDevice(
137 const std::string
& device_id
,
138 const GURL
& security_origin
,
139 const SwitchOutputDeviceCB
& callback
) {
140 DVLOG(1) << __FUNCTION__
<< "(" << device_id
<< ")";
141 task_runner()->PostTask(
142 FROM_HERE
, base::Bind(&AudioOutputDevice::SwitchOutputDeviceOnIOThread
,
143 this, device_id
, security_origin
, callback
));
146 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters
& params
) {
147 DCHECK(task_runner()->BelongsToCurrentThread());
148 if (state_
== IDLE
) {
149 state_
= CREATING_STREAM
;
150 ipc_
->CreateStream(this, params
, session_id_
);
154 void AudioOutputDevice::PlayOnIOThread() {
155 DCHECK(task_runner()->BelongsToCurrentThread());
156 if (state_
== PAUSED
) {
157 TRACE_EVENT_ASYNC_BEGIN0(
158 "audio", "StartingPlayback", audio_callback_
.get());
161 play_on_start_
= false;
163 play_on_start_
= true;
167 void AudioOutputDevice::PauseOnIOThread() {
168 DCHECK(task_runner()->BelongsToCurrentThread());
169 if (state_
== PLAYING
) {
170 TRACE_EVENT_ASYNC_END0(
171 "audio", "StartingPlayback", audio_callback_
.get());
175 play_on_start_
= false;
178 void AudioOutputDevice::ShutDownOnIOThread() {
179 DCHECK(task_runner()->BelongsToCurrentThread());
181 // Close the stream, if we haven't already.
182 if (state_
>= CREATING_STREAM
) {
187 // We can run into an issue where ShutDownOnIOThread is called right after
188 // OnStreamCreated is called in cases where Start/Stop are called before we
189 // get the OnStreamCreated callback. To handle that corner case, we call
190 // Stop(). In most cases, the thread will already be stopped.
192 // Another situation is when the IO thread goes away before Stop() is called
193 // in which case, we cannot use the message loop to close the thread handle
194 // and can't rely on the main thread existing either.
195 base::AutoLock
auto_lock_(audio_thread_lock_
);
196 base::ThreadRestrictions::ScopedAllowIO allow_io
;
197 audio_thread_
.Stop(NULL
);
198 audio_callback_
.reset();
199 stopping_hack_
= false;
202 void AudioOutputDevice::SetVolumeOnIOThread(double volume
) {
203 DCHECK(task_runner()->BelongsToCurrentThread());
204 if (state_
>= CREATING_STREAM
)
205 ipc_
->SetVolume(volume
);
208 void AudioOutputDevice::SwitchOutputDeviceOnIOThread(
209 const std::string
& device_id
,
210 const GURL
& security_origin
,
211 const SwitchOutputDeviceCB
& callback
) {
212 DCHECK(task_runner()->BelongsToCurrentThread());
213 DVLOG(1) << __FUNCTION__
<< "(" << device_id
<< "," << security_origin
<< ")";
214 if (state_
>= CREATING_STREAM
) {
215 SetCurrentSwitchRequest(callback
);
216 ipc_
->SwitchOutputDevice(device_id
, security_origin
,
217 current_switch_request_id_
);
219 callback
.Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED
);
223 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegateState state
) {
224 DCHECK(task_runner()->BelongsToCurrentThread());
226 // Do nothing if the stream has been closed.
227 if (state_
< CREATING_STREAM
)
230 // TODO(miu): Clean-up inconsistent and incomplete handling here.
231 // http://crbug.com/180640
233 case AUDIO_OUTPUT_IPC_DELEGATE_STATE_PLAYING
:
234 case AUDIO_OUTPUT_IPC_DELEGATE_STATE_PAUSED
:
236 case AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR
:
237 DLOG(WARNING
) << "AudioOutputDevice::OnStateChanged(ERROR)";
238 // Don't dereference the callback object if the audio thread
239 // is stopped or stopping. That could mean that the callback
240 // object has been deleted.
241 // TODO(tommi): Add an explicit contract for clearing the callback
242 // object. Possibly require calling Initialize again or provide
243 // a callback object via Start() and clear it in Stop().
244 if (!audio_thread_
.IsStopped())
245 callback_
->OnRenderError();
253 void AudioOutputDevice::OnStreamCreated(
254 base::SharedMemoryHandle handle
,
255 base::SyncSocket::Handle socket_handle
,
257 DCHECK(task_runner()->BelongsToCurrentThread());
258 DCHECK(base::SharedMemory::IsHandleValid(handle
));
260 DCHECK(socket_handle
);
262 DCHECK_GE(socket_handle
, 0);
264 DCHECK_GT(length
, 0);
266 if (state_
!= CREATING_STREAM
)
269 // We can receive OnStreamCreated() on the IO thread after the client has
270 // called Stop() but before ShutDownOnIOThread() is processed. In such a
271 // situation |callback_| might point to freed memory. Instead of starting
272 // |audio_thread_| do nothing and wait for ShutDownOnIOThread() to get called.
274 // TODO(scherkus): The real fix is to have sane ownership semantics. The fact
275 // that |callback_| (which should own and outlive this object!) can point to
276 // freed memory is a mess. AudioRendererSink should be non-refcounted so that
277 // owners (WebRtcAudioDeviceImpl, AudioRendererImpl, etc...) can Stop() and
278 // delete as they see fit. AudioOutputDevice should internally use WeakPtr
279 // to handle teardown and thread hopping. See http://crbug.com/151051 for
281 base::AutoLock
auto_lock(audio_thread_lock_
);
285 DCHECK(audio_thread_
.IsStopped());
286 audio_callback_
.reset(new AudioOutputDevice::AudioThreadCallback(
287 audio_parameters_
, handle
, length
, callback_
));
289 audio_callback_
.get(), socket_handle
, "AudioOutputDevice", true);
292 // We handle the case where Play() and/or Pause() may have been called
293 // multiple times before OnStreamCreated() gets called.
298 void AudioOutputDevice::SetCurrentSwitchRequest(
299 const SwitchOutputDeviceCB
& callback
) {
300 DCHECK(task_runner()->BelongsToCurrentThread());
301 DVLOG(1) << __FUNCTION__
;
302 // If there is a previous unresolved request, resolve it as obsolete
303 if (!current_switch_callback_
.is_null()) {
304 base::ResetAndReturn(¤t_switch_callback_
).Run(
305 SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE
);
307 current_switch_callback_
= callback
;
308 current_switch_request_id_
++;
311 void AudioOutputDevice::OnOutputDeviceSwitched(
313 SwitchOutputDeviceResult result
) {
314 DCHECK(task_runner()->BelongsToCurrentThread());
315 DCHECK(request_id
<= current_switch_request_id_
);
316 DVLOG(1) << __FUNCTION__
317 << "(" << request_id
<< ", " << result
<< ")";
318 if (request_id
!= current_switch_request_id_
) {
321 base::ResetAndReturn(¤t_switch_callback_
).Run(result
);
324 void AudioOutputDevice::OnIPCClosed() {
325 DCHECK(task_runner()->BelongsToCurrentThread());
330 void AudioOutputDevice::WillDestroyCurrentMessageLoop() {
331 LOG(ERROR
) << "IO loop going away before the audio device has been stopped";
332 ShutDownOnIOThread();
335 // AudioOutputDevice::AudioThreadCallback
337 AudioOutputDevice::AudioThreadCallback::AudioThreadCallback(
338 const AudioParameters
& audio_parameters
,
339 base::SharedMemoryHandle memory
,
341 AudioRendererSink::RenderCallback
* render_callback
)
342 : AudioDeviceThread::Callback(audio_parameters
, memory
, memory_length
, 1),
343 render_callback_(render_callback
),
346 AudioOutputDevice::AudioThreadCallback::~AudioThreadCallback() {
349 void AudioOutputDevice::AudioThreadCallback::MapSharedMemory() {
350 CHECK_EQ(total_segments_
, 1);
351 CHECK(shared_memory_
.Map(memory_length_
));
352 DCHECK_EQ(memory_length_
, AudioBus::CalculateMemorySize(audio_parameters_
));
355 AudioBus::WrapMemory(audio_parameters_
, shared_memory_
.memory());
358 // Called whenever we receive notifications about pending data.
359 void AudioOutputDevice::AudioThreadCallback::Process(uint32 pending_data
) {
360 // Convert the number of pending bytes in the render buffer into milliseconds.
361 int audio_delay_milliseconds
= pending_data
/ bytes_per_ms_
;
364 TRACE_EVENT1("audio", "AudioOutputDevice::FireRenderCallback",
365 "callback_num", callback_num_
);
367 // When playback starts, we get an immediate callback to Process to make sure
368 // that we have some data, we'll get another one after the device is awake and
369 // ingesting data, which is what we want to track with this trace.
370 if (callback_num_
== 2) {
371 TRACE_EVENT_ASYNC_END0("audio", "StartingPlayback", this);
374 // Update the audio-delay measurement then ask client to render audio. Since
375 // |output_bus_| is wrapping the shared memory the Render() call is writing
376 // directly into the shared memory.
377 render_callback_
->Render(output_bus_
.get(), audio_delay_milliseconds
);
380 } // namespace media.