From 1e7c12a6dd32d0c15609a18a5a4840e563d6892a Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 11 Aug 2018 19:09:56 +0800 Subject: [PATCH] For #910, Support HTTP FLV with HTTP callback. 2.0.254 --- README.md | 2 + trunk/src/app/srs_app_http_conn.cpp | 6 +- trunk/src/app/srs_app_http_stream.cpp | 94 +++++++++++++++++++++++-- trunk/src/app/srs_app_http_stream.hpp | 3 + trunk/src/core/srs_core.hpp | 2 +- trunk/src/protocol/srs_rtmp_stack.cpp | 6 ++ trunk/src/protocol/srs_rtmp_stack.hpp | 3 + trunk/src/protocol/srs_rtmp_utility.cpp | 4 ++ 8 files changed, 111 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 03193b3b8b..a07c25596b 100755 --- a/README.md +++ b/README.md @@ -333,6 +333,7 @@ Remark: ## History +* v2.0, 2018-08-11, For [#910][bug #910], Support HTTP FLV with HTTP callback. 2.0.254 * v2.0, 2018-08-11, For [#1110][bug #1110], Refine params in http callback. 2.0.253 * v2.0, 2018-08-05, Refine HTTP-FLV latency, support realtime mode. 2.0.252 * v2.0, 2018-08-04, For [#1110][bug #1110], Support params in http callback. 2.0.251 @@ -1321,6 +1322,7 @@ Winlin [bug #1119]: https://github.com/ossrs/srs/issues/1119 [bug #1031]: https://github.com/ossrs/srs/issues/1031 [bug #1110]: https://github.com/ossrs/srs/issues/1110 +[bug #910]: https://github.com/ossrs/srs/issues/910 [bug #xxxxxxxxxx]: https://github.com/ossrs/srs/issues/xxxxxxxxxx [exo #828]: https://github.com/google/ExoPlayer/pull/828 diff --git a/trunk/src/app/srs_app_http_conn.cpp b/trunk/src/app/srs_app_http_conn.cpp index 90845890df..88efeedfd6 100644 --- a/trunk/src/app/srs_app_http_conn.cpp +++ b/trunk/src/app/srs_app_http_conn.cpp @@ -864,10 +864,8 @@ SrsRequest* SrsHttpMessage::to_request(string vhost) req->pageUrl = get_request_header("Referer"); req->objectEncoding = 0; - srs_discovery_tc_url(req->tcUrl, - req->schema, req->host, req->vhost, req->app, req->stream, req->port, - req->param); - req->strip(); + srs_discovery_tc_url(req->tcUrl, req->schema, req->host, req->vhost, req->app, req->stream, req->port, req->param); + req->as_http(); return req; } diff --git a/trunk/src/app/srs_app_http_stream.cpp b/trunk/src/app/srs_app_http_stream.cpp index 9181fb4dda..edf0d8c563 100755 --- a/trunk/src/app/srs_app_http_stream.cpp +++ b/trunk/src/app/srs_app_http_stream.cpp @@ -55,6 +55,7 @@ using namespace std; #include #include #include +#include #endif @@ -64,7 +65,7 @@ using namespace std; SrsStreamCache::SrsStreamCache(SrsSource* s, SrsRequest* r) { - req = r->copy(); + req = r->copy()->as_http(); source = s; queue = new SrsMessageQueue(true); pthread = new SrsEndlessThread("http-stream", this); @@ -457,7 +458,7 @@ SrsLiveStream::SrsLiveStream(SrsSource* s, SrsRequest* r, SrsStreamCache* c) { source = s; cache = c; - req = r->copy(); + req = r->copy()->as_http(); } SrsLiveStream::~SrsLiveStream() @@ -472,7 +473,7 @@ int SrsLiveStream::update(SrsSource* s, SrsRequest* r) srs_freep(req); source = s; req = r->copy(); - + return ret; } @@ -480,6 +481,22 @@ int SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { int ret = ERROR_SUCCESS; + if ((ret = http_hooks_on_play()) != ERROR_SUCCESS) { + srs_error("http hook on_play failed. ret=%d", ret); + return ret; + } + + ret = do_serve_http(w, r); + + http_hooks_on_stop(); + + return ret; +} + +int SrsLiveStream::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) +{ + int ret = ERROR_SUCCESS; + ISrsStreamEncoder* enc = NULL; srs_assert(entry); @@ -619,6 +636,75 @@ int SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) return ret; } +int SrsLiveStream::http_hooks_on_play() +{ + int ret = ERROR_SUCCESS; + +#ifdef SRS_AUTO_HTTP_CALLBACK + if (!_srs_config->get_vhost_http_hooks_enabled(req->vhost)) { + return ret; + } + + // the http hooks will cause context switch, + // so we must copy all hooks for the on_connect may freed. + // @see https://github.com/ossrs/srs/issues/475 + vector hooks; + + if (true) { + SrsConfDirective* conf = _srs_config->get_vhost_on_play(req->vhost); + + if (!conf) { + srs_info("ignore the empty http callback: on_play"); + return ret; + } + + hooks = conf->args; + } + + for (int i = 0; i < (int)hooks.size(); i++) { + std::string url = hooks.at(i); + if ((ret = SrsHttpHooks::on_play(url, req)) != ERROR_SUCCESS) { + srs_error("hook client on_play failed. url=%s, ret=%d", url.c_str(), ret); + return ret; + } + } +#endif + + return ret; +} + +void SrsLiveStream::http_hooks_on_stop() +{ +#ifdef SRS_AUTO_HTTP_CALLBACK + if (!_srs_config->get_vhost_http_hooks_enabled(req->vhost)) { + return; + } + + // the http hooks will cause context switch, + // so we must copy all hooks for the on_connect may freed. + // @see https://github.com/ossrs/srs/issues/475 + vector hooks; + + if (true) { + SrsConfDirective* conf = _srs_config->get_vhost_on_stop(req->vhost); + + if (!conf) { + srs_info("ignore the empty http callback: on_stop"); + return; + } + + hooks = conf->args; + } + + for (int i = 0; i < (int)hooks.size(); i++) { + std::string url = hooks.at(i); + SrsHttpHooks::on_stop(url, req); + } +#endif + + return; +} + int SrsLiveStream::streaming_send_messages(ISrsStreamEncoder* enc, SrsSharedPtrMessage** msgs, int nb_msgs) { int ret = ERROR_SUCCESS; @@ -844,7 +930,7 @@ int SrsHttpStreamServer::http_mount(SrsSource* s, SrsRequest* r) srs_freep(tmpl->req); tmpl->source = s; - tmpl->req = r->copy(); + tmpl->req = r->copy()->as_http(); sflvs[sid] = entry; diff --git a/trunk/src/app/srs_app_http_stream.hpp b/trunk/src/app/srs_app_http_stream.hpp index 60fc23bc5d..18c84a5d05 100755 --- a/trunk/src/app/srs_app_http_stream.hpp +++ b/trunk/src/app/srs_app_http_stream.hpp @@ -232,6 +232,9 @@ class SrsLiveStream : public ISrsHttpHandler public: virtual int serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r); private: + virtual int do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r); + virtual int http_hooks_on_play(); + virtual void http_hooks_on_stop(); virtual int streaming_send_messages(ISrsStreamEncoder* enc, SrsSharedPtrMessage** msgs, int nb_msgs); }; diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 7b49613ff2..2c41b8f697 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // current release version #define VERSION_MAJOR 2 #define VERSION_MINOR 0 -#define VERSION_REVISION 253 +#define VERSION_REVISION 254 // generated by configure, only macros. #include diff --git a/trunk/src/protocol/srs_rtmp_stack.cpp b/trunk/src/protocol/srs_rtmp_stack.cpp index 8095092cc0..fdf45c2240 100644 --- a/trunk/src/protocol/srs_rtmp_stack.cpp +++ b/trunk/src/protocol/srs_rtmp_stack.cpp @@ -1776,6 +1776,12 @@ void SrsRequest::strip() stream = srs_string_trim_start(stream, "/"); } +SrsRequest* SrsRequest::as_http() +{ + schema = "http"; + return this; +} + SrsResponse::SrsResponse() { stream_id = SRS_DEFAULT_SID; diff --git a/trunk/src/protocol/srs_rtmp_stack.hpp b/trunk/src/protocol/srs_rtmp_stack.hpp index 3c8d3edf95..9900000dc0 100644 --- a/trunk/src/protocol/srs_rtmp_stack.hpp +++ b/trunk/src/protocol/srs_rtmp_stack.hpp @@ -606,6 +606,9 @@ class SrsRequest * strip url, user must strip when update the url. */ virtual void strip(); +public: + // Transform it as HTTP request. + virtual SrsRequest* as_http(); }; /** diff --git a/trunk/src/protocol/srs_rtmp_utility.cpp b/trunk/src/protocol/srs_rtmp_utility.cpp index fdbaddb05d..c322a97968 100644 --- a/trunk/src/protocol/srs_rtmp_utility.cpp +++ b/trunk/src/protocol/srs_rtmp_utility.cpp @@ -71,6 +71,10 @@ void srs_discovery_tc_url( vhost = host; srs_vhost_resolve(vhost, app, param); srs_vhost_resolve(vhost, stream, param); + + if (param == "?vhost="SRS_CONSTS_RTMP_DEFAULT_VHOST) { + param = ""; + } } void srs_vhost_resolve(string& vhost, string& app, string& param)