Re: Bug#17470 Example code

My understanding from reading the bug is that you want to be able to call
addKey() with a predetermined blob without needing to specify type or
initData. This is possible and is essentially what is shown in Example 8.1

Regarding the example below, I don't think you would need the second
session. You only ever need to specify type and initData if you want to use
the initData in generating a license request. Note that
http://dvcs.w3.org/hg/html-media/raw-file/tip/encrypted-media/encrypted-media.html#dom-closecurrently
says the keys are cleared, so I don't think you would want to
call close().

David

On Tue, Oct 16, 2012 at 11:44 PM, Joe Steele <steele@adobe.com> wrote:

> If no-one has any comments on this example code -- would the editors
> please take an action to add this to the spec in the appropriate place?
>
> I think adding this as section 8.5 with a title of "Pre-loading keys
> during page-load" would work well.
>
>  Joe Steele
> steele@adobe.com
>
> On Oct 15, 2012, at 11:54 AM, Joe Steele wrote:
>
> I had an request from two meetings ago to produce some example code for
> pre-loading a MediaKeys instance independent of the HTMLMediaElements that
> the MediaKeys object may later be attached to.
>
> Here is the example I propose:
> --------
> <script>
>
> var cdm;
>
> // Preload some keys when page loads
> function load()
> {
>   // Create a MediaKeys instance for the desired CDM not associated with
> an HTMLMediaElement
>   cdm = new MediaKeys("com.example.somesystem");
>   if (!cdm)
>       throw "Could not create MediaKeys";
>
>   // Create a new key session using the MediaKeys instance
>   var keySession = cdm.createSession(NULL, NULL);
>   if (!keySession)
>       throw "Could not create MediaKeySession";
>   keySession.onkeymessage="handleMessage(event)";
>
>   // Load an application defined key package into the key session
>   // The key package can be hard-coded into the page or loaded as a
> resource
>   // Loading the key package may cause additional key messages to be sent
>   var Uint8Array keyToPreload;
>   keySession.addKey(keyToPreload);
>   keySession.close();
> }
>
> // Respond to key message
> function handleMessage(event)
> {
>     var keySession = event.target;
>     var message = event.message;
>
>     var xmlhttp = new XMLHttpRequest();
>     xmlhttp.open("POST", "http://.../getkey", false);
>     xmlhttp.send(message);
>
>     var key = new Uint8Array(xmlhttp.response);
>     keySession.addKey(key);
> }
>
> // Attach pre-initialized MediaKeys to an HTMLMediaElement
> function setMediaKeys()
> {
>   var video = event.target;
>   var initData = event.initData;
>
>   if (!cdm)
> throw "MediaKeys was not already created!";
>   if (!video.keys)
> video.keys = cdm;
>
>   // Create a new key session using the mimeType and initData
>   var keySession = video.keys.createSession(mimeType, initData);
>   if (!keySession)
> throw "Could not create MediaKeySession";
>   keySession.onkeymessage="handleMessage(event)";
> }
>
> <body onload="load()">
>   <video src="foo.dash" autoplay onneedkey="setMediaKeys(event)"
> onkeymessage="handleMessage(event)"></video>
> </body>
>
> </script>
> --------
>
> I believe this should all work within the confines of the current draft. I
> am intentionally leaving how the preloaded key package is derived as an
> exercise for the application developer, since there are many possible ways
> to handle that.
>
>  Joe Steele
> steele@adobe.com
>
>
>

Received on Monday, 29 October 2012 09:17:45 UTC