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 "base/basictypes.h"
8 #include "base/debug/trace_event.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "base/time/time.h"
12 #include "media/audio/audio_output_controller.h"
13 #include "media/audio/shared_memory_util.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 virtual ~AudioThreadCallback();
30 virtual void MapSharedMemory() OVERRIDE
;
32 // Called whenever we receive notifications about pending data.
33 virtual void Process(int pending_data
) OVERRIDE
;
36 AudioRendererSink::RenderCallback
* render_callback_
;
37 scoped_ptr
<AudioBus
> input_bus_
;
38 scoped_ptr
<AudioBus
> output_bus_
;
39 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback
);
42 AudioOutputDevice::AudioOutputDevice(
43 scoped_ptr
<AudioOutputIPC
> ipc
,
44 const scoped_refptr
<base::MessageLoopProxy
>& io_loop
)
45 : ScopedLoopObserver(io_loop
),
51 stopping_hack_(false) {
54 // The correctness of the code depends on the relative values assigned in the
56 COMPILE_ASSERT(IPC_CLOSED
< IDLE
, invalid_enum_value_assignment_0
);
57 COMPILE_ASSERT(IDLE
< CREATING_STREAM
, invalid_enum_value_assignment_1
);
58 COMPILE_ASSERT(CREATING_STREAM
< PAUSED
, invalid_enum_value_assignment_2
);
59 COMPILE_ASSERT(PAUSED
< PLAYING
, invalid_enum_value_assignment_3
);
62 void AudioOutputDevice::InitializeUnifiedStream(const AudioParameters
& params
,
63 RenderCallback
* callback
,
65 DCHECK(!callback_
) << "Calling InitializeUnifiedStream() twice?";
66 DCHECK(params
.IsValid());
67 audio_parameters_
= params
;
69 session_id_
= session_id
;
72 void AudioOutputDevice::Initialize(const AudioParameters
& params
,
73 RenderCallback
* callback
) {
74 InitializeUnifiedStream(params
, callback
, 0);
77 AudioOutputDevice::~AudioOutputDevice() {
78 // The current design requires that the user calls Stop() before deleting
80 DCHECK(audio_thread_
.IsStopped());
83 void AudioOutputDevice::Start() {
84 DCHECK(callback_
) << "Initialize hasn't been called";
85 message_loop()->PostTask(FROM_HERE
,
86 base::Bind(&AudioOutputDevice::CreateStreamOnIOThread
, this,
90 void AudioOutputDevice::Stop() {
92 base::AutoLock
auto_lock(audio_thread_lock_
);
93 audio_thread_
.Stop(base::MessageLoop::current());
94 stopping_hack_
= true;
97 message_loop()->PostTask(FROM_HERE
,
98 base::Bind(&AudioOutputDevice::ShutDownOnIOThread
, this));
101 void AudioOutputDevice::Play() {
102 message_loop()->PostTask(FROM_HERE
,
103 base::Bind(&AudioOutputDevice::PlayOnIOThread
, this));
106 void AudioOutputDevice::Pause() {
107 message_loop()->PostTask(FROM_HERE
,
108 base::Bind(&AudioOutputDevice::PauseOnIOThread
, this));
111 bool AudioOutputDevice::SetVolume(double volume
) {
112 if (volume
< 0 || volume
> 1.0)
115 if (!message_loop()->PostTask(FROM_HERE
,
116 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread
, this, volume
))) {
123 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters
& params
) {
124 DCHECK(message_loop()->BelongsToCurrentThread());
125 if (state_
== IDLE
) {
126 state_
= CREATING_STREAM
;
127 ipc_
->CreateStream(this, params
, session_id_
);
131 void AudioOutputDevice::PlayOnIOThread() {
132 DCHECK(message_loop()->BelongsToCurrentThread());
133 if (state_
== PAUSED
) {
136 play_on_start_
= false;
138 play_on_start_
= true;
142 void AudioOutputDevice::PauseOnIOThread() {
143 DCHECK(message_loop()->BelongsToCurrentThread());
144 if (state_
== PLAYING
) {
148 play_on_start_
= false;
151 void AudioOutputDevice::ShutDownOnIOThread() {
152 DCHECK(message_loop()->BelongsToCurrentThread());
154 // Close the stream, if we haven't already.
155 if (state_
>= CREATING_STREAM
) {
160 // We can run into an issue where ShutDownOnIOThread is called right after
161 // OnStreamCreated is called in cases where Start/Stop are called before we
162 // get the OnStreamCreated callback. To handle that corner case, we call
163 // Stop(). In most cases, the thread will already be stopped.
165 // Another situation is when the IO thread goes away before Stop() is called
166 // in which case, we cannot use the message loop to close the thread handle
167 // and can't rely on the main thread existing either.
168 base::AutoLock
auto_lock_(audio_thread_lock_
);
169 base::ThreadRestrictions::ScopedAllowIO allow_io
;
170 audio_thread_
.Stop(NULL
);
171 audio_callback_
.reset();
172 stopping_hack_
= false;
175 void AudioOutputDevice::SetVolumeOnIOThread(double volume
) {
176 DCHECK(message_loop()->BelongsToCurrentThread());
177 if (state_
>= CREATING_STREAM
)
178 ipc_
->SetVolume(volume
);
181 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegate::State state
) {
182 DCHECK(message_loop()->BelongsToCurrentThread());
184 // Do nothing if the stream has been closed.
185 if (state_
< CREATING_STREAM
)
188 // TODO(miu): Clean-up inconsistent and incomplete handling here.
189 // http://crbug.com/180640
191 case AudioOutputIPCDelegate::kPlaying
:
192 case AudioOutputIPCDelegate::kPaused
:
194 case AudioOutputIPCDelegate::kError
:
195 DLOG(WARNING
) << "AudioOutputDevice::OnStateChanged(kError)";
196 // Don't dereference the callback object if the audio thread
197 // is stopped or stopping. That could mean that the callback
198 // object has been deleted.
199 // TODO(tommi): Add an explicit contract for clearing the callback
200 // object. Possibly require calling Initialize again or provide
201 // a callback object via Start() and clear it in Stop().
202 if (!audio_thread_
.IsStopped())
203 callback_
->OnRenderError();
211 void AudioOutputDevice::OnStreamCreated(
212 base::SharedMemoryHandle handle
,
213 base::SyncSocket::Handle socket_handle
,
215 DCHECK(message_loop()->BelongsToCurrentThread());
218 DCHECK(socket_handle
);
220 DCHECK_GE(handle
.fd
, 0);
221 DCHECK_GE(socket_handle
, 0);
223 DCHECK_GT(length
, 0);
225 if (state_
!= CREATING_STREAM
)
228 // We can receive OnStreamCreated() on the IO thread after the client has
229 // called Stop() but before ShutDownOnIOThread() is processed. In such a
230 // situation |callback_| might point to freed memory. Instead of starting
231 // |audio_thread_| do nothing and wait for ShutDownOnIOThread() to get called.
233 // TODO(scherkus): The real fix is to have sane ownership semantics. The fact
234 // that |callback_| (which should own and outlive this object!) can point to
235 // freed memory is a mess. AudioRendererSink should be non-refcounted so that
236 // owners (WebRtcAudioDeviceImpl, AudioRendererImpl, etc...) can Stop() and
237 // delete as they see fit. AudioOutputDevice should internally use WeakPtr
238 // to handle teardown and thread hopping. See http://crbug.com/151051 for
240 base::AutoLock
auto_lock(audio_thread_lock_
);
244 DCHECK(audio_thread_
.IsStopped());
245 audio_callback_
.reset(new AudioOutputDevice::AudioThreadCallback(
246 audio_parameters_
, handle
, length
, callback_
));
247 audio_thread_
.Start(audio_callback_
.get(), socket_handle
,
248 "AudioOutputDevice");
251 // We handle the case where Play() and/or Pause() may have been called
252 // multiple times before OnStreamCreated() gets called.
257 void AudioOutputDevice::OnIPCClosed() {
258 DCHECK(message_loop()->BelongsToCurrentThread());
263 void AudioOutputDevice::WillDestroyCurrentMessageLoop() {
264 LOG(ERROR
) << "IO loop going away before the audio device has been stopped";
265 ShutDownOnIOThread();
268 // AudioOutputDevice::AudioThreadCallback
270 AudioOutputDevice::AudioThreadCallback::AudioThreadCallback(
271 const AudioParameters
& audio_parameters
,
272 base::SharedMemoryHandle memory
,
274 AudioRendererSink::RenderCallback
* render_callback
)
275 : AudioDeviceThread::Callback(audio_parameters
,
279 render_callback_(render_callback
) {
282 AudioOutputDevice::AudioThreadCallback::~AudioThreadCallback() {
285 void AudioOutputDevice::AudioThreadCallback::MapSharedMemory() {
286 CHECK_EQ(total_segments_
, 1);
287 CHECK(shared_memory_
.Map(TotalSharedMemorySizeInBytes(memory_length_
)));
289 // Calculate output and input memory size.
290 int output_memory_size
= AudioBus::CalculateMemorySize(audio_parameters_
);
291 int input_channels
= audio_parameters_
.input_channels();
292 int frames
= audio_parameters_
.frames_per_buffer();
293 int input_memory_size
=
294 AudioBus::CalculateMemorySize(input_channels
, frames
);
296 int io_size
= output_memory_size
+ input_memory_size
;
298 DCHECK_EQ(memory_length_
, io_size
);
301 AudioBus::WrapMemory(audio_parameters_
, shared_memory_
.memory());
303 if (input_channels
> 0) {
304 // The input data is after the output data.
306 static_cast<char*>(shared_memory_
.memory()) + output_memory_size
;
308 AudioBus::WrapMemory(input_channels
, frames
, input_data
);
312 // Called whenever we receive notifications about pending data.
313 void AudioOutputDevice::AudioThreadCallback::Process(int pending_data
) {
314 if (pending_data
== kPauseMark
) {
315 memset(shared_memory_
.memory(), 0, memory_length_
);
316 SetActualDataSizeInBytes(&shared_memory_
, memory_length_
, 0);
320 // Convert the number of pending bytes in the render buffer
321 // into milliseconds.
322 int audio_delay_milliseconds
= pending_data
/ bytes_per_ms_
;
324 TRACE_EVENT0("audio", "AudioOutputDevice::FireRenderCallback");
326 // Update the audio-delay measurement then ask client to render audio. Since
327 // |output_bus_| is wrapping the shared memory the Render() call is writing
328 // directly into the shared memory.
329 int input_channels
= audio_parameters_
.input_channels();
330 size_t num_frames
= audio_parameters_
.frames_per_buffer();
332 if (input_bus_
.get() && input_channels
> 0) {
333 render_callback_
->RenderIO(input_bus_
.get(),
335 audio_delay_milliseconds
);
337 num_frames
= render_callback_
->Render(output_bus_
.get(),
338 audio_delay_milliseconds
);
341 // Let the host know we are done.
342 // 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
344 // relying on AudioSyncReader::Read() to parse this with that in mind. Rename
345 // these methods to Set/GetActualFrameCount().
346 SetActualDataSizeInBytes(
347 &shared_memory_
, memory_length_
,
348 num_frames
* sizeof(*output_bus_
->channel(0)) * output_bus_
->channels());
351 } // namespace media.