1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk
Winlin 15fbe45a9a
FLV: Refine source and http handler. v6.0.155 v7.0.14 (#4165)
1. Do not create a source when mounting FLV because it may not unmount
FLV when freeing the source. If you access the FLV stream without any
publisher, then wait for source cleanup and review the FLV stream again,
there is an annoying warning message.

```bash
# View HTTP FLV stream by curl, wait for stream to be ready.
# curl http://localhost:8080/live/livestream.flv -v >/dev/null
HTTP #0 127.0.0.1:58026 GET http://localhost:8080/live/livestream.flv, content-length=-1
new live source, stream_url=/live/livestream
http: mount flv stream for sid=/live/livestream, mount=/live/livestream.flv

# Cancel the curl and trigger source cleanup without http unmount.
client disconnect peer. ret=1007
Live: cleanup die source, id=[], total=1

# View the stream again, it fails.
# curl http://localhost:8080/live/livestream.flv -v >/dev/null
HTTP #0 127.0.0.1:58040 GET http://localhost:8080/live/livestream.flv, content-length=-1
serve error code=1097(NoSource)(No source found) : process request=0 : cors serve : serve http : no source for /live/livestream
serve_http() [srs_app_http_stream.cpp:641]
```

> Note: There is an inconsistency. The first time, you can access the
FLV stream and wait for the publisher, but the next time, you cannot.

2. Create a source when starting to serve the FLV client. We do not need
to create the source when creating the HTTP handler. Instead, we should
try to create the source in the cache or stream. Because the source
cleanup does not unmount the HTTP handler, the handler remains after the
source is destroyed. The next time you access the FLV stream, the source
is not found.

```cpp
srs_error_t SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph) {
    SrsSharedPtr<SrsLiveSource> live_source;
    if ((err = _srs_sources->fetch_or_create(r.get(), server, live_source)) != srs_success) { }
    if ((err = http_mount(r.get())) != srs_success) { }

srs_error_t SrsBufferCache::cycle() {
    SrsSharedPtr<SrsLiveSource> live_source = _srs_sources->fetch(req);
    if (!live_source.get()) {
        return srs_error_new(ERROR_NO_SOURCE, "no source for %s", req->get_stream_url().c_str());
    }

srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) {
    SrsSharedPtr<SrsLiveSource> live_source = _srs_sources->fetch(req);
    if (!live_source.get()) {
        return srs_error_new(ERROR_NO_SOURCE, "no source for %s", req->get_stream_url().c_str());
    }
```

> Note: We should not create the source in hijack, instead, we create it
in cache or stream:

```cpp
srs_error_t SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph) {
    if ((err = http_mount(r.get())) != srs_success) { }

srs_error_t SrsBufferCache::cycle() {
    SrsSharedPtr<SrsLiveSource> live_source;
    if ((err = _srs_sources->fetch_or_create(req, server_, live_source)) != srs_success) { }

srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) {
    SrsSharedPtr<SrsLiveSource> live_source;
    if ((err = _srs_sources->fetch_or_create(req, server_, live_source)) != srs_success) { }
```

> Note: This fixes the failure and annoying warning message, and
maintains consistency by always waiting for the stream to be ready if
there is no publisher.

3. Fail the http request if the HTTP handler is disposing, and also keep
the handler entry when disposing the stream, because we should dispose
the handler entry and stream at the same time.

```cpp
srs_error_t SrsHttpStreamServer::http_mount(SrsRequest* r) {
        entry = streamHandlers[sid];
        if (entry->disposing) {
            return srs_error_new(ERROR_STREAM_DISPOSING, "stream is disposing");
        }

void SrsHttpStreamServer::http_unmount(SrsRequest* r) {
    std::map<std::string, SrsLiveEntry*>::iterator it = streamHandlers.find(sid);
    SrsUniquePtr<SrsLiveEntry> entry(it->second);
    entry->disposing = true;
```

> Note: If the disposal process takes a long time, this will prevent
unexpected behavior or access to the resource that is being disposed of.

4. In edge mode, the edge ingester will unpublish the source when the
last consumer quits, which is actually triggered by the HTTP stream.
While it also waits for the stream to quit when the HTTP unmounts, there
is a self-destruction risk: the HTTP live stream object destroys itself.

```cpp
srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) {
    SrsUniquePtr<SrsLiveConsumer> consumer(consumer_raw); // Trigger destroy.

void SrsHttpStreamServer::http_unmount(SrsRequest* r) {
    for (;;) { if (!cache->alive() && !stream->alive()) { break; } // A circle reference.
    mux.unhandle(entry->mount, stream.get()); // Free the SrsLiveStream itself.
```

> Note: It also introduces a circular reference in the object
relationships, the stream reference to itself when unmount:

```text
SrsLiveStream::serve_http 
    -> SrsLiveConsumer::~SrsLiveConsumer -> SrsEdgeIngester::stop 
    -> SrsLiveSource::on_unpublish -> SrsHttpStreamServer::http_unmount 
        -> SrsLiveStream::alive
```

> Note: We should use an asynchronous worker to perform the cleanup to
avoid the stream destroying itself and to prevent self-referencing.

```cpp
void SrsHttpStreamServer::http_unmount(SrsRequest* r) {
    entry->disposing = true;
    if ((err = async_->execute(new SrsHttpStreamDestroy(&mux, &streamHandlers, sid))) != srs_success) { }
```

> Note: This also ensures there are no circular references and no
self-destruction.

---------

Co-authored-by: Jacob Su <suzp1984@gmail.com>
2024-09-01 13:02:07 +08:00
..
3rdparty ASAN: Support coroutine context switching and stack tracing (#4153) 2024-08-22 17:12:39 +08:00
auto Fix crash when quiting. v6.0.151 v7.0.10 (#4157) 2024-08-24 22:40:39 +08:00
conf GB28181: Support external SIP server. v6.0.144 (#4101) 2024-07-27 09:06:12 +08:00
doc FLV: Refine source and http handler. v6.0.155 v7.0.14 (#4165) 2024-09-01 13:02:07 +08:00
etc/init.d Squash: Fix bugs 2022-01-13 18:26:28 +08:00
gdb The identifier "ShowCouroutines" needs to be modified to "ShowCoroutines" in order to rectify the typographical error. v6.0.63 (#3703) 2023-07-27 08:12:39 +08:00
ide/srs_clion UniquePtr: Support SrsUniquePtr to replace SrsAutoFree. v6.0.136 (#4109) 2024-07-09 10:29:36 +08:00
modules Moduels: Refine the description 2021-04-21 11:18:35 +08:00
packaging Windows: Refine cygwin pipeline (#3260) 2022-11-21 14:32:26 +08:00
research ASAN: Support coroutine context switching and stack tracing (#4153) 2024-08-22 17:12:39 +08:00
scripts UniquePtr: Support SrsUniquePtr to replace SrsAutoFree. v6.0.136 (#4109) 2024-07-09 10:29:36 +08:00
src FLV: Refine source and http handler. v6.0.155 v7.0.14 (#4165) 2024-09-01 13:02:07 +08:00
usr/lib/systemd/system Squash: Support RPM. Fix bugs. 2021-11-04 07:37:17 +08:00
.gitignore For #2136: API: Cleanup no active streams for statistics. v5.0.42 2022-08-19 19:45:19 +08:00
AUTHORS.md Update contributors. 2023-09-28 11:03:34 +08:00
AUTHORS.txt Update AUTHORS 2021-08-25 07:43:35 +08:00
configure ASAN: Support coroutine context switching and stack tracing (#4153) 2024-08-22 17:12:39 +08:00
Dockerfile.builds Use new cache image name. v6.0.86 (#3815) 2023-10-08 07:43:14 -05:00
Dockerfile.cov Asan: Refine asan warning message for macOS. 2022-12-31 21:20:51 +08:00
Dockerfile.pkg Merge branch v5.0.103 into develop 2022-12-03 21:15:22 +08:00
Dockerfile.test Fix opus delay options, use ffmpeg-opus in docker test. v6.0.102 (#3883) 2023-11-22 14:58:30 +08:00