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 AudioClock(base::TimeDelta start_timestamp, int sample_rate);
24 // |frames_written| amount of audio data scaled to |playback_rate| written.
25 // |frames_requested| amount of audio data requested by hardware.
26 // |delay_frames| is the current amount of hardware delay.
27 void WroteAudio(int frames_written,
32 // Calculates the current media timestamp taking silence and changes in
33 // playback rate into account.
34 base::TimeDelta current_media_timestamp() const {
35 return current_media_timestamp_;
38 // Clients can provide |time_since_writing| to simulate the passage of time
39 // since last writing audio to get a more accurate current media timestamp.
40 base::TimeDelta CurrentMediaTimestampSinceWriting(
41 base::TimeDelta time_since_writing) const;
43 // Returns the amount of contiguous media time buffered at the head of the
44 // audio hardware buffer. Silence introduced into the audio hardware buffer is
45 // treated as a break in media time.
46 base::TimeDelta contiguous_audio_data_buffered() const {
47 return contiguous_audio_data_buffered_;
50 // Same as above, but also treats changes in playback rate as a break in media
52 base::TimeDelta contiguous_audio_data_buffered_at_same_rate() const {
53 return contiguous_audio_data_buffered_at_same_rate_;
56 // Returns true if there is any audio data buffered by the audio hardware,
57 // even if there is silence mixed in.
58 bool audio_data_buffered() const { return audio_data_buffered_; }
61 // Even with a ridiculously high sample rate of 256kHz, using 64 bits will
62 // permit tracking up to 416999965 days worth of time (that's 1141 millenia).
64 // 32 bits on the other hand would top out at measly 2 hours and 20 minutes.
66 AudioData(int64_t frames, float playback_rate);
72 // Helpers for operating on |buffered_|.
73 void PushBufferedAudioData(int64_t frames, float playback_rate);
74 void PopBufferedAudioData(int64_t frames);
75 base::TimeDelta ComputeBufferedMediaTime(int64_t frames) const;
77 const base::TimeDelta start_timestamp_;
78 const int sample_rate_;
79 const double microseconds_per_frame_;
81 std::deque<AudioData> buffered_;
82 int64_t total_buffered_frames_;
84 base::TimeDelta current_media_timestamp_;
86 // Cached results of last call to WroteAudio().
87 bool audio_data_buffered_;
88 base::TimeDelta contiguous_audio_data_buffered_;
89 base::TimeDelta contiguous_audio_data_buffered_at_same_rate_;
91 DISALLOW_COPY_AND_ASSIGN(AudioClock);
96 #endif // MEDIA_FILTERS_AUDIO_CLOCK_H_