1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_dvr.hpp

380 lines
11 KiB
C++
Raw Normal View History

/*
The MIT License (MIT)
2016-12-16 03:57:25 +00:00
Copyright (c) 2013-2017 SRS(ossrs)
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.
*/
2014-04-16 01:28:02 +00:00
#ifndef SRS_APP_DVR_HPP
#define SRS_APP_DVR_HPP
/*
2014-04-16 01:28:02 +00:00
#include <srs_app_dvr.hpp>
*/
#include <srs_core.hpp>
#include <string>
#include <sstream>
#ifdef SRS_AUTO_DVR
2014-04-15 06:24:03 +00:00
class SrsSource;
class SrsOriginHub;
2014-04-15 06:24:03 +00:00
class SrsRequest;
2015-09-22 00:57:31 +00:00
class SrsBuffer;
2014-04-17 10:13:59 +00:00
class SrsRtmpJitter;
2014-04-17 08:06:49 +00:00
class SrsOnMetaDataPacket;
class SrsSharedPtrMessage;
class SrsFileWriter;
class SrsFlvEncoder;
class SrsDvrPlan;
class SrsJsonAny;
class SrsJsonObject;
class SrsThread;
2017-02-06 10:33:26 +00:00
class SrsMp4Encoder;
2014-04-17 04:59:35 +00:00
#include <srs_app_source.hpp>
#include <srs_app_reload.hpp>
#include <srs_app_async_call.hpp>
2014-04-23 08:33:42 +00:00
/**
2017-02-06 10:33:26 +00:00
* The segmenter for DVR, to write a segment file in flv/mp4.
*/
class SrsDvrSegmenter : public ISrsReloadHandler
2014-04-23 08:33:42 +00:00
{
2017-02-06 10:33:26 +00:00
protected:
SrsFileWriter* fs;
2017-02-06 10:33:26 +00:00
// The path of current segment flv file path.
2014-04-24 04:22:36 +00:00
std::string path;
2017-02-06 10:33:26 +00:00
protected:
SrsRtmpJitter* jitter;
SrsRtmpJitterAlgorithm jitter_algorithm;
// The duration in ms of current segment.
2014-04-24 04:22:36 +00:00
int64_t duration;
2017-02-06 10:33:26 +00:00
protected:
SrsRequest* req;
SrsDvrPlan* plan;
private:
std::string tmp_dvr_file;
2014-04-23 08:33:42 +00:00
public:
2017-02-06 10:33:26 +00:00
SrsDvrSegmenter();
virtual ~SrsDvrSegmenter();
2014-06-28 16:09:55 +00:00
public:
/**
* initialize the segment.
*/
2017-02-06 10:33:26 +00:00
virtual int initialize(SrsDvrPlan* p, SrsRequest* r);
/**
* get the current dvr path.
*/
virtual std::string get_path();
/**
* whether segment is overflow.
*/
virtual bool is_overflow(int64_t max_duration);
/**
* open new segment file, timestamp start at 0 for fresh flv file.
* @remark ignore when already open.
2015-02-21 08:52:37 +00:00
* @param use_tmp_file whether use tmp file if possible.
*/
2017-02-06 10:33:26 +00:00
virtual int open(bool use_tmp_file);
/**
* close current segment.
* @remark ignore when already closed.
*/
virtual int close();
2017-02-06 10:33:26 +00:00
protected:
virtual int open_encoder() = 0;
public:
// Write the metadata.
virtual int write_metadata(SrsSharedPtrMessage* metadata) = 0;
// Write audio packet.
// @param shared_audio, directly ptr, copy it if need to save it.
virtual int write_audio(SrsSharedPtrMessage* shared_audio) = 0;
// Write video packet.
// @param shared_video, directly ptr, copy it if need to save it.
virtual int write_video(SrsSharedPtrMessage* shared_video) = 0;
// Refresh the metadata. For example, there is duration in flv metadata,
// when DVR in append mode, the duration must be update every some seconds.
// @remark Maybe ignored by concreate segmenter.
virtual int refresh_metadata() = 0;
protected:
virtual int close_encoder() = 0;
private:
/**
* generate the flv segment path.
*/
virtual std::string generate_path();
/**
* create flv jitter. load jitter when flv exists.
2017-02-06 10:33:26 +00:00
* @param target_exists whether loads the jitter from exists flv file.
*/
2017-02-06 10:33:26 +00:00
virtual int create_jitter(bool target_exists);
// interface ISrsReloadHandler
public:
virtual int on_reload_vhost_dvr(std::string vhost);
2014-04-23 08:33:42 +00:00
};
2017-02-06 10:33:26 +00:00
/**
* The FLV segmenter to use FLV encoder to write file.
*/
class SrsDvrFlvSegmenter : public SrsDvrSegmenter
{
private:
// The FLV encoder, for FLV target.
SrsFlvEncoder* enc;
private:
// The offset of file for duration value.
// The next 8 bytes is the double value.
int64_t duration_offset;
// The offset of file for filesize value.
// The next 8 bytes is the double value.
int64_t filesize_offset;
// Whether current segment has keyframe.
bool has_keyframe;
private:
// The current segment starttime in ms, RTMP pkt time.
int64_t starttime;
// The stream start time in ms, to generate atc pts. abs time.
int64_t stream_starttime;
// The stream duration in ms, to generate atc segment.
int64_t stream_duration;
/**
* The previous stream RTMP pkt time in ms, used to calc the duration.
* for the RTMP timestamp will overflow.
*/
// TODO: FIXME: Use utility object to calc it.
int64_t stream_previous_pkt_time;
public:
SrsDvrFlvSegmenter();
virtual ~SrsDvrFlvSegmenter();
protected:
virtual int open_encoder();
virtual int close_encoder();
private:
// When update the duration of segment by rtmp msg.
virtual int on_update_duration(SrsSharedPtrMessage* msg);
public:
virtual int write_metadata(SrsSharedPtrMessage* metadata);
virtual int write_audio(SrsSharedPtrMessage* shared_audio);
virtual int write_video(SrsSharedPtrMessage* shared_video);
virtual int refresh_metadata();
};
/**
* The MP4 segmenter to use MP4 encoder to write file.
*/
class SrsDvrMp4Segmenter : public SrsDvrSegmenter
{
private:
// The MP4 encoder, for MP4 target.
SrsMp4Encoder* enc;
public:
SrsDvrMp4Segmenter();
virtual ~SrsDvrMp4Segmenter();
protected:
virtual int open_encoder();
virtual int close_encoder();
public:
virtual int write_metadata(SrsSharedPtrMessage* metadata);
virtual int write_audio(SrsSharedPtrMessage* shared_audio);
virtual int write_video(SrsSharedPtrMessage* shared_video);
virtual int refresh_metadata();
};
/**
* the dvr async call.
*/
class SrsDvrAsyncCallOnDvr : public ISrsAsyncCallTask
{
private:
int cid;
std::string path;
SrsRequest* req;
public:
SrsDvrAsyncCallOnDvr(int c, SrsRequest* r, std::string p);
virtual ~SrsDvrAsyncCallOnDvr();
public:
virtual int call();
virtual std::string to_string();
};
/**
* the plan for dvr.
* use to control the following dvr params:
* 1. filename: the filename for record file.
* 2. reap flv: when to reap the flv and start new piece.
*/
// TODO: FIXME: the plan is too fat, refine me.
class SrsDvrPlan
{
public:
SrsRequest* req;
protected:
2017-02-06 10:33:26 +00:00
SrsDvrSegmenter* segment;
SrsAsyncCallWorker* async;
bool dvr_enabled;
public:
SrsDvrPlan();
virtual ~SrsDvrPlan();
public:
2017-02-06 10:33:26 +00:00
virtual int initialize(SrsDvrSegmenter* s, SrsRequest* r);
virtual int on_publish() = 0;
virtual void on_unpublish() = 0;
/**
* when got metadata.
*/
virtual int on_meta_data(SrsSharedPtrMessage* shared_metadata);
/**
* @param shared_audio, directly ptr, copy it if need to save it.
*/
virtual int on_audio(SrsSharedPtrMessage* shared_audio);
/**
* @param shared_video, directly ptr, copy it if need to save it.
*/
virtual int on_video(SrsSharedPtrMessage* shared_video);
2017-02-06 10:33:26 +00:00
// Internal interface for segmenter.
public:
// When segmenter close a segment.
virtual int on_reap_segment();
2017-02-06 10:33:26 +00:00
// When segmenter got a keyframe.
virtual int on_video_keyframe();
2017-02-06 10:33:26 +00:00
// The plan may need to process the timestamp.
virtual int64_t filter_timestamp(int64_t timestamp);
public:
2017-02-06 10:33:26 +00:00
static int create_plan(std::string vhost, SrsDvrPlan** pplan);
};
/**
2014-04-17 09:35:21 +00:00
* session plan: reap flv when session complete(unpublish)
*/
class SrsDvrSessionPlan : public SrsDvrPlan
{
public:
SrsDvrSessionPlan();
virtual ~SrsDvrSessionPlan();
public:
virtual int on_publish();
virtual void on_unpublish();
2015-02-21 08:52:37 +00:00
};
/**
* always append to flv file, never reap it.
*/
class SrsDvrAppendPlan : public SrsDvrPlan
{
private:
int64_t last_update_time;
2015-02-21 08:52:37 +00:00
public:
SrsDvrAppendPlan();
virtual ~SrsDvrAppendPlan();
public:
virtual int on_publish();
virtual void on_unpublish();
virtual int on_audio(SrsSharedPtrMessage* shared_audio);
virtual int on_video(SrsSharedPtrMessage* shared_video);
private:
virtual int update_duration(SrsSharedPtrMessage* msg);
};
2014-04-17 09:35:21 +00:00
/**
* segment plan: reap flv when duration exceed.
*/
class SrsDvrSegmentPlan : public SrsDvrPlan
{
private:
// in config, in ms
int segment_duration;
SrsSharedPtrMessage* sh_audio;
SrsSharedPtrMessage* sh_video;
SrsSharedPtrMessage* metadata;
2014-04-17 09:35:21 +00:00
public:
SrsDvrSegmentPlan();
virtual ~SrsDvrSegmentPlan();
public:
2017-02-06 10:33:26 +00:00
virtual int initialize(SrsDvrSegmenter* s, SrsRequest* req);
2014-04-17 10:13:59 +00:00
virtual int on_publish();
2014-04-17 09:35:21 +00:00
virtual void on_unpublish();
virtual int on_meta_data(SrsSharedPtrMessage* shared_metadata);
virtual int on_audio(SrsSharedPtrMessage* shared_audio);
virtual int on_video(SrsSharedPtrMessage* shared_video);
2014-04-17 09:35:21 +00:00
private:
virtual int update_duration(SrsSharedPtrMessage* msg);
2014-04-17 09:35:21 +00:00
};
2014-04-15 06:24:03 +00:00
/**
2017-02-06 10:33:26 +00:00
* DVR(Digital Video Recorder) to record RTMP stream to flv/mp4 file.
* TODO: FIXME: add utest for it.
*/
class SrsDvr : public ISrsReloadHandler
2014-04-15 06:24:03 +00:00
{
private:
SrsOriginHub* hub;
SrsDvrPlan* plan;
SrsRequest* req;
private:
// whether the dvr is actived by filter, which is specified by dvr_apply.
// we always initialize the dvr, which crote plan and segment object,
// but they never create actual piece of file util the apply active it.
bool actived;
2014-04-15 06:24:03 +00:00
public:
SrsDvr();
2014-04-15 06:24:03 +00:00
virtual ~SrsDvr();
public:
/**
* initialize dvr, create dvr plan.
* when system initialize(encoder publish at first time, or reload),
* initialize the dvr will reinitialize the plan, the whole dvr framework.
*/
virtual int initialize(SrsOriginHub* h, SrsRequest* r);
2014-04-15 06:24:03 +00:00
/**
* publish stream event,
* when encoder start to publish RTMP stream.
* @param fetch_sequence_header whether fetch sequence from source.
*/
virtual int on_publish(bool fetch_sequence_header);
2014-04-15 06:24:03 +00:00
/**
2014-04-16 09:54:41 +00:00
* the unpublish event.,
* when encoder stop(unpublish) to publish RTMP stream.
2014-04-15 06:24:03 +00:00
*/
virtual void on_unpublish();
/**
* get some information from metadata, it's optinal.
*/
virtual int on_meta_data(SrsOnMetaDataPacket* m);
2014-04-15 06:24:03 +00:00
/**
2014-04-16 09:54:41 +00:00
* mux the audio packets to dvr.
* @param shared_audio, directly ptr, copy it if need to save it.
2014-04-15 06:24:03 +00:00
*/
virtual int on_audio(SrsSharedPtrMessage* shared_audio);
2014-04-15 06:24:03 +00:00
/**
2014-04-16 09:54:41 +00:00
* mux the video packets to dvr.
* @param shared_video, directly ptr, copy it if need to save it.
2014-04-15 06:24:03 +00:00
*/
virtual int on_video(SrsSharedPtrMessage* shared_video);
// interface ISrsReloadHandler
public:
virtual int on_reload_vhost_dvr_apply(std::string vhost);
2014-04-15 06:24:03 +00:00
};
#endif
#endif
2014-08-02 14:18:39 +00:00