fa0dd47c by David LaPalomento

Consolidate seeking loops

Use a single loop through the FLV tags to determine the right tag to begin buffering at. Fix up some whitespace.
1 parent 8f63b1a9
...@@ -110,27 +110,22 @@ var ...@@ -110,27 +110,22 @@ var
110 return 1; // HAVE_METADATA 110 return 1; // HAVE_METADATA
111 }; 111 };
112 112
113 player.on('ready', function() {
114 tech = player.el().querySelector('.vjs-tech');
115 tech.vjs_setProperty('playerId', player.P);
116 });
117
118 player.on('seeking', function() { 113 player.on('seeking', function() {
119 var seekValue = tech.vjs_getProperty('lastSeekedTime'); 114 var seekValue = tech.vjs_getProperty('lastSeekedTime');
120 player.hls.mediaIndex = player.hls.getSegmentIndexByTime(seekValue); 115 player.hls.mediaIndex = player.hls.getSegmentIndexByTime(seekValue);
121 fillBuffer(seekValue*1000); 116 fillBuffer(seekValue * 1000);
122 }); 117 });
123 118
124 player.on('ended', function() { 119 player.on('waiting', function() {
125 //console.log('ended received'); 120 if (player.hls.mediaIndex === player.hls.media.segments.length) {
121 player.pause();
122 player.trigger('ended');
123 }
126 }); 124 });
127 125
128 player.hls.isLastSegment = function() { 126 player.hls.getSegmentByTime = function(time) {
129 return player.hls.mediaIndex === player.hls.selectPlaylist().segments.length; 127 var index, currentSegment;
130 };
131 128
132 player.hls.getSegmentIndexByTime = function(time) {
133 var index, counter, currentSegmentRange, timeRanges;
134 if (player.hls.media && player.hls.media.segments) { 129 if (player.hls.media && player.hls.media.segments) {
135 130
136 timeRanges = []; 131 timeRanges = [];
...@@ -191,11 +186,6 @@ var ...@@ -191,11 +186,6 @@ var
191 } 186 }
192 } 187 }
193 188
194 // console.log('bandwidth:',
195 // player.hls.bandwidth,
196 // 'variant:',
197 // (bestVariant || sortedPlaylists[0]).attributes.BANDWIDTH);
198
199 // if no acceptable variant was found, fall back on the lowest 189 // if no acceptable variant was found, fall back on the lowest
200 // bitrate playlist 190 // bitrate playlist
201 return bestVariant || sortedPlaylists[0]; 191 return bestVariant || sortedPlaylists[0];
...@@ -258,11 +248,7 @@ var ...@@ -258,11 +248,7 @@ var
258 // update the duration 248 // update the duration
259 player.duration(parser.manifest.totalDuration); 249 player.duration(parser.manifest.totalDuration);
260 // Notify the flash layer 250 // Notify the flash layer
261 try {
262 player.el().getElementsByClassName('vjs-tech')[0].vjs_setProperty('duration',parser.manifest.totalDuration); 251 player.el().getElementsByClassName('vjs-tech')[0].vjs_setProperty('duration',parser.manifest.totalDuration);
263 } catch (err) {
264
265 }
266 } 252 }
267 player.trigger('loadedmanifest'); 253 player.trigger('loadedmanifest');
268 player.trigger('loadedmetadata'); 254 player.trigger('loadedmetadata');
...@@ -297,16 +283,8 @@ var ...@@ -297,16 +283,8 @@ var
297 bufferedTime = 0, 283 bufferedTime = 0,
298 segment = player.hls.media.segments[player.hls.mediaIndex], 284 segment = player.hls.media.segments[player.hls.mediaIndex],
299 segmentUri, 285 segmentUri,
300 desiredMillisecond,
301 startTime, 286 startTime,
302 targetTag, 287 tagIndex;
303 tagIndex,
304 bleedIndex = 0;
305
306 if(millisecond)
307 {
308 desiredMillisecond = millisecond;
309 }
310 288
311 // if there is a request already in flight, do nothing 289 // if there is a request already in flight, do nothing
312 if (segmentXhr) { 290 if (segmentXhr) {
...@@ -346,25 +324,17 @@ var ...@@ -346,25 +324,17 @@ var
346 // transmux the segment data from MP2T to FLV 324 // transmux the segment data from MP2T to FLV
347 segmentParser.parseSegmentBinaryData(new Uint8Array(segmentXhr.response)); 325 segmentParser.parseSegmentBinaryData(new Uint8Array(segmentXhr.response));
348 326
349 if(desiredMillisecond!==null && desiredMillisecond>0) 327 // handle intra-segment seeking, if requested
350 { 328 if (millisecond !== undefined) {
351 for(tagIndex = 0; tagIndex < segmentParser.getTags().length-1; tagIndex++) { 329 for (tagIndex = 0; tagIndex < segmentParser.getTags().length; tagIndex++) {
352 if(segmentParser.getTags()[tagIndex].pts <= desiredMillisecond && segmentParser.getTags()[tagIndex+1].pts > desiredMillisecond) { 330 if (segmentParser.getTags()[tagIndex].pts > millisecond) {
353 targetTag = tagIndex; 331 continue;
354 }
355 }
356
357 try {
358 tech.vjs_setProperty('lastSeekedTime', segmentParser.getTags()[targetTag].pts/1000);
359 } catch(err) {
360
361 } 332 }
362 333 // we're seeking past this tag, so ignore it
363 while(bleedIndex < targetTag) {
364 segmentParser.getNextTag(); 334 segmentParser.getNextTag();
365 bleedIndex++;
366 } 335 }
367 336
337 tech.vjs_setProperty('lastSeekedTime', segmentParser.getTags()[tagIndex].pts / 1000);
368 } 338 }
369 339
370 while (segmentParser.tagsAvailable()) { 340 while (segmentParser.tagsAvailable()) {
......