Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/client/plugin/pepper_audio_player.h" | 5 #include "remoting/client/plugin/pepper_audio_player.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "remoting/proto/audio.pb.h" | |
| 11 | 10 |
| 12 namespace { | 11 namespace { |
| 13 // Constants used to create an audio configuration resource. | 12 // Constants used to create an audio configuration resource. |
| 14 // The sample count we will request from the browser. | 13 // The sample count we will request from the browser. |
| 15 const uint32_t kSampleFrameCount = 4096u; | 14 const uint32_t kSampleFrameCount = 4096u; |
| 16 // The number of channels in the audio stream (only supporting stereo audio | 15 // The number of channels in the audio stream (only supporting stereo audio |
| 17 // for now). | 16 // for now). |
| 18 const uint32_t kChannels = 2u; | 17 const uint32_t kChannels = 2u; |
| 19 const int kSampleSizeBytes = 2; | 18 const int kSampleSizeBytes = 2; |
| 19 | |
| 20 PP_AudioSampleRate ConvertToPepperSampleRate( | |
| 21 remoting::AudioPacket::SamplingRate sampling_rate) { | |
| 22 switch (sampling_rate) { | |
| 23 case remoting::AudioPacket::SAMPLING_RATE_44100: | |
| 24 return PP_AUDIOSAMPLERATE_44100; | |
| 25 case remoting::AudioPacket::SAMPLING_RATE_48000: | |
| 26 return PP_AUDIOSAMPLERATE_48000; | |
| 27 default: | |
| 28 NOTREACHED(); | |
| 29 } | |
| 30 return PP_AUDIOSAMPLERATE_NONE; | |
| 31 } | |
| 32 | |
| 20 } // namespace | 33 } // namespace |
| 21 | 34 |
| 22 namespace remoting { | 35 namespace remoting { |
| 23 | 36 |
| 24 bool PepperAudioPlayer::IsRunning() const { | 37 PepperAudioPlayer::PepperAudioPlayer(pp::Instance* instance) |
| 25 return running_; | 38 : instance_(instance), |
| 39 sampling_rate_(AudioPacket::SAMPLING_RATE_INVALID), | |
| 40 samples_per_frame_(kSampleFrameCount), | |
| 41 bytes_consumed_(0), | |
| 42 failed_playback_(false) { | |
| 26 } | 43 } |
| 27 | 44 |
| 28 PepperAudioPlayer::PepperAudioPlayer(pp::Instance* instance) | 45 PepperAudioPlayer::~PepperAudioPlayer() {} |
| 29 : samples_per_frame_(kSampleFrameCount), | 46 |
| 30 bytes_consumed_(0), | 47 bool PepperAudioPlayer::StartPlayback() { |
| 31 running_(false) { | 48 bool success = audio_.StartPlayback(); |
| 49 if (!success) | |
| 50 LOG(ERROR) << "Failed to start Pepper audio player"; | |
| 51 return success; | |
| 52 } | |
| 53 | |
| 54 void PepperAudioPlayer::ResetAudioPlayer( | |
|
Sergey Ulanov
2012/07/24 18:10:03
Don't think we need to separate StartPlayback() an
kxing
2012/07/24 18:32:01
Done.
| |
| 55 AudioPacket::SamplingRate sampling_rate) { | |
| 56 sampling_rate_ = sampling_rate; | |
| 57 PP_AudioSampleRate sample_rate = | |
| 58 ConvertToPepperSampleRate(sampling_rate); | |
| 59 | |
| 32 // Ask the browser/device for an appropriate sample frame count size. | 60 // Ask the browser/device for an appropriate sample frame count size. |
| 33 samples_per_frame_ = | 61 samples_per_frame_ = |
| 34 pp::AudioConfig::RecommendSampleFrameCount(instance, | 62 pp::AudioConfig::RecommendSampleFrameCount(instance_, |
| 35 PP_AUDIOSAMPLERATE_44100, | 63 sample_rate, |
| 36 kSampleFrameCount); | 64 kSampleFrameCount); |
| 37 | 65 |
| 38 // Create an audio configuration resource. | 66 // Create an audio configuration resource. |
| 39 pp::AudioConfig audio_config = pp::AudioConfig(instance, | 67 pp::AudioConfig audio_config = pp::AudioConfig(instance_, |
| 40 PP_AUDIOSAMPLERATE_44100, | 68 sample_rate, |
| 41 samples_per_frame_); | 69 samples_per_frame_); |
| 42 | 70 |
| 43 // Create an audio resource. | 71 // Create an audio resource. |
| 44 audio_ = pp::Audio(instance, audio_config, PepperAudioPlayerCallback, this); | 72 audio_ = pp::Audio(instance_, audio_config, PepperAudioPlayerCallback, this); |
| 45 } | |
| 46 | |
| 47 PepperAudioPlayer::~PepperAudioPlayer() { } | |
| 48 | |
| 49 bool PepperAudioPlayer::Start() { | |
| 50 running_ = audio_.StartPlayback(); | |
| 51 return running_; | |
| 52 } | 73 } |
| 53 | 74 |
| 54 void PepperAudioPlayer::ProcessAudioPacket(scoped_ptr<AudioPacket> packet) { | 75 void PepperAudioPlayer::ProcessAudioPacket(scoped_ptr<AudioPacket> packet) { |
| 55 // TODO(kxing): Limit the size of the queue so that latency doesn't grow | 76 // TODO(kxing): Limit the size of the queue so that latency doesn't grow |
| 56 // too large. | 77 // too large. |
| 57 if (packet->data().size() % (kChannels * kSampleSizeBytes) != 0) { | 78 if (packet->data().size() % (kChannels * kSampleSizeBytes) != 0) { |
| 58 LOG(WARNING) << "Received corrupted packet."; | 79 LOG(WARNING) << "Received corrupted packet."; |
| 59 return; | 80 return; |
| 60 } | 81 } |
| 61 base::AutoLock auto_lock(lock_); | 82 base::AutoLock auto_lock(lock_); |
| 62 | 83 |
| 84 // No-op if the Pepper player won't start. | |
| 85 if (failed_playback_) { | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 // Start the Pepper audio player if this is the first packet. | |
| 90 if (sampling_rate_ != packet->sampling_rate()) { | |
| 91 // Drop all packets currently in the queue, since they are sampled at the | |
| 92 // wrong rate. | |
| 93 STLDeleteElements(&queued_packets_); | |
| 94 | |
| 95 ResetAudioPlayer(packet->sampling_rate()); | |
| 96 bool success = StartPlayback(); | |
| 97 if (!success) { | |
| 98 failed_playback_ = true; | |
| 99 return; | |
| 100 } | |
| 101 } | |
| 102 | |
| 63 queued_packets_.push_back(packet.release()); | 103 queued_packets_.push_back(packet.release()); |
| 64 } | 104 } |
| 65 | 105 |
| 66 // static | 106 // static |
| 67 void PepperAudioPlayer::PepperAudioPlayerCallback(void* samples, | 107 void PepperAudioPlayer::PepperAudioPlayerCallback(void* samples, |
| 68 uint32_t buffer_size, | 108 uint32_t buffer_size, |
| 69 void* data) { | 109 void* data) { |
| 70 PepperAudioPlayer* audio_player = static_cast<PepperAudioPlayer*>(data); | 110 PepperAudioPlayer* audio_player = static_cast<PepperAudioPlayer*>(data); |
| 71 audio_player->FillWithSamples(samples, buffer_size); | 111 audio_player->FillWithSamples(samples, buffer_size); |
| 72 } | 112 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 bytes_needed - bytes_extracted); | 144 bytes_needed - bytes_extracted); |
| 105 memcpy(next_sample, packet_data.data() + bytes_consumed_, bytes_to_copy); | 145 memcpy(next_sample, packet_data.data() + bytes_consumed_, bytes_to_copy); |
| 106 | 146 |
| 107 next_sample += bytes_to_copy; | 147 next_sample += bytes_to_copy; |
| 108 bytes_consumed_ += bytes_to_copy; | 148 bytes_consumed_ += bytes_to_copy; |
| 109 bytes_extracted += bytes_to_copy; | 149 bytes_extracted += bytes_to_copy; |
| 110 } | 150 } |
| 111 } | 151 } |
| 112 | 152 |
| 113 } // namespace remoting | 153 } // namespace remoting |
| OLD | NEW |