1 // Copyright 2013 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_CDM_PPAPI_FFMPEG_CDM_AUDIO_DECODER_H_ 6 #define MEDIA_CDM_PPAPI_FFMPEG_CDM_AUDIO_DECODER_H_ 7 8 #include <vector> 9 10 #include "base/basictypes.h" 11 #include "base/compiler_specific.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/time/time.h" 14 #include "media/cdm/ppapi/clear_key_cdm_common.h" 15 16 struct AVCodecContext; 17 struct AVFrame; 18 19 namespace media { 20 class AudioBus; 21 class AudioTimestampHelper; 22 class ScopedPtrAVFreeContext; 23 class ScopedPtrAVFreeFrame; 24 } 25 26 namespace media { 27 // TODO(xhwang): This class is partially cloned from FFmpegAudioDecoder. When 28 // FFmpegAudioDecoder is updated, it's a pain to keep this class in sync with 29 // FFmpegAudioDecoder. We need a long term sustainable solution for this. See 30 // http://crbug.com/169203 31 class FFmpegCdmAudioDecoder { 32 public: 33 explicit FFmpegCdmAudioDecoder(ClearKeyCdmHost* host); 34 ~FFmpegCdmAudioDecoder(); 35 bool Initialize(const cdm::AudioDecoderConfig& config); 36 void Deinitialize(); 37 void Reset(); 38 39 // Returns true when |config| is a valid audio decoder configuration. 40 static bool IsValidConfig(const cdm::AudioDecoderConfig& config); 41 42 // Decodes |compressed_buffer|. Returns |cdm::kSuccess| after storing 43 // output in |decoded_frames| when output is available. Returns 44 // |cdm::kNeedMoreData| when |compressed_frame| does not produce output. 45 // Returns |cdm::kDecodeError| when decoding fails. 46 // 47 // A null |compressed_buffer| will attempt to flush the decoder of any 48 // remaining frames. |compressed_buffer_size| and |timestamp| are ignored. 49 cdm::Status DecodeBuffer(const uint8_t* compressed_buffer, 50 int32_t compressed_buffer_size, 51 int64_t timestamp, 52 cdm::AudioFrames* decoded_frames); 53 54 private: 55 void ResetTimestampState(); 56 void ReleaseFFmpegResources(); 57 58 base::TimeDelta GetNextOutputTimestamp() const; 59 60 void SerializeInt64(int64_t value); 61 62 bool is_initialized_; 63 64 ClearKeyCdmHost* const host_; 65 66 // FFmpeg structures owned by this object. 67 scoped_ptr_malloc<AVCodecContext, ScopedPtrAVFreeContext> codec_context_; 68 scoped_ptr_malloc<AVFrame, ScopedPtrAVFreeFrame> av_frame_; 69 70 // Audio format. 71 int samples_per_second_; 72 int channels_; 73 74 // AVSampleFormat initially requested; not Chrome's SampleFormat. 75 int av_sample_format_; 76 77 // Used for computing output timestamps. 78 scoped_ptr<AudioTimestampHelper> output_timestamp_helper_; 79 int bytes_per_frame_; 80 base::TimeDelta last_input_timestamp_; 81 82 // Number of output sample bytes to drop before generating output buffers. 83 // This is required for handling negative timestamps when decoding Vorbis 84 // audio, for example. 85 int output_bytes_to_drop_; 86 87 typedef std::vector<uint8_t> SerializedAudioFrames; 88 SerializedAudioFrames serialized_audio_frames_; 89 90 DISALLOW_COPY_AND_ASSIGN(FFmpegCdmAudioDecoder); 91 }; 92 93 } // namespace media 94 95 #endif // MEDIA_CDM_PPAPI_FFMPEG_CDM_AUDIO_DECODER_H_ 96