Skip to content

Commit

Permalink
For #910, Support HTTP FLV with HTTP callback. 2.0.254
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Aug 11, 2018
1 parent 6cea551 commit 1e7c12a
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions trunk/src/app/srs_app_http_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
94 changes: 90 additions & 4 deletions trunk/src/app/srs_app_http_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ using namespace std;
#include <srs_app_source.hpp>
#include <srs_app_server.hpp>
#include <srs_app_recv_thread.hpp>
#include <srs_app_http_hooks.hpp>

#endif

Expand All @@ -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);
Expand Down Expand Up @@ -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()
Expand All @@ -472,14 +473,30 @@ int SrsLiveStream::update(SrsSource* s, SrsRequest* r)
srs_freep(req);
source = s;
req = r->copy();

return ret;
}

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);
Expand Down Expand Up @@ -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<string> 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<string> 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;
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions trunk/src/app/srs_app_http_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <srs_auto_headers.hpp>
Expand Down
6 changes: 6 additions & 0 deletions trunk/src/protocol/srs_rtmp_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions trunk/src/protocol/srs_rtmp_stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

/**
Expand Down
4 changes: 4 additions & 0 deletions trunk/src/protocol/srs_rtmp_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

2 comments on commit 1e7c12a

@fastfading
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function is left empty cause Dvr Segment Plan last flv.tmp file was not rename and http call back after rtmp client is closed
I just copy frome void SrsDvrSessionPlan::on_unpublish()
will this help ?

void SrsDvrSegmentPlan::on_unpublish()
{
    srs_trace(SrsDvrSegmentPlan::on_unpublish);
    // copy frome void SrsDvrSessionPlan::on_unpublish()
    if (!dvr_enabled) {
        return;
    }

    // ignore error.
    int ret = segment->close();
    if (ret != ERROR_SUCCESS) {
        srs_warn("ignore flv close error. ret=%d", ret);
    }

    dvr_enabled = false;
}

@winlinvip
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function is left empty cause Dvr Segment Plan last flv.tmp file was not rename and http call back after rtmp client is closed
I just copy frome void SrsDvrSessionPlan::on_unpublish()
will this help ?

void SrsDvrSegmentPlan::on_unpublish()
{
    srs_trace(SrsDvrSegmentPlan::on_unpublish);
    // copy frome void SrsDvrSessionPlan::on_unpublish()
    if (!dvr_enabled) {
        return;
    }

    // ignore error.
    int ret = segment->close();
    if (ret != ERROR_SUCCESS) {
        srs_warn("ignore flv close error. ret=%d", ret);
    }

    dvr_enabled = false;
}

Please file a PR to 3.0release.

Please # to comment.