Re: [EME] object-oriented design representing sessions as objects

Below are a modified version of the proposed IDL changes along with a
couple examples modified to reflect the changes.

I modified the proposal in section 9.2 to have an explicit
createKeySession() method and keep together generateKeyRequest() with the
other methods on the session object. sessionId is also no longer optional.
Moving generateKeyRequest() allows multiple initData values to be managed
in the same session. While not shown here, needkey is still fired at the
media element and the other events are fired at the session object. It is
still TBD whether needkey can be fired at the session object.

The only real drawback I encountered while modifying the examples was that
related event handlers must be specified on different objects and those on
the session object could no longer be specified declaratively. I also had
to be careful to get some values (keySystem and sessionId) from the session
object (event.target) rather than the event.

partial interface HTMLMediaElement {
    MediaKeySession createKeySession <#dom-createkeysession>(in
DOMString keySystem <#key-system>);
};

interface MediaKeySession : EventTarget <#dom-eventtarget> {
    readonly attribute DOMString keySystem <#dom-keysystem>;
    readonly attribute DOMString sessionId <#dom-sessionid>;

    void generateKeyRequest <#dom-generatekeyrequest>(in Uint8Array? initData);
    void addKey <#dom-addkey>(in Uint8Array key);
    void cancel();
};


This is an updated version of Example 7.2.2.

<script>
  function handleKeyNeeded(event) {
    if (event.keySystem <#dom-keysystem> && event.keySystem
<#dom-keysystem> != "com.example.somesystem.1_0")
      throw "Unhandled keySystem in event";
    var initData = event.initData <#dom-initdata>;
    var video = event.target;

    var session = video.createKeySession("com.example.somesystem.1_0");
    session.onkeymessage <#dom-keymessage>="licenseRequestReady(event)"
    session.generateKeyRequest <#dom-generatekeyrequest>(initData);
  }

  function licenseRequestReady(event) {
    var session = event.target;
    if (session.keySystem <#dom-keysystem> != "com.example.somesystem.1_0")
      throw "Unhandled keySystem in event";
    var request = event.message <#dom-message>;
    if (!request)
      throw "Could not create license request";

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "http://.../getkey", false);
    xmlhttp.send(request);
    var license = new Uint8Array(xmlhttp.response);
    session.addKey <#dom-addkey>(license);
  }
</script>

<video src="foo.webm" autoplay onneedkey
<#dom-needkey>="handleKeyNeeded(event)"></video>


This is an updated version of Example 7.4.


<script>
  var keySystem;
  var licenseUrl;

  function handleMessageResponse() {
    var license = new Uint8Array(xmlhttp.response);
    this.session.addKey <#dom-addkey>(license);
  }

  function sendMessage(message, session) {
    xmlhttp = new XMLHttpRequest();
    xmlhttp.session = session;
    xmlhttp.onreadystatechange = handleMessageResponse;
    xmlhttp.open("POST", licenseUrl, true);
    xmlhttp.send(message);
  }

  function handleKeyMessage(event) {
    var session = event.target;
    if (session.keySystem <#dom-keysystem> != keySystem)
      throw "Message from unexpected Key System";
    var message = event.message <#dom-message>;
    if (!message)
      throw "Invalid key message";

    sendMessage(message, session);
  }

  function handleKeyComplete(event) {
    // Do some bookkeeping with event.target.sessionId
<#dom-sessionid> if necessary.
  }

  function handleKeyError(event) {
    // Report event.errorCode <#dom-errorcode> and do some bookkeeping
with event.target.sessionId <#dom-sessionid> if necessary.
  }

  function handleKeyNeeded(event) {
    var targetKeySystem = event.keySystem <#dom-keysystem>;
    if (targetKeySystem == null) {
      selectKeySystem(video);  // See previous example for implementation.
      targetKeySystem = keySystem;
    }
    var initData = event.initData <#dom-initdata>;
    var video = event.target;

    var session = video.createKeySession(targetKeySystem);
    session.onkeymessage <#dom-keymessage>="handleKeyMessage(event)";
    session.onkeyadded <#dom-keyadded>="handleKeyComplete(event)";
    session.onkeyerror <#dom-keyerror>="handleKeyError(event)";

    session.generateKeyRequest <#dom-generatekeyrequest>(initData);
  }
</script>

<video src="foo.webm" autoplay onneedkey
<#dom-needkey>="handleKeyNeeded(event)"></video>

Received on Monday, 25 June 2012 04:23:57 UTC