| 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 #ifndef MEDIA_FILTERS_AUDIO_CLOCK_H_ | 5 #ifndef MEDIA_FILTERS_AUDIO_CLOCK_H_ |
| 6 #define MEDIA_FILTERS_AUDIO_CLOCK_H_ | 6 #define MEDIA_FILTERS_AUDIO_CLOCK_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <cmath> |
| 10 #include <deque> | 11 #include <deque> |
| 11 | 12 |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 14 #include "media/base/media_export.h" | 15 #include "media/base/media_export.h" |
| 15 | 16 |
| 16 namespace media { | 17 namespace media { |
| 17 | 18 |
| 18 // Models a queue of buffered audio in a playback pipeline for use with | 19 // Models a queue of buffered audio in a playback pipeline for use with |
| 19 // estimating the amount of delay in wall clock time. Takes changes in playback | 20 // estimating the amount of delay in wall clock time. Takes changes in playback |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 // +-----------------------+-----------------------+-----------------------+ | 83 // +-----------------------+-----------------------+-----------------------+ |
| 83 // | 10 frames silence | 20 frames @ 1.0x | 20 frames @ 0.5x | | 84 // | 10 frames silence | 20 frames @ 1.0x | 20 frames @ 0.5x | |
| 84 // | = 250 ms (wall) | = 500 ms (wall) | = 500 ms (wall) | | 85 // | = 250 ms (wall) | = 500 ms (wall) | = 500 ms (wall) | |
| 85 // | = 0 ms (media) | = 500 ms (media) | = 250 ms (media) | | 86 // | = 0 ms (media) | = 500 ms (media) | = 250 ms (media) | |
| 86 // +-----------------------+-----------------------+-----------------------+ | 87 // +-----------------------+-----------------------+-----------------------+ |
| 87 // ^ ^ | 88 // ^ ^ |
| 88 // front_timestamp() is equal to back_timestamp() is equal to | 89 // front_timestamp() is equal to back_timestamp() is equal to |
| 89 // |start_timestamp| since no amount of media frames tracked | 90 // |start_timestamp| since no amount of media frames tracked |
| 90 // media data has been played yet. by AudioClock, which would be | 91 // media data has been played yet. by AudioClock, which would be |
| 91 // 1000 + 500 + 250 = 1750 ms. | 92 // 1000 + 500 + 250 = 1750 ms. |
| 92 base::TimeDelta front_timestamp() const { return front_timestamp_; } | 93 base::TimeDelta front_timestamp() const { |
| 93 base::TimeDelta back_timestamp() const { return back_timestamp_; } | 94 return base::TimeDelta::FromMicroseconds( |
| 95 std::round(front_timestamp_micros_)); |
| 96 } |
| 97 base::TimeDelta back_timestamp() const { |
| 98 return base::TimeDelta::FromMicroseconds( |
| 99 std::round(back_timestamp_micros_)); |
| 100 } |
| 94 | 101 |
| 95 // Returns the amount of wall time until |timestamp| will be played by the | 102 // Returns the amount of wall time until |timestamp| will be played by the |
| 96 // audio hardware. | 103 // audio hardware. |
| 97 // | 104 // |
| 98 // |timestamp| must be within front_timestamp() and back_timestamp(). | 105 // |timestamp| must be within front_timestamp() and back_timestamp(). |
| 99 base::TimeDelta TimeUntilPlayback(base::TimeDelta timestamp) const; | 106 base::TimeDelta TimeUntilPlayback(base::TimeDelta timestamp) const; |
| 100 | 107 |
| 101 void ContiguousAudioDataBufferedForTesting( | 108 void ContiguousAudioDataBufferedForTesting( |
| 102 base::TimeDelta* total, | 109 base::TimeDelta* total, |
| 103 base::TimeDelta* same_rate_total) const; | 110 base::TimeDelta* same_rate_total) const; |
| 104 | 111 |
| 105 private: | 112 private: |
| 106 // Even with a ridiculously high sample rate of 256kHz, using 64 bits will | 113 // Even with a ridiculously high sample rate of 256kHz, using 64 bits will |
| 107 // permit tracking up to 416999965 days worth of time (that's 1141 millenia). | 114 // permit tracking up to 416999965 days worth of time (that's 1141 millenia). |
| 108 // | 115 // |
| 109 // 32 bits on the other hand would top out at measly 2 hours and 20 minutes. | 116 // 32 bits on the other hand would top out at measly 2 hours and 20 minutes. |
| 110 struct AudioData { | 117 struct AudioData { |
| 111 AudioData(int64_t frames, double playback_rate); | 118 AudioData(int64_t frames, double playback_rate); |
| 112 | 119 |
| 113 int64_t frames; | 120 int64_t frames; |
| 114 double playback_rate; | 121 double playback_rate; |
| 115 }; | 122 }; |
| 116 | 123 |
| 117 // Helpers for operating on |buffered_|. | 124 // Helpers for operating on |buffered_|. |
| 118 void PushBufferedAudioData(int64_t frames, double playback_rate); | 125 void PushBufferedAudioData(int64_t frames, double playback_rate); |
| 119 void PopBufferedAudioData(int64_t frames); | 126 void PopBufferedAudioData(int64_t frames); |
| 120 base::TimeDelta ComputeBufferedMediaDuration() const; | 127 double ComputeBufferedMediaDurationMicros() const; |
| 121 | 128 |
| 122 const base::TimeDelta start_timestamp_; | 129 const base::TimeDelta start_timestamp_; |
| 123 const double microseconds_per_frame_; | 130 const double microseconds_per_frame_; |
| 124 | 131 |
| 125 std::deque<AudioData> buffered_; | 132 std::deque<AudioData> buffered_; |
| 126 int64_t total_buffered_frames_; | 133 int64_t total_buffered_frames_; |
| 127 | 134 |
| 128 base::TimeDelta front_timestamp_; | 135 // Use double rather than TimeDelta to avoid loss of partial microseconds when |
| 129 base::TimeDelta back_timestamp_; | 136 // converting between frames-written/delayed and time-passed (see conversion |
| 137 // in WroteAudio()). Particularly for |back_timestamp|, which accumulates more |
| 138 // time with each call to WroteAudio(), the loss of precision can accumulate |
| 139 // to create noticeable audio/video sync drift for longer (2-3 hr) videos. |
| 140 // See http://crbug.com/564604. |
| 141 double front_timestamp_micros_; |
| 142 double back_timestamp_micros_; |
| 130 | 143 |
| 131 DISALLOW_COPY_AND_ASSIGN(AudioClock); | 144 DISALLOW_COPY_AND_ASSIGN(AudioClock); |
| 132 }; | 145 }; |
| 133 | 146 |
| 134 } // namespace media | 147 } // namespace media |
| 135 | 148 |
| 136 #endif // MEDIA_FILTERS_AUDIO_CLOCK_H_ | 149 #endif // MEDIA_FILTERS_AUDIO_CLOCK_H_ |
| OLD | NEW |