Blame view

trunk/src/app/srs_app_dvr.hpp 5.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
The MIT License (MIT)

Copyright (c) 2013-2014 winlin

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.
*/
winlin authored
24 25
#ifndef SRS_APP_DVR_HPP
#define SRS_APP_DVR_HPP
26 27

/*
winlin authored
28
#include <srs_app_dvr.hpp>
29 30 31
*/
#include <srs_core.hpp>
32 33
#include <string>
34 35
#ifdef SRS_AUTO_DVR
winlin authored
36 37
class SrsSource;
class SrsRequest;
winlin authored
38
class SrsStream;
winlin authored
39
class SrsRtmpJitter;
winlin authored
40
class SrsOnMetaDataPacket;
41
class SrsSharedPtrMessage;
42
class SrsFileWriter;
43
class SrsFlvEncoder;
winlin authored
44
45 46 47
#include <srs_app_source.hpp>
#include <srs_app_reload.hpp>
winlin authored
48
/**
49 50 51 52 53 54
* a piece of flv segment.
*/
class SrsFlvSegment
{
public:
    /**
55
    * current segment flv file path.
56
    */
57
    std::string path;
58 59 60
    /**
    * whether current segment has keyframe.
    */
61
    bool has_keyframe;
62
    /**
63
    * current segment starttime, RTMP pkt time.
64
    */
65
    int64_t starttime;
66
    /**
67 68 69 70 71
    * current segment duration
    */
    int64_t duration;
    /**
    * stream start time, to generate atc pts. abs time.
72 73
    */
    int64_t stream_starttime;
74 75 76 77 78 79 80 81 82
    /**
    * stream duration, to generate atc segment.
    */
    int64_t stream_duration;
    /**
    * previous stream RTMP pkt time, used to calc the duration.
    * for the RTMP timestamp will overflow.
    */
    int64_t stream_previous_pkt_time;
83 84
public:
    SrsFlvSegment();
winlin authored
85 86
    virtual ~SrsFlvSegment();
public:
87 88 89 90
    virtual void reset();
};

/**
91 92 93 94 95
* 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.
*/
96
// TODO: FIXME: the plan is too fat, refine me.
97
class SrsDvrPlan : public ISrsReloadHandler
98
{
99
private:
100 101 102 103 104 105 106
    /**
    * the underlayer dvr stream.
    * if close, the flv is reap and closed.
    * if open, new flv file is crote.
    */
    SrsFlvEncoder* enc;
    SrsSource* _source;
winlin authored
107
    SrsRtmpJitter* jitter;
108 109
    SrsRtmpJitterAlgorithm jitter_algorithm;
protected:
110
    SrsFlvSegment* segment;
111 112
    SrsRequest* _req;
    bool dvr_enabled;
113
    SrsFileWriter* fs;
114 115 116 117
public:
    SrsDvrPlan();
    virtual ~SrsDvrPlan();
public:
118
    virtual int initialize(SrsSource* source, SrsRequest* req);
winlin authored
119
    virtual int on_publish();
120 121
    virtual void on_unpublish() = 0;
    virtual int on_meta_data(SrsOnMetaDataPacket* metadata);
122 123
    virtual int on_audio(SrsSharedPtrMessage* audio);
    virtual int on_video(SrsSharedPtrMessage* video);
124 125 126
// interface ISrsReloadHandler
public:
    virtual int on_reload_vhost_dvr(std::string vhost);
127 128 129
protected:
    virtual int flv_open(std::string stream, std::string path);
    virtual int flv_close();
130
    virtual int open_new_segment();
131
    virtual int update_duration(SrsSharedPtrMessage* msg);
132 133 134 135
    virtual int write_flv_header();
    virtual int on_dvr_request_sh();
    virtual int on_video_keyframe();
    virtual int64_t filter_timestamp(int64_t timestamp);
136
public:
137
    static SrsDvrPlan* create_plan(std::string vhost);
138 139 140
};

/**
141
* session plan: reap flv when session complete(unpublish)
142 143 144 145 146 147 148 149 150 151 152
*/
class SrsDvrSessionPlan : public SrsDvrPlan
{
public:
    SrsDvrSessionPlan();
    virtual ~SrsDvrSessionPlan();
public:
    virtual void on_unpublish();
};

/**
153 154 155 156 157 158 159
* segment plan: reap flv when duration exceed.
*/
class SrsDvrSegmentPlan : public SrsDvrPlan
{
private:
    // in config, in ms
    int segment_duration;
160 161
    SrsSharedPtrMessage* sh_audio;
    SrsSharedPtrMessage* sh_video;
162 163 164 165 166
public:
    SrsDvrSegmentPlan();
    virtual ~SrsDvrSegmentPlan();
public:
    virtual int initialize(SrsSource* source, SrsRequest* req);
winlin authored
167
    virtual int on_publish();
168
    virtual void on_unpublish();
169 170
    virtual int on_audio(SrsSharedPtrMessage* audio);
    virtual int on_video(SrsSharedPtrMessage* video);
171
private:
172
    virtual int update_duration(SrsSharedPtrMessage* msg);
173 174 175
};

/**
winlin authored
176 177 178 179 180 181 182
* dvr(digital video recorder) to record RTMP stream to flv file.
* TODO: FIXME: add utest for it.
*/
class SrsDvr
{
private:
    SrsSource* _source;
winlin authored
183
private:
184
    SrsDvrPlan* plan;
winlin authored
185 186 187 188 189
public:
    SrsDvr(SrsSource* source);
    virtual ~SrsDvr();
public:
    /**
190 191 192 193
    * 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.
    */
194
    virtual int initialize(SrsRequest* req);
195
    /**
winlin authored
196 197
    * publish stream event, 
    * when encoder start to publish RTMP stream.
winlin authored
198 199 200
    */
    virtual int on_publish(SrsRequest* req);
    /**
winlin authored
201 202
    * the unpublish event.,
    * when encoder stop(unpublish) to publish RTMP stream.
winlin authored
203 204 205 206 207
    */
    virtual void on_unpublish();
    /**
    * get some information from metadata, it's optinal.
    */
winlin authored
208
    virtual int on_meta_data(SrsOnMetaDataPacket* metadata);
winlin authored
209
    /**
winlin authored
210
    * mux the audio packets to dvr.
winlin authored
211
    */
212
    virtual int on_audio(SrsSharedPtrMessage* audio);
winlin authored
213
    /**
winlin authored
214
    * mux the video packets to dvr.
winlin authored
215
    */
216
    virtual int on_video(SrsSharedPtrMessage* video);
winlin authored
217 218
};
219 220
#endif
221
#endif
222