4 <script src=
"/w3c/resources/testharness.js"></script>
5 <script src=
"/w3c/resources/testharnessreport.js"></script>
6 <script src=
"mediasource-util.js"></script>
7 <link rel='stylesheet' href='/w3c/resources/testharness.css'
>
12 function mediasource_truncated_duration_seek_test(testFunction
, description
, options
)
14 return mediasource_testafterdataloaded(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
, mediaData
)
16 assert_greater_than(segmentInfo
.duration
, 2, 'Sufficient test media duration');
18 var fullDuration
= segmentInfo
.duration
;
19 var seekTo
= fullDuration
/ 2.0;
20 var truncatedDuration
= seekTo
/ 2.0;
24 // Append all the segments
25 test
.expectEvent(sourceBuffer
, 'updateend', 'sourceBuffer');
26 test
.expectEvent(mediaElement
, 'playing', 'Playing triggered');
27 sourceBuffer
.appendBuffer(mediaData
);
29 test
.waitForExpectedEvents(function()
31 assert_equals(mediaElement
.duration
, fullDuration
, 'mediaElement fullDuration');
32 assert_equals(mediaSource
.duration
, fullDuration
, 'mediaSource fullDuration');
34 test
.expectEvent(mediaElement
, 'seeking', 'seeking to seekTo');
35 test
.expectEvent(mediaElement
, 'timeupdate', 'timeupdate while seeking to seekTo');
36 test
.expectEvent(mediaElement
, 'seeked', 'seeked to seekTo');
37 mediaElement
.currentTime
= seekTo
;
38 assert_true(mediaElement
.seeking
, 'mediaElement.seeking (to seekTo)');
41 test
.waitForExpectedEvents(function()
43 assert_greater_than_equal(mediaElement
.currentTime
, seekTo
, 'Playback time has reached seekTo');
44 assert_equals(mediaElement
.duration
, fullDuration
, 'mediaElement fullDuration after seekTo');
45 assert_equals(mediaSource
.duration
, fullDuration
, 'mediaSource fullDuration after seekTo');
46 assert_false(mediaElement
.seeking
, 'mediaElement.seeking after seeked to seekTo');
48 test
.expectEvent(mediaElement
, 'seeking', 'Seeking to truncated duration');
49 mediaSource
.duration
= truncatedDuration
;
50 assert_true(mediaElement
.seeking
, 'Seeking after setting truncatedDuration');
53 test
.waitForExpectedEvents(function()
55 assert_equals(mediaElement
.currentTime
, truncatedDuration
,
56 'Playback time is truncatedDuration while seeking');
57 assert_true(mediaElement
.seeking
, 'mediaElement.seeking while seeking to truncatedDuration');
58 assert_equals(mediaElement
.duration
, truncatedDuration
,
59 'mediaElement truncatedDuration during seek to it');
60 assert_equals(mediaSource
.duration
, truncatedDuration
,
61 'mediaSource truncatedDuration during seek to it');
63 // FIXME: Confirm 'waiting' and then 'stalled' fire here. See http://crbug.com/266592.
65 testFunction(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
, mediaData
,
68 }, description
, options
);
71 mediasource_truncated_duration_seek_test(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
,
72 mediaData
, truncatedDuration
)
74 // Tests that duration truncation below current playback position
75 // starts seek to new duration.
77 }, 'Test seek starts on duration truncation below currentTime');
79 mediasource_truncated_duration_seek_test(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
,
80 mediaData
, truncatedDuration
)
82 // The duration has been truncated at this point, and there is an
83 // outstanding seek pending.
84 test
.expectEvent(sourceBuffer
, 'updateend', 'updateend after appending more data');
86 // FIXME: Confirm 'playing' fires here. See http://crbug.com/266592.
88 test
.expectEvent(mediaElement
, 'timeupdate', 'timeupdate while finishing seek to truncatedDuration');
89 test
.expectEvent(mediaElement
, 'seeked', 'seeked to truncatedDuration');
91 // Allow seek to complete by appending more data beginning at the
92 // truncated duration timestamp.
93 sourceBuffer
.timestampOffset
= truncatedDuration
;
94 sourceBuffer
.appendBuffer(mediaData
);
96 test
.waitForExpectedEvents(function()
98 assert_greater_than_equal(mediaElement
.currentTime
, truncatedDuration
,
99 'Playback time has reached truncatedDuration');
100 assert_approx_equals(mediaElement
.duration
, truncatedDuration
+ segmentInfo
.duration
, 0.05,
101 'mediaElement duration increased by new append');
102 assert_equals(mediaSource
.duration
, mediaElement
.duration
,
103 'mediaSource duration increased by new append');
104 assert_false(mediaElement
.seeking
, 'mediaElement.seeking after seeked to truncatedDuration');
108 }, 'Test appendBuffer completes previous seek to truncated duration');
110 mediasource_truncated_duration_seek_test(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
,
111 mediaData
, truncatedDuration
)
113 // The duration has been truncated at this point, and there is an
114 // outstanding seek pending.
115 test
.expectEvent(mediaSource
, 'sourceended', 'endOfStream acknowledged');
117 // FIXME: Investigate if 'playing' should fire here. See http://crbug.com/266592.
119 test
.expectEvent(mediaElement
, 'timeupdate', 'timeupdate while finishing seek to truncatedDuration');
120 test
.expectEvent(mediaElement
, 'seeked', 'seeked to truncatedDuration');
122 // Call endOfStream() to complete the pending seek.
123 mediaSource
.endOfStream();
125 test
.waitForExpectedEvents(function()
127 assert_equals(mediaElement
.currentTime
, truncatedDuration
,
128 'Playback time has reached truncatedDuration');
129 assert_equals(mediaElement
.duration
, truncatedDuration
,
130 'mediaElement truncatedDuration after seek to it');
131 assert_equals(mediaSource
.duration
, truncatedDuration
,
132 'mediaSource truncatedDuration after seek to it');
133 assert_false(mediaElement
.seeking
, 'mediaElement.seeking after seeked to truncatedDuration');
137 }, 'Test endOfStream completes previous seek to truncated duration');
139 mediasource_testafterdataloaded(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
, mediaData
)
141 assert_greater_than(segmentInfo
.duration
, 2, 'Sufficient test media duration');
143 var fullDuration
= segmentInfo
.duration
;
144 var newDuration
= 0.5;
146 var durationchangeEventCounter
= 0;
147 var expectedDurationChangeEventCount
= 1;
148 var durationchangeEventHandler
= test
.step_func(function(event
)
150 assert_equals(mediaElement
.duration
, newDuration
, 'mediaElement newDuration');
151 assert_equals(mediaSource
.duration
, newDuration
, 'mediaSource newDuration');
152 durationchangeEventCounter
++;
157 // Append all the segments
158 test
.expectEvent(sourceBuffer
, 'updateend', 'sourceBuffer');
159 test
.expectEvent(mediaElement
, 'playing', 'Playing triggered');
160 sourceBuffer
.appendBuffer(mediaData
);
162 test
.waitForExpectedEvents(function()
164 assert_equals(mediaElement
.duration
, fullDuration
, 'mediaElement fullDuration');
165 assert_equals(mediaSource
.duration
, fullDuration
, 'mediaSource fullDuration');
166 assert_less_than(mediaElement
.currentTime
, newDuration
/ 2, 'mediaElement currentTime');
168 // Media load also fires 'durationchange' event, so only start counting them now.
169 mediaElement
.addEventListener('durationchange', durationchangeEventHandler
);
171 // Truncate duration. This should result in one 'durationchange' fired.
172 mediaSource
.duration
= newDuration
;
174 assert_true(sourceBuffer
.updating
, "sourceBuffer.updating");
175 test
.expectEvent(sourceBuffer
, "updateend");
178 test
.waitForExpectedEvents(function()
180 // Set duration again to make sure it does not trigger another 'durationchange' event.
181 mediaSource
.duration
= newDuration
;
183 assert_false(sourceBuffer
.updating
, "sourceBuffer.updating");
185 // Mark endOfStream so that playback can reach 'ended' at the new duration.
186 test
.expectEvent(mediaSource
, 'sourceended', 'endOfStream acknowledged');
187 mediaSource
.endOfStream();
189 // endOfStream can change duration downwards slightly.
190 // Allow for one more 'durationchange' event only in this case.
191 var currentDuration
= mediaSource
.duration
;
192 if (currentDuration
!= newDuration
) {
193 assert_true(currentDuration
> 0 && currentDuration
< newDuration
, 'adjusted duration');
194 newDuration
= currentDuration
;
195 ++expectedDurationChangeEventCount
;
198 // Allow media to play to end while counting 'durationchange' events.
199 test
.expectEvent(mediaElement
, 'ended', 'Playback ended');
200 test
.waitForExpectedEvents(function()
202 mediaElement
.removeEventListener('durationchange', durationchangeEventHandler
);
203 assert_equals(durationchangeEventCounter
, expectedDurationChangeEventCount
, 'durationchanges');
207 }, 'Test setting same duration multiple times does not fire duplicate durationchange', {timeout
: 2500});