| 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 current_media_timestamp_(start_timestamp), |
| 22 audio_data_buffered_(0) { | 22 latest_media_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); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 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 current_media_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 latest_media_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 |
| (...skipping 55 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 |