1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

support on_hls for http hooks. 2.0.152.

This commit is contained in:
winlin 2015-03-31 17:42:12 +08:00
parent 97442c56b6
commit c49bc0628e
15 changed files with 308 additions and 176 deletions

View file

@ -0,0 +1,98 @@
/*
The MIT License (MIT)
Copyright (c) 2013-2015 winlin
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_async_call.hpp>
using namespace std;
#include <srs_kernel_error.hpp>
#include <srs_kernel_log.hpp>
// the sleep interval for http async callback.
#define SRS_AUTO_ASYNC_CALLBACL_SLEEP_US 300000
ISrsDvrAsyncCall::ISrsDvrAsyncCall()
{
}
ISrsDvrAsyncCall::~ISrsDvrAsyncCall()
{
}
SrsDvrAsyncCallThread::SrsDvrAsyncCallThread()
{
pthread = new SrsThread("async", this, SRS_AUTO_ASYNC_CALLBACL_SLEEP_US, true);
}
SrsDvrAsyncCallThread::~SrsDvrAsyncCallThread()
{
stop();
srs_freep(pthread);
std::vector<ISrsDvrAsyncCall*>::iterator it;
for (it = callbacks.begin(); it != callbacks.end(); ++it) {
ISrsDvrAsyncCall* call = *it;
srs_freep(call);
}
callbacks.clear();
}
int SrsDvrAsyncCallThread::call(ISrsDvrAsyncCall* c)
{
int ret = ERROR_SUCCESS;
callbacks.push_back(c);
return ret;
}
int SrsDvrAsyncCallThread::start()
{
return pthread->start();
}
void SrsDvrAsyncCallThread::stop()
{
pthread->stop();
}
int SrsDvrAsyncCallThread::cycle()
{
int ret = ERROR_SUCCESS;
std::vector<ISrsDvrAsyncCall*> copies = callbacks;
callbacks.clear();
std::vector<ISrsDvrAsyncCall*>::iterator it;
for (it = copies.begin(); it != copies.end(); ++it) {
ISrsDvrAsyncCall* call = *it;
if ((ret = call->call()) != ERROR_SUCCESS) {
srs_warn("dvr: ignore callback %s, ret=%d", call->to_string().c_str(), ret);
}
srs_freep(call);
}
return ret;
}

View file

@ -0,0 +1,75 @@
/*
The MIT License (MIT)
Copyright (c) 2013-2015 winlin
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.
*/
#ifndef SRS_APP_ASYNC_CALL_HPP
#define SRS_APP_ASYNC_CALL_HPP
/*
#include <srs_app_async_call.hpp>
*/
#include <srs_core.hpp>
#include <string>
#include <vector>
#include <srs_app_thread.hpp>
/**
* the async call for http hooks,
* for the http hooks will switch st-thread,
* so we must use isolate thread to avoid the thread corrupt,
* for example, when dvr call http hooks, the video receive thread got
* a video and pass it to the dvr again.
* futhurmore, the aync call never block the main worker thread.
*/
class ISrsDvrAsyncCall
{
public:
ISrsDvrAsyncCall();
virtual ~ISrsDvrAsyncCall();
public:
virtual int call() = 0;
virtual std::string to_string() = 0;
};
/**
* the async callback for dvr.
*/
class SrsDvrAsyncCallThread : public ISrsThreadHandler
{
private:
SrsThread* pthread;
std::vector<ISrsDvrAsyncCall*> callbacks;
public:
SrsDvrAsyncCallThread();
virtual ~SrsDvrAsyncCallThread();
public:
virtual int call(ISrsDvrAsyncCall* c);
public:
virtual int start();
virtual void stop();
virtual int cycle();
};
#endif

View file

