Bump browserslist from 4.16.3 to 4.16.6
[KisSync.git] / player / youtube.coffee
blob7c94f8a909358c6d1f0da1985dea4534f527af09
1 window.YouTubePlayer = class YouTubePlayer extends Player
2     constructor: (data) ->
3         if not (this instanceof YouTubePlayer)
4             return new YouTubePlayer(data)
6         @setMediaProperties(data)
7         @qualityRaceCondition = true
8         @pauseSeekRaceCondition = false
10         waitUntilDefined(window, 'YT', =>
11             # Even after window.YT is defined, YT.Player may not be, which causes a
12             # 'YT.Player is not a constructor' error occasionally
13             waitUntilDefined(YT, 'Player', =>
14                 removeOld()
16                 wmode = if USEROPTS.wmode_transparent then 'transparent' else 'opaque'
17                 @yt = new YT.Player('ytapiplayer',
18                     videoId: data.id
19                     playerVars:
20                         autohide: 1
21                         autoplay: 1
22                         controls: 1
23                         iv_load_policy: 3 # iv_load_policy 3 indicates no annotations
24                         rel: 0
25                         wmode: wmode
26                     events:
27                         onReady: @onReady.bind(this)
28                         onStateChange: @onStateChange.bind(this)
29                 )
30             )
31         )
33     load: (data) ->
34         @setMediaProperties(data)
35         if @yt and @yt.ready
36             @yt.loadVideoById(data.id, data.currentTime)
37             @qualityRaceCondition = true
38             if USEROPTS.default_quality
39                 @setQuality(USEROPTS.default_quality)
40         else
41             console.error('WTF?  YouTubePlayer::load() called but yt is not ready')
43     onReady: ->
44         @yt.ready = true
45         @setVolume(VOLUME)
47     onStateChange: (ev) ->
48         # For some reason setting the quality doesn't work
49         # until the first event has fired.
50         if @qualityRaceCondition
51             @qualityRaceCondition = false
52             if USEROPTS.default_quality
53                 @setQuality(USEROPTS.default_quality)
55         # Similar to above, if you pause the video before the first PLAYING
56         # event is emitted, weird things happen.
57         if ev.data == YT.PlayerState.PLAYING and @pauseSeekRaceCondition
58             @pause()
59             @pauseSeekRaceCondition = false
61         if (ev.data == YT.PlayerState.PAUSED and not @paused) or
62                 (ev.data == YT.PlayerState.PLAYING and @paused)
63             @paused = (ev.data == YT.PlayerState.PAUSED)
64             if CLIENT.leader
65                 sendVideoUpdate()
67         if ev.data == YT.PlayerState.ENDED and CLIENT.leader
68             socket.emit('playNext')
70     play: ->
71         @paused = false
72         if @yt and @yt.ready
73             @yt.playVideo()
75     pause: ->
76         @paused = true
77         if @yt and @yt.ready
78             @yt.pauseVideo()
80     seekTo: (time) ->
81         if @yt and @yt.ready
82             @yt.seekTo(time, true)
84     setVolume: (volume) ->
85         if @yt and @yt.ready
86             if volume > 0
87                 # If the player is muted, even if the volume is set,
88                 # the player remains muted
89                 @yt.unMute()
90             @yt.setVolume(volume * 100)
92     setQuality: (quality) ->
93         if not @yt or not @yt.ready
94             return
96         ytQuality = switch String(quality)
97             when '240' then 'small'
98             when '360' then 'medium'
99             when '480' then 'large'
100             when '720' then 'hd720'
101             when '1080' then 'hd1080'
102             when 'best' then 'highres'
103             else 'auto'
105         if ytQuality != 'auto'
106             @yt.setPlaybackQuality(ytQuality)
108     getTime: (cb) ->
109         if @yt and @yt.ready
110             cb(@yt.getCurrentTime())
111         else
112             cb(0)
114     getVolume: (cb) ->
115         if @yt and @yt.ready
116             if @yt.isMuted()
117                 cb(0)
118             else
119                 cb(@yt.getVolume() / 100)
120         else
121             cb(VOLUME)