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 setup({ explicit_timeout: 120000 });
8 var SEGMENT_INFO_LIST = [
10 url: '/media/resources/media-source/webm/test.webm',
11 type: 'video/webm; codecs="vp8, vorbis"',
12 durationInInitSegment: 6.042,
14 // Supports jagged-ended stream end timestamps with some less than duration:
15 bufferedRangeEndBeforeEndOfStream: 6.040,
16 init: { offset: 0, size: 4357 },
18 { offset: 4357, size: 11830, timecode: 0, highest_end_time: 0.398 },
19 { offset: 16187, size: 12588, timecode: 0.385, highest_end_time: 0.797 },
20 { offset: 28775, size: 14588, timecode: 0.779, highest_end_time: 1.195 },
21 { offset: 43363, size: 13023, timecode: 1.174, highest_end_time: 1.593 },
22 { offset: 56386, size: 13127, timecode: 1.592, highest_end_time: 1.992 },
23 { offset: 69513, size: 14456, timecode: 1.987, highest_end_time: 2.39 },
24 { offset: 83969, size: 13458, timecode: 2.381, highest_end_time: 2.789 },
25 { offset: 97427, size: 14566, timecode: 2.776, highest_end_time: 3.187 },
26 { offset: 111993, size: 13201, timecode: 3.171, highest_end_time: 3.585 },
27 { offset: 125194, size: 14061, timecode: 3.566, highest_end_time: 3.984 },
28 { offset: 139255, size: 15353, timecode: 3.96, highest_end_time: 4.382 },
29 { offset: 154608, size: 13618, timecode: 4.378, highest_end_time: 4.781 },
30 { offset: 168226, size: 15094, timecode: 4.773, highest_end_time: 5.179 },
31 { offset: 183320, size: 13069, timecode: 5.168, highest_end_time: 5.577 },
32 { offset: 196389, size: 13788, timecode: 5.563, highest_end_time: 5.976 },
33 { offset: 210177, size: 9009, timecode: 5.957, highest_end_time: 6.042 },
37 url: '/media/resources/media-source/mp4/test.mp4',
38 type: 'video/mp4; codecs="mp4a.40.2, avc1.4D401E"',
39 // FIXME: Get the init segment duration fixed to match duration after append.
40 // See http://crbug.com/354284.
41 durationInInitSegment: 6.0368,
43 bufferedRangeEndBeforeEndOfStream: 6.0368,
44 init: { offset: 0, size: 1178 },
46 // FIXME: Fix these timecodes to be PTS, not DTS, and highest_end_times to correspond
47 // to highest PTS+duration, not highest DTS+duration. See http://crbug.com/373039.
48 // FIXME: Some segments are parsed to start with keyframe but DTS < PTS. See
49 // http://crbug.com/371947 and http://crbug.com/367786.
50 { offset: 1246, size: 23828, timecode: 0, highest_end_time: 0.835917 },
51 { offset: 25142, size: 25394, timecode: 0.797, highest_end_time: 1.625395 },
52 { offset: 50604, size: 24761, timecode: 1.594, highest_end_time: 2.414874 },
53 { offset: 75433, size: 25138, timecode: 2.390, highest_end_time: 3.227572 },
54 { offset: 100639, size: 22935, timecode: 3.187, highest_end_time: 4.017051 },
55 { offset: 123642, size: 24995, timecode: 3.984, highest_end_time: 4.806529 },
56 { offset: 148637, size: 24968, timecode: 4.781, highest_end_time: 5.619228 },
57 { offset: 173689, size: 19068, timecode: 5.578, highest_end_time: 6.0424 },
58 { offset: 192757, size: 200, timecode: 5.619, highest_end_time: 6.0424 },
62 EventExpectationsManager = function(test)
65 this.eventTargetList_ = [];
66 this.waitCallbacks_ = [];
69 EventExpectationsManager.prototype.expectEvent = function(object, eventName, description)
71 var eventInfo = { 'target': object, 'type': eventName, 'description': description};
72 var expectations = this.getExpectations_(object);
73 expectations.push(eventInfo);
76 var waitHandler = this.test_.step_func(this.handleWaitCallback_.bind(this));
77 var eventHandler = this.test_.step_func(function(event)
79 object.removeEventListener(eventName, eventHandler);
80 var expected = expectations[0];
81 assert_equals(event.target, expected.target, "Event target match.");
82 assert_equals(event.type, expected.type, "Event types match.");
83 assert_equals(eventInfo.description, expected.description, "Descriptions match for '" + event.type + "'.");
85 expectations.shift(1);
86 if (t.waitCallbacks_.length > 0)
87 setTimeout(waitHandler, 0);
89 object.addEventListener(eventName, eventHandler);
92 EventExpectationsManager.prototype.waitForExpectedEvents = function(callback)
94 this.waitCallbacks_.push(callback);
95 setTimeout(this.test_.step_func(this.handleWaitCallback_.bind(this)), 0);
98 EventExpectationsManager.prototype.expectingEvents = function()
100 for (var i = 0; i < this.eventTargetList_.length; ++i) {
101 if (this.eventTargetList_[i].expectations.length > 0) {
108 EventExpectationsManager.prototype.handleWaitCallback_ = function()
110 if (this.waitCallbacks_.length == 0 || this.expectingEvents())
112 var callback = this.waitCallbacks_.shift(1);
116 EventExpectationsManager.prototype.getExpectations_ = function(target)
118 for (var i = 0; i < this.eventTargetList_.length; ++i) {
119 var info = this.eventTargetList_[i];
120 if (info.target == target) {
121 return info.expectations;
124 var expectations = [];
125 this.eventTargetList_.push({ 'target': target, 'expectations': expectations });
129 function loadData_(test, url, callback, isBinary)
131 var request = new XMLHttpRequest();
132 request.open("GET", url, true);
134 request.responseType = 'arraybuffer';
136 request.onload = test.step_func(function(event)
138 if (request.status != 200) {
139 assert_unreached("Unexpected status code : " + request.status);
142 var response = request.response;
144 response = new Uint8Array(response);
148 request.onerror = test.step_func(function(event)
150 assert_unreached("Unexpected error");
155 function openMediaSource_(test, mediaTag, callback)
157 var mediaSource = new MediaSource();
158 var mediaSourceURL = URL.createObjectURL(mediaSource);
160 var eventHandler = test.step_func(onSourceOpen);
161 function onSourceOpen(event)
163 mediaSource.removeEventListener('sourceopen', eventHandler);
164 URL.revokeObjectURL(mediaSourceURL);
165 callback(mediaSource);
168 mediaSource.addEventListener('sourceopen', eventHandler);
169 mediaTag.src = mediaSourceURL;
172 var MediaSourceUtil = {};
174 MediaSourceUtil.loadTextData = function(test, url, callback)
176 loadData_(test, url, callback, false);
179 MediaSourceUtil.loadBinaryData = function(test, url, callback)
181 loadData_(test, url, callback, true);
184 MediaSourceUtil.fetchManifestAndData = function(test, manifestFilename, callback)
186 var baseURL = '/media/resources/media-source/';
187 var manifestURL = baseURL + manifestFilename;
188 MediaSourceUtil.loadTextData(test, manifestURL, function(manifestText)
190 var manifest = JSON.parse(manifestText);
192 assert_true(MediaSource.isTypeSupported(manifest.type), manifest.type + " is supported.");
194 var mediaURL = baseURL + manifest.url;
195 MediaSourceUtil.loadBinaryData(test, mediaURL, function(mediaData)
197 callback(manifest.type, mediaData);
202 MediaSourceUtil.extractSegmentData = function(mediaData, info)
204 var start = info.offset;
205 var end = start + info.size;
206 return mediaData.subarray(start, end);
209 MediaSourceUtil.getMediaDataForPlaybackTime = function(mediaData, segmentInfo, playbackTimeToAdd)
211 assert_less_than_equal(playbackTimeToAdd, segmentInfo.duration);
212 var mediaInfo = segmentInfo.media;
213 var start = mediaInfo[0].offset;
215 var segmentIndex = 0;
216 while (segmentIndex < mediaInfo.length && mediaInfo[segmentIndex].timecode <= playbackTimeToAdd)
218 numBytes += mediaInfo[segmentIndex].size;
221 return mediaData.subarray(start, numBytes + start);
224 function getFirstSupportedType(typeList)
226 for (var i = 0; i < typeList.length; ++i) {
227 if (MediaSource.isTypeSupported(typeList[i]))
233 function getSegmentInfo()
235 for (var i = 0; i < SEGMENT_INFO_LIST.length; ++i) {
236 var segmentInfo = SEGMENT_INFO_LIST[i];
237 if (MediaSource.isTypeSupported(segmentInfo.type)) {
244 var audioOnlyTypes = ['audio/webm;codecs="vorbis"', 'audio/mp4;codecs="mp4a.40.2"'];
245 var videoOnlyTypes = ['video/webm;codecs="vp8"', 'video/mp4;codecs="avc1.4D4001"'];
246 var audioVideoTypes = ['video/webm;codecs="vp8,vorbis"', 'video/mp4;codecs="mp4a.40.2"'];
247 MediaSourceUtil.AUDIO_ONLY_TYPE = getFirstSupportedType(audioOnlyTypes);
248 MediaSourceUtil.VIDEO_ONLY_TYPE = getFirstSupportedType(videoOnlyTypes);
249 MediaSourceUtil.AUDIO_VIDEO_TYPE = getFirstSupportedType(audioVideoTypes);
250 MediaSourceUtil.SEGMENT_INFO = getSegmentInfo();
252 MediaSourceUtil.getSubType = function(mimetype) {
253 var slashIndex = mimetype.indexOf("/");
254 var semicolonIndex = mimetype.indexOf(";");
255 if (slashIndex <= 0) {
256 assert_unreached("Invalid mimetype '" + mimetype + "'");
260 var start = slashIndex + 1;
261 if (semicolonIndex >= 0) {
262 if (semicolonIndex <= start) {
263 assert_unreached("Invalid mimetype '" + mimetype + "'");
267 return mimetype.substr(start, semicolonIndex - start)
270 return mimetype.substr(start);
273 // TODO: Add wrapper object to MediaSourceUtil that binds loaded mediaData to its
274 // associated segmentInfo.
276 function addExtraTestMethods(test)
278 test.failOnEvent = function(object, eventName)
280 object.addEventListener(eventName, test.step_func(function(event)
282 assert_unreached("Unexpected event '" + eventName + "'");
286 test.endOnEvent = function(object, eventName)
288 object.addEventListener(eventName, test.step_func(function(event) { test.done(); }));
291 test.eventExpectations_ = new EventExpectationsManager(test);
292 test.expectEvent = function(object, eventName, description)
294 test.eventExpectations_.expectEvent(object, eventName, description);
297 test.waitForExpectedEvents = function(callback)
299 test.eventExpectations_.waitForExpectedEvents(callback);
302 test.waitForCurrentTimeChange = function(mediaElement, callback)
304 var initialTime = mediaElement.currentTime;
306 var onTimeUpdate = test.step_func(function()
308 if (mediaElement.currentTime != initialTime) {
309 mediaElement.removeEventListener('timeupdate', onTimeUpdate);
314 mediaElement.addEventListener('timeupdate', onTimeUpdate);
317 var oldTestDone = test.done.bind(test);
318 test.done = function()
320 if (test.status == test.PASS) {
321 assert_false(test.eventExpectations_.expectingEvents(), "No pending event expectations.");
327 window['MediaSourceUtil'] = MediaSourceUtil;
328 window['media_test'] = function(testFunction, description, properties)
330 properties = properties || {};
331 return async_test(function(test)
333 addExtraTestMethods(test);
335 }, description, properties);
337 window['mediasource_test'] = function(testFunction, description, properties)
339 return media_test(function(test)
341 var mediaTag = document.createElement("video");
342 document.body.appendChild(mediaTag);
344 // Overload done() so that element added to the document can be removed.
345 test.removeMediaElement_ = true;
346 var oldTestDone = test.done.bind(test);
347 test.done = function()
349 if (test.removeMediaElement_) {
350 document.body.removeChild(mediaTag);
351 test.removeMediaElement_ = false;
356 openMediaSource_(test, mediaTag, function(mediaSource)
358 testFunction(test, mediaTag, mediaSource);
360 }, description, properties);
363 // In addition to test harness's async_test() properties parameter, this
364 // function recognizes the property allow_media_element_error.
365 window['mediasource_testafterdataloaded'] = function(testFunction, description, properties)
367 mediasource_test(function(test, mediaElement, mediaSource)
369 var segmentInfo = MediaSourceUtil.SEGMENT_INFO;
372 assert_unreached("No segment info compatible with this MediaSource implementation.");
376 if (properties == null || properties.allow_media_element_error == null || !properties.allow_media_element_error)
377 test.failOnEvent(mediaElement, 'error');
379 var sourceBuffer = mediaSource.addSourceBuffer(segmentInfo.type);
380 MediaSourceUtil.loadBinaryData(test, segmentInfo.url, function(mediaData)
382 testFunction(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData);
384 }, description, properties);
387 function timeRangesToString(ranges)
390 for (var i = 0; i < ranges.length; ++i) {
391 s += " [" + ranges.start(i).toFixed(3) + ", " + ranges.end(i).toFixed(3) + ")";
396 window['assertBufferedEquals'] = function(obj, expected, description)
398 var actual = timeRangesToString(obj.buffered);
399 assert_equals(actual, expected, description);
402 window['assertSeekableEquals'] = function(obj, expected, description)
404 var actual = timeRangesToString(obj.seekable);
405 assert_equals(actual, expected, description);