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.32k stars 5.33k forks source link

HLS: How to achieve on-demand streaming and disconnect the stream when no one is watching. #3205

Closed zh19900725 closed 1 year ago

zh19900725 commented 1 year ago

Note: Please read FAQ before file an issue, see 2716

Note: Before asking a question, please refer to the FAQ, specifically 2716.

How to achieve on-demand streaming with HLS

> After recording ts with SRS, HLS playback actually goes through nginx for downloading. I would like to know how to automatically disconnect the streaming when there are no viewers, that is, how to determine that HLS has no clients and then disconnect the camera's streaming. For RTMP, the number of clients can be determined by querying the /api/v1/streams/ interface, but HLS playback does not increase the number of clients through this interface.

1. SRS Version (版本): srs4

1. SRS Log (日志):

**N/A**

1. SRS Config (Configuration):

vhost __defaultVhost__ {
    hls {
        enabled         on;
        hls_path        /run/media;
        hls_fragment    4;
        hls_window      10;
    }
}

Expect (Expected Behavior)

Provide a way for me to obtain the number of clients on the HLS playback side, so as to achieve automatic stream disconnection when no one is watching.

TRANS_BY_GPT3

kyhx1984 commented 1 year ago

curl -v http://127.0.0.1:1985/api/v1/streams/ There is a number of clients curl -v -X DELETE "http://127.0.0.1:1985/api/v1/clients/streamID

TRANS_BY_GPT3

zh19900725 commented 1 year ago

curl -v http://127.0.0.1:1985/api/v1/streams/ There is a client count curl -v -X DELETE "http://127.0.0.1:1985/api/v1/clients/streamID

I am currently using this interface, and there are no issues with rtmp and flv, but there is a problem with hls. It seems that this interface does not include hls in the client count statistics.

TRANS_BY_GPT3

winlinvip commented 1 year ago

@kyhx1984 This plan is very good, thank you. 👍

@zh19900725 HLS is supported to be counted to clients in version 4.0, but if your HLS is not distributed through SRS, it will not be counted. There is a configuration called hls_ctx that you should pay attention to.

        # Whether enable hls_ctx for HLS streaming, for which we create a "fake" connection for HTTP API and callback.
        # For each HLS streaming session, we use a child m3u8 with a session identified by query "hls_ctx", it simply
        # work as the session id.
        # Once the HLS streaming session is created, we will cleanup it when timeout in 2*hls_window seconds. So it
        # takes a long time period to identify the timeout.
        # Now we got a HLS stremaing session, just like RTMP/WebRTC/HTTP-FLV streaming, we're able to stat the session
        # as a "fake" connection, do HTTP callback when start playing the HLS streaming. You're able to do querying and
        # authentication.
        # Note that it will make NGINX edge cache always missed, so never enable HLS streaming if use NGINX edges.
        # Overwrite by env SRS_VHOST_HLS_HLS_CTX for all vhosts.
        # Default: on
        hls_ctx on;

TRANS_BY_GPT3