9ca37f0f by Tom Johnson

seeking fixes

1 parent f564cde0
...@@ -33,6 +33,47 @@ var ...@@ -33,6 +33,47 @@ var
33 }, 33 },
34 34
35 /** 35 /**
36 * TODO - Document this great feature.
37 *
38 * @param playlist
39 * @param time
40 * @returns int
41 */
42 getMediaIndexByTime = function (playlist, time) {
43 var index, counter, timeRanges, currentSegmentRange;
44
45 timeRanges = [];
46 for (index = 0; index < playlist.segments.length; index++) {
47 currentSegmentRange = {};
48 currentSegmentRange.start = (index === 0) ? 0 : timeRanges[index - 1].end;
49 currentSegmentRange.end = currentSegmentRange.start + playlist.segments[index].duration;
50 timeRanges.push(currentSegmentRange);
51 }
52
53 for (counter = 0; counter < timeRanges.length; counter++) {
54 if (time >= timeRanges[counter].start && time < timeRanges[counter].end) {
55 return counter;
56 }
57 }
58
59 return -1;
60
61 },
62 getPtsByTime = function (segmentParser, time) {
63 var index = 0;
64
65 for (index; index<segmentParser.getTags().length; index++) {
66 if(index === segmentParser.getTags().length-1) {
67 return segmentParser.getTags()[index].pts;
68 } else {
69 if (segmentParser.getTags()[index].pts <= time && segmentParser.getTags()[index+1].pts > time) {
70 return segmentParser.getTags()[index].pts;
71 }
72 }
73 }
74 },
75
76 /**
36 * Constructs a new URI by interpreting a path relative to another 77 * Constructs a new URI by interpreting a path relative to another
37 * URI. 78 * URI.
38 * @param basePath {string} a relative or absolute URI 79 * @param basePath {string} a relative or absolute URI
...@@ -111,47 +152,10 @@ var ...@@ -111,47 +152,10 @@ var
111 152
112 player.on('seeking', function() { 153 player.on('seeking', function() {
113 var seekValue = player.el().querySelector('.vjs-tech').vjs_getProperty('lastSeekedTime'); 154 var seekValue = player.el().querySelector('.vjs-tech').vjs_getProperty('lastSeekedTime');
114 player.hls.mediaIndex = player.hls.getSegmentIndexByTime(seekValue); 155 player.hls.mediaIndex = getMediaIndexByTime(player.hls.media, seekValue);
115 console.log('seek to ', seekValue, 'segment index', player.hls.mediaIndex);
116 fillBuffer(seekValue * 1000); 156 fillBuffer(seekValue * 1000);
117 }); 157 });
118 158
119 player.on('timeupdate', function() {
120 console.log('tu', player.currentTime());
121 });
122
123 player.on('ended', function() {
124 console.log('ended');
125 })
126
127 player.hls.getSegmentIndexByTime = function(time) {
128 var index, counter, timeRanges, currentSegmentRange;
129
130 if (player.hls.media && player.hls.media.segments) {
131
132 timeRanges = [];
133
134 for (index = 0; index < player.hls.media.segments.length; index++) {
135 currentSegmentRange = {};
136 currentSegmentRange.start = (index===0) ? 0 : timeRanges[index-1].end;
137 currentSegmentRange.end = currentSegmentRange.start + player.hls.media.segments[index].duration;
138 timeRanges.push(currentSegmentRange);
139 }
140
141 for(counter = 0; counter < timeRanges.length; counter++){
142 if(time >= timeRanges[counter].start && time < timeRanges[counter].end) {
143
144 return counter;
145 }
146 }
147
148 return null;
149
150 }
151
152 return null;
153 };
154
155 /** 159 /**
156 * Chooses the appropriate media playlist based on the current 160 * Chooses the appropriate media playlist based on the current
157 * bandwidth estimate and the player size. 161 * bandwidth estimate and the player size.
...@@ -249,7 +253,7 @@ var ...@@ -249,7 +253,7 @@ var
249 // update the duration 253 // update the duration
250 player.duration(parser.manifest.totalDuration); 254 player.duration(parser.manifest.totalDuration);
251 // Notify the flash layer 255 // Notify the flash layer
252 player.el().querySelector('.vjs-tech').vjs_setProperty('duration',parser.manifest.totalDuration); 256 //player.el().querySelector('.vjs-tech').vjs_setProperty('duration',parser.manifest.totalDuration);
253 } 257 }
254 player.trigger('loadedmanifest'); 258 player.trigger('loadedmanifest');
255 player.trigger('loadedmetadata'); 259 player.trigger('loadedmetadata');
...@@ -325,28 +329,17 @@ var ...@@ -325,28 +329,17 @@ var
325 // transmux the segment data from MP2T to FLV 329 // transmux the segment data from MP2T to FLV
326 segmentParser.parseSegmentBinaryData(new Uint8Array(segmentXhr.response)); 330 segmentParser.parseSegmentBinaryData(new Uint8Array(segmentXhr.response));
327 331
328 console.log('reporting segment', player.hls.mediaIndex, player.hls.media.segments.length);
329
330 if(player.hls.mediaIndex === player.hls.media.segments.length-1)
331 {
332 console.log('- tag count', segmentParser.getTags().length);
333 console.log('- start pts', segmentParser.getTags()[0].pts);
334 console.log('- end pts', segmentParser.getTags()[segmentParser.getTags().length-1].pts);
335 player.duration(19);
336 }
337
338 // handle intra-segment seeking, if requested // 332 // handle intra-segment seeking, if requested //
339 // do not iterate over 0 index because it comes back with the end time // 333 // do not iterate over 0 index because it comes back with the end time //
340 if (millisecond !== undefined && typeof(millisecond) === "number") { 334 if (millisecond !== undefined && typeof(millisecond) === "number") {
341 for (tagIndex = 1; tagIndex < segmentParser.getTags().length; tagIndex++) { 335 player.el().querySelector('.vjs-tech').vjs_setProperty('lastSeekedTime', getPtsByTime(segmentParser,millisecond)/1000);
336 for (tagIndex = 0; tagIndex < segmentParser.getTags().length; tagIndex++) {
342 if (segmentParser.getTags()[tagIndex].pts > millisecond) { 337 if (segmentParser.getTags()[tagIndex].pts > millisecond) {
343 break; 338 break;
344 } 339 }
345 // we're seeking past this tag, so ignore it 340 // we're seeking past this tag, so ignore it
346 segmentParser.getNextTag(); 341 segmentParser.getNextTag();
347 } 342 }
348 player.el().querySelector('.vjs-tech').vjs_setProperty('lastSeekedTime', segmentParser.getTags()[tagIndex].pts/1000);
349 console.log('LST', tagIndex, '/', segmentParser.getTags().length, segmentParser.getTags()[tagIndex].pts/1000, player.currentTime());
350 } 343 }
351 344
352 while (segmentParser.tagsAvailable()) { 345 while (segmentParser.tagsAvailable()) {
...@@ -356,6 +349,12 @@ var ...@@ -356,6 +349,12 @@ var
356 segmentXhr = null; 349 segmentXhr = null;
357 player.hls.mediaIndex++; 350 player.hls.mediaIndex++;
358 351
352 if (player.hls.mediaIndex === player.hls.media.segments.length) {
353 //TODO - Fix the endofstream //
354 mediaSource.endOfStream();
355 return;
356 }
357
359 // figure out what stream the next segment should be downloaded from 358 // figure out what stream the next segment should be downloaded from
360 // with the updated bandwidth information 359 // with the updated bandwidth information
361 playlist = player.hls.selectPlaylist(); 360 playlist = player.hls.selectPlaylist();
......