-
Notifications
You must be signed in to change notification settings - Fork 0
/
fc2.go
878 lines (804 loc) · 22.9 KB
/
fc2.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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
// Package fc2 provides a way to watch a FC2 channel.
package fc2
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"os"
"reflect"
"strings"
"sync"
"time"
"github.com/Darkness4/fc2-live-dl-go/hls"
"github.com/Darkness4/fc2-live-dl-go/notify/notifier"
"github.com/Darkness4/fc2-live-dl-go/state"
"github.com/Darkness4/fc2-live-dl-go/telemetry/metrics"
"github.com/Darkness4/fc2-live-dl-go/utils"
"github.com/Darkness4/fc2-live-dl-go/utils/try"
"github.com/Darkness4/fc2-live-dl-go/video/concat"
"github.com/Darkness4/fc2-live-dl-go/video/probe"
"github.com/Darkness4/fc2-live-dl-go/video/remux"
"github.com/coder/websocket"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
)
const (
tracerName = "fc2"
msgBufMax = 100
errBufMax = 10
commentBufMax = 100
)
var (
// ErrQualityNotExpected is returned when the quality is not expected.
ErrQualityNotExpected = errors.New("requested quality is not expected")
// ErrQualityNotAvailable is returned when the quality is not available.
ErrQualityNotAvailable = errors.New("requested quality is not available")
)
// FC2 is responsible to watch a FC2 channel.
type FC2 struct {
*http.Client
params *Params
channelID string
log *zerolog.Logger
}
// New creates a new FC2.
func New(client *http.Client, params *Params, channelID string) *FC2 {
if client == nil {
log.Panic().Msg("client is nil")
}
logger := log.With().Str("channelID", channelID).Logger()
return &FC2{
Client: client,
params: params,
channelID: channelID,
log: &logger,
}
}
// Watch watches the channel for any new live stream.
func (f *FC2) Watch(ctx context.Context) (*GetMetaData, error) {
f.log.Info().Any("params", f.params).Msg("watching channel")
ls := NewLiveStream(f.Client, f.channelID)
if online, err := ls.IsOnline(ctx); err != nil {
return nil, err
} else if !online {
if !f.params.WaitForLive {
return nil, ErrLiveStreamNotOnline
}
if err := ls.WaitForOnline(ctx, f.params.WaitPollInterval); err != nil {
return nil, err
}
}
ctx, span := otel.Tracer(tracerName).
Start(ctx, "fc2.Watch", trace.WithAttributes(attribute.String("channelID", f.channelID),
attribute.Stringer("params", f.params),
))
defer span.End()
metrics.TimeStartRecordingDeferred(f.channelID)
span.AddEvent("getting metadata")
meta, err := ls.GetMeta(ctx, WithRefetch())
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return nil, err
}
jsonMeta, err := json.MarshalIndent(meta, "", " ")
if err != nil {
span.SetAttributes(
attribute.String("metadata", string(jsonMeta)),
)
}
span.AddEvent("preparing files")
state.DefaultState.SetChannelState(
f.channelID,
state.DownloadStatePreparingFiles,
state.WithLabels(f.params.Labels),
)
if err := notifier.NotifyPreparingFiles(ctx, f.channelID, f.params.Labels, meta); err != nil {
log.Err(err).Msg("notify failed")
}
fnameInfo, err := PrepareFile(f.params.OutFormat, meta, f.params.Labels, "info.json")
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return meta, err
}
fnameThumb, err := PrepareFile(f.params.OutFormat, meta, f.params.Labels, "png")
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return meta, err
}
fnameStream, err := PrepareFile(f.params.OutFormat, meta, f.params.Labels, "ts")
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return meta, err
}
fnameChat, err := PrepareFile(f.params.OutFormat, meta, f.params.Labels, "fc2chat.json")
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return meta, err
}
fnameMuxedExt := strings.ToLower(f.params.RemuxFormat)
fnameMuxed, err := PrepareFile(f.params.OutFormat, meta, f.params.Labels, fnameMuxedExt)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return meta, err
}
fnameAudio, err := PrepareFile(f.params.OutFormat, meta, f.params.Labels, "m4a")
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return meta, err
}
nameConcatenated, err := FormatOutput(
f.params.OutFormat,
meta,
f.params.Labels,
"combined."+fnameMuxedExt,
)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return meta, err
}
nameConcatenatedPrefix := strings.TrimSuffix(
nameConcatenated,
".combined."+fnameMuxedExt,
)
nameAudioConcatenated, err := FormatOutput(
f.params.OutFormat,
meta,
f.params.Labels,
"combined.m4a",
)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return meta, err
}
nameAudioConcatenatedPrefix := strings.TrimSuffix(
nameAudioConcatenated,
".combined.m4a",
)
if f.params.WriteInfoJSON {
f.log.Info().Str("fnameInfo", fnameInfo).Msg("writing info json")
func() {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(meta); err != nil {
f.log.Error().Err(err).Msg("failed to encode meta in info json")
return
}
if err := os.WriteFile(fnameInfo, buf.Bytes(), 0o755); err != nil {
f.log.Error().Err(err).Msg("failed to write meta in info json")
return
}
}()
}
if f.params.WriteThumbnail {
f.log.Info().Str("fnameThumb", fnameThumb).Msg("writing thunnail")
func() {
url := meta.ChannelData.Image
resp, err := f.Get(url)
if err != nil {
f.log.Error().Err(err).Msg("failed to fetch thumbnail")
return
}
defer resp.Body.Close()
out, err := os.Create(fnameThumb)
if err != nil {
f.log.Error().Err(err).Msg("failed to open thumbnail file")
return
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
f.log.Error().Err(err).Msg("failed to download thumbnail file")
return
}
}()
}
span.AddEvent("downloading")
state.DefaultState.SetChannelState(
f.channelID,
state.DownloadStateDownloading,
state.WithLabels(f.params.Labels),
state.WithExtra(map[string]interface{}{
"metadata": meta,
}),
)
if err := notifier.NotifyDownloading(
ctx,
f.channelID,
f.params.Labels,
meta,
); err != nil {
log.Err(err).Msg("notify failed")
}
wsURL, err := ls.GetWebSocketURL(ctx)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return meta, err
}
errWs := f.HandleWS(ctx, wsURL, fnameStream, fnameChat)
if errWs != nil {
span.RecordError(errWs)
span.SetStatus(codes.Error, errWs.Error())
f.log.Error().Err(errWs).Msg("fc2 finished with error")
}
span.AddEvent("post-processing")
end := metrics.TimeStartRecording(
ctx,
metrics.PostProcessing.CompletionTime,
time.Second,
metric.WithAttributes(
attribute.String("channel_id", f.channelID),
),
)
defer end()
metrics.PostProcessing.Runs.Add(ctx, 1, metric.WithAttributes(
attribute.String("channel_id", f.channelID),
))
state.DefaultState.SetChannelState(
f.channelID,
state.DownloadStatePostProcessing,
state.WithLabels(f.params.Labels),
state.WithExtra(map[string]interface{}{
"metadata": meta,
}),
)
if err := notifier.NotifyPostProcessing(
ctx,
f.channelID,
f.params.Labels,
meta,
); err != nil {
log.Err(err).Msg("notify failed")
}
f.log.Info().Msg("post-processing...")
var remuxErr error
probeErr := probe.Do([]string{fnameStream}, probe.WithQuiet())
if probeErr != nil {
f.log.Error().Err(probeErr).Msg("ts is unreadable by ffmpeg")
if f.params.DeleteCorrupted {
if err := os.Remove(fnameStream); err != nil {
f.log.Error().
Str("path", fnameStream).
Err(err).
Msg("failed to remove corrupted file")
}
}
}
if f.params.Remux && probeErr == nil {
f.log.Info().Str("output", fnameMuxed).Str("input", fnameStream).Msg(
"remuxing stream...",
)
remuxErr = remux.Do(ctx, fnameMuxed, fnameStream)
if remuxErr != nil {
f.log.Error().Err(remuxErr).Msg("ffmpeg remux finished with error")
metrics.PostProcessing.Errors.Add(ctx, 1, metric.WithAttributes(
attribute.String("channel_id", f.channelID),
))
}
}
var extractAudioErr error
// Extract audio if remux on, or when concat is off.
if f.params.ExtractAudio && (!f.params.Concat || f.params.Remux) && probeErr == nil {
f.log.Info().Str("output", fnameAudio).Str("input", fnameStream).Msg(
"extrating audio...",
)
extractAudioErr = remux.Do(ctx, fnameAudio, fnameStream, remux.WithAudioOnly())
if extractAudioErr != nil {
f.log.Error().Err(extractAudioErr).Msg("ffmpeg audio extract finished with error")
metrics.PostProcessing.Errors.Add(ctx, 1, metric.WithAttributes(
attribute.String("channel_id", f.channelID),
))
}
}
// Concat
if f.params.Concat {
f.log.Info().Str("output", nameConcatenated).Str("prefix", nameConcatenatedPrefix).Msg(
"concatenating stream...",
)
concatOpts := []concat.Option{
concat.IgnoreExtension(),
}
if concatErr := concat.WithPrefix(ctx, f.params.RemuxFormat, nameConcatenatedPrefix, concatOpts...); concatErr != nil {
f.log.Error().Err(concatErr).Msg("ffmpeg concat finished with error")
metrics.PostProcessing.Errors.Add(ctx, 1, metric.WithAttributes(
attribute.String("channel_id", f.channelID),
))
}
if f.params.ExtractAudio {
f.log.Info().
Str("output", nameAudioConcatenated).
Str("prefix", nameAudioConcatenatedPrefix).
Msg(
"concatenating audio stream...",
)
concatOpts = append(concatOpts, concat.WithAudioOnly())
if concatErr := concat.WithPrefix(ctx, "m4a", nameAudioConcatenatedPrefix, concatOpts...); concatErr != nil {
f.log.Error().Err(concatErr).Msg("ffmpeg concat finished with error")
metrics.PostProcessing.Errors.Add(ctx, 1, metric.WithAttributes(
attribute.String("channel_id", f.channelID),
))
}
}
}
// Delete intermediates
if !f.params.KeepIntermediates && f.params.Remux &&
probeErr == nil &&
remuxErr == nil &&
extractAudioErr == nil {
f.log.Info().Str("file", fnameStream).Msg("delete intermediate files")
if err := os.Remove(fnameStream); err != nil {
f.log.Error().Err(err).Msg("couldn't delete intermediate file")
metrics.PostProcessing.Errors.Add(ctx, 1, metric.WithAttributes(
attribute.String("channel_id", f.channelID),
))
}
}
span.AddEvent("done")
f.log.Info().Msg("done")
return meta, errWs
}
// HandleWS handles the websocket connection.
//
// This function blocks until the websocket connection is closed, i.e., until
// the stream ends.
func (f *FC2) HandleWS(
ctx context.Context,
wsURL string,
fnameStream string,
fnameChat string,
) error {
ctx, span := otel.Tracer(tracerName).Start(ctx, "fc2.HandleWS", trace.WithAttributes(
attribute.String("channel_id", f.channelID),
attribute.String("ws_url", wsURL),
attribute.String("fname_stream", fnameStream),
attribute.String("fname_chat", fnameChat),
))
defer span.End()
msgChan := make(chan *WSResponse, msgBufMax)
var commentChan chan *Comment
if f.params.WriteChat {
commentChan = make(chan *Comment, commentBufMax)
}
ws := NewWebSocket(f.Client, wsURL, 30*time.Second)
conn, err := ws.Dial(ctx)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return err
}
defer conn.Close(websocket.StatusNormalClosure, "ended connection")
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
err := ws.HeartbeatLoop(ctx, conn, msgChan)
if err == nil {
f.log.Panic().Msg(
"undefined behavior, heartbeat finished with nil, the download MUST finish with io.EOF or Canceled",
)
}
if err == io.EOF {
f.log.Info().Msg("healthcheck finished")
} else if errors.Is(err, context.Canceled) {
f.log.Info().Msg("healthcheck canceled")
} else {
f.log.Error().Err(err).Msg("healthcheck failed")
}
return err
})
g.Go(func() error {
err := ws.Listen(ctx, conn, msgChan, commentChan)
if err == nil {
f.log.Panic().Msg(
"undefined behavior, ws listen finished with nil, the ws listen MUST finish with io.EOF",
)
}
if err == io.EOF || err == ErrWebSocketStreamEnded {
f.log.Info().Msg("ws listen finished")
return io.EOF
} else if errors.Is(err, context.Canceled) {
f.log.Info().Msg("ws listen canceled")
} else {
f.log.Error().Err(err).Msg("ws listen failed")
}
return err
})
g.Go(func() error {
ctx, span := otel.Tracer(tracerName).
Start(ctx, "fc2.HandleWS.download", trace.WithAttributes(
attribute.String("channel_id", f.channelID),
attribute.String("ws_url", wsURL),
))
defer span.End()
playlistChan := make(chan *Playlist)
go func() {
<-ctx.Done()
close(playlistChan)
}()
// Playlist fetching and quality upgrade loop
//
// It exits after fetching the first playlist if quality upgrade is not allowed.
go func() {
ticker := time.NewTicker(f.params.PollQualityUpgradeInterval)
defer ticker.Stop()
ctx, span := otel.Tracer(tracerName).
Start(ctx, "fc2.FetchPlaylistAndQualityUpgrade", trace.WithAttributes(
attribute.String("channel_id", f.channelID),
))
defer span.End()
downloading := false
for {
playlist, err := f.FetchPlaylist(ctx, ws, conn, msgChan, !downloading)
if err == nil {
// Everything is normal
playlistChan <- playlist
return
}
if !downloading {
if errors.Is(err, ErrQualityNotExpected) {
f.log.Warn().
Any("playlist", playlist).
Msg("quality is not expected, will retry during download")
// Use the best quality available
playlistChan <- playlist
downloading = true
} else {
f.log.Error().Err(err).Msg("failed to fetch playlist")
}
}
if !f.params.AllowQualityUpgrade {
// Exit because we are not allowed to upgrade, therefore, we will not retry.
return
}
select {
case <-ticker.C:
continue
case <-ctx.Done():
f.log.Info().Msg("cancelling quality upgrade loop")
return
}
}
}()
err = f.downloadStream(ctx, playlistChan, fnameStream)
if err == nil {
f.log.Panic().Msg(
"undefined behavior, downloader finished with nil, the download MUST finish with io.EOF",
)
}
if err == io.EOF {
f.log.Info().Msg("download stream finished")
} else if errors.Is(err, context.Canceled) {
f.log.Info().Msg("download stream canceled")
} else {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
f.log.Error().Err(err).Msg("download stream failed")
}
return err
})
if f.params.WriteChat {
g.Go(func() error {
err := f.downloadChat(ctx, commentChan, fnameChat)
if err == nil {
f.log.Panic().Msg(
"undefined behavior, chat downloader finished with nil, the chat downloader MUST finish with io.EOF",
)
}
if err == io.EOF {
f.log.Info().Msg("download chat finished")
} else if errors.Is(err, context.Canceled) {
f.log.Info().Msg("download chat canceled")
} else {
f.log.Error().Err(err).Msg("download chat failed")
}
return err
})
}
ticker := time.NewTicker(5 * time.Second) // print channel length every 5 seconds
defer ticker.Stop()
for {
select {
// Check for overflow
case <-ticker.C:
if len(msgChan) == msgBufMax {
f.log.Error().Msg("msgChan overflow, flushing...")
utils.Flush(msgChan)
}
if f.params.WriteChat {
if lenCommentChan := len(commentChan); lenCommentChan == commentBufMax {
f.log.Error().Msg("commentChan overflow, flushing...")
utils.Flush(commentChan)
}
}
// Stop at the first error
case <-ctx.Done():
f.log.Info().Msg("cancelling goroutine group...")
err = g.Wait()
f.log.Info().Msg("cancelled goroutine group.")
if err == io.EOF {
return nil
}
span.RecordError(err)
return err
}
}
}
func (f *FC2) downloadStream(ctx context.Context, playlists <-chan *Playlist, fName string) error {
ctx, span := otel.Tracer(tracerName).Start(ctx, "fc2.downloadStream", trace.WithAttributes(
attribute.String("channel_id", f.channelID),
attribute.String("fname", fName),
))
defer span.End()
file, err := os.Create(fName)
if err != nil {
return err
}
defer file.Close()
errChan := make(chan error, errBufMax)
// Variables used to save old downloader and checkpoint in case of quality upgrade.
var (
currentCtx context.Context
currentCancel context.CancelFunc
// Channel used to assure only one downloader can be launched
doneChan chan struct{}
// Checkpoint for the downloader when switching playlists
checkpoint = hls.DefaultCheckpoint()
checkpointMu sync.Mutex
)
playlistLoop:
for {
select {
// Received a new playlist URL
case playlist, ok := <-playlists:
if !ok {
// Playlist channel closed, meaning the stream ended.
if currentCancel != nil {
currentCancel()
}
continue
}
if playlist == nil {
panic("nil playlist")
}
f.log.Info().Any("playlist", playlist).Msg("received new HLS info")
span.AddEvent("playlist received", trace.WithAttributes(
attribute.String("url", playlist.URL),
attribute.Int("mode", playlist.Mode),
))
metrics.TimeEndRecording(ctx, metrics.Downloads.InitTime, f.channelID, metric.WithAttributes(
attribute.String("channel_id", f.channelID),
))
downloader := hls.NewDownloader(
f.Client,
f.log,
f.params.PacketLossMax,
playlist.URL,
)
// Is there a downloader running?
if currentCancel != nil {
// There is a downloader running, we need to switch to the new playlist.
// To avoid a cut off in the recording, we probe the playlist URL before downloading.
f.log.Info().Msg("QUALITY UPGRADE! Wait for new stream to be ready...")
span.AddEvent("quality upgrade")
for { // Healthcheck the new playlist.
ok, err := downloader.Probe(ctx)
if err != nil {
f.log.Error().Err(err).Msg("failed to probe playlist, won't redownload")
continue playlistLoop
}
if ok {
break
}
time.Sleep(5 * time.Second)
}
// Cancel the old downloader.
span.AddEvent("stream alive, cancel old downloader")
currentCancel()
f.log.Info().Msg("switching downloader seamlessly...")
select {
case <-doneChan:
log.Info().Msg("downloader switched")
case <-time.After(30 * time.Second):
log.Fatal().Msg("couldn't switch downloader because of a deadlock")
}
}
currentCtx, currentCancel = context.WithCancel(ctx)
doneChan = make(chan struct{}, 1)
// Download thread.
go func(ctx context.Context) {
log.Info().Msg("downloader thread started")
defer func() {
close(doneChan)
}()
span.AddEvent("downloading")
end := metrics.TimeStartRecording(ctx, metrics.Downloads.CompletionTime, time.Second, metric.WithAttributes(
attribute.String("channel_id", f.channelID),
),
)
defer end()
metrics.Downloads.Runs.Add(ctx, 1, metric.WithAttributes(
attribute.String("channel_id", f.channelID),
))
// Actually download. It will block until the download is finished.
checkpointMu.Lock()
checkpoint, err = downloader.Read(ctx, file, checkpoint)
checkpointMu.Unlock()
if err != nil {
errChan <- err
}
f.log.Info().Msg("downloader thread finished")
}(currentCtx)
case err := <-errChan:
if err == nil {
f.log.Panic().Msg(
"undefined behavior, downloader finished with nil, the download MUST finish with io.EOF",
)
}
if err == io.EOF {
f.log.Info().Msg("downloader finished reading")
} else if errors.Is(err, context.Canceled) {
select {
case <-ctx.Done():
f.log.Info().Msg("downloader cancelled by parent context")
// Parent context was cancelled, we should return.
default:
// Parent context was not cancelled, we should continue.
f.log.Info().Msg("downloader cancelled")
continue
}
} else {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
f.log.Error().Err(err).Msg("downloader failed with error")
}
if currentCancel != nil {
currentCancel()
}
return err
}
}
}
func removeDuplicatesComment(input <-chan *Comment) <-chan *Comment {
output := make(chan *Comment)
var last *Comment
go func() {
defer close(output)
for new := range input {
if !reflect.DeepEqual(new, last) {
output <- new
}
last = new
}
}()
return output
}
func (f *FC2) downloadChat(ctx context.Context, commentChan <-chan *Comment, fName string) error {
file, err := os.Create(fName)
if err != nil {
return err
}
filteredCommentChannel := removeDuplicatesComment(commentChan)
// Write to file
for {
select {
case data, ok := <-filteredCommentChannel:
if !ok {
f.log.Error().Msg("writing chat failed, channel was closed")
return io.EOF
}
if data == nil {
continue
}
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
_, err = file.Write(jsonData)
if err != nil {
return err
}
_, err = file.Write([]byte("\n"))
if err != nil {
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
}
func playlistsSummary(pp []Playlist) []struct {
Quality Quality
Latency Latency
} {
summary := make([]struct {
Quality Quality
Latency Latency
}, len(pp))
for i, p := range pp {
summary[i] = struct {
Quality Quality
Latency Latency
}{
Quality: QualityFromMode(p.Mode),
Latency: LatencyFromMode(p.Mode),
}
}
return summary
}
// FetchPlaylist fetches the playlist.
func (f *FC2) FetchPlaylist(
ctx context.Context,
ws *WebSocket,
conn *websocket.Conn,
msgChan chan *WSResponse,
verbose bool,
) (*Playlist, error) {
ctx, span := otel.Tracer(tracerName).Start(ctx, "fc2.FetchPlaylist", trace.WithAttributes(
attribute.String("channel_id", f.channelID),
))
defer span.End()
expectedMode := int(f.params.Quality) + int(f.params.Latency) - 1
maxTries := f.params.WaitForQualityMaxTries
res, err := try.DoWithContextTimeoutWithResult(
ctx,
maxTries,
time.Second,
15*time.Second,
verbose,
func(ctx context.Context, try int) (*Playlist, error) {
hlsInfo, err := ws.GetHLSInformation(ctx, conn, msgChan)
if err != nil {
span.RecordError(err)
return nil, err
}
playlists := SortPlaylists(ExtractAndMergePlaylists(hlsInfo))
playlist, err := GetPlaylistOrBest(
playlists,
expectedMode,
)
if err != nil {
span.RecordError(err)
return nil, err
}
if expectedMode != playlist.Mode {
if try == maxTries-1 && verbose {
if verbose {
f.log.Warn().
Stringer("expected_quality", QualityFromMode(expectedMode)).
Stringer("expected_latency", LatencyFromMode(expectedMode)).
Stringer("got_quality", QualityFromMode(playlist.Mode)).
Stringer("got_latency", LatencyFromMode(playlist.Mode)).
Any("available_playlists", playlistsSummary(playlists)).
Msg("requested quality is not available, will do...")
}
span.RecordError(ErrQualityNotExpected)
return playlist, ErrQualityNotExpected
}
span.RecordError(ErrQualityNotAvailable)
return nil, ErrQualityNotAvailable
}
return playlist, nil
},
)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return res, err
}
return res, nil
}