-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.js
430 lines (375 loc) · 12 KB
/
player.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
This file is part of motion-webplayer
Copyright (C) 2015 Matthew Watts
motion-webplayer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as published by
the Free Software Foundation
motion-webplayer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with motion-webplayer. If not, see <http://www.gnu.org/licenses/>.
Contains portions of code from Dag Erlandsson (langarod@gmail.com) - http://www.lavrsen.dk/foswiki/bin/view/Motion/MotionJpegViewer
*/
// psuedo enum
var PLAYSTATE_NOCHANGE = -2;
var PLAYSTATE_RESET = -1;
var PLAYSTATE_STOPPED = 0;
var PLAYSTATE_PLAY_FORWARDS = 1;
var PLAYSTATE_PLAY_BACKWARDS = 2;
var PLAYSTATE_STEP_FORWARDS = 3;
var PLAYSTATE_STEP_BACKWARDS = 4;
var PLAYSTATE_STEP_START = 5;
var PLAYSTATE_STEP_END = 6;
// globals
var frame=-1;
var frameStep = 1;
var playState = PLAYSTATE_STOPPED;
var currentFeed = '';
var feeds = new Array();
var frame_list = new Array();
var jpeg_mode = true;
$( document ).ready(function() {
// Update Feeds List
$.ajax({
url: "feedlist.php",
dataType: "json",
success: function(data) {
feeds=data;
var feedsHTML = "";
for (currentFeedIndex in feeds) {
feedsHTML += "<span id='" + feeds[currentFeedIndex][0] +
"' data-feed='" + feeds[currentFeedIndex][0] +
"' class='feedUnSelect'>" + feeds[currentFeedIndex][0] +"</span>";
}
destDiv = document.getElementById('feedbuttons_container');
destDiv.innerHTML = feedsHTML;
},
error: function (request, status, error) {
alert(request.responseText);
}
});
$("#image1").load(function() {
updateUIPositionInfo();
loadNextFrame(PLAYSTATE_NOCHANGE);
});
$("#video1").on("timeupdate", function (e) {
frameTime = 1 / 25; //assume 25 fps
if ( playState == PLAYSTATE_PLAY_FORWARDS ) {
frame = Math.floor( video1.currentTime / frameTime );
updateUIPositionInfo();
loadNextFrame(PLAYSTATE_NOCHANGE);
} else {
updateUIPositionInfo();
}
});
$('#feedbuttons_container').on('click', '.feedUnSelect', function() {
enterFeed($(this).data("feed"));
});
$('#txtFeedDate').on('change', function() {
dateClick();
});
});
$(document).keydown( function(event) {
key_up = 38;
key_down = 40;
key_left = 37;
key_right = 39;
key_left_comma = 188;
key_right_stop = 190;
switch(event.which) {
case key_up :
loadNextFrame(PLAYSTATE_STOPPED);
break;
case key_down :
loadNextFrame(PLAYSTATE_STOPPED);
break;
case key_left :
loadNextFrame(PLAYSTATE_PLAY_BACKWARDS);
break;
case key_right :
loadNextFrame(PLAYSTATE_PLAY_FORWARDS);
break;
case key_left_comma :
loadNextFrame(PLAYSTATE_STEP_BACKWARDS);
break;
case key_right_stop :
loadNextFrame(PLAYSTATE_STEP_FORWARDS);
break;
}
});
function loadNextFrame(newPlayState) {
if(newPlayState !== PLAYSTATE_NOCHANGE && newPlayState != PLAYSTATE_PLAY_FORWARDS && ! video1.paused) {
video1.pause();
}
if(newPlayState !== PLAYSTATE_NOCHANGE) {
playState = newPlayState;
}
updateUIPositionInfo();
frame=Number(frame);
oldFrame = frame;
switch(playState) {
case PLAYSTATE_STOPPED: // stopped - no next frame to load
break;
case PLAYSTATE_PLAY_FORWARDS: // playing forwards
if ( frame_list[frame]['frame_filehash'] == "video" ) {
video1.playbackRate = 2.0;
video1.defaultPlaybackRate = 2.0;
video1.play();
} else {
if( (frame + frameStep) < frame_list.length ) {
frame+=frameStep;
} else {
playState=PLAYSTATE_STOPPED;
}
}
break;
case PLAYSTATE_PLAY_BACKWARDS: // playing backwards
if( (frame - frameStep) > -1 ) {
frame-=frameStep;
} else {
playState=PLAYSTATE_STOPPED;
}
break;
case PLAYSTATE_STEP_FORWARDS: // step forward one frame & stop
if( (frame + frameStep) < frame_list.length ) {
frame+=frameStep;
playState = PLAYSTATE_STOPPED;
} else {
playState = PLAYSTATE_STOPPED;
}
break;
case PLAYSTATE_STEP_BACKWARDS: // step backwards one frame & stop
if( (frame - frameStep) > -1 ) {
frame-=frameStep;
playState = PLAYSTATE_STOPPED;
} else {
playState = PLAYSTATE_STOPPED;
}
break;
case PLAYSTATE_STEP_END:
frame = frame_list.length -1;
break;
case PLAYSTATE_STEP_START:
frame = 0;
break;
case PLAYSTATE_RESET:
playState = PLAYSTATE_STOPPED;
oldFrame = -1;
break;
}
if(oldFrame !== frame) {
updateVideoPosition();
}
}
// either update jpeg img src or set position on video player
function updateVideoPosition() {
frameTime = 1 / 25; //assume 25 fps
if ( frame_list[frame]['frame_filehash'] == "video" ) {
video1.play();
video1.currentTime = frame * frameTime;
console.log(video1.currentTime);
video1.pause();
// unlike setting src on img for jpeg frame loading there will be no callback
// for setting a video frame so we will make a callback function
setTimeout( updateVideoPosition_videocallback, Math.floor(frameTime * 1000));
} else {
var fileHash = frame_list[Number(frame)]['frame_filehash'];
imgUrl = 'getframe.php?q=80&frame=' + encodeURIComponent(fileHash);
img = document.getElementById('image1');
img.src = imgUrl;
}
}
var updateVideoPosition_videocallback_framewaittime = 0;
function updateVideoPosition_videocallback() {
// check video is not still seeking before returning to loadnextframe
if ( ! video1.seeking ) {
$('#loading_frameimage').css("display", "none");
updateUIPositionInfo();
loadNextFrame(PLAYSTATE_NOCHANGE);
updateVideoPosition_videocallback_framewaittime = 0;
} else {
console.log("seeking");
// if still seeking try the callback again in 10ms
// if it has been seeking for >500ms then display the loading image
updateVideoPosition_videocallback_framewaittime += 10;
if ( updateVideoPosition_videocallback_framewaittime > 500 ) {
$('#loading_frameimage').css("display", "block");
}
setTimeout( updateVideoPosition_videocallback, 10);
}
}
function updateUIPositionInfo() {
// check if any frame is selected
if(frame == -1) {
return;
}
// select progress bar and ensure it is ready
var progressBar = document.getElementById('progress-bar');
progressBar.max = 24 * 60 * 60; // seconds in a day
// get the time from the current frame (in seconds from midnight) - convert into hh mm ss and subframe number
currentFrameFullSeconds = frame_list[Number(frame)]['frame_seconds'];
currentFrameIndex = frame_list[Number(frame)]['subframe_index'];
currentFrameHours = Math.floor(currentFrameFullSeconds / 3600);
currentFrameHours_remainder = currentFrameFullSeconds%3600;
currentFrameMins = Math.floor(currentFrameHours_remainder / 60);
currentFrameSeconds = currentFrameHours_remainder%60;
// pad with zeros
currentFrameHours = ("0" + currentFrameHours).slice (-2);
currentFrameMins = ("0" + currentFrameMins).slice (-2);
currentFrameSeconds = ("0" + currentFrameSeconds).slice (-2);
currentFrameIndex = ("0" + currentFrameIndex).slice (-2);
// update document
$('#frameTime_HH').attr('value',currentFrameHours);
$('#frameTime_MM').attr('value',currentFrameMins);
$('#frameTime_SS').attr('value',currentFrameSeconds);
$('#frameTime_FF').attr('value',currentFrameIndex);
progressBar.value = Number(frame_list[Number(frame)]['frame_seconds']);
}
function getFrameNearestToSeconds(secondsTime) {
found_frame = 0;
for (var i in frame_list) {
if(frame_list[i]['frame_seconds'] < secondsTime) {
found_frame = i;
}
}
return found_frame;
}
function printDateDropdown(feedName) {
dl = document.getElementById('txtFeedDate');
if (typeof dl.options[dl.selectedIndex].value !== 'undefined') {
oldFeedDate = dl.options[dl.selectedIndex].value;
} else {
oldFeedDate = '';
}
for(i = dl.options.length-1; i>=0; i--) {
dl.options[i] = null;
}
selected = false;
for(fi = 0; fi < feeds.length; fi++) {
if(feeds[fi][0] == feedName) {
for(di = 1; di < feeds[fi].length; di++) {
dl.options[di-1] = new Option(feeds[fi][di]);
dl.options[di-1].value = feeds[fi][di];
if(feeds[fi][di] == oldFeedDate) {
dl.options[di-1].selected = true;
selected = true;
}
}
if(selected == false) {
dl.options[dl.options.length-1].selected=true; // select last item in list
}
dateClick();
}
}
}
function dateClick() {
$('#loading_frameimage').css("display", "block");
updateTimeline();
}
function updateTimeline() {
$('#footer_container').css("display", "none");
$('#loading_frameimage').css("display", "block");
$('#image1').css("display", "none");
frame = -1;
fd = document.getElementById('txtFeedDate');
feedDate = fd.options[fd.selectedIndex].value;
if(currentFeed != '') {
timeUrl = 'timeline.php?feed=' + currentFeed + '&date=' + feedDate;
framesUrl = 'frame_list.php?feed=' + currentFeed + '&date=' + feedDate;
} else {
timeUrl = 'timeline.php';
}
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
frame_list = JSON.parse(xmlhttp.responseText);
$('#timeline').attr('src',timeUrl);
frame = 0;
loadNextFrame(PLAYSTATE_RESET);
$('#footer_container').css("display", "block");
$('#loading_frameimage').css("display", "none");
if ( frame_list[0]['frame_filehash'] == "video" ) {
$('#image1').css("display", "none");
$('#video1').css("display", "block");
$('#video1').attr('src',"files/" + frame_list[0]['frame_filename']);
setTimeout(function(){ video1.load; video1.play(); video1.pause(); }, 1000);
jpeg_mode = true;
} else {
$('#image1').css("display", "block");
$('#video1').css("display", "none");
jpeg_mode = false;
}
}
}
xmlhttp.open("GET", framesUrl, true);
xmlhttp.send();
}
function enterFeed(feedName) {
if(currentFeed != '') {
feedUnSelect(currentFeed);
} else {
$('#loading_frameimage').css("display", "block");
}
feedSelect(feedName);
currentFeed = feedName;
printDateDropdown(feedName);
updateTimeline();
}
function feedSelect(feedName) {
td = document.getElementById(feedName);
td.className = 'feedSelect';
}
function feedUnSelect(feedName) {
td = document.getElementById(feedName);
td.className = 'feedUnSelect';
}
var saved_time_selection=0;
function tl_move(e) {
if (document.layers) {
rx = e.pageX;
rx = rx - document.getElementById('timeline').x;
}
else {
if (document.all) {
rx = event.clientX + document.body.scrollLeft;
} else {
rx = e.clientX + document.body.scrollLeft;
}
iPos = 0; elt = document.getElementById('timeline');
while (elt != null) {
iPos += elt.offsetLeft;
elt = elt.offsetParent;
}
}
rx = rx - iPos;
saved_time_selection_pre=parseInt(rx * (86400/(document.getElementById('timeline').width-2)));
saved_time_selection=getFrameNearestToSeconds(saved_time_selection_pre);
// get the time from the current frame (in seconds from midnight) - convert into hh mm ss and subframe number
currentFrameFullSeconds = frame_list[Number(saved_time_selection)]['frame_seconds'];
currentFrameIndex = frame_list[Number(frame)]['subframe_index'];
currentFrameHours = Math.floor(currentFrameFullSeconds / 3600);
currentFrameHours_remainder = currentFrameFullSeconds%3600;
currentFrameMins = Math.floor(currentFrameHours_remainder / 60);
currentFrameSeconds = currentFrameHours_remainder%60;
// pad with zeros
currentFrameHours = ("0" + currentFrameHours).slice (-2);
currentFrameMins = ("0" + currentFrameMins).slice (-2);
currentFrameSeconds = ("0" + currentFrameSeconds).slice (-2);
currentFrameIndex = ("0" + currentFrameIndex).slice (-2);
// update document
$('#frameTime_HH').attr('value',currentFrameHours);
$('#frameTime_MM').attr('value',currentFrameMins);
$('#frameTime_SS').attr('value',currentFrameSeconds);
$('#frameTime_FF').attr('value',currentFrameIndex);
return false;
}
function tl_out() {
updateUIPositionInfo();
}
function tl_click() {
frame = Number(saved_time_selection);
updateUIPositionInfo();
loadNextFrame(PLAYSTATE_RESET);
}