From 7cf8c48157d90d1b4cdeae7d53bac559d11f172a Mon Sep 17 00:00:00 2001 From: Winlin Date: Fri, 12 May 2023 15:22:01 +0800 Subject: [PATCH] WHIP: Improve HTTP DELETE for notifying server unpublish event (#3539) This PR improves the functionality of the HTTP DELETE method used by WHIP to notify the server when the client stops publishing. The URL is parsed from the location header returned by SRS, and the URL is refined with the addition of the action=delete parameter to ensure more accurate identification of the DELETE request. Furthermore, SRS will disconnect and close the session, enabling the client to publish the stream again quickly and easily. This update eliminates the approximately 30-second waiting period previously required for republishing the stream after an unpublish event. Overall, this update provides a more effective and efficient method for notifying the server about unpublish events and will enhance the workflow experience for users of the WHIP platform. ------- Co-authored-by: Haibo Chen <495810242@qq.com> Co-authored-by: ChenGH --- trunk/src/app/srs_app_rtc_api.cpp | 11 +++++++++-- trunk/src/app/srs_app_rtc_api.hpp | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/trunk/src/app/srs_app_rtc_api.cpp b/trunk/src/app/srs_app_rtc_api.cpp index 16bdd6d07..346592043 100644 --- a/trunk/src/app/srs_app_rtc_api.cpp +++ b/trunk/src/app/srs_app_rtc_api.cpp @@ -581,6 +581,7 @@ srs_error_t SrsGoApiRtcPublish::http_hooks_on_publish(SrsRequest* req) SrsGoApiRtcWhip::SrsGoApiRtcWhip(SrsRtcServer* server) { + server_ = server; publish_ = new SrsGoApiRtcPublish(server); play_ = new SrsGoApiRtcPlay(server); } @@ -601,7 +602,12 @@ srs_error_t SrsGoApiRtcWhip::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa // Client stop publish. // TODO: FIXME: Stop and cleanup the RTC session. if (r->method() == SRS_CONSTS_HTTP_DELETE) { - srs_trace("WHIP: Delete stream %s", r->url().c_str()); + string username = r->query_get("session"); + SrsRtcConnection* session = server_->find_session_by_username(username); + if (session) session->expire(); + srs_trace("WHIP: Delete session=%s, p=%p, url=%s", username.c_str(), session, r->url().c_str()); + + w->header()->set_content_length(0); w->write_header(SRS_CONSTS_HTTP_OK); return w->write(NULL, 0); } @@ -620,7 +626,8 @@ srs_error_t SrsGoApiRtcWhip::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa // Setup the content type to SDP. w->header()->set("Content-Type", "application/sdp"); // The location for DELETE resource, not required by SRS, but required by WHIP. - w->header()->set("Location", srs_fmt("/rtc/v1/whip/?app=%s&stream=%s", ruc.req_->app.c_str(), ruc.req_->stream.c_str())); + w->header()->set("Location", srs_fmt("/rtc/v1/whip/?action=delete&app=%s&stream=%s&session=%s", + ruc.req_->app.c_str(), ruc.req_->stream.c_str(), ruc.session_id_.c_str())); w->header()->set_content_length((int64_t)sdp.length()); // Must be 201, see https://datatracker.ietf.org/doc/draft-ietf-wish-whip/ w->write_header(201); diff --git a/trunk/src/app/srs_app_rtc_api.hpp b/trunk/src/app/srs_app_rtc_api.hpp index cf27f8d3d..4df5cf0d7 100644 --- a/trunk/src/app/srs_app_rtc_api.hpp +++ b/trunk/src/app/srs_app_rtc_api.hpp @@ -58,6 +58,7 @@ private: class SrsGoApiRtcWhip : public ISrsHttpHandler { private: + SrsRtcServer* server_; SrsGoApiRtcPublish* publish_; SrsGoApiRtcPlay* play_; public: