This repository has been archived by the owner on Nov 18, 2023. It is now read-only.
/
youtube.go
848 lines (730 loc) · 23.6 KB
/
youtube.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
package youtube
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/aykevl/plaincast/apps/youtube/mp"
"github.com/aykevl/plaincast/config"
"github.com/aykevl/plaincast/log"
"github.com/nu7hatch/gouuid"
)
var logger = log.New("youtube", "log YouTube app")
// How often a new connection attempt should be done.
// With a starting delay of 500ms that exponentially increases, this is about 5
// minutes.
const RETRIES = 25
// Initial retry timeout in milliseconds. This timeout increases exponentially.
const RETRY_TIMEOUT = 500
// # Preventing race conditions & leaks
//
// There were a *lot* race conditions, but most have been fixed by now, using a
// new design. Many race conditions were triggered when Stop() was called.
// Stop() will hold a mutex for the `running` variable. As will the part
// receiving messages. As soon as Stop() is called, no more new messages will be
// received and run() will be stopped using a separate channel (`runQuit`).
// The overall exiting order looks like this:
// Stop() + bind() -> run() -> backend (via player) -> player -> playerEvents -> outgoingMessages
//
// These are also all goroutines that will exist (a few possible exceptions
// aside that will manage their lifetime themselves).
// The player will ensure proper synchronisation so it won't call more than one
// method on the backend at any time, thus backend.Quit() will also be
// synchronous. Then it will close the main playerEvents channel to signal to
// playerEvents no more signals will be sent.
// The YouTube app can play the audio track of YouTube videos, and is designed
// to be very lightweight (not running Chrome).
type YouTube struct {
systemName string
running bool
runningMutex sync.Mutex
// TODO split everything under here into a separate struct, so re-running
// the app won't clash with the previous run.
rid *RandomID // generates random numbers for outgoing messages
runQuit chan struct{}
uuid string
loungeToken string
sendMutex sync.Mutex
sid string
gsessionid string
aid int32 // int32 is thread-safe on ARM and Intel processors
mp *mp.MediaPlayer
mpMutex sync.Mutex // to quit the player safely
incomingMessages chan incomingMessage
outgoingMessages chan outgoingMessage
pairingCodes chan string
}
// JSON data structures for get_lounge_token_batch.
type loungeTokenBatchJson struct {
Screens []screenTokenJson "screens"
}
type screenTokenJson struct {
ScreenId string "screenId"
Expiration int64 "expiration"
LoungeToken string "loungeToken"
}
// JSON data structure for messages received over the message channel.
type incomingMessagesJson []incomingMessageJson
type incomingMessageJson []interface{}
type incomingMessage struct {
index int
command string
args map[string]string
}
// A single outgoing message, to be fed to the outgoingMessages channel.
type outgoingMessage struct {
command string
args map[string]string
}
// New returns a new YouTube object (app).
func New(systemName string) *YouTube {
yt := YouTube{}
yt.systemName = systemName
yt.runQuit = make(chan struct{})
return &yt
}
func (yt *YouTube) FriendlyName() string {
return "YouTube"
}
// Start starts the YouTube app asynchronously.
// Attaches a new device if the app has already started.
func (yt *YouTube) Start(postData string) {
yt.runningMutex.Lock()
running := yt.running
yt.runningMutex.Unlock()
arguments, err := url.ParseQuery(postData)
// TODO proper error handling
if err != nil {
panic(err)
}
if running {
// Only use `pairingCode`, ignore `v` and `t` arguments.
yt.pairingCodes <- arguments["pairingCode"][0]
} else {
yt.start(arguments)
}
}
// Quit stops this app if it is running.
func (yt *YouTube) Quit() {
// shut down everything about this app
yt.runningMutex.Lock()
defer yt.runningMutex.Unlock()
if !yt.running {
return
}
yt.running = false
yt.runQuit <- struct{}{}
}
func (yt *YouTube) init(arguments url.Values, stateChange chan mp.StateChange) {
var err error
yt.rid = NewRandomID()
yt.uuid, err = config.Get().GetString("apps.youtube.uuid", func() (string, error) {
uuid, err := uuid.NewV4()
if err != nil {
return "", err
}
return uuid.String(), nil
})
if err != nil {
panic(err)
}
yt.incomingMessages = make(chan incomingMessage, 5)
yt.outgoingMessages = make(chan outgoingMessage, 5)
// This is a goroutine that receives messages from YouTube and starts a
// goroutine to send messages to YouTube.
go yt.connect()
if pairingCodes, ok := arguments["pairingCode"]; ok {
go func() {
yt.pairingCodes <- pairingCodes[0]
}()
}
yt.mp = mp.New(stateChange)
video, ok := arguments["v"]
if ok && len(video[0]) > 0 {
videoId := video[0]
position, err := time.ParseDuration(arguments["t"][0] + "s")
if err != nil {
panic(err)
}
yt.mp.SetPlaystate([]string{videoId}, 0, position, "")
}
}
func (yt *YouTube) start(arguments url.Values) {
yt.runningMutex.Lock()
defer yt.runningMutex.Unlock()
yt.running = true
// Of all values, this one should not be initialized inside a goroutine
// because that's a race condition.
yt.pairingCodes = make(chan string)
go yt.run(arguments)
}
func (yt *YouTube) run(arguments url.Values) {
stateChange := make(chan mp.StateChange)
volumeChan := make(chan int, 1)
playlistChan := make(chan mp.PlaylistState)
nowPlayingChan := make(chan mp.PlaylistState, 1)
// nowPlayingChan will ask for a signal inside playerEvents.
// This goroutine handles all signals coming from the media player.
go yt.playerEvents(stateChange, volumeChan, playlistChan, nowPlayingChan)
yt.init(arguments, stateChange)
for {
select {
case message := <-yt.incomingMessages:
// Only print a message for less-verbose output.
switch message.command {
//case "remoteConnected", "remoteDisconnected":
default:
logger.Printf("command: %d %s %#v\n", message.index, message.command, message.args)
}
switch message.command {
case "remoteConnected":
logger.Printf("Remote connected: %s (%s)\n", message.args["name"], message.args["user"])
case "remoteDisconnected":
logger.Printf("Remote disconnected: %s (%s)\n", message.args["name"], message.args["user"])
case "loungeStatus":
case "getVolume":
yt.mp.RequestVolume(volumeChan)
case "setVolume":
delta, ok := message.args["delta"]
if ok {
delta, err := strconv.Atoi(delta)
if err != nil {
logger.Warnln("volume delta could not be parsed:", err)
break
}
yt.mp.ChangeVolume(delta, volumeChan)
} else {
volume, err := strconv.Atoi(message.args["volume"])
if err != nil {
logger.Warnln("volume could not be parsed:", err)
break
}
yt.mp.SetVolume(volume, volumeChan)
}
case "getPlaylist":
yt.mp.RequestPlaylist(playlistChan)
case "setPlaylist":
playlist := strings.Split(message.args["videoIds"], ",")
index, err := strconv.Atoi(message.args["currentIndex"])
if err != nil {
logger.Warnln("currentIndex could not be parsed:", err)
break
}
position, err := time.ParseDuration(message.args["currentTime"] + "s")
if err != nil {
if message.args["currentTime"] == "" {
logger.Warnln("currentTime was empty")
} else {
logger.Warnln("currentTime could not be parsed:", err)
}
// position is 0s, nothing too problematic
}
if index < 0 || len(playlist) == 0 || index >= len(playlist) {
logger.Warnln("setPlaylist got invalid parameters")
break
}
yt.mp.SetPlaystate(playlist, index, position, message.args["listId"])
case "updatePlaylist":
playlist := strings.Split(message.args["videoIds"], ",")
yt.mp.UpdatePlaylist(playlist, message.args["listId"])
yt.outgoingMessages <- outgoingMessage{"confirmPlaylistUpdate", map[string]string{"updated": "true"}}
case "setVideo":
videoId := message.args["videoId"]
position, err := time.ParseDuration(message.args["currentTime"] + "s")
if err != nil {
logger.Warnln("could not parse currentTime:", err)
break
}
yt.mp.SetVideo(videoId, position)
case "getNowPlaying":
yt.mp.RequestPlaylist(nowPlayingChan)
case "getSubtitlesTrack":
// Just send out an empty message. It looks like the Android
// YouTube client doesn't care too much about this message
// anyway. Usually `getSubtitlesTrack` is only sent on
// connection, and not asked (or sent) when switching videos,
// which is kinda odd to me. When a video is playing while this
// message is sent, the videoId is sent with it, and some other
// stuff like `languageCode` to indicate the currently playing
// subtitles track. Again, this is not updated when the video
// changes.
// No subtitles are visible anyway on a headless Chromecast
// installation, and the Android client doesn't seem to change
// it's behavior much when leaving out this message.
yt.outgoingMessages <- outgoingMessage{"onSubtitlesTrackChanged", map[string]string{"videoId": ""}}
case "pause":
yt.mp.Pause()
case "play":
yt.mp.Play()
case "seekTo":
position, err := time.ParseDuration(message.args["newTime"] + "s")
if err != nil {
logger.Warnln("could not parse newTime for seekTo:", err)
break
}
yt.mp.Seek(position)
case "stopVideo":
yt.mp.Stop()
}
case <-yt.runQuit:
// The YouTube app has been stopped.
yt.mpMutex.Lock()
yt.mp.Quit()
yt.mp = nil
yt.mpMutex.Unlock()
return
}
}
}
func (yt *YouTube) playerEvents(stateChange chan mp.StateChange, volumeChan chan int, playlistChan, nowPlayingChan chan mp.PlaylistState) {
for {
select {
case change, ok := <-stateChange:
if !ok {
// player has quit
close(yt.outgoingMessages)
return
}
if change.State == mp.STATE_BUFFERING || change.State == mp.STATE_STOPPED {
// Only access yt.mp when it is certain it isn't being quit.
// yt.mp is nil when it is being stopped.
yt.mpMutex.Lock()
if yt.mp != nil {
yt.mp.RequestPlaylist(nowPlayingChan)
}
yt.mpMutex.Unlock()
}
if change.State == mp.STATE_SEEKING {
// YouTube only knows buffering, not seeking
change.State = mp.STATE_BUFFERING
}
yt.outgoingMessages <- outgoingMessage{"onStateChange", map[string]string{
"currentTime": strconv.FormatFloat(change.Position.Seconds(), 'f', 3, 64),
"duration": strconv.FormatFloat(change.Duration.Seconds(), 'f', 3, 64),
"seekableStartTime": "0",
"seekableEndTime": strconv.FormatFloat(change.Duration.Seconds(), 'f', 3, 64),
"state": strconv.Itoa(int(change.State)),
}}
case volume := <-volumeChan:
yt.outgoingMessages <- outgoingMessage{"onVolumeChanged", map[string]string{
"volume": strconv.Itoa(volume),
"muted": "false",
}}
case ps := <-playlistChan:
message := outgoingMessage{"nowPlayingPlaylist", map[string]string{}}
if len(ps.Playlist) > 0 {
message.args["videoIds"] = strings.Join(ps.Playlist, ",")
message.args["videoId"] = ps.Playlist[ps.Index]
message.args["currentTime"] = strconv.FormatFloat(ps.Position.Seconds(), 'f', 3, 64)
message.args["duration"] = strconv.FormatFloat(ps.Duration.Seconds(), 'f', 3, 64)
message.args["state"] = strconv.Itoa(int(ps.State))
message.args["currentIndex"] = strconv.Itoa(ps.Index)
//message.args["listId"] = ""
}
yt.outgoingMessages <- message
case ps := <-nowPlayingChan:
message := outgoingMessage{"nowPlaying", map[string]string{}}
if len(ps.Playlist) > 0 {
message.args = map[string]string{
"videoId": ps.Playlist[ps.Index],
"currentTime": strconv.FormatFloat(ps.Position.Seconds(), 'f', 3, 64),
"duration": strconv.FormatFloat(ps.Duration.Seconds(), 'f', 3, 64),
"seekableStartTime": "0",
"seekableEndTime": strconv.FormatFloat(ps.Duration.Seconds(), 'f', 3, 64),
"state": strconv.Itoa(int(ps.State)),
"currentIndex": strconv.Itoa(ps.Index),
"listId": ps.ListId,
}
}
yt.outgoingMessages <- message
}
}
}
func (yt *YouTube) Running() bool {
yt.runningMutex.Lock()
defer yt.runningMutex.Unlock()
return yt.running
}
func (yt *YouTube) connect() {
yt.loadLoungeToken()
// Start sending/receiving channel.
// There should now be enough information.
yt.bind()
}
func (yt *YouTube) loadLoungeToken() {
params := url.Values{
"screen_ids": []string{yt.getScreenId()},
}
logger.Println("Getting lounge token batch...")
response, err := httpPostFormBody("https://www.youtube.com/api/lounge/pairing/get_lounge_token_batch", params)
if err != nil {
// TODO exit the app or something when this happens, don't panic
logger.Panic(err)
}
loungeTokenBatch := loungeTokenBatchJson{}
json.Unmarshal(response, &loungeTokenBatch)
yt.loungeToken = loungeTokenBatch.Screens[0].LoungeToken
}
func (yt *YouTube) getScreenId() string {
screenId, err := config.Get().GetString("apps.youtube.screenId", func() (string, error) {
logger.Println("Getting screen_id...")
response, err := httpGetBody("https://www.youtube.com/api/lounge/pairing/generate_screen_id")
return string(response), err
})
if err != nil {
// TODO use proper error handling
logger.Panic(err)
}
return screenId
}
func (yt *YouTube) openChannel(initial bool) *http.Response {
if initial {
yt.rid.Restart()
logger.Println("Getting first batch of messages")
}
doInitial := initial
// How often the connection was retried. Zero on normal operation.
// The retry timeout increases exponentially with the number of failures.
retries := 0
for {
yt.sendMutex.Lock()
aid := yt.aid
yt.sendMutex.Unlock()
var bindUrl string
// TODO more fields should be query-escaped
if !doInitial {
// normal reconnect
bindUrl = fmt.Sprintf("https://www.youtube.com/api/lounge/bc/bind?device=LOUNGE_SCREEN&id=%s&name=%s&loungeIdToken=%s&VER=8&RID=rpc&SID=%s&CI=0&AID=%d&gsessionid=%s&TYPE=xmlhttp&zx=%s",
yt.uuid, url.QueryEscape(yt.systemName), yt.loungeToken, yt.sid, aid, yt.gsessionid, zx())
} else if yt.sid == "" {
// first connection
bindUrl = fmt.Sprintf("https://www.youtube.com/api/lounge/bc/bind?device=LOUNGE_SCREEN&id=%s&name=%s&loungeIdToken=%s&VER=8&RID=%d&zx=%s",
yt.uuid, url.QueryEscape(yt.systemName), yt.loungeToken, yt.rid.Next(), zx())
} else {
// connection after a 400 Unknown SID error
bindUrl = fmt.Sprintf("https://www.youtube.com/api/lounge/bc/bind?device=LOUNGE_SCREEN&id=%s&name=%s&loungeIdToken=%s&OSID=%s&OAID=%d&VER=8&RID=%d&zx=%s",
yt.uuid, url.QueryEscape(yt.systemName), yt.loungeToken, yt.sid, aid, yt.rid.Next(), zx())
}
timeBeforeGet := time.Now()
var resp *http.Response
var err error
if doInitial {
params := url.Values{
"count": []string{"0"},
}
resp, err = http.PostForm(bindUrl, params)
} else {
resp, err = http.Get(bindUrl)
}
if err != nil {
if err == io.EOF {
if !yt.errorRetryTimeout(&retries, "EOF on bind", err) {
yt.Quit()
break
}
// reconnect
continue
} else if _, ok := err.(net.Error); ok && err.(net.Error).Timeout() {
logger.Warnln("timeout while connecting to message channel, retrying in 30s...")
time.Sleep(30 * time.Second)
continue
}
logger.Errln("Unknown error:", err)
yt.Quit()
break
}
if resp.Status == "400 Unknown SID" {
logger.Println("error:", resp.Status, ". Reconnecting the message channel...")
// Restart the Channel API connection
doInitial = true
continue
} else if resp.Status == "400 Bad Request" {
// Most likely this is also an "Unknown SID" error.
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Errln("could not read error message after 400 Bad Request:", err)
yt.Quit()
break
}
if strings.Index(string(buf), "<TITLE>Unknown SID</TITLE>") < 0 {
// Some other error
logger.Errln("response body:")
os.Stdout.Write(buf)
os.Stdout.Sync()
yt.Quit()
break
}
// Let's try again, similar to "400 Unknown SID"
logger.Println("error: 400 Bad Request (Unknown SID). Reconnecting the message channel...")
doInitial = true
continue
} else if resp.Status == "410 Gone" {
if !yt.errorRetryTimeout(&retries, "got 410 Gone on reconnect", nil) {
yt.Quit()
break
}
// Restart Channel API connection from the beginning
yt.sendMutex.Lock()
yt.sid = ""
yt.loadLoungeToken()
yt.sendMutex.Unlock()
doInitial = true
continue
} else if resp.StatusCode == 502 {
if !yt.errorRetryTimeout(&retries, "got HTTP error 502 on reconnect", nil) {
yt.Quit()
break
}
continue
} else if resp.StatusCode != 200 {
logger.Errln("HTTP error while connecting to message channel:", resp.Status)
// most likely the YouTube server gives back an error in HTML form
printHTTPError(resp)
yt.Quit()
break
}
if !initial {
latency := time.Now().Sub(timeBeforeGet) / time.Millisecond * time.Millisecond
logger.Println("Connected to message channel in", latency)
}
if doInitial {
yt.sendMutex.Lock()
yt.aid = -1
yt.sendMutex.Unlock()
}
return resp
}
return nil
}
func (yt *YouTube) bind() {
resp := yt.openChannel(true)
if resp == nil || yt.handleMessageStream(resp, true) {
return
}
// now yt.sid and yt.gsessionid should be defined, so sendMessages has
// enough information to start
go yt.sendMessages()
// Loop to keep the connection open
for {
resp := yt.openChannel(false)
if resp == nil {
break
}
if yt.handleMessageStream(resp, false) {
break
}
}
}
// errorRetryTimeout waits an exponential amount of time to retry something, or
// gives up after a number of retries. It returns true when it should retry, or
// false otherwise.
func (yt *YouTube) errorRetryTimeout(retries *int, message string, err error) bool {
*retries++
retryTimeout := time.Duration((*retries)*(*retries)) * RETRY_TIMEOUT * time.Millisecond
ending := ""
if err != nil {
ending = ": " + err.Error()
}
if *retries > RETRIES {
logger.Errf("%s, giving up%s\n", message, ending)
return false
}
logger.Warnf("%s, retrying in %s%s\n", message, retryTimeout, ending)
time.Sleep(retryTimeout)
return true
}
func (yt *YouTube) handleMessageStream(resp *http.Response, singleBatch bool) bool {
defer resp.Body.Close()
reader := bufio.NewReader(resp.Body)
for {
line, err := reader.ReadString('\n')
if err != nil {
if line == "" && err == io.EOF {
// The stream has terminated.
return false // try again
}
logger.Printf("error: %s (line: %#v)\n", err, line)
// try again
logger.Println("Trying to reconnect to message channel...")
return false
}
length, err := strconv.Atoi(line[:len(line)-1])
if err != nil {
panic(err)
}
data := make([]byte, length)
_, err = io.ReadFull(reader, data)
if err != nil {
panic(err)
}
messages := incomingMessagesJson{}
json.Unmarshal(data, &messages)
for _, message := range messages {
if yt.handleRawReceivedMessage(message) {
return true
}
}
if singleBatch {
break
}
}
return false
}
func (yt *YouTube) handleRawReceivedMessage(rawMessage incomingMessageJson) bool {
message := incomingMessage{}
message.index = int(rawMessage[0].(float64))
if message.index != int(yt.aid+1) {
if message.index <= int(yt.aid) {
logger.Warnln("old command:", message.index, message.command, message.args)
return false
} else {
logger.Errf("missing some messages, message number=%d, expected number=%d\n", message.index, yt.aid)
}
}
yt.aid = int32(message.index)
message.command = rawMessage[1].([]interface{})[0].(string)
args := make([]interface{}, len(rawMessage[1].([]interface{}))-1)
for i := 0; i < len(args); i++ {
args[i] = rawMessage[1].([]interface{})[i+1]
}
yt.runningMutex.Lock()
running := yt.running
yt.runningMutex.Unlock()
if !running {
return true
}
switch message.command {
case "noop":
// no-op, ignore
case "c":
if len(args) == 0 {
logger.Warnln("no argument to 'c' command")
break
}
sid, ok := args[0].(string)
if !ok {
logger.Warnln("SID is not a string")
} else {
yt.sid = sid
}
case "S":
if len(args) == 0 {
logger.Warnln("no argument to 'S' command")
break
}
gsessionid, ok := args[0].(string)
if !ok {
logger.Warnln("gsessionid is not a string")
} else {
yt.gsessionid = gsessionid
}
default:
if len(args) > 0 {
// convert map[string]interface{} into map[string]string
argsMap, ok := args[0].(map[string]interface{})
if !ok {
logger.Warnln("message values are not a map", message.command)
}
message.args = make(map[string]string, len(argsMap))
for k, v := range argsMap {
if s, ok := v.(string); !ok {
logger.Warnln("message", message.command, "does not have string value for key", k)
} else {
message.args[k] = s
}
}
}
yt.incomingMessages <- message
}
return false
}
func (yt *YouTube) sendMessages() {
queuedMessages := make([]outgoingMessage, 0, 3)
count := 0
var deadline time.Time
deadlineStart := make(chan struct{})
deadlineEnd := make(chan struct{})
go func() {
for _ = range deadlineStart {
// It looks like 10ms is a good default. HTTP latency appears to be
// relatively independent of the machine performance, so I guess it is bound
// by the speed of light...
time.Sleep(1 * time.Millisecond)
deadlineEnd <- struct{}{}
}
}()
defer close(deadlineStart)
for {
select {
case message, ok := <-yt.outgoingMessages:
if !ok {
// This is the sign the sendMessages goroutine should quit.
return
}
queuedMessages = append(queuedMessages, message)
if deadline.IsZero() {
deadline = time.Now()
deadlineStart <- struct{}{}
continue
}
case <-deadlineEnd:
values := url.Values{
"count": []string{strconv.Itoa(len(queuedMessages))}, // the amount of messages in this POST
"ofs": []string{strconv.Itoa(count)}, // which index the first message has
}
for i, message := range queuedMessages {
req := "req" + strconv.Itoa(i) + "_"
values.Set(req+"_sc", message.command)
for k, v := range message.args {
values.Set(req+k, v)
}
logger.Println("send msg:", message.command, message.args)
}
timeBeforeSend := time.Now()
retries := 0
for {
yt.sendMutex.Lock()
_, err := httpPostFormBody(fmt.Sprintf("https://www.youtube.com/api/lounge/bc/bind?device=LOUNGE_SCREEN&id=%s&name=%s&loungeIdToken=%s&VER=8&SID=%s&RID=%d&AID=%d&gsessionid=%s&zx=%s",
yt.uuid, url.QueryEscape(yt.systemName), yt.loungeToken, yt.sid, yt.rid.Next(), yt.aid, yt.gsessionid, zx()), values)
yt.sendMutex.Unlock()
if err != nil {
if !yt.errorRetryTimeout(&retries, "could not send message", err) {
yt.Quit()
return
}
continue
}
retries = 0
break
}
prepareLatency := timeBeforeSend.Sub(deadline) / time.Millisecond * time.Millisecond
httpLatency := time.Now().Sub(timeBeforeSend) / time.Millisecond * time.Millisecond
logger.Printf("messages sent: %d (prepare %s, http latency %s)\n", len(queuedMessages), prepareLatency, httpLatency)
count += len(queuedMessages)
queuedMessages = queuedMessages[:0]
deadline = time.Time{}
case pairingCode := <-yt.pairingCodes:
// Register the pairing code: that can be done after sending and
// receiving message channels have been set up.
logger.Println("Registering pairing code...")
params := url.Values{
"access_type": []string{"permanent"},
"pairing_code": []string{pairingCode},
"screen_id": []string{yt.getScreenId()},
}
_, err := httpPostFormBody("https://www.youtube.com/api/lounge/pairing/register_pairing_code", params)
if err != nil {
logger.Warnln("could not register pairing code:", err)
}
}
}
}