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> | |
8 | |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "media/base/buffers.h" | 10 #include "media/base/buffers.h" |
9 | 11 |
10 namespace media { | 12 namespace media { |
11 | 13 |
12 AudioClock::AudioClock(int sample_rate) | 14 AudioClock::AudioClock(base::TimeDelta start_timestamp, int sample_rate) |
13 : sample_rate_(sample_rate), last_endpoint_timestamp_(kNoTimestamp()) { | 15 : start_timestamp_(start_timestamp), |
16 sample_rate_(sample_rate), | |
17 total_buffered_frames_(0), | |
18 audio_data_buffered_(0), | |
19 current_media_timestamp_(start_timestamp) { | |
14 } | 20 } |
15 | 21 |
16 AudioClock::~AudioClock() { | 22 AudioClock::~AudioClock() { |
17 } | 23 } |
18 | 24 |
19 void AudioClock::WroteAudio(int frames, | 25 void AudioClock::WroteAudio(int frames_written, |
26 int frames_requested, | |
20 int delay_frames, | 27 int delay_frames, |
21 float playback_rate, | 28 float playback_rate) { |
22 base::TimeDelta timestamp) { | 29 DCHECK_GE(frames_written, 0); |
23 CHECK_GT(playback_rate, 0); | 30 DCHECK_LE(frames_written, frames_requested); |
24 CHECK(timestamp != kNoTimestamp()); | |
25 DCHECK_GE(frames, 0); | |
26 DCHECK_GE(delay_frames, 0); | 31 DCHECK_GE(delay_frames, 0); |
32 DCHECK_GE(playback_rate, 0); | |
27 | 33 |
28 if (last_endpoint_timestamp_ == kNoTimestamp()) | 34 // First write: initialize buffer with silence. |
29 PushBufferedAudio(delay_frames, 0, kNoTimestamp()); | 35 if (buffered_.empty()) { |
36 PushAudioData(&buffered_, delay_frames, 0.0f); | |
37 total_buffered_frames_ = delay_frames; | |
38 } | |
30 | 39 |
31 TrimBufferedAudioToMatchDelay(delay_frames); | 40 // Move frames from |buffered_| to |played_| based on |delay_frames|. |
32 PushBufferedAudio(frames, playback_rate, timestamp); | 41 int64_t played_frames = |
42 std::max(INT64_C(0), total_buffered_frames_ - delay_frames); | |
43 while (played_frames > 0) { | |
44 int64_t frames_to_move = std::min(buffered_.front().frames, played_frames); | |
33 | 45 |
34 last_endpoint_timestamp_ = timestamp; | 46 // No need to keep track of silent audio. |
47 if (buffered_.front().playback_rate > 0.0f) | |
48 PushAudioData(&played_, frames_to_move, buffered_.front().playback_rate); | |
49 | |
50 buffered_.front().frames -= frames_to_move; | |
51 if (buffered_.front().frames == 0) | |
52 buffered_.pop_front(); | |
53 | |
54 played_frames -= frames_to_move; | |
55 } | |
56 | |
57 // Push in newly buffered data. | |
58 PushAudioData(&buffered_, frames_written, playback_rate); | |
59 PushAudioData(&buffered_, frames_requested - frames_written, 0.0f); | |
60 | |
61 // Update cached values. | |
DaleCurtis
2014/08/04 18:55:19
Hmm, I didn't strictly mean a cache. I.e. the tot
scherkus (not reviewing)
2014/08/05 00:55:00
Reorganized code and helper methods.
| |
62 total_buffered_frames_ = 0; | |
63 audio_data_buffered_ = false; | |
64 for (size_t i = 0; i < buffered_.size(); ++i) { | |
65 total_buffered_frames_ += buffered_[i].frames; | |
66 if (buffered_[i].playback_rate > 0.0f) | |
67 audio_data_buffered_ = true; | |
68 } | |
69 | |
70 double scaled_frames = 0; | |
71 for (size_t i = 0; i < played_.size(); ++i) { | |
72 DCHECK_NE(played_[i].playback_rate, 0.0f) | |
73 << "Silent audio doesn't need to be tracked in |played_|."; | |
74 scaled_frames += played_[i].frames * played_[i].playback_rate; | |
75 } | |
76 current_media_timestamp_ = | |
DaleCurtis
2014/08/04 18:55:19
Again, I didn't mean as a strict cache, but a roll
scherkus (not reviewing)
2014/08/05 00:55:00
Ditto.
| |
77 start_timestamp_ + | |
78 base::TimeDelta::FromSecondsD(scaled_frames / sample_rate_); | |
79 | |
80 scaled_frames = 0; | |
81 for (size_t i = 0; i < buffered_.size(); ++i) { | |
82 // Any buffered silence breaks our contiguous stretch of audio data. | |
83 if (buffered_[i].playback_rate == 0) | |
84 break; | |
85 scaled_frames += (buffered_[i].frames * buffered_[i].playback_rate); | |
86 } | |
87 contiguous_audio_data_buffered_ = | |
88 base::TimeDelta::FromSecondsD(scaled_frames / sample_rate_); | |
89 | |
90 scaled_frames = 0; | |
DaleCurtis
2014/08/04 18:55:19
If you calculate this first you can avoid the for
scherkus (not reviewing)
2014/08/05 00:55:00
Ditto.
| |
91 if (buffered_.front().playback_rate > 0.0f) { | |
92 scaled_frames = buffered_.front().frames * buffered_.front().playback_rate; | |
93 } | |
94 contiguous_audio_data_buffered_at_same_rate_ = | |
95 base::TimeDelta::FromSecondsD(scaled_frames / sample_rate_); | |
35 } | 96 } |
36 | 97 |
37 void AudioClock::WroteSilence(int frames, int delay_frames) { | 98 base::TimeDelta AudioClock::CurrentMediaTimestampSinceWriting( |
38 DCHECK_GE(frames, 0); | 99 base::TimeDelta time_since_writing) const { |
39 DCHECK_GE(delay_frames, 0); | 100 base::TimeDelta computed_timestamp = current_media_timestamp_; |
40 | 101 |
41 if (last_endpoint_timestamp_ == kNoTimestamp()) | 102 // Count up all |buffered_| audio based on |time_since_writing|. |
42 PushBufferedAudio(delay_frames, 0, kNoTimestamp()); | 103 int64_t frames_played_since_writing = |
104 static_cast<int64_t>(time_since_writing.InSecondsF() * sample_rate_); | |
105 for (size_t i = 0; i < buffered_.size() && frames_played_since_writing > 0; | |
106 ++i) { | |
107 int64_t frames_played = | |
108 std::min(buffered_[i].frames, frames_played_since_writing); | |
109 computed_timestamp += base::TimeDelta::FromMicroseconds( | |
110 (frames_played * buffered_[i].playback_rate) / sample_rate_ * | |
111 base::Time::kMicrosecondsPerSecond); | |
112 frames_played_since_writing -= frames_played; | |
113 } | |
43 | 114 |
44 TrimBufferedAudioToMatchDelay(delay_frames); | 115 return computed_timestamp; |
45 PushBufferedAudio(frames, 0, kNoTimestamp()); | |
46 } | 116 } |
47 | 117 |
48 base::TimeDelta AudioClock::CurrentMediaTimestamp( | 118 AudioClock::AudioData::AudioData(int64_t frames, float playback_rate) |
49 base::TimeDelta time_since_writing) const { | 119 : frames(frames), playback_rate(playback_rate) { |
50 int frames_to_skip = | |
51 static_cast<int>(time_since_writing.InSecondsF() * sample_rate_); | |
52 int silence_frames = 0; | |
53 for (size_t i = 0; i < buffered_audio_.size(); ++i) { | |
54 int frames = buffered_audio_[i].frames; | |
55 if (frames_to_skip > 0) { | |
56 if (frames <= frames_to_skip) { | |
57 frames_to_skip -= frames; | |
58 continue; | |
59 } | |
60 frames -= frames_to_skip; | |
61 frames_to_skip = 0; | |
62 } | |
63 | |
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 } | 120 } |
83 | 121 |
84 void AudioClock::TrimBufferedAudioToMatchDelay(int delay_frames) { | 122 // static |
85 if (buffered_audio_.empty()) | 123 void AudioClock::PushAudioData(std::deque<AudioData>* audio_data, |
86 return; | 124 int64_t frames, |
87 | 125 float playback_rate) { |
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; | |
108 } | |
109 | |
110 // At this point |i| points at what will be the new head of |buffered_audio_| | |
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 } | |
117 | |
118 void AudioClock::PushBufferedAudio(int frames, | |
119 float playback_rate, | |
120 base::TimeDelta endpoint_timestamp) { | |
121 if (playback_rate == 0) | |
122 DCHECK(endpoint_timestamp == kNoTimestamp()); | |
123 | |
124 if (frames == 0) | 126 if (frames == 0) |
125 return; | 127 return; |
126 | 128 |
127 // Avoid creating extra elements where possible. | 129 // Avoid creating extra elements where possible. |
128 if (!buffered_audio_.empty() && | 130 if (!audio_data->empty() && |
129 buffered_audio_.back().playback_rate == playback_rate) { | 131 audio_data->back().playback_rate == playback_rate) { |
130 buffered_audio_.back().frames += frames; | 132 audio_data->back().frames += frames; |
131 buffered_audio_.back().endpoint_timestamp = endpoint_timestamp; | |
132 return; | 133 return; |
133 } | 134 } |
134 | 135 |
135 buffered_audio_.push_back( | 136 audio_data->push_back(AudioData(frames, playback_rate)); |
136 BufferedAudio(frames, playback_rate, endpoint_timestamp)); | |
137 } | |
138 | |
139 AudioClock::BufferedAudio::BufferedAudio(int frames, | |
140 float playback_rate, | |
141 base::TimeDelta endpoint_timestamp) | |
142 : frames(frames), | |
143 playback_rate(playback_rate), | |
144 endpoint_timestamp(endpoint_timestamp) { | |
145 } | 137 } |
146 | 138 |
147 } // namespace media | 139 } // namespace media |
OLD | NEW |