summaryrefslogtreecommitdiffstats
path: root/content/renderer/media/audio_device.cc
blob: 19349c9116169543d521b6fd4fd336726bdcb294 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/media/audio_device.h"

#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "base/message_loop.h"
#include "base/threading/thread_restrictions.h"
#include "base/time.h"
#include "content/common/child_process.h"
#include "content/common/media/audio_messages.h"
#include "content/common/view_messages.h"
#include "content/renderer/render_thread_impl.h"
#include "media/audio/audio_output_controller.h"
#include "media/audio/audio_util.h"

AudioDevice::AudioDevice()
    : buffer_size_(0),
      channels_(0),
      bits_per_sample_(16),
      sample_rate_(0),
      latency_format_(AudioParameters::AUDIO_PCM_LOW_LATENCY),
      callback_(0),
      is_initialized_(false),
      audio_delay_milliseconds_(0),
      volume_(1.0),
      stream_id_(0),
      play_on_start_(true),
      is_started_(false),
      shared_memory_handle_(base::SharedMemory::NULLHandle()),
      memory_length_(0) {
  filter_ = RenderThreadImpl::current()->audio_message_filter();
}

AudioDevice::AudioDevice(size_t buffer_size,
                         int channels,
                         double sample_rate,
                         RenderCallback* callback)
    : bits_per_sample_(16),
      is_initialized_(false),
      audio_delay_milliseconds_(0),
      volume_(1.0),
      stream_id_(0),
      play_on_start_(true),
      is_started_(false),
      shared_memory_handle_(base::SharedMemory::NULLHandle()),
      memory_length_(0) {
  filter_ = RenderThreadImpl::current()->audio_message_filter();
  Initialize(buffer_size,
             channels,
             sample_rate,
             AudioParameters::AUDIO_PCM_LOW_LATENCY,
             callback);
}

void AudioDevice::Initialize(size_t buffer_size,
                             int channels,
                             double sample_rate,
                             AudioParameters::Format latency_format,
                             RenderCallback* callback) {
  CHECK_EQ(0, stream_id_) <<
      "AudioDevice::Initialize() must be called before Start()";

  CHECK(!is_initialized_);

  buffer_size_ = buffer_size;
  channels_ = channels;
  sample_rate_ = sample_rate;
  latency_format_ = latency_format;
  callback_ = callback;

  // Cleanup from any previous initialization.
  for (size_t i = 0; i < audio_data_.size(); ++i)
    delete [] audio_data_[i];

  audio_data_.reserve(channels);
  for (int i = 0; i < channels; ++i) {
    float* channel_data = new float[buffer_size];
    audio_data_.push_back(channel_data);
  }

  is_initialized_ = true;
}

AudioDevice::~AudioDevice() {
  // The current design requires that the user calls Stop() before deleting
  // this class.
  CHECK_EQ(0, stream_id_);
  for (int i = 0; i < channels_; ++i)
    delete [] audio_data_[i];
}

void AudioDevice::Start() {
  AudioParameters params;
  params.format = latency_format_;
  params.channels = channels_;
  params.sample_rate = static_cast<int>(sample_rate_);
  params.bits_per_sample = bits_per_sample_;
  params.samples_per_packet = buffer_size_;

  ChildProcess::current()->io_message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&AudioDevice::InitializeOnIOThread, this, params));
}

void AudioDevice::Stop() {
  DCHECK(MessageLoop::current() != ChildProcess::current()->io_message_loop());

  // Stop and shutdown the audio thread from the IO thread.
  // This operation must be synchronous for now since the |callback_| pointer
  // isn't ref counted and the object might go out of scope after Stop()
  // returns (and FireRenderCallback might dereference a bogus pointer).
  // TODO(tommi): Add an Uninitialize() method to AudioRendererSink?
  base::WaitableEvent done(true, false);
  ChildProcess::current()->io_message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&AudioDevice::ShutDownOnIOThread, this, &done));
  done.Wait();
}

void AudioDevice::Play() {
  ChildProcess::current()->io_message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&AudioDevice::PlayOnIOThread, this));
}

void AudioDevice::Pause(bool flush) {
  ChildProcess::current()->io_message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&AudioDevice::PauseOnIOThread, this, flush));
}

bool AudioDevice::SetVolume(double volume) {
  if (volume < 0 || volume > 1.0)
    return false;

  ChildProcess::current()->io_message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&AudioDevice::SetVolumeOnIOThread, this, volume));

  volume_ = volume;

  return true;
}

void AudioDevice::GetVolume(double* volume) {
  // Return a locally cached version of the current scaling factor.
  *volume = volume_;
}

void AudioDevice::InitializeOnIOThread(const AudioParameters& params) {
  DCHECK_EQ(MessageLoop::current(), ChildProcess::current()->io_message_loop());
  // Make sure we don't create the stream more than once.
  DCHECK_EQ(0, stream_id_);
  if (stream_id_)
    return;

  stream_id_ = filter_->AddDelegate(this);
  Send(new AudioHostMsg_CreateStream(stream_id_, params, true));
}

void AudioDevice::PlayOnIOThread() {
  DCHECK_EQ(MessageLoop::current(), ChildProcess::current()->io_message_loop());
  if (stream_id_ && is_started_)
    Send(new AudioHostMsg_PlayStream(stream_id_));
  else
    play_on_start_ = true;
}

void AudioDevice::PauseOnIOThread(bool flush) {
  DCHECK_EQ(MessageLoop::current(), ChildProcess::current()->io_message_loop());
  if (stream_id_ && is_started_) {
    Send(new AudioHostMsg_PauseStream(stream_id_));
    if (flush)
      Send(new AudioHostMsg_FlushStream(stream_id_));
  } else {
    // Note that |flush| isn't relevant here since this is the case where
    // the stream is first starting.
    play_on_start_ = false;
  }
}

