From 7ff8df4d973e6dbc0496d7c2c3e0740429128fea Mon Sep 17 00:00:00 2001 From: winlin Date: Wed, 23 Apr 2014 16:25:18 +0800 Subject: [PATCH] refine code, notice api when segment close --- trunk/src/app/srs_app_dvr.cpp | 80 +++++++++++++++-------------------- trunk/src/app/srs_app_dvr.hpp | 25 ++++++----- 2 files changed, 48 insertions(+), 57 deletions(-) diff --git a/trunk/src/app/srs_app_dvr.cpp b/trunk/src/app/srs_app_dvr.cpp index 10773935d..f100183fd 100644 --- a/trunk/src/app/srs_app_dvr.cpp +++ b/trunk/src/app/srs_app_dvr.cpp @@ -305,6 +305,9 @@ SrsDvrPlan::SrsDvrPlan() dvr_enabled = false; fs = new SrsFileStream(); enc = new SrsFlvEncoder(); + segment_has_keyframe = true; + starttime = -1; + duration = 0; } SrsDvrPlan::~SrsDvrPlan() @@ -407,7 +410,7 @@ int SrsDvrPlan::on_audio(SrsSharedPtrMessage* audio) return ret; } - if ((ret = on_audio_msg(audio)) != ERROR_SUCCESS) { + if ((ret = update_duration(audio)) != ERROR_SUCCESS) { return ret; } @@ -435,15 +438,13 @@ int SrsDvrPlan::on_video(SrsSharedPtrMessage* video) #ifdef SRS_AUTO_HTTP_CALLBACK bool is_key_frame = SrsCodec::video_is_keyframe((int8_t*)payload, size); - srs_verbose("dvr video is key: %d", is_key_frame); if (is_key_frame) { - if ((ret = on_dvr_keyframe()) != ERROR_SUCCESS) { - return ret; - } + segment_has_keyframe = true; } + srs_verbose("dvr video is key: %d", is_key_frame); #endif - if ((ret = on_video_msg(video)) != ERROR_SUCCESS) { + if ((ret = update_duration(video)) != ERROR_SUCCESS) { return ret; } @@ -471,22 +472,12 @@ int SrsDvrPlan::flv_open(string stream, string path) return ret; } + segment_has_keyframe = false; + srs_trace("dvr stream %s to file %s", stream.c_str(), path.c_str()); return ret; } -int SrsDvrPlan::on_audio_msg(SrsSharedPtrMessage* /*audio*/) -{ - int ret = ERROR_SUCCESS; - return ret; -} - -int SrsDvrPlan::on_video_msg(SrsSharedPtrMessage* /*video*/) -{ - int ret = ERROR_SUCCESS; - return ret; -} - int SrsDvrPlan::flv_close() { int ret = ERROR_SUCCESS; @@ -503,6 +494,28 @@ int SrsDvrPlan::flv_close() return ret; } +#ifdef SRS_AUTO_HTTP_CALLBACK + if (segment_has_keyframe) { + if ((ret = on_dvr_keyframe()) != ERROR_SUCCESS) { + return ret; + } + } +#endif + + return ret; +} + +int SrsDvrPlan::update_duration(SrsSharedPtrMessage* msg) +{ + int ret = ERROR_SUCCESS; + + // foreach msg, collect the duration. + if (starttime < 0 || starttime > msg->header.timestamp) { + starttime = msg->header.timestamp; + } + duration += msg->header.timestamp - starttime; + starttime = msg->header.timestamp; + return ret; } @@ -565,8 +578,6 @@ void SrsDvrSessionPlan::on_unpublish() SrsDvrSegmentPlan::SrsDvrSegmentPlan() { - starttime = -1; - duration = 0; segment_duration = -1; } @@ -611,38 +622,13 @@ void SrsDvrSegmentPlan::on_unpublish() dvr_enabled = false; } -int SrsDvrSegmentPlan::on_audio_msg(SrsSharedPtrMessage* audio) -{ - int ret = ERROR_SUCCESS; - - if ((ret = update_duration(audio)) != ERROR_SUCCESS) { - return ret; - } - - return ret; -} - -int SrsDvrSegmentPlan::on_video_msg(SrsSharedPtrMessage* video) -{ - int ret = ERROR_SUCCESS; - - if ((ret = update_duration(video)) != ERROR_SUCCESS) { - return ret; - } - - return ret; -} - int SrsDvrSegmentPlan::update_duration(SrsSharedPtrMessage* msg) { int ret = ERROR_SUCCESS; - // foreach msg, collect the duration. - if (starttime < 0 || starttime > msg->header.timestamp) { - starttime = msg->header.timestamp; + if ((ret = SrsDvrPlan::update_duration(msg)) != ERROR_SUCCESS) { + return ret; } - duration += msg->header.timestamp - starttime; - starttime = msg->header.timestamp; // reap if exceed duration. if (duration > 0 && segment_duration > 0 && duration > segment_duration) { diff --git a/trunk/src/app/srs_app_dvr.hpp b/trunk/src/app/srs_app_dvr.hpp index be665fa8f..2fd21966a 100644 --- a/trunk/src/app/srs_app_dvr.hpp +++ b/trunk/src/app/srs_app_dvr.hpp @@ -127,10 +127,20 @@ protected: SrsSource* _source; SrsRequest* _req; SrsRtmpJitter* jitter; +protected: /** * current flv file path. */ std::string current_flv_path; + /** + * whether current segment has keyframe. + */ + bool segment_has_keyframe; + /** + * current segment duration and starttime. + */ + int64_t duration; + int64_t starttime; public: SrsDvrPlan(); virtual ~SrsDvrPlan(); @@ -143,14 +153,13 @@ public: virtual int on_video(SrsSharedPtrMessage* video); protected: virtual int flv_open(std::string stream, std::string path); - /** - * user should override this method. - * for the audio/video is corrected by jitter. - */ - virtual int on_audio_msg(SrsSharedPtrMessage* audio); - virtual int on_video_msg(SrsSharedPtrMessage* video); virtual int flv_close(); + virtual int update_duration(SrsSharedPtrMessage* msg); private: + /** + * when srs reap the flv(close the segment), + * if has keyframe, notice the api. + */ virtual int on_dvr_keyframe(); public: static SrsDvrPlan* create_plan(std::string vhost); @@ -174,8 +183,6 @@ public: class SrsDvrSegmentPlan : public SrsDvrPlan { private: - int64_t duration; - int64_t starttime; // in config, in ms int segment_duration; public: @@ -185,8 +192,6 @@ public: virtual int initialize(SrsSource* source, SrsRequest* req); virtual int on_publish(); virtual void on_unpublish(); - virtual int on_audio_msg(SrsSharedPtrMessage* audio); - virtual int on_video_msg(SrsSharedPtrMessage* video); private: virtual int update_duration(SrsSharedPtrMessage* msg); };