Skip to content

Commit

Permalink
Ignore LL-HLS requests for segments that have already expired
Browse files Browse the repository at this point in the history
  • Loading branch information
HyeockJinKim committed May 10, 2024
1 parent 8387829 commit d2ed981
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
7 changes: 4 additions & 3 deletions muxer_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (
"github.com/bluenviron/mediacommon/pkg/codecs/h264"
"github.com/bluenviron/mediacommon/pkg/codecs/h265"

"github.com/bluenviron/mediacommon/pkg/formats/fmp4"
"github.com/bluenviron/mediacommon/pkg/formats/fmp4/seekablebuffer"

"github.com/bluenviron/gohlslib/pkg/codecparams"
"github.com/bluenviron/gohlslib/pkg/codecs"
"github.com/bluenviron/gohlslib/pkg/playlist"
"github.com/bluenviron/mediacommon/pkg/formats/fmp4"
"github.com/bluenviron/mediacommon/pkg/formats/fmp4/seekablebuffer"
)

func boolPtr(v bool) *bool {
Expand Down Expand Up @@ -564,7 +565,7 @@ func (s *muxerServer) handleMediaPlaylist(msn string, part string, skip string,
// exceeds the last Partial Segment in the current Playlist by the
// Advance Part Limit, then the server SHOULD immediately return Bad
// Request, such as HTTP 400.
if msnint > (s.nextSegmentID + 1) {
if msnint > (s.nextSegmentID+1) || msnint < (s.nextSegmentID-uint64(len(s.segments)-1)) {
w.WriteHeader(http.StatusBadRequest)
return nil, nil
}
Expand Down
77 changes: 77 additions & 0 deletions muxer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package gohlslib
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -1051,3 +1053,78 @@ func TestMuxerInvalidFolder(t *testing.T) {
})
}
}

func TestMuxerExpiredSegment(t *testing.T) {
t.Parallel()

testcases := []struct {
name string
variant MuxerVariant
}{
{
name: "lowLatency",
variant: MuxerVariantLowLatency,
},
}

for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
muxer := &Muxer{
Variant: tc.variant,
SegmentCount: 7,
VideoTrack: testVideoTrack,
Directory: "/nonexisting",
}
err := muxer.Start()
require.NoError(t, err)
for i := 0; i < 9; i++ {
muxer.server.segments = append(muxer.server.segments, &mockSegmentMP4{})
}
muxer.server.nextSegmentParts = append(muxer.server.nextSegmentParts, &muxerPart{})
w := &dummyResponseWriter{
h: make(http.Header),
}
v := url.Values{}
v.Set("_HLS_msn", "1")
v.Set("_HLS_part", "0")

r := &http.Request{
URL: &url.URL{
Path: "stream.m3u8",
RawQuery: v.Encode(),
},
}
muxer.Handle(w, r)
require.Equal(t, http.StatusBadRequest, w.statusCode)
})
}
}

type mockSegmentMP4 struct {
}

func (m mockSegmentMP4) close() {
return
}

func (m mockSegmentMP4) getName() string {
return "mock"
}

func (m mockSegmentMP4) getDuration() time.Duration {
return 100 * time.Millisecond
}

func (m mockSegmentMP4) getSize() uint64 {
return 12345
}

func (m mockSegmentMP4) isForceSwitched() bool {
return false
}

func (m mockSegmentMP4) reader() (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader("mock")), nil
}

0 comments on commit d2ed981

Please sign in to comment.