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/audio_player.h" | 5 #include "remoting/client/audio_player.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 | 11 |
12 // The number of channels in the audio stream (only supporting stereo audio | 12 // The number of channels in the audio stream (only supporting stereo audio |
13 // for now). | 13 // for now). |
14 const int kChannels = 2u; | 14 const int kChannels = 2; |
15 const int kSampleSizeBytes = 2; | 15 const int kSampleSizeBytes = 2; |
16 | 16 |
17 // If queue grows bigger than 150ms we start dropping packets. | 17 // If queue grows bigger than 150ms we start dropping packets. |
18 const int kMaxQueueLatencyMs = 150; | 18 const int kMaxQueueLatencyMs = 150; |
19 | 19 |
20 namespace remoting { | 20 namespace remoting { |
21 | 21 |
22 AudioPlayer::AudioPlayer() | 22 AudioPlayer::AudioPlayer() |
23 : sampling_rate_(AudioPacket::SAMPLING_RATE_INVALID), | 23 : sampling_rate_(AudioPacket::SAMPLING_RATE_INVALID), |
24 start_failed_(false), | 24 start_failed_(false), |
25 queued_samples_(0), | 25 queued_bytes_(0), |
26 bytes_consumed_(0) { | 26 bytes_consumed_(0) { |
27 } | 27 } |
28 | 28 |
29 AudioPlayer::~AudioPlayer() { | 29 AudioPlayer::~AudioPlayer() { |
30 base::AutoLock auto_lock(lock_); | 30 base::AutoLock auto_lock(lock_); |
31 ResetQueue(); | 31 ResetQueue(); |
32 } | 32 } |
33 | 33 |
34 void AudioPlayer::ProcessAudioPacket(scoped_ptr<AudioPacket> packet) { | 34 void AudioPlayer::ProcessAudioPacket(scoped_ptr<AudioPacket> packet) { |
35 CHECK_EQ(1, packet->data_size()); | 35 CHECK_EQ(1, packet->data_size()); |
(...skipping 20 matching lines...) Expand all Loading... |
56 sampling_rate_ = packet->sampling_rate(); | 56 sampling_rate_ = packet->sampling_rate(); |
57 bool success = ResetAudioPlayer(sampling_rate_); | 57 bool success = ResetAudioPlayer(sampling_rate_); |
58 if (!success) { | 58 if (!success) { |
59 start_failed_ = true; | 59 start_failed_ = true; |
60 return; | 60 return; |
61 } | 61 } |
62 } | 62 } |
63 | 63 |
64 base::AutoLock auto_lock(lock_); | 64 base::AutoLock auto_lock(lock_); |
65 | 65 |
66 if (queued_samples_ > kMaxQueueLatencyMs * sampling_rate_ / | 66 queued_bytes_ += packet->data(0).size(); |
67 base::Time::kMillisecondsPerSecond) { | 67 queued_packets_.push_back(packet.release()); |
68 ResetQueue(); | 68 |
| 69 int max_buffer_size_ = |
| 70 kMaxQueueLatencyMs * sampling_rate_ * kSampleSizeBytes * kChannels / |
| 71 base::Time::kMillisecondsPerSecond; |
| 72 while (queued_bytes_ > max_buffer_size_) { |
| 73 queued_bytes_ -= queued_packets_.front()->data(0).size() - bytes_consumed_; |
| 74 DCHECK_GE(queued_bytes_, 0); |
| 75 delete queued_packets_.front(); |
| 76 queued_packets_.pop_front(); |
| 77 bytes_consumed_ = 0; |
69 } | 78 } |
70 | |
71 queued_samples_ += packet->data(0).size() / (kChannels * kSampleSizeBytes); | |
72 queued_packets_.push_back(packet.release()); | |
73 } | 79 } |
74 | 80 |
75 // static | 81 // static |
76 void AudioPlayer::AudioPlayerCallback(void* samples, | 82 void AudioPlayer::AudioPlayerCallback(void* samples, |
77 uint32 buffer_size, | 83 uint32 buffer_size, |
78 void* data) { | 84 void* data) { |
79 AudioPlayer* audio_player = static_cast<AudioPlayer*>(data); | 85 AudioPlayer* audio_player = static_cast<AudioPlayer*>(data); |
80 audio_player->FillWithSamples(samples, buffer_size); | 86 audio_player->FillWithSamples(samples, buffer_size); |
81 } | 87 } |
82 | 88 |
83 void AudioPlayer::ResetQueue() { | 89 void AudioPlayer::ResetQueue() { |
84 lock_.AssertAcquired(); | 90 lock_.AssertAcquired(); |
85 STLDeleteElements(&queued_packets_); | 91 STLDeleteElements(&queued_packets_); |
86 queued_samples_ = 0; | 92 queued_bytes_ = 0; |
87 bytes_consumed_ = 0; | 93 bytes_consumed_ = 0; |
88 } | 94 } |
89 | 95 |
90 void AudioPlayer::FillWithSamples(void* samples, uint32 buffer_size) { | 96 void AudioPlayer::FillWithSamples(void* samples, uint32 buffer_size) { |
91 base::AutoLock auto_lock(lock_); | 97 base::AutoLock auto_lock(lock_); |
92 | 98 |
93 const size_t bytes_needed = kChannels * kSampleSizeBytes * | 99 const size_t bytes_needed = kChannels * kSampleSizeBytes * |
94 GetSamplesPerFrame(); | 100 GetSamplesPerFrame(); |
95 | 101 |
96 // Make sure we don't overrun the buffer. | 102 // Make sure we don't overrun the buffer. |
(...skipping 19 matching lines...) Expand all Loading... |
116 | 122 |
117 const std::string& packet_data = queued_packets_.front()->data(0); | 123 const std::string& packet_data = queued_packets_.front()->data(0); |
118 size_t bytes_to_copy = std::min( | 124 size_t bytes_to_copy = std::min( |
119 packet_data.size() - bytes_consumed_, | 125 packet_data.size() - bytes_consumed_, |
120 bytes_needed - bytes_extracted); | 126 bytes_needed - bytes_extracted); |
121 memcpy(next_sample, packet_data.data() + bytes_consumed_, bytes_to_copy); | 127 memcpy(next_sample, packet_data.data() + bytes_consumed_, bytes_to_copy); |
122 | 128 |
123 next_sample += bytes_to_copy; | 129 next_sample += bytes_to_copy; |
124 bytes_consumed_ += bytes_to_copy; | 130 bytes_consumed_ += bytes_to_copy; |
125 bytes_extracted += bytes_to_copy; | 131 bytes_extracted += bytes_to_copy; |
126 queued_samples_ -= bytes_to_copy / kSampleSizeBytes / kChannels; | 132 queued_bytes_ -= bytes_to_copy; |
127 DCHECK_GE(queued_samples_, 0); | 133 DCHECK_GE(queued_bytes_, 0); |
128 } | 134 } |
129 } | 135 } |
130 | 136 |
131 } // namespace remoting | 137 } // namespace remoting |
OLD | NEW |