From 7077b74d03b36de3e995a438d8c686d7ef2bf106 Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 21 Feb 2015 16:52:37 +0800 Subject: [PATCH] for #179, add dvr plan append. --- trunk/conf/full.conf | 1 + trunk/src/app/srs_app_config.hpp | 1 + trunk/src/app/srs_app_dvr.cpp | 43 +++++++++++++++++++++++++++++--- trunk/src/app/srs_app_dvr.hpp | 16 +++++++++++- 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index c5d6181d1..e67931d4f 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -284,6 +284,7 @@ vhost dvr.srs.com { # the dvr plan. canbe: # session reap flv when session end(unpublish). # segment reap flv when flv duration exceed the specified dvr_duration. + # append always append to flv file, never reap it. # api reap flv when api required. # default: session dvr_plan session; diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index afa0b0aa5..8e149b954 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -60,6 +60,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define SRS_CONF_DEFAULT_DVR_PATH "./objs/nginx/html" #define SRS_CONF_DEFAULT_DVR_PLAN_SESSION "session" #define SRS_CONF_DEFAULT_DVR_PLAN_SEGMENT "segment" +#define SRS_CONF_DEFAULT_DVR_PLAN_APPEND "append" #define SRS_CONF_DEFAULT_DVR_PLAN_API "api" #define SRS_CONF_DEFAULT_DVR_PLAN SRS_CONF_DEFAULT_DVR_PLAN_SESSION #define SRS_CONF_DEFAULT_DVR_DURATION 30 diff --git a/trunk/src/app/srs_app_dvr.cpp b/trunk/src/app/srs_app_dvr.cpp index d83d1c69e..bdacd0c14 100644 --- a/trunk/src/app/srs_app_dvr.cpp +++ b/trunk/src/app/srs_app_dvr.cpp @@ -86,7 +86,7 @@ bool SrsFlvSegment::is_overflow(int64_t max_duration) return duration >= max_duration; } -int SrsFlvSegment::open() +int SrsFlvSegment::open(bool use_tmp_file) { int ret = ERROR_SUCCESS; @@ -113,7 +113,7 @@ int SrsFlvSegment::open() } // generate the tmp flv path. - if (!fresh_flv_file) { + if (!fresh_flv_file || !use_tmp_file) { // when path exists, always append to it. // so we must use the target flv path as output flv. tmp_flv_file = path; @@ -546,8 +546,11 @@ SrsDvrPlan* SrsDvrPlan::create_plan(string vhost) return new SrsDvrSegmentPlan(); } else if (plan == SRS_CONF_DEFAULT_DVR_PLAN_SESSION) { return new SrsDvrSessionPlan(); + } else if (plan == SRS_CONF_DEFAULT_DVR_PLAN_APPEND) { + return new SrsDvrAppendPlan(); } else { - return new SrsDvrSessionPlan(); + srs_error("invalid dvr plan=%s, vhost=%s", plan.c_str(), vhost.c_str()); + srs_assert(false); } } @@ -601,6 +604,40 @@ void SrsDvrSessionPlan::on_unpublish() dvr_enabled = false; } +SrsDvrAppendPlan::SrsDvrAppendPlan() +{ +} + +SrsDvrAppendPlan::~SrsDvrAppendPlan() +{ +} + +int SrsDvrAppendPlan::on_publish() +{ + int ret = ERROR_SUCCESS; + + // support multiple publish. + if (dvr_enabled) { + return ret; + } + + if (!_srs_config->get_dvr_enabled(req->vhost)) { + return ret; + } + + if ((ret = segment->open(false)) != ERROR_SUCCESS) { + return ret; + } + + dvr_enabled = true; + + return ret; +} + +void SrsDvrAppendPlan::on_unpublish() +{ +} + SrsDvrSegmentPlan::SrsDvrSegmentPlan() { segment_duration = -1; diff --git a/trunk/src/app/srs_app_dvr.hpp b/trunk/src/app/srs_app_dvr.hpp index 93a35b95f..a2eb8ec74 100644 --- a/trunk/src/app/srs_app_dvr.hpp +++ b/trunk/src/app/srs_app_dvr.hpp @@ -113,8 +113,9 @@ public: /** * open new segment file, timestamp start at 0 for fresh flv file. * @remark ignore when already open. + * @param use_tmp_file whether use tmp file if possible. */ - virtual int open(); + virtual int open(bool use_tmp_file = true); /** * close current segment. * @remark ignore when already closed. @@ -207,6 +208,19 @@ public: virtual void on_unpublish(); }; +/** +* always append to flv file, never reap it. +*/ +class SrsDvrAppendPlan : public SrsDvrPlan +{ +public: + SrsDvrAppendPlan(); + virtual ~SrsDvrAppendPlan(); +public: + virtual int on_publish(); + virtual void on_unpublish(); +}; + /** * segment plan: reap flv when duration exceed. */