decrypting_audio_decoder.h revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
1// Copyright (c) 2012 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_FILTERS_DECRYPTING_AUDIO_DECODER_H_
6#define MEDIA_FILTERS_DECRYPTING_AUDIO_DECODER_H_
7
8#include "base/callback.h"
9#include "base/memory/ref_counted.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/memory/weak_ptr.h"
12#include "base/time/time.h"
13#include "media/base/audio_decoder.h"
14#include "media/base/decryptor.h"
15#include "media/base/demuxer_stream.h"
16
17namespace base {
18class SingleThreadTaskRunner;
19}
20
21namespace media {
22
23class AudioTimestampHelper;
24class DecoderBuffer;
25class Decryptor;
26
27// Decryptor-based AudioDecoder implementation that can decrypt and decode
28// encrypted audio buffers and return decrypted and decompressed audio frames.
29// All public APIs and callbacks are trampolined to the |task_runner_| so
30// that no locks are required for thread safety.
31class MEDIA_EXPORT DecryptingAudioDecoder : public AudioDecoder {
32 public:
33  // We do not currently have a way to let the Decryptor choose the output
34  // audio sample format and notify us of its choice. Therefore, we require all
35  // Decryptor implementations to decode audio into a fixed integer sample
36  // format designated by kSupportedBitsPerChannel.
37  // TODO(xhwang): Remove this restriction after http://crbug.com/169105 fixed.
38  static const int kSupportedBitsPerChannel;
39
40  DecryptingAudioDecoder(
41      const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
42      const SetDecryptorReadyCB& set_decryptor_ready_cb);
43  virtual ~DecryptingAudioDecoder();
44
45  // AudioDecoder implementation.
46  virtual void Initialize(const AudioDecoderConfig& config,
47                          const PipelineStatusCB& status_cb) OVERRIDE;
48  virtual void Decode(const scoped_refptr<DecoderBuffer>& buffer,
49                      const DecodeCB& decode_cb) OVERRIDE;
50  virtual scoped_refptr<AudioBuffer> GetDecodeOutput() OVERRIDE;
51  virtual void Reset(const base::Closure& closure) OVERRIDE;
52  virtual void Stop(const base::Closure& closure) OVERRIDE;
53  virtual int bits_per_channel() OVERRIDE;
54  virtual ChannelLayout channel_layout() OVERRIDE;
55  virtual int samples_per_second() OVERRIDE;
56
57 private:
58  // For a detailed state diagram please see this link: http://goo.gl/8jAok
59  // TODO(xhwang): Add a ASCII state diagram in this file after this class
60  // stabilizes.
61  // TODO(xhwang): Update this diagram for DecryptingAudioDecoder.
62  enum State {
63    kUninitialized = 0,
64    kDecryptorRequested,
65    kPendingDecoderInit,
66    kIdle,
67    kPendingDecode,
68    kWaitingForKey,
69    kDecodeFinished,
70    kStopped,
71  };
72
73  // Callback for DecryptorHost::RequestDecryptor().
74  void SetDecryptor(Decryptor* decryptor);
75
76  // Initializes the audio decoder on the |decryptor_| with |config_|.
77  void InitializeDecoder();
78
79  // Callback for Decryptor::InitializeAudioDecoder() during initialization.
80  void FinishInitialization(bool success);
81
82  void DecodePendingBuffer();
83
84  // Callback for Decryptor::DecryptAndDecodeAudio().
85  void DeliverFrame(int buffer_size,
86                    Decryptor::Status status,
87                    const Decryptor::AudioBuffers& frames);
88
89  // Callback for the |decryptor_| to notify this object that a new key has been
90  // added.
91  void OnKeyAdded();
92
93  // Resets decoder and calls |reset_cb_|.
94  void DoReset();
95
96  // Updates audio configs from |demuxer_stream_| and resets
97  // |output_timestamp_base_| and |total_samples_decoded_|.
98  void UpdateDecoderConfig();
99
100  // Sets timestamp and duration for |queued_audio_frames_| to make sure the
101  // renderer always receives continuous frames without gaps and overlaps.
102  void EnqueueFrames(const Decryptor::AudioBuffers& frames);
103
104  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
105
106  State state_;
107
108  PipelineStatusCB init_cb_;
109  DecodeCB decode_cb_;
110  base::Closure reset_cb_;
111  base::Closure stop_cb_;
112
113  // The current decoder configuration.
114  AudioDecoderConfig config_;
115
116  // Callback to request/cancel decryptor creation notification.
117  SetDecryptorReadyCB set_decryptor_ready_cb_;
118
119  Decryptor* decryptor_;
120
121  // The buffer that needs decrypting/decoding.
122  scoped_refptr<media::DecoderBuffer> pending_buffer_to_decode_;
123
124  // Indicates the situation where new key is added during pending decode
125  // (in other words, this variable can only be set in state kPendingDecode).
126  // If this variable is true and kNoKey is returned then we need to try
127  // decrypting/decoding again in case the newly added key is the correct
128  // decryption key.
129  bool key_added_while_decode_pending_;
130
131  Decryptor::AudioBuffers queued_audio_frames_;
132
133  // Decoded audio format.
134  int bits_per_channel_;
135  ChannelLayout channel_layout_;
136  int samples_per_second_;
137
138  scoped_ptr<AudioTimestampHelper> timestamp_helper_;
139
140  // NOTE: Weak pointers must be invalidated before all other member variables.
141  base::WeakPtrFactory<DecryptingAudioDecoder> weak_factory_;
142  base::WeakPtr<DecryptingAudioDecoder> weak_this_;
143
144  DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoder);
145};
146
147}  // namespace media
148
149#endif  // MEDIA_FILTERS_DECRYPTING_AUDIO_DECODER_H_
150