| 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 <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "media/base/buffers.h" | 10 #include "media/base/buffers.h" |
| 11 | 11 |
| 12 namespace media { | 12 namespace media { |
| 13 | 13 |
| 14 AudioClock::AudioClock(base::TimeDelta start_timestamp, int sample_rate) | 14 AudioClock::AudioClock(base::TimeDelta start_timestamp, int sample_rate) |
| 15 : start_timestamp_(start_timestamp), | 15 : start_timestamp_(start_timestamp), |
| 16 sample_rate_(sample_rate), | 16 sample_rate_(sample_rate), |
| 17 microseconds_per_frame_( | 17 microseconds_per_frame_( |
| 18 static_cast<double>(base::Time::kMicrosecondsPerSecond) / | 18 static_cast<double>(base::Time::kMicrosecondsPerSecond) / |
| 19 sample_rate), | 19 sample_rate), |
| 20 total_buffered_frames_(0), | 20 total_buffered_frames_(0), |
| 21 current_media_timestamp_(start_timestamp), | 21 front_timestamp_(start_timestamp), |
| 22 audio_data_buffered_(0) { | 22 back_timestamp_(start_timestamp) { |
| 23 } | 23 } |
| 24 | 24 |
| 25 AudioClock::~AudioClock() { | 25 AudioClock::~AudioClock() { |
| 26 } | 26 } |
| 27 | 27 |
| 28 void AudioClock::WroteAudio(int frames_written, | 28 void AudioClock::WroteAudio(int frames_written, |
| 29 int frames_requested, | 29 int frames_requested, |
| 30 int delay_frames, | 30 int delay_frames, |
| 31 float playback_rate) { | 31 float playback_rate) { |
| 32 DCHECK_GE(frames_written, 0); | 32 DCHECK_GE(frames_written, 0); |
| 33 DCHECK_LE(frames_written, frames_requested); | 33 DCHECK_LE(frames_written, frames_requested); |
| 34 DCHECK_GE(delay_frames, 0); | 34 DCHECK_GE(delay_frames, 0); |
| 35 DCHECK_GE(playback_rate, 0); | 35 DCHECK_GE(playback_rate, 0); |
| 36 | 36 |
| 37 // First write: initialize buffer with silence. | 37 // First write: initialize buffer with silence. |
| 38 if (start_timestamp_ == current_media_timestamp_ && buffered_.empty()) | 38 if (start_timestamp_ == front_timestamp_ && buffered_.empty()) |
| 39 PushBufferedAudioData(delay_frames, 0.0f); | 39 PushBufferedAudioData(delay_frames, 0.0f); |
| 40 | 40 |
| 41 // Move frames from |buffered_| into the computed timestamp based on | 41 // Move frames from |buffered_| into the computed timestamp based on |
| 42 // |delay_frames|. | 42 // |delay_frames|. |
| 43 // | 43 // |
| 44 // The ordering of compute -> push -> pop eliminates unnecessary memory | 44 // The ordering of compute -> push -> pop eliminates unnecessary memory |
| 45 // reallocations in cases where |buffered_| gets emptied. | 45 // reallocations in cases where |buffered_| gets emptied. |
| 46 int64_t frames_played = | 46 int64_t frames_played = |
| 47 std::max(INT64_C(0), total_buffered_frames_ - delay_frames); | 47 std::max(INT64_C(0), total_buffered_frames_ - delay_frames); |
| 48 current_media_timestamp_ += ComputeBufferedMediaTime(frames_played); | 48 front_timestamp_ += ComputeBufferedMediaTime(frames_played); |
| 49 PushBufferedAudioData(frames_written, playback_rate); | 49 PushBufferedAudioData(frames_written, playback_rate); |
| 50 PushBufferedAudioData(frames_requested - frames_written, 0.0f); | 50 PushBufferedAudioData(frames_requested - frames_written, 0.0f); |
| 51 PopBufferedAudioData(frames_played); | 51 PopBufferedAudioData(frames_played); |
| 52 | 52 |
| 53 back_timestamp_ += base::TimeDelta::FromMicroseconds( |
| 54 frames_written * playback_rate * microseconds_per_frame_); |
| 55 |
| 53 // Update cached values. | 56 // Update cached values. |
| 54 double scaled_frames = 0; | 57 double scaled_frames = 0; |
| 55 double scaled_frames_at_same_rate = 0; | 58 double scaled_frames_at_same_rate = 0; |
| 56 bool found_silence = false; | 59 bool found_silence = false; |
| 57 audio_data_buffered_ = false; | |
| 58 for (size_t i = 0; i < buffered_.size(); ++i) { | 60 for (size_t i = 0; i < buffered_.size(); ++i) { |
| 59 if (buffered_[i].playback_rate == 0) { | 61 if (buffered_[i].playback_rate == 0) { |
| 60 found_silence = true; | 62 found_silence = true; |
| 61 continue; | 63 continue; |
| 62 } | 64 } |
| 63 | 65 |
| 64 audio_data_buffered_ = true; | |
| 65 | |
| 66 // Any buffered silence breaks our contiguous stretch of audio data. | 66 // Any buffered silence breaks our contiguous stretch of audio data. |
| 67 if (found_silence) | 67 if (found_silence) |
| 68 break; | 68 break; |
| 69 | 69 |
| 70 scaled_frames += (buffered_[i].frames * buffered_[i].playback_rate); | 70 scaled_frames += (buffered_[i].frames * buffered_[i].playback_rate); |
| 71 | 71 |
| 72 if (i == 0) | 72 if (i == 0) |
| 73 scaled_frames_at_same_rate = scaled_frames; | 73 scaled_frames_at_same_rate = scaled_frames; |
| 74 } | 74 } |
| 75 | 75 |
| 76 contiguous_audio_data_buffered_ = base::TimeDelta::FromMicroseconds( | 76 contiguous_audio_data_buffered_ = base::TimeDelta::FromMicroseconds( |
| 77 scaled_frames * microseconds_per_frame_); | 77 scaled_frames * microseconds_per_frame_); |
| 78 contiguous_audio_data_buffered_at_same_rate_ = | 78 contiguous_audio_data_buffered_at_same_rate_ = |
| 79 base::TimeDelta::FromMicroseconds(scaled_frames_at_same_rate * | 79 base::TimeDelta::FromMicroseconds(scaled_frames_at_same_rate * |
| 80 microseconds_per_frame_); | 80 microseconds_per_frame_); |
| 81 } | 81 } |
| 82 | 82 |
| 83 base::TimeDelta AudioClock::CurrentMediaTimestampSinceWriting( | 83 base::TimeDelta AudioClock::TimestampSinceWriting( |
| 84 base::TimeDelta time_since_writing) const { | 84 base::TimeDelta time_since_writing) const { |
| 85 int64_t frames_played_since_writing = std::min( | 85 int64_t frames_played_since_writing = std::min( |
| 86 total_buffered_frames_, | 86 total_buffered_frames_, |
| 87 static_cast<int64_t>(time_since_writing.InSecondsF() * sample_rate_)); | 87 static_cast<int64_t>(time_since_writing.InSecondsF() * sample_rate_)); |
| 88 return current_media_timestamp_ + | 88 return front_timestamp_ + |
| 89 ComputeBufferedMediaTime(frames_played_since_writing); | 89 ComputeBufferedMediaTime(frames_played_since_writing); |
| 90 } | 90 } |
| 91 | 91 |
| 92 AudioClock::AudioData::AudioData(int64_t frames, float playback_rate) | 92 AudioClock::AudioData::AudioData(int64_t frames, float playback_rate) |
| 93 : frames(frames), playback_rate(playback_rate) { | 93 : frames(frames), playback_rate(playback_rate) { |
| 94 } | 94 } |
| 95 | 95 |
| 96 void AudioClock::PushBufferedAudioData(int64_t frames, float playback_rate) { | 96 void AudioClock::PushBufferedAudioData(int64_t frames, float playback_rate) { |
| 97 if (frames == 0) | 97 if (frames == 0) |
| 98 return; | 98 return; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 int64_t min_frames = std::min(buffered_[i].frames, frames); | 131 int64_t min_frames = std::min(buffered_[i].frames, frames); |
| 132 scaled_frames += min_frames * buffered_[i].playback_rate; | 132 scaled_frames += min_frames * buffered_[i].playback_rate; |
| 133 frames -= min_frames; | 133 frames -= min_frames; |
| 134 } | 134 } |
| 135 | 135 |
| 136 return base::TimeDelta::FromMicroseconds(scaled_frames * | 136 return base::TimeDelta::FromMicroseconds(scaled_frames * |
| 137 microseconds_per_frame_); | 137 microseconds_per_frame_); |
| 138 } | 138 } |
| 139 | 139 |
| 140 } // namespace media | 140 } // namespace media |
| OLD | NEW |