2014-04-01 10:40:24 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2015-04-29 09:38:23 +00:00
|
|
|
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
|
2014-04-01 10:40:24 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <srs_app_http_hooks.hpp>
|
|
|
|
|
2014-04-15 06:01:57 +00:00
|
|
|
#ifdef SRS_AUTO_HTTP_CALLBACK
|
2014-04-01 10:40:24 +00:00
|
|
|
|
|
|
|
#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>
|
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
|
|
|
|
|
|
|
#define SRS_HTTP_HEADER_BUFFER 1024
|
2015-04-10 07:00:07 +00:00
|
|
|
#define SRS_HTTP_READ_BUFFER 4096
|
2014-04-01 10:40:24 +00:00
|
|
|
#define SRS_HTTP_BODY_BUFFER 32 * 1024
|
|
|
|
|
2015-04-10 05:45:21 +00:00
|
|
|
// the timeout for hls notify, in us.
|
|
|
|
#define SRS_HLS_NOTIFY_TIMEOUT_US (int64_t)(10*1000*1000LL)
|
|
|
|
|
2014-04-01 10:40:24 +00:00
|
|
|
SrsHttpHooks::SrsHttpHooks()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpHooks::~SrsHttpHooks()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-03-31 09:52:49 +00:00
|
|
|
int SrsHttpHooks::on_connect(string url, SrsRequest* req)
|
2014-04-01 10:40:24 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
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()));
|
2015-03-06 03:51:20 +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;
|
|
|
|
if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) {
|
2014-04-01 10:40:24 +00:00
|
|
|
srs_error("http post on_connect uri failed. "
|
2015-03-06 03:51:20 +00:00
|
|
|
"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 ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_trace("http hook on_connect success. "
|
|
|
|
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
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));
|
2015-03-06 03:51:20 +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;
|
|
|
|
if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) {
|
2014-04-01 10:40:24 +00:00
|
|
|
srs_warn("http post on_close uri failed, ignored. "
|
2015-03-06 03:51:20 +00:00
|
|
|
"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;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_trace("http hook on_close success. "
|
|
|
|
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-31 09:52:49 +00:00
|
|
|
int SrsHttpHooks::on_publish(string url, SrsRequest* req)
|
2014-04-01 10:40:24 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
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()));
|
|
|
|
obj->set("stream", SrsJsonAny::str(req->stream.c_str()));
|
2015-03-06 03:51:20 +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;
|
|
|
|
if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) {
|
2014-04-01 10:40:24 +00:00
|
|
|
srs_error("http post on_publish uri failed. "
|
2015-03-06 03:51:20 +00:00
|
|
|
"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 ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_trace("http hook on_publish success. "
|
|
|
|
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-31 09:52:49 +00:00
|
|
|
void SrsHttpHooks::on_unpublish(string url, SrsRequest* req)
|
2014-04-01 10:40:24 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
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()));
|
2015-03-06 03:51:20 +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;
|
|
|
|
if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) {
|
2014-04-01 10:40:24 +00:00
|
|
|
srs_warn("http post on_unpublish uri failed, ignored. "
|
2015-03-06 03:51:20 +00:00
|
|
|
"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;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_trace("http hook on_unpublish success. "
|
|
|
|
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-31 09:52:49 +00:00
|
|
|
int SrsHttpHooks::on_play(string url, SrsRequest* req)
|
2014-04-01 10:40:24 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
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()));
|
|
|
|
obj->set("pageUrl", SrsJsonAny::str(req->pageUrl.c_str()));
|
2015-03-06 03:51:20 +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;
|
|
|
|
if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) {
|
2014-04-01 10:40:24 +00:00
|
|
|
srs_error("http post on_play uri failed. "
|
2015-03-06 03:51:20 +00:00
|
|
|
"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 ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_trace("http hook on_play success. "
|
|
|
|
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-31 09:52:49 +00:00
|
|
|
void SrsHttpHooks::on_stop(string url, SrsRequest* req)
|
2014-04-01 10:40:24 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
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()));
|
2015-03-06 03:51:20 +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;
|
|
|
|
if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) {
|
2014-04-01 10:40:24 +00:00
|
|
|
srs_warn("http post on_stop uri failed, ignored. "
|
2015-03-06 03:51:20 +00:00
|
|
|
"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;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_trace("http hook on_stop success. "
|
|
|
|
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-14 05:47:25 +00:00
|
|
|
int SrsHttpHooks::on_dvr(int cid, string url, SrsRequest* req, string file)
|
2015-01-03 07:33:23 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
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()));
|
|
|
|
obj->set("cwd", SrsJsonAny::str(cwd.c_str()));
|
|
|
|
obj->set("file", SrsJsonAny::str(file.c_str()));
|
2015-03-06 03:51:20 +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;
|
|
|
|
if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) {
|
2015-01-03 07:33:23 +00:00
|
|
|
srs_error("http post on_dvr uri failed, ignored. "
|
2015-03-06 03:51:20 +00:00
|
|
|
"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);
|
2015-01-03 07:33:23 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_trace("http hook on_dvr success. "
|
|
|
|
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2014-08-02 14:18:39 +00:00
|
|
|
|
2015-09-14 05:47:25 +00:00
|
|
|
int 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
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
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_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()));
|
|
|
|
obj->set("duration", SrsJsonAny::number(duration));
|
|
|
|
obj->set("cwd", SrsJsonAny::str(cwd.c_str()));
|
|
|
|
obj->set("file", SrsJsonAny::str(file.c_str()));
|
|
|
|
obj->set("url", SrsJsonAny::str(url.c_str()));
|
|
|
|
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));
|
2015-03-06 03:51:20 +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;
|
|
|
|
if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) {
|
2015-03-31 09:42:12 +00:00
|
|
|
srs_error("http post on_hls uri failed, ignored. "
|
2015-03-06 03:51:20 +00:00
|
|
|
"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);
|
2015-02-22 02:45:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-31 09:42:12 +00:00
|
|
|
srs_trace("http hook on_hls success. "
|
2015-03-06 03:51:20 +00:00
|
|
|
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
|
|
|
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-14 05:47:25 +00:00
|
|
|
int 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
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (srs_string_starts_with(ts_url, "http://") || srs_string_starts_with(ts_url, "https://")) {
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {
|
|
|
|
srs_error("http: post failed. url=%s, ret=%d", url.c_str(), ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpClient http;
|
2015-04-10 05:45:21 +00:00
|
|
|
if ((ret = http.initialize(uri.get_host(), uri.get_port(), SRS_HLS_NOTIFY_TIMEOUT_US)) != ERROR_SUCCESS) {
|
2015-04-10 04:01:45 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
srs_warn("GET %s", path.c_str());
|
|
|
|
|
2015-05-22 14:24:05 +00:00
|
|
|
ISrsHttpMessage* msg = NULL;
|
2015-04-14 09:53:53 +00:00
|
|
|
if ((ret = http.get(path.c_str(), "", &msg)) != ERROR_SUCCESS) {
|
2015-04-10 04:01:45 +00:00
|
|
|
return ret;
|
|
|
|
}
|
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];
|
|
|
|
SrsAutoFree(char, buf);
|
|
|
|
|
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;
|
|
|
|
if ((ret = br->read(buf, nb_buf, &nb_bytes)) != ERROR_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);
|
2015-04-10 06:17:49 +00:00
|
|
|
srs_trace("http hook on_hls_notify success. client_id=%d, url=%s, code=%d, spent=%dms, read=%dB, ret=%d",
|
|
|
|
client_id, url.c_str(), msg->status_code(), spenttime, nb_read, ret);
|
2015-04-10 04:01:45 +00:00
|
|
|
|
|
|
|
// ignore any error for on_hls_notify.
|
|
|
|
ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-17 03:37:35 +00:00
|
|
|
int SrsHttpHooks::do_post(SrsHttpClient* hc, std::string url, std::string req, int& code, string& res)
|
2015-03-06 03:51:20 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
SrsHttpUri uri;
|
|
|
|
if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {
|
|
|
|
srs_error("http: post failed. url=%s, ret=%d", url.c_str(), ret);
|
2015-02-22 02:45:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-17 03:37:35 +00:00
|
|
|
if ((ret = hc->initialize(uri.get_host(), uri.get_port())) != ERROR_SUCCESS) {
|
2015-03-06 04:07:12 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:24:05 +00:00
|
|
|
ISrsHttpMessage* msg = NULL;
|
2015-09-17 03:37:35 +00:00
|
|
|
if ((ret = hc->post(uri.get_path(), req, &msg)) != ERROR_SUCCESS) {
|
2015-02-22 02:45:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
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();
|
|
|
|
if ((ret = msg->body_read_all(res)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
2015-02-22 02:45:13 +00:00
|
|
|
|
2015-03-06 03:51:20 +00:00
|
|
|
// ensure the http status is ok.
|
2015-04-29 09:06:32 +00:00
|
|
|
// https://github.com/simple-rtmp-server/srs/issues/158
|
2015-03-06 03:51:20 +00:00
|
|
|
if (code != SRS_CONSTS_HTTP_OK) {
|
2015-10-20 02:17:48 +00:00
|
|
|
ret = ERROR_HTTP_STATUS_INVALID;
|
2015-10-04 00:09:39 +00:00
|
|
|
srs_error("invalid response status=%d. ret=%d", code, ret);
|
|
|
|
return ret;
|
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()) {
|
2015-10-20 02:17:48 +00:00
|
|
|
ret = ERROR_HTTP_DATA_INVALID;
|
2015-10-04 00:09:39 +00:00
|
|
|
srs_error("invalid empty response. ret=%d", ret);
|
|
|
|
return ret;
|
2015-03-06 03:51:20 +00:00
|
|
|
}
|
2015-06-08 09:28:39 +00:00
|
|
|
|
|
|
|
// parse string res to json.
|
|
|
|
SrsJsonAny* info = SrsJsonAny::loads((char*)res.c_str());
|
2015-10-04 00:09:39 +00:00
|
|
|
if (!info) {
|
2015-10-20 02:17:48 +00:00
|
|
|
ret = ERROR_HTTP_DATA_INVALID;
|
2015-10-04 00:09:39 +00:00
|
|
|
srs_error("invalid response %s. ret=%d", res.c_str(), ret);
|
|
|
|
return ret;
|
|
|
|
}
|
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()) {
|
|
|
|
if (res != SRS_HTTP_RESPONSE_OK) {
|
2015-10-20 02:17:48 +00:00
|
|
|
ret = ERROR_HTTP_DATA_INVALID;
|
2015-10-04 00:09:39 +00:00
|
|
|
srs_error("invalid response number %s. ret=%d", res.c_str(), ret);
|
|
|
|
return ret;
|
2015-06-08 09:28:39 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
ret = ERROR_RESPONSE_CODE;
|
2015-10-04 00:09:39 +00:00
|
|
|
srs_error("invalid response without code, ret=%d", ret);
|
2015-06-08 09:28:39 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2015-09-17 05:36:02 +00:00
|
|
|
|
2015-06-08 09:28:39 +00:00
|
|
|
if ((res_code->to_integer()) != ERROR_SUCCESS) {
|
|
|
|
ret = ERROR_RESPONSE_CODE;
|
2015-10-04 00:09:39 +00:00
|
|
|
srs_error("error response code=%d. ret=%d", res_code->to_integer(), ret);
|
2015-06-08 09:28:39 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2015-09-17 05:36:02 +00:00
|
|
|
|
2015-02-22 02:45:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-03 07:33:23 +00:00
|
|
|
#endif
|