Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / media / cdm / ppapi / api / content_decryption_module.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 CDM_CONTENT_DECRYPTION_MODULE_H_
6 #define CDM_CONTENT_DECRYPTION_MODULE_H_
7
8 #if defined(_MSC_VER)
9 typedef unsigned char uint8_t;
10 typedef unsigned int uint32_t;
11 typedef int int32_t;
12 typedef __int64 int64_t;
13 #else
14 #include <stdint.h>
15 #endif
16
17 // Define CDM_EXPORT so that functionality implemented by the CDM module
18 // can be exported to consumers.
19 #if defined(WIN32)
20
21 #if defined(CDM_IMPLEMENTATION)
22 #define CDM_EXPORT __declspec(dllexport)
23 #else
24 #define CDM_EXPORT __declspec(dllimport)
25 #endif  // defined(CDM_IMPLEMENTATION)
26
27 #else  // defined(WIN32)
28
29 #if defined(CDM_IMPLEMENTATION)
30 #define CDM_EXPORT __attribute__((visibility("default")))
31 #else
32 #define CDM_EXPORT
33 #endif
34
35 #endif  // defined(WIN32)
36
37 // The version number must be rolled when the exported functions are updated!
38 // If the CDM and the adapter use different versions of these functions, the
39 // adapter will fail to load or crash!
40 #define CDM_MODULE_VERSION 4
41
42 // Build the versioned entrypoint name.
43 // The extra macros are necessary to expand version to an actual value.
44 #define INITIALIZE_CDM_MODULE \
45   BUILD_ENTRYPOINT(InitializeCdmModule, CDM_MODULE_VERSION)
46 #define BUILD_ENTRYPOINT(name, version) \
47   BUILD_ENTRYPOINT_NO_EXPANSION(name, version)
48 #define BUILD_ENTRYPOINT_NO_EXPANSION(name, version) name##_##version
49
50 extern "C" {
51 CDM_EXPORT void INITIALIZE_CDM_MODULE();
52
53 CDM_EXPORT void DeinitializeCdmModule();
54
55 // Returns a pointer to the requested CDM Host interface upon success.
56 // Returns NULL if the requested CDM Host interface is not supported.
57 // The caller should cast the returned pointer to the type matching
58 // |host_interface_version|.
59 typedef void* (*GetCdmHostFunc)(int host_interface_version, void* user_data);
60
61 // Returns a pointer to the requested CDM upon success.
62 // Returns NULL if an error occurs or the requested |cdm_interface_version| or
63 // |key_system| is not supported or another error occurs.
64 // The caller should cast the returned pointer to the type matching
65 // |cdm_interface_version|.
66 // Caller retains ownership of arguments and must call Destroy() on the returned
67 // object.
68 CDM_EXPORT void* CreateCdmInstance(
69     int cdm_interface_version,
70     const char* key_system, uint32_t key_system_size,
71     GetCdmHostFunc get_cdm_host_func, void* user_data);
72
73 CDM_EXPORT const char* GetCdmVersion();
74 }
75
76 namespace cdm {
77
78 class AudioFrames_2;
79 typedef AudioFrames_2 AudioFrames;
80
81 class Host_4;
82 class Host_5;
83 class Host_6;
84
85 class DecryptedBlock;
86 class VideoFrame;
87
88 enum Status {
89   kSuccess = 0,
90   kNeedMoreData,  // Decoder needs more data to produce a decoded frame/sample.
91   kNoKey,  // The required decryption key is not available.
92   kSessionError,  // Session management error.
93   kDecryptError,  // Decryption failed.
94   kDecodeError,  // Error decoding audio or video.
95   kDeferredInitialization  // Decoder is not ready for initialization.
96 };
97
98 // This must be consistent with MediaKeyError defined in the spec:
99 // https://dvcs.w3.org/hg/html-media/raw-file/eme-v0.1b/encrypted-media/encrypted-media.html#error-codes
100 // Support the minimum required set with backwards compatible values.
101 enum MediaKeyError {
102   kPrefixedUnknownError = 1,
103   kPrefixedClientError = 2,
104   kPrefixedOutputError = 4
105 };
106
107 // This must at least contain the exceptions defined in the spec:
108 // https://dvcs.w3.org/hg/html-media/raw-file/default/encrypted-media/encrypted-media.html#exceptions
109 // The following starts with the list of DOM4 exceptions from:
110 // http://www.w3.org/TR/dom/#domexception
111 // Some DOM4 exceptions are not included as they are not expected to be used.
112 enum Error {
113   kNotSupportedError = 9,
114   kInvalidStateError = 11,
115   kInvalidAccessError = 15,
116   kQuotaExceededError = 22,
117
118   // Additional exceptions that don't have assigned codes.
119   // There are other non-EME-specific values, not included in this list.
120   kUnknownError = 30,
121
122   // Additional values from previous EME versions. They currently have no
123   // matching DOMException.
124   kClientError = 100,
125   kOutputError = 101
126 };
127
128 // Time is defined as the number of seconds since the
129 // Epoch (00:00:00 UTC, January 1, 1970).
130 typedef double Time;
131
132 // An input buffer can be split into several continuous subsamples.
133 // A SubsampleEntry specifies the number of clear and cipher bytes in each
134 // subsample. For example, the following buffer has three subsamples:
135 //
136 // |<----- subsample1 ----->|<----- subsample2 ----->|<----- subsample3 ----->|
137 // |   clear1   |  cipher1  |  clear2  |   cipher2   | clear3 |    cipher3    |
138 //
139 // For decryption, all of the cipher bytes in a buffer should be concatenated
140 // (in the subsample order) into a single logical stream. The clear bytes should
141 // not be considered as part of decryption.
142 //
143 // Stream to decrypt:   |  cipher1  |   cipher2   |    cipher3    |
144 // Decrypted stream:    | decrypted1|  decrypted2 |   decrypted3  |
145 //
146 // After decryption, the decrypted bytes should be copied over the position
147 // of the corresponding cipher bytes in the original buffer to form the output
148 // buffer. Following the above example, the decrypted buffer should be:
149 //
150 // |<----- subsample1 ----->|<----- subsample2 ----->|<----- subsample3 ----->|
151 // |   clear1   | decrypted1|  clear2  |  decrypted2 | clear3 |   decrypted3  |
152 //
153 // TODO(xhwang): Add checks to make sure these structs have fixed layout.
154 struct SubsampleEntry {
155   SubsampleEntry(uint32_t clear_bytes, uint32_t cipher_bytes)
156       : clear_bytes(clear_bytes), cipher_bytes(cipher_bytes) {}
157
158   uint32_t clear_bytes;
159   uint32_t cipher_bytes;
160 };
161
162 // Represents an input buffer to be decrypted (and possibly decoded). It does
163 // not own any pointers in this struct.
164 // Deprecated: New CDM implementations should use InputBuffer.
165 struct InputBuffer_1 {
166   InputBuffer_1()
167       : data(NULL),
168         data_size(0),
169         data_offset(0),
170         key_id(NULL),
171         key_id_size(0),
172         iv(NULL),
173         iv_size(0),
174         subsamples(NULL),
175         num_subsamples(0),
176         timestamp(0) {}
177
178   const uint8_t* data;  // Pointer to the beginning of the input data.
179   uint32_t data_size;  // Size (in bytes) of |data|.
180
181   uint32_t data_offset;  // Number of bytes to be discarded before decryption.
182
183   const uint8_t* key_id;  // Key ID to identify the decryption key.
184   uint32_t key_id_size;  // Size (in bytes) of |key_id|.
185
186   const uint8_t* iv;  // Initialization vector.
187   uint32_t iv_size;  // Size (in bytes) of |iv|.
188
189   const struct SubsampleEntry* subsamples;
190   uint32_t num_subsamples;  // Number of subsamples in |subsamples|.
191
192   int64_t timestamp;  // Presentation timestamp in microseconds.
193 };
194
195 // Represents an input buffer to be decrypted (and possibly decoded). It does
196 // not own any pointers in this struct. If |iv_size| = 0, the data is
197 // unencrypted.
198 struct InputBuffer_2 {
199   InputBuffer_2()
200       : data(NULL),
201         data_size(0),
202         key_id(NULL),
203         key_id_size(0),
204         iv(NULL),
205         iv_size(0),
206         subsamples(NULL),
207         num_subsamples(0),
208         timestamp(0) {}
209
210   const uint8_t* data;  // Pointer to the beginning of the input data.
211   uint32_t data_size;  // Size (in bytes) of |data|.
212
213   const uint8_t* key_id;  // Key ID to identify the decryption key.
214   uint32_t key_id_size;  // Size (in bytes) of |key_id|.
215
216   const uint8_t* iv;  // Initialization vector.
217   uint32_t iv_size;  // Size (in bytes) of |iv|.
218
219   const struct SubsampleEntry* subsamples;
220   uint32_t num_subsamples;  // Number of subsamples in |subsamples|.
221
222   int64_t timestamp;  // Presentation timestamp in microseconds.
223 };
224
225 typedef InputBuffer_2 InputBuffer;
226
227 struct AudioDecoderConfig {
228   enum AudioCodec {
229     kUnknownAudioCodec = 0,
230     kCodecVorbis,
231     kCodecAac
232   };
233
234   AudioDecoderConfig()
235       : codec(kUnknownAudioCodec),
236         channel_count(0),
237         bits_per_channel(0),
238         samples_per_second(0),
239         extra_data(NULL),
240         extra_data_size(0) {}
241
242   AudioCodec codec;
243   int32_t channel_count;
244   int32_t bits_per_channel;
245   int32_t samples_per_second;
246
247   // Optional byte data required to initialize audio decoders, such as the
248   // vorbis setup header.
249   uint8_t* extra_data;
250   uint32_t extra_data_size;
251 };
252
253 // Supported sample formats for AudioFrames.
254 enum AudioFormat {
255   kUnknownAudioFormat = 0,  // Unknown format value. Used for error reporting.
256   kAudioFormatU8,  // Interleaved unsigned 8-bit w/ bias of 128.
257   kAudioFormatS16,  // Interleaved signed 16-bit.
258   kAudioFormatS32,  // Interleaved signed 32-bit.
259   kAudioFormatF32,  // Interleaved float 32-bit.
260   kAudioFormatPlanarS16,  // Signed 16-bit planar.
261   kAudioFormatPlanarF32,  // Float 32-bit planar.
262 };
263
264 // Surface formats based on FOURCC labels, see: http://www.fourcc.org/yuv.php
265 enum VideoFormat {
266   kUnknownVideoFormat = 0,  // Unknown format value. Used for error reporting.
267   kYv12,  // 12bpp YVU planar 1x1 Y, 2x2 VU samples.
268   kI420  // 12bpp YVU planar 1x1 Y, 2x2 UV samples.
269 };
270
271 struct Size {
272   Size() : width(0), height(0) {}
273   Size(int32_t width, int32_t height) : width(width), height(height) {}
274
275   int32_t width;
276   int32_t height;
277 };
278
279 struct VideoDecoderConfig {
280   enum VideoCodec {
281     kUnknownVideoCodec = 0,
282     kCodecVp8,
283     kCodecH264,
284     kCodecVp9
285   };
286
287   enum VideoCodecProfile {
288     kUnknownVideoCodecProfile = 0,
289     kProfileNotNeeded,
290     kH264ProfileBaseline,
291     kH264ProfileMain,
292     kH264ProfileExtended,
293     kH264ProfileHigh,
294     kH264ProfileHigh10,
295     kH264ProfileHigh422,
296     kH264ProfileHigh444Predictive
297   };
298
299   VideoDecoderConfig()
300       : codec(kUnknownVideoCodec),
301         profile(kUnknownVideoCodecProfile),
302         format(kUnknownVideoFormat),
303         extra_data(NULL),
304         extra_data_size(0) {}
305
306   VideoCodec codec;
307   VideoCodecProfile profile;
308   VideoFormat format;
309
310   // Width and height of video frame immediately post-decode. Not all pixels
311   // in this region are valid.
312   Size coded_size;
313
314   // Optional byte data required to initialize video decoders, such as H.264
315   // AAVC data.
316   uint8_t* extra_data;
317   uint32_t extra_data_size;
318 };
319
320 enum StreamType {
321   kStreamTypeAudio = 0,
322   kStreamTypeVideo = 1
323 };
324
325 // Structure provided to ContentDecryptionModule::OnPlatformChallengeResponse()
326 // after a platform challenge was initiated via Host::SendPlatformChallenge().
327 // All values will be NULL / zero in the event of a challenge failure.
328 struct PlatformChallengeResponse {
329   // |challenge| provided during Host::SendPlatformChallenge() combined with
330   // nonce data and signed with the platform's private key.
331   const uint8_t* signed_data;
332   uint32_t signed_data_length;
333
334   // RSASSA-PKCS1-v1_5-SHA256 signature of the |signed_data| block.
335   const uint8_t* signed_data_signature;
336   uint32_t signed_data_signature_length;
337
338   // X.509 device specific certificate for the |service_id| requested.
339   const uint8_t* platform_key_certificate;
340   uint32_t platform_key_certificate_length;
341 };
342
343 // Used when passing arrays of binary data. Does not own the referenced data.
344 struct BinaryData {
345   BinaryData() : data(NULL), length(0) {}
346   const uint8_t* data;
347   uint32_t length;
348 };
349
350 // Supported output protection methods for use with EnableOutputProtection() and
351 // returned by OnQueryOutputProtectionStatus().
352 enum OutputProtectionMethods {
353   kProtectionNone = 0,
354   kProtectionHDCP = 1 << 0
355 };
356
357 // Connected output link types returned by OnQueryOutputProtectionStatus().
358 enum OutputLinkTypes {
359   kLinkTypeNone = 0,
360   kLinkTypeUnknown = 1 << 0,
361   kLinkTypeInternal = 1 << 1,
362   kLinkTypeVGA = 1 << 2,
363   kLinkTypeHDMI = 1 << 3,
364   kLinkTypeDVI = 1 << 4,
365   kLinkTypeDisplayPort = 1 << 5,
366   kLinkTypeNetwork = 1 << 6
367 };
368
369 // The type of session to create. The valid types are defined in the spec:
370 // https://dvcs.w3.org/hg/html-media/raw-file/default/encrypted-media/encrypted-media.html#dom-sessiontype
371 enum SessionType {
372   kTemporary = 0,
373   kPersistent = 1
374 };
375
376 // FileIO interface provides a way for the CDM to store data in a file in
377 // persistent storage. This interface aims only at providing basic read/write
378 // capabilities and should not be used as a full fledged file IO API.
379 // Each domain (e.g. "example.com") and each CDM has it's own persistent
380 // storage. All instances of a given CDM associated with a given domain share
381 // the same persistent storage.
382 class FileIO {
383  public:
384   // Opens the file with |file_name| for read and write.
385   // FileIOClient::OnOpenComplete() will be called after the opening
386   // operation finishes.
387   // - When the file is opened by a CDM instance, it will be classified as "in
388   //   use". In this case other CDM instances in the same domain may receive
389   //   kInUse status when trying to open it.
390   // - |file_name| should not include path separators.
391   virtual void Open(const char* file_name, uint32_t file_name_size) = 0;
392
393   // Reads the contents of the file. FileIOClient::OnReadComplete() will be
394   // called with the read status. Read() should not be called if a previous
395   // Read() or Write() call is still pending; otherwise OnReadComplete() will
396   // be called with kInUse.
397   virtual void Read() = 0;
398
399   // Writes |data_size| bytes of |data| into the file.
400   // FileIOClient::OnWriteComplete() will be called with the write status.
401   // All existing contents in the file will be overwritten. Calling Write() with
402   // NULL |data| will clear all contents in the file. Write() should not be
403   // called if a previous Write() or Read() call is still pending; otherwise
404   // OnWriteComplete() will be called with kInUse.
405   virtual void Write(const uint8_t* data, uint32_t data_size) = 0;
406
407   // Closes the file if opened, destroys this FileIO object and releases any
408   // resources allocated. The CDM must call this method when it finished using
409   // this object. A FileIO object must not be used after Close() is called.
410   virtual void Close() = 0;
411
412  protected:
413   FileIO() {}
414   virtual ~FileIO() {}
415 };
416
417 // Responses to FileIO calls. All responses will be called asynchronously.
418 class FileIOClient {
419  public:
420   enum Status {
421     kSuccess = 0,
422     kInUse,
423     kError
424   };
425
426   // Response to a FileIO::Open() call with the open |status|.
427   virtual void OnOpenComplete(Status status) = 0;
428
429   // Response to a FileIO::Read() call to provide |data_size| bytes of |data|
430   // read from the file.
431   // - kSuccess indicates that all contents of the file has been successfully
432   //   read. In this case, 0 |data_size| means that the file is empty.
433   // - kInUse indicates that there are other read/write operations pending.
434   // - kError indicates read failure, e.g. the storage isn't open or cannot be
435   //   fully read.
436   virtual void OnReadComplete(Status status,
437                               const uint8_t* data, uint32_t data_size) = 0;
438
439   // Response to a FileIO::Write() call.
440   // - kSuccess indicates that all the data has been written into the file
441   //   successfully.
442   // - kInUse indicates that there are other read/write operations pending.
443   // - kError indicates write failure, e.g. the storage isn't open or cannot be
444   //   fully written. Upon write failure, the contents of the file should be
445   //   regarded as corrupt and should not used.
446   virtual void OnWriteComplete(Status status) = 0;
447
448  protected:
449   FileIOClient() {}
450   virtual ~FileIOClient() {}
451 };
452
453 // ContentDecryptionModule interface that all CDMs need to implement.
454 // The interface is versioned for backward compatibility.
455 // Note: ContentDecryptionModule implementations must use the allocator
456 // provided in CreateCdmInstance() to allocate any Buffer that needs to
457 // be passed back to the caller. Implementations must call Buffer::Destroy()
458 // when a Buffer is created that will never be returned to the caller.
459 class ContentDecryptionModule_4 {
460  public:
461   static const int kVersion = 4;
462   typedef Host_4 Host;
463
464   // CreateSession(), UpdateSession(), and ReleaseSession() get passed a
465   // |session_id| for a MediaKeySession object. It must be used in the reply via
466   // Host methods (e.g. Host::OnSessionMessage()).
467   // Note: |session_id| is different from MediaKeySession's sessionId attribute,
468   // which is referred to as |web_session_id| in this file.
469
470   // Creates a session given |type| and |init_data|.
471   virtual void CreateSession(
472       uint32_t session_id,
473       const char* type, uint32_t type_size,
474       const uint8_t* init_data, uint32_t init_data_size) = 0;
475
476   // Loads a session that has |web_session_id|.
477   virtual void LoadSession(
478       uint32_t session_id,
479       const char* web_session_id, uint32_t web_session_id_length) = 0;
480
481   // Updates the session with |response|.
482   virtual void UpdateSession(
483       uint32_t session_id,
484       const uint8_t* response, uint32_t response_size) = 0;
485
486   // Releases the resources for the session.
487   virtual void ReleaseSession(uint32_t session_id) = 0;
488
489   // Performs scheduled operation with |context| when the timer fires.
490   virtual void TimerExpired(void* context) = 0;
491
492   // Decrypts the |encrypted_buffer|.
493   //
494   // Returns kSuccess if decryption succeeded, in which case the callee
495   // should have filled the |decrypted_buffer| and passed the ownership of
496   // |data| in |decrypted_buffer| to the caller.
497   // Returns kNoKey if the CDM did not have the necessary decryption key
498   // to decrypt.
499   // Returns kDecryptError if any other error happened.
500   // If the return value is not kSuccess, |decrypted_buffer| should be ignored
501   // by the caller.
502   virtual Status Decrypt(const InputBuffer_1& encrypted_buffer,
503                          DecryptedBlock* decrypted_buffer) = 0;
504
505   // Initializes the CDM audio decoder with |audio_decoder_config|. This
506   // function must be called before DecryptAndDecodeSamples() is called.
507   //
508   // Returns kSuccess if the |audio_decoder_config| is supported and the CDM
509   // audio decoder is successfully initialized.
510   // Returns kSessionError if |audio_decoder_config| is not supported. The CDM
511   // may still be able to do Decrypt().
512   // Returns kDeferredInitialization if the CDM is not ready to initialize the
513   // decoder at this time. Must call Host::OnDeferredInitializationDone() once
514   // initialization is complete.
515   virtual Status InitializeAudioDecoder(
516       const AudioDecoderConfig& audio_decoder_config) = 0;
517
518   // Initializes the CDM video decoder with |video_decoder_config|. This
519   // function must be called before DecryptAndDecodeFrame() is called.
520   //
521   // Returns kSuccess if the |video_decoder_config| is supported and the CDM
522   // video decoder is successfully initialized.
523   // Returns kSessionError if |video_decoder_config| is not supported. The CDM
524   // may still be able to do Decrypt().
525   // Returns kDeferredInitialization if the CDM is not ready to initialize the
526   // decoder at this time. Must call Host::OnDeferredInitializationDone() once
527   // initialization is complete.
528   virtual Status InitializeVideoDecoder(
529       const VideoDecoderConfig& video_decoder_config) = 0;
530
531   // De-initializes the CDM decoder and sets it to an uninitialized state. The
532   // caller can initialize the decoder again after this call to re-initialize
533   // it. This can be used to reconfigure the decoder if the configuration
534   // changes.
535   virtual void DeinitializeDecoder(StreamType decoder_type) = 0;
536
537   // Resets the CDM decoder to an initialized clean state. All internal buffers
538   // MUST be flushed.
539   virtual void ResetDecoder(StreamType decoder_type) = 0;
540
541   // Decrypts the |encrypted_buffer| and decodes the decrypted buffer into a
542   // |video_frame|. Upon end-of-stream, the caller should call this function
543   // repeatedly with empty |encrypted_buffer| (|data| == NULL) until only empty
544   // |video_frame| (|format| == kEmptyVideoFrame) is produced.
545   //
546   // Returns kSuccess if decryption and decoding both succeeded, in which case
547   // the callee will have filled the |video_frame| and passed the ownership of
548   // |frame_buffer| in |video_frame| to the caller.
549   // Returns kNoKey if the CDM did not have the necessary decryption key
550   // to decrypt.
551   // Returns kNeedMoreData if more data was needed by the decoder to generate
552   // a decoded frame (e.g. during initialization and end-of-stream).
553   // Returns kDecryptError if any decryption error happened.
554   // Returns kDecodeError if any decoding error happened.
555   // If the return value is not kSuccess, |video_frame| should be ignored by
556   // the caller.
557   virtual Status DecryptAndDecodeFrame(const InputBuffer_1& encrypted_buffer,
558                                        VideoFrame* video_frame) = 0;
559
560   // Decrypts the |encrypted_buffer| and decodes the decrypted buffer into
561   // |audio_frames|. Upon end-of-stream, the caller should call this function
562   // repeatedly with empty |encrypted_buffer| (|data| == NULL) until only empty
563   // |audio_frames| is produced.
564   //
565   // Returns kSuccess if decryption and decoding both succeeded, in which case
566   // the callee will have filled |audio_frames| and passed the ownership of
567   // |data| in |audio_frames| to the caller.
568   // Returns kNoKey if the CDM did not have the necessary decryption key
569   // to decrypt.
570   // Returns kNeedMoreData if more data was needed by the decoder to generate
571   // audio samples (e.g. during initialization and end-of-stream).
572   // Returns kDecryptError if any decryption error happened.
573   // Returns kDecodeError if any decoding error happened.
574   // If the return value is not kSuccess, |audio_frames| should be ignored by
575   // the caller.
576   virtual Status DecryptAndDecodeSamples(const InputBuffer_1& encrypted_buffer,
577                                          AudioFrames* audio_frames) = 0;
578
579   // Called by the host after a platform challenge was initiated via
580   // Host::SendPlatformChallenge().
581   virtual void OnPlatformChallengeResponse(
582       const PlatformChallengeResponse& response) = 0;
583
584   // Called by the host after a call to Host::QueryOutputProtectionStatus(). The
585   // |link_mask| is a bit mask of OutputLinkTypes. The |output_protection_mask|
586   // is a bit mask of OutputProtectionMethods, indicating whether all
587   // protectable links in |link_mask| are protected. Protectable links include
588   // HDMI, DVI and DisplayPort.
589   virtual void OnQueryOutputProtectionStatus(
590       uint32_t link_mask, uint32_t output_protection_mask) = 0;
591
592   // Destroys the object in the same context as it was created.
593   virtual void Destroy() = 0;
594
595  protected:
596   ContentDecryptionModule_4() {}
597   virtual ~ContentDecryptionModule_4() {}
598 };
599
600 // ContentDecryptionModule interface that all CDMs need to implement.
601 // The interface is versioned for backward compatibility.
602 // Note: ContentDecryptionModule implementations must use the allocator
603 // provided in CreateCdmInstance() to allocate any Buffer that needs to
604 // be passed back to the caller. Implementations must call Buffer::Destroy()
605 // when a Buffer is created that will never be returned to the caller.
606 class ContentDecryptionModule_5 {
607  public:
608   static const int kVersion = 5;
609   typedef Host_5 Host;
610
611   // CreateSession(), LoadSession(), UpdateSession(), and ReleaseSession()
612   // accept a |promise_id|, which must be passed to the completion Host method
613   // (e.g. Host::OnResolveNewSessionPromise()).
614
615   // Creates a session given |init_data_type|, |init_data| and |session_type|.
616   // The CDM must respond by calling either Host::OnResolveNewSessionPromise()
617   // or Host::OnRejectPromise().
618   virtual void CreateSession(
619       uint32_t promise_id,
620       const char* init_data_type, uint32_t init_data_type_size,
621       const uint8_t* init_data, uint32_t init_data_size,
622       SessionType session_type) = 0;
623
624   // Loads the session with |web_session_id|. The CDM must respond by calling
625   // either Host::OnResolveNewSessionPromise() or Host::OnRejectPromise().
626   // If the session is not found, call Host::OnResolveNewSessionPromise()
627   // with web_session_id = NULL.
628   virtual void LoadSession(
629       uint32_t promise_id,
630       const char* web_session_id, uint32_t web_session_id_length) = 0;
631
632   // Updates the session with |response|. The CDM must respond by calling
633   // either Host::OnResolvePromise() or Host::OnRejectPromise().
634   virtual void UpdateSession(
635       uint32_t promise_id,
636       const char* web_session_id, uint32_t web_session_id_length,
637       const uint8_t* response, uint32_t response_size) = 0;
638
639   // Releases the resources for the session. The CDM must respond by calling
640   // either Host::OnResolvePromise() or Host::OnRejectPromise().
641   // Once the session is closed, Host::OnSessionClosed() must also be called.
642   virtual void ReleaseSession(
643       uint32_t promise_id,
644       const char* web_session_id, uint32_t web_session_id_length) = 0;
645
646   // Provides a server certificate to be used to encrypt messages to the
647   // license server.
648   virtual void SetServerCertificate(
649       uint32_t promise_id,
650       const uint8_t* server_certificate_data,
651       uint32_t server_certificate_data_size) = 0;
652
653   // Performs scheduled operation with |context| when the timer fires.
654   virtual void TimerExpired(void* context) = 0;
655
656   // Decrypts the |encrypted_buffer|.
657   //
658   // Returns kSuccess if decryption succeeded, in which case the callee
659   // should have filled the |decrypted_buffer| and passed the ownership of
660   // |data| in |decrypted_buffer| to the caller.
661   // Returns kNoKey if the CDM did not have the necessary decryption key
662   // to decrypt.
663   // Returns kDecryptError if any other error happened.
664   // If the return value is not kSuccess, |decrypted_buffer| should be ignored
665   // by the caller.
666   virtual Status Decrypt(const InputBuffer_1& encrypted_buffer,
667                          DecryptedBlock* decrypted_buffer) = 0;
668
669   // Initializes the CDM audio decoder with |audio_decoder_config|. This
670   // function must be called before DecryptAndDecodeSamples() is called.
671   //
672   // Returns kSuccess if the |audio_decoder_config| is supported and the CDM
673   // audio decoder is successfully initialized.
674   // Returns kSessionError if |audio_decoder_config| is not supported. The CDM
675   // may still be able to do Decrypt().
676   // Returns kDeferredInitialization if the CDM is not ready to initialize the
677   // decoder at this time. Must call Host::OnDeferredInitializationDone() once
678   // initialization is complete.
679   virtual Status InitializeAudioDecoder(
680       const AudioDecoderConfig& audio_decoder_config) = 0;
681
682   // Initializes the CDM video decoder with |video_decoder_config|. This
683   // function must be called before DecryptAndDecodeFrame() is called.
684   //
685   // Returns kSuccess if the |video_decoder_config| is supported and the CDM
686   // video decoder is successfully initialized.
687   // Returns kSessionError if |video_decoder_config| is not supported. The CDM
688   // may still be able to do Decrypt().
689   // Returns kDeferredInitialization if the CDM is not ready to initialize the
690   // decoder at this time. Must call Host::OnDeferredInitializationDone() once
691   // initialization is complete.
692   virtual Status InitializeVideoDecoder(
693       const VideoDecoderConfig& video_decoder_config) = 0;
694
695   // De-initializes the CDM decoder and sets it to an uninitialized state. The
696   // caller can initialize the decoder again after this call to re-initialize
697   // it. This can be used to reconfigure the decoder if the configuration
698   // changes.
699   virtual void DeinitializeDecoder(StreamType decoder_type) = 0;
700
701   // Resets the CDM decoder to an initialized clean state. All internal buffers
702   // MUST be flushed.
703   virtual void ResetDecoder(StreamType decoder_type) = 0;
704
705   // Decrypts the |encrypted_buffer| and decodes the decrypted buffer into a
706   // |video_frame|. Upon end-of-stream, the caller should call this function
707   // repeatedly with empty |encrypted_buffer| (|data| == NULL) until only empty
708   // |video_frame| (|format| == kEmptyVideoFrame) is produced.
709   //
710   // Returns kSuccess if decryption and decoding both succeeded, in which case
711   // the callee will have filled the |video_frame| and passed the ownership of
712   // |frame_buffer| in |video_frame| to the caller.
713   // Returns kNoKey if the CDM did not have the necessary decryption key
714   // to decrypt.
715   // Returns kNeedMoreData if more data was needed by the decoder to generate
716   // a decoded frame (e.g. during initialization and end-of-stream).
717   // Returns kDecryptError if any decryption error happened.
718   // Returns kDecodeError if any decoding error happened.
719   // If the return value is not kSuccess, |video_frame| should be ignored by
720   // the caller.
721   virtual Status DecryptAndDecodeFrame(const InputBuffer_1& encrypted_buffer,
722                                        VideoFrame* video_frame) = 0;
723
724   // Decrypts the |encrypted_buffer| and decodes the decrypted buffer into
725   // |audio_frames|. Upon end-of-stream, the caller should call this function
726   // repeatedly with empty |encrypted_buffer| (|data| == NULL) until only empty
727   // |audio_frames| is produced.
728   //
729   // Returns kSuccess if decryption and decoding both succeeded, in which case
730   // the callee will have filled |audio_frames| and passed the ownership of
731   // |data| in |audio_frames| to the caller.
732   // Returns kNoKey if the CDM did not have the necessary decryption key
733   // to decrypt.
734   // Returns kNeedMoreData if more data was needed by the decoder to generate
735   // audio samples (e.g. during initialization and end-of-stream).
736   // Returns kDecryptError if any decryption error happened.
737   // Returns kDecodeError if any decoding error happened.
738   // If the return value is not kSuccess, |audio_frames| should be ignored by
739   // the caller.
740   virtual Status DecryptAndDecodeSamples(const InputBuffer_1& encrypted_buffer,
741                                          AudioFrames* audio_frames) = 0;
742
743   // Called by the host after a platform challenge was initiated via
744   // Host::SendPlatformChallenge().
745   virtual void OnPlatformChallengeResponse(
746       const PlatformChallengeResponse& response) = 0;
747
748   // Called by the host after a call to Host::QueryOutputProtectionStatus(). The
749   // |link_mask| is a bit mask of OutputLinkTypes and |output_protection_mask|
750   // is a bit mask of OutputProtectionMethods.
751   virtual void OnQueryOutputProtectionStatus(
752       uint32_t link_mask, uint32_t output_protection_mask) = 0;
753
754   // Destroys the object in the same context as it was created.
755   virtual void Destroy() = 0;
756
757  protected:
758   ContentDecryptionModule_5() {}
759   virtual ~ContentDecryptionModule_5() {}
760 };
761
762 // ContentDecryptionModule interface that all CDMs need to implement.
763 // The interface is versioned for backward compatibility.
764 // Note: ContentDecryptionModule implementations must use the allocator
765 // provided in CreateCdmInstance() to allocate any Buffer that needs to
766 // be passed back to the caller. Implementations must call Buffer::Destroy()
767 // when a Buffer is created that will never be returned to the caller.
768 class ContentDecryptionModule_6 {
769  public:
770   static const int kVersion = 6;
771   typedef Host_6 Host;
772
773   // CreateSession(), LoadSession(), UpdateSession(), and ReleaseSession()
774   // accept a |promise_id|, which must be passed to the completion Host method
775   // (e.g. Host::OnResolveNewSessionPromise()).
776
777   // Creates a session given |init_data_type|, |init_data| and |session_type|.
778   // The CDM must respond by calling either Host::OnResolveNewSessionPromise()
779   // or Host::OnRejectPromise().
780   virtual void CreateSession(
781       uint32_t promise_id,
782       const char* init_data_type, uint32_t init_data_type_size,
783       const uint8_t* init_data, uint32_t init_data_size,
784       SessionType session_type) = 0;
785
786   // Loads the session with |web_session_id|. The CDM must respond by calling
787   // either Host::OnResolveNewSessionPromise() or Host::OnRejectPromise().
788   // If the session is not found, call Host::OnResolveNewSessionPromise()
789   // with web_session_id = NULL.
790   virtual void LoadSession(
791       uint32_t promise_id,
792       const char* web_session_id, uint32_t web_session_id_length) = 0;
793
794   // Updates the session with |response|. The CDM must respond by calling
795   // either Host::OnResolvePromise() or Host::OnRejectPromise().
796   virtual void UpdateSession(
797       uint32_t promise_id,
798       const char* web_session_id, uint32_t web_session_id_length,
799       const uint8_t* response, uint32_t response_size) = 0;
800
801   // Requests that the CDM close the session. The CDM must respond by calling
802   // either Host::OnResolvePromise() or Host::OnRejectPromise() when the request
803   // has been processed. This may be before the session is closed. Once the
804   // session is closed, Host::OnSessionClosed() must also be called.
805   virtual void CloseSession(
806       uint32_t promise_id,
807       const char* web_session_id, uint32_t web_session_id_length) = 0;
808
809   // Removes any stored session data associated with this session. Will only be
810   // called for persistent sessions. The CDM must respond by calling either
811   // Host::OnResolvePromise() or Host::OnRejectPromise() when the request has
812   // been processed.
813   virtual void RemoveSession(
814       uint32_t promise_id,
815       const char* web_session_id, uint32_t web_session_id_length) = 0;
816
817   // Requests the key IDs for keys in the session that the CDM knows are
818   // currently usable to decrypt media data. The CDM must respond by calling
819   // either Host::OnResolveGetUsableKeyIdsPromise() or Host::OnRejectPromise().
820   virtual void GetUsableKeyIds(
821       uint32_t promise_id,
822       const char* web_session_id, uint32_t web_session_id_length) = 0;
823
824   // Provides a server certificate to be used to encrypt messages to the
825   // license server. The CDM must respond by calling either
826   // Host::OnResolvePromise() or Host::OnRejectPromise().
827   virtual void SetServerCertificate(
828       uint32_t promise_id,
829       const uint8_t* server_certificate_data,
830       uint32_t server_certificate_data_size) = 0;
831
832   // Performs scheduled operation with |context| when the timer fires.
833   virtual void TimerExpired(void* context) = 0;
834
835   // Decrypts the |encrypted_buffer|.
836   //
837   // Returns kSuccess if decryption succeeded, in which case the callee
838   // should have filled the |decrypted_buffer| and passed the ownership of
839   // |data| in |decrypted_buffer| to the caller.
840   // Returns kNoKey if the CDM did not have the necessary decryption key
841   // to decrypt.
842   // Returns kDecryptError if any other error happened.
843   // If the return value is not kSuccess, |decrypted_buffer| should be ignored
844   // by the caller.
845   virtual Status Decrypt(const InputBuffer& encrypted_buffer,
846                          DecryptedBlock* decrypted_buffer) = 0;
847
848   // Initializes the CDM audio decoder with |audio_decoder_config|. This
849   // function must be called before DecryptAndDecodeSamples() is called.
850   //
851   // Returns kSuccess if the |audio_decoder_config| is supported and the CDM
852   // audio decoder is successfully initialized.
853   // Returns kSessionError if |audio_decoder_config| is not supported. The CDM
854   // may still be able to do Decrypt().
855   // Returns kDeferredInitialization if the CDM is not ready to initialize the
856   // decoder at this time. Must call Host::OnDeferredInitializationDone() once
857   // initialization is complete.
858   virtual Status InitializeAudioDecoder(
859       const AudioDecoderConfig& audio_decoder_config) = 0;
860
861   // Initializes the CDM video decoder with |video_decoder_config|. This
862   // function must be called before DecryptAndDecodeFrame() is called.
863   //
864   // Returns kSuccess if the |video_decoder_config| is supported and the CDM
865   // video decoder is successfully initialized.
866   // Returns kSessionError if |video_decoder_config| is not supported. The CDM
867   // may still be able to do Decrypt().
868   // Returns kDeferredInitialization if the CDM is not ready to initialize the
869   // decoder at this time. Must call Host::OnDeferredInitializationDone() once
870   // initialization is complete.
871   virtual Status InitializeVideoDecoder(
872       const VideoDecoderConfig& video_decoder_config) = 0;
873
874   // De-initializes the CDM decoder and sets it to an uninitialized state. The
875   // caller can initialize the decoder again after this call to re-initialize
876   // it. This can be used to reconfigure the decoder if the configuration
877   // changes.
878   virtual void DeinitializeDecoder(StreamType decoder_type) = 0;
879
880   // Resets the CDM decoder to an initialized clean state. All internal buffers
881   // MUST be flushed.
882   virtual void ResetDecoder(StreamType decoder_type) = 0;
883
884   // Decrypts the |encrypted_buffer| and decodes the decrypted buffer into a
885   // |video_frame|. Upon end-of-stream, the caller should call this function
886   // repeatedly with empty |encrypted_buffer| (|data| == NULL) until only empty
887   // |video_frame| (|format| == kEmptyVideoFrame) is produced.
888   //
889   // Returns kSuccess if decryption and decoding both succeeded, in which case
890   // the callee will have filled the |video_frame| and passed the ownership of
891   // |frame_buffer| in |video_frame| to the caller.
892   // Returns kNoKey if the CDM did not have the necessary decryption key
893   // to decrypt.
894   // Returns kNeedMoreData if more data was needed by the decoder to generate
895   // a decoded frame (e.g. during initialization and end-of-stream).
896   // Returns kDecryptError if any decryption error happened.
897   // Returns kDecodeError if any decoding error happened.
898   // If the return value is not kSuccess, |video_frame| should be ignored by
899   // the caller.
900   virtual Status DecryptAndDecodeFrame(const InputBuffer& encrypted_buffer,
901                                        VideoFrame* video_frame) = 0;
902
903   // Decrypts the |encrypted_buffer| and decodes the decrypted buffer into
904   // |audio_frames|. Upon end-of-stream, the caller should call this function
905   // repeatedly with empty |encrypted_buffer| (|data| == NULL) until only empty
906   // |audio_frames| is produced.
907   //
908   // Returns kSuccess if decryption and decoding both succeeded, in which case
909   // the callee will have filled |audio_frames| and passed the ownership of
910   // |data| in |audio_frames| to the caller.
911   // Returns kNoKey if the CDM did not have the necessary decryption key
912   // to decrypt.
913   // Returns kNeedMoreData if more data was needed by the decoder to generate
914   // audio samples (e.g. during initialization and end-of-stream).
915   // Returns kDecryptError if any decryption error happened.
916   // Returns kDecodeError if any decoding error happened.
917   // If the return value is not kSuccess, |audio_frames| should be ignored by
918   // the caller.
919   virtual Status DecryptAndDecodeSamples(const InputBuffer& encrypted_buffer,
920                                          AudioFrames* audio_frames) = 0;
921
922   // Called by the host after a platform challenge was initiated via
923   // Host::SendPlatformChallenge().
924   virtual void OnPlatformChallengeResponse(
925       const PlatformChallengeResponse& response) = 0;
926
927   // Called by the host after a call to Host::QueryOutputProtectionStatus(). The
928   // |link_mask| is a bit mask of OutputLinkTypes and |output_protection_mask|
929   // is a bit mask of OutputProtectionMethods.
930   virtual void OnQueryOutputProtectionStatus(
931       uint32_t link_mask, uint32_t output_protection_mask) = 0;
932
933   // Destroys the object in the same context as it was created.
934   virtual void Destroy() = 0;
935
936  protected:
937   ContentDecryptionModule_6() {}
938   virtual ~ContentDecryptionModule_6() {}
939 };
940
941 typedef ContentDecryptionModule_6 ContentDecryptionModule;
942
943 // Represents a buffer created by Allocator implementations.
944 class Buffer {
945  public:
946   // Destroys the buffer in the same context as it was created.
947   virtual void Destroy() = 0;
948
949   virtual uint32_t Capacity() const = 0;
950   virtual uint8_t* Data() = 0;
951   virtual void SetSize(uint32_t size) = 0;
952   virtual uint32_t Size() const = 0;
953
954  protected:
955   Buffer() {}
956   virtual ~Buffer() {}
957
958  private:
959   Buffer(const Buffer&);
960   void operator=(const Buffer&);
961 };
962
963 class Host_4 {
964  public:
965   static const int kVersion = 4;
966
967   // Returns a Buffer* containing non-zero members upon success, or NULL on
968   // failure. The caller owns the Buffer* after this call. The buffer is not
969   // guaranteed to be zero initialized. The capacity of the allocated Buffer
970   // is guaranteed to be not less than |capacity|.
971   virtual Buffer* Allocate(uint32_t capacity) = 0;
972
973   // Requests the host to call ContentDecryptionModule::TimerFired() |delay_ms|
974   // from now with |context|.
975   virtual void SetTimer(int64_t delay_ms, void* context) = 0;
976
977   // Returns the current epoch wall time in seconds.
978   virtual double GetCurrentWallTimeInSeconds() = 0;
979
980   // Called by the CDM when a session is created or loaded and the value for the
981   // MediaKeySession's sessionId attribute is available (|web_session_id|).
982   // This must be called before OnSessionMessage() or OnSessionReady() is called
983   // for |session_id|. |web_session_id_length| should not include null
984   // termination.
985   // When called in response to LoadSession(), the |web_session_id| must be the
986   // same as the |web_session_id| passed in LoadSession().
987   virtual void OnSessionCreated(
988       uint32_t session_id,
989       const char* web_session_id, uint32_t web_session_id_length) = 0;
990
991   // Called by the CDM when it has a message for session |session_id|.
992   // Length parameters should not include null termination.
993   virtual void OnSessionMessage(
994       uint32_t session_id,
995       const char* message, uint32_t message_length,
996       const char* destination_url, uint32_t destination_url_length) = 0;
997
998   // Called by the CDM when session |session_id| is ready.
999   // Note: "ready" event is deprecated. This is only used for the prefixed EME
1000   // API's "keyAdded" event. Drop this when we deprecate prefixed EME API.
1001   virtual void OnSessionReady(uint32_t session_id) = 0;
1002
1003   // Called by the CDM when session |session_id| is closed.
1004   virtual void OnSessionClosed(uint32_t session_id) = 0;
1005
1006   // Called by the CDM when an error occurs in session |session_id|.
1007   virtual void OnSessionError(uint32_t session_id,
1008                               MediaKeyError error_code,
1009                               uint32_t system_code) = 0;
1010
1011   // The following are optional methods that may not be implemented on all
1012   // platforms.
1013
1014   // Sends a platform challenge for the given |service_id|. |challenge| is at
1015   // most 256 bits of data to be signed. Once the challenge has been completed,
1016   // the host will call ContentDecryptionModule::OnPlatformChallengeResponse()
1017   // with the signed challenge response and platform certificate. Length
1018   // parameters should not include null termination.
1019   virtual void SendPlatformChallenge(
1020       const char* service_id, uint32_t service_id_length,
1021       const char* challenge, uint32_t challenge_length) = 0;
1022
1023   // Attempts to enable output protection (e.g. HDCP) on the display link. The
1024   // |desired_protection_mask| is a bit mask of OutputProtectionMethods. No
1025   // status callback is issued, the CDM must call QueryOutputProtectionStatus()
1026   // periodically to ensure the desired protections are applied.
1027   virtual void EnableOutputProtection(uint32_t desired_protection_mask) = 0;
1028
1029   // Requests the current output protection status. Once the host has the status
1030   // it will call ContentDecryptionModule::OnQueryOutputProtectionStatus().
1031   virtual void QueryOutputProtectionStatus() = 0;
1032
1033   // Must be called by the CDM if it returned kDeferredInitialization during
1034   // InitializeAudioDecoder() or InitializeVideoDecoder().
1035   virtual void OnDeferredInitializationDone(StreamType stream_type,
1036                                             Status decoder_status) = 0;
1037
1038   // Creates a FileIO object from the host to do file IO operation. Returns NULL
1039   // if a FileIO object cannot be obtained. Once a valid FileIO object is
1040   // returned, |client| must be valid until FileIO::Close() is called. The
1041   // CDM can call this method multiple times to operate on different files.
1042   virtual FileIO* CreateFileIO(FileIOClient* client) = 0;
1043
1044  protected:
1045   Host_4() {}
1046   virtual ~Host_4() {}
1047 };
1048
1049 class Host_5 {
1050  public:
1051   static const int kVersion = 5;
1052
1053   // Returns a Buffer* containing non-zero members upon success, or NULL on
1054   // failure. The caller owns the Buffer* after this call. The buffer is not
1055   // guaranteed to be zero initialized. The capacity of the allocated Buffer
1056   // is guaranteed to be not less than |capacity|.
1057   virtual Buffer* Allocate(uint32_t capacity) = 0;
1058
1059   // Requests the host to call ContentDecryptionModule::TimerFired() |delay_ms|
1060   // from now with |context|.
1061   virtual void SetTimer(int64_t delay_ms, void* context) = 0;
1062
1063   // Returns the current wall time in seconds.
1064   virtual Time GetCurrentTime() = 0;
1065
1066   // Called by the CDM when a session is created or loaded and the value for the
1067   // MediaKeySession's sessionId attribute is available (|web_session_id|).
1068   // This must be called before OnSessionMessage() or OnSessionReady() is called
1069   // for the same session. |web_session_id_length| should not include null
1070   // termination.
1071   // When called in response to LoadSession(), the |web_session_id| must be the
1072   // same as the |web_session_id| passed in LoadSession(), or NULL if the
1073   // session could not be loaded.
1074   virtual void OnResolveNewSessionPromise(
1075       uint32_t promise_id,
1076       const char* web_session_id, uint32_t web_session_id_length) = 0;
1077
1078   // Called by the CDM when a session is updated or released.
1079   virtual void OnResolvePromise(uint32_t promise_id) = 0;
1080
1081   // Called by the CDM when an error occurs as a result of one of the
1082   // ContentDecryptionModule calls that accept a |promise_id|.
1083   // |error| must be specified, |error_message| and |system_code|
1084   // are optional. Length parameters should not include null termination.
1085   virtual void OnRejectPromise(
1086       uint32_t promise_id,
1087       Error error,
1088       uint32_t system_code,
1089       const char* error_message, uint32_t error_message_length) = 0;
1090
1091   // Called by the CDM when it has a message for session |web_session_id|.
1092   // Length parameters should not include null termination.
1093   virtual void OnSessionMessage(
1094       const char* web_session_id, uint32_t web_session_id_length,
1095       const char* message, uint32_t message_length,
1096       const char* destination_url, uint32_t destination_url_length) = 0;
1097
1098   // Called by the CDM when there has been a change in usable keys for
1099   // session |web_session_id|. |has_additional_usable_key| should be set if a
1100   // key is newly usable (e.g. new key available, previously expired key has
1101   // been renewed, etc.) and the browser should attempt to resume playback.
1102   // Length parameter should not include null termination.
1103   virtual void OnSessionKeysChange(
1104       const char* web_session_id, uint32_t web_session_id_length,
1105       bool has_additional_usable_key) = 0;
1106
1107   // Called by the CDM when there has been a change in the expiration time for
1108   // session |web_session_id|. This can happen as the result of an Update() call
1109   // or some other event. If this happens as a result of a call to Update(),
1110   // it must be called before resolving the Update() promise. |new_expiry_time|
1111   // can be 0 to represent "undefined". Length parameter should not include
1112   // null termination.
1113   virtual void OnExpirationChange(
1114       const char* web_session_id, uint32_t web_session_id_length,
1115       Time new_expiry_time) = 0;
1116
1117   // Called by the CDM when session |web_session_id| is ready.
1118   // Note: "ready" event is deprecated. This is only used for the prefixed EME
1119   // API's "keyAdded" event. Drop this when we deprecate prefixed EME API.
1120   // Length parameter should not include null termination.
1121   virtual void OnSessionReady(
1122       const char* web_session_id, uint32_t web_session_id_length) = 0;
1123
1124   // Called by the CDM when session |web_session_id| is closed. Length
1125   // parameter should not include null termination.
1126   virtual void OnSessionClosed(
1127       const char* web_session_id, uint32_t web_session_id_length) = 0;
1128
1129   // Called by the CDM when an error occurs in session |web_session_id|
1130   // unrelated to one of the ContentDecryptionModule calls that accept a
1131   // |promise_id|. |error| must be specified, |error_message| and
1132   // |system_code| are optional. Length parameters should not include null
1133   // termination.
1134   virtual void OnSessionError(
1135       const char* web_session_id, uint32_t web_session_id_length,
1136       Error error,
1137       uint32_t system_code,
1138       const char* error_message, uint32_t error_message_length) = 0;
1139
1140   // The following are optional methods that may not be implemented on all
1141   // platforms.
1142
1143   // Sends a platform challenge for the given |service_id|. |challenge| is at
1144   // most 256 bits of data to be signed. Once the challenge has been completed,
1145   // the host will call ContentDecryptionModule::OnPlatformChallengeResponse()
1146   // with the signed challenge response and platform certificate. Length
1147   // parameters should not include null termination.
1148   virtual void SendPlatformChallenge(
1149       const char* service_id, uint32_t service_id_length,
1150       const char* challenge, uint32_t challenge_length) = 0;
1151
1152   // Attempts to enable output protection (e.g. HDCP) on the display link. The
1153   // |desired_protection_mask| is a bit mask of OutputProtectionMethods. No
1154   // status callback is issued, the CDM must call QueryOutputProtectionStatus()
1155   // periodically to ensure the desired protections are applied.
1156   virtual void EnableOutputProtection(uint32_t desired_protection_mask) = 0;
1157
1158   // Requests the current output protection status. Once the host has the status
1159   // it will call ContentDecryptionModule::OnQueryOutputProtectionStatus().
1160   virtual void QueryOutputProtectionStatus() = 0;
1161
1162   // Must be called by the CDM if it returned kDeferredInitialization during
1163   // InitializeAudioDecoder() or InitializeVideoDecoder().
1164   virtual void OnDeferredInitializationDone(StreamType stream_type,
1165                                             Status decoder_status) = 0;
1166
1167   // Creates a FileIO object from the host to do file IO operation. Returns NULL
1168   // if a FileIO object cannot be obtained. Once a valid FileIO object is
1169   // returned, |client| must be valid until FileIO::Close() is called. The
1170   // CDM can call this method multiple times to operate on different files.
1171   virtual FileIO* CreateFileIO(FileIOClient* client) = 0;
1172
1173  protected:
1174   Host_5() {}
1175   virtual ~Host_5() {}
1176 };
1177
1178 class Host_6 {
1179  public:
1180   static const int kVersion = 6;
1181
1182   // Returns a Buffer* containing non-zero members upon success, or NULL on
1183   // failure. The caller owns the Buffer* after this call. The buffer is not
1184   // guaranteed to be zero initialized. The capacity of the allocated Buffer
1185   // is guaranteed to be not less than |capacity|.
1186   virtual Buffer* Allocate(uint32_t capacity) = 0;
1187
1188   // Requests the host to call ContentDecryptionModule::TimerFired() |delay_ms|
1189   // from now with |context|.
1190   virtual void SetTimer(int64_t delay_ms, void* context) = 0;
1191
1192   // Returns the current wall time in seconds.
1193   virtual Time GetCurrentWallTime() = 0;
1194
1195   // Called by the CDM when a session is created or loaded and the value for the
1196   // MediaKeySession's sessionId attribute is available (|web_session_id|).
1197   // This must be called before OnSessionMessage() or OnSessionReady() is called
1198   // for the same session. |web_session_id_length| should not include null
1199   // termination.
1200   // When called in response to LoadSession(), the |web_session_id| must be the
1201   // same as the |web_session_id| passed in LoadSession(), or NULL if the
1202   // session could not be loaded.
1203   virtual void OnResolveNewSessionPromise(
1204       uint32_t promise_id,
1205       const char* web_session_id, uint32_t web_session_id_length) = 0;
1206
1207   // Called by the CDM when a session is updated or released.
1208   virtual void OnResolvePromise(uint32_t promise_id) = 0;
1209
1210   // Called by the CDM to return a list of key IDs. The caller owns the
1211   // BinaryData array and the values the elements reference.
1212   virtual void OnResolveKeyIdsPromise(
1213       uint32_t promise_id,
1214       const BinaryData* key_ids, uint32_t key_ids_length) = 0;
1215
1216   // Called by the CDM when an error occurs as a result of one of the
1217   // ContentDecryptionModule calls that accept a |promise_id|.
1218   // |error| must be specified, |error_message| and |system_code|
1219   // are optional. Length parameters should not include null termination.
1220   virtual void OnRejectPromise(
1221       uint32_t promise_id,
1222       Error error,
1223       uint32_t system_code,
1224       const char* error_message, uint32_t error_message_length) = 0;
1225
1226   // Called by the CDM when it has a message for session |web_session_id|.
1227   // Length parameters should not include null termination.
1228   virtual void OnSessionMessage(
1229       const char* web_session_id, uint32_t web_session_id_length,
1230       const char* message, uint32_t message_length,
1231       const char* destination_url, uint32_t destination_url_length) = 0;
1232
1233   // Called by the CDM when there has been a change in usable keys for
1234   // session |web_session_id|. |has_additional_usable_key| should be set if a
1235   // key is newly usable (e.g. new key available, previously expired key has
1236   // been renewed, etc.) and the browser should attempt to resume playback.
1237   // Length parameter should not include null termination.
1238   virtual void OnSessionUsableKeysChange(
1239       const char* web_session_id, uint32_t web_session_id_length,
1240       bool has_additional_usable_key) = 0;
1241
1242   // Called by the CDM when there has been a change in the expiration time for
1243   // session |web_session_id|. This can happen as the result of an Update() call
1244   // or some other event. If this happens as a result of a call to Update(),
1245   // it must be called before resolving the Update() promise. |new_expiry_time|
1246   // can be 0 to represent "undefined". Length parameter should not include
1247   // null termination.
1248   virtual void OnExpirationChange(
1249       const char* web_session_id, uint32_t web_session_id_length,
1250       Time new_expiry_time) = 0;
1251
1252   // Called by the CDM when session |web_session_id| is closed. Length
1253   // parameter should not include null termination.
1254   virtual void OnSessionClosed(
1255       const char* web_session_id, uint32_t web_session_id_length) = 0;
1256
1257   // Called by the CDM when an error occurs in session |web_session_id|
1258   // unrelated to one of the ContentDecryptionModule calls that accept a
1259   // |promise_id|. |error| must be specified, |error_message| and
1260   // |system_code| are optional. Length parameters should not include null
1261   // termination.
1262   virtual void OnSessionError(
1263       const char* web_session_id, uint32_t web_session_id_length,
1264       Error error,
1265       uint32_t system_code,
1266       const char* error_message, uint32_t error_message_length) = 0;
1267
1268   // The following are optional methods that may not be implemented on all
1269   // platforms.
1270
1271   // Sends a platform challenge for the given |service_id|. |challenge| is at
1272   // most 256 bits of data to be signed. Once the challenge has been completed,
1273   // the host will call ContentDecryptionModule::OnPlatformChallengeResponse()
1274   // with the signed challenge response and platform certificate. Length
1275   // parameters should not include null termination.
1276   virtual void SendPlatformChallenge(
1277       const char* service_id, uint32_t service_id_length,
1278       const char* challenge, uint32_t challenge_length) = 0;
1279
1280   // Attempts to enable output protection (e.g. HDCP) on the display link. The
1281   // |desired_protection_mask| is a bit mask of OutputProtectionMethods. No
1282   // status callback is issued, the CDM must call QueryOutputProtectionStatus()
1283   // periodically to ensure the desired protections are applied.
1284   virtual void EnableOutputProtection(uint32_t desired_protection_mask) = 0;
1285
1286   // Requests the current output protection status. Once the host has the status
1287   // it will call ContentDecryptionModule::OnQueryOutputProtectionStatus().
1288   virtual void QueryOutputProtectionStatus() = 0;
1289
1290   // Must be called by the CDM if it returned kDeferredInitialization during
1291   // InitializeAudioDecoder() or InitializeVideoDecoder().
1292   virtual void OnDeferredInitializationDone(StreamType stream_type,
1293                                             Status decoder_status) = 0;
1294
1295   // Creates a FileIO object from the host to do file IO operation. Returns NULL
1296   // if a FileIO object cannot be obtained. Once a valid FileIO object is
1297   // returned, |client| must be valid until FileIO::Close() is called. The
1298   // CDM can call this method multiple times to operate on different files.
1299   virtual FileIO* CreateFileIO(FileIOClient* client) = 0;
1300
1301  protected:
1302   Host_6() {}
1303   virtual ~Host_6() {}
1304 };
1305
1306 // Represents a decrypted block that has not been decoded.
1307 class DecryptedBlock {
1308  public:
1309   virtual void SetDecryptedBuffer(Buffer* buffer) = 0;
1310   virtual Buffer* DecryptedBuffer() = 0;
1311
1312   // TODO(tomfinegan): Figure out if timestamp is really needed. If it is not,
1313   // we can just pass Buffer pointers around.
1314   virtual void SetTimestamp(int64_t timestamp) = 0;
1315   virtual int64_t Timestamp() const = 0;
1316
1317  protected:
1318   DecryptedBlock() {}
1319   virtual ~DecryptedBlock() {}
1320 };
1321
1322 class VideoFrame {
1323  public:
1324   enum VideoPlane {
1325     kYPlane = 0,
1326     kUPlane = 1,
1327     kVPlane = 2,
1328     kMaxPlanes = 3,
1329   };
1330
1331   virtual void SetFormat(VideoFormat format) = 0;
1332   virtual VideoFormat Format() const = 0;
1333
1334   virtual void SetSize(cdm::Size size) = 0;
1335   virtual cdm::Size Size() const = 0;
1336
1337   virtual void SetFrameBuffer(Buffer* frame_buffer) = 0;
1338   virtual Buffer* FrameBuffer() = 0;
1339
1340   virtual void SetPlaneOffset(VideoPlane plane, uint32_t offset) = 0;
1341   virtual uint32_t PlaneOffset(VideoPlane plane) = 0;
1342
1343   virtual void SetStride(VideoPlane plane, uint32_t stride) = 0;
1344   virtual uint32_t Stride(VideoPlane plane) = 0;
1345
1346   virtual void SetTimestamp(int64_t timestamp) = 0;
1347   virtual int64_t Timestamp() const = 0;
1348
1349  protected:
1350   VideoFrame() {}
1351   virtual ~VideoFrame() {}
1352 };
1353
1354 // Represents decrypted and decoded audio frames. AudioFrames can contain
1355 // multiple audio output buffers, which are serialized into this format:
1356 //
1357 // |<------------------- serialized audio buffer ------------------->|
1358 // | int64_t timestamp | int64_t length | length bytes of audio data |
1359 //
1360 // For example, with three audio output buffers, the AudioFrames will look
1361 // like this:
1362 //
1363 // |<----------------- AudioFrames ------------------>|
1364 // | audio buffer 0 | audio buffer 1 | audio buffer 2 |
1365 class AudioFrames_2 {
1366  public:
1367   virtual void SetFrameBuffer(Buffer* buffer) = 0;
1368   virtual Buffer* FrameBuffer() = 0;
1369
1370   // The CDM must call this method, providing a valid format, when providing
1371   // frame buffers. Planar data should be stored end to end; e.g.,
1372   // |ch1 sample1||ch1 sample2|....|ch1 sample_last||ch2 sample1|...
1373   virtual void SetFormat(AudioFormat format) = 0;
1374   virtual AudioFormat Format() const = 0;
1375
1376  protected:
1377   AudioFrames_2() {}
1378   virtual ~AudioFrames_2() {}
1379 };
1380
1381 }  // namespace cdm
1382
1383 #endif  // CDM_CONTENT_DECRYPTION_MODULE_H_