Chromium Code Reviews| Index: remoting/client/plugin/pepper_audio_player.cc |
| diff --git a/remoting/client/plugin/pepper_audio_player.cc b/remoting/client/plugin/pepper_audio_player.cc |
| index bf67436c1f91c369fb8696e2c62a4ec8958e6991..b260fb74d4e79f682c883981c823f9bbdcba4338 100644 |
| --- a/remoting/client/plugin/pepper_audio_player.cc |
| +++ b/remoting/client/plugin/pepper_audio_player.cc |
| @@ -7,7 +7,6 @@ |
| #include <algorithm> |
| #include "base/stl_util.h" |
| -#include "remoting/proto/audio.pb.h" |
| namespace { |
| // Constants used to create an audio configuration resource. |
| @@ -26,29 +25,71 @@ bool PepperAudioPlayer::IsRunning() const { |
| } |
| PepperAudioPlayer::PepperAudioPlayer(pp::Instance* instance) |
| - : samples_per_frame_(kSampleFrameCount), |
| - bytes_consumed_(0), |
| - running_(false) { |
| + : instance_(instance), |
| + sampling_rate_(AudioPacket::SAMPLING_RATE_INVALID), |
| + samples_per_frame_(kSampleFrameCount), |
| + bytes_consumed_(0), |
| + running_(false) { |
| +} |
| + |
| +PepperAudioPlayer::~PepperAudioPlayer() { |
| + if (running_ && !audio_.is_null()) |
| + DoStop(); |
|
Sergey Ulanov
2012/07/23 22:01:47
Do we really need to call this explicitly? Doesn't
kxing
2012/07/23 22:58:47
Done.
|
| +} |
| + |
| +bool PepperAudioPlayer::Start() { |
| + running_ = true; |
|
Sergey Ulanov
2012/07/23 22:01:47
Do we want to do that even when we fail to start?
kxing
2012/07/23 22:58:47
Done.
|
| + if (!audio_.is_null()) |
| + return DoStart(); |
|
Sergey Ulanov
2012/07/23 22:01:47
That's going to fail if audio_ is null. I wonder w
kxing
2012/07/23 22:58:47
Done.
|
| + return true; |
| +} |
| + |
| +bool PepperAudioPlayer::DoStart() { |
|
Sergey Ulanov
2012/07/23 22:01:47
Doesn't seem like a good name given that it called
kxing
2012/07/23 22:58:47
Done.
|
| + bool success = audio_.StartPlayback(); |
| + if (!success) |
| + LOG(ERROR) << "Failed to start Pepper audio player"; |
| + return success; |
| +} |
| + |
| +void PepperAudioPlayer::DoStop() { |
| + bool success = audio_.StopPlayback(); |
| + if (!success) |
| + LOG(ERROR) << "Failed to stop Pepper audio player"; |
| +} |
| + |
| +// static |
| +PP_AudioSampleRate PepperAudioPlayer::ConvertToPepperSampleRate( |
|
Sergey Ulanov
2012/07/23 22:01:47
Doesn't need to be a class member. Just make it a
kxing
2012/07/23 22:58:47
Done.
|
| + AudioPacket::SamplingRate sampling_rate) { |
| + switch (sampling_rate) { |
| + case AudioPacket::SAMPLING_RATE_44100: |
| + return PP_AUDIOSAMPLERATE_44100; |
| + case AudioPacket::SAMPLING_RATE_48000: |
| + return PP_AUDIOSAMPLERATE_48000; |
| + default: |
| + NOTREACHED(); |
| + } |
| + return PP_AUDIOSAMPLERATE_NONE; |
| +} |
| + |
| +void PepperAudioPlayer::ResetAudioPlayer( |
| + AudioPacket::SamplingRate sampling_rate) { |
| + sampling_rate_ = sampling_rate; |
| + PP_AudioSampleRate sample_rate = |
| + ConvertToPepperSampleRate(sampling_rate); |
| + |
| // Ask the browser/device for an appropriate sample frame count size. |
| samples_per_frame_ = |
| - pp::AudioConfig::RecommendSampleFrameCount(instance, |
| - PP_AUDIOSAMPLERATE_44100, |
| + pp::AudioConfig::RecommendSampleFrameCount(instance_, |
| + sample_rate, |
| kSampleFrameCount); |
| // Create an audio configuration resource. |
| - pp::AudioConfig audio_config = pp::AudioConfig(instance, |
| - PP_AUDIOSAMPLERATE_44100, |
| + pp::AudioConfig audio_config = pp::AudioConfig(instance_, |
| + sample_rate, |
| samples_per_frame_); |
| // Create an audio resource. |
| - audio_ = pp::Audio(instance, audio_config, PepperAudioPlayerCallback, this); |
| -} |
| - |
| -PepperAudioPlayer::~PepperAudioPlayer() { } |
| - |
| -bool PepperAudioPlayer::Start() { |
| - running_ = audio_.StartPlayback(); |
| - return running_; |
| + audio_ = pp::Audio(instance_, audio_config, PepperAudioPlayerCallback, this); |
| } |
| void PepperAudioPlayer::ProcessAudioPacket(scoped_ptr<AudioPacket> packet) { |
| @@ -60,6 +101,15 @@ void PepperAudioPlayer::ProcessAudioPacket(scoped_ptr<AudioPacket> packet) { |
| } |
| base::AutoLock auto_lock(lock_); |
| + // Start the Pepper audio player if this is the first packet. |
| + if (sampling_rate_ != packet->sampling_rate()) { |
| + if (running_ && !audio_.is_null()) |
| + DoStop(); |
|
Sergey Ulanov
2012/07/23 22:01:47
ResetAudioPlayer() creates new pp::Audio object an
kxing
2012/07/23 22:58:47
Done.
|
| + ResetAudioPlayer(packet->sampling_rate()); |
| + if (running_) |
| + DoStart(); |
| + } |
| + |
| queued_packets_.push_back(packet.release()); |
| } |