Merge branch '3.0' of https://github.com/calzoneman/sync into 3.0
[KisSync.git] / player / dailymotion.coffee
blob2d8e3b2187aa57135ee71f9f47b8cb928e5759cc
1 window.DailymotionPlayer = class DailymotionPlayer extends Player
2     constructor: (data) ->
3         if not (this instanceof DailymotionPlayer)
4             return new DailymotionPlayer(data)
6         @setMediaProperties(data)
7         @initialVolumeSet = false
8         @playbackReadyCb = null
10         waitUntilDefined(window, 'DM', =>
11             removeOld()
13             params =
14                 autoplay: 1
15                 wmode: if USEROPTS.wmode_transparent then 'transparent' else 'opaque'
16                 logo: 0
18             quality = @mapQuality(USEROPTS.default_quality)
19             if quality != 'auto'
20                 params.quality = quality
22             @dm = DM.player('ytapiplayer',
23                 video: data.id
24                 width: parseInt(VWIDTH, 10)
25                 height: parseInt(VHEIGHT, 10)
26                 params: params
27             )
29             @dm.addEventListener('apiready', =>
30                 @dmReady = true
31                 @dm.addEventListener('ended', ->
32                     if CLIENT.leader
33                         socket.emit('playNext')
34                 )
36                 @dm.addEventListener('pause', =>
37                     @paused = true
38                     if CLIENT.leader
39                         sendVideoUpdate()
40                 )
42                 @dm.addEventListener('playing', =>
43                     @paused = false
44                     if CLIENT.leader
45                         sendVideoUpdate()
47                     if not @initialVolumeSet
48                         @setVolume(VOLUME)
49                         @initialVolumeSet = true
50                 )
52                 # Once the video stops, the internal state of the player
53                 # becomes unusable and attempting to load() will corrupt it and
54                 # crash the player with an error.  As a short–medium term
55                 # workaround, mark the player as "not ready" until the next
56                 # playback_ready event
57                 @dm.addEventListener('video_end', =>
58                     @dmReady = false
59                 )
60                 @dm.addEventListener('playback_ready', =>
61                     @dmReady = true
62                     if @playbackReadyCb
63                         @playbackReadyCb()
64                         @playbackReadyCb = null
65                 )
66             )
67         )
69     load: (data) ->
70         @setMediaProperties(data)
71         if @dm and @dmReady
72             @dm.load(data.id)
73             @dm.seek(data.currentTime)
74         else if @dm
75             # TODO: Player::load() needs to be made asynchronous in the future
76             console.log('Warning: load() called before DM is ready, queueing callback')
77             @playbackReadyCb = () =>
78                 @dm.load(data.id)
79                 @dm.seek(data.currentTime)
80         else
81             console.error('WTF?  DailymotionPlayer::load() called but @dm is undefined')
83     pause: ->
84         if @dm and @dmReady
85             @paused = true
86             @dm.pause()
88     play: ->
89         if @dm and @dmReady
90             @paused = false
91             @dm.play()
93     seekTo: (time) ->
94         if @dm and @dmReady
95             @dm.seek(time)
97     setVolume: (volume) ->
98         if @dm and @dmReady
99             @dm.setVolume(volume)
101     getTime: (cb) ->
102         if @dm and @dmReady
103             cb(@dm.currentTime)
104         else
105             cb(0)
107     getVolume: (cb) ->
108         if @dm and @dmReady
109             if @dm.muted
110                 cb(0)
111             else
112                 volume = @dm.volume
113                 # There was once a bug in Dailymotion where it sometimes gave back
114                 # volumes in the wrong range.  Not sure if this is still a necessary
115                 # check.
116                 if volume > 1
117                     volume /= 100
118                 cb(volume)
119         else
120             cb(VOLUME)
122     mapQuality: (quality) ->
123         switch String(quality)
124             when '240', '480', '720', '1080' then String(quality)
125             when '360' then '380'
126             when 'best' then '1080'
127             else 'auto'
129     destroy: ->
130         if @dm
131             @dm.destroy('ytapiplayer')