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_EXTERNAL_CLEAR_KEY_FFMPEG_CDM_AUDIO_DECODER_H_
6#define MEDIA_CDM_PPAPI_EXTERNAL_CLEAR_KEY_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/external_clear_key/clear_key_cdm_common.h"
15#include "media/ffmpeg/ffmpeg_deleters.h"
16
17struct AVCodecContext;
18struct AVFrame;
19
20namespace media {
21class AudioBus;
22class AudioTimestampHelper;
23}
24
25namespace media {
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
30class FFmpegCdmAudioDecoder {
31 public:
32  explicit FFmpegCdmAudioDecoder(ClearKeyCdmHost* 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  ClearKeyCdmHost* const host_;
64
65  // FFmpeg structures owned by this object.
66  scoped_ptr<AVCodecContext, ScopedPtrAVFreeContext> codec_context_;
67  scoped_ptr<AVFrame, ScopedPtrAVFreeFrame> av_frame_;
68
69  // Audio format.
70  int samples_per_second_;
71  int channels_;
72
73  // AVSampleFormat initially requested; not Chrome's SampleFormat.
74  int av_sample_format_;
75
76  // Used for computing output timestamps.
77  scoped_ptr<AudioTimestampHelper> output_timestamp_helper_;
78  int bytes_per_frame_;
79  base::TimeDelta last_input_timestamp_;
80
81  // Number of output sample bytes to drop before generating output buffers.
82  // This is required for handling negative timestamps when decoding Vorbis
83  // audio, for example.
84  int output_bytes_to_drop_;
85
86  typedef std::vector<uint8_t> SerializedAudioFrames;
87  SerializedAudioFrames serialized_audio_frames_;
88
89  DISALLOW_COPY_AND_ASSIGN(FFmpegCdmAudioDecoder);
90};
91
92}  // namespace media
93
94#endif  // MEDIA_CDM_PPAPI_EXTERNAL_CLEAR_KEY_FFMPEG_CDM_AUDIO_DECODER_H_
95