467fa2fe by Tom Johnson

update intra segment seek to use pts for millisecond level accuracy

1 parent 3c5fb8b7
...@@ -296,8 +296,8 @@ hls.FlvTag = function(type, extraData) { ...@@ -296,8 +296,8 @@ hls.FlvTag = function(type, extraData) {
296 296
297 // trim down the byte buffer to what is actually being used 297 // trim down the byte buffer to what is actually being used
298 this.bytes = this.bytes.subarray(0, this.length); 298 this.bytes = this.bytes.subarray(0, this.length);
299 this.frameTime = hls.FlvTag.frameTime(this.bytes);
299 console.assert(this.bytes.byteLength === this.length); 300 console.assert(this.bytes.byteLength === this.length);
300
301 return this; 301 return this;
302 }; 302 };
303 }; 303 };
......
...@@ -80,6 +80,7 @@ var ...@@ -80,6 +80,7 @@ var
80 mediaSource = new videojs.MediaSource(), 80 mediaSource = new videojs.MediaSource(),
81 segmentParser = new videojs.hls.SegmentParser(), 81 segmentParser = new videojs.hls.SegmentParser(),
82 player = this, 82 player = this,
83 tech,
83 extname, 84 extname,
84 srcUrl, 85 srcUrl,
85 86
...@@ -109,33 +110,27 @@ var ...@@ -109,33 +110,27 @@ var
109 return 1; // HAVE_METADATA 110 return 1; // HAVE_METADATA
110 }; 111 };
111 112
112 player.currentTime = function(value) { 113 player.on('ready', function() {
113 var returnValue; 114 player.muted(true);
115 tech = player.el().querySelector('.vjs-tech');
116 tech.vjs_setProperty('playerId', player.P);
117 });
114 118
115 if(value) { 119 player.on('seeking', function() {
116 try { 120 var seekValue = tech.vjs_getProperty('lastSeekedTime');
117 player.el().getElementsByClassName('vjs-tech')[0].vjs_setProperty('currentTime', value); 121 player.hls.mediaIndex = player.hls.getSegmentIndexByTime(seekValue);
118 player.el().getElementsByClassName('vjs-tech')[0].vjs_setProperty('appendBytesAction', 'resetSeek'); 122 fillBuffer(seekValue*1000);
119 player.el().getElementsByClassName('vjs-tech')[0].vjs_setProperty('appendBytesAction', 'resetBegin'); 123 });
120 } catch(err) {
121 124
125 player.on('waiting', function() {
126 if(player.currentTime() >= player.duration() && player.hls.isLastSegment()) {
127 player.pause();
128 player.trigger('ended');
122 } 129 }
123 player.hls.sourceBuffer.appendBuffer(segmentParser.getFlvHeader()); 130 });
124 player.hls.mediaIndex = player.hls.selectSegmentIndexByTime(value); 131
125 fillBuffer(value-player.hls.getSegmentByTime(value).timeRange.start); 132 player.hls.isLastSegment = function() {
126 } else { 133 return player.hls.mediaIndex === player.hls.selectPlaylist().segments.length;
127 try {
128 returnValue = player.el().getElementsByClassName('vjs-tech')[0].vjs_getProperty('currentTime');
129 if(returnValue > player.duration()) {
130 returnValue = player.duration();
131 } else if (returnValue < 0) {
132 returnValue = 0;
133 }
134 return returnValue;
135 } catch(err) {
136 return 0;
137 }
138 }
139 }; 134 };
140 135
141 player.hls.getSegmentByTime = function(time) { 136 player.hls.getSegmentByTime = function(time) {
...@@ -151,9 +146,8 @@ var ...@@ -151,9 +146,8 @@ var
151 } 146 }
152 }; 147 };
153 148
154 player.hls.selectSegmentIndexByTime = function(time) { 149 player.hls.getSegmentIndexByTime = function(time) {
155 var index, currentSegment; 150 var index, currentSegment;
156
157 if (player.hls.media && player.hls.media.segments) { 151 if (player.hls.media && player.hls.media.segments) {
158 for (index = 0; index < player.hls.media.segments.length; index++) { 152 for (index = 0; index < player.hls.media.segments.length; index++) {
159 currentSegment = player.hls.media.segments[index]; 153 currentSegment = player.hls.media.segments[index];
...@@ -299,18 +293,21 @@ var ...@@ -299,18 +293,21 @@ var
299 * Determines whether there is enough video data currently in the buffer 293 * Determines whether there is enough video data currently in the buffer
300 * and downloads a new segment if the buffered time is less than the goal. 294 * and downloads a new segment if the buffered time is less than the goal.
301 */ 295 */
302 fillBuffer = function(intrasegmentSecond) { 296 fillBuffer = function(millisecond) {
303 var 297 var
304 buffered = player.buffered(), 298 buffered = player.buffered(),
305 bufferedTime = 0, 299 bufferedTime = 0,
306 segment = player.hls.media.segments[player.hls.mediaIndex], 300 segment = player.hls.media.segments[player.hls.mediaIndex],
307 segmentUri, 301 segmentUri,
308 gotoSecond, 302 desiredMillisecond,
309 startTime; 303 startTime,
304 targetTag,
305 tagIndex,
306 bleedIndex = 0;
310 307
311 if(intrasegmentSecond) 308 if(millisecond)
312 { 309 {
313 gotoSecond = intrasegmentSecond; 310 desiredMillisecond = millisecond;
314 } 311 }
315 312
316 // if there is a request already in flight, do nothing 313 // if there is a request already in flight, do nothing
...@@ -351,19 +348,16 @@ var ...@@ -351,19 +348,16 @@ var
351 // transmux the segment data from MP2T to FLV 348 // transmux the segment data from MP2T to FLV
352 segmentParser.parseSegmentBinaryData(new Uint8Array(segmentXhr.response)); 349 segmentParser.parseSegmentBinaryData(new Uint8Array(segmentXhr.response));
353 350
354 if(gotoSecond!==null && gotoSecond>0) 351 if(desiredMillisecond!==null && desiredMillisecond>0)
355 {
356 var seekToTagIndex = parseInt((segmentParser.getTags().length/player.hls.selectPlaylist().segments[player.hls.mediaIndex].duration)*gotoSecond,10);
357 var seekToTagCounter = 0;
358
359 //console.log('Go to:', gotoSecond);
360 //console.log('tag index:', seekToTagIndex);
361
362 //drain until where you want in the buffer
363 while(seekToTagCounter<seekToTagIndex)
364 { 352 {
353 for(tagIndex = 0; tagIndex < segmentParser.getTags().length-1; tagIndex++) {
354 if(segmentParser.getTags()[tagIndex].pts <= desiredMillisecond && segmentParser.getTags()[tagIndex+1].pts > desiredMillisecond) {
355 targetTag = tagIndex;
356 }
357 }
358 while(bleedIndex < targetTag) {
365 segmentParser.getNextTag(); 359 segmentParser.getNextTag();
366 seekToTagCounter++; 360 bleedIndex++;
367 } 361 }
368 } 362 }
369 363
...@@ -371,18 +365,6 @@ var ...@@ -371,18 +365,6 @@ var
371 player.hls.sourceBuffer.appendBuffer(segmentParser.getNextTag().bytes, player); 365 player.hls.sourceBuffer.appendBuffer(segmentParser.getNextTag().bytes, player);
372 } 366 }
373 367
374 /*
375 if(player.hls.mediaIndex === player.hsl.selectPlaylist.segments.length-1)
376 {
377 try {
378 // TODO - Still need to test this. This tells the Flash Player the stream is over
379 // player.el().getElementsByClassName('vjs-tech')[0].vjs_setProperty('appendBytesAction', 'endSequence');
380 } catch(err) {
381
382 }
383 }
384 */
385
386 segmentXhr = null; 368 segmentXhr = null;
387 player.hls.mediaIndex++; 369 player.hls.mediaIndex++;
388 370
......