- add sources.
[platform/framework/web/crosswalk.git] / src / media / filters / decrypting_audio_decoder.h
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
17 namespace base {
18 class MessageLoopProxy;
19 }
20
21 namespace media {
22
23 class AudioTimestampHelper;
24 class DecoderBuffer;
25 class 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 |message_loop_| so
30 // that no locks are required for thread safety.
31 class 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::MessageLoopProxy>& message_loop,
42       const SetDecryptorReadyCB& set_decryptor_ready_cb);
43   virtual ~DecryptingAudioDecoder();
44
45   // AudioDecoder implementation.
46   virtual void Initialize(DemuxerStream* stream,
47                           const PipelineStatusCB& status_cb,
48                           const StatisticsCB& statistics_cb) OVERRIDE;
49   virtual void Read(const ReadCB& read_cb) OVERRIDE;
50   virtual void Reset(const base::Closure& closure) OVERRIDE;
51   virtual int bits_per_channel() OVERRIDE;
52   virtual ChannelLayout channel_layout() OVERRIDE;
53   virtual int samples_per_second() OVERRIDE;
54
55  private:
56   // For a detailed state diagram please see this link: http://goo.gl/8jAok
57   // TODO(xhwang): Add a ASCII state diagram in this file after this class
58   // stabilizes.
59   // TODO(xhwang): Update this diagram for DecryptingAudioDecoder.
60   enum State {
61     kUninitialized = 0,
62     kDecryptorRequested,
63     kPendingDecoderInit,
64     kIdle,
65     kPendingConfigChange,
66     kPendingDemuxerRead,
67     kPendingDecode,
68     kWaitingForKey,
69     kDecodeFinished,
70   };
71
72   // Callback for DecryptorHost::RequestDecryptor().
73   void SetDecryptor(Decryptor* decryptor);
74
75   // Callback for Decryptor::InitializeAudioDecoder() during initialization.
76   void FinishInitialization(bool success);
77
78   // Callback for Decryptor::InitializeAudioDecoder() during config change.
79   void FinishConfigChange(bool success);
80
81   // Reads from the demuxer stream with corresponding callback method.
82   void ReadFromDemuxerStream();
83   void DecryptAndDecodeBuffer(DemuxerStream::Status status,
84                               const scoped_refptr<DecoderBuffer>& buffer);
85
86   void DecodePendingBuffer();
87
88   // Callback for Decryptor::DecryptAndDecodeAudio().
89   void DeliverFrame(int buffer_size,
90                     Decryptor::Status status,
91                     const Decryptor::AudioBuffers& frames);
92
93   // Callback for the |decryptor_| to notify this object that a new key has been
94   // added.
95   void OnKeyAdded();
96
97   // Resets decoder and calls |reset_cb_|.
98   void DoReset();
99
100   // Updates audio configs from |demuxer_stream_| and resets
101   // |output_timestamp_base_| and |total_samples_decoded_|.
102   void UpdateDecoderConfig();
103
104   // Sets timestamp and duration for |queued_audio_frames_| to make sure the
105   // renderer always receives continuous frames without gaps and overlaps.
106   void EnqueueFrames(const Decryptor::AudioBuffers& frames);
107
108   scoped_refptr<base::MessageLoopProxy> message_loop_;
109   base::WeakPtrFactory<DecryptingAudioDecoder> weak_factory_;
110   base::WeakPtr<DecryptingAudioDecoder> weak_this_;
111
112   State state_;
113
114   PipelineStatusCB init_cb_;
115   StatisticsCB statistics_cb_;
116   ReadCB read_cb_;
117   base::Closure reset_cb_;
118
119   // Pointer to the demuxer stream that will feed us compressed buffers.
120   DemuxerStream* demuxer_stream_;
121
122   // Callback to request/cancel decryptor creation notification.
123   SetDecryptorReadyCB set_decryptor_ready_cb_;
124
125   Decryptor* decryptor_;
126
127   // The buffer returned by the demuxer that needs decrypting/decoding.
128   scoped_refptr<media::DecoderBuffer> pending_buffer_to_decode_;
129
130   // Indicates the situation where new key is added during pending decode
131   // (in other words, this variable can only be set in state kPendingDecode).
132   // If this variable is true and kNoKey is returned then we need to try
133   // decrypting/decoding again in case the newly added key is the correct
134   // decryption key.
135   bool key_added_while_decode_pending_;
136
137   Decryptor::AudioBuffers queued_audio_frames_;
138
139   // Decoded audio format.
140   int bits_per_channel_;
141   ChannelLayout channel_layout_;
142   int samples_per_second_;
143
144   scoped_ptr<AudioTimestampHelper> timestamp_helper_;
145
146   DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoder);
147 };
148
149 }  // namespace media
150
151 #endif  // MEDIA_FILTERS_DECRYPTING_AUDIO_DECODER_H_