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 // 35 // NOTE: End discard is only supported when there is no |decoder_delay|. 36 AudioDiscardHelper(int sample_rate, size_t decoder_delay); 37 ~AudioDiscardHelper(); 38 39 // Converts a TimeDelta to a frame count based on the constructed sample rate. 40 // |duration| must be positive. 41 size_t TimeDeltaToFrames(base::TimeDelta duration) const; 42 43 // Resets internal state and indicates that |initial_discard| of upcoming 44 // frames should be discarded. 45 void Reset(size_t initial_discard); 46 47 // Applies discard padding from the encoded buffer along with any initial 48 // discards. |decoded_buffer| may be NULL, if not the timestamp and duration 49 // will be set after discards are applied. Returns true if |decoded_buffer| 50 // exists after processing discard events. Returns false if |decoded_buffer| 51 // was NULL, is completely discarded, or a processing error occurs. 52 // 53 // If AudioDiscardHelper is not initialized() the timestamp of the first 54 // |encoded_buffer| will be used as the basis for all future timestamps set on 55 // |decoded_buffer|s. If the first buffer has a negative timestamp it will be 56 // clamped to zero. 57 bool ProcessBuffers(const scoped_refptr<DecoderBuffer>& encoded_buffer, 58 const scoped_refptr<AudioBuffer>& decoded_buffer); 59 60 // Whether any buffers have been processed. initialized()61 bool initialized() const { 62 return timestamp_helper_.base_timestamp() != kNoTimestamp(); 63 } 64 65 private: 66 const int sample_rate_; 67 const size_t decoder_delay_; 68 AudioTimestampHelper timestamp_helper_; 69 70 size_t discard_frames_; 71 base::TimeDelta last_input_timestamp_; 72 73 bool delayed_discard_; 74 DecoderBuffer::DiscardPadding delayed_discard_padding_; 75 76 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDiscardHelper); 77 }; 78 79 } // namespace media 80 81 #endif // MEDIA_BASE_AUDIO_DISCARD_HELPER_H_ 82