@ -1494,7 +1494,7 @@ int SrsConfig::check_config()
string m = conf->at(j)->name.c_str();
if (m != "enabled" && m != "on_connect" && m != "on_close" && m != "on_publish"
&& m != "on_unpublish" && m != "on_play" && m != "on_stop"
&& m != "on_dvr"
&& m != "on_dvr" && m != "on_hls"
) {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("unsupported vhost http_hooks directive %s, ret=%d", m.c_str(), ret);
@ -2403,14 +2403,25 @@ SrsConfDirective* SrsConfig::get_vhost_on_stop(string vhost)
SrsConfDirective* SrsConfig::get_vhost_on_dvr(string vhost)
{
SrsConfDirective* conf = get_vhost_http_hooks(vhost);
if (!conf) {
if (!conf) {
return NULL;
}
return conf->get("on_dvr");
}
SrsConfDirective* SrsConfig::get_vhost_on_hls(string vhost)
{
SrsConfDirective* conf = get_vhost_http_hooks(vhost);
if (!conf) {
return NULL;
}
return conf->get("on_hls");
}
bool SrsConfig::get_bw_check_enabled(string vhost)
{
SrsConfDirective* conf = get_vhost(vhost);

View file

@ -627,10 +627,15 @@ public:
*/
virtual SrsConfDirective* get_vhost_on_stop(std::string vhost);
/**
* get the on_dvr callbacks of vhost.
* @return the on_dvr callback directive, the args is the url to callback.
*/
* get the on_dvr callbacks of vhost.
* @return the on_dvr callback directive, the args is the url to callback.
*/
virtual SrsConfDirective* get_vhost_on_dvr(std::string vhost);
/**
* get the on_hls callbacks of vhost.
* @return the on_hls callback directive, the args is the url to callback.
*/
virtual SrsConfDirective* get_vhost_on_hls(std::string vhost);
// bwct(bandwidth check tool) section
public:
/**

View file

@ -46,12 +46,6 @@ using namespace std;
// update the flv duration and filesize every this interval in ms.
#define SRS_DVR_UPDATE_DURATION_INTERVAL 60000
// the sleep interval for http async callback.
#define SRS_AUTO_ASYNC_CALLBACL_SLEEP_US 300000
// the use raction for dvr rpc.
#define SRS_DVR_USER_ACTION_REAP_SEGMENT "reap_segment"
SrsFlvSegment::SrsFlvSegment(SrsDvrPlan* p)
{
req = NULL;
@ -502,14 +496,6 @@ int SrsFlvSegment::on_reload_vhost_dvr(std::string /*vhost*/)
return ret;
}
ISrsDvrAsyncCall::ISrsDvrAsyncCall()
{
}
ISrsDvrAsyncCall::~ISrsDvrAsyncCall()
{
}
SrsDvrAsyncCallOnDvr::SrsDvrAsyncCallOnDvr(SrsRequest* r, string p)
{
req = r;
@ -558,62 +544,6 @@ string SrsDvrAsyncCallOnDvr::to_string()
return ss.str();
}
SrsDvrAsyncCallThread::SrsDvrAsyncCallThread()
{
pthread = new SrsThread("async", this, SRS_AUTO_ASYNC_CALLBACL_SLEEP_US, true);
}
SrsDvrAsyncCallThread::~SrsDvrAsyncCallThread()
{
stop();
srs_freep(pthread);
std::vector<ISrsDvrAsyncCall*>::iterator it;
for (it = callbacks.begin(); it != callbacks.end(); ++it) {
ISrsDvrAsyncCall* call = *it;
srs_freep(call);
}
callbacks.clear();
}
int SrsDvrAsyncCallThread::call(ISrsDvrAsyncCall* c)
{
int ret = ERROR_SUCCESS;
callbacks.push_back(c);
return ret;
}
int SrsDvrAsyncCallThread::start()
{
return pthread->start();
}
void SrsDvrAsyncCallThread::stop()
{
pthread->stop();
}
int SrsDvrAsyncCallThread::cycle()
{
int ret = ERROR_SUCCESS;
std::vector<ISrsDvrAsyncCall*> copies = callbacks;
callbacks.clear();
std::vector<ISrsDvrAsyncCall*>::iterator it;
for (it = copies.begin(); it != copies.end(); ++it) {
ISrsDvrAsyncCall* call = *it;
if ((ret = call->call()) != ERROR_SUCCESS) {
srs_warn("dvr: ignore callback %s, ret=%d", call->to_string().c_str(), ret);
}
srs_freep(call);
}
return ret;
}
SrsDvrPlan::SrsDvrPlan()
{
req = NULL;

View file

@ -49,7 +49,7 @@ class SrsThread;
#include <srs_app_source.hpp>
#include <srs_app_reload.hpp>
#include <srs_app_thread.hpp>
#include <srs_app_async_call.hpp>
/**
* a piece of flv segment.
@ -178,15 +178,6 @@ public:
/**
* the dvr async call.
*/
class ISrsDvrAsyncCall
{
public:
ISrsDvrAsyncCall();
virtual ~ISrsDvrAsyncCall();
public:
virtual int call() = 0;
virtual std::string to_string() = 0;
};
class SrsDvrAsyncCallOnDvr : public ISrsDvrAsyncCall
{
private:
@ -200,25 +191,6 @@ public:
virtual std::string to_string();
};
/**
* the async callback for dvr.
*/
class SrsDvrAsyncCallThread : public ISrsThreadHandler
{
private:
SrsThread* pthread;
std::vector<ISrsDvrAsyncCall*> callbacks;
public:
SrsDvrAsyncCallThread();
virtual ~SrsDvrAsyncCallThread();
public:
virtual int call(ISrsDvrAsyncCall* c);
public:
virtual int start();
virtual void stop();
virtual int cycle();
};
/**
* the plan for dvr.
* use to control the following dvr params:

View file

@ -54,6 +54,7 @@ using namespace std;
#include <srs_rtmp_buffer.hpp>
#include <srs_kernel_ts.hpp>
#include <srs_app_utility.hpp>
#include <srs_app_http_hooks.hpp>
// drop the segment when duration of ts too small.
#define SRS_AUTO_HLS_SEGMENT_MIN_DURATION_MS 100
@ -169,6 +170,55 @@ void SrsHlsSegment::update_duration(int64_t current_frame_dts)
return;
}
SrsDvrAsyncCallOnHls::SrsDvrAsyncCallOnHls(SrsRequest* r, string p, int s)
{
req = r;
path = p;
seq_no = s;
}
SrsDvrAsyncCallOnHls::~SrsDvrAsyncCallOnHls()
{
}
int SrsDvrAsyncCallOnHls::call()
{
int ret = ERROR_SUCCESS;
#ifdef SRS_AUTO_HTTP_CALLBACK
// http callback for on_hls in config.
if (_srs_config->get_vhost_http_hooks_enabled(req->vhost)) {
// HTTP: on_hls
SrsConfDirective* on_hls = _srs_config->get_vhost_on_hls(req->vhost);
if (!on_hls) {
srs_info("ignore the empty http callback: on_hls");
return ret;
}
int connection_id = _srs_context->get_id();
std::string cwd = _srs_config->cwd();
std::string file = path;
int sn = seq_no;
for (int i = 0; i < (int)on_hls->args.size(); i++) {
std::string url = on_hls->args.at(i);
if ((ret = SrsHttpHooks::on_hls(url, connection_id, req, cwd, file, sn)) != ERROR_SUCCESS) {
srs_error("hook client on_hls failed. url=%s, ret=%d", url.c_str(), ret);
return ret;
}
}
}
#endif
return ret;
}
string SrsDvrAsyncCallOnHls::to_string()
{
std::stringstream ss;
ss << "vhost=" << req->vhost << ", file=" << path;
return ss.str();
}
SrsHlsMuxer::SrsHlsMuxer()
{
req = NULL;
@ -185,6 +235,7 @@ SrsHlsMuxer::SrsHlsMuxer()
acodec = SrsCodecAudioReserved1;
should_write_cache = false;
should_write_file = true;
async = new SrsDvrAsyncCallThread();
}
SrsHlsMuxer::~SrsHlsMuxer()
@ -198,6 +249,7 @@ SrsHlsMuxer::~SrsHlsMuxer()
srs_freep(current);
srs_freep(req);
srs_freep(async);
}
int SrsHlsMuxer::initialize(ISrsHlsHandler* h)
@ -205,6 +257,10 @@ int SrsHlsMuxer::initialize(ISrsHlsHandler* h)
int ret = ERROR_SUCCESS;
handler = h;
if ((ret = async->start()) != ERROR_SUCCESS) {
return ret;
}
return ret;
}
@ -523,6 +579,11 @@ int SrsHlsMuxer::segment_close(string log_desc)
if (hls_ts_floor) {
hls_fragment_deviation += (double)(hls_fragment - current->duration);
}
// use async to call the http hooks, for it will cause thread switch.
if ((ret = async->call(new SrsDvrAsyncCallOnHls(req, current->full_path, current->sequence_no))) != ERROR_SUCCESS) {
return ret;
}
srs_info("%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64", deviation=%.2f",
log_desc.c_str(), current->sequence_no, current->uri.c_str(), current->duration,

View file

@ -39,6 +39,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_kernel_codec.hpp>
#include <srs_kernel_file.hpp>
#include <srs_app_async_call.hpp>
class SrsSharedPtrMessage;
class SrsCodecSample;
@ -154,6 +155,23 @@ public:
virtual void update_duration(int64_t current_frame_dts);
};
/**
* the dvr async call.
*/
class SrsDvrAsyncCallOnHls : public ISrsDvrAsyncCall
{
private:
std::string path;
int seq_no;
SrsRequest* req;
public:
SrsDvrAsyncCallOnHls(SrsRequest* r, std::string p, int s);
virtual ~SrsDvrAsyncCallOnHls();
public:
virtual int call();
virtual std::string to_string();
};
/**
* muxer the HLS stream(m3u8 and ts files).
* generally, the m3u8 muxer only provides methods to open/close segments,
@ -174,6 +192,7 @@ private:
double hls_aof_ratio;
double hls_fragment;
double hls_window;
SrsDvrAsyncCallThread* async;
private:
// whether use floor algorithm for timestamp.
bool hls_ts_floor;

View file

@ -271,32 +271,34 @@ int SrsHttpHooks::on_dvr(string url, int client_id, string ip, SrsRequest* req,
return ret;
}
int SrsHttpHooks::on_dvr_reap_segment(string url, int client_id, SrsRequest* req, string cwd, string file)
int SrsHttpHooks::on_hls(string url, int client_id, SrsRequest* req, string cwd, string file, int sn)
{
int ret = ERROR_SUCCESS;
std::stringstream ss;
ss << SRS_JOBJECT_START
<< SRS_JFIELD_STR("action", "on_dvr_reap_segment") << SRS_JFIELD_CONT
<< SRS_JFIELD_STR("action", "on_hls") << SRS_JFIELD_CONT
<< SRS_JFIELD_ORG("client_id", client_id) << SRS_JFIELD_CONT
<< SRS_JFIELD_STR("ip", req->ip) << SRS_JFIELD_CONT
<< SRS_JFIELD_STR("vhost", req->vhost) << SRS_JFIELD_CONT
<< SRS_JFIELD_STR("app", req->app) << SRS_JFIELD_CONT
<< SRS_JFIELD_STR("stream", req->stream) << SRS_JFIELD_CONT
<< SRS_JFIELD_STR("cwd", cwd) << SRS_JFIELD_CONT
<< SRS_JFIELD_STR("file", file)
<< SRS_JFIELD_STR("file", file) << SRS_JFIELD_CONT
<< SRS_JFIELD_ORG("seq_no", sn)
<< SRS_JOBJECT_END;
std::string data = ss.str();
std::string res;
int status_code;
if ((ret = do_post(url, data, status_code, res)) != ERROR_SUCCESS) {
srs_error("http post on_dvr_reap_segment uri failed, ignored. "
srs_error("http post on_hls uri failed, ignored. "
"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);
return ret;
}
srs_trace("http hook on_dvr_reap_segment success. "
srs_trace("http hook on_hls success. "
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
client_id, url.c_str(), data.c_str(), res.c_str(), ret);

View file

@ -105,14 +105,15 @@ public:
*/
static int on_dvr(std::string url, int client_id, std::string ip, SrsRequest* req, std::string cwd, std::string file);
/**
* when dvr reap segment, callback.
* when hls reap segment, callback.
* @param client_id the id of client on server.
* @param url the api server url, to process the event.
* ignore if empty.
* @param cwd the current work directory, used to resolve the reltive file path.
* @param file the file path, can be relative or absolute path.
* @param file the ts file path, can be relative or absolute path.
* @param sn the seq_no, the sequence number of ts in hls/m3u8.
*/
static int on_dvr_reap_segment(std::string url, int client_id, SrsRequest* req, std::string cwd, std::string file);
static int on_hls(std::string url, int client_id, SrsRequest* req, std::string cwd, std::string file, int sn);
private:
static int do_post(std::string url, std::string req, int& code, std::string& res);
};

View file

@ -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 151
#define VERSION_REVISION 152
// server info.
#define RTMP_SIG_SRS_KEY "SRS"