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

319 lines
9.3 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>
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;
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:
// The underlayer file object.
SrsFileWriter* fs;
// Whether wait keyframe to reap segment.
bool wait_keyframe;
// The duration in ms of current segment.
int64_t duration;
private:
// The path of current segment flv file path.
std::string path;
2017-02-06 12:58:52 +00:00
std::string tmp_dvr_file;
private:
2017-02-06 10:33:26 +00:00
SrsRequest* req;
SrsDvrPlan* plan;
2017-02-06 12:58:52 +00:00
private:
SrsRtmpJitter* jitter;
SrsRtmpJitterAlgorithm jitter_algorithm;
private:
// 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;
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.
2017-02-06 10:33:26 +00:00
virtual std::string get_path();
2017-02-06 12:58:52 +00:00
// Get the duration in ms of segment.
virtual int64_t get_duration();
// Open new segment file.
// @param use_tmp_file Whether use tmp file for DVR, and rename when close.
// @remark Ignore when file is already open.
virtual int open();
2017-02-06 10:33:26 +00:00
// Write the metadata.
virtual int write_metadata(SrsSharedPtrMessage* metadata);
2017-02-06 10:33:26 +00:00
// Write audio packet.
// @param shared_audio, directly ptr, copy it if need to save it.
virtual int write_audio(SrsSharedPtrMessage* shared_audio);
2017-02-06 10:33:26 +00:00
// Write video packet.
// @param shared_video, directly ptr, copy it if need to save it.
virtual int write_video(SrsSharedPtrMessage* shared_video);
2017-02-06 10:33:26 +00:00
// 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;
// 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;
virtual int encode_metadata(SrsSharedPtrMessage* metadata) = 0;
2017-02-06 12:58:52 +00:00
virtual int encode_audio(SrsSharedPtrMessage* audio) = 0;
virtual int encode_video(SrsSharedPtrMessage* video) = 0;
2017-02-06 10:33:26 +00:00
virtual int close_encoder() = 0;
private:
2017-02-06 12:58:52 +00:00
// Generate the flv segment path.
virtual std::string generate_path();
// When update the duration of segment by rtmp msg.
virtual int on_update_duration(SrsSharedPtrMessage* msg);
// 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;
public:
SrsDvrFlvSegmenter();
virtual ~SrsDvrFlvSegmenter();
public:
virtual int refresh_metadata();
2017-02-06 10:33:26 +00:00
protected:
virtual int open_encoder();
virtual int encode_metadata(SrsSharedPtrMessage* metadata);
2017-02-06 12:58:52 +00:00
virtual int encode_audio(SrsSharedPtrMessage* audio);
virtual int encode_video(SrsSharedPtrMessage* video);
2017-02-06 10:33:26 +00:00
virtual int close_encoder();
};
/**
* The MP4 segmenter to use MP4 encoder to write file.
*/
class SrsDvrMp4Segmenter : public SrsDvrSegmenter
{
private:
// The MP4 encoder, for MP4 target.
SrsMp4Encoder* enc;
// The buffer to demux the packet to mp4 sample.
SrsBuffer* buffer;
2017-02-06 10:33:26 +00:00
public:
SrsDvrMp4Segmenter();
virtual ~SrsDvrMp4Segmenter();
public:
virtual int refresh_metadata();
2017-02-06 10:33:26 +00:00
protected:
virtual int open_encoder();
virtual int encode_metadata(SrsSharedPtrMessage* metadata);
2017-02-06 12:58:52 +00:00
virtual int encode_audio(SrsSharedPtrMessage* audio);
virtual int encode_video(SrsSharedPtrMessage* video);
2017-02-06 10:33:26 +00:00
virtual int close_encoder();
};
/**
* 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();
};
/**
2017-02-06 12:58:52 +00:00
* The DVR plan, when and how to reap segment.
*/
2017-02-06 12:58:52 +00:00
class SrsDvrPlan : public ISrsReloadHandler
{
public:
SrsRequest* req;
protected:
2017-02-06 12:58:52 +00:00
SrsOriginHub* hub;
2017-02-06 10:33:26 +00:00
SrsDvrSegmenter* segment;
SrsAsyncCallWorker* async;
bool dvr_enabled;
public:
SrsDvrPlan();
virtual ~SrsDvrPlan();
public:
2017-02-06 12:58:52 +00:00
virtual int initialize(SrsOriginHub* h, SrsDvrSegmenter* s, SrsRequest* r);
virtual int on_publish() = 0;
virtual void on_unpublish() = 0;
virtual int on_meta_data(SrsSharedPtrMessage* shared_metadata);
virtual int on_audio(SrsSharedPtrMessage* shared_audio);
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();
public:
2017-02-06 10:33:26 +00:00
static int create_plan(std::string vhost, SrsDvrPlan** pplan);
};
/**
2017-02-06 12:58:52 +00:00
* The DVR 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
};
2014-04-17 09:35:21 +00:00
/**
2017-02-06 12:58:52 +00:00
* The DVR segment plan: reap flv when duration exceed.
*/
2014-04-17 09:35:21 +00:00
class SrsDvrSegmentPlan : public SrsDvrPlan
{
private:
// in config, in ms
2017-02-06 12:58:52 +00:00
int cduration;
bool wait_keyframe;
2014-04-17 09:35:21 +00:00
public:
SrsDvrSegmentPlan();
virtual ~SrsDvrSegmentPlan();
public:
2017-02-06 12:58:52 +00:00
virtual int initialize(SrsOriginHub* h, SrsDvrSegmenter* s, SrsRequest* r);
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_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);
2017-02-06 12:58:52 +00:00
// interface ISrsReloadHandler
public:
virtual int on_reload_vhost_dvr(std::string vhost);
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.
*/
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.
*/
2017-02-11 13:14:28 +00:00
virtual int on_publish();
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.
*/
2017-02-06 12:58:52 +00:00
virtual int on_meta_data(SrsSharedPtrMessage* metadata);
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