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 microseconds_per_frame_( | |
scherkus (not reviewing)
2014/08/05 00:55:00
Using a rolling calculation involving division act
| |
18 static_cast<double>(base::Time::kMicrosecondsPerSecond) / | |
19 sample_rate), | |
20 total_buffered_frames_(0), | |
21 audio_data_buffered_(0), | |
22 current_media_timestamp_(start_timestamp) { | |
14 } | 23 } |
15 | 24 |
16 AudioClock::~AudioClock() { | 25 AudioClock::~AudioClock() { |
17 } | 26 } |
18 | 27 |
19 void AudioClock::WroteAudio(int frames, | 28 void AudioClock::WroteAudio(int frames_written, |
29 int frames_requested, | |
20 int delay_frames, | 30 int delay_frames, |
21 float playback_rate, | 31 float playback_rate) { |
22 base::TimeDelta timestamp) { | 32 DCHECK_GE(frames_written, 0); |
23 CHECK_GT(playback_rate, 0); | 33 DCHECK_LE(frames_written, frames_requested); |
24 CHECK(timestamp != kNoTimestamp()); | |
25 DCHECK_GE(frames, 0); | |
26 DCHECK_GE(delay_frames, 0); | 34 DCHECK_GE(delay_frames, 0); |
35 DCHECK_GE(playback_rate, 0); | |
27 | 36 |
28 if (last_endpoint_timestamp_ == kNoTimestamp()) | 37 // First write: initialize buffer with silence. |
29 PushBufferedAudio(delay_frames, 0, kNoTimestamp()); | 38 if (start_timestamp_ == current_media_timestamp_ && buffered_.empty()) |
39 PushBufferedAudioData(delay_frames, 0.0f); | |
30 | 40 |
31 TrimBufferedAudioToMatchDelay(delay_frames); | 41 // Move frames from |buffered_| into the computed timestamp based on |
32 PushBufferedAudio(frames, playback_rate, timestamp); | 42 // |delay_frames|. |
43 // | |
44 // The ordering of compute -> push -> pop eliminates unnecessary memory | |
45 // reallocations in cases where |buffered_| gets emptied. | |
46 int64_t frames_played = | |
47 std::max(INT64_C(0), total_buffered_frames_ - delay_frames); | |
48 current_media_timestamp_ += ComputeBufferedMediaTime(frames_played); | |
49 PushBufferedAudioData(frames_written, playback_rate); | |
50 PushBufferedAudioData(frames_requested - frames_written, 0.0f); | |
51 PopBufferedAudioData(frames_played); | |
33 | 52 |
34 last_endpoint_timestamp_ = timestamp; | 53 // Update cached values. |
35 } | 54 double scaled_frames = 0; |
36 | 55 double scaled_frames_at_same_rate = 0; |
37 void AudioClock::WroteSilence(int frames, int delay_frames) { | 56 bool found_silence = false; |
38 DCHECK_GE(frames, 0); | 57 audio_data_buffered_ = false; |
39 DCHECK_GE(delay_frames, 0); | 58 for (size_t i = 0; i < buffered_.size(); ++i) { |
40 | 59 if (buffered_[i].playback_rate == 0) { |
41 if (last_endpoint_timestamp_ == kNoTimestamp()) | 60 found_silence = true; |
42 PushBufferedAudio(delay_frames, 0, kNoTimestamp()); | |
43 | |
44 TrimBufferedAudioToMatchDelay(delay_frames); | |
45 PushBufferedAudio(frames, 0, kNoTimestamp()); | |
46 } | |
47 | |
48 base::TimeDelta AudioClock::CurrentMediaTimestamp( | |
49 base::TimeDelta time_since_writing) const { | |
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; | 61 continue; |
68 } | 62 } |
69 | 63 |
70 // Multiply by playback rate as frames represent time-scaled audio. | 64 audio_data_buffered_ = true; |
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 | 65 |
77 // Either: | 66 // Any buffered silence breaks our contiguous stretch of audio data. |
78 // 1) AudioClock is uninitialziated and we'll return kNoTimestamp() | 67 if (found_silence) { |
79 // 2) All previously buffered audio has been replaced by silence, | 68 // Exit loop early if we've discovered all data we care about. |
80 // meaning media time is now at the last endpoint | 69 if (audio_data_buffered_) |
DaleCurtis
2014/08/05 01:21:31
This is always true at this point, no? Also, foun
scherkus (not reviewing)
2014/08/05 01:44:01
Nah this was just a result of shuffling some code
| |
81 return last_endpoint_timestamp_; | 70 break; |
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; | 71 continue; |
101 } | 72 } |
102 | 73 |
103 // All of |delay_frames| has been accounted for: adjust amount of frames | 74 scaled_frames += (buffered_[i].frames * buffered_[i].playback_rate); |
DaleCurtis
2014/08/05 01:21:31
You can reuse this value below.
scherkus (not reviewing)
2014/08/05 01:44:01
Done.
| |
104 // left in current buffer. All preceeding elements with index < |i| should | 75 |
105 // be considered played out and hence discarded. | 76 if (i == 0) { |
106 buffered_audio_[i].frames = delay_frames; | 77 scaled_frames_at_same_rate = |
107 break; | 78 buffered_[i].frames * buffered_[i].playback_rate; |
79 } | |
108 } | 80 } |
109 | 81 |
110 // At this point |i| points at what will be the new head of |buffered_audio_| | 82 contiguous_audio_data_buffered_ = base::TimeDelta::FromMicroseconds( |
111 // however if it contains no audio it should be removed as well. | 83 scaled_frames * microseconds_per_frame_); |
112 if (buffered_audio_[i].frames == 0) | 84 contiguous_audio_data_buffered_at_same_rate_ = |
113 ++i; | 85 base::TimeDelta::FromMicroseconds(scaled_frames_at_same_rate * |
114 | 86 microseconds_per_frame_); |
115 buffered_audio_.erase(buffered_audio_.begin(), buffered_audio_.begin() + i); | |
116 } | 87 } |
117 | 88 |
118 void AudioClock::PushBufferedAudio(int frames, | 89 base::TimeDelta AudioClock::CurrentMediaTimestampSinceWriting( |
119 float playback_rate, | 90 base::TimeDelta time_since_writing) const { |
120 base::TimeDelta endpoint_timestamp) { | 91 int64_t frames_played_since_writing = |
121 if (playback_rate == 0) | 92 static_cast<int64_t>(time_since_writing.InSecondsF() * sample_rate_); |
122 DCHECK(endpoint_timestamp == kNoTimestamp()); | 93 return current_media_timestamp_ + |
94 ComputeBufferedMediaTime(frames_played_since_writing); | |
95 } | |
123 | 96 |
97 AudioClock::AudioData::AudioData(int64_t frames, float playback_rate) | |
98 : frames(frames), playback_rate(playback_rate) { | |
99 } | |
100 | |
101 void AudioClock::PushBufferedAudioData(int64_t frames, float playback_rate) { | |
124 if (frames == 0) | 102 if (frames == 0) |
125 return; | 103 return; |
126 | 104 |
105 total_buffered_frames_ += frames; | |
106 | |
127 // Avoid creating extra elements where possible. | 107 // Avoid creating extra elements where possible. |
128 if (!buffered_audio_.empty() && | 108 if (!buffered_.empty() && |
129 buffered_audio_.back().playback_rate == playback_rate) { | 109 buffered_.back().playback_rate == playback_rate) { |
130 buffered_audio_.back().frames += frames; | 110 buffered_.back().frames += frames; |
131 buffered_audio_.back().endpoint_timestamp = endpoint_timestamp; | |
132 return; | 111 return; |
133 } | 112 } |
134 | 113 |
135 buffered_audio_.push_back( | 114 buffered_.push_back(AudioData(frames, playback_rate)); |
136 BufferedAudio(frames, playback_rate, endpoint_timestamp)); | |
137 } | 115 } |
138 | 116 |
139 AudioClock::BufferedAudio::BufferedAudio(int frames, | 117 void AudioClock::PopBufferedAudioData(int64_t frames) { |
140 float playback_rate, | 118 DCHECK_LE(frames, total_buffered_frames_); |
141 base::TimeDelta endpoint_timestamp) | 119 |
142 : frames(frames), | 120 total_buffered_frames_ -= frames; |
143 playback_rate(playback_rate), | 121 |
144 endpoint_timestamp(endpoint_timestamp) { | 122 while (frames > 0) { |
123 int64_t frames_to_pop = std::min(buffered_.front().frames, frames); | |
124 buffered_.front().frames -= frames_to_pop; | |
DaleCurtis
2014/08/05 01:21:31
Is this correct? You're subtracting frames at diff
scherkus (not reviewing)
2014/08/05 01:44:01
I'm assuming you mean different playback rates, bu
DaleCurtis
2014/08/05 01:45:51
Don't you use this above to compute the media time
scherkus (not reviewing)
2014/08/05 01:57:35
If we the audio hardware tells us it's played 100
DaleCurtis
2014/08/05 02:13:47
Ahh, right, my mistake.
| |
125 if (buffered_.front().frames == 0) | |
126 buffered_.pop_front(); | |
127 | |
128 frames -= frames_to_pop; | |
129 } | |
130 } | |
131 | |
132 base::TimeDelta AudioClock::ComputeBufferedMediaTime(int64_t frames) const { | |
133 DCHECK_LE(frames, total_buffered_frames_); | |
134 | |
135 base::TimeDelta media_time; | |
136 for (size_t i = 0; i < buffered_.size() && frames > 0; ++i) { | |
137 int64_t min_frames = std::min(buffered_[i].frames, frames); | |
138 media_time += base::TimeDelta::FromMicroseconds( | |
DaleCurtis
2014/08/05 01:21:31
Convert to int64/TimeDelta outside of loop like ab
scherkus (not reviewing)
2014/08/05 01:44:01
Done.
| |
139 min_frames * buffered_[i].playback_rate * microseconds_per_frame_); | |
140 frames -= min_frames; | |
141 } | |
142 | |
143 return media_time; | |
145 } | 144 } |
146 | 145 |
147 } // namespace media | 146 } // namespace media |
OLD | NEW |