winlin

refine srs live flashP2P(integrated by chnvideo)

... ... @@ -322,7 +322,20 @@ vhost hooks.callback.srs.com {
# support multiple api hooks, format:
# on_stop http://xxx/api0 http://xxx/api1 http://xxx/apiN
on_stop http://127.0.0.1:8085/api/v1/sessions http://localhost:8085/api/v1/sessions;
# when dvr got an keyframe, call the hook,
# when dvr got flv header, call the hook,
# the request in the POST data string is a object encode by json:
# {
# "action": "on_dvr_reap_flv",
# "vhost": "video.test.com", "app": "live",
# "stream": "livestream",
# "segment": {
# "cwd": "/usr/local/srs",
# "path": "./objs/nginx/html/live/livestream.1398315892865.flv",
# "duration": 1001, "offset":0,
# "has_keyframe": true, "pts":1398315895958
# }
# }
# when dvr reap flv file, call the hook,
# the request in the POST data string is a object encode by json:
# {
# "action": "on_dvr_reap_flv",
... ...
... ... @@ -804,9 +804,49 @@ void SrsDvrHssPlan::on_unpublish()
dvr_enabled = false;
}
int SrsDvrHssPlan::on_meta_data(SrsOnMetaDataPacket* /*metadata*/)
int SrsDvrHssPlan::on_meta_data(SrsOnMetaDataPacket* metadata)
{
int ret = ERROR_SUCCESS;
SrsRequest* req = _req;
// new flv file
std::stringstream path;
path << _srs_config->get_dvr_path(req->vhost)
<< "/" << req->app << "/"
<< req->stream << ".header.flv";
SrsFileStream fs;
if ((ret = fs.open(path.str().c_str())) != ERROR_SUCCESS) {
return ret;
}
SrsFlvEncoder enc;
if ((ret = enc.initialize(&fs)) != ERROR_SUCCESS) {
return ret;
}
if ((ret = enc.write_header()) != ERROR_SUCCESS) {
return ret;
}
int size = 0;
char* payload = NULL;
if ((ret = metadata->encode(size, payload)) != ERROR_SUCCESS) {
return ret;
}
SrsAutoFree(char, payload, true);
if ((ret = enc.write_metadata(payload, size)) != ERROR_SUCCESS) {
return ret;
}
#ifdef SRS_AUTO_HTTP_CALLBACK
if ((ret = on_dvr_reap_flv_header(path.str())) != ERROR_SUCCESS) {
return ret;
}
#endif
return ret;
}
... ... @@ -842,6 +882,27 @@ int64_t SrsDvrHssPlan::filter_timestamp(int64_t timestamp)
return segment->stream_starttime + timestamp;
}
int SrsDvrHssPlan::on_dvr_reap_flv_header(string path)
{
int ret = ERROR_SUCCESS;
#ifdef SRS_AUTO_HTTP_CALLBACK
// HTTP: on_dvr_reap_flv_header
SrsConfDirective* on_dvr_reap_flv = _srs_config->get_vhost_on_dvr_reap_flv(_req->vhost);
if (!on_dvr_reap_flv) {
srs_info("ignore the empty http callback: on_dvr_reap_flv");
return ret;
}
for (int i = 0; i < (int)on_dvr_reap_flv->args.size(); i++) {
std::string url = on_dvr_reap_flv->args.at(i);
SrsHttpHooks::on_dvr_reap_flv_header(url, _req, path);
}
#endif
return ret;
}
int SrsDvrHssPlan::update_duration(SrsSharedPtrMessage* msg)
{
int ret = ERROR_SUCCESS;
... ...
... ... @@ -259,6 +259,7 @@ protected:
virtual int on_video_keyframe();
virtual int64_t filter_timestamp(int64_t timestamp);
private:
virtual int on_dvr_reap_flv_header(std::string path);
virtual int update_duration(SrsSharedPtrMessage* msg);
};
... ...
... ... @@ -459,6 +459,55 @@ void SrsHttpHooks::on_stop(string url, int client_id, string ip, SrsRequest* req
return;
}
void SrsHttpHooks::on_dvr_reap_flv_header(std::string url, SrsRequest* req, std::string header_file)
{
int ret = ERROR_SUCCESS;
srs_verbose("flv header reap, file=%s", header_file.c_str());
SrsHttpUri uri;
if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {
srs_warn("http uri parse on_dvr_reap_flv_header url failed, ignored. "
"url=%s, ret=%d", url.c_str(), ret);
return;
}
std::stringstream ss;
ss << JOBJECT_START
<< JFIELD_STR("action", "on_dvr_reap_flv_header") << JFIELD_CONT
<< JFIELD_STR("vhost", req->vhost) << JFIELD_CONT
<< JFIELD_STR("app", req->app) << JFIELD_CONT
<< JFIELD_STR("stream", req->stream) << JFIELD_CONT
<< JFIELD_NAME("segment") << JOBJECT_START
<< JFIELD_STR("cwd", _srs_config->get_cwd()) << JFIELD_CONT
<< JFIELD_STR("path", header_file)
<< JOBJECT_END
<< JOBJECT_END;
std::string data = ss.str();
std::string res;
SrsHttpClient http;
if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) {
srs_warn("http post on_dvr_reap_flv_header uri failed, ignored. "
"url=%s, request=%s, response=%s, ret=%d",
url.c_str(), data.c_str(), res.c_str(), ret);
return;
}
if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
ret = ERROR_HTTP_DATA_INVLIAD;
srs_warn("http hook on_dvr_reap_flv_header validate failed, ignored. "
"res=%s, ret=%d", res.c_str(), ret);
return;
}
srs_info("http hook on_dvr_reap_flv_header success. "
"url=%s, request=%s, response=%s, ret=%d",
url.c_str(), data.c_str(), res.c_str(), ret);
return;
}
void SrsHttpHooks::on_dvr_reap_flv(string url, SrsRequest* req, SrsFlvSegment* segment)
{
int ret = ERROR_SUCCESS;
... ...
... ... @@ -124,6 +124,13 @@ public:
static void on_stop(std::string url, int client_id, std::string ip, SrsRequest* req);
public:
/**
* on_dvr_reap_flv_header hook, when dvr write flv file header.
* @param url the api server url, to process the event.
* ignore if empty.
* @param header_file the flv header file.
*/
static void on_dvr_reap_flv_header(std::string url, SrsRequest* req, std::string header_file);
/**
* on_dvr_reap_flv hook, when dvr close flv file.
* @param url the api server url, to process the event.
* ignore if empty.
... ...