ossrs / srs

SRS is a simple, high-efficiency, real-time media server supporting RTMP, WebRTC, HLS, HTTP-FLV, HTTP-TS, SRT, MPEG-DASH, and GB28181.
https://ossrs.io
MIT License
25.65k stars 5.38k forks source link

Problems setting correct EXT-X-TARGETDURATION via hls_fragment and hls_window configuration #3531

Closed tsaiyuen closed 1 year ago

tsaiyuen commented 1 year ago

Description

I want to configure my EXT-X-TARGETDURATION with value 5. My segment needs to have max 5 seconds, and I tried may values for hls_fragment and hls_window. My minimum value is always EXT-X-TARGETDURATION: 9. Is there any limitation?

  1. SRS Version: 4.0

  2. SRS Log:

[2023-05-04 17:29:14.081][Trace][1][2p45mgj7] start publish mr=0/350, p1stpt=20000, pnt=5000, tcp_nodelay=0
[2023-05-04 17:29:14.087][Trace][1][2p45mgj7] got metadata, width=1280, height=960, vcodec=7
[2023-05-04 17:29:14.087][Trace][1][2p45mgj7] 46B video sh,  codec(7, profile=Baseline, level=3.2, 1280x960, 0kbps, 0.0fps, 0.0s)
[2023-05-04 17:29:15.563][Trace][1][2p45mgj7] -> HLS time=100113204ms, sno=146, ts=livestream-145.ts, dur=2866ms, dva=0p
[2023-05-04 17:29:17.122][Trace][1][29f575ni] Hybrid cpu=19.02%,15MB, cid=4,1, timer=61,0,0, clock=0,30,16,1,1,0,1,0,0, free=1, objs=(pkt:298,raw:266,fua:31,msg:1120,oth:1,buf:1)
[2023-05-04 17:29:22.122][Trace][1][29f575ni] Hybrid cpu=18.98%,15MB, cid=4,1, timer=61,0,0, clock=0,30,16,1,1,0,1,0,0, free=1, objs=(pkt:298,raw:266,fua:31,msg:1120,oth:1,buf:1)
[2023-05-04 17:29:25.565][Trace][1][2p45mgj7] -> HLS time=110117297ms, sno=162, ts=livestream-161.ts, dur=3600ms, dva=0p
[2023-05-04 17:29:27.122][Trace][1][29f575ni] Hybrid cpu=17.00%,15MB, cid=4,1, timer=61,0,0, clock=0,29,17,1,0,0,0,0,0, objs=(pkt:381,raw:340,fua:39,msg:1430,oth:1,buf:1)
[2023-05-04 17:29:32.126][Trace][1][29f575ni] Hybrid cpu=20.00%,15MB, cid=4,1, timer=61,0,0, clock=0,29,17,1,0,0,0,0,0, objs=(pkt:381,raw:340,fua:39,msg:1430,oth:1,buf:1)
[2023-05-04 17:29:35.588][Trace][1][2p45mgj7] -> HLS time=120134670ms, sno=178, ts=livestream-177.ts, dur=1934ms, dva=0p
[2023-05-04 17:29:37.126][Trace][1][29f575ni] Hybrid cpu=19.98%,15MB, cid=3,0, timer=60,0,0, clock=0,28,17,1,1,0,0,0,0, objs=(pkt:423,raw:376,fua:45,msg:1582,oth:1,buf:1)
[2023-05-04 17:29:39.083][Trace][1][2p45mgj7] <- CPB time=20002032, okbps=1,0,0, ikbps=3233,0,0, mr=0/350, p1stpt=20000, pnt=5000
[2023-05-04 17:29:42.126][Trace][1][29f575ni] Hybrid cpu=14.97%,15MB, cid=3,0, timer=60,0,0, clock=0,28,17,1,1,0,0,0,0, objs=(pkt:423,raw:376,fua:45,msg:1582,oth:1,buf:1)
[2023-05-04 17:29:45.598][Trace][1][2p45mgj7] -> HLS time=130148717ms, sno=193, ts=livestream-192.ts, dur=4300ms, dva=0p
  1. SRS Config:
vhost __defaultVhost__ {
    hls {
        enabled         on;
        hls_fragment    2;
        hls_window      120;
    }

Replay

Please describe how to replay the bug?

Step 1: Run docker, copying the docker.conf from my file, with the configuration above

docker run --rm -it -p 1935:1935 -p 1985:1985 -p 8080:8080 \
    -v /Users/cynthia.tong/globo/poc-rtmp-lite/docker.conf:/usr/local/srs/conf/docker.conf ossrs/srs:4 ./objs/srs -c conf/docker.conf

Step 2: run ffmpeg streaming

ffmpeg -r 30 -f lavfi -i testsrc -vf scale=1280:960 -vcodec libx264 -profile:v baseline -pix_fmt yuv420p -f flv rtmp://localhost/live/livestream

Step 3: curl localhost with wery string

➜ curl "http://localhost:8080/live/livestream.m3u8"
' translates to:

➜ curl "http://localhost:8080/live/livestream.m3u8"

#EXTM3U
#EXT-X-STREAM-INF:BANDWIDTH=1,AVERAGE-BANDWIDTH=1
/live/livestream.m3u8?hls_ctx=223b622j%
➜ curl "http://localhost:8080/live/livestream.m3u8?hls_ctx=223b622j"
' translates to:

➜ curl "http://localhost:8080/live/livestream.m3u8?hls_ctx=223b622j"

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:34
#EXT-X-TARGETDURATION:9
#EXTINF:8.300, no desc
livestream-34.ts
#EXTINF:8.300, no desc
livestream-35.ts
#EXTINF:8.300, no desc
livestream-36.ts
#EXTINF:8.300, no desc
livestream-37.ts
#EXTINF:8.300, no desc
livestream-38.ts
#EXTINF:8.300, no desc
livestream-39.ts
#EXTINF:8.300, no desc
livestream-40.ts
#EXTINF:8.300, no desc
livestream-41.ts
#EXTINF:8.300, no desc
livestream-42.ts
#EXTINF:8.300, no desc
livestream-43.ts
#EXTINF:8.300, no desc
livestream-44.ts
#EXTINF:8.300, no desc
livestream-45.ts
#EXTINF:8.300, no desc
livestream-46.ts
#EXTINF:8.300, no desc
livestream-47.ts
#EXTINF:8.300, no desc
livestream-48.ts

Expect

I believe we should have a smaller EXT-X-TARGETDURATION and EXTINF. Maybe EXT-X-TARGETDURATION:5 and EXTINF:4.8

TRANS_BY_GPT3

winlinvip commented 1 year ago

SRS has a default setting where it waits for a complete GOP of frames before closing a TS segment. This means that if you set hls_fragment to 2, but your GOP is 5 seconds long, the resulting segment may exceed the 5-second limit.

To reduce the size of the GOP, you can modify the encoder settings. For instance, in FFmpeg, you can set the GOP to 2 seconds by using the command -r 25 -g 50 by -g frames.

You can also configure SRS to not wait for a complete GOP before closing a TS segment file by adjusting thehls_wait_keyframe setting.

        # whether wait keyframe to reap segment,
        # if off, reap segment when duration exceed the fragment,
        # if on, reap segment when duration exceed and got keyframe.
        # default: on
        hls_wait_keyframe       on;

See FAQ: