var syncable_media_basic = function () { var that = this; that.HANDLER_TYPE_NAME = 'media'; that.run = function(seconds, record) { if (record['data']['type'] == 'start') { //close video if one is showing if (my_media_tracking.get_media_id() != null && record['data']['media_id'] != my_media_tracking.get_media_id()) { $('#sync_player_container').remove(); my_media_tracking.init(); } //get anchor information var sync_media_anchor = $('#sync_media_anchor').val(); var sync_media_width = $('#' + sync_media_anchor).width(); var sync_media_height = $('#' + sync_media_anchor).height(); var sync_media_offset = $("#" + sync_media_anchor).offset(); //get current time code of main video try { var main_timecode = Main.getTimeCode("flash_player"); } catch (err) { return; } //compare with start time of this video var start_offset = 0; if (Math.abs(main_timecode - record['timecode']) > 2) { var start_offset = main_timecode - record['timecode']; } //get the html for the flash player $.ajax({ cache: false, type: "GET", url: "/ajax/media/sync.php", data: { presentation_id: record['data']['presentation_id'], media_id: record['data']['media_id'], height: record['data']['height'], width: record['data']['width'], start_offset: start_offset }, dataType: "html", success: function(html) { //replace width and height var width_replace, height_replace; width_replace = new RegExp('width="' + record['data']['width']+ '"', 'g'); height_replace = new RegExp('height="' + record['data']['height']+ '"', 'g'); html = html.replace(width_replace, 'width="' + sync_media_width + '"'); html = html.replace(height_replace, 'height="' + sync_media_height + '"'); width_replace = new RegExp('vidWidth=' + record['data']['width'], 'g'); height_replace = new RegExp('vidHeight=' + record['data']['height'], 'g'); html = html.replace(width_replace, 'vidWidth=' + sync_media_width); html = html.replace(height_replace, 'vidHeight=' + sync_media_height); //initialize MainSync.stop("sync_player"); $('#sync_player_container').remove(); //place video in page $("body").prepend('
' + html + '
'); //mark this video as playing my_media_tracking.set_media_id(record['data']['media_id']); my_media_tracking.set_start_time(record['timecode']); my_media_tracking.check_pause_or_play(); my_media_tracking.check_seek(); }, error: function() { alert('error'); } }); } else { //remove the item from the page $('#sync_player_container').remove(); my_media_tracking.set_media_id(null); } }; return that; }; var media_tracking = function () { var media_id = null; var start_time = null; var sync_timecode = null; var main_timecode = null; var main_timecode_last = null; var mode = null; var seek_block = 0; var that = this; var debug_p = false; var locked_until = null; var locked_by = null; //contstants var seek_poll_rate = 2000; var pause_play_poll_rate = 1000; var seek_threshold = 4000; var seek_lock_duration = 10; var start_lock_duration = 3; debug = function (msg) { if (debug_p == true) { try { //console.log(msg); } catch (e) {} } } init = function () { media_id = null; start_time = null; sync_timecode = null; main_timecode = null; main_timecode_last = null; mode = null; seek_block = 0; locked_until = null; locked_by = null; debug('initialized'); }; that.set_start_time = function (start_time_in) { start_time = start_time_in; }; that.get_media_id = function () { return that.media_id; }; that.set_media_id = function (media_id_in) { init(); media_id = media_id_in; lock('start', start_lock_duration); unlock(); }; lock = function (role, until) { locked_by = role; if (until != null) { var delay = new Date; var unixtime_ms = delay.getTime(); locked_until = parseInt(unixtime_ms / 1000) + until; } }; unlock = function () { locked_by = null; }; check_locked = function () { if (locked_by != null) { //debug('locked_by: ' + locked_by); return true; } if (locked_until != null) { var delay = new Date; var unixtime_ms = delay.getTime(); // Returns milliseconds since the epoch var now = parseInt(unixtime_ms / 1000); //debug('locked? ' + now + ' < ' + locked_until); if (now < locked_until) { //debug('locked_until: ' + (locked_until - now)); return true; } } return false; } that.check_pause_or_play = function () { debug('check_pause_or_play'); if (media_id == null) { debug('no media_id'); return; } //debug('mode: ' + mode); if (!MainSync.getTimeCode("sync_player")) { debug('Pause Player: nothing yet'); setTimeout(function () { that.check_pause_or_play(); }, pause_play_poll_rate); return; } var time_diff; //sync play / pause main_timecode_last = main_timecode; main_timecode = Main.getTimeCode("flash_player"); debug('main_timecode: ' + main_timecode); if (main_timecode == null || main_timecode < 1) { setTimeout(function () { that.check_pause_or_play(); }, pause_play_poll_rate); return; } if (main_timecode_last != null) { if (main_timecode == main_timecode_last) { if (mode == 'play' || mode == null) { pause(); } } else { if (mode == 'pause' || mode == null) { play(); } } } //call self recursively setTimeout(function () { that.check_pause_or_play(); }, pause_play_poll_rate); }; that.check_seek = function () { //debug('check seek'); if (media_id == null) { debug('no media_id'); return; } if (!MainSync.getTimeCode("sync_player")) { debug('Seek: nothing yet'); setTimeout(function () { that.check_seek(); }, seek_poll_rate); return; } // var last_access = Main.getScrubBarTimeFromLastAccess("sync_player"); //debug('last_access: ' + last_access); // if (last_access < seek_threshold) { seek(); // } //call self recursively setTimeout(function () { that.check_seek(); }, seek_poll_rate); }; var pause = function () { debug('pause'); MainSync.pause("sync_player"); mode = 'pause'; }; var play = function () { debug('play'); MainSync.resume("sync_player"); mode = 'play'; }; var seek = function (timecode) { var direction; if (check_locked() == true) { return; } var sync_timecode = MainSync.getTimeCode("sync_player"); if (sync_timecode < 1 || sync_timecode == null) { return; } var main_timecode = Main.getTimeCode("flash_player"); if (main_timecode < 1 || main_timecode == null) { return; } var diff = ((start_time + sync_timecode) - main_timecode); var seekto = sync_timecode + (diff * -1); if (seekto < main_timecode) { direction = 'back'; } else { direction = 'forward'; } debug(direction + ', (start_time: ' + start_time + ' + sync_timecode: ' + sync_timecode + ') - main_timecode: ' + main_timecode + ') = ' + diff); if (Math.abs(diff) > 3) { //debug('SEEK. now: ' + sync_timecode + ', goto: ' + seekto + ', diff ' + (diff * -1)); lock('sync', seek_lock_duration); MainSync.setTimeCode("sync_player", seekto); unlock(); } }; return that; }; var my_media_tracking = media_tracking();