sync/player/dailymotion.coffee

136 lines
4.3 KiB
CoffeeScript
Raw Normal View History

2015-05-05 20:06:37 +00:00
window.DailymotionPlayer = class DailymotionPlayer extends Player
constructor: (data) ->
if not (this instanceof DailymotionPlayer)
return new DailymotionPlayer(data)
@setMediaProperties(data)
@initialVolumeSet = false
@playbackReadyCb = null
2015-05-05 20:06:37 +00:00
waitUntilDefined(window, 'DM', =>
removeOld()
params =
autoplay: 1
logo: 0
quality = @mapQuality(USEROPTS.default_quality)
if quality != 'auto'
params.quality = quality
@element = DM.$('ytapiplayer')
if not @element or @element.nodeType != Node.ELEMENT_NODE
throw new Error("Invalid player element in DailymotionPlayer(), requires an existing HTML element: " + @element)
if DM.Player._INSTANCES[@element.id] != undefined
@element = DM.Player.destroy(@element.id)
@dm = DM.Player.create(@element,
2015-05-05 20:06:37 +00:00
video: data.id
width: parseInt(VWIDTH, 10)
height: parseInt(VHEIGHT, 10)
params: params
)
@dm.addEventListener('apiready', =>
@dmReady = true
2015-05-05 20:06:37 +00:00
@dm.addEventListener('ended', ->
if CLIENT.leader
socket.emit('playNext')
)
@dm.addEventListener('pause', =>
@paused = true
if CLIENT.leader
sendVideoUpdate()
)
@dm.addEventListener('playing', =>
@paused = false
if CLIENT.leader
sendVideoUpdate()
if not @initialVolumeSet
@setVolume(VOLUME)
@initialVolumeSet = true
)
2019-02-02 23:56:20 +00:00
# Once the video stops, the internal state of the player
# becomes unusable and attempting to load() will corrupt it and
# crash the player with an error. As a shortmedium term
# workaround, mark the player as "not ready" until the next
# playback_ready event
2019-02-02 23:56:20 +00:00
@dm.addEventListener('video_end', =>
@dmReady = false
2019-02-02 23:56:20 +00:00
)
@dm.addEventListener('playback_ready', =>
@dmReady = true
if @playbackReadyCb
@playbackReadyCb()
@playbackReadyCb = null
2019-02-02 23:56:20 +00:00
)
2015-05-05 20:06:37 +00:00
)
)
load: (data) ->
@setMediaProperties(data)
if @dm and @dmReady
2015-05-05 20:06:37 +00:00
@dm.load(data.id)
@dm.seek(data.currentTime)
else if @dm
# TODO: Player::load() needs to be made asynchronous in the future
console.log('Warning: load() called before DM is ready, queueing callback')
@playbackReadyCb = () =>
@dm.load(data.id)
@dm.seek(data.currentTime)
2015-05-05 20:06:37 +00:00
else
console.error('WTF? DailymotionPlayer::load() called but @dm is undefined')
2015-05-05 20:06:37 +00:00
pause: ->
if @dm and @dmReady
2015-05-05 20:06:37 +00:00
@paused = true
@dm.pause()
play: ->
if @dm and @dmReady
2015-05-05 20:06:37 +00:00
@paused = false
@dm.play()
seekTo: (time) ->
if @dm and @dmReady
2015-05-05 20:06:37 +00:00
@dm.seek(time)
setVolume: (volume) ->
if @dm and @dmReady
2015-05-05 20:06:37 +00:00
@dm.setVolume(volume)
getTime: (cb) ->
if @dm and @dmReady
2015-05-05 20:06:37 +00:00
cb(@dm.currentTime)
else
cb(0)
getVolume: (cb) ->
if @dm and @dmReady
2015-05-05 20:06:37 +00:00
if @dm.muted
cb(0)
else
volume = @dm.volume
# There was once a bug in Dailymotion where it sometimes gave back
# volumes in the wrong range. Not sure if this is still a necessary
# check.
if volume > 1
volume /= 100
cb(volume)
else
cb(VOLUME)
mapQuality: (quality) ->
switch String(quality)
when '240', '480', '720', '1080' then String(quality)
when '360' then '380'
when 'best' then '1080'
else 'auto'
2020-06-20 01:31:07 +00:00
destroy: ->
if @dm
@dm.destroy('ytapiplayer')