1 // Copyright 2014 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.
5 // The PlayerUtils provides utility functions to binding common media events
6 // to specific player functions. It also provides functions to load media source
7 // base on test configurations.
8 var PlayerUtils = new function() {
11 // Prepares a video element for playback by setting default event handlers
12 // and source attribute.
13 PlayerUtils.registerDefaultEventListeners = function(player) {
14 Utils.timeLog('Registering video event handlers.');
15 // Map from event name to event listener function name. It is common for
16 // event listeners to be named onEventName.
17 var eventListenerMap = {
18 'encrypted': 'onEncrypted',
19 'webkitneedkey': 'onWebkitNeedKey',
20 'webkitkeymessage': 'onWebkitKeyMessage',
21 'webkitkeyadded': 'onWebkitKeyAdded',
22 'webkitkeyerror': 'onWebkitKeyError'
24 for (eventName in eventListenerMap) {
25 var eventListenerFunction = player[eventListenerMap[eventName]];
26 if (eventListenerFunction) {
27 player.video.addEventListener(eventName, function(e) {
28 player[eventListenerMap[e.type]](e);
32 // List of events that fail tests.
33 var failingEvents = ['error', 'abort'];
34 for (var i = 0; i < failingEvents.length; i++) {
35 player.video.addEventListener(failingEvents[i], Utils.failTest);
39 // Register the necessary event handlers needed when playing encrypted content
40 // using the unprefixed API. Returns a promise that resolves to the player.
41 PlayerUtils.registerEMEEventListeners = function(player) {
42 player.video.addEventListener('encrypted', function(message) {
44 function addMediaKeySessionListeners(mediaKeySession) {
45 mediaKeySession.addEventListener('message', function(message) {
46 player.video.receivedKeyMessage = true;
47 if (Utils.isRenewalMessage(message)) {
48 Utils.timeLog('MediaKeySession onMessage - renewal', message);
49 player.video.receivedRenewalMessage = true;
51 if (message.messageType != 'license-request') {
52 Utils.failTest('Unexpected message type "' + message.messageType +
57 player.onMessage(message);
59 mediaKeySession.addEventListener('error', function(error) {
60 Utils.failTest(error, KEY_ERROR);
65 if (player.testConfig.sessionToLoad) {
66 Utils.timeLog('Loading session: ' + player.testConfig.sessionToLoad);
68 message.target.mediaKeys.createSession('persistent-license');
69 addMediaKeySessionListeners(session);
70 session.load(player.testConfig.sessionToLoad)
71 .catch(function(error) { Utils.failTest(error, KEY_ERROR); });
73 Utils.timeLog('Creating new media key session for initDataType: ' +
74 message.initDataType + ', initData: ' +
75 Utils.getHexString(new Uint8Array(message.initData)));
76 var session = message.target.mediaKeys.createSession();
77 addMediaKeySessionListeners(session);
78 session.generateRequest(message.initDataType, message.initData)
79 .catch(function(error) { Utils.failTest(error, KEY_ERROR); });
86 this.registerDefaultEventListeners(player);
87 player.video.receivedKeyMessage = false;
88 Utils.timeLog('Setting video media keys: ' + player.testConfig.keySystem);
90 if (player.testConfig.sessionToLoad) {
92 persistentState: "required",
93 sessionTypes: ["temporary", "persistent-license"]
96 return navigator.requestMediaKeySystemAccess(
97 player.testConfig.keySystem, [config])
98 .then(function(access) { return access.createMediaKeys(); })
99 .then(function(mediaKeys) {
100 return player.video.setMediaKeys(mediaKeys);
102 .then(function(result) { return player; })
103 .catch(function(error) { Utils.failTest(error, NOTSUPPORTEDERROR); });
106 // Register the necessary event handlers needed when playing encrypted content
107 // using the prefixed API. Even though the prefixed API is all synchronous,
108 // returns a promise that resolves to the player.
109 PlayerUtils.registerPrefixedEMEEventListeners = function(player) {
110 player.video.addEventListener('webkitneedkey', function(message) {
111 var initData = message.initData;
112 if (player.testConfig.sessionToLoad) {
113 Utils.timeLog('Loading session: ' + player.testConfig.sessionToLoad);
114 initData = Utils.convertToUint8Array(
115 PREFIXED_API_LOAD_SESSION_HEADER + player.testConfig.sessionToLoad);
117 Utils.timeLog(player.testConfig.keySystem +
118 ' Generate key request, initData: ' +
119 Utils.getHexString(initData));
121 message.target.webkitGenerateKeyRequest(player.testConfig.keySystem,
128 player.video.addEventListener('webkitkeyadded', function(message) {
129 Utils.timeLog('onWebkitKeyAdded', message);
130 message.target.receivedKeyAdded = true;
133 player.video.addEventListener('webkitkeyerror', function(error) {
134 Utils.timeLog('onWebkitKeyError', error);
135 Utils.failTest(error, KEY_ERROR);
138 player.video.addEventListener('webkitkeymessage', function(message) {
139 Utils.timeLog('onWebkitKeyMessage', message);
140 message.target.receivedKeyMessage = true;
141 if (Utils.isRenewalMessagePrefixed(message.message)) {
142 Utils.timeLog('onWebkitKeyMessage - renewal', message);
143 message.target.receivedRenewalMessage = true;
147 // The prefixed API is all synchronous, so wrap the calls in a promise.
148 return new Promise(function(resolve, reject) {
149 PlayerUtils.registerDefaultEventListeners(player);
150 player.video.receivedKeyMessage = false;
155 PlayerUtils.setVideoSource = function(player) {
156 if (player.testConfig.useMSE) {
157 Utils.timeLog('Loading media using MSE.');
159 MediaSourceUtils.loadMediaSourceFromTestConfig(player.testConfig);
160 player.video.src = window.URL.createObjectURL(mediaSource);
162 Utils.timeLog('Loading media using src.');
163 player.video.src = player.testConfig.mediaFile;
167 // Initialize the player to play encrypted content. Returns a promise that
168 // resolves to the player.
169 PlayerUtils.initEMEPlayer = function(player) {
170 return player.registerEventListeners().then(function(result) {
171 PlayerUtils.setVideoSource(player);
176 // Return the appropriate player based on test configuration.
177 PlayerUtils.createPlayer = function(video, testConfig) {
178 // Update keySystem if using prefixed Clear Key since it is not available as a
179 // separate key system to choose from; however it can be set in URL query.
180 var usePrefixedEME = testConfig.usePrefixedEME;
181 if (testConfig.keySystem == CLEARKEY && usePrefixedEME)
182 testConfig.keySystem = PREFIXED_CLEARKEY;
184 function getPlayerType(keySystem) {
186 case WIDEVINE_KEYSYSTEM:
188 return PrefixedWidevinePlayer;
189 return WidevinePlayer;
190 case PREFIXED_CLEARKEY:
191 return PrefixedClearKeyPlayer;
192 case EXTERNAL_CLEARKEY:
195 return PrefixedClearKeyPlayer;
196 return ClearKeyPlayer;
197 case FILE_IO_TEST_KEYSYSTEM:
198 return FileIOTestPlayer;
200 Utils.timeLog(keySystem + ' is not a known key system');
202 return PrefixedClearKeyPlayer;
203 return ClearKeyPlayer;
206 var Player = getPlayerType(testConfig.keySystem);
207 return new Player(video, testConfig);