Managed bookmarks for supervised users.
[chromium-blink-merge.git] / media / base / audio_discard_helper.h
blobded404fec1392d907b6e351ded691cf4b5db1b56
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_BASE_AUDIO_DISCARD_HELPER_H_
6 #define MEDIA_BASE_AUDIO_DISCARD_HELPER_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/time/time.h"
10 #include "media/base/audio_timestamp_helper.h"
11 #include "media/base/buffers.h"
12 #include "media/base/decoder_buffer.h"
13 #include "media/base/media_export.h"
15 namespace media {
17 class AudioBuffer;
19 // Helper class for managing timestamps and discard events around decoding.
20 class MEDIA_EXPORT AudioDiscardHelper {
21 public:
22 // |sample_rate| is the sample rate of decoded data which will be handed into
23 // the ProcessBuffers() call.
25 // |decoder_delay| is the number of frames a decoder will output before data
26 // corresponding to the first encoded buffer is output. Callers only need to
27 // specify this if the decoder inserts frames which have no corresponding
28 // encoded buffer.
30 // For example, most MP3 decoders will output 529 junk frames before the data
31 // corresponding to the first encoded buffer is output. These frames are not
32 // represented in the encoded data stream and instead are an artifact of how
33 // most MP3 decoders work. See http://lame.sourceforge.net/tech-FAQ.txt
34 AudioDiscardHelper(int sample_rate, size_t decoder_delay);
35 ~AudioDiscardHelper();
37 // Converts a TimeDelta to a frame count based on the constructed sample rate.
38 // |duration| must be positive.
39 size_t TimeDeltaToFrames(base::TimeDelta duration) const;
41 // Resets internal state and indicates that |initial_discard| of upcoming
42 // frames should be discarded.
43 void Reset(size_t initial_discard);
45 // Applies discard padding from the encoded buffer along with any initial
46 // discards. |decoded_buffer| may be NULL, if not the timestamp and duration
47 // will be set after discards are applied. Returns true if |decoded_buffer|
48 // exists after processing discard events. Returns false if |decoded_buffer|
49 // was NULL, is completely discarded, or a processing error occurs.
51 // If AudioDiscardHelper is not initialized() the timestamp of the first
52 // |encoded_buffer| will be used as the basis for all future timestamps set on
53 // |decoded_buffer|s. If the first buffer has a negative timestamp it will be
54 // clamped to zero.
55 bool ProcessBuffers(const scoped_refptr<DecoderBuffer>& encoded_buffer,
56 const scoped_refptr<AudioBuffer>& decoded_buffer);
58 // Whether any buffers have been processed.
59 bool initialized() const {
60 return timestamp_helper_.base_timestamp() != kNoTimestamp();
63 private:
64 // The sample rate of the decoded audio samples. Used by TimeDeltaToFrames()
65 // and the timestamp helper.
66 const int sample_rate_;
68 // Some codecs output extra samples during the first decode. In order to trim
69 // DiscardPadding correctly the helper must know the offset into the decoded
70 // buffers at which real samples start.
71 const size_t decoder_delay_;
73 // Used to regenerate sample accurate timestamps for decoded buffers. The
74 // timestamp of the first encoded buffer seen by ProcessBuffers() is used as
75 // the base timestamp.
76 AudioTimestampHelper timestamp_helper_;
78 // The number of frames to discard from the front of the next buffer. Can be
79 // set by Reset() and added to by a front DiscardPadding larger than its
80 // associated buffer.
81 size_t discard_frames_;
83 // The last encoded buffer timestamp seen by ProcessBuffers() or kNoTimestamp
84 // if no buffers have been seen thus far. Used to issue warnings for buffer
85 // sequences with non-monotonic timestamps.
86 base::TimeDelta last_input_timestamp_;
88 // Certain codecs require two encoded buffers before they'll output the first
89 // decoded buffer. In this case DiscardPadding must be carried over from the
90 // previous encoded buffer. Enabled automatically if an encoded buffer is
91 // given to ProcessBuffers() with a NULL decoded buffer.
92 bool delayed_discard_;
93 DecoderBuffer::DiscardPadding delayed_discard_padding_;
95 // When |decoder_delay_| > 0, the number of frames which should be discarded
96 // from the next buffer. The index at which to start discarding is calculated
97 // by subtracting |delayed_end_discard_| from |decoder_delay_|.
98 size_t delayed_end_discard_;
100 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDiscardHelper);
103 } // namespace media
105 #endif // MEDIA_BASE_AUDIO_DISCARD_HELPER_H_