| 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 <deque> | 8 #include <deque> |
| 9 | 9 |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "media/base/media_export.h" | 11 #include "media/base/media_export.h" |
| 12 | 12 |
| 13 namespace media { | 13 namespace media { |
| 14 | 14 |
| 15 // Models a queue of buffered audio in a playback pipeline for use with | 15 // Models a queue of buffered audio in a playback pipeline for use with |
| 16 // estimating the amount of delay in wall clock time. Takes changes in playback | 16 // estimating the amount of delay in wall clock time. Takes changes in playback |
| 17 // rate into account to handle scenarios where multiple rates may be present in | 17 // rate into account to handle scenarios where multiple rates may be present in |
| 18 // a playback pipeline with large delay. | 18 // a playback pipeline with large delay. |
| 19 class MEDIA_EXPORT AudioClock { | 19 class MEDIA_EXPORT AudioClock { |
| 20 public: | 20 public: |
| 21 explicit AudioClock(int sample_rate); | 21 AudioClock(base::TimeDelta start_timestamp, int sample_rate); |
| 22 ~AudioClock(); | 22 ~AudioClock(); |
| 23 | 23 |
| 24 // |frames| amount of audio data scaled to |playback_rate| was written. | 24 // |frames_written| amount of audio data scaled to |playback_rate| written. |
| 25 // |frames_requested| amount of audio data requested by hardware. |
| 25 // |delay_frames| is the current amount of hardware delay. | 26 // |delay_frames| is the current amount of hardware delay. |
| 26 // |timestamp| is the endpoint media timestamp of the audio data written. | 27 void WroteAudio(int frames_written, |
| 27 void WroteAudio(int frames, | 28 int frames_requested, |
| 28 int delay_frames, | 29 int delay_frames, |
| 29 float playback_rate, | 30 float playback_rate); |
| 30 base::TimeDelta timestamp); | |
| 31 | |
| 32 // |frames| amount of silence was written. | |
| 33 // |delay_frames| is the current amount of hardware delay. | |
| 34 void WroteSilence(int frames, int delay_frames); | |
| 35 | 31 |
| 36 // Calculates the current media timestamp taking silence and changes in | 32 // Calculates the current media timestamp taking silence and changes in |
| 37 // playback rate into account. | 33 // playback rate into account. |
| 38 // | 34 // |
| 39 // Clients can provide |time_since_writing| to simulate the passage of time | 35 // Clients can provide |time_since_writing| to simulate the passage of time |
| 40 // since last writing audio to get a more accurate current media timestamp. | 36 // since last writing audio to get a more accurate current media timestamp. |
| 41 base::TimeDelta CurrentMediaTimestamp( | 37 base::TimeDelta CurrentMediaTimestamp( |
| 42 base::TimeDelta time_since_writing) const; | 38 base::TimeDelta time_since_writing) const; |
| 43 | 39 |
| 44 // Returns the last endpoint timestamp provided to WroteAudio(). | 40 // Returns the amount of contiguous media time buffered at the head of the |
| 45 base::TimeDelta last_endpoint_timestamp() const { | 41 // audio hardware buffer. Silence introduced into the audio hardware buffer is |
| 46 return last_endpoint_timestamp_; | 42 // treated as a break in media time. |
| 47 } | 43 base::TimeDelta ContiguousAudioDataBuffered() const; |
| 44 |
| 45 // Same as above, but also treats changes in playback rate as a break in media |
| 46 // time. |
| 47 base::TimeDelta ContiguousAudioDataBufferedAtSameRate() const; |
| 48 |
| 49 // Returns true if there is any audio data buffered by the audio hardware, |
| 50 // even if there is silence mixed in. |
| 51 bool AudioDataBuffered() const; |
| 48 | 52 |
| 49 private: | 53 private: |
| 50 void TrimBufferedAudioToMatchDelay(int delay_frames); | 54 // Even with a ridiculously high sample rate of 256kHz, using 64 bits will |
| 51 void PushBufferedAudio(int frames, | 55 // permit tracking up to 416999965 days worth of time (that's 1141 millenia). |
| 52 float playback_rate, | 56 // |
| 53 base::TimeDelta endpoint_timestamp); | 57 // 32 bits on the other hand would top out at measly 2 hours and 20 minutes. |
| 58 struct AudioData { |
| 59 AudioData(int64_t frames, float playback_rate); |
| 54 | 60 |
| 61 int64_t frames; |
| 62 float playback_rate; |
| 63 }; |
| 64 |
| 65 // Helpers for operating on a std::deque<AudioData>. |
| 66 static void PushAudioData(std::deque<AudioData>* audio_data, |
| 67 int64_t frames, |
| 68 float playback_rate); |
| 69 static int64_t TotalFrames(const std::deque<AudioData>& audio_data); |
| 70 |
| 71 const base::TimeDelta start_timestamp_; |
| 55 const int sample_rate_; | 72 const int sample_rate_; |
| 56 | 73 |
| 57 // Initially set to kNoTimestamp(), otherwise is the last endpoint timestamp | 74 std::deque<AudioData> buffered_; |
| 58 // delivered to WroteAudio(). A copy is kept outside of |buffered_audio_| to | 75 std::deque<AudioData> played_; |
| 59 // handle the case where all of |buffered_audio_| has been replaced with | |
| 60 // silence. | |
| 61 base::TimeDelta last_endpoint_timestamp_; | |
| 62 | |
| 63 struct BufferedAudio { | |
| 64 BufferedAudio(int frames, | |
| 65 float playback_rate, | |
| 66 base::TimeDelta endpoint_timestamp); | |
| 67 | |
| 68 int frames; | |
| 69 float playback_rate; | |
| 70 base::TimeDelta endpoint_timestamp; | |
| 71 }; | |
| 72 | |
| 73 std::deque<BufferedAudio> buffered_audio_; | |
| 74 | 76 |
| 75 DISALLOW_COPY_AND_ASSIGN(AudioClock); | 77 DISALLOW_COPY_AND_ASSIGN(AudioClock); |
| 76 }; | 78 }; |
| 77 | 79 |
| 78 } // namespace media | 80 } // namespace media |
| 79 | 81 |
| 80 #endif // MEDIA_FILTERS_AUDIO_CLOCK_H_ | 82 #endif // MEDIA_FILTERS_AUDIO_CLOCK_H_ |
| OLD | NEW |