[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / media / base / decryptor.h
blob47b4645efd9ba328655c3af7c321243ecbf94c15
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.
5 #ifndef MEDIA_BASE_DECRYPTOR_H_
6 #define MEDIA_BASE_DECRYPTOR_H_
8 #include <list>
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "media/base/audio_buffer.h"
14 #include "media/base/media_export.h"
16 namespace media {
18 class AudioDecoderConfig;
19 class DecoderBuffer;
20 class VideoDecoderConfig;
21 class VideoFrame;
23 // Decrypts (and decodes) encrypted buffer.
25 // All methods are called on the (video/audio) decoder thread. Decryptor
26 // implementations must be thread safe when methods are called this way.
27 // Depending on the implementation callbacks may be fired synchronously or
28 // asynchronously.
29 class MEDIA_EXPORT Decryptor {
30 public:
31 // TODO(xhwang): Replace kError with kDecryptError and kDecodeError.
32 // TODO(xhwang): Replace kNeedMoreData with kNotEnoughData.
33 enum Status {
34 kSuccess, // Decryption successfully completed. Decrypted buffer ready.
35 kNoKey, // No key is available to decrypt.
36 kNeedMoreData, // Decoder needs more data to produce a frame.
37 kError // Key is available but an error occurred during decryption.
40 // TODO(xhwang): Unify this with DemuxerStream::Type.
41 enum StreamType {
42 kAudio,
43 kVideo
46 Decryptor();
47 virtual ~Decryptor();
49 // Indicates that a new key has been added to the MediaKeys object associated
50 // with the Decryptor.
51 typedef base::Callback<void()> NewKeyCB;
53 // Registers a NewKeyCB which should be called when a new key is added to the
54 // decryptor. Only one NewKeyCB can be registered for one |stream_type|.
55 // If this function is called multiple times for the same |stream_type|, the
56 // previously registered callback will be replaced. In other words,
57 // registering a null callback cancels the originally registered callback.
58 virtual void RegisterNewKeyCB(StreamType stream_type,
59 const NewKeyCB& key_added_cb) = 0;
61 // Indicates completion of a decryption operation.
63 // First parameter: The status of the decryption operation.
64 // - Set to kSuccess if the encrypted buffer is successfully decrypted and
65 // the decrypted buffer is ready to be read.
66 // - Set to kNoKey if no decryption key is available to decrypt the encrypted
67 // buffer. In this case the decrypted buffer must be NULL.
68 // - Set to kError if unexpected error has occurred. In this case the
69 // decrypted buffer must be NULL.
70 // - This parameter should not be set to kNeedMoreData.
71 // Second parameter: The decrypted buffer.
72 // - Only |data|, |data_size| and |timestamp| are set in the returned
73 // DecoderBuffer. The callback handler is responsible for setting other
74 // fields as appropriate.
75 typedef base::Callback<void(Status,
76 const scoped_refptr<DecoderBuffer>&)> DecryptCB;
78 // Decrypts the |encrypted| buffer. The decrypt status and decrypted buffer
79 // are returned via the provided callback |decrypt_cb|. The |encrypted| buffer
80 // must not be NULL.
81 // Decrypt() should not be called until any previous DecryptCB of the same
82 // |stream_type| has completed. Thus, only one DecryptCB may be pending at
83 // a time for a given |stream_type|.
84 virtual void Decrypt(StreamType stream_type,
85 const scoped_refptr<DecoderBuffer>& encrypted,
86 const DecryptCB& decrypt_cb) = 0;
88 // Cancels the scheduled decryption operation for |stream_type| and fires the
89 // pending DecryptCB immediately with kSuccess and NULL.
90 // Decrypt() should not be called again before the pending DecryptCB for the
91 // same |stream_type| is fired.
92 virtual void CancelDecrypt(StreamType stream_type) = 0;
94 // Indicates completion of audio/video decoder initialization.
96 // First Parameter: Indicates initialization success.
97 // - Set to true if initialization was successful. False if an error occurred.
98 typedef base::Callback<void(bool)> DecoderInitCB;
100 // Initializes a decoder with the given |config|, executing the |init_cb|
101 // upon completion.
102 virtual void InitializeAudioDecoder(const AudioDecoderConfig& config,
103 const DecoderInitCB& init_cb) = 0;
104 virtual void InitializeVideoDecoder(const VideoDecoderConfig& config,
105 const DecoderInitCB& init_cb) = 0;
107 // Helper structure for managing multiple decoded audio buffers per input.
108 typedef std::list<scoped_refptr<AudioBuffer> > AudioFrames;
110 // Indicates completion of audio/video decrypt-and-decode operation.
112 // First parameter: The status of the decrypt-and-decode operation.
113 // - Set to kSuccess if the encrypted buffer is successfully decrypted and
114 // decoded. In this case, the decoded frame/buffers can be/contain:
115 // 1) NULL, which means the operation has been aborted.
116 // 2) End-of-stream (EOS) frame, which means that the decoder has hit EOS,
117 // flushed all internal buffers and cannot produce more video frames.
118 // 3) Decrypted and decoded video frame or audio buffer.
119 // - Set to kNoKey if no decryption key is available to decrypt the encrypted
120 // buffer. In this case the returned frame(s) must be NULL/empty.
121 // - Set to kNeedMoreData if more data is needed to produce a video frame. In
122 // this case the returned frame(s) must be NULL/empty.
123 // - Set to kError if unexpected error has occurred. In this case the
124 // returned frame(s) must be NULL/empty.
125 // Second parameter: The decoded video frame or audio buffers.
126 typedef base::Callback<void(Status, const AudioFrames&)> AudioDecodeCB;
127 typedef base::Callback<void(Status,
128 const scoped_refptr<VideoFrame>&)> VideoDecodeCB;
130 // Decrypts and decodes the |encrypted| buffer. The status and the decrypted
131 // buffer are returned via the provided callback.
132 // The |encrypted| buffer must not be NULL.
133 // At end-of-stream, this method should be called repeatedly with
134 // end-of-stream DecoderBuffer until no frame/buffer can be produced.
135 // These methods can only be called after the corresponding decoder has
136 // been successfully initialized.
137 virtual void DecryptAndDecodeAudio(
138 const scoped_refptr<DecoderBuffer>& encrypted,
139 const AudioDecodeCB& audio_decode_cb) = 0;
140 virtual void DecryptAndDecodeVideo(
141 const scoped_refptr<DecoderBuffer>& encrypted,
142 const VideoDecodeCB& video_decode_cb) = 0;
144 // Resets the decoder to an initialized clean state, cancels any scheduled
145 // decrypt-and-decode operations, and fires any pending
146 // AudioDecodeCB/VideoDecodeCB immediately with kError and NULL.
147 // This method can only be called after the corresponding decoder has been
148 // successfully initialized.
149 virtual void ResetDecoder(StreamType stream_type) = 0;
151 // Releases decoder resources, deinitializes the decoder, cancels any
152 // scheduled initialization or decrypt-and-decode operations, and fires
153 // any pending DecoderInitCB/AudioDecodeCB/VideoDecodeCB immediately.
154 // DecoderInitCB should be fired with false. AudioDecodeCB/VideoDecodeCB
155 // should be fired with kError.
156 // This method can be called any time after Initialize{Audio|Video}Decoder()
157 // has been called (with the correct stream type).
158 // After this operation, the decoder is set to an uninitialized state.
159 // The decoder can be reinitialized after it is uninitialized.
160 virtual void DeinitializeDecoder(StreamType stream_type) = 0;
162 private:
163 DISALLOW_COPY_AND_ASSIGN(Decryptor);
166 // Callback to notify that the decryptor has been completely attached into the
167 // pipeline. Parameter indicates whether the operation succeeded.
168 typedef base::Callback<void(bool)> DecryptorAttachedCB;
170 // Callback to notify that a decryptor is ready. DecryptorAttachedCB is called
171 // when the decryptor has been completely inserted into the pipeline.
172 typedef base::Callback<void(Decryptor*, const DecryptorAttachedCB&)>
173 DecryptorReadyCB;
175 // Callback to set/cancel a DecryptorReadyCB.
176 // Calling this callback with a non-null callback registers decryptor ready
177 // notification. When the decryptor is ready, notification will be sent
178 // through the provided callback.
179 // Calling this callback with a null callback cancels previously registered
180 // decryptor ready notification. Any previously provided callback will be
181 // fired immediately with NULL.
182 typedef base::Callback<void(const DecryptorReadyCB&)> SetDecryptorReadyCB;
184 } // namespace media
186 #endif // MEDIA_BASE_DECRYPTOR_H_