From 521681e1b86e1356b140fac20984ae4df63dbb3c Mon Sep 17 00:00:00 2001 From: Christian <42749713+rAcHekLoS@users.noreply.github.com> Date: Wed, 4 Aug 2021 20:34:02 +0200 Subject: [PATCH] Sort after build variants index number, fix broken index for HLS path. (#1282) * safely generate the thumbnail.jpg & preview.gif * Sort after build variants index number, fix broken index for HLS path. * Don't export VideoBitrate and IsVideoPassthrough * Unused variables deleted * Solution for sorting not working correctly. * Rename StreamSortVariants to streamSortVariants --- controllers/video.go | 49 +++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/controllers/video.go b/controllers/video.go index 537319579..e19a4e711 100644 --- a/controllers/video.go +++ b/controllers/video.go @@ -7,28 +7,53 @@ import ( "github.com/owncast/owncast/core/data" ) +type variantsSort struct { + Index int + Name string + IsVideoPassthrough bool + VideoBitrate int +} + type variantsResponse struct { - Name string `json:"name"` Index int `json:"index"` + Name string `json:"name"` } // GetVideoStreamOutputVariants will return the video variants available. func GetVideoStreamOutputVariants(w http.ResponseWriter, r *http.Request) { outputVariants := data.GetStreamOutputVariants() - sort.Slice(outputVariants, func(i, j int) bool { - return outputVariants[j].VideoBitrate < outputVariants[i].VideoBitrate - }) - - result := make([]variantsResponse, len(outputVariants)) - + streamSortVariants := make([]variantsSort, len(outputVariants)) for i, variant := range outputVariants { - variantResponse := variantsResponse{ - Index: i, - Name: variant.GetName(), + variantSort := variantsSort{ + Index: i, + Name: variant.GetName(), + IsVideoPassthrough: variant.IsVideoPassthrough, + VideoBitrate: variant.VideoBitrate, } - result[i] = variantResponse + streamSortVariants[i] = variantSort } - WriteResponse(w, result) + sort.Slice(streamSortVariants, func(i, j int) bool { + if streamSortVariants[i].IsVideoPassthrough && !streamSortVariants[j].IsVideoPassthrough { + return true + } + + if !streamSortVariants[i].IsVideoPassthrough && streamSortVariants[j].IsVideoPassthrough { + return false + } + + return streamSortVariants[i].VideoBitrate > streamSortVariants[j].VideoBitrate + }) + + response := make([]variantsResponse, len(streamSortVariants)) + for i, variant := range streamSortVariants { + variantResponse := variantsResponse{ + Index: variant.Index, + Name: variant.Name, + } + response[i] = variantResponse + } + + WriteResponse(w, response) }