mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
refine code, notice api when segment close
This commit is contained in:
parent
09901d31da
commit
7ff8df4d97
2 changed files with 48 additions and 57 deletions
|
@ -305,6 +305,9 @@ SrsDvrPlan::SrsDvrPlan()
|
||||||
dvr_enabled = false;
|
dvr_enabled = false;
|
||||||
fs = new SrsFileStream();
|
fs = new SrsFileStream();
|
||||||
enc = new SrsFlvEncoder();
|
enc = new SrsFlvEncoder();
|
||||||
|
segment_has_keyframe = true;
|
||||||
|
starttime = -1;
|
||||||
|
duration = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsDvrPlan::~SrsDvrPlan()
|
SrsDvrPlan::~SrsDvrPlan()
|
||||||
|
@ -407,7 +410,7 @@ int SrsDvrPlan::on_audio(SrsSharedPtrMessage* audio)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((ret = on_audio_msg(audio)) != ERROR_SUCCESS) {
|
if ((ret = update_duration(audio)) != ERROR_SUCCESS) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -435,15 +438,13 @@ int SrsDvrPlan::on_video(SrsSharedPtrMessage* video)
|
||||||
|
|
||||||
#ifdef SRS_AUTO_HTTP_CALLBACK
|
#ifdef SRS_AUTO_HTTP_CALLBACK
|
||||||
bool is_key_frame = SrsCodec::video_is_keyframe((int8_t*)payload, size);
|
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 (is_key_frame) {
|
||||||
if ((ret = on_dvr_keyframe()) != ERROR_SUCCESS) {
|
segment_has_keyframe = true;
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
srs_verbose("dvr video is key: %d", is_key_frame);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if ((ret = on_video_msg(video)) != ERROR_SUCCESS) {
|
if ((ret = update_duration(video)) != ERROR_SUCCESS) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -471,22 +472,12 @@ int SrsDvrPlan::flv_open(string stream, string path)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
segment_has_keyframe = false;
|
||||||
|
|
||||||
srs_trace("dvr stream %s to file %s", stream.c_str(), path.c_str());
|
srs_trace("dvr stream %s to file %s", stream.c_str(), path.c_str());
|
||||||
return ret;
|
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 SrsDvrPlan::flv_close()
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
@ -503,6 +494,28 @@ int SrsDvrPlan::flv_close()
|
||||||
return ret;
|
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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -565,8 +578,6 @@ void SrsDvrSessionPlan::on_unpublish()
|
||||||
|
|
||||||
SrsDvrSegmentPlan::SrsDvrSegmentPlan()
|
SrsDvrSegmentPlan::SrsDvrSegmentPlan()
|
||||||
{
|
{
|
||||||
starttime = -1;
|
|
||||||
duration = 0;
|
|
||||||
segment_duration = -1;
|
segment_duration = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -611,38 +622,13 @@ void SrsDvrSegmentPlan::on_unpublish()
|
||||||
dvr_enabled = false;
|
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 SrsDvrSegmentPlan::update_duration(SrsSharedPtrMessage* msg)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
// foreach msg, collect the duration.
|
if ((ret = SrsDvrPlan::update_duration(msg)) != ERROR_SUCCESS) {
|
||||||
if (starttime < 0 || starttime > msg->header.timestamp) {
|
return ret;
|
||||||
starttime = msg->header.timestamp;
|
|
||||||
}
|
}
|
||||||
duration += msg->header.timestamp - starttime;
|
|
||||||
starttime = msg->header.timestamp;
|
|
||||||
|
|
||||||
// reap if exceed duration.
|
// reap if exceed duration.
|
||||||
if (duration > 0 && segment_duration > 0 && duration > segment_duration) {
|
if (duration > 0 && segment_duration > 0 && duration > segment_duration) {
|
||||||
|
|
|
@ -127,10 +127,20 @@ protected:
|
||||||
SrsSource* _source;
|
SrsSource* _source;
|
||||||
SrsRequest* _req;
|
SrsRequest* _req;
|
||||||
SrsRtmpJitter* jitter;
|
SrsRtmpJitter* jitter;
|
||||||
|
protected:
|
||||||
/**
|
/**
|
||||||
* current flv file path.
|
* current flv file path.
|
||||||
*/
|
*/
|
||||||
std::string current_flv_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:
|
public:
|
||||||
SrsDvrPlan();
|
SrsDvrPlan();
|
||||||
virtual ~SrsDvrPlan();
|
virtual ~SrsDvrPlan();
|
||||||
|
@ -143,14 +153,13 @@ public:
|
||||||
virtual int on_video(SrsSharedPtrMessage* video);
|
virtual int on_video(SrsSharedPtrMessage* video);
|
||||||
protected:
|
protected:
|
||||||
virtual int flv_open(std::string stream, std::string path);
|
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 flv_close();
|
||||||
|
virtual int update_duration(SrsSharedPtrMessage* msg);
|
||||||
private:
|
private:
|
||||||
|
/**
|
||||||
|
* when srs reap the flv(close the segment),
|
||||||
|
* if has keyframe, notice the api.
|
||||||
|
*/
|
||||||
virtual int on_dvr_keyframe();
|
virtual int on_dvr_keyframe();
|
||||||
public:
|
public:
|
||||||
static SrsDvrPlan* create_plan(std::string vhost);
|
static SrsDvrPlan* create_plan(std::string vhost);
|
||||||
|
@ -174,8 +183,6 @@ public:
|
||||||
class SrsDvrSegmentPlan : public SrsDvrPlan
|
class SrsDvrSegmentPlan : public SrsDvrPlan
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int64_t duration;
|
|
||||||
int64_t starttime;
|
|
||||||
// in config, in ms
|
// in config, in ms
|
||||||
int segment_duration;
|
int segment_duration;
|
||||||
public:
|
public:
|
||||||
|
@ -185,8 +192,6 @@ public:
|
||||||
virtual int initialize(SrsSource* source, SrsRequest* req);
|
virtual int initialize(SrsSource* source, SrsRequest* req);
|
||||||
virtual int on_publish();
|
virtual int on_publish();
|
||||||
virtual void on_unpublish();
|
virtual void on_unpublish();
|
||||||
virtual int on_audio_msg(SrsSharedPtrMessage* audio);
|
|
||||||
virtual int on_video_msg(SrsSharedPtrMessage* video);
|
|
||||||
private:
|
private:
|
||||||
virtual int update_duration(SrsSharedPtrMessage* msg);
|
virtual int update_duration(SrsSharedPtrMessage* msg);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue