Home | History | Annotate | Download | only in ppapi
      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/api/content_decryption_module.h"
     15 
     16 struct AVCodecContext;
     17 struct AVFrame;
     18 
     19 namespace media {
     20 class AudioBus;
     21 class AudioTimestampHelper;
     22 }
     23 
     24 namespace media {
     25 
     26 // TODO(xhwang): This class is partially cloned from FFmpegAudioDecoder. When
     27 // FFmpegAudioDecoder is updated, it's a pain to keep this class in sync with
     28 // FFmpegAudioDecoder. We need a long term sustainable solution for this. See
     29 // http://crbug.com/169203
     30 class FFmpegCdmAudioDecoder {
     31  public:
     32   explicit FFmpegCdmAudioDecoder(cdm::Host* host);
     33   ~FFmpegCdmAudioDecoder();
     34   bool Initialize(const cdm::AudioDecoderConfig& config);
     35   void Deinitialize();
     36   void Reset();
     37 
     38   // Returns true when |config| is a valid audio decoder configuration.
     39   static bool IsValidConfig(const cdm::AudioDecoderConfig& config);
     40 
     41   // Decodes |compressed_buffer|. Returns |cdm::kSuccess| after storing
     42   // output in |decoded_frames| when output is available. Returns
     43   // |cdm::kNeedMoreData| when |compressed_frame| does not produce output.
     44   // Returns |cdm::kDecodeError| when decoding fails.
     45   //
     46   // A null |compressed_buffer| will attempt to flush the decoder of any
     47   // remaining frames. |compressed_buffer_size| and |timestamp| are ignored.
     48   cdm::Status DecodeBuffer(const uint8_t* compressed_buffer,
     49                            int32_t compressed_buffer_size,
     50                            int64_t timestamp,
     51                            cdm::AudioFrames* decoded_frames);
     52 
     53  private:
     54   void ResetTimestampState();
     55   void ReleaseFFmpegResources();
     56 
     57   base::TimeDelta GetNextOutputTimestamp() const;
     58 
     59   void SerializeInt64(int64_t value);
     60 
     61   bool is_initialized_;
     62 
     63   cdm::Host* const host_;
     64 
     65   // FFmpeg structures owned by this object.
     66   AVCodecContext* codec_context_;
     67   AVFrame* av_frame_;
     68 
     69   // Audio format.
     70   int bits_per_channel_;
     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   // We may need to convert the audio data coming out of FFmpeg from planar
     83   // float to integer.
     84   scoped_ptr<AudioBus> converter_bus_;
     85 
     86   // Number of output sample bytes to drop before generating output buffers.
     87   // This is required for handling negative timestamps when decoding Vorbis
     88   // audio, for example.
     89   int output_bytes_to_drop_;
     90 
     91   typedef std::vector<uint8_t> SerializedAudioFrames;
     92   SerializedAudioFrames serialized_audio_frames_;
     93 
     94   DISALLOW_COPY_AND_ASSIGN(FFmpegCdmAudioDecoder);
     95 };
     96 
     97 }  // namespace media
     98 
     99 #endif  // MEDIA_CDM_PPAPI_FFMPEG_CDM_AUDIO_DECODER_H_
    100