Skip to content

Commit d171b63

Browse files
committed
sort layers by bitrate.
1 parent d265218 commit d171b63

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

hlsvod/utils.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hlsvod
33
import (
44
"fmt"
55
"math"
6+
"sort"
67
"strings"
78
"time"
89
)
@@ -57,15 +58,35 @@ func convertToSegments(rawTimeList []float64, duration time.Duration, segmentLen
5758
}
5859

5960
func StreamsPlaylist(profiles map[string]VideoProfile, segmentNameFmt string) string {
61+
layers := []struct {
62+
Bitrate int
63+
Entries []string
64+
}{}
65+
66+
for name, profile := range profiles {
67+
layers = append(layers, struct {
68+
Bitrate int
69+
Entries []string
70+
}{
71+
profile.Bitrate,
72+
[]string{
73+
fmt.Sprintf("#EXT-X-STREAM-INF:BANDWIDTH=%d,RESOLUTION=%dx%d,NAME=%s", profile.Bitrate, profile.Width, profile.Height, name),
74+
fmt.Sprintf(segmentNameFmt, name),
75+
},
76+
})
77+
}
78+
79+
// sort by bitrate
80+
sort.Slice(layers, func(i, j int) bool {
81+
return layers[i].Bitrate < layers[j].Bitrate
82+
})
83+
6084
// playlist prefix
6185
playlist := []string{"#EXTM3U"}
6286

6387
// playlist segments
64-
for name, profile := range profiles {
65-
playlist = append(playlist,
66-
fmt.Sprintf("#EXT-X-STREAM-INF:BANDWIDTH=%d,RESOLUTION=%dx%d,NAME=%s", profile.Bitrate, profile.Width, profile.Height, name),
67-
fmt.Sprintf(segmentNameFmt, name),
68-
)
88+
for _, profile := range layers {
89+
playlist = append(playlist, profile.Entries...)
6990
}
7091

7192
// join with newlines

0 commit comments

Comments
 (0)