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.
5 #ifndef MEDIA_FILTERS_AUDIO_CLOCK_H_
6 #define MEDIA_FILTERS_AUDIO_CLOCK_H_
10 #include "base/time/time.h"
11 #include "media/base/media_export.h"
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
{
21 explicit AudioClock(int sample_rate
);
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
,
30 base::TimeDelta timestamp
);
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
);
36 // Calculates the current media timestamp taking silence and changes in
37 // playback rate into account.
38 base::TimeDelta
CurrentMediaTimestamp() const;
40 // Returns the last endpoint timestamp provided to WroteAudio().
41 base::TimeDelta
last_endpoint_timestamp() const {
42 return last_endpoint_timestamp_
;
46 void TrimBufferedAudioToMatchDelay(int delay_frames
);
47 void PushBufferedAudio(int frames
,
49 base::TimeDelta endpoint_timestamp
);
51 const int sample_rate_
;
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
57 base::TimeDelta last_endpoint_timestamp_
;
59 struct BufferedAudio
{
60 BufferedAudio(int frames
,
62 base::TimeDelta endpoint_timestamp
);
66 base::TimeDelta endpoint_timestamp
;
69 std::deque
<BufferedAudio
> buffered_audio_
;
71 DISALLOW_COPY_AND_ASSIGN(AudioClock
);
76 #endif // MEDIA_FILTERS_AUDIO_CLOCK_H_