Update README.md
[KisSync.git] / player / twitch.coffee
blob4348036fd41bb24914c9a1a45fdb65ed9126b4d0
1 window.TWITCH_PARAMS_ERROR = 'The Twitch embed player now uses parameters which only
2 work if the following requirements are met: (1) The embedding website uses
3 HTTPS; (2) The embedding website uses the default port (443) and is accessed
4 via https://example.com instead of https://example.com:port.  I have no
5 control over this -- see <a href="https://discuss.dev.twitch.tv/t/twitch-embedded-player-migration-timeline-update/25588" rel="noopener noreferrer" target="_blank">this Twitch post</a>
6 for details'
8 window.TwitchPlayer = class TwitchPlayer extends Player
9     constructor: (data) ->
10         if not (this instanceof TwitchPlayer)
11             return new TwitchPlayer(data)
13         @setMediaProperties(data)
14         waitUntilDefined(window, 'Twitch', =>
15             waitUntilDefined(Twitch, 'Player', =>
16                 @init(data)
17             )
18         )
20     init: (data) ->
21         removeOld()
23         if location.hostname != location.host or location.protocol != 'https:'
24             alert = makeAlert(
25                 'Twitch API Parameters',
26                 window.TWITCH_PARAMS_ERROR,
27                 'alert-danger'
28             ).removeClass('col-md-12')
29             removeOld(alert)
30             @twitch = null
31             return
33         options =
34             parent: [location.hostname]
35             width: $('#ytapiplayer').width()
36             height: $('#ytapiplayer').height()
38         if data.type is 'tv'
39             # VOD
40             options.video = data.id
41         else
42             # Livestream
43             options.channel = data.id
45         @twitch = new Twitch.Player('ytapiplayer', options)
46         @twitch.addEventListener(Twitch.Player.READY, =>
47             @setVolume(VOLUME)
48             @twitch.setQuality(@mapQuality(USEROPTS.default_quality))
49             @twitch.addEventListener(Twitch.Player.PLAY, =>
50                 @paused = false
51                 if CLIENT.leader
52                     sendVideoUpdate()
53             )
54             @twitch.addEventListener(Twitch.Player.PAUSE, =>
55                 @paused = true
56                 if CLIENT.leader
57                     sendVideoUpdate()
58             )
59             @twitch.addEventListener(Twitch.Player.ENDED, =>
60                 if CLIENT.leader
61                     socket.emit('playNext')
62             )
63         )
65     load: (data) ->
66         @setMediaProperties(data)
67         try
68             if data.type is 'tv'
69                 # VOD
70                 @twitch.setVideo(data.id)
71             else
72                 # Livestream
73                 @twitch.setChannel(data.id)
74         catch error
75             console.error(error)
77     pause: ->
78         try
79             @twitch.pause()
80             @paused = true
81         catch error
82             console.error(error)
84     play: ->
85         try
86             @twitch.play()
87             @paused = false
88         catch error
89             console.error(error)
91     seekTo: (time) ->
92         try
93             @twitch.seek(time)
94         catch error
95             console.error(error)
97     getTime: (cb) ->
98         try
99             cb(@twitch.getCurrentTime())
100         catch error
101             console.error(error)
103     setVolume: (volume) ->
104         try
105             @twitch.setVolume(volume)
106             if volume > 0
107                 @twitch.setMuted(false)
108         catch error
109             console.error(error)
111     getVolume: (cb) ->
112         try
113             if @twitch.isPaused()
114                 cb(0)
115             else
116                 cb(@twitch.getVolume())
117         catch error
118             console.error(error)
120     mapQuality: (quality) ->
121         switch String(quality)
122             when '1080' then 'chunked'
123             when '720' then 'high'
124             when '480' then 'medium'
125             when '360' then 'low'
126             when '240' then 'mobile'
127             when 'best' then 'chunked'
128             else ''