void AudioDevice::ShutDownOnIOThread(base::WaitableEvent* signal) {
  DCHECK_EQ(MessageLoop::current(), ChildProcess::current()->io_message_loop());

  // Make sure we don't call shutdown more than once.
  if (stream_id_) {
    is_started_ = false;

    filter_->RemoveDelegate(stream_id_);
    Send(new AudioHostMsg_CloseStream(stream_id_));
    stream_id_ = 0;

    ShutDownAudioThread();
  }

  signal->Signal();
}

void AudioDevice::SetVolumeOnIOThread(double volume) {
  DCHECK_EQ(MessageLoop::current(), ChildProcess::current()->io_message_loop());
  if (stream_id_)
    Send(new AudioHostMsg_SetVolume(stream_id_, volume));
}

void AudioDevice::OnRequestPacket(AudioBuffersState buffers_state) {
  // This method does not apply to the low-latency system.
}

void AudioDevice::OnStateChanged(AudioStreamState state) {
  if (state == kAudioStreamError) {
    DLOG(WARNING) << "AudioDevice::OnStateChanged(kError)";
    callback_->OnError();
  }
}

void AudioDevice::OnCreated(
    base::SharedMemoryHandle handle, uint32 length) {
  // Not needed in this simple implementation.
}

void AudioDevice::OnLowLatencyCreated(
    base::SharedMemoryHandle handle,
    base::SyncSocket::Handle socket_handle,
    uint32 length) {
  DCHECK_EQ(MessageLoop::current(), ChildProcess::current()->io_message_loop());
  DCHECK_GE(length, buffer_size_ * sizeof(int16) * channels_);
#if defined(OS_WIN)
  DCHECK(handle);
  DCHECK(socket_handle);
#else
  DCHECK_GE(handle.fd, 0);
  DCHECK_GE(socket_handle, 0);
#endif

  // Takes care of the case when Stop() is called before OnLowLatencyCreated().
  if (!stream_id_) {
    base::SharedMemory::CloseHandle(handle);
    // Close the socket handler.
    base::SyncSocket socket(socket_handle);
    return;
  }

  shared_memory_handle_ = handle;
  memory_length_ = length;
  audio_socket_.reset(new base::CancelableSyncSocket(socket_handle));

  audio_thread_.reset(
      new base::DelegateSimpleThread(this, "renderer_audio_thread"));
  audio_thread_->Start();

  // We handle the case where Play() and/or Pause() may have been called
  // multiple times before OnLowLatencyCreated() gets called.
  is_started_ = true;
  if (play_on_start_)
    PlayOnIOThread();
}

void AudioDevice::OnVolume(double volume) {
  NOTIMPLEMENTED();
}

void AudioDevice::Send(IPC::Message* message) {
  filter_->Send(message);
}

// Our audio thread runs here.
void AudioDevice::Run() {
  audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio);

  base::SharedMemory shared_memory(shared_memory_handle_, false);
  shared_memory.Map(media::TotalSharedMemorySizeInBytes(memory_length_));
  base::CancelableSyncSocket* audio_socket = audio_socket_.get();

  const int samples_per_ms = static_cast<int>(sample_rate_) / 1000;
  const int bytes_per_ms = channels_ * (bits_per_sample_ / 8) * samples_per_ms;

  while (true) {
    uint32 pending_data = 0;
    size_t bytes_read = audio_socket->Receive(&pending_data,
                                              sizeof(pending_data));
    if (bytes_read != sizeof(pending_data)) {
      DCHECK_EQ(bytes_read, 0U);
      break;
    }

    if (pending_data ==
        static_cast<uint32>(media::AudioOutputController::kPauseMark)) {
      memset(shared_memory.memory(), 0, memory_length_);
      media::SetActualDataSizeInBytes(&shared_memory, memory_length_, 0);
      continue;
    }

    // Convert the number of pending bytes in the render buffer
    // into milliseconds.
    audio_delay_milliseconds_ = pending_data / bytes_per_ms;
    size_t num_frames = FireRenderCallback(
        reinterpret_cast<int16*>(shared_memory.memory()));

    // Let the host know we are done.
    media::SetActualDataSizeInBytes(&shared_memory,
                                    memory_length_,
                                    num_frames * channels_ * sizeof(int16));
  }
}

size_t AudioDevice::FireRenderCallback(int16* data) {
  TRACE_EVENT0("audio", "AudioDevice::FireRenderCallback");

  size_t num_frames = 0;
  if (callback_) {
    // Update the audio-delay measurement then ask client to render audio.
    num_frames = callback_->Render(audio_data_,
                                   buffer_size_,
                                   audio_delay_milliseconds_);

    // Interleave, scale, and clip to int16.
    // TODO(crogers): avoid converting to integer here, and pass the data
    // to the browser process as float, so we don't lose precision for
    // audio hardware which has better than 16bit precision.
    media::InterleaveFloatToInt16(audio_data_,
                                  data,
                                  buffer_size_);
  }
  return num_frames;
}

void AudioDevice::ShutDownAudioThread() {
  DCHECK_EQ(MessageLoop::current(), ChildProcess::current()->io_message_loop());

  if (audio_thread_.get()) {
    // Close the socket to terminate the main thread function in the
    // audio thread.
    audio_socket_->Shutdown();  // Stops blocking Receive calls.
    // TODO(tommi): We must not do this from the IO thread.  Fix.
    base::ThreadRestrictions::ScopedAllowIO allow_wait;
    audio_thread_->Join();
    audio_thread_.reset(NULL);
    audio_socket_.reset();
  }
}