summaryrefslogtreecommitdiffstats
path: root/chrome/browser/notifications/notification_audio_controller.cc
blob: 213f9c423b12d394f595d6db69ecaeb6ba2a003a (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright 2013 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 "chrome/browser/notifications/notification_audio_controller.h"

#include <cstring>

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/cancelable_callback.h"
#include "base/location.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/sys_byteorder.h"
#include "base/task_runner_util.h"
#include "media/audio/audio_io.h"
#include "media/audio/audio_manager.h"
#include "media/audio/audio_parameters.h"
#include "media/base/audio_bus.h"
#include "media/base/channel_layout.h"

namespace {

// The size of the header of a wav file. The header consists of 'RIFF', 4 bytes
// of total data length, and 'WAVE'.
const size_t kWavFileHeaderSize = 12;

// The size of a chunk header in wav file format. A chunk header consists of a
// tag ('fmt ' or 'data') and 4 bytes of chunk length.
const size_t kChunkHeaderSize = 8;

// The minimum size of 'fmt' chunk.
const size_t kFmtChunkMinimumSize = 16;

// The offsets of 'fmt' fields.
const size_t kAudioFormatOffset = 0;
const size_t kChunnelOffset = 2;
const size_t kSampleRateOffset = 4;
const size_t kBitsPerSampleOffset = 14;

// Some constants for audio format.
const int kAudioFormatPCM = 1;

// The frame-per-buffer parameter for wav data format reader. Since wav format
// doesn't have this parameter in its header, just choose some value.
const int kFramesPerBuffer = 4096;

///////////////////////////////////////////////////////////////////////////
// WavAudioHandler
//
// This class provides the input from wav file format.
// See https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
class WavAudioHandler {
 public:
  explicit WavAudioHandler(const base::StringPiece& data);
  virtual ~WavAudioHandler();

  // Returns true when it plays to the end of the track.
  bool AtEnd();

  // Copies the audio data to |dest|.
  void CopyTo(media::AudioBus* dest);

  uint16 num_channels() const { return num_channels_; }
  uint32 sample_rate() const { return sample_rate_; }
  uint16 bits_per_sample() const { return bits_per_sample_; }

 private:
  // Parses a chunk of wav format data. Returns the length of the chunk.
  int ParseSubChunk(const base::StringPiece& data);

  // Parses the 'fmt' section chunk and stores |params_|.
  void ParseFmtChunk(const base::StringPiece& data);

  // Parses the 'data' section chunk and stores |data_|.
  void ParseDataChunk(const base::StringPiece& data);

  // Reads an integer from |data| with |offset|.
  template<typename T> T ReadInt(const base::StringPiece& data, size_t offset);

  base::StringPiece data_;
  size_t cursor_;
  uint16 num_channels_;
  uint32 sample_rate_;
  uint16 bits_per_sample_;

  DISALLOW_COPY_AND_ASSIGN(WavAudioHandler);
};

WavAudioHandler::WavAudioHandler(const base::StringPiece& data)
    : cursor_(0) {
  if (data.size() < kWavFileHeaderSize)
    return;

  if (!data.starts_with("RIFF") || ::memcmp(data.data() + 8, "WAVE", 4) != 0)
    return;

  uint32 total_length =
      std::min(ReadInt<uint32>(data, 4), static_cast<uint32>(data.size()));

  uint32 offset = kWavFileHeaderSize;
  while (offset < total_length)
    offset += ParseSubChunk(data.substr(offset));
}

WavAudioHandler::~WavAudioHandler() {
}

bool WavAudioHandler::AtEnd() {
  return data_.size() <= cursor_;
}

void WavAudioHandler::CopyTo(media::AudioBus* dest) {
  DCHECK_EQ(dest->channels(), num_channels_);
  if (cursor_ >= data_.size()) {
    dest->Zero();
    return;
  }

  const int bytes_per_sample = bits_per_sample_ / 8;
  const int bytes_per_frame = num_channels_ * bytes_per_sample;
  const int remaining_frames = (data_.size() - cursor_) / bytes_per_frame;
  const int frames = std::min(dest->frames(), remaining_frames);
  dest->FromInterleaved(data_.data() + cursor_, frames, bytes_per_sample);
  cursor_ += frames * bytes_per_frame;
  dest->ZeroFramesPartial(frames, dest->frames() - frames);
}

int WavAudioHandler::ParseSubChunk(const base::StringPiece& data) {
  if (data.size() < kChunkHeaderSize)
    return data.size();
  uint32 chunk_length = ReadInt<uint32>(data, 4);

  if (data.starts_with("fmt "))
    ParseFmtChunk(data.substr(kChunkHeaderSize, chunk_length));
  else if (data.starts_with("data"))
    ParseDataChunk(data.substr(kChunkHeaderSize, chunk_length));
  else
    DLOG(WARNING) << "Unknown data chunk: " << data.substr(0, 4);
  return chunk_length + kChunkHeaderSize;
}

void WavAudioHandler::ParseFmtChunk(const base::StringPiece& data) {
  if (data.size() < kFmtChunkMinimumSize) {
    DLOG(ERROR) << "data size " << data.size() << " is too short.";
    return;
  }

  DCHECK_EQ(ReadInt<uint16>(data, kAudioFormatOffset), kAudioFormatPCM);

  num_channels_ = ReadInt<uint16>(data, kChunnelOffset);
  sample_rate_ = ReadInt<uint32>(data, kSampleRateOffset);
  bits_per_sample_ = ReadInt<uint16>(data, kBitsPerSampleOffset);
}

void WavAudioHandler::ParseDataChunk(const base::StringPiece& data) {
  data_ = data;
}

template<typename T> T WavAudioHandler::ReadInt(const base::StringPiece& data,
                                                size_t offset) {
  DCHECK_LE(offset + sizeof(T), data.size());
  T result;
  ::memcpy(&result, data.data() + offset, sizeof(T));
#if !defined(ARCH_CPU_LITTLE_ENDIAN)
  result = base::ByteSwap(result);
#endif

  return result;
}

}  // namespace

///////////////////////////////////////////////////////////////////////////
// NotificationAudioController::AudioHandler
//
// This class connects a sound for a notification to the audio manager. It
// will be released by its owner when the sound ends.
class NotificationAudioController::AudioHandler
    : public media::AudioOutputStream::AudioSourceCallback {
 public:
  typedef base::Callback<void(AudioHandler*)> OnFinishedCB;

  AudioHandler(const OnFinishedCB& on_finished_callback,
               const std::string& notification_id,
               const Profile* profile,
               const base::StringPiece& data);
  virtual ~AudioHandler();

  const std::string& notification_id() const { return notification_id_; }
  const Profile* profile() const { return profile_; }

  // Start playing the sound. Returns true when it's successfully scheduled.
  bool StartPlayingSound();

  // Stops the playing sound request.
  void StopPlayingSound();

 private:
  // media::AudioOutputStream::AudioSourceCallback overrides:
  virtual int OnMoreData(media::AudioBus* dest,
                         media::AudioBuffersState state) OVERRIDE;
  virtual int OnMoreIOData(media::AudioBus* source,
                           media::AudioBus* dest,
                           media::AudioBuffersState state) OVERRIDE;
  virtual void OnError(media::AudioOutputStream* stream) OVERRIDE;

  OnFinishedCB on_finished_callback_;
  base::CancelableClosure stop_playing_sound_;
  std::string notification_id_;
  const Profile* profile_;

  media::AudioManager* audio_manager_;
  WavAudioHandler wav_audio_;
  media::AudioOutputStream* stream_;

  DISALLOW_COPY_AND_ASSIGN(AudioHandler);
};

NotificationAudioController::AudioHandler::AudioHandler(
    const OnFinishedCB& on_finished_callback,
    const std::string& notification_id,
    const Profile* profile,
    const base::StringPiece& data)
    : on_finished_callback_(on_finished_callback),
      notification_id_(notification_id),
      profile_(profile),
      audio_manager_(media::AudioManager::Get()),
      wav_audio_(data) {
}

NotificationAudioController::AudioHandler::~AudioHandler() {
}

bool NotificationAudioController::AudioHandler::StartPlayingSound() {
  DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread());

  media::AudioParameters params = media::AudioParameters(
      media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
      media::GuessChannelLayout(wav_audio_.num_channels()),
      wav_audio_.num_channels(),
      wav_audio_.sample_rate(),
      wav_audio_.bits_per_sample(),
      kFramesPerBuffer);

  if (!params.IsValid())
    return false;

  stream_ = audio_manager_->MakeAudioOutputStreamProxy(params);
  if (!stream_->Open()) {
    DLOG(ERROR) << "Failed to open the output stream";
    stream_->Close();
    return false;
  }

  stop_playing_sound_.Reset(base::Bind(
      &AudioHandler::StopPlayingSound, base::Unretained(this)));
  stream_->Start(this);
  return true;
}

