Upstream version 10.39.225.0
[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 SingleThreadTaskRunner;
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 |task_runner_| 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::SingleThreadTaskRunner>& task_runner,
42       const SetDecryptorReadyCB& set_decryptor_ready_cb);
43   virtual ~DecryptingAudioDecoder();
44
45   // AudioDecoder implementation.
46   virtual std::string GetDisplayName() const OVERRIDE;
47   virtual void Initialize(const AudioDecoderConfig& config,
48                           const PipelineStatusCB& status_cb,
49                           const OutputCB& output_cb) OVERRIDE;
50   virtual void Decode(const scoped_refptr<DecoderBuffer>& buffer,
51                       const DecodeCB& decode_cb) OVERRIDE;
52   virtual void Reset(const base::Closure& closure) OVERRIDE;
53
54  private:
55   // For a detailed state diagram please see this link: http://goo.gl/8jAok
56   // TODO(xhwang): Add a ASCII state diagram in this file after this class
57   // stabilizes.
58   // TODO(xhwang): Update this diagram for DecryptingAudioDecoder.
59   enum State {
60     kUninitialized = 0,
61     kDecryptorRequested,
62     kPendingDecoderInit,
63     kIdle,
64     kPendingDecode,
65     kWaitingForKey,
66     kDecodeFinished,
67     kError
68   };
69
70   // Callback for DecryptorHost::RequestDecryptor(). |decryptor_attached_cb| is
71   // called when the decryptor has been completely attached to the pipeline.
72   void SetDecryptor(Decryptor* decryptor,
73                     const DecryptorAttachedCB& decryptor_attached_cb);
74
75   // Initializes the audio decoder on the |decryptor_| with |config_|.
76   void InitializeDecoder();
77
78   // Callback for Decryptor::InitializeAudioDecoder() during initialization.
79   void FinishInitialization(bool success);
80
81   void DecodePendingBuffer();
82
83   // Callback for Decryptor::DecryptAndDecodeAudio().
84   void DeliverFrame(int buffer_size,
85                     Decryptor::Status status,
86                     const Decryptor::AudioBuffers& frames);
87
88   // Callback for the |decryptor_| to notify this object that a new key has been
89   // added.
90   void OnKeyAdded();
91
92   // Resets decoder and calls |reset_cb_|.
93   void DoReset();
94
95   // Sets timestamps for |frames| and then passes them to |output_cb_|.
96   void ProcessDecodedFrames(const Decryptor::AudioBuffers& frames);
97
98   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
99
100   State state_;
101
102   PipelineStatusCB init_cb_;
103   OutputCB output_cb_;
104   DecodeCB decode_cb_;
105   base::Closure reset_cb_;
106
107   // The current decoder configuration.
108   AudioDecoderConfig config_;
109
110   // Callback to request/cancel decryptor creation notification.
111   SetDecryptorReadyCB set_decryptor_ready_cb_;
112
113   Decryptor* decryptor_;
114
115   // The buffer that needs decrypting/decoding.
116   scoped_refptr<media::DecoderBuffer> pending_buffer_to_decode_;
117
118   // Indicates the situation where new key is added during pending decode
119   // (in other words, this variable can only be set in state kPendingDecode).
120   // If this variable is true and kNoKey is returned then we need to try
121   // decrypting/decoding again in case the newly added key is the correct
122   // decryption key.
123   bool key_added_while_decode_pending_;
124
125   scoped_ptr<AudioTimestampHelper> timestamp_helper_;
126
127   // NOTE: Weak pointers must be invalidated before all other member variables.
128   base::WeakPtrFactory<DecryptingAudioDecoder> weak_factory_;
129   base::WeakPtr<DecryptingAudioDecoder> weak_this_;
130
131   DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoder);
132 };
133
134 }  // namespace media
135
136 #endif  // MEDIA_FILTERS_DECRYPTING_AUDIO_DECODER_H_