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 3 // found in the LICENSE file. 4 5 #ifndef MEDIA_FILTERS_AUDIO_CLOCK_H_ 6 #define MEDIA_FILTERS_AUDIO_CLOCK_H_ 7 8 #include <deque> 9 10 #include "base/time/time.h" 11 #include "media/base/media_export.h" 12 13 namespace media { 14 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 17 // rate into account to handle scenarios where multiple rates may be present in 18 // a playback pipeline with large delay. 19 class MEDIA_EXPORT AudioClock { 20 public: 21 explicit AudioClock(int sample_rate); 22 ~AudioClock(); 23 24 // |frames| amount of audio data scaled to |playback_rate| was written. 25 // |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, 28 int delay_frames, 29 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 36 // Calculates the current media timestamp taking silence and changes in 37 // playback rate into account. 38 base::TimeDelta CurrentMediaTimestamp() const; 39 40 // Returns the last endpoint timestamp provided to WroteAudio(). last_endpoint_timestamp()41 base::TimeDelta last_endpoint_timestamp() const { 42 return last_endpoint_timestamp_; 43 } 44 45 private: 46 void TrimBufferedAudioToMatchDelay(int delay_frames); 47 void PushBufferedAudio(int frames, 48 float playback_rate, 49 base::TimeDelta endpoint_timestamp); 50 51 const int sample_rate_; 52 53 // Initially set to kNoTimestamp(), otherwise is the last endpoint timestamp 54 // delivered to WroteAudio(). A copy is kept outside of |buffered_audio_| to 55 // handle the case where all of |buffered_audio_| has been replaced with 56 // silence. 57 base::TimeDelta last_endpoint_timestamp_; 58 59 struct BufferedAudio { 60 BufferedAudio(int frames, 61 float playback_rate, 62 base::TimeDelta endpoint_timestamp); 63 64 int frames; 65 float playback_rate; 66 base::TimeDelta endpoint_timestamp; 67 }; 68 69 std::deque<BufferedAudio> buffered_audio_; 70 71 DISALLOW_COPY_AND_ASSIGN(AudioClock); 72 }; 73 74 } // namespace media 75 76 #endif // MEDIA_FILTERS_AUDIO_CLOCK_H_ 77