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.
4
5#include "remoting/client/plugin/pepper_audio_player.h"
6
7#include <algorithm>
8
9#include "base/logging.h"
10#include "base/stl_util.h"
11
12// The frame size we will request from the browser.
13const int kFrameSizeMs = 40;
14
15namespace remoting {
16
17PepperAudioPlayer::PepperAudioPlayer(pp::Instance* instance)
18    : instance_(instance),
19      samples_per_frame_(0) {
20}
21
22PepperAudioPlayer::~PepperAudioPlayer() {
23}
24
25uint32 PepperAudioPlayer::GetSamplesPerFrame() {
26  return samples_per_frame_;
27}
28
29bool PepperAudioPlayer::ResetAudioPlayer(
30      AudioPacket::SamplingRate sampling_rate) {
31  PP_AudioSampleRate pp_sampling_rate = PP_AUDIOSAMPLERATE_NONE;
32  switch (sampling_rate) {
33    case AudioPacket::SAMPLING_RATE_44100:
34      pp_sampling_rate = PP_AUDIOSAMPLERATE_44100;
35      break;
36    case AudioPacket::SAMPLING_RATE_48000:
37      pp_sampling_rate = PP_AUDIOSAMPLERATE_48000;
38      break;
39    default:
40      LOG(ERROR) << "Unsupported audio sampling rate: " << sampling_rate;
41      return false;
42  }
43
44  // Ask the browser/device for an appropriate frame size.
45  samples_per_frame_ = pp::AudioConfig::RecommendSampleFrameCount(
46      instance_, pp_sampling_rate,
47      kFrameSizeMs * sampling_rate / base::Time::kMillisecondsPerSecond);
48
49  // Create an audio configuration resource.
50  pp::AudioConfig audio_config = pp::AudioConfig(
51      instance_, pp_sampling_rate, samples_per_frame_);
52
53  // Create an audio resource.
54  audio_ = pp::Audio(instance_, audio_config, AudioPlayerCallback, this);
55
56  // Immediately start the player.
57  bool success = audio_.StartPlayback();
58  if (!success)
59    LOG(ERROR) << "Failed to start Pepper audio player";
60  return success;
61}
62
63}  // namespace remoting
64