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

client: support getting absolute timestamp with low-latency streams #127

Merged
merged 1 commit into from
Jan 1, 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
29 changes: 24 additions & 5 deletions client_stream_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"net/http"
"net/url"
"strconv"
"time"

"github.com/bluenviron/gohlslib/pkg/playlist"
)
Expand All @@ -29,6 +30,25 @@
return segments[index], index, len(segments) - index
}

func dateTimeOfPreloadHint(pl *playlist.Media) *time.Time {
if len(pl.Segments) == 0 {
return nil
}

Check warning on line 36 in client_stream_downloader.go

View check run for this annotation

Codecov / codecov/patch

client_stream_downloader.go#L35-L36

Added lines #L35 - L36 were not covered by tests

lastSeg := pl.Segments[len(pl.Segments)-1]
if lastSeg.DateTime == nil {
return nil
}

Check warning on line 41 in client_stream_downloader.go

View check run for this annotation

Codecov / codecov/patch

client_stream_downloader.go#L40-L41

Added lines #L40 - L41 were not covered by tests

d := lastSeg.DateTime.Add(lastSeg.Duration)

for _, part := range pl.Parts {
d = d.Add(part.Duration)
}

return &d
}

type clientStreamDownloader struct {
isLeading bool
httpClient *http.Client
Expand Down Expand Up @@ -120,8 +140,8 @@
}

d.segmentQueue.push(&segmentData{
// dateTime: seg.DateTime,
payload: byts,
dateTime: dateTimeOfPreloadHint(pl),
payload: byts,
})

pl, err = d.downloadPlaylist(ctx, d.firstPlaylist.ServerControl.CanSkipUntil != nil)
Expand Down Expand Up @@ -202,9 +222,8 @@
}

if preloadHint.ByteRangeLength != nil {
req.Header.Add("Range", "bytes="+
strconv.FormatUint(preloadHint.ByteRangeStart, 10)+"-"+
strconv.FormatUint(preloadHint.ByteRangeStart+*preloadHint.ByteRangeLength-1, 10))
req.Header.Add("Range", "bytes="+strconv.FormatUint(preloadHint.ByteRangeStart, 10)+
"-"+strconv.FormatUint(preloadHint.ByteRangeStart+*preloadHint.ByteRangeLength-1, 10))

Check warning on line 226 in client_stream_downloader.go

View check run for this annotation

Codecov / codecov/patch

client_stream_downloader.go#L225-L226

Added lines #L225 - L226 were not covered by tests
}

res, err := d.httpClient.Do(req)
Expand Down
21 changes: 15 additions & 6 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,44 +610,44 @@ func TestClientFMP4LowLatency(t *testing.T) {
switch count {
case 0:
require.Equal(t, "", ctx.Query("_HLS_skip"))

ctx.Writer.Write([]byte("#EXTM3U\n" +
"#EXT-X-VERSION:9\n" +
"#EXT-X-SERVER-CONTROL:CAN-BLOCK-RELOAD=YES,PART-HOLD-BACK=5.00000,CAN-SKIP-UNTIL=24.00000\n" +
"#EXT-X-MEDIA-SEQUENCE:20\n" +
"#EXT-X-TARGETDURATION:2\n" +
"#EXT-X-MAP:URI=\"init.mp4\"\n" +
"#EXT-X-PROGRAM-DATE-TIME:2015-02-05T01:02:02Z\n" +
"#EXTINF:2,\n" +
"segment.mp4\n" +
"#EXT-X-PRELOAD-HINT:TYPE=PART,URI=part1.mp4\n"))

case 1:
require.Equal(t, "YES", ctx.Query("_HLS_skip"))

ctx.Writer.Write([]byte("#EXTM3U\n" +
"#EXT-X-VERSION:9\n" +
"#EXT-X-SERVER-CONTROL:CAN-BLOCK-RELOAD=YES,PART-HOLD-BACK=5.00000,CAN-SKIP-UNTIL=24.00000\n" +
"#EXT-X-MEDIA-SEQUENCE:20\n" +
"#EXT-X-TARGETDURATION:2\n" +
"#EXT-X-MAP:URI=\"init.mp4\"\n" +
"#EXT-X-PROGRAM-DATE-TIME:2015-02-05T01:02:02Z\n" +
"#EXTINF:2,\n" +
"segment.mp4\n" +
"#EXT-X-PART:DURATION=1.00000,URI=\"part1.mp4\",INDEPENDENT=YES\n" +
"#EXT-X-PART:DURATION=0.066666666,URI=\"part1.mp4\",INDEPENDENT=YES\n" +
"#EXT-X-PRELOAD-HINT:TYPE=PART,URI=part2.mp4\n"))

case 2:
require.Equal(t, "YES", ctx.Query("_HLS_skip"))

ctx.Writer.Write([]byte("#EXTM3U\n" +
"#EXT-X-VERSION:9\n" +
"#EXT-X-SERVER-CONTROL:CAN-BLOCK-RELOAD=YES,PART-HOLD-BACK=5.00000,CAN-SKIP-UNTIL=24.00000\n" +
"#EXT-X-MEDIA-SEQUENCE:20\n" +
"#EXT-X-TARGETDURATION:2\n" +
"#EXT-X-MAP:URI=\"init.mp4\"\n" +
"#EXT-X-PROGRAM-DATE-TIME:2015-02-05T01:02:02Z\n" +
"#EXTINF:2,\n" +
"segment.mp4\n" +
"#EXT-X-PART:DURATION=1.00000,URI=\"part1.mp4\",INDEPENDENT=YES\n" +
"#EXT-X-PART:DURATION=1.00000,URI=\"part2.mp4\"\n" +
"#EXT-X-PART:DURATION=0.066666666,URI=\"part1.mp4\",INDEPENDENT=YES\n" +
"#EXT-X-PART:DURATION=0.033333333,URI=\"part2.mp4\"\n" +
"#EXT-X-PRELOAD-HINT:TYPE=PART,URI=part3.mp4\n"))
}
count++
Expand Down Expand Up @@ -754,6 +754,9 @@ func TestClientFMP4LowLatency(t *testing.T) {
c.OnDataH26x(tracks[0], func(pts time.Duration, dts time.Duration, au [][]byte) {
switch recvCount {
case 0:
ntp, ok := c.AbsoluteTime(tracks[0], dts)
require.Equal(t, true, ok)
require.Equal(t, time.Date(2015, time.February, 5, 1, 2, 4, 0, time.UTC), ntp)
require.Equal(t, 0*time.Second, pts)
require.Equal(t, time.Duration(0), dts)
require.Equal(t, [][]byte{
Expand All @@ -763,11 +766,17 @@ func TestClientFMP4LowLatency(t *testing.T) {
}, au)

case 1:
ntp, ok := c.AbsoluteTime(tracks[0], dts)
require.Equal(t, true, ok)
require.Equal(t, time.Date(2015, time.February, 5, 1, 2, 4, 33333333, time.UTC), ntp)
require.Equal(t, 33333333*time.Nanosecond, pts)
require.Equal(t, 33333333*time.Nanosecond, dts)
require.Equal(t, [][]byte{{1, 4, 5, 6}}, au)

case 2:
ntp, ok := c.AbsoluteTime(tracks[0], dts)
require.Equal(t, true, ok)
require.Equal(t, time.Date(2015, time.February, 5, 1, 2, 4, 66666666, time.UTC), ntp)
require.Equal(t, 66666666*time.Nanosecond, pts)
require.Equal(t, 66666666*time.Nanosecond, dts)
require.Equal(t, [][]byte{{1, 7, 8, 9}}, au)
Expand Down
Loading