cleanup
[KisSync.git] / player / twitch.coffee
blobe95ef2af42f435e0e60d300166152030b950ed77
1 window.TwitchPlayer = class TwitchPlayer extends Player
2     constructor: (data) ->
3         if not (this instanceof TwitchPlayer)
4             return new TwitchPlayer(data)
6         @setMediaProperties(data)
7         waitUntilDefined(window, 'Twitch', =>
8             waitUntilDefined(Twitch, 'Player', =>
9                 @init(data)
10             )
11         )
13     init: (data) ->
14         removeOld()
15         if data.type is 'tv'
16             # VOD
17             options =
18                 video: data.id
19         else
20             # Livestream
21             options =
22                 channel: data.id
23         @twitch = new Twitch.Player('ytapiplayer', options)
24         @twitch.addEventListener(Twitch.Player.READY, =>
25             @setVolume(VOLUME)
26             @twitch.setQuality(@mapQuality(USEROPTS.default_quality))
27             @twitch.addEventListener(Twitch.Player.PLAY, =>
28                 @paused = false
29                 if CLIENT.leader
30                     sendVideoUpdate()
31             )
32             @twitch.addEventListener(Twitch.Player.PAUSE, =>
33                 @paused = true
34                 if CLIENT.leader
35                     sendVideoUpdate()
36             )
37             @twitch.addEventListener(Twitch.Player.ENDED, =>
38                 if CLIENT.leader
39                     socket.emit('playNext')
40             )
41         )
43     load: (data) ->
44         @setMediaProperties(data)
45         try
46             if data.type is 'tv'
47                 # VOD
48                 @twitch.setVideo(data.id)
49             else
50                 # Livestream
51                 @twitch.setChannel(data.id)
52         catch error
53             console.error(error)
55     pause: ->
56         try
57             @twitch.pause()
58             @paused = true
59         catch error
60             console.error(error)
62     play: ->
63         try
64             @twitch.play()
65             @paused = false
66         catch error
67             console.error(error)
69     seekTo: (time) ->
70         try
71             @twitch.seek(time)
72         catch error
73             console.error(error)
75     getTime: (cb) ->
76         try
77             cb(@twitch.getCurrentTime())
78         catch error
79             console.error(error)
81     setVolume: (volume) ->
82         try
83             @twitch.setVolume(volume)
84             if volume > 0
85                 @twitch.setMuted(false)
86         catch error
87             console.error(error)
89     getVolume: (cb) ->
90         try
91             if @twitch.isPaused()
92                 cb(0)
93             else
94                 cb(@twitch.getVolume())
95         catch error
96             console.error(error)
98     mapQuality: (quality) ->
99         switch String(quality)
100             when '1080' then 'chunked'
101             when '720' then 'high'
102             when '480' then 'medium'
103             when '360' then 'low'
104             when '240' then 'mobile'
105             when 'best' then 'chunked'
106             else ''