2017-03-25 09:21:39 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2018-01-07 02:58:53 +00:00
|
|
|
* Copyright (c) 2013-2018 Winlin
|
2017-03-25 09:21:39 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2014-04-01 10:40:24 +00:00
|
|
|
|
|
|
|
#include <srs_app_http_hooks.hpp>
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <srs_kernel_error.hpp>
|
2015-06-13 08:04:59 +00:00
|
|
|
#include <srs_rtmp_stack.hpp>
|
2015-06-14 00:43:38 +00:00
|
|
|
#include <srs_app_st.hpp>
|
2015-08-21 08:20:19 +00:00
|
|
|
#include <srs_protocol_json.hpp>
|
2014-04-23 09:53:14 +00:00
|
|
|
#include <srs_app_dvr.hpp>
|
2014-05-19 09:39:01 +00:00
|
|
|
#include <srs_app_http_client.hpp>
|
2015-03-06 03:51:20 +00:00
|
|
|
#include <srs_core_autofree.hpp>
|
2015-03-31 09:52:49 +00:00
|
|
|
#include <srs_app_config.hpp>
|
2015-04-10 04:01:45 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2015-05-22 14:34:03 +00:00
|
|
|
#include <srs_app_http_conn.hpp>
|
2015-09-22 01:05:21 +00:00
|
|
|
#include <srs_protocol_amf0.hpp>
|
2015-12-11 03:29:10 +00:00
|
|
|
#include <srs_app_utility.hpp>
|
2014-04-01 10:40:24 +00:00
|
|
|
|
2015-03-21 02:25:03 +00:00
|
|
|
#define SRS_HTTP_RESPONSE_OK SRS_XSTR(ERROR_SUCCESS)
|
2014-04-01 10:40:24 +00:00
|
|
|
|
2017-01-17 04:25:30 +00:00
|
|
|
#define SRS_HTTP_HEADER_BUFFER 1024
|
|
|
|
#define SRS_HTTP_READ_BUFFER 4096
|
|
|
|
#define SRS_HTTP_BODY_BUFFER (32 * 1024)
|
2014-04-01 10:40:24 +00:00
|
|
|
|
2017-01-17 04:25:30 +00:00
|
|
|
// the timeout for hls notify, in ms.
|
|
|
|
#define SRS_HLS_NOTIFY_TMMS (10 * 1000)
|
2015-04-10 05:45:21 +00:00
|
|
|
|
2014-04-01 10:40:24 +00:00
|
|
|
SrsHttpHooks::SrsHttpHooks()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpHooks::~SrsHttpHooks()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t SrsHttpHooks::on_connect(string url, SrsRequest* req)
|
2014-04-01 10:40:24 +00:00
|
|
|
{
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t err = srs_success;
|
2014-04-01 10:40:24 +00:00
|
|
|
|
2015-03-31 09:52:49 +00:00
|
|
|
int client_id = _srs_context->get_id();
|
|
|
|
|
2015-09-19 04:27:31 +00:00
|
|
|
SrsJsonObject* obj = SrsJsonAny::object();
|
|
|
|
SrsAutoFree(SrsJsonObject, obj);
|
|
|
|
|
|
|
|
obj->set("action", SrsJsonAny::str("on_connect"));
|
2015-09-19 06:40:32 +00:00
|
|
|
obj->set("client_id", SrsJsonAny::integer(client_id));
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
|
|
|
|
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
|
|
|
|
obj->set("app", SrsJsonAny::str(req->app.c_str()));
|
|
|
|
obj->set("tcUrl", SrsJsonAny::str(req->tcUrl.c_str()));
|
|
|
|
obj->set("pageUrl", SrsJsonAny::str(req->pageUrl.c_str()));
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-09-19 05:37:56 +00:00
|
|
|
std::string data = obj->dumps();
|
2014-04-01 10:40:24 +00:00
|
|
|
std::string res;
|
2015-01-02 02:21:04 +00:00
|
|
|
int status_code;
|
2015-09-17 03:37:35 +00:00
|
|
|
|
|
|
|
SrsHttpClient http;
|
2017-09-22 11:54:50 +00:00
|
|
|
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http: on_connect failed, client_id=%d, url=%s, request=%s, response=%s, code=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), status_code);
|
2014-04-01 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_trace("http: on_connect ok, client_id=%d, url=%s, request=%s, response=%s",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str());
|
2014-04-01 10:40:24 +00:00
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
return err;
|
2014-04-01 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 09:52:49 +00:00
|
|
|
void SrsHttpHooks::on_close(string url, SrsRequest* req, int64_t send_bytes, int64_t recv_bytes)
|
2014-04-01 10:40:24 +00:00
|
|
|
{
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t err = srs_success;
|
2014-04-01 10:40:24 +00:00
|
|
|
|
2015-03-31 09:52:49 +00:00
|
|
|
int client_id = _srs_context->get_id();
|
|
|
|
|
2015-09-19 04:27:31 +00:00
|
|
|
SrsJsonObject* obj = SrsJsonAny::object();
|
|
|
|
SrsAutoFree(SrsJsonObject, obj);
|
2015-08-28 07:11:11 +00:00
|
|
|
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("action", SrsJsonAny::str("on_close"));
|
2015-09-19 06:40:32 +00:00
|
|
|
obj->set("client_id", SrsJsonAny::integer(client_id));
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
|
|
|
|
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
|
|
|
|
obj->set("app", SrsJsonAny::str(req->app.c_str()));
|
2015-09-19 06:40:32 +00:00
|
|
|
obj->set("send_bytes", SrsJsonAny::integer(send_bytes));
|
|
|
|
obj->set("recv_bytes", SrsJsonAny::integer(recv_bytes));
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-09-19 05:37:56 +00:00
|
|
|
std::string data = obj->dumps();
|
2014-04-01 10:40:24 +00:00
|
|
|
std::string res;
|
2015-01-02 02:21:04 +00:00
|
|
|
int status_code;
|
2015-09-17 03:37:35 +00:00
|
|
|
|
|
|
|
SrsHttpClient http;
|
2017-09-22 11:54:50 +00:00
|
|
|
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
|
|
|
|
int ret = srs_error_code(err);
|
|
|
|
srs_freep(err);
|
|
|
|
srs_warn("http: ignore on_close failed, client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret);
|
2014-04-01 10:40:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_trace("http: on_close ok, client_id=%d, url=%s, request=%s, response=%s",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str());
|
2014-04-01 10:40:24 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t SrsHttpHooks::on_publish(string url, SrsRequest* req)
|
2014-04-01 10:40:24 +00:00
|
|
|
{
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t err = srs_success;
|
2014-04-01 10:40:24 +00:00
|
|
|
|
2015-03-31 09:52:49 +00:00
|
|
|
int client_id = _srs_context->get_id();
|
|
|
|
|
2015-09-19 04:27:31 +00:00
|
|
|
SrsJsonObject* obj = SrsJsonAny::object();
|
|
|
|
SrsAutoFree(SrsJsonObject, obj);
|
2015-08-28 07:11:11 +00:00
|
|
|
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("action", SrsJsonAny::str("on_publish"));
|
2015-09-19 06:40:32 +00:00
|
|
|
obj->set("client_id", SrsJsonAny::integer(client_id));
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
|
|
|
|
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
|
|
|
|
obj->set("app", SrsJsonAny::str(req->app.c_str()));
|
2016-01-09 08:06:22 +00:00
|
|
|
obj->set("tcUrl", SrsJsonAny::str(req->tcUrl.c_str()));
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("stream", SrsJsonAny::str(req->stream.c_str()));
|
2018-08-04 11:51:04 +00:00
|
|
|
obj->set("param", SrsJsonAny::str(req->param.c_str()));
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-09-19 05:37:56 +00:00
|
|
|
std::string data = obj->dumps();
|
2014-04-01 10:40:24 +00:00
|
|
|
std::string res;
|
2015-01-02 02:21:04 +00:00
|
|
|
int status_code;
|
2015-09-17 03:37:35 +00:00
|
|
|
|
|
|
|
SrsHttpClient http;
|
2017-09-22 11:54:50 +00:00
|
|
|
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http: on_publish failed, client_id=%d, url=%s, request=%s, response=%s, code=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), status_code);
|
2014-04-01 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_trace("http: on_publish ok, client_id=%d, url=%s, request=%s, response=%s",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str());
|
2014-04-01 10:40:24 +00:00
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
return err;
|
2014-04-01 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 09:52:49 +00:00
|
|
|
void SrsHttpHooks::on_unpublish(string url, SrsRequest* req)
|
2014-04-01 10:40:24 +00:00
|
|
|
{
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t err = srs_success;
|
2014-04-01 10:40:24 +00:00
|
|
|
|
2015-03-31 09:52:49 +00:00
|
|
|
int client_id = _srs_context->get_id();
|
|
|
|
|
2015-09-19 04:27:31 +00:00
|
|
|
SrsJsonObject* obj = SrsJsonAny::object();
|
|
|
|
SrsAutoFree(SrsJsonObject, obj);
|
2015-08-28 07:11:11 +00:00
|
|
|
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("action", SrsJsonAny::str("on_unpublish"));
|
2015-09-19 06:40:32 +00:00
|
|
|
obj->set("client_id", SrsJsonAny::integer(client_id));
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
|
|
|
|
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
|
|
|
|
obj->set("app", SrsJsonAny::str(req->app.c_str()));
|
|
|
|
obj->set("stream", SrsJsonAny::str(req->stream.c_str()));
|
2018-08-04 11:51:04 +00:00
|
|
|
obj->set("param", SrsJsonAny::str(req->param.c_str()));
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-09-19 05:37:56 +00:00
|
|
|
std::string data = obj->dumps();
|
2014-04-01 10:40:24 +00:00
|
|
|
std::string res;
|
2015-01-02 02:21:04 +00:00
|
|
|
int status_code;
|
2015-09-17 03:37:35 +00:00
|
|
|
|
|
|
|
SrsHttpClient http;
|
2017-09-22 11:54:50 +00:00
|
|
|
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
|
|
|
|
int ret = srs_error_code(err);
|
|
|
|
srs_freep(err);
|
|
|
|
srs_warn("http: ignore on_unpublish failed, client_id=%d, url=%s, request=%s, response=%s, status=%d, ret=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret);
|
2014-04-01 10:40:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_trace("http: on_unpublish ok, client_id=%d, url=%s, request=%s, response=%s",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str());
|
2014-04-01 10:40:24 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t SrsHttpHooks::on_play(string url, SrsRequest* req)
|
2014-04-01 10:40:24 +00:00
|
|
|
{
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t err = srs_success;
|
2014-04-01 10:40:24 +00:00
|
|
|
|
2015-03-31 09:52:49 +00:00
|
|
|
int client_id = _srs_context->get_id();
|
|
|
|
|
2015-09-19 04:27:31 +00:00
|
|
|
SrsJsonObject* obj = SrsJsonAny::object();
|
|
|
|
SrsAutoFree(SrsJsonObject, obj);
|
2015-08-28 07:11:11 +00:00
|
|
|
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("action", SrsJsonAny::str("on_play"));
|
2015-09-19 06:40:32 +00:00
|
|
|
obj->set("client_id", SrsJsonAny::integer(client_id));
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
|
|
|
|
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
|
|
|
|
obj->set("app", SrsJsonAny::str(req->app.c_str()));
|
|
|
|
obj->set("stream", SrsJsonAny::str(req->stream.c_str()));
|
2018-08-04 11:51:04 +00:00
|
|
|
obj->set("param", SrsJsonAny::str(req->param.c_str()));
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("pageUrl", SrsJsonAny::str(req->pageUrl.c_str()));
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-09-19 05:37:56 +00:00
|
|
|
std::string data = obj->dumps();
|
2014-04-01 10:40:24 +00:00
|
|
|
std::string res;
|
2015-01-02 02:21:04 +00:00
|
|
|
int status_code;
|
2015-09-17 03:37:35 +00:00
|
|
|
|
|
|
|
SrsHttpClient http;
|
2017-09-22 11:54:50 +00:00
|
|
|
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http: on_play failed, client_id=%d, url=%s, request=%s, response=%s, status=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), status_code);
|
2014-04-01 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_trace("http: on_play ok, client_id=%d, url=%s, request=%s, response=%s",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str());
|
2014-04-01 10:40:24 +00:00
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
return err;
|
2014-04-01 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 09:52:49 +00:00
|
|
|
void SrsHttpHooks::on_stop(string url, SrsRequest* req)
|
2014-04-01 10:40:24 +00:00
|
|
|
{
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t err = srs_success;
|
2014-04-01 10:40:24 +00:00
|
|
|
|
2015-03-31 09:52:49 +00:00
|
|
|
int client_id = _srs_context->get_id();
|
|
|
|
|
2015-09-19 04:27:31 +00:00
|
|
|
SrsJsonObject* obj = SrsJsonAny::object();
|
|
|
|
SrsAutoFree(SrsJsonObject, obj);
|
2015-08-28 07:11:11 +00:00
|
|
|
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("action", SrsJsonAny::str("on_stop"));
|
2015-09-19 06:40:32 +00:00
|
|
|
obj->set("client_id", SrsJsonAny::integer(client_id));
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
|
|
|
|
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
|
|
|
|
obj->set("app", SrsJsonAny::str(req->app.c_str()));
|
|
|
|
obj->set("stream", SrsJsonAny::str(req->stream.c_str()));
|
2018-08-04 11:51:04 +00:00
|
|
|
obj->set("param", SrsJsonAny::str(req->param.c_str()));
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-09-19 05:37:56 +00:00
|
|
|
std::string data = obj->dumps();
|
2014-04-01 10:40:24 +00:00
|
|
|
std::string res;
|
2015-01-02 02:21:04 +00:00
|
|
|
int status_code;
|
2015-09-17 03:37:35 +00:00
|
|
|
|
|
|
|
SrsHttpClient http;
|
2017-09-22 11:54:50 +00:00
|
|
|
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
|
|
|
|
int ret = srs_error_code(err);
|
|
|
|
srs_freep(err);
|
|
|
|
srs_warn("http: ignore on_stop failed, client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret);
|
2014-04-01 10:40:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_trace("http: on_stop ok, client_id=%d, url=%s, request=%s, response=%s",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str());
|
2014-04-01 10:40:24 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t SrsHttpHooks::on_dvr(int cid, string url, SrsRequest* req, string file)
|
2015-01-03 07:33:23 +00:00
|
|
|
{
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-01-03 07:33:23 +00:00
|
|
|
|
2015-09-14 05:47:25 +00:00
|
|
|
int client_id = cid;
|
2015-03-31 09:52:49 +00:00
|
|
|
std::string cwd = _srs_config->cwd();
|
|
|
|
|
2015-09-19 04:27:31 +00:00
|
|
|
SrsJsonObject* obj = SrsJsonAny::object();
|
|
|
|
SrsAutoFree(SrsJsonObject, obj);
|
|
|
|
|
|
|
|
obj->set("action", SrsJsonAny::str("on_dvr"));
|
2015-09-19 06:40:32 +00:00
|
|
|
obj->set("client_id", SrsJsonAny::integer(client_id));
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
|
|
|
|
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
|
|
|
|
obj->set("app", SrsJsonAny::str(req->app.c_str()));
|
|
|
|
obj->set("stream", SrsJsonAny::str(req->stream.c_str()));
|
2018-08-04 11:51:04 +00:00
|
|
|
obj->set("param", SrsJsonAny::str(req->param.c_str()));
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("cwd", SrsJsonAny::str(cwd.c_str()));
|
|
|
|
obj->set("file", SrsJsonAny::str(file.c_str()));
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-09-19 05:37:56 +00:00
|
|
|
std::string data = obj->dumps();
|
2015-01-03 07:33:23 +00:00
|
|
|
std::string res;
|
|
|
|
int status_code;
|
2015-09-17 03:37:35 +00:00
|
|
|
|
|
|
|
SrsHttpClient http;
|
2017-09-22 11:54:50 +00:00
|
|
|
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http post on_dvr uri failed, client_id=%d, url=%s, request=%s, response=%s, code=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), status_code);
|
2015-01-03 07:33:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_trace("http hook on_dvr success. client_id=%d, url=%s, request=%s, response=%s",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str());
|
2015-01-03 07:33:23 +00:00
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
return err;
|
2015-01-03 07:33:23 +00:00
|
|
|
}
|
2014-08-02 14:18:39 +00:00
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t SrsHttpHooks::on_hls(int cid, string url, SrsRequest* req, string file, string ts_url, string m3u8, string m3u8_url, int sn, double duration)
|
2015-02-22 02:45:13 +00:00
|
|
|
{
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-02-22 02:45:13 +00:00
|
|
|
|
2015-09-14 05:47:25 +00:00
|
|
|
int client_id = cid;
|
2015-03-31 09:52:49 +00:00
|
|
|
std::string cwd = _srs_config->cwd();
|
|
|
|
|
2015-12-11 03:29:10 +00:00
|
|
|
// the ts_url is under the same dir of m3u8_url.
|
|
|
|
string prefix = srs_path_dirname(m3u8_url);
|
|
|
|
if (!prefix.empty() && !srs_string_is_http(ts_url)) {
|
|
|
|
ts_url = prefix + "/" + ts_url;
|
|
|
|
}
|
|
|
|
|
2015-09-19 04:27:31 +00:00
|
|
|
SrsJsonObject* obj = SrsJsonAny::object();
|
|
|
|
SrsAutoFree(SrsJsonObject, obj);
|
|
|
|
|
|
|
|
obj->set("action", SrsJsonAny::str("on_hls"));
|
2015-09-19 06:40:32 +00:00
|
|
|
obj->set("client_id", SrsJsonAny::integer(client_id));
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
|
|
|
|
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
|
|
|
|
obj->set("app", SrsJsonAny::str(req->app.c_str()));
|
|
|
|
obj->set("stream", SrsJsonAny::str(req->stream.c_str()));
|
2018-08-04 11:51:04 +00:00
|
|
|
obj->set("param", SrsJsonAny::str(req->param.c_str()));
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("duration", SrsJsonAny::number(duration));
|
|
|
|
obj->set("cwd", SrsJsonAny::str(cwd.c_str()));
|
|
|
|
obj->set("file", SrsJsonAny::str(file.c_str()));
|
2015-12-29 10:33:02 +00:00
|
|
|
obj->set("url", SrsJsonAny::str(ts_url.c_str()));
|
2015-09-19 04:27:31 +00:00
|
|
|
obj->set("m3u8", SrsJsonAny::str(m3u8.c_str()));
|
|
|
|
obj->set("m3u8_url", SrsJsonAny::str(m3u8_url.c_str()));
|
2015-09-19 06:40:32 +00:00
|
|
|
obj->set("seq_no", SrsJsonAny::integer(sn));
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-09-19 05:37:56 +00:00
|
|
|
std::string data = obj->dumps();
|
2015-02-22 02:45:13 +00:00
|
|
|
std::string res;
|
|
|
|
int status_code;
|
2015-09-17 03:37:35 +00:00
|
|
|
|
|
|
|
SrsHttpClient http;
|
2017-09-22 11:54:50 +00:00
|
|
|
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http: post %s with %s, status=%d, res=%s", url.c_str(), data.c_str(), status_code, res.c_str());
|
2015-02-22 02:45:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_trace("http: on_hls ok, client_id=%d, url=%s, request=%s, response=%s",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str());
|
2015-03-06 03:51:20 +00:00
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
return err;
|
2015-03-06 03:51:20 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t SrsHttpHooks::on_hls_notify(int cid, std::string url, SrsRequest* req, std::string ts_url, int nb_notify)
|
2015-04-10 04:01:45 +00:00
|
|
|
{
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-04-10 04:01:45 +00:00
|
|
|
|
2015-09-14 05:47:25 +00:00
|
|
|
int client_id = cid;
|
2015-04-10 04:01:45 +00:00
|
|
|
std::string cwd = _srs_config->cwd();
|
|
|
|
|
2015-12-11 03:29:10 +00:00
|
|
|
if (srs_string_is_http(ts_url)) {
|
2015-04-10 04:01:45 +00:00
|
|
|
url = ts_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
url = srs_string_replace(url, "[app]", req->app);
|
|
|
|
url = srs_string_replace(url, "[stream]", req->stream);
|
|
|
|
url = srs_string_replace(url, "[ts_url]", ts_url);
|
2018-08-04 11:51:04 +00:00
|
|
|
url = srs_string_replace(url, "[param]", req->param);
|
2015-04-10 04:01:45 +00:00
|
|
|
|
2015-04-10 04:30:22 +00:00
|
|
|
int64_t starttime = srs_update_system_time_ms();
|
|
|
|
|
2015-04-10 04:01:45 +00:00
|
|
|
SrsHttpUri uri;
|
2018-01-01 11:39:57 +00:00
|
|
|
if ((err = uri.initialize(url)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http: init url=%s", url.c_str());
|
2015-04-10 04:01:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpClient http;
|
2017-09-22 11:54:50 +00:00
|
|
|
if ((err = http.initialize(uri.get_host(), uri.get_port(), SRS_HLS_NOTIFY_TMMS)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http: init client for %s", url.c_str());
|
2015-04-10 04:01:45 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 09:53:53 +00:00
|
|
|
std::string path = uri.get_query();
|
|
|
|
if (path.empty()) {
|
|
|
|
path = uri.get_path();
|
|
|
|
} else {
|
|
|
|
path = uri.get_path();
|
|
|
|
path += "?";
|
|
|
|
path += uri.get_query();
|
|
|
|
}
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_info("GET %s", path.c_str());
|
2015-04-14 09:53:53 +00:00
|
|
|
|
2015-05-22 14:24:05 +00:00
|
|
|
ISrsHttpMessage* msg = NULL;
|
2017-09-22 11:54:50 +00:00
|
|
|
if ((err = http.get(path.c_str(), "", &msg)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http: get %s", url.c_str());
|
2015-04-10 04:01:45 +00:00
|
|
|
}
|
2015-05-22 14:24:05 +00:00
|
|
|
SrsAutoFree(ISrsHttpMessage, msg);
|
2015-04-10 04:01:45 +00:00
|
|
|
|
2015-04-10 07:00:07 +00:00
|
|
|
int nb_buf = srs_min(nb_notify, SRS_HTTP_READ_BUFFER);
|
|
|
|
char* buf = new char[nb_buf];
|
2015-11-02 03:29:20 +00:00
|
|
|
SrsAutoFreeA(char, buf);
|
2015-04-10 07:00:07 +00:00
|
|
|
|
2015-04-10 06:17:49 +00:00
|
|
|
int nb_read = 0;
|
2015-04-10 04:01:45 +00:00
|
|
|
ISrsHttpResponseReader* br = msg->body_reader();
|
2015-04-10 06:44:18 +00:00
|
|
|
while (nb_read < nb_notify && !br->eof()) {
|
2015-04-10 07:00:07 +00:00
|
|
|
int nb_bytes = 0;
|
2018-01-01 11:39:57 +00:00
|
|
|
if ((err = br->read(buf, nb_buf, &nb_bytes)) != srs_success) {
|
2015-04-10 06:44:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-04-10 07:00:07 +00:00
|
|
|
nb_read += nb_bytes;
|
2015-04-10 04:01:45 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 04:30:22 +00:00
|
|
|
int spenttime = (int)(srs_update_system_time_ms() - starttime);
|
2018-01-01 11:39:57 +00:00
|
|
|
srs_trace("http hook on_hls_notify success. client_id=%d, url=%s, code=%d, spent=%dms, read=%dB, err=%s",
|
|
|
|
client_id, url.c_str(), msg->status_code(), spenttime, nb_read, srs_error_desc(err).c_str());
|
2015-04-10 04:01:45 +00:00
|
|
|
|
|
|
|
// ignore any error for on_hls_notify.
|
2018-01-01 11:39:57 +00:00
|
|
|
srs_error_reset(err);
|
|
|
|
return srs_success;
|
2015-04-10 04:01:45 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 08:39:07 +00:00
|
|
|
srs_error_t SrsHttpHooks::discover_co_workers(string url, string& host, int& port)
|
|
|
|
{
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
std::string res;
|
|
|
|
int status_code;
|
|
|
|
|
|
|
|
SrsHttpClient http;
|
|
|
|
if ((err = do_post(&http, url, "", status_code, res)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http: post %s, status=%d, res=%s", url.c_str(), status_code, res.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsJsonObject* robj = NULL;
|
|
|
|
SrsAutoFree(SrsJsonObject, robj);
|
|
|
|
|
|
|
|
if (true) {
|
|
|
|
SrsJsonAny* jr = NULL;
|
|
|
|
if ((jr = SrsJsonAny::loads(res)) == NULL) {
|
|
|
|
return srs_error_new(ERROR_OCLUSTER_DISCOVER, "load json from %s", res.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!jr->is_object()) {
|
|
|
|
srs_freep(jr);
|
|
|
|
return srs_error_new(ERROR_OCLUSTER_DISCOVER, "response %s", res.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
robj = jr->to_object();
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsJsonAny* prop = NULL;
|
|
|
|
if ((prop = robj->ensure_property_object("data")) == NULL) {
|
|
|
|
return srs_error_new(ERROR_OCLUSTER_DISCOVER, "parse data %s", res.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsJsonObject* p = prop->to_object();
|
|
|
|
if ((prop = p->ensure_property_object("origin")) == NULL) {
|
|
|
|
return srs_error_new(ERROR_OCLUSTER_DISCOVER, "parse data %s", res.c_str());
|
|
|
|
}
|
|
|
|
p = prop->to_object();
|
|
|
|
|
|
|
|
if ((prop = p->ensure_property_string("ip")) == NULL) {
|
|
|
|
return srs_error_new(ERROR_OCLUSTER_DISCOVER, "parse data %s", res.c_str());
|
|
|
|
}
|
|
|
|
host = prop->to_str();
|
|
|
|
|
|
|
|
if ((prop = p->ensure_property_integer("port")) == NULL) {
|
|
|
|
return srs_error_new(ERROR_OCLUSTER_DISCOVER, "parse data %s", res.c_str());
|
|
|
|
}
|
|
|
|
port = (int)prop->to_integer();
|
|
|
|
|
|
|
|
srs_trace("http: on_hls ok, url=%s, response=%s", url.c_str(), res.c_str());
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t SrsHttpHooks::do_post(SrsHttpClient* hc, std::string url, std::string req, int& code, string& res)
|
2015-03-06 03:51:20 +00:00
|
|
|
{
|
2017-09-22 11:54:50 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-03-06 03:51:20 +00:00
|
|
|
|
|
|
|
SrsHttpUri uri;
|
2018-01-01 11:39:57 +00:00
|
|
|
if ((err = uri.initialize(url)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http: post failed. url=%s", url.c_str());
|
2015-02-22 02:45:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
if ((err = hc->initialize(uri.get_host(), uri.get_port())) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http: init client");
|
2015-03-06 04:07:12 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 08:39:07 +00:00
|
|
|
string path = uri.get_path();
|
|
|
|
if (!uri.get_query().empty()) {
|
|
|
|
path += "?" + uri.get_query();
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:24:05 +00:00
|
|
|
ISrsHttpMessage* msg = NULL;
|
2018-02-16 08:39:07 +00:00
|
|
|
if ((err = hc->post(path, req, &msg)) != srs_success) {
|
2017-09-22 11:54:50 +00:00
|
|
|
return srs_error_wrap(err, "http: client post");
|
2015-02-22 02:45:13 +00:00
|
|
|
}
|
2015-05-22 14:24:05 +00:00
|
|
|
SrsAutoFree(ISrsHttpMessage, msg);
|
2015-02-22 02:45:13 +00:00
|
|
|
|
2015-03-06 03:51:20 +00:00
|
|
|
code = msg->status_code();
|
2018-01-01 11:39:57 +00:00
|
|
|
if ((err = msg->body_read_all(res)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http: body read");
|
2015-03-06 03:51:20 +00:00
|
|
|
}
|
2015-02-22 02:45:13 +00:00
|
|
|
|
2015-03-06 03:51:20 +00:00
|
|
|
// ensure the http status is ok.
|
2015-11-11 02:37:50 +00:00
|
|
|
// https://github.com/ossrs/srs/issues/158
|
2015-12-29 10:33:02 +00:00
|
|
|
if (code != SRS_CONSTS_HTTP_OK && code != SRS_CONSTS_HTTP_Created) {
|
2017-09-22 11:54:50 +00:00
|
|
|
return srs_error_new(ERROR_HTTP_STATUS_INVALID, "http: status %d", code);
|
2015-03-06 03:51:20 +00:00
|
|
|
}
|
|
|
|
|
2015-10-04 00:09:39 +00:00
|
|
|
// should never be empty.
|
2015-06-08 09:28:39 +00:00
|
|
|
if (res.empty()) {
|
2017-09-22 11:54:50 +00:00
|
|
|
return srs_error_new(ERROR_HTTP_DATA_INVALID, "http: empty response");
|
2015-03-06 03:51:20 +00:00
|
|
|
}
|
2015-06-08 09:28:39 +00:00
|
|
|
|
|
|
|
// parse string res to json.
|
2017-05-29 23:40:18 +00:00
|
|
|
SrsJsonAny* info = SrsJsonAny::loads(res);
|
2015-10-04 00:09:39 +00:00
|
|
|
if (!info) {
|
2017-09-22 11:54:50 +00:00
|
|
|
return srs_error_new(ERROR_HTTP_DATA_INVALID, "http: not json %s", res.c_str());
|
2015-10-04 00:09:39 +00:00
|
|
|
}
|
2015-06-08 09:28:39 +00:00
|
|
|
SrsAutoFree(SrsJsonAny, info);
|
|
|
|
|
2015-10-04 00:09:39 +00:00
|
|
|
// response error code in string.
|
2015-06-08 09:28:39 +00:00
|
|
|
if (!info->is_object()) {
|
2017-09-22 11:54:50 +00:00
|
|
|
if (res == SRS_HTTP_RESPONSE_OK) {
|
|
|
|
return err;
|
2015-06-08 09:28:39 +00:00
|
|
|
}
|
2017-09-22 11:54:50 +00:00
|
|
|
return srs_error_new(ERROR_HTTP_DATA_INVALID, "http: response number code %s", res.c_str());
|
2015-06-08 09:28:39 +00:00
|
|
|
}
|
|
|
|
|
2015-10-04 00:09:39 +00:00
|
|
|
// response standard object, format in json: {"code": 0, "data": ""}
|
2015-06-08 09:28:39 +00:00
|
|
|
SrsJsonObject* res_info = info->to_object();
|
|
|
|
SrsJsonAny* res_code = NULL;
|
|
|
|
if ((res_code = res_info->ensure_property_integer("code")) == NULL) {
|
2017-09-22 11:54:50 +00:00
|
|
|
return srs_error_new(ERROR_RESPONSE_CODE, "http: response object no code %s", res.c_str());
|
2015-06-08 09:28:39 +00:00
|
|
|
}
|
2015-09-17 05:36:02 +00:00
|
|
|
|
2015-06-08 09:28:39 +00:00
|
|
|
if ((res_code->to_integer()) != ERROR_SUCCESS) {
|
2017-09-22 11:54:50 +00:00
|
|
|
return srs_error_new(ERROR_RESPONSE_CODE, "http: response object code %d %s", res_code->to_integer(), res.c_str());
|
2015-06-08 09:28:39 +00:00
|
|
|
}
|
2015-09-17 05:36:02 +00:00
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
return err;
|
2015-02-22 02:45:13 +00:00
|
|
|
}
|