Calculate currentTime after seeks discontinuities taking into account.
Add a new duration method that accepts a start and end index. Add "use strict" to the top of the scope for strict mode goodness.
Showing
1 changed file
with
42 additions
and
17 deletions
... | @@ -7,6 +7,7 @@ | ... | @@ -7,6 +7,7 @@ |
7 | */ | 7 | */ |
8 | 8 | ||
9 | (function(window, videojs, document, undefined) { | 9 | (function(window, videojs, document, undefined) { |
10 | 'use strict'; | ||
10 | 11 | ||
11 | var | 12 | var |
12 | 13 | ||
... | @@ -145,22 +146,42 @@ var | ... | @@ -145,22 +146,42 @@ var |
145 | }, | 146 | }, |
146 | 147 | ||
147 | /** | 148 | /** |
149 | * Calculate the duration of a playlist from a given start index to a given | ||
150 | * end index. | ||
151 | * @param playlist {object} a media playlist object | ||
152 | * @param startIndex {number} an inclusive lower boundary for the playlist. | ||
153 | * Defaults to 0. | ||
154 | * @param endIndex {number} an exclusive upper boundary for the playlist. | ||
155 | * Defaults to playlist length. | ||
156 | * @return {number} the duration between the start index and end index. | ||
157 | */ | ||
158 | duration = function(playlist, startIndex, endIndex) { | ||
159 | var dur = 0, | ||
160 | segment, | ||
161 | i; | ||
162 | |||
163 | startIndex = startIndex || 0; | ||
164 | endIndex = endIndex || (playlist.segments || []).length; | ||
165 | i = endIndex - 1; | ||
166 | |||
167 | for (; i >= startIndex; i--) { | ||
168 | segment = playlist.segments[i]; | ||
169 | dur += segment.duration || playlist.targetDuration || 0; | ||
170 | } | ||
171 | |||
172 | return dur; | ||
173 | }, | ||
174 | |||
175 | /** | ||
148 | * Calculate the total duration for a playlist based on segment metadata. | 176 | * Calculate the total duration for a playlist based on segment metadata. |
149 | * @param playlist {object} a media playlist object | 177 | * @param playlist {object} a media playlist object |
150 | * @return {number} the currently known duration, in seconds | 178 | * @return {number} the currently known duration, in seconds |
151 | */ | 179 | */ |
152 | totalDuration = function(playlist) { | 180 | totalDuration = function(playlist) { |
153 | var | ||
154 | duration = 0, | ||
155 | segment, | ||
156 | i; | ||
157 | |||
158 | if (!playlist) { | 181 | if (!playlist) { |
159 | return 0; | 182 | return 0; |
160 | } | 183 | } |
161 | 184 | ||
162 | i = (playlist.segments || []).length; | ||
163 | |||
164 | // if present, use the duration specified in the playlist | 185 | // if present, use the duration specified in the playlist |
165 | if (playlist.totalDuration) { | 186 | if (playlist.totalDuration) { |
166 | return playlist.totalDuration; | 187 | return playlist.totalDuration; |
... | @@ -171,11 +192,7 @@ var | ... | @@ -171,11 +192,7 @@ var |
171 | return window.Infinity; | 192 | return window.Infinity; |
172 | } | 193 | } |
173 | 194 | ||
174 | while (i--) { | 195 | return duration(playlist); |
175 | segment = playlist.segments[i]; | ||
176 | duration += segment.duration || playlist.targetDuration || 0; | ||
177 | } | ||
178 | return duration; | ||
179 | }, | 196 | }, |
180 | 197 | ||
181 | resolveUrl, | 198 | resolveUrl, |
... | @@ -197,6 +214,7 @@ var | ... | @@ -197,6 +214,7 @@ var |
197 | } | 214 | } |
198 | return this.el().vjs_getProperty('currentTime'); | 215 | return this.el().vjs_getProperty('currentTime'); |
199 | }; | 216 | }; |
217 | |||
200 | player.hls.setCurrentTime = function(currentTime) { | 218 | player.hls.setCurrentTime = function(currentTime) { |
201 | if (!(this.playlists && this.playlists.media())) { | 219 | if (!(this.playlists && this.playlists.media())) { |
202 | // return immediately if the metadata is not ready yet | 220 | // return immediately if the metadata is not ready yet |
... | @@ -404,16 +422,23 @@ var | ... | @@ -404,16 +422,23 @@ var |
404 | // playback time | 422 | // playback time |
405 | if (typeof offset === 'number') { | 423 | if (typeof offset === 'number') { |
406 | (function() { | 424 | (function() { |
407 | var tag = segmentParser.getTags()[0]; | 425 | var tag = segmentParser.getTags()[0], |
426 | ptsnaught = tag.pts, | ||
427 | ptsdelta, | ||
428 | playlist = player.hls.playlists.media(), | ||
429 | segmentOffset, | ||
430 | ptsTime; | ||
431 | |||
432 | segmentOffset = duration(playlist, 0, player.hls.mediaIndex) * 1000; | ||
408 | 433 | ||
409 | for (; tag && tag.pts < offset; tag = segmentParser.getTags()[0]) { | 434 | ptsTime = offset - segmentOffset + ptsnaught; |
435 | |||
436 | for (; tag && tag.pts < ptsTime; tag = segmentParser.getTags()[0]) { | ||
410 | segmentParser.getNextTag(); | 437 | segmentParser.getNextTag(); |
411 | } | 438 | } |
412 | 439 | ||
413 | // tell the SWF where we will be seeking to | 440 | // tell the SWF where we will be seeking to |
414 | if (tag) { | 441 | player.hls.el().vjs_setProperty('currentTime', (tag.pts - ptsnaught + segmentOffset) * 0.001); |
415 | player.hls.el().vjs_setProperty('currentTime', tag.pts * 0.001); | ||
416 | } | ||
417 | 442 | ||
418 | lastSeekedTime = null; | 443 | lastSeekedTime = null; |
419 | })(); | 444 | })(); | ... | ... |
-
Please register or sign in to post a comment