@@ -3,6 +3,10 @@ package player
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "net/http"
7
+ "net/url"
8
+ "time"
9
+
6
10
librespot "github.com/devgianlu/go-librespot"
7
11
"github.com/devgianlu/go-librespot/audio"
8
12
"github.com/devgianlu/go-librespot/output"
@@ -11,16 +15,19 @@ import (
11
15
"github.com/devgianlu/go-librespot/spclient"
12
16
"github.com/devgianlu/go-librespot/vorbis"
13
17
"golang.org/x/exp/rand"
14
- "net/http"
15
- "net/url"
16
- "time"
17
18
)
18
19
19
- const SampleRate = 44100
20
- const Channels = 2
20
+ const (
21
+ SampleRate = 44100
22
+ Channels = 2
23
+ )
21
24
22
25
const MaxStateVolume = 65535
23
26
27
+ const DisableCheckMediaRestricted = true
28
+
29
+ const CdnUrlQuarantineDuration = 15 * time .Minute
30
+
24
31
type Player struct {
25
32
log librespot.Logger
26
33
@@ -33,6 +40,8 @@ type Player struct {
33
40
audioKey * audio.KeyProvider
34
41
events EventManager
35
42
43
+ cdnQuarantine map [string ]time.Time
44
+
36
45
newOutput func (source librespot.Float32Reader , volume float32 ) (output.Output , error )
37
46
38
47
cmd chan playerCmd
@@ -141,6 +150,7 @@ func NewPlayer(opts *Options) (*Player, error) {
141
150
sp : opts .Spclient ,
142
151
audioKey : opts .AudioKey ,
143
152
events : opts .Events ,
153
+ cdnQuarantine : make (map [string ]time.Time ),
144
154
normalisationEnabled : opts .NormalisationEnabled ,
145
155
normalisationUseAlbumGain : opts .NormalisationUseAlbumGain ,
146
156
normalisationPregain : opts .NormalisationPregain ,
@@ -414,8 +424,6 @@ func (p *Player) SetSecondaryStream(source librespot.AudioSource) {
414
424
<- resp
415
425
}
416
426
417
- const DisableCheckMediaRestricted = true
418
-
419
427
func (p * Player ) httpChunkedReaderFromStorageResolve (log librespot.Logger , client * http.Client , storageResolve * downloadpb.StorageResolveResponse ) (* audio.HttpChunkedReader , error ) {
420
428
if storageResolve .Result == downloadpb .StorageResolveResponse_STORAGE {
421
429
return nil , fmt .Errorf ("old storage not supported" )
@@ -437,13 +445,24 @@ func (p *Player) httpChunkedReaderFromStorageResolve(log librespot.Logger, clien
437
445
continue
438
446
}
439
447
448
+ if lastFailed , found := p .cdnQuarantine [cdnUrl .Host ]; found {
449
+ if i == len (storageResolve .Cdnurl )- 1 {
450
+ log .WithField ("host" , cdnUrl .Host ).Warnf ("cannot skip cdn url because it is the last one" )
451
+ } else if time .Since (lastFailed ) < CdnUrlQuarantineDuration {
452
+ log .WithField ("host" , cdnUrl .Host ).Infof ("skipping cdn url because it has failed recently" )
453
+ continue
454
+ }
455
+ }
456
+
440
457
var rawStream * audio.HttpChunkedReader
441
458
rawStream , err = audio .NewHttpChunkedReader (log , client , cdnUrl .String ())
442
459
if err != nil {
443
- log .WithError (err ).WithField ("url" , cdnUrl .String ()).Warnf ("failed creating chunked reader for %s, trying next url" , cdnUrl .Host )
460
+ log .WithError (err ).WithField ("host" , cdnUrl .Host ).Warnf ("failed creating chunked reader, trying next url" )
461
+ p .cdnQuarantine [cdnUrl .Host ] = time .Now ()
444
462
continue
445
463
}
446
464
465
+ delete (p .cdnQuarantine , cdnUrl .Host )
447
466
return rawStream , nil
448
467
}
449
468
0 commit comments