// Stops actually the audio output stream.
void NotificationAudioController::AudioHandler::StopPlayingSound() {
  DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread());
  stop_playing_sound_.Cancel();

  // Close deletes |stream| too.
  stream_->Stop();
  stream_->Close();
  on_finished_callback_.Run(this);
}

int NotificationAudioController::AudioHandler::OnMoreData(
    media::AudioBus* dest,
    media::AudioBuffersState state) {
  wav_audio_.CopyTo(dest);
  if (wav_audio_.AtEnd()) {
    audio_manager_->GetMessageLoop()->PostTask(
        FROM_HERE, stop_playing_sound_.callback());
  }
  return dest->frames();
}

int NotificationAudioController::AudioHandler::OnMoreIOData(
    media::AudioBus* source,
    media::AudioBus* dest,
    media::AudioBuffersState state) {
  return OnMoreData(dest, state);
}

void NotificationAudioController::AudioHandler::OnError(
    media::AudioOutputStream* stream) {
}

///////////////////////////////////////////////////////////////////////////
// NotificationAudioController
//

NotificationAudioController::NotificationAudioController()
    : message_loop_(media::AudioManager::Get()->GetMessageLoop()) {
}

NotificationAudioController::~NotificationAudioController() {
  DCHECK(message_loop_->BelongsToCurrentThread());
}

