1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MEDIA_BASE_DECRYPTOR_H_
#define MEDIA_BASE_DECRYPTOR_H_
#include <list>
#include <string>
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "media/base/audio_decoder.h"
#include "media/base/media_export.h"
namespace media {
class AudioDecoderConfig;
class DataBuffer;
class DecoderBuffer;
class VideoDecoderConfig;
class VideoFrame;
// Performs key operations and decrypts (and decodes) encrypted buffer.
//
// Key operations (GenerateKeyRequest(), AddKey() and CancelKeyRequest())
// are called on the renderer thread. Therefore, these calls should be fast
// and nonblocking; key events should be fired asynchronously.
// All other methods are called on the (video/audio) decoder thread.
// Decryptor implementations must be thread safe when methods are called
// following the above model.
// Depending on the implementation callbacks may be fired synchronously or
// asynchronously.
class MEDIA_EXPORT Decryptor {
public:
// Reported to UMA, so never reuse a value!
// Must be kept in sync with WebKit::WebMediaPlayerClient::MediaKeyErrorCode
// (enforced in webmediaplayer_impl.cc).
enum KeyError {
kUnknownError = 1,
kClientError,
kServiceError,
kOutputError,
kHardwareChangeError,
kDomainError,
kMaxKeyError // Must be last and greater than any legit value.
};
// TODO(xhwang): Replace kError with kDecryptError and kDecodeError.
// TODO(xhwang): Replace kNeedMoreData with kNotEnoughData.
enum Status {
kSuccess, // Decryption successfully completed. Decrypted buffer ready.
kNoKey, // No key is available to decrypt.
kNeedMoreData, // Decoder needs more data to produce a frame.
kError // Key is available but an error occurred during decryption.
};
// TODO(xhwang): Unify this with DemuxerStream::Type.
enum StreamType {
kAudio,
kVideo
};
Decryptor();
virtual ~Decryptor();
// Generates a key request for the |key_system| with |type| and
// |init_data| provided.
// Returns true if generating key request succeeded, false otherwise.
// Note: AddKey() and CancelKeyRequest() should only be called after
// GenerateKeyRequest() returns true.
virtual bool GenerateKeyRequest(const std::string& key_system,
const std::string& type,
const uint8* init_data,
int init_data_length) = 0;
// Adds a |key| to the |key_system|. The |key| is not limited to a decryption
// key. It can be any data that the key system accepts, such as a license.
// If multiple calls of this function set different keys for the same
// key ID, the older key will be replaced by the newer key.
virtual void AddKey(const std::string& key_system,
const uint8* key,
int key_length,
const uint8* init_data,
int init_data_length,
const std::string& session_id) = 0;
// Cancels the key request specified by |session_id|.
virtual void CancelKeyRequest(const std::string& key_system,
const std::string& session_id) = 0;
// Indicates that a new key has been added to the Decryptor.
typedef base::Callback<void()> NewKeyCB;
// Registers a NewKeyCB which should be called when a new key is added to the
// decryptor. Only one NewKeyCB can be registered for one |stream_type|.
// If this function is called multiple times for the same |stream_type|, the
// previously registered callback will be replaced. In other words,
// registering a null callback cancels the originally registered callback.
virtual void RegisterNewKeyCB(StreamType stream_type,
const NewKeyCB& key_added_cb) = 0;
// Indicates completion of a decryption operation.
//
// First parameter: The status of the decryption operation.
// - Set to kSuccess if the encrypted buffer is successfully decrypted and
// the decrypted buffer is ready to be read.
// - Set to kNoKey if no decryption key is available to decrypt the encrypted
// buffer. In this case the decrypted buffer must be NULL.
// - Set to kError if unexpected error has occurred. In this case the
// decrypted buffer must be NULL.
// - This parameter should not be set to kNeedMoreData.
// Second parameter: The decrypted buffer.
typedef base::Callback<void(Status,
const scoped_refptr<DecoderBuffer>&)> DecryptCB;
// Decrypts the |encrypted| buffer. The decrypt status and decrypted buffer
// are returned via the provided callback |decrypt_cb|. The |encrypted| buffer
// must not be NULL.
// Decrypt() should not be called until any previous DecryptCB of the same
// |stream_type| has completed. Thus, only one DecryptCB may be pending at
// a time for a given |stream_type|.
virtual void Decrypt(StreamType stream_type,
const scoped_refptr<DecoderBuffer>& encrypted,
const DecryptCB& decrypt_cb) = 0;
// Cancels the scheduled decryption operation for |stream_type| and fires the
// pending DecryptCB immediately with kSuccess and NULL.
// Decrypt() should not be called again before the pending DecryptCB for the
// same |stream_type| is fired.
virtual void CancelDecrypt(StreamType stream_type) = 0;
// Indicates completion of audio/video decoder initialization.
//
// First Parameter: Indicates initialization success.
// - Set to true if initialization was successful. False if an error occurred.
typedef base::Callback<void(bool)> DecoderInitCB;
// Initializes a decoder with the given |config|, executing the |init_cb|
// upon completion.
virtual void InitializeAudioDecoder(scoped_ptr<AudioDecoderConfig> config,
const DecoderInitCB& init_cb) = 0;
virtual void InitializeVideoDecoder(scoped_ptr<VideoDecoderConfig> config,
const DecoderInitCB& init_cb) = 0;
// Helper structure for managing multiple decoded audio buffers per input.
// TODO(xhwang): Rename this to AudioFrames.
typedef std::list<scoped_refptr<DataBuffer> > AudioBuffers;
// Indicates completion of audio/video decrypt-and-decode operation.
//
// First parameter: The status of the decrypt-and-decode operation.
// - Set to kSuccess if the encrypted buffer is successfully decrypted and
// decoded. In this case, the decoded frame/buffers can be/contain:
// 1) NULL, which means the operation has been aborted.
// 2) End-of-stream (EOS) frame, which means that the decoder has hit EOS,
// flushed all internal buffers and cannot produce more video frames.
// 3) Decrypted and decoded video frame or audio buffer.
// - Set to kNoKey if no decryption key is available to decrypt the encrypted
// buffer. In this case the returned frame(s) must be NULL/empty.
// - Set to kNeedMoreData if more data is needed to produce a video frame. In
// this case the returned frame(s) must be NULL/empty.
// - Set to kError if unexpected error has occurred. In this case the
// returned frame(s) must be NULL/empty.
// Second parameter: The decoded video frame or audio buffers.
typedef base::Callback<void(Status, const AudioBuffers&)> AudioDecodeCB;
typedef base::Callback<void(Status,
const scoped_refptr<VideoFrame>&)> VideoDecodeCB;
// Decrypts and decodes the |encrypted| buffer. The status and the decrypted
// buffer are returned via the provided callback.
// The |encrypted| buffer must not be NULL.
// At end-of-stream, this method should be called repeatedly with
// end-of-stream DecoderBuffer until no frame/buffer can be produced.
// These methods can only be called after the corresponding decoder has
// been successfully initialized.
virtual void DecryptAndDecodeAudio(
const scoped_refptr<DecoderBuffer>& encrypted,
const AudioDecodeCB& audio_decode_cb) = 0;
virtual void DecryptAndDecodeVideo(
const scoped_refptr<DecoderBuffer>& encrypted,
const VideoDecodeCB& video_decode_cb) = 0;
// Resets the decoder to an initialized clean state, cancels any scheduled
// decrypt-and-decode operations, and fires any pending
// AudioDecodeCB/VideoDecodeCB immediately with kError and NULL.
// This method can only be called after the corresponding decoder has been
// successfully initialized.
virtual void ResetDecoder(StreamType stream_type) = 0;
// Releases decoder resources, deinitializes the decoder, cancels any
// scheduled initialization or decrypt-and-decode operations, and fires
// any pending DecoderInitCB/AudioDecodeCB/VideoDecodeCB immediately.
// DecoderInitCB should be fired with false. AudioDecodeCB/VideoDecodeCB
// should be fired with kError.
// This method can be called any time after Initialize{Audio|Video}Decoder()
// has been called (with the correct stream type).
// After this operation, the decoder is set to an uninitialized state.
// The decoder can be reinitialized after it is uninitialized.
virtual void DeinitializeDecoder(StreamType stream_type) = 0;
private:
DISALLOW_COPY_AND_ASSIGN(Decryptor);
};
// Callback to notify that a decryptor is ready.
typedef base::Callback<void(Decryptor*)> DecryptorReadyCB;
// Callback to set/cancel a DecryptorReadyCB.
// Calling this callback with a non-null callback registers decryptor ready
// notification. When the decryptor is ready, notification will be sent
// through the provided callback.
// Calling this callback with a null callback cancels previously registered
// decryptor ready notification. Any previously provided callback will be
// fired immediately with NULL.
typedef base::Callback<void(const DecryptorReadyCB&)> SetDecryptorReadyCB;
// Key event callbacks. See the spec for details:
// http://dvcs.w3.org/hg/html-media/raw-file/eme-v0.1b/encrypted-media/encrypted-media.html#event-summary
typedef base::Callback<void(const std::string& key_system,
const std::string& session_id)> KeyAddedCB;
typedef base::Callback<void(const std::string& key_system,
const std::string& session_id,
media::Decryptor::KeyError error_code,
int system_code)> KeyErrorCB;
typedef base::Callback<void(const std::string& key_system,
const std::string& session_id,
const std::string& message,
const std::string& default_url)> KeyMessageCB;
typedef base::Callback<void(const std::string& key_system,
const std::string& session_id,
const std::string& type,
scoped_array<uint8> init_data,
int init_data_size)> NeedKeyCB;
} // namespace media
#endif // MEDIA_BASE_DECRYPTOR_H_
|