mediasource-config-changes.js (4752B)
1 // Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). 2 3 // Extract & return the resolution string from a filename, if any. 4 function resolutionFromFilename(filename) 5 { 6 resolution = filename.replace(/^.*[^0-9]([0-9]+x[0-9]+)[^0-9].*$/, "$1"); 7 if (resolution != filename) { 8 return resolution; 9 } 10 return ""; 11 } 12 13 function appendBuffer(test, sourceBuffer, data) 14 { 15 test.expectEvent(sourceBuffer, "update"); 16 test.expectEvent(sourceBuffer, "updateend"); 17 sourceBuffer.appendBuffer(data); 18 } 19 20 function mediaSourceConfigChangeTest(directory, idA, idB, description) 21 { 22 var manifestFilenameA = directory + "/test-" + idA + "-manifest.json"; 23 var manifestFilenameB = directory + "/test-" + idB + "-manifest.json"; 24 mediasource_test(function(test, mediaElement, mediaSource) 25 { 26 mediaElement.pause(); 27 mediaElement.addEventListener('error', test.unreached_func("Unexpected event 'error'")); 28 var expectResizeEvents = resolutionFromFilename(manifestFilenameA) != resolutionFromFilename(manifestFilenameB); 29 var expectedResizeEventCount = 0; 30 31 MediaSourceUtil.fetchManifestAndData(test, manifestFilenameA, function(typeA, dataA) 32 { 33 MediaSourceUtil.fetchManifestAndData(test, manifestFilenameB, function(typeB, dataB) 34 { 35 assert_equals(typeA, typeB, "Media format types match"); 36 37 var sourceBuffer = mediaSource.addSourceBuffer(typeA); 38 39 appendBuffer(test, sourceBuffer, dataA); 40 ++expectedResizeEventCount; 41 42 test.waitForExpectedEvents(function() 43 { 44 // Add the second buffer starting at 0.5 second. 45 sourceBuffer.timestampOffset = 0.5; 46 appendBuffer(test, sourceBuffer, dataB); 47 ++expectedResizeEventCount; 48 }); 49 50 test.waitForExpectedEvents(function() 51 { 52 // Add the first buffer starting at 1 second. 53 sourceBuffer.timestampOffset = 1; 54 appendBuffer(test, sourceBuffer, dataA); 55 ++expectedResizeEventCount; 56 }); 57 58 test.waitForExpectedEvents(function() 59 { 60 // Add the second buffer starting at 1.5 second. 61 sourceBuffer.timestampOffset = 1.5; 62 appendBuffer(test, sourceBuffer, dataB); 63 ++expectedResizeEventCount; 64 }); 65 66 test.waitForExpectedEvents(function() 67 { 68 assert_false(sourceBuffer.updating, "updating"); 69 70 // Truncate the presentation to a duration of 2 seconds. 71 // First, explicitly remove the media beyond 2 seconds. 72 sourceBuffer.remove(2, Infinity); 73 74 assert_true(sourceBuffer.updating, "sourceBuffer.updating during range removal"); 75 test.expectEvent(sourceBuffer, 'updatestart', 'sourceBuffer'); 76 test.expectEvent(sourceBuffer, 'update', 'sourceBuffer'); 77 test.expectEvent(sourceBuffer, 'updateend', 'sourceBuffer'); 78 }); 79 80 test.waitForExpectedEvents(function() 81 { 82 assert_false(sourceBuffer.updating, "sourceBuffer.updating prior to duration reduction"); 83 assert_greater_than(mediaSource.duration, 2, "duration"); 84 85 // Complete the truncation of presentation to 2 second 86 // duration. 87 mediaSource.duration = 2; 88 assert_false(sourceBuffer.updating, "sourceBuffer.updating synchronously after duration reduction"); 89 90 test.expectEvent(mediaElement, "durationchange"); 91 }); 92 93 test.waitForExpectedEvents(function() 94 { 95 assert_false(sourceBuffer.updating, "updating"); 96 97 mediaSource.endOfStream(); 98 99 assert_false(sourceBuffer.updating, "updating"); 100 101 if (expectResizeEvents) { 102 for (var i = 0; i < expectedResizeEventCount; ++i) { 103 test.expectEvent(mediaElement, "resize"); 104 } 105 } 106 test.expectEvent(mediaElement, "ended"); 107 mediaElement.play(); 108 }); 109 110 test.waitForExpectedEvents(function() { 111 test.done(); 112 }); 113 }); 114 }); 115 }, description); 116 };