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_BASE_AUDIO_DISCARD_HELPER_H_ 6 #define MEDIA_BASE_AUDIO_DISCARD_HELPER_H_ 7 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" 14 15 namespace media { 16 17 class AudioBuffer; 18 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. 24 // 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. 29 // 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(); 36 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; 40 41 // Resets internal state and indicates that |initial_discard| of upcoming 42 // frames should be discarded. 43 void Reset(size_t initial_discard); 44 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. 50 // 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); 57 58 // Whether any buffers have been processed. initialized()59 bool initialized() const { 60 return timestamp_helper_.base_timestamp() != kNoTimestamp(); 61 } 62 63 private: 64 // The sample rate of the decoded audio samples. Used by TimeDeltaToFrames() 65 // and the timestamp helper. 66 const int sample_rate_; 67 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_; 72 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_; 77 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_; 82 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_; 87 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_; 94 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_; 99 100 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDiscardHelper); 101 }; 102 103 } // namespace media 104 105 #endif // MEDIA_BASE_AUDIO_DISCARD_HELPER_H_ 106