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

256 lines
8.3 KiB
C++
Raw Normal View History

//
// Copyright (c) 2013-2021 The SRS Authors
//
// SPDX-License-Identifier: MIT
//
2014-04-16 01:28:02 +00:00
#ifndef SRS_APP_DVR_HPP
#define SRS_APP_DVR_HPP
#include <srs_core.hpp>
#include <string>
#include <sstream>
2021-05-16 08:14:00 +00:00
class SrsLiveSource;
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 SrsFlvTransmuxer;
class SrsDvrPlan;
class SrsJsonAny;
class SrsJsonObject;
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
#include <srs_app_source.hpp>
#include <srs_app_reload.hpp>
#include <srs_app_async_call.hpp>
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:
// The underlayer file object.
SrsFileWriter* fs;
// 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:
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:
// 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();
// 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;
// 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;
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.
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
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.
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();
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();
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.
class SrsDvrAsyncCallOnDvr : public ISrsAsyncCallTask
{
private:
SrsContextId cid;
std::string path;
SrsRequest* req;
public:
SrsDvrAsyncCallOnDvr(SrsContextId c, SrsRequest* r, std::string p);
virtual ~SrsDvrAsyncCallOnDvr();
public:
virtual srs_error_t call();
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
{
public:
SrsRequest* req;
protected:
2017-02-06 12:58:52 +00:00
SrsOriginHub* hub;
2017-02-06 10:33:26 +00:00
SrsDvrSegmenter* segment;
bool dvr_enabled;
public:
SrsDvrPlan();
virtual ~SrsDvrPlan();
public:
2017-06-11 01:40:07 +00:00
virtual srs_error_t initialize(SrsOriginHub* h, SrsDvrSegmenter* s, SrsRequest* r);
virtual srs_error_t on_publish();
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.
virtual srs_error_t on_reap_segment();
public:
2017-06-11 01:40:07 +00:00
static srs_error_t create_plan(std::string vhost, SrsDvrPlan** pplan);
};
2019-04-30 00:24:52 +00:00
// The DVR session plan: reap flv when session complete(unpublish)
class SrsDvrSessionPlan : public SrsDvrPlan
{
public:
SrsDvrSessionPlan();
virtual ~SrsDvrSessionPlan();
public:
2017-09-23 14:12:33 +00:00
virtual srs_error_t on_publish();
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:
// in config, in srs_utime_t
srs_utime_t cduration;
2017-02-06 12:58:52 +00:00
bool wait_keyframe;
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);
2017-09-23 14:12:33 +00:00
virtual srs_error_t on_publish();
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.
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:
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.
2017-09-23 14:12:33 +00:00
virtual srs_error_t on_publish();
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
};
#endif