Merge branch '3.0' of github.com:calzoneman/sync into 3.0
[KisSync.git] / player / dailymotion.coffee
blob9a8bc1f4b60a5b19d64d4b49d7273155cf41d5b8
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
9         waitUntilDefined(window, 'DM', =>
10             removeOld()
12             params =
13                 autoplay: 1
14                 wmode: if USEROPTS.wmode_transparent then 'transparent' else 'opaque'
15                 logo: 0
17             quality = @mapQuality(USEROPTS.default_quality)
18             if quality != 'auto'
19                 params.quality = quality
21             @dm = DM.player('ytapiplayer',
22                 video: data.id
23                 width: parseInt(VWIDTH, 10)
24                 height: parseInt(VHEIGHT, 10)
25                 params: params
26             )
28             @dm.addEventListener('apiready', =>
29                 @dm.ready = true
30                 @dm.apiready = 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                 # video loads
57                 @dm.addEventListener('video_end', =>
58                     @dm.ready = false
59                 )
60                 @dm.addEventListener('playback_ready', =>
61                     @dm.ready = true
62                 )
63             )
64         )
66     load: (data) ->
67         @setMediaProperties(data)
68         if @dm and @dm.apiready
69             @dm.load(data.id)
70             @dm.seek(data.currentTime)
71         else
72             console.error('WTF?  DailymotionPlayer::load() called but dm is not ready')
74     pause: ->
75         if @dm and @dm.ready
76             @paused = true
77             @dm.pause()
79     play: ->
80         if @dm and @dm.ready
81             @paused = false
82             @dm.play()
84     seekTo: (time) ->
85         if @dm and @dm.ready
86             @dm.seek(time)
88     setVolume: (volume) ->
89         if @dm and @dm.ready
90             @dm.setVolume(volume)
92     getTime: (cb) ->
93         if @dm and @dm.ready
94             cb(@dm.currentTime)
95         else
96             cb(0)
98     getVolume: (cb) ->
99         if @dm and @dm.ready
100             if @dm.muted
101                 cb(0)
102             else
103                 volume = @dm.volume
104                 # There was once a bug in Dailymotion where it sometimes gave back
105                 # volumes in the wrong range.  Not sure if this is still a necessary
106                 # check.
107                 if volume > 1
108                     volume /= 100
109                 cb(volume)
110         else
111             cb(VOLUME)
113     mapQuality: (quality) ->
114         switch String(quality)
115             when '240', '480', '720', '1080' then String(quality)
116             when '360' then '380'
117             when 'best' then '1080'
118             else 'auto'