2 // Set the testharness.js timeout to 120 seconds so that it is higher than
3 // the LayoutTest timeout. This prevents testharness.js from prematurely
4 // terminating tests and allows the LayoutTest runner to control when to
6 // FIXME: Change this to use explicit_timeout instead once /resources/testharness.js
7 // is updated to a more recent version.
8 setup({ timeout: 120000 });
10 var SEGMENT_INFO_LIST = [
12 url: '/media/resources/media-source/webm/test.webm',
13 type: 'video/webm; codecs="vp8, vorbis"',
14 // FIXME: Get the init segment duration fixed to match duration after append.
15 // See http://crbug.com/354284.
16 durationInInitSegment: 6.042,
18 // Supports jagged-ended stream end timestamps with some less than duration:
19 bufferedRangeEndBeforeEndOfStream: 6.040,
20 init: { offset: 0, size: 4357 },
22 { offset: 4357, size: 11830, timecode: 0 },
23 { offset: 16187, size: 12588, timecode: 0.385 },
24 { offset: 28775, size: 14588, timecode: 0.779 },
25 { offset: 43363, size: 13023, timecode: 1.174 },
26 { offset: 56386, size: 13127, timecode: 1.592 },
27 { offset: 69513, size: 14456, timecode: 1.987 },
28 { offset: 83969, size: 13458, timecode: 2.381 },
29 { offset: 97427, size: 14566, timecode: 2.776 },
30 { offset: 111993, size: 13201, timecode: 3.171 },
31 { offset: 125194, size: 14061, timecode: 3.566 },
32 { offset: 139255, size: 15353, timecode: 3.96 },
33 { offset: 154608, size: 13618, timecode: 4.378 },
34 { offset: 168226, size: 15094, timecode: 4.773 },
35 { offset: 183320, size: 13069, timecode: 5.168 },
36 { offset: 196389, size: 13788, timecode: 5.563 },
37 { offset: 210177, size: 9009, timecode: 5.957 },
41 url: '/media/resources/media-source/mp4/test.mp4',
42 type: 'video/mp4; codecs="mp4a.40.2, avc1.4D401E"',
43 durationInInitSegment: 6.0368,
45 bufferedRangeEndBeforeEndOfStream: 6.0368,
46 init: { offset: 0, size: 1178 },
48 { offset: 1246, size: 23828, timecode: 0 },
49 { offset: 25142, size: 25394, timecode: 0.797 },
50 { offset: 50604, size: 24761, timecode: 1.594 },
51 { offset: 75433, size: 25138, timecode: 2.390 },
52 { offset: 100639, size: 22935, timecode: 3.187 },
53 { offset: 123642, size: 24995, timecode: 3.984},
54 { offset: 148637, size: 24968, timecode: 4.781 },
55 { offset: 173689, size: 19068, timecode: 5.578 },
56 { offset: 192757, size: 200, timecode: 5.619 },
60 EventExpectationsManager = function(test)
63 this.eventTargetList_ = [];
64 this.waitCallbacks_ = [];
67 EventExpectationsManager.prototype.expectEvent = function(object, eventName, description)
69 var eventInfo = { 'target': object, 'type': eventName, 'description': description};
70 var expectations = this.getExpectations_(object);
71 expectations.push(eventInfo);
74 var waitHandler = this.test_.step_func(this.handleWaitCallback_.bind(this));
75 var eventHandler = this.test_.step_func(function(event)
77 object.removeEventListener(eventName, eventHandler);
78 var expected = expectations[0];
79 assert_equals(event.target, expected.target, "Event target match.");
80 assert_equals(event.type, expected.type, "Event types match.");
81 assert_equals(eventInfo.description, expected.description, "Descriptions match for '" + event.type + "'.");
83 expectations.shift(1);
84 if (t.waitCallbacks_.length > 0)
85 setTimeout(waitHandler, 0);
87 object.addEventListener(eventName, eventHandler);
90 EventExpectationsManager.prototype.waitForExpectedEvents = function(callback)
92 this.waitCallbacks_.push(callback);
93 setTimeout(this.test_.step_func(this.handleWaitCallback_.bind(this)), 0);
96 EventExpectationsManager.prototype.expectingEvents = function()
98 for (var i = 0; i < this.eventTargetList_.length; ++i) {
99 if (this.eventTargetList_[i].expectations.length > 0) {
106 EventExpectationsManager.prototype.handleWaitCallback_ = function()
108 if (this.waitCallbacks_.length == 0 || this.expectingEvents())
110 var callback = this.waitCallbacks_.shift(1);
114 EventExpectationsManager.prototype.getExpectations_ = function(target)
116 for (var i = 0; i < this.eventTargetList_.length; ++i) {
117 var info = this.eventTargetList_[i];
118 if (info.target == target) {
119 return info.expectations;
122 var expectations = [];
123 this.eventTargetList_.push({ 'target': target, 'expectations': expectations });
127 function loadData_(test, url, callback, isBinary)
129 var request = new XMLHttpRequest();
130 request.open("GET", url, true);
132 request.responseType = 'arraybuffer';
134 request.onload = test.step_func(function(event)
136 if (request.status != 200) {
137 assert_unreached("Unexpected status code : " + request.status);
140 var response = request.response;
142 response = new Uint8Array(response);
146 request.onerror = test.step_func(function(event)
148 assert_unreached("Unexpected error");
153 function openMediaSource_(test, mediaTag, callback)
155 var mediaSource = new MediaSource();
156 var mediaSourceURL = URL.createObjectURL(mediaSource);
158 var eventHandler = test.step_func(onSourceOpen);
159 function onSourceOpen(event)
161 mediaSource.removeEventListener('sourceopen', eventHandler);
162 URL.revokeObjectURL(mediaSourceURL);
163 callback(mediaSource);
166 mediaSource.addEventListener('sourceopen', eventHandler);
167 mediaTag.src = mediaSourceURL;
170 var MediaSourceUtil = {};
172 MediaSourceUtil.loadTextData = function(test, url, callback)
174 loadData_(test, url, callback, false);
177 MediaSourceUtil.loadBinaryData = function(test, url, callback)
179 loadData_(test, url, callback, true);
182 MediaSourceUtil.fetchManifestAndData = function(test, manifestFilename, callback)
184 var baseURL = '/media/resources/media-source/';
185 var manifestURL = baseURL + manifestFilename;
186 MediaSourceUtil.loadTextData(test, manifestURL, function(manifestText)
188 var manifest = JSON.parse(manifestText);
190 assert_true(MediaSource.isTypeSupported(manifest.type), manifest.type + " is supported.");
192 var mediaURL = baseURL + manifest.url;
193 MediaSourceUtil.loadBinaryData(test, mediaURL, function(mediaData)
195 callback(manifest.type, mediaData);
200 MediaSourceUtil.extractSegmentData = function(mediaData, info)
202 var start = info.offset;
203 var end = start + info.size;
204 return mediaData.subarray(start, end);
207 MediaSourceUtil.getMediaDataForPlaybackTime = function(mediaData, segmentInfo, playbackTimeToAdd)
209 assert_less_than_equal(playbackTimeToAdd, segmentInfo.duration);
210 var mediaInfo = segmentInfo.media;
211 var start = mediaInfo[0].offset;
213 var segmentIndex = 0;
214 while (segmentIndex < mediaInfo.length && mediaInfo[segmentIndex].timecode <= playbackTimeToAdd)
216 numBytes += mediaInfo[segmentIndex].size;
219 return mediaData.subarray(start, numBytes + start);
222 function getFirstSupportedType(typeList)
224 for (var i = 0; i < typeList.length; ++i) {
225 if (MediaSource.isTypeSupported(typeList[i]))
231 function getSegmentInfo()
233 for (var i = 0; i < SEGMENT_INFO_LIST.length; ++i) {
234 var segmentInfo = SEGMENT_INFO_LIST[i];
235 if (MediaSource.isTypeSupported(segmentInfo.type)) {
242 var audioOnlyTypes = ['audio/webm;codecs="vorbis"', 'audio/mp4;codecs="mp4a.40.2"'];
243 var videoOnlyTypes = ['video/webm;codecs="vp8"', 'video/mp4;codecs="avc1.4D4001"'];
244 var audioVideoTypes = ['video/webm;codecs="vp8,vorbis"', 'video/mp4;codecs="mp4a.40.2"'];
245 MediaSourceUtil.AUDIO_ONLY_TYPE = getFirstSupportedType(audioOnlyTypes);
246 MediaSourceUtil.VIDEO_ONLY_TYPE = getFirstSupportedType(videoOnlyTypes);
247 MediaSourceUtil.AUDIO_VIDEO_TYPE = getFirstSupportedType(audioVideoTypes);
248 MediaSourceUtil.SEGMENT_INFO = getSegmentInfo();
250 MediaSourceUtil.getSubType = function(mimetype) {
251 var slashIndex = mimetype.indexOf("/");
252 var semicolonIndex = mimetype.indexOf(";");
253 if (slashIndex <= 0) {
254 assert_unreached("Invalid mimetype '" + mimetype + "'");
258 var start = slashIndex + 1;
259 if (semicolonIndex >= 0) {
260 if (semicolonIndex <= start) {
261 assert_unreached("Invalid mimetype '" + mimetype + "'");
265 return mimetype.substr(start, semicolonIndex - start)
268 return mimetype.substr(start);
271 // TODO: Add wrapper object to MediaSourceUtil that binds loaded mediaData to its
272 // associated segmentInfo.
274 function addExtraTestMethods(test)
276 test.failOnEvent = function(object, eventName)
278 object.addEventListener(eventName, test.step_func(function(event)
280 assert_unreached("Unexpected event '" + eventName + "'");
284 test.endOnEvent = function(object, eventName)
286 object.addEventListener(eventName, test.step_func(function(event) { test.done(); }));
289 test.eventExpectations_ = new EventExpectationsManager(test);
290 test.expectEvent = function(object, eventName, description)
292 test.eventExpectations_.expectEvent(object, eventName, description);
295 test.waitForExpectedEvents = function(callback)
297 test.eventExpectations_.waitForExpectedEvents(callback);
300 test.waitForCurrentTimeChange = function(mediaElement, callback)
302 var initialTime = mediaElement.currentTime;
304 var onTimeUpdate = test.step_func(function()
306 if (mediaElement.currentTime != initialTime) {
307 mediaElement.removeEventListener('timeupdate', onTimeUpdate);
312 mediaElement.addEventListener('timeupdate', onTimeUpdate);
315 var oldTestDone = test.done.bind(test);
316 test.done = function()
318 if (test.status == test.PASS) {
319 assert_false(test.eventExpectations_.expectingEvents(), "No pending event expectations.");
325 window['MediaSourceUtil'] = MediaSourceUtil;
326 window['media_test'] = function(testFunction, description, options)
328 options = options || {};
329 return async_test(function(test)
331 addExtraTestMethods(test);
333 }, description, options);
335 window['mediasource_test'] = function(testFunction, description, options)
337 return media_test(function(test)
339 var mediaTag = document.createElement("video");
340 document.body.appendChild(mediaTag);
342 // Overload done() so that element added to the document can be removed.
343 test.removeMediaElement_ = true;
344 var oldTestDone = test.done.bind(test);
345 test.done = function()
347 if (test.removeMediaElement_) {
348 document.body.removeChild(mediaTag);
349 test.removeMediaElement_ = false;
354 openMediaSource_(test, mediaTag, function(mediaSource)
356 testFunction(test, mediaTag, mediaSource);
358 }, description, options);
361 window['mediasource_testafterdataloaded'] = function(testFunction, description, options)
363 mediasource_test(function(test, mediaElement, mediaSource)
365 var segmentInfo = MediaSourceUtil.SEGMENT_INFO;
368 assert_unreached("No segment info compatible with this MediaSource implementation.");
372 test.failOnEvent(mediaElement, 'error');
374 var sourceBuffer = mediaSource.addSourceBuffer(segmentInfo.type);
375 MediaSourceUtil.loadBinaryData(test, segmentInfo.url, function(mediaData)
377 testFunction(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData);
379 }, description, options);
382 function timeRangesToString(ranges)
385 for (var i = 0; i < ranges.length; ++i) {
386 s += " [" + ranges.start(i).toFixed(3) + ", " + ranges.end(i).toFixed(3) + ")";
391 window['assertBufferedEquals'] = function(obj, expected, description)
393 var actual = timeRangesToString(obj.buffered);
394 assert_equals(actual, expected, description);