Chromium Code Reviews| 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" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 ~AudioClock(); | 22 ~AudioClock(); |
| 23 | 23 |
| 24 // |frames_written| amount of audio data scaled to |playback_rate| written. | 24 // |frames_written| amount of audio data scaled to |playback_rate| written. |
| 25 // |frames_requested| amount of audio data requested by hardware. | 25 // |frames_requested| amount of audio data requested by hardware. |
| 26 // |delay_frames| is the current amount of hardware delay. | 26 // |delay_frames| is the current amount of hardware delay. |
| 27 void WroteAudio(int frames_written, | 27 void WroteAudio(int frames_written, |
| 28 int frames_requested, | 28 int frames_requested, |
| 29 int delay_frames, | 29 int delay_frames, |
| 30 float playback_rate); | 30 float playback_rate); |
| 31 | 31 |
| 32 // Calculates the current media timestamp taking silence and changes in | 32 // Returns the bounds of media data currently buffered by the audio hardware, |
| 33 // playback rate into account. | 33 // taking silence and changes in playback rate into account. |
| 34 base::TimeDelta current_media_timestamp() const { | 34 // |
|
DaleCurtis
2014/09/03 22:00:45
Can you add the WroteAudio() calls that generated
scherkus (not reviewing)
2014/09/03 22:23:18
Done.
| |
| 35 return current_media_timestamp_; | 35 // start_timestamp = 1000 ms sample_rate = 40 Hz |
| 36 } | 36 // +-----------------------+----------------------+-----------------------+ |
| 37 // | 10 frames silence | 20 frames @ 1.0x | 20 frames @ 0.5x | | |
| 38 // | | = 500 ms | = 250 ms | | |
| 39 // +-----------------------+----------------------+-----------------------+ | |
| 40 // ^ ^ | |
| 41 // front_timestamp() is equal to back_timestamp() is equal to | |
| 42 // |start_timestamp| since no amount of media frames tracked | |
| 43 // media data has been played yet. by AudioClock, which would be | |
| 44 // 1750 ms. | |
| 45 base::TimeDelta front_timestamp() const { return front_timestamp_; } | |
| 46 base::TimeDelta back_timestamp() const { return back_timestamp_; } | |
| 37 | 47 |
| 38 // Clients can provide |time_since_writing| to simulate the passage of time | 48 // 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. | 49 // since last writing audio to get a more accurate current media timestamp. |
| 40 base::TimeDelta CurrentMediaTimestampSinceWriting( | 50 // |
| 51 // The value will be bounded between front_timestamp() and back_timestamp(). | |
| 52 base::TimeDelta TimestampSinceWriting( | |
| 41 base::TimeDelta time_since_writing) const; | 53 base::TimeDelta time_since_writing) const; |
| 42 | 54 |
| 43 // Returns the amount of contiguous media time buffered at the head of the | 55 // 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 | 56 // audio hardware buffer. Silence introduced into the audio hardware buffer is |
| 45 // treated as a break in media time. | 57 // treated as a break in media time. |
| 46 base::TimeDelta contiguous_audio_data_buffered() const { | 58 base::TimeDelta contiguous_audio_data_buffered() const { |
| 47 return contiguous_audio_data_buffered_; | 59 return contiguous_audio_data_buffered_; |
| 48 } | 60 } |
| 49 | 61 |
| 50 // Same as above, but also treats changes in playback rate as a break in media | 62 // Same as above, but also treats changes in playback rate as a break in media |
| 51 // time. | 63 // time. |
| 52 base::TimeDelta contiguous_audio_data_buffered_at_same_rate() const { | 64 base::TimeDelta contiguous_audio_data_buffered_at_same_rate() const { |
| 53 return contiguous_audio_data_buffered_at_same_rate_; | 65 return contiguous_audio_data_buffered_at_same_rate_; |
| 54 } | 66 } |
| 55 | 67 |
| 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_; } | |
| 59 | |
| 60 private: | 68 private: |
| 61 // Even with a ridiculously high sample rate of 256kHz, using 64 bits will | 69 // 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). | 70 // permit tracking up to 416999965 days worth of time (that's 1141 millenia). |
| 63 // | 71 // |
| 64 // 32 bits on the other hand would top out at measly 2 hours and 20 minutes. | 72 // 32 bits on the other hand would top out at measly 2 hours and 20 minutes. |
| 65 struct AudioData { | 73 struct AudioData { |
| 66 AudioData(int64_t frames, float playback_rate); | 74 AudioData(int64_t frames, float playback_rate); |
| 67 | 75 |
| 68 int64_t frames; | 76 int64_t frames; |
| 69 float playback_rate; | 77 float playback_rate; |
| 70 }; | 78 }; |
| 71 | 79 |
| 72 // Helpers for operating on |buffered_|. | 80 // Helpers for operating on |buffered_|. |
| 73 void PushBufferedAudioData(int64_t frames, float playback_rate); | 81 void PushBufferedAudioData(int64_t frames, float playback_rate); |
| 74 void PopBufferedAudioData(int64_t frames); | 82 void PopBufferedAudioData(int64_t frames); |
| 75 base::TimeDelta ComputeBufferedMediaTime(int64_t frames) const; | 83 base::TimeDelta ComputeBufferedMediaTime(int64_t frames) const; |
| 76 | 84 |
| 77 const base::TimeDelta start_timestamp_; | 85 const base::TimeDelta start_timestamp_; |
| 78 const int sample_rate_; | 86 const int sample_rate_; |
| 79 const double microseconds_per_frame_; | 87 const double microseconds_per_frame_; |
| 80 | 88 |
| 81 std::deque<AudioData> buffered_; | 89 std::deque<AudioData> buffered_; |
| 82 int64_t total_buffered_frames_; | 90 int64_t total_buffered_frames_; |
| 83 | 91 |
| 84 base::TimeDelta current_media_timestamp_; | 92 base::TimeDelta front_timestamp_; |
| 93 base::TimeDelta back_timestamp_; | |
| 85 | 94 |
| 86 // Cached results of last call to WroteAudio(). | 95 // Cached results of last call to WroteAudio(). |
| 87 bool audio_data_buffered_; | |
| 88 base::TimeDelta contiguous_audio_data_buffered_; | 96 base::TimeDelta contiguous_audio_data_buffered_; |
| 89 base::TimeDelta contiguous_audio_data_buffered_at_same_rate_; | 97 base::TimeDelta contiguous_audio_data_buffered_at_same_rate_; |
| 90 | 98 |
| 91 DISALLOW_COPY_AND_ASSIGN(AudioClock); | 99 DISALLOW_COPY_AND_ASSIGN(AudioClock); |
| 92 }; | 100 }; |
| 93 | 101 |
| 94 } // namespace media | 102 } // namespace media |
| 95 | 103 |
| 96 #endif // MEDIA_FILTERS_AUDIO_CLOCK_H_ | 104 #endif // MEDIA_FILTERS_AUDIO_CLOCK_H_ |
| OLD | NEW |