Add needkey/keyadded handler to support encrypted media.

Change-Id: I4f17e34a0b2dc6a5555cc87dd2790e9ab312a26e
diff --git a/adaptive/dash-player.html b/adaptive/dash-player.html
index edf50cc..ef96786 100644
--- a/adaptive/dash-player.html
+++ b/adaptive/dash-player.html
@@ -272,7 +272,7 @@
 
 </head>
   <body>
-    <input id="u" type="text" size="80" value="webm_dash_mevq-vid6-aud1.xml" /><input type="button" value="Start" onClick="onStartClick();"><br/>
+    <input id="u" type="text" size="80" value="webm_dash_mevq-key-enc-vid6-aud1.mpd.xml" /><input type="button" value="Start" onClick="onStartClick();"><br/>
     <video id="v" autoplay>
     </video>
 
diff --git a/adaptive/dash_player.js b/adaptive/dash_player.js
index d6a64d2..b2c99e1 100644
--- a/adaptive/dash_player.js
+++ b/adaptive/dash_player.js
@@ -551,6 +551,12 @@
   this.videoElement.addEventListener('webkitsourceclose', function() {
     t.doClose();
   });
+  this.videoElement.addEventListener('webkitneedkey', function(e) {
+    t.doNeedKey(e);
+  });
+  this.videoElement.addEventListener('webkitkeyadded', function() {
+    t.doKeyAdded();
+  });
   this.videoElement.addEventListener('seeking', function() {
     t.doSeeking();
   });
@@ -879,6 +885,33 @@
 };
 
 /**
+ * Handle Media Source webkitneedkey event.
+ * @param {Object} e Need key message.
+ */
+DashPlayer.prototype.doNeedKey = function(e) {
+  this.log('doNeedKey() : ');
+  var key_id = e.initData;
+  this.log('Need key for the following ID: ');
+  this.log(key_id);
+  // In a real implementation, we would fetch the key. For now, the key ID is
+  // the key.
+  var key = key_id;
+  this.log('Adding key for the following ID: ');
+  this.log(key_id);
+  this.videoElement.webkitAddKey("webkit-org.w3.clearkey", key, key_id);
+};
+
+/**
+ * Handle Media Source webkitkeyadded event.
+ */
+DashPlayer.prototype.doKeyAdded = function() {
+  this.log('doKeyAdded() : ');
+  // TODO(xhwang): Remove and restore original call point when Chrome is capable
+  // of resuming playback when key is added. See http://crbug.com/125753.
+  this.loadFirstClusters();
+};
+
+/**
  * Tells the video tag there was a parsing error on the stream.
  */
 DashPlayer.prototype.reportParseError = function() {
@@ -1197,7 +1230,11 @@
 
   this.changeState(DashPlayer.LOADING);
   this.appendData(infoTracks);
-  this.loadFirstClusters();
+  // TODO(xhwang): Until Chrome is capable of resuming playback when a key is
+  // added, we must not load the clusters. See http://crbug.com/125753.
+  // For now, only do this if EME is not enabled.
+  if (!this.videoElement.webkitAddKey)
+    this.loadFirstClusters();
 };
 
 /**