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) { | |
26 } | 42 } |
27 | 43 |
28 PepperAudioPlayer::PepperAudioPlayer(pp::Instance* instance) | 44 PepperAudioPlayer::~PepperAudioPlayer() {} |
29 : samples_per_frame_(kSampleFrameCount), | 45 |
30 bytes_consumed_(0), | 46 bool PepperAudioPlayer::StartPlayback() { |
31 running_(false) { | 47 bool success = audio_.StartPlayback(); |
48 if (!success) | |
49 LOG(ERROR) << "Failed to start Pepper audio player"; | |
50 return success; | |
51 } | |
52 | |
53 void PepperAudioPlayer::ResetAudioPlayer( | |
54 AudioPacket::SamplingRate sampling_rate) { | |
55 sampling_rate_ = sampling_rate; | |
56 PP_AudioSampleRate sample_rate = | |
57 ConvertToPepperSampleRate(sampling_rate); | |
58 | |
32 // Ask the browser/device for an appropriate sample frame count size. | 59 // Ask the browser/device for an appropriate sample frame count size. |
33 samples_per_frame_ = | 60 samples_per_frame_ = |
34 pp::AudioConfig::RecommendSampleFrameCount(instance, | 61 pp::AudioConfig::RecommendSampleFrameCount(instance_, |
35 PP_AUDIOSAMPLERATE_44100, | 62 sample_rate, |
36 kSampleFrameCount); | 63 kSampleFrameCount); |
37 | 64 |
38 // Create an audio configuration resource. | 65 // Create an audio configuration resource. |
39 pp::AudioConfig audio_config = pp::AudioConfig(instance, | 66 pp::AudioConfig audio_config = pp::AudioConfig(instance_, |
40 PP_AUDIOSAMPLERATE_44100, | 67 sample_rate, |
41 samples_per_frame_); | 68 samples_per_frame_); |
42 | 69 |
43 // Create an audio resource. | 70 // Create an audio resource. |
44 audio_ = pp::Audio(instance, audio_config, PepperAudioPlayerCallback, this); | 71 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 } | 72 } |
53 | 73 |
54 void PepperAudioPlayer::ProcessAudioPacket(scoped_ptr<AudioPacket> packet) { | 74 void PepperAudioPlayer::ProcessAudioPacket(scoped_ptr<AudioPacket> packet) { |
55 // TODO(kxing): Limit the size of the queue so that latency doesn't grow | 75 // TODO(kxing): Limit the size of the queue so that latency doesn't grow |
56 // too large. | 76 // too large. |
57 if (packet->data().size() % (kChannels * kSampleSizeBytes) != 0) { | 77 if (packet->data().size() % (kChannels * kSampleSizeBytes) != 0) { |
58 LOG(WARNING) << "Received corrupted packet."; | 78 LOG(WARNING) << "Received corrupted packet."; |
59 return; | 79 return; |
60 } | 80 } |
61 base::AutoLock auto_lock(lock_); | 81 base::AutoLock auto_lock(lock_); |
62 | 82 |
83 // Start the Pepper audio player if this is the first packet. | |
84 if (sampling_rate_ != packet->sampling_rate()) { | |
85 ResetAudioPlayer(packet->sampling_rate()); | |
86 StartPlayback(); | |
Sergey Ulanov
2012/07/23 23:32:02
we never use result returned from that function. I
kxing
2012/07/24 17:20:43
Done.
| |
87 } | |
88 | |
63 queued_packets_.push_back(packet.release()); | 89 queued_packets_.push_back(packet.release()); |
64 } | 90 } |
65 | 91 |
66 // static | 92 // static |
67 void PepperAudioPlayer::PepperAudioPlayerCallback(void* samples, | 93 void PepperAudioPlayer::PepperAudioPlayerCallback(void* samples, |
68 uint32_t buffer_size, | 94 uint32_t buffer_size, |
69 void* data) { | 95 void* data) { |
70 PepperAudioPlayer* audio_player = static_cast<PepperAudioPlayer*>(data); | 96 PepperAudioPlayer* audio_player = static_cast<PepperAudioPlayer*>(data); |
71 audio_player->FillWithSamples(samples, buffer_size); | 97 audio_player->FillWithSamples(samples, buffer_size); |
72 } | 98 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 bytes_needed - bytes_extracted); | 130 bytes_needed - bytes_extracted); |
105 memcpy(next_sample, packet_data.data() + bytes_consumed_, bytes_to_copy); | 131 memcpy(next_sample, packet_data.data() + bytes_consumed_, bytes_to_copy); |
106 | 132 |
107 next_sample += bytes_to_copy; | 133 next_sample += bytes_to_copy; |
108 bytes_consumed_ += bytes_to_copy; | 134 bytes_consumed_ += bytes_to_copy; |
109 bytes_extracted += bytes_to_copy; | 135 bytes_extracted += bytes_to_copy; |
110 } | 136 } |
111 } | 137 } |
112 | 138 |
113 } // namespace remoting | 139 } // namespace remoting |
OLD | NEW |