Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "media/filters/audio_clock.h" | 5 #include "media/filters/audio_clock.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "media/base/buffers.h" | 8 #include "media/base/buffers.h" |
| 9 | 9 |
| 10 namespace media { | 10 namespace media { |
| 11 | 11 |
| 12 AudioClock::AudioClock(int sample_rate) | 12 AudioClock::AudioClock(base::TimeDelta start_timestamp, int sample_rate) |
| 13 : sample_rate_(sample_rate), last_endpoint_timestamp_(kNoTimestamp()) { | 13 : start_timestamp_(start_timestamp), sample_rate_(sample_rate) { |
| 14 } | 14 } |
| 15 | 15 |
| 16 AudioClock::~AudioClock() { | 16 AudioClock::~AudioClock() { |
| 17 } | 17 } |
| 18 | 18 |
| 19 void AudioClock::WroteAudio(int frames, | 19 void AudioClock::WroteAudio(int frames_written, |
| 20 int frames_requested, | |
| 20 int delay_frames, | 21 int delay_frames, |
| 21 float playback_rate, | 22 float playback_rate) { |
| 22 base::TimeDelta timestamp) { | 23 DCHECK_GE(frames_written, 0); |
| 23 CHECK_GT(playback_rate, 0); | 24 DCHECK_LE(frames_written, frames_requested); |
| 24 CHECK(timestamp != kNoTimestamp()); | |
| 25 DCHECK_GE(frames, 0); | |
| 26 DCHECK_GE(delay_frames, 0); | 25 DCHECK_GE(delay_frames, 0); |
| 26 DCHECK_GE(playback_rate, 0); | |
| 27 | 27 |
| 28 if (last_endpoint_timestamp_ == kNoTimestamp()) | 28 // First write: initialize buffer with silence. |
| 29 PushBufferedAudio(delay_frames, 0, kNoTimestamp()); | 29 if (buffered_.empty()) |
| 30 PushAudioData(&buffered_, delay_frames, 0.0f); | |
| 30 | 31 |
| 31 TrimBufferedAudioToMatchDelay(delay_frames); | 32 // Move frames from |buffered_| to |played_| based on |delay_frames|. |
| 32 PushBufferedAudio(frames, playback_rate, timestamp); | 33 int64_t played_frames = std::max(0L, TotalFrames(buffered_) - delay_frames); |
|
DaleCurtis
2014/08/02 00:51:32
Seems like you could simply keep track of TotalFra
scherkus (not reviewing)
2014/08/02 01:55:24
Done.
| |
| 34 while (played_frames > 0) { | |
| 35 int64_t frames_to_move = std::min(buffered_.front().frames, played_frames); | |
| 33 | 36 |
| 34 last_endpoint_timestamp_ = timestamp; | 37 // No need to keep track of silent audio. |
| 35 } | 38 if (buffered_.front().playback_rate > 0.0f) |
| 39 PushAudioData(&played_, frames_to_move, buffered_.front().playback_rate); | |
| 36 | 40 |
| 37 void AudioClock::WroteSilence(int frames, int delay_frames) { | 41 buffered_.front().frames -= frames_to_move; |
| 38 DCHECK_GE(frames, 0); | 42 if (buffered_.front().frames == 0) |
| 39 DCHECK_GE(delay_frames, 0); | 43 buffered_.pop_front(); |
| 40 | 44 |
| 41 if (last_endpoint_timestamp_ == kNoTimestamp()) | 45 played_frames -= frames_to_move; |
| 42 PushBufferedAudio(delay_frames, 0, kNoTimestamp()); | 46 } |
| 43 | 47 |
| 44 TrimBufferedAudioToMatchDelay(delay_frames); | 48 // Push in newly buffered data. |
| 45 PushBufferedAudio(frames, 0, kNoTimestamp()); | 49 PushAudioData(&buffered_, frames_written, playback_rate); |
| 50 PushAudioData(&buffered_, frames_requested - frames_written, 0.0f); | |
| 46 } | 51 } |
| 47 | 52 |
| 48 base::TimeDelta AudioClock::CurrentMediaTimestamp( | 53 base::TimeDelta AudioClock::CurrentMediaTimestamp( |
| 49 base::TimeDelta time_since_writing) const { | 54 base::TimeDelta time_since_writing) const { |
| 50 int frames_to_skip = | 55 // Count up all |played_| audio since |start_timestamp_|. |
| 51 static_cast<int>(time_since_writing.InSecondsF() * sample_rate_); | 56 base::TimeDelta current_timestamp = start_timestamp_; |
| 52 int silence_frames = 0; | 57 for (size_t i = 0; i < played_.size(); ++i) { |
|
DaleCurtis
2014/08/02 00:51:33
played_ never shrinks, so you should just cache cu
scherkus (not reviewing)
2014/08/02 01:55:24
have to think about it some more ... but doesn't t
DaleCurtis
2014/08/04 18:55:18
I'd guess only slightly more error than you're alr
| |
| 53 for (size_t i = 0; i < buffered_audio_.size(); ++i) { | 58 DCHECK_NE(played_[i].playback_rate, 0.0f) |
| 54 int frames = buffered_audio_[i].frames; | 59 << "Silent audio doesn't need to be tracked in |played_|."; |
| 55 if (frames_to_skip > 0) { | 60 current_timestamp += base::TimeDelta::FromMicroseconds( |
|
DaleCurtis
2014/08/02 00:51:32
calculate this as a double or float and only divid
scherkus (not reviewing)
2014/08/02 01:55:25
Done.
| |
| 56 if (frames <= frames_to_skip) { | 61 (played_[i].frames * played_[i].playback_rate) / sample_rate_ * |
| 57 frames_to_skip -= frames; | 62 base::Time::kMicrosecondsPerSecond); |
| 58 continue; | 63 } |
| 59 } | 64 |
| 60 frames -= frames_to_skip; | 65 // Count up all |buffered_| audio based on |time_since_writing|. |
| 61 frames_to_skip = 0; | 66 int64_t frames_played_since_writing = |
| 67 static_cast<int64_t>(time_since_writing.InSecondsF() * sample_rate_); | |
| 68 for (size_t i = 0; i < buffered_.size() && frames_played_since_writing > 0; | |
|
DaleCurtis
2014/08/02 00:51:33
You could cache this too and subtract off time_sin
scherkus (not reviewing)
2014/08/02 01:55:24
Done.
DaleCurtis
2014/08/04 18:55:18
You said done, but didn't do this. Was that your
| |
| 69 ++i) { | |
| 70 int64_t frames_played = | |
| 71 std::min(buffered_[i].frames, frames_played_since_writing); | |
| 72 current_timestamp += base::TimeDelta::FromMicroseconds( | |
|
DaleCurtis
2014/08/02 00:51:32
Ditto.
DaleCurtis
2014/08/04 18:55:18
I meant break out the TimeDelta conversion like ab
| |
| 73 (frames_played * buffered_[i].playback_rate) / sample_rate_ * | |
| 74 base::Time::kMicrosecondsPerSecond); | |
| 75 frames_played_since_writing -= frames_played; | |
| 76 } | |
| 77 | |
| 78 return current_timestamp; | |
| 79 } | |
| 80 | |
| 81 base::TimeDelta AudioClock::ContiguousAudioDataBuffered() const { | |
| 82 base::TimeDelta buffered; | |
| 83 for (size_t i = 0; i < buffered_.size(); ++i) { | |
| 84 // Any buffered silence breaks our contiguous stretch of audio data. | |
| 85 if (buffered_[i].playback_rate == 0) | |
| 86 break; | |
| 87 | |
| 88 // Multiply by playback rate as frames represent time-scaled audio. | |
| 89 buffered += base::TimeDelta::FromMicroseconds( | |
| 90 (buffered_[i].frames * buffered_[i].playback_rate) / sample_rate_ * | |
| 91 base::Time::kMicrosecondsPerSecond); | |
| 92 } | |
| 93 | |
| 94 return buffered; | |
| 95 } | |
| 96 | |
| 97 base::TimeDelta AudioClock::ContiguousAudioDataBufferedAtSameRate() const { | |
| 98 base::TimeDelta buffered; | |
| 99 for (size_t i = 0; i < buffered_.size(); ++i) { | |
| 100 // Any buffered silence breaks our contiguous stretch of audio data. | |
| 101 if (buffered_[i].playback_rate == 0) | |
| 102 break; | |
| 103 | |
| 104 // Multiply by playback rate as frames represent time-scaled audio. | |
| 105 buffered = base::TimeDelta::FromMicroseconds( | |
|
DaleCurtis
2014/08/02 00:51:33
Should this be += ? I don't understand why you hav
scherkus (not reviewing)
2014/08/02 01:55:24
Nah this was just silly. We always break so this d
| |
| 106 (buffered_[i].frames * buffered_[i].playback_rate) / sample_rate_ * | |
| 107 base::Time::kMicrosecondsPerSecond); | |
| 108 | |
| 109 if ((i + 1) < buffered_.size()) { | |
| 110 DCHECK_NE(buffered_[i].playback_rate, buffered_[i + 1].playback_rate) | |
| 111 << "Adjacent AudioData elements shouldn't have same playback rate"; | |
| 62 } | 112 } |
| 63 | 113 |
| 64 // Account for silence ahead of the buffer closest to being played. | |
| 65 if (buffered_audio_[i].playback_rate == 0) { | |
| 66 silence_frames += frames; | |
| 67 continue; | |
| 68 } | |
| 69 | |
| 70 // Multiply by playback rate as frames represent time-scaled audio. | |
| 71 return buffered_audio_[i].endpoint_timestamp - | |
| 72 base::TimeDelta::FromMicroseconds( | |
| 73 ((frames * buffered_audio_[i].playback_rate) + silence_frames) / | |
| 74 sample_rate_ * base::Time::kMicrosecondsPerSecond); | |
| 75 } | |
| 76 | |
| 77 // Either: | |
| 78 // 1) AudioClock is uninitialziated and we'll return kNoTimestamp() | |
| 79 // 2) All previously buffered audio has been replaced by silence, | |
| 80 // meaning media time is now at the last endpoint | |
| 81 return last_endpoint_timestamp_; | |
| 82 } | |
| 83 | |
| 84 void AudioClock::TrimBufferedAudioToMatchDelay(int delay_frames) { | |
| 85 if (buffered_audio_.empty()) | |
| 86 return; | |
| 87 | |
| 88 size_t i = buffered_audio_.size() - 1; | |
| 89 while (true) { | |
| 90 if (buffered_audio_[i].frames <= delay_frames) { | |
| 91 // Reached the end before accounting for all of |delay_frames|. This | |
| 92 // means we haven't written enough audio data yet to account for hardware | |
| 93 // delay. In this case, do nothing. | |
| 94 if (i == 0) | |
| 95 return; | |
| 96 | |
| 97 // Keep accounting for |delay_frames|. | |
| 98 delay_frames -= buffered_audio_[i].frames; | |
| 99 --i; | |
| 100 continue; | |
| 101 } | |
| 102 | |
| 103 // All of |delay_frames| has been accounted for: adjust amount of frames | |
| 104 // left in current buffer. All preceeding elements with index < |i| should | |
| 105 // be considered played out and hence discarded. | |
| 106 buffered_audio_[i].frames = delay_frames; | |
| 107 break; | 114 break; |
| 108 } | 115 } |
| 109 | 116 |
| 110 // At this point |i| points at what will be the new head of |buffered_audio_| | 117 return buffered; |
| 111 // however if it contains no audio it should be removed as well. | |
| 112 if (buffered_audio_[i].frames == 0) | |
| 113 ++i; | |
| 114 | |
| 115 buffered_audio_.erase(buffered_audio_.begin(), buffered_audio_.begin() + i); | |
| 116 } | 118 } |
| 117 | 119 |
| 118 void AudioClock::PushBufferedAudio(int frames, | 120 bool AudioClock::AudioDataBuffered() const { |
| 119 float playback_rate, | 121 for (size_t i = 0; i < buffered_.size(); ++i) { |
| 120 base::TimeDelta endpoint_timestamp) { | 122 if (buffered_[i].playback_rate != 0) { |
| 121 if (playback_rate == 0) | 123 DCHECK_NE(buffered_[i].frames, 0) |
| 122 DCHECK(endpoint_timestamp == kNoTimestamp()); | 124 << "AudioData elements with zero frames shouldn't exist"; |
| 125 return true; | |
| 126 } | |
| 127 } | |
| 128 return false; | |
| 129 } | |
| 123 | 130 |
| 131 AudioClock::AudioData::AudioData(int64_t frames, float playback_rate) | |
|
DaleCurtis
2014/08/02 00:51:33
Up to you, but you can remove this and use a POD-t
| |
| 132 : frames(frames), playback_rate(playback_rate) { | |
| 133 } | |
| 134 | |
| 135 // static | |
| 136 void AudioClock::PushAudioData(std::deque<AudioData>* audio_data, | |
| 137 int64_t frames, | |
| 138 float playback_rate) { | |
| 124 if (frames == 0) | 139 if (frames == 0) |
| 125 return; | 140 return; |
| 126 | 141 |
| 127 // Avoid creating extra elements where possible. | 142 // Avoid creating extra elements where possible. |
| 128 if (!buffered_audio_.empty() && | 143 if (!audio_data->empty() && |
| 129 buffered_audio_.back().playback_rate == playback_rate) { | 144 audio_data->back().playback_rate == playback_rate) { |
| 130 buffered_audio_.back().frames += frames; | 145 audio_data->back().frames += frames; |
| 131 buffered_audio_.back().endpoint_timestamp = endpoint_timestamp; | |
| 132 return; | 146 return; |
| 133 } | 147 } |
| 134 | 148 |
| 135 buffered_audio_.push_back( | 149 audio_data->push_back(AudioData(frames, playback_rate)); |
| 136 BufferedAudio(frames, playback_rate, endpoint_timestamp)); | |
| 137 } | 150 } |
| 138 | 151 |
| 139 AudioClock::BufferedAudio::BufferedAudio(int frames, | 152 // static |
| 140 float playback_rate, | 153 int64_t AudioClock::TotalFrames(const std::deque<AudioData>& audio_data) { |
| 141 base::TimeDelta endpoint_timestamp) | 154 int64_t total_frames = 0; |
| 142 : frames(frames), | 155 for (size_t i = 0; i < audio_data.size(); ++i) { |
| 143 playback_rate(playback_rate), | 156 total_frames += audio_data[i].frames; |
| 144 endpoint_timestamp(endpoint_timestamp) { | 157 } |
| 158 return total_frames; | |
| 145 } | 159 } |
| 146 | 160 |
| 147 } // namespace media | 161 } // namespace media |
| OLD | NEW |