Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore LL-HLS requests for segments that have already expired #156

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions muxer_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
"github.com/bluenviron/mediacommon/pkg/codecs/av1"
"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 +564,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
76 changes: 76 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,77 @@ 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() {
// do nothing
}

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
}
Loading