Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / media / media-source / mediasource-util.js
blob4b18a74f8554c1185c1dd2357d730e28c0fb14bf
1 (function(window) {
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
5     // timeout the test.
6     setup({ explicit_timeout: 120000 });
8     var SEGMENT_INFO_LIST = [
9         {
10             url: '/media/resources/media-source/webm/test.webm',
11             type: 'video/webm; codecs="vp8, vorbis"',
12             durationInInitSegment: 6.042,
13             duration: 6.042,
14             // Supports jagged-ended stream end timestamps with some less than duration:
15             bufferedRangeEndBeforeEndOfStream: 6.040,
16             init: { offset: 0, size: 4357 },
17             media: [
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 },
34             ],
35         },
36         {
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,
42             duration: 6.0424,
43             bufferedRangeEndBeforeEndOfStream: 6.0368,
44             init: { offset: 0, size: 1178 },
45             media: [
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 },
59             ],
60         }
61     ];
62     EventExpectationsManager = function(test)
63     {
64         this.test_ = test;
65         this.eventTargetList_ = [];
66         this.waitCallbacks_ = [];
67     };
69     EventExpectationsManager.prototype.expectEvent = function(object, eventName, description)
70     {
71         var eventInfo = { 'target': object, 'type': eventName, 'description': description};
72         var expectations = this.getExpectations_(object);
73         expectations.push(eventInfo);
75         var t = this;
76         var waitHandler = this.test_.step_func(this.handleWaitCallback_.bind(this));
77         var eventHandler = this.test_.step_func(function(event)
78         {
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);
88         });
89         object.addEventListener(eventName, eventHandler);
90     };
92     EventExpectationsManager.prototype.waitForExpectedEvents = function(callback)
93     {
94         this.waitCallbacks_.push(callback);
95         setTimeout(this.test_.step_func(this.handleWaitCallback_.bind(this)), 0);
96     };
98     EventExpectationsManager.prototype.expectingEvents = function()
99     {
100         for (var i = 0; i < this.eventTargetList_.length; ++i) {
101             if (this.eventTargetList_[i].expectations.length > 0) {
102                 return true;
103             }
104         }
105         return false;
106     }
108     EventExpectationsManager.prototype.handleWaitCallback_ = function()
109     {
110         if (this.waitCallbacks_.length == 0 || this.expectingEvents())
111             return;
112         var callback = this.waitCallbacks_.shift(1);
113         callback();
114     };
116     EventExpectationsManager.prototype.getExpectations_ = function(target)
117     {
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;
122             }
123         }
124         var expectations = [];
125         this.eventTargetList_.push({ 'target': target, 'expectations': expectations });
126         return expectations;
127     };
129     function loadData_(test, url, callback, isBinary)
130     {
131         var request = new XMLHttpRequest();
132         request.open("GET", url, true);
133         if (isBinary) {
134             request.responseType = 'arraybuffer';
135         }
136         request.onload = test.step_func(function(event)
137         {
138             if (request.status != 200) {
139                 assert_unreached("Unexpected status code : " + request.status);
140                 return;
141             }
142             var response = request.response;
143             if (isBinary) {
144                 response = new Uint8Array(response);
145             }
146             callback(response);
147         });
148         request.onerror = test.step_func(function(event)
149         {
150             assert_unreached("Unexpected error");
151         });
152         request.send();
153     }
155     function openMediaSource_(test, mediaTag, callback)
156     {
157         var mediaSource = new MediaSource();
158         var mediaSourceURL = URL.createObjectURL(mediaSource);
160         var eventHandler = test.step_func(onSourceOpen);
161         function onSourceOpen(event)
162         {
163             mediaSource.removeEventListener('sourceopen', eventHandler);
164             URL.revokeObjectURL(mediaSourceURL);
165             callback(mediaSource);
166         }
168         mediaSource.addEventListener('sourceopen', eventHandler);
169         mediaTag.src = mediaSourceURL;
170     }
172     var MediaSourceUtil = {};
174     MediaSourceUtil.loadTextData = function(test, url, callback)
175     {
176         loadData_(test, url, callback, false);
177     };
179     MediaSourceUtil.loadBinaryData = function(test, url, callback)
180     {
181         loadData_(test, url, callback, true);
182     };
184     MediaSourceUtil.fetchManifestAndData = function(test, manifestFilename, callback)
185     {
186         var baseURL = '/media/resources/media-source/';
187         var manifestURL = baseURL + manifestFilename;
188         MediaSourceUtil.loadTextData(test, manifestURL, function(manifestText)
189         {
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)
196             {
197                 callback(manifest.type, mediaData);
198             });
199         });
200     };
202     MediaSourceUtil.extractSegmentData = function(mediaData, info)
203     {
204         var start = info.offset;
205         var end = start + info.size;
206         return mediaData.subarray(start, end);
207     }
209     MediaSourceUtil.getMediaDataForPlaybackTime = function(mediaData, segmentInfo, playbackTimeToAdd)
210     {
211         assert_less_than_equal(playbackTimeToAdd, segmentInfo.duration);
212         var mediaInfo = segmentInfo.media;
213         var start = mediaInfo[0].offset;
214         var numBytes = 0;
215         var segmentIndex = 0;
216         while (segmentIndex < mediaInfo.length && mediaInfo[segmentIndex].timecode <= playbackTimeToAdd)
217         {
218           numBytes += mediaInfo[segmentIndex].size;
219           ++segmentIndex;
220         }
221         return mediaData.subarray(start, numBytes + start);
222     }
224     function getFirstSupportedType(typeList)
225     {
226         for (var i = 0; i < typeList.length; ++i) {
227             if (MediaSource.isTypeSupported(typeList[i]))
228                 return typeList[i];
229         }
230         return "";
231     }
233     function getSegmentInfo()
234     {
235         for (var i = 0; i < SEGMENT_INFO_LIST.length; ++i) {
236             var segmentInfo = SEGMENT_INFO_LIST[i];
237             if (MediaSource.isTypeSupported(segmentInfo.type)) {
238                 return segmentInfo;
239             }
240         }
241         return null;
242     }
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 + "'");
257             return;
258         }
260         var start = slashIndex + 1;
261         if (semicolonIndex >= 0) {
262             if (semicolonIndex <= start) {
263                 assert_unreached("Invalid mimetype '" + mimetype + "'");
264                 return;
265             }
267             return mimetype.substr(start, semicolonIndex - start)
268         }
270         return mimetype.substr(start);
271     };
273     // TODO: Add wrapper object to MediaSourceUtil that binds loaded mediaData to its
274     // associated segmentInfo.
276     function addExtraTestMethods(test)
277     {
278         test.failOnEvent = function(object, eventName)
279         {
280             object.addEventListener(eventName, test.step_func(function(event)
281             {
282                 assert_unreached("Unexpected event '" + eventName + "'");
283             }));
284         };
286         test.endOnEvent = function(object, eventName)
287         {
288             object.addEventListener(eventName, test.step_func(function(event) { test.done(); }));
289         };
291         test.eventExpectations_ = new EventExpectationsManager(test);
292         test.expectEvent = function(object, eventName, description)
293         {
294             test.eventExpectations_.expectEvent(object, eventName, description);
295         };
297         test.waitForExpectedEvents = function(callback)
298         {
299             test.eventExpectations_.waitForExpectedEvents(callback);
300         };
302         test.waitForCurrentTimeChange = function(mediaElement, callback)
303         {
304             var initialTime = mediaElement.currentTime;
306             var onTimeUpdate = test.step_func(function()
307             {
308                 if (mediaElement.currentTime != initialTime) {
309                     mediaElement.removeEventListener('timeupdate', onTimeUpdate);
310                     callback();
311                 }
312             });
314             mediaElement.addEventListener('timeupdate', onTimeUpdate);
315         }
317         var oldTestDone = test.done.bind(test);
318         test.done = function()
319         {
320             if (test.status == test.PASS) {
321                 assert_false(test.eventExpectations_.expectingEvents(), "No pending event expectations.");
322             }
323             oldTestDone();
324         };
325     };
327     window['MediaSourceUtil'] = MediaSourceUtil;
328     window['media_test'] = function(testFunction, description, properties)
329     {
330         properties = properties || {};
331         return async_test(function(test)
332         {
333             addExtraTestMethods(test);
334             testFunction(test);
335         }, description, properties);
336     };
337     window['mediasource_test'] = function(testFunction, description, properties)
338     {
339         return media_test(function(test)
340         {
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()
348             {
349                 if (test.removeMediaElement_) {
350                     document.body.removeChild(mediaTag);
351                     test.removeMediaElement_ = false;
352                 }
353                 oldTestDone();
354             };
356             openMediaSource_(test, mediaTag, function(mediaSource)
357             {
358                 testFunction(test, mediaTag, mediaSource);
359             });
360         }, description, properties);
361     };
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)
366     {
367         mediasource_test(function(test, mediaElement, mediaSource)
368         {
369             var segmentInfo = MediaSourceUtil.SEGMENT_INFO;
371             if (!segmentInfo) {
372                 assert_unreached("No segment info compatible with this MediaSource implementation.");
373                 return;
374             }
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)
381             {
382                 testFunction(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData);
383             });
384         }, description, properties);
385     };
387     function timeRangesToString(ranges)
388     {
389         var s = "{";
390         for (var i = 0; i < ranges.length; ++i) {
391             s += " [" + ranges.start(i).toFixed(3) + ", " + ranges.end(i).toFixed(3) + ")";
392         }
393         return s + " }";
394     }
396     window['assertBufferedEquals'] = function(obj, expected, description)
397     {
398         var actual = timeRangesToString(obj.buffered);
399         assert_equals(actual, expected, description);
400     };
402     window['assertSeekableEquals'] = function(obj, expected, description)
403     {
404         var actual = timeRangesToString(obj.seekable);
405         assert_equals(actual, expected, description);
406     };
408 })(window);