Skip to content

Commit

Permalink
fix(withny): test playlist before downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkness4 committed Aug 20, 2024
1 parent 98dc279 commit 47aaf92
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
25 changes: 16 additions & 9 deletions withny/api/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,16 @@ func splitByCommaAvoidQuote(s string) []string {

// PlaylistConstraint is used to filter playlists based on their attributes.
type PlaylistConstraint struct {
MinBandwidth int64 `yaml:"minBandwidth"`
MaxBandwidth int64 `yaml:"maxBandwidth"`
MinHeight int64 `yaml:"minHeight"`
MaxHeight int64 `yaml:"maxHeight"`
MinWidth int64 `yaml:"minWidth"`
MaxWidth int64 `yaml:"maxWidth"`
MinFrameRate float64 `yaml:"minFrameRate"`
MaxFrameRate float64 `yaml:"maxFrameRate"`
AudioOnly bool `yaml:"audioOnly"`
MinBandwidth int64 `yaml:"minBandwidth"`
MaxBandwidth int64 `yaml:"maxBandwidth"`
MinHeight int64 `yaml:"minHeight"`
MaxHeight int64 `yaml:"maxHeight"`
MinWidth int64 `yaml:"minWidth"`
MaxWidth int64 `yaml:"maxWidth"`
MinFrameRate float64 `yaml:"minFrameRate"`
MaxFrameRate float64 `yaml:"maxFrameRate"`
AudioOnly bool `yaml:"audioOnly"`
Ignored []string `yaml:"ignored"`
}

// GetBestPlaylist returns the best playlist based on the constraints.
Expand All @@ -137,6 +138,12 @@ streamLoop:
constraint.AudioOnly && stream.Video != "audio_only":
continue streamLoop
}

for _, ignored := range constraint.Ignored {
if strings.Contains(stream.URL, ignored) {
continue streamLoop
}
}
}

if !found || compareStreams(stream, best) > 0 {
Expand Down
56 changes: 36 additions & 20 deletions withny/livestream.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/Darkness4/withny-dl/hls"
"github.com/Darkness4/withny-dl/telemetry/metrics"
"github.com/Darkness4/withny-dl/utils/try"
"github.com/Darkness4/withny-dl/withny/api"
"github.com/rs/zerolog/log"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -46,21 +47,43 @@ func DownloadLiveStream(ctx context.Context, client *api.Client, ls LiveStream)
return err
}

playlist, ok := api.GetBestPlaylist(playlists, ls.Params.QualityConstraint)
if !ok {
log.Warn().
Any("playlists", playlists).
Any("fallback", playlists[0]).
Any("constraint", ls.Params.QualityConstraint).
Msg("no playlist found with current constraint")
playlist = playlists[0]
var downloader *hls.Downloader
constraint := ls.Params.QualityConstraint
for {
playlist, ok := api.GetBestPlaylist(playlists, constraint)
if !ok {
log.Warn().
Any("playlists", playlists).
Any("fallback", playlists[0]).
Any("constraint", constraint).
Msg("no playlist found with current constraint")
playlist = playlists[0]
}

downloader := hls.NewDownloader(
client,
&log.Logger,
ls.Params.PacketLossMax,
playlist.URL,
)

if ok, err := try.DoWithResult(5, 5*time.Second, func() (bool, error) {
return downloader.Probe(ctx)
}); !ok || err != nil {
log.Warn().Err(err).Msg("failed to fetch playlist, switching to next playlist")
constraint.Ignored = append(constraint.Ignored, playlist.URL)
}

if ok {
log.Info().Any("playlist", playlist).Msg("received new HLS info")
span.AddEvent("playlist received", trace.WithAttributes(
attribute.String("url", playlist.URL),
attribute.String("format", playlist.Video),
))
break
}
}

log.Info().Any("playlist", playlist).Msg("received new HLS info")
span.AddEvent("playlist received", trace.WithAttributes(
attribute.String("url", playlist.URL),
attribute.String("format", playlist.Video),
))
metrics.TimeEndRecording(
ctx,
metrics.Downloads.InitTime,
Expand All @@ -70,13 +93,6 @@ func DownloadLiveStream(ctx context.Context, client *api.Client, ls LiveStream)
),
)

downloader := hls.NewDownloader(
client,
&log.Logger,
ls.Params.PacketLossMax,
playlist.URL,
)

span.AddEvent("downloading")
end := metrics.TimeStartRecording(
ctx,
Expand Down

0 comments on commit 47aaf92

Please sign in to comment.