/
socket.go
339 lines (310 loc) · 8.63 KB
/
socket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Jellycli is a terminal music player for Jellyfin.
* Copyright (C) 2020 Tero Vierimaa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package jellyfin
import (
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"math"
"net/url"
"strconv"
"strings"
"syscall"
"time"
"tryffel.net/go/jellycli/interfaces"
"tryffel.net/go/jellycli/models"
)
const (
pongTimeout = 10 * time.Second
pingPeriod = (pongTimeout * 9) / 10
)
func (jf *Jellyfin) connectSocket() error {
if jf.token == "" {
return fmt.Errorf("no access token")
}
u, err := url.Parse(jf.host)
host := u.Host + u.Path
scheme := "wss"
if u.Scheme == "http" {
logrus.Info("Websocket encryption disabled")
scheme = "ws"
}
dialer := websocket.Dialer{
Proxy: nil,
HandshakeTimeout: time.Second * 10,
}
logrus.Debug("connecting websocket to ", host)
socket, _, err := dialer.Dial(
fmt.Sprintf("%s://%s/socket?api_key=%s&deviceId=%s", scheme, host, jf.token, jf.DeviceId), nil)
if err != nil {
jf.socketState = socketDisconnected
return fmt.Errorf("websocket connection failed: %v", err)
}
jf.socketLock.Lock()
defer jf.socketLock.Unlock()
logrus.Debugf("websocket connected")
jf.socket = socket
err = jf.socket.SetReadDeadline(time.Now().Add(pongTimeout))
if err != nil {
logrus.Errorf("set socket read deadline: %v", err)
}
jf.socket.SetPongHandler(func(string) error {
logrus.Trace("Websocket received pong")
return jf.socket.SetReadDeadline(time.Now().Add(pongTimeout))
})
jf.socketState = socketConnected
return nil
}
func (jf *Jellyfin) handleSocketOutbount(msg interface{}) error {
if jf.socket == nil {
return fmt.Errorf("socket not open")
}
return jf.socket.WriteJSON(msg)
}
// read next message from socket in blocking mode. Messages are read as long as socket connection is ok
func (jf *Jellyfin) readMessage() {
if jf.WebsocketOk() {
msgType, buff, err := jf.socket.ReadMessage()
if err != nil {
jf.handleSocketError(err)
}
if msgType == websocket.TextMessage {
err = jf.parseInboudMessage(&buff)
jf.handleSocketError(err)
}
go jf.readMessage()
}
}
type webSocketInboudMsg struct {
MessageType string `json:"MessageType"`
Data interface{} `json:"Data"`
}
type controlCommand struct {
Name string `json:"Name"`
Arguments interface{}
}
func (jf *Jellyfin) parseInboudMessage(buff *[]byte) error {
msg := webSocketInboudMsg{}
err := json.Unmarshal(*buff, &msg)
if err != nil {
logrus.Errorf("Parse json: %v", err)
str := string(*buff)
logrus.Error(str)
return fmt.Errorf("parse json: %v, body: %s", err, str)
}
dataMap, ok := msg.Data.(map[string]interface{})
if !ok {
if msg.MessageType != "ForceKeepAlive" {
logrus.Errorf("Unknown websocket event: %v", msg)
}
return nil
}
cmd := strings.ToLower(msg.MessageType)
if cmd == "generalcommand" {
name := dataMap["Name"]
ar := dataMap["Arguments"]
args, ok := ar.(map[string]interface{})
if ok {
switch name {
case "SetVolume":
vol := args["Volume"]
volume, err := strconv.Atoi(vol.(string))
if err != nil {
logrus.Error("Invalid volume parameter")
} else {
volume := interfaces.AudioVolume(volume)
jf.player.SetVolume(volume)
}
case "ToggleMute":
jf.player.ToggleMute()
default:
logrus.Warning("unknown socket command: ", name)
}
} else {
logrus.Error("unexpected command format from websocket, expected general command args map[string]interface, got", jf)
}
} else if cmd == "playstate" {
rawCmd := dataMap["Command"]
cmd, ok := rawCmd.(string)
if ok {
err = jf.pushCommand(cmd)
}
} else if cmd == "play" {
var items []string
if i, ok := dataMap["ItemIds"].([]interface{}); ok {
for _, v := range i {
if id, ok := v.(string); ok {
items = append(items, id)
} else {
logrus.Errorf("remote play, item id is not string: %s", v)
}
}
} else {
logrus.Error("Received play command, but queue ids are not array. command: ", msg.Data)
}
index, ok := dataMap["StartIndex"].(float64)
startIndex := 0
if ok {
startIndex = int(index)
}
command, ok := dataMap["PlayCommand"].(string)
if !ok {
logrus.Error("Received play command, but command is not string: ", msg.Data)
} else {
go jf.pushSongsToQueue(items[startIndex:], command)
}
}
return err
}
func (jf *Jellyfin) pushCommand(cmd string) error {
if jf.player == nil {
return nil
}
switch cmd {
case "PlayPause":
jf.player.PlayPause()
case "NextTrack":
jf.player.Next()
case "PreviousTrack":
jf.player.Previous()
case "Pause":
jf.player.Pause()
case "Unpause":
jf.player.Continue()
case "StopMedia":
jf.player.StopMedia()
jf.queue.ClearQueue(true)
case "Stop":
jf.player.StopMedia()
jf.queue.ClearQueue(true)
default:
logrus.Info("Unknown websocket playstate command: ", cmd)
}
return nil
}
// handle errors and try reconnecting
func (jf *Jellyfin) handleSocketError(err error) {
if err == nil {
return
}
jf.socketLock.Lock()
defer jf.socketLock.Unlock()
if jf.socketState == socketReConnecting {
return
}
awaitReconnect := func(reason string) {
if jf.socketState == socketConnected {
jf.socketState = socketAwaitsReconnecting
logrus.Warning("Websocket closed: ", reason)
}
}
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
awaitReconnect("going away")
} else if websocket.IsUnexpectedCloseError(err, websocket.CloseAbnormalClosure) {
awaitReconnect("abnormal closure")
} else if errors.Is(err, syscall.ECONNABORTED) {
// happens when disconnected from network, e.g. computer on sleep
awaitReconnect("connection aborted")
} else if strings.Contains(err.Error(), "i/o timeout") {
// happens when disconnected from network, e.g. computer on sleep
awaitReconnect("io timeout")
} else {
logrus.Errorf("unknown socket err: %v", err)
}
}
// WebsocketOk returns true if websocket connection is ok
func (jf *Jellyfin) WebsocketOk() bool {
jf.socketLock.RLock()
defer jf.socketLock.RUnlock()
return jf.socketState == socketConnected
}
// try reconnecting socket. Return true if success
func (jf *Jellyfin) reconnectSocket() bool {
jf.socketLock.Lock()
jf.socketState = socketReConnecting
var err error
if jf.socket != nil {
err = jf.socket.Close()
if err != nil {
logrus.Debugf("reconnect socket: close socket: %v", err)
} else {
jf.socket = nil
}
}
jf.socketLock.Unlock()
err = jf.connectSocket()
if err != nil {
logrus.Debugf("reconnect socket: %v", err)
return false
}
logrus.Warning("Websocket reconnected")
return true
}
// push songs to queue.
func (jf *Jellyfin) pushSongsToQueue(items []string, mode string) {
ids := []models.Id{}
for _, v := range items {
ids = append(ids, models.Id(v))
}
var songs []*models.Song
var err error
// server does not accept too long id list (> 15 ids), so we need to split large queries
if len(ids) > 15 {
rounds := int(math.Ceil(float64(len(ids)) / 15))
logrus.Infof("Too many songs for single query, split query: %d total, %d queries", len(ids), rounds)
for i := 0; i < rounds; i++ {
from := i * 15
to := (i + 1) * 15
if to > len(ids) {
to = len(ids)
}
logrus.Debugf("Download songs [%d, %d]", from, to)
s, err := jf.GetSongsById(ids[from:to])
if err != nil {
logrus.Errorf("download songs: %v", err)
}
songs = append(songs, s...)
}
if len(songs) != len(ids) {
logrus.Errorf("some songs were not downloaded: expect %d, got %d", len(ids), len(songs))
}
} else {
songs, err = jf.GetSongsById(ids)
}
if err != nil {
logrus.Errorf("remote control: add songs to queue: get songs from ids: %v", err)
return
}
logrus.Debug("received play event: ", mode)
// some modes are swapped in other clients, use those for consistency
if mode == "PlayNow" {
jf.player.StopMedia()
jf.queue.ClearQueue(true)
jf.queue.PlayNext(songs)
} else if mode == "PlayLast" {
//} else if mode == "PlayNext" {
jf.queue.PlayNext(songs)
} else if mode == "PlayNext" {
//} else if mode == "PlayLast" {
jf.queue.AddSongs(songs)
} else {
logrus.Errorf("unknown remote play mode: %s", mode)
}
}