sync/player/update.coffee

117 lines
3.6 KiB
CoffeeScript
Raw Normal View History

2015-05-02 16:45:35 +00:00
TYPE_MAP =
yt: YouTubePlayer
vi: VimeoPlayer
2015-05-05 20:06:37 +00:00
dm: DailymotionPlayer
2016-08-16 04:00:56 +00:00
gd: GoogleDrivePlayer
2015-05-15 06:19:08 +00:00
gp: VideoJSPlayer
2015-07-01 16:38:01 +00:00
fi: FilePlayer
jw: FilePlayer
2015-06-16 11:39:39 +00:00
sc: SoundCloudPlayer
2015-06-16 16:50:17 +00:00
li: LivestreamPlayer
2015-06-16 20:44:14 +00:00
tw: TwitchPlayer
2016-09-05 01:53:38 +00:00
tv: TwitchPlayer
2015-06-18 22:46:33 +00:00
cu: CustomEmbedPlayer
2015-06-20 02:12:48 +00:00
rt: RTMPPlayer
2017-05-20 23:50:00 +00:00
hb: SmashcastPlayer
2015-06-21 14:39:16 +00:00
us: UstreamPlayer
im: ImgurPlayer
2016-06-26 00:09:48 +00:00
vm: VideoJSPlayer
2016-08-07 04:14:52 +00:00
hl: HLSPlayer
2016-08-03 05:35:00 +00:00
sb: VideoJSPlayer
2017-05-27 18:49:27 +00:00
tc: VideoJSPlayer
2015-05-02 16:45:35 +00:00
window.loadMediaPlayer = (data) ->
try
if window.PLAYER
window.PLAYER.destroy()
catch error
console.error error
if data.meta.direct and data.type != 'gd'
2015-07-05 20:50:34 +00:00
try
window.PLAYER = new VideoJSPlayer(data)
catch e
console.error e
2015-06-28 16:42:21 +00:00
else if data.type of TYPE_MAP
2015-05-02 16:45:35 +00:00
try
window.PLAYER = TYPE_MAP[data.type](data)
catch e
console.error e
window.handleMediaUpdate = (data) ->
PLAYER = window.PLAYER
# Do not update if the current time is past the end of the video, unless
# the video has length 0 (which is a special case for livestreams)
if typeof PLAYER.mediaLength is 'number' and
PLAYER.mediaLength > 0 and
data.currentTime > PLAYER.mediaLength
return
# Negative currentTime indicates a lead-in for clients to load the video,
# but not play it yet (helps with initial buffering)
waiting = data.currentTime < 0
# Load a new video in the same player if the ID changed
if data.id and data.id != PLAYER.mediaId
if data.currentTime < 0
data.currentTime = 0
PLAYER.load(data)
PLAYER.play()
if waiting
2015-05-02 22:37:09 +00:00
PLAYER.seekTo(0)
2015-05-02 16:45:35 +00:00
# YouTube player has a race condition that crashes the player if
# play(), seek(0), and pause() are called quickly without waiting
# for events to fire. Setting a flag variable that is checked in the
# event handler mitigates this.
if PLAYER instanceof YouTubePlayer
PLAYER.pauseSeekRaceCondition = true
else
PLAYER.pause()
2015-05-02 22:37:09 +00:00
return
2015-05-02 16:45:35 +00:00
else if PLAYER instanceof YouTubePlayer
PLAYER.pauseSeekRaceCondition = false
if CLIENT.leader or not USEROPTS.synch
return
if data.paused and not PLAYER.paused
PLAYER.seekTo(data.currentTime)
PLAYER.pause()
2015-07-05 20:29:06 +00:00
else if PLAYER.paused and not data.paused
2015-05-02 16:45:35 +00:00
PLAYER.play()
PLAYER.getTime((seconds) ->
time = data.currentTime
diff = (time - seconds) or time
accuracy = USEROPTS.sync_accuracy
# Dailymotion can't seek very accurately in Flash due to keyframe
# placement. Accuracy should not be set lower than 5 or the video
# may be very choppy.
2015-05-15 06:19:08 +00:00
if PLAYER instanceof DailymotionPlayer
2015-05-02 16:45:35 +00:00
accuracy = Math.max(accuracy, 5)
if diff > accuracy
# The player is behind the correct time
PLAYER.seekTo(time)
else if diff < -accuracy
# The player is ahead of the correct time
# Don't seek all the way back, to account for possible buffering.
# However, do seek all the way back for Dailymotion due to the
# keyframe issue mentioned above.
2015-05-15 06:19:08 +00:00
if not (PLAYER instanceof DailymotionPlayer)
2015-05-02 16:45:35 +00:00
time += 1
PLAYER.seekTo(time)
)
window.removeOld = (replace) ->
$('#soundcloud-volume-holder').remove()
2015-05-02 16:45:35 +00:00
replace ?= $('<div/>').addClass('embed-responsive-item')
old = $('#ytapiplayer')
replace.insertBefore(old)
old.remove()
replace.attr('id', 'ytapiplayer')
return replace