void NotificationAudioController::RequestPlayNotificationSound(
    const std::string& notification_id,
    const Profile* profile,
    const base::StringPiece& data) {
  message_loop_->PostTask(
      FROM_HERE,
      base::Bind(
          &NotificationAudioController::PlayNotificationSound,
          base::Unretained(this),
          notification_id,
          base::Unretained(profile),
          data));
}

void NotificationAudioController::RequestShutdown() {
  message_loop_->PostTask(
      FROM_HERE,
      base::Bind(
          &NotificationAudioController::Shutdown, base::Unretained(this)));
}

void NotificationAudioController::OnNotificationSoundFinished(
    AudioHandler* audio_handler) {
  DCHECK(message_loop_->BelongsToCurrentThread());
  std::vector<AudioHandler*>::iterator it =
      std::find(audio_handlers_.begin(), audio_handlers_.end(), audio_handler);
  DCHECK(it != audio_handlers_.end());
  audio_handlers_.erase(it);
}

void NotificationAudioController::GetAudioHandlersSizeForTest(
    const base::Callback<void(size_t)>& on_get) {
  base::PostTaskAndReplyWithResult(
      message_loop_.get(),
      FROM_HERE,
      base::Bind(&NotificationAudioController::GetAudioHandlersSizeCallback,
                 base::Unretained(this)),
      on_get);
}

void NotificationAudioController::PlayNotificationSound(
    const std::string& notification_id,
    const Profile* profile,
    const base::StringPiece& data) {
  DCHECK(message_loop_->BelongsToCurrentThread());

  for (size_t i = 0; i < audio_handlers_.size(); ++i) {
    if (audio_handlers_[i]->notification_id() == notification_id &&
        audio_handlers_[i]->profile() == profile) {
      audio_handlers_[i]->StopPlayingSound();
      break;
    }
  }

  scoped_ptr<AudioHandler> new_handler(new AudioHandler(
      base::Bind(&NotificationAudioController::OnNotificationSoundFinished,
                 base::Unretained(this)),
      notification_id,
      profile,
      data));
  if (new_handler->StartPlayingSound())
    audio_handlers_.push_back(new_handler.release());
}

void NotificationAudioController::Shutdown() {
  DCHECK(message_loop_->BelongsToCurrentThread());

  while (!audio_handlers_.empty())
    audio_handlers_[0]->StopPlayingSound();

  delete this;
}

size_t NotificationAudioController::GetAudioHandlersSizeCallback() {
  DCHECK(message_loop_->BelongsToCurrentThread());
  return audio_handlers_.size();
}