2021-05-31 05:42:20 +00:00
|
|
|
//
|
2021-07-08 06:30:47 +00:00
|
|
|
// Copyright (c) 2013-2021 The SRS Authors
|
2021-05-31 05:42:20 +00:00
|
|
|
//
|
2022-01-13 10:40:17 +00:00
|
|
|
// SPDX-License-Identifier: MIT or MulanPSL-2.0
|
2021-05-31 05:42:20 +00:00
|
|
|
//
|
2014-04-15 06:01:57 +00:00
|
|
|
|
2014-04-16 01:28:02 +00:00
|
|
|
#ifndef SRS_APP_DVR_HPP
|
|
|
|
#define SRS_APP_DVR_HPP
|
2014-04-15 06:01:57 +00:00
|
|
|
|
|
|
|
#include <srs_core.hpp>
|
|
|
|
|
2014-06-08 05:03:03 +00:00
|
|
|
#include <string>
|
2015-02-21 13:17:59 +00:00
|
|
|
#include <sstream>
|
2014-06-08 05:03:03 +00:00
|
|
|
|
2021-05-16 08:14:00 +00:00
|
|
|
class SrsLiveSource;
|
2017-01-19 04:38:55 +00:00
|
|
|
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-29 06:44:07 +00:00
|
|
|
class SrsSharedPtrMessage;
|
2014-06-29 09:05:26 +00:00
|
|
|
class SrsFileWriter;
|
2017-02-12 13:50:02 +00:00
|
|
|
class SrsFlvTransmuxer;
|
2015-02-21 08:25:04 +00:00
|
|
|
class SrsDvrPlan;
|
2015-02-21 15:09:21 +00:00
|
|
|
class SrsJsonAny;
|
2015-02-24 09:29:30 +00:00
|
|
|
class SrsJsonObject;
|
2015-02-23 11:23:32 +00:00
|
|
|
class SrsThread;
|
2017-02-06 10:33:26 +00:00
|
|
|
class SrsMp4Encoder;
|
2017-03-18 13:41:01 +00:00
|
|
|
class SrsFragment;
|
2017-06-04 07:10:35 +00:00
|
|
|
class SrsFormat;
|
2014-04-17 04:59:35 +00:00
|
|
|
|
2014-06-25 09:14:11 +00:00
|
|
|
#include <srs_app_source.hpp>
|
|
|
|
#include <srs_app_reload.hpp>
|
2015-03-31 09:42:12 +00:00
|
|
|
#include <srs_app_async_call.hpp>
|
2014-06-25 09:14:11 +00:00
|
|
|
|
2019-04-30 00:24:52 +00:00
|
|
|
// The segmenter for DVR, to write a segment file in flv/mp4.
|
2017-02-06 10:33:26 +00:00
|
|
|
class SrsDvrSegmenter : public ISrsReloadHandler
|
2014-04-23 08:33:42 +00:00
|
|
|
{
|
2017-02-06 10:33:26 +00:00
|
|
|
protected:
|
2017-02-06 12:22:07 +00:00
|
|
|
// The underlayer file object.
|
2015-02-21 08:25:04 +00:00
|
|
|
SrsFileWriter* fs;
|
2017-02-06 12:22:07 +00:00
|
|
|
// Whether wait keyframe to reap segment.
|
|
|
|
bool wait_keyframe;
|
2017-03-18 13:41:01 +00:00
|
|
|
// The FLV/MP4 fragment file.
|
|
|
|
SrsFragment* fragment;
|
2017-02-06 12:58:52 +00:00
|
|
|
private:
|
2017-02-06 10:33:26 +00:00
|
|
|
SrsRequest* req;
|
|
|
|
SrsDvrPlan* plan;
|
2017-02-06 12:58:52 +00:00
|
|
|
private:
|
2017-02-06 12:22:07 +00:00
|
|
|
SrsRtmpJitter* jitter;
|
|
|
|
SrsRtmpJitterAlgorithm jitter_algorithm;
|
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:
|
2017-02-06 12:22:07 +00:00
|
|
|
// Initialize the segment.
|
2017-06-11 01:40:07 +00:00
|
|
|
virtual srs_error_t initialize(SrsDvrPlan* p, SrsRequest* r);
|
2017-03-18 13:41:01 +00:00
|
|
|
// Get the current framgnet.
|
|
|
|
virtual SrsFragment* current();
|
2017-02-06 12:22:07 +00:00
|
|
|
// 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.
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t open();
|
2017-02-06 10:33:26 +00:00
|
|
|
// Write the metadata.
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t 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.
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t write_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format);
|
2017-02-06 10:33:26 +00:00
|
|
|
// Write video packet.
|
|
|
|
// @param shared_video, directly ptr, copy it if need to save it.
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t write_video(SrsSharedPtrMessage* shared_video, SrsFormat* format);
|
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.
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t refresh_metadata() = 0;
|
2017-02-06 12:22:07 +00:00
|
|
|
// Close current segment.
|
|
|
|
// @remark ignore when already closed.
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t close();
|
2017-02-06 10:33:26 +00:00
|
|
|
protected:
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t open_encoder() = 0;
|
|
|
|
virtual srs_error_t encode_metadata(SrsSharedPtrMessage* metadata) = 0;
|
|
|
|
virtual srs_error_t encode_audio(SrsSharedPtrMessage* audio, SrsFormat* format) = 0;
|
|
|
|
virtual srs_error_t encode_video(SrsSharedPtrMessage* video, SrsFormat* format) = 0;
|
|
|
|
virtual srs_error_t close_encoder() = 0;
|
2015-02-21 08:25:04 +00:00
|
|
|
private:
|
2017-02-06 12:58:52 +00:00
|
|
|
// Generate the flv segment path.
|
2015-02-21 08:25:04 +00:00
|
|
|
virtual std::string generate_path();
|
2017-02-07 13:56:20 +00:00
|
|
|
// When update the duration of segment by rtmp msg.
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t on_update_duration(SrsSharedPtrMessage* msg);
|
2019-04-30 00:30:13 +00:00
|
|
|
// Interface ISrsReloadHandler
|
2015-02-21 08:25:04 +00:00
|
|
|
public:
|
2017-09-22 08:14:30 +00:00
|
|
|
virtual srs_error_t on_reload_vhost_dvr(std::string vhost);
|
2014-04-23 08:33:42 +00:00
|
|
|
};
|
|
|
|
|
2019-04-30 00:24:52 +00:00
|
|
|
// The FLV segmenter to use FLV encoder to write file.
|
2017-02-06 10:33:26 +00:00
|
|
|
class SrsDvrFlvSegmenter : public SrsDvrSegmenter
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
// The FLV encoder, for FLV target.
|
2017-02-12 13:50:02 +00:00
|
|
|
SrsFlvTransmuxer* enc;
|
2017-02-06 10:33:26 +00:00
|
|
|
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();
|
2017-02-06 12:22:07 +00:00
|
|
|
public:
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t refresh_metadata();
|
2017-02-06 10:33:26 +00:00
|
|
|
protected:
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t open_encoder();
|
|
|
|
virtual srs_error_t encode_metadata(SrsSharedPtrMessage* metadata);
|
|
|
|
virtual srs_error_t encode_audio(SrsSharedPtrMessage* audio, SrsFormat* format);
|
|
|
|
virtual srs_error_t encode_video(SrsSharedPtrMessage* video, SrsFormat* format);
|
|
|
|
virtual srs_error_t close_encoder();
|
2017-02-06 10:33:26 +00:00
|
|
|
};
|
|
|
|
|
2019-04-30 00:24:52 +00:00
|
|
|
// The MP4 segmenter to use MP4 encoder to write file.
|
2017-02-06 10:33:26 +00:00
|
|
|
class SrsDvrMp4Segmenter : public SrsDvrSegmenter
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
// The MP4 encoder, for MP4 target.
|
|
|
|
SrsMp4Encoder* enc;
|
|
|
|
public:
|
|
|
|
SrsDvrMp4Segmenter();
|
|
|
|
virtual ~SrsDvrMp4Segmenter();
|
2017-02-06 12:22:07 +00:00
|
|
|
public:
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t refresh_metadata();
|
2017-02-06 10:33:26 +00:00
|
|
|
protected:
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t open_encoder();
|
|
|
|
virtual srs_error_t encode_metadata(SrsSharedPtrMessage* metadata);
|
|
|
|
virtual srs_error_t encode_audio(SrsSharedPtrMessage* audio, SrsFormat* format);
|
|
|
|
virtual srs_error_t encode_video(SrsSharedPtrMessage* video, SrsFormat* format);
|
|
|
|
virtual srs_error_t close_encoder();
|
2017-02-06 10:33:26 +00:00
|
|
|
};
|
|
|
|
|
2019-04-30 00:24:52 +00:00
|
|
|
// the dvr async call.
|
2015-05-22 23:57:45 +00:00
|
|
|
class SrsDvrAsyncCallOnDvr : public ISrsAsyncCallTask
|
2015-02-23 11:23:32 +00:00
|
|
|
{
|
|
|
|
private:
|
2020-07-05 15:26:55 +00:00
|
|
|
SrsContextId cid;
|
2015-02-23 11:23:32 +00:00
|
|
|
std::string path;
|
|
|
|
SrsRequest* req;
|
|
|
|
public:
|
2020-07-05 15:26:55 +00:00
|
|
|
SrsDvrAsyncCallOnDvr(SrsContextId c, SrsRequest* r, std::string p);
|
2015-02-23 11:23:32 +00:00
|
|
|
virtual ~SrsDvrAsyncCallOnDvr();
|
|
|
|
public:
|
2017-09-22 11:54:50 +00:00
|
|
|
virtual srs_error_t call();
|
2015-02-23 11:23:32 +00:00
|
|
|
virtual std::string to_string();
|
|
|
|
};
|
|
|
|
|
2019-04-30 00:24:52 +00:00
|
|
|
// The DVR plan, when and how to reap segment.
|
2017-02-06 12:58:52 +00:00
|
|
|
class SrsDvrPlan : public ISrsReloadHandler
|
2014-04-17 08:57:04 +00:00
|
|
|
{
|
2015-02-21 15:09:21 +00:00
|
|
|
public:
|
|
|
|
SrsRequest* req;
|
2014-06-25 09:14:11 +00:00
|
|
|
protected:
|
2017-02-06 12:58:52 +00:00
|
|
|
SrsOriginHub* hub;
|
2017-02-06 10:33:26 +00:00
|
|
|
SrsDvrSegmenter* segment;
|
2014-06-25 09:14:11 +00:00
|
|
|
bool dvr_enabled;
|
2014-04-17 08:57:04 +00:00
|
|
|
public:
|
|
|
|
SrsDvrPlan();
|
|
|
|
virtual ~SrsDvrPlan();
|
|
|
|
public:
|
2017-06-11 01:40:07 +00:00
|
|
|
virtual srs_error_t initialize(SrsOriginHub* h, SrsDvrSegmenter* s, SrsRequest* r);
|
2021-11-07 08:59:33 +00:00
|
|
|
virtual srs_error_t on_publish(SrsRequest* r);
|
2020-01-15 11:51:33 +00:00
|
|
|
virtual void on_unpublish();
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t on_meta_data(SrsSharedPtrMessage* shared_metadata);
|
|
|
|
virtual srs_error_t on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format);
|
|
|
|
virtual srs_error_t on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format);
|
|
|
|
// Internal interface for segmenter.
|
2017-02-06 10:33:26 +00:00
|
|
|
public:
|
|
|
|
// When segmenter close a segment.
|
2017-09-22 13:50:54 +00:00
|
|
|
virtual srs_error_t on_reap_segment();
|
2014-04-17 08:57:04 +00:00
|
|
|
public:
|
2017-06-11 01:40:07 +00:00
|
|
|
static srs_error_t create_plan(std::string vhost, SrsDvrPlan** pplan);
|
2014-04-17 08:57:04 +00:00
|
|
|
};
|
|
|
|
|
2019-04-30 00:24:52 +00:00
|
|
|
// The DVR session plan: reap flv when session complete(unpublish)
|
2014-04-17 08:57:04 +00:00
|
|
|
class SrsDvrSessionPlan : public SrsDvrPlan
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SrsDvrSessionPlan();
|
|
|
|
virtual ~SrsDvrSessionPlan();
|
|
|
|
public:
|
2021-11-07 08:59:33 +00:00
|
|
|
virtual srs_error_t on_publish(SrsRequest* r);
|
2014-04-17 08:57:04 +00:00
|
|
|
virtual void on_unpublish();
|
2015-02-21 08:52:37 +00:00
|
|
|
};
|
|
|
|
|
2019-04-30 00:24: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:
|
2019-04-10 00:32:46 +00:00
|
|
|
// in config, in srs_utime_t
|
|
|
|
srs_utime_t cduration;
|
2017-02-06 12:58:52 +00:00
|
|
|
bool wait_keyframe;
|
2022-01-11 00:40:05 +00:00
|
|
|
// Whether reopening the DVR file.
|
|
|
|
bool reopening_segment_;
|
2014-04-17 09:35:21 +00:00
|
|
|
public:
|
|
|
|
SrsDvrSegmentPlan();
|
|
|
|
virtual ~SrsDvrSegmentPlan();
|
|
|
|
public:
|
2017-06-11 01:40:07 +00:00
|
|
|
virtual srs_error_t initialize(SrsOriginHub* h, SrsDvrSegmenter* s, SrsRequest* r);
|
2021-11-07 08:59:33 +00:00
|
|
|
virtual srs_error_t on_publish(SrsRequest* r);
|
2014-04-17 09:35:21 +00:00
|
|
|
virtual void on_unpublish();
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format);
|
|
|
|
virtual srs_error_t on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format);
|
2014-04-17 09:35:21 +00:00
|
|
|
private:
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t update_duration(SrsSharedPtrMessage* msg);
|
2019-04-30 00:30:13 +00:00
|
|
|
// Interface ISrsReloadHandler
|
2017-02-06 12:58:52 +00:00
|
|
|
public:
|
2017-09-22 08:14:30 +00:00
|
|
|
virtual srs_error_t on_reload_vhost_dvr(std::string vhost);
|
2014-04-17 09:35:21 +00:00
|
|
|
};
|
|
|
|
|
2019-04-30 00:24:52 +00:00
|
|
|
// DVR(Digital Video Recorder) to record RTMP stream to flv/mp4 file.
|
2015-09-15 07:58:57 +00:00
|
|
|
class SrsDvr : public ISrsReloadHandler
|
2014-04-15 06:24:03 +00:00
|
|
|
{
|
|
|
|
private:
|
2017-01-19 04:38:55 +00:00
|
|
|
SrsOriginHub* hub;
|
2014-04-17 08:57:04 +00:00
|
|
|
SrsDvrPlan* plan;
|
2015-09-15 07:58:57 +00:00
|
|
|
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:
|
2015-02-27 12:39:36 +00:00
|
|
|
SrsDvr();
|
2014-04-15 06:24:03 +00:00
|
|
|
virtual ~SrsDvr();
|
|
|
|
public:
|
2019-04-30 00:24:52 +00:00
|
|
|
// 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.
|
2017-06-11 01:40:07 +00:00
|
|
|
virtual srs_error_t initialize(SrsOriginHub* h, SrsRequest* r);
|
2019-04-30 00:24:52 +00:00
|
|
|
// publish stream event,
|
|
|
|
// when encoder start to publish RTMP stream.
|
|
|
|
// @param fetch_sequence_header whether fetch sequence from source.
|
2021-11-07 08:59:33 +00:00
|
|
|
virtual srs_error_t on_publish(SrsRequest* r);
|
2019-04-30 00:24:52 +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();
|
2019-04-30 00:24:52 +00:00
|
|
|
// get some information from metadata, it's optinal.
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t on_meta_data(SrsSharedPtrMessage* metadata);
|
2019-04-30 00:24:52 +00:00
|
|
|
// mux the audio packets to dvr.
|
|
|
|
// @param shared_audio, directly ptr, copy it if need to save it.
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* foramt);
|
2019-04-30 00:24:52 +00:00
|
|
|
// mux the video packets to dvr.
|
|
|
|
// @param shared_video, directly ptr, copy it if need to save it.
|
2017-09-23 14:12:33 +00:00
|
|
|
virtual srs_error_t on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format);
|
2014-04-15 06:24:03 +00:00
|
|
|
};
|
|
|
|
|
2014-04-15 06:01:57 +00:00
|
|
|
#endif
|
|
|
|
|