srs_app_dvr.hpp 11.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
/*
The MIT License (MIT)

Copyright (c) 2013-2015 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.
*/

#ifndef SRS_APP_DVR_HPP
#define SRS_APP_DVR_HPP

/*
#include <srs_app_dvr.hpp>
*/
#include <srs_core.hpp>

#include <string>
#include <sstream>

#ifdef SRS_AUTO_DVR

class SrsSource;
class SrsRequest;
class SrsStream;
class SrsRtmpJitter;
class SrsOnMetaDataPacket;
class SrsSharedPtrMessage;
class SrsFileWriter;
class SrsFlvEncoder;
class SrsDvrPlan;
class SrsJsonAny;
class SrsJsonObject;
class SrsThread;

#include <srs_app_source.hpp>
#include <srs_app_reload.hpp>
#include <srs_app_thread.hpp>

/**
* a piece of flv segment.
* when open segment, support start at 0 or not.
*/
class SrsFlvSegment : public ISrsReloadHandler
{
private:
    SrsSource* source;
    SrsRequest* req;
    SrsDvrPlan* plan;
private:
    /**
    * the underlayer dvr stream.
    * if close, the flv is reap and closed.
    * if open, new flv file is crote.
    */
    SrsFlvEncoder* enc;
    SrsRtmpJitter* jitter;
    SrsRtmpJitterAlgorithm jitter_algorithm;
    SrsFileWriter* fs;
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;
private:
    std::string tmp_flv_file;
private:
    /**
    * current segment flv file path.
    */
    std::string path;
    /**
    * whether current segment has keyframe.
    */
    bool has_keyframe;
    /**
    * current segment starttime, RTMP pkt time.
    */
    int64_t starttime;
    /**
    * current segment duration
    */
    int64_t duration;
    /**
    * stream start time, to generate atc pts. abs time.
    */
    int64_t stream_starttime;
    /**
    * 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;
public:
    SrsFlvSegment(SrsDvrPlan* p);
    virtual ~SrsFlvSegment();
public:
    /**
    * initialize the segment.
    */
    virtual int initialize(SrsSource* s, SrsRequest* r);
    /**
    * 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.
    * @param use_tmp_file whether use tmp file if possible.
    */
    virtual int open(bool use_tmp_file = true);
    /**
    * close current segment.
    * @remark ignore when already closed.
    */
    virtual int close();
    /**
    * write the metadata to segment.
    */
    virtual int write_metadata(SrsSharedPtrMessage* metadata);
    /**
    * @param __audio, directly ptr, copy it if need to save it.
    */
    virtual int write_audio(SrsSharedPtrMessage* __audio);
    /**
    * @param __video, directly ptr, copy it if need to save it.
    */
    virtual int write_video(SrsSharedPtrMessage* __video);
    /**
    * update the flv metadata.
    */
    virtual int update_flv_metadata();
    /**
    * get the current dvr path.
    */
    virtual std::string get_path();
private:
    /**
    * generate the flv segment path.
    */
    virtual std::string generate_path();
    /**
    * create flv jitter. load jitter when flv exists.
    * @param loads_from_flv whether loads the jitter from exists flv file.
    */
    virtual int create_jitter(bool loads_from_flv);
    /**
    * 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);
};

/**
* the dvr async call.
*/
class ISrsDvrAsyncCall
{
public:
    ISrsDvrAsyncCall();
    virtual ~ISrsDvrAsyncCall();
public:
    virtual int call() = 0;
    virtual std::string to_string() = 0;
};
class SrsDvrAsyncCallOnDvr : public ISrsDvrAsyncCall
{
private:
    std::string path;
    SrsRequest* req;
public:
    SrsDvrAsyncCallOnDvr(SrsRequest* r, std::string p);
    virtual ~SrsDvrAsyncCallOnDvr();
public:
    virtual int call();
    virtual std::string to_string();
};
class SrsDvrAsyncCallOnSegment : public ISrsDvrAsyncCall
{
private:
    std::string callback;
    std::string path;
    SrsRequest* req;
public:
    SrsDvrAsyncCallOnSegment(SrsRequest* r, std::string c, std::string p);
    virtual ~SrsDvrAsyncCallOnSegment();
public:
    virtual int call();
    virtual std::string to_string();
};

/**
* the async callback for dvr.
*/
class SrsDvrAsyncCallThread : public ISrsThreadHandler
{
private:
    SrsThread* pthread;
    std::vector<ISrsDvrAsyncCall*> callbacks;
public:
    SrsDvrAsyncCallThread();
    virtual ~SrsDvrAsyncCallThread();
public:
    virtual int call(ISrsDvrAsyncCall* c);
public:
    virtual int start();
    virtual void stop();
    virtual int cycle();
};

/**
* 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:
    friend class SrsFlvSegment;
public:
    SrsRequest* req;
protected:
    SrsSource* source;
    SrsFlvSegment* segment;
    SrsDvrAsyncCallThread* async;
    bool dvr_enabled;
public:
    SrsDvrPlan();
    virtual ~SrsDvrPlan();
public:
    virtual int initialize(SrsSource* s, SrsRequest* r);
    virtual int on_publish() = 0;
    virtual void on_unpublish() = 0;
    /**
    * when got metadata.
    */
    virtual int on_meta_data(SrsSharedPtrMessage* __metadata);
    /**
    * @param __audio, directly ptr, copy it if need to save it.
    */
    virtual int on_audio(SrsSharedPtrMessage* __audio);
    /**
    * @param __video, directly ptr, copy it if need to save it.
    */
    virtual int on_video(SrsSharedPtrMessage* __video);
protected:
    virtual int on_reap_segment();
    virtual int on_dvr_request_sh();
    virtual int on_video_keyframe();
    virtual int64_t filter_timestamp(int64_t timestamp);
public:
    static SrsDvrPlan* create_plan(std::string vhost);
};

/**
* 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();
};

/**
* api plan: reap flv by api.
*/
class SrsDvrApiPlan : public SrsDvrPlan
{
private:
    // cache the metadata and sequence header, for new segment maybe opened.
    SrsSharedPtrMessage* sh_audio;
    SrsSharedPtrMessage* sh_video;
    SrsSharedPtrMessage* metadata;
private:
    std::string callback;
    bool autostart;
    bool started;
private:
    // user action, reap_segment.
    std::string action;
    std::string path_template;
public:
    SrsDvrApiPlan();
    virtual ~SrsDvrApiPlan();
public:
    virtual int initialize(SrsSource* s, SrsRequest* r);
    virtual int on_publish();
    virtual void on_unpublish();
    virtual int on_meta_data(SrsSharedPtrMessage* __metadata);
    virtual int on_audio(SrsSharedPtrMessage* __audio);
    virtual int on_video(SrsSharedPtrMessage* __video);
public:
    virtual int set_path_tmpl(std::string path_tmpl);
    virtual int set_callback(std::string value);
    virtual int set_wait_keyframe(bool wait_keyframe);
    virtual int start();
    virtual int dumps(std::stringstream& ss);
    virtual int stop();
    virtual int rpc(SrsJsonObject* obj);
protected:
    virtual int on_reap_segment();
private:
    virtual int check_user_actions(SrsSharedPtrMessage* msg);
};

/**
* always append to flv file, never reap it.
*/
class SrsDvrAppendPlan : public SrsDvrPlan
{
private:
    int64_t last_update_time;
public:
    SrsDvrAppendPlan();
    virtual ~SrsDvrAppendPlan();
public:
    virtual int on_publish();
    virtual void on_unpublish();
    virtual int on_audio(SrsSharedPtrMessage* __audio);
    virtual int on_video(SrsSharedPtrMessage* __video);
private:
    virtual int update_duration(SrsSharedPtrMessage* msg);
};

/**
* 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;
public:
    SrsDvrSegmentPlan();
    virtual ~SrsDvrSegmentPlan();
public:
    virtual int initialize(SrsSource* source, SrsRequest* req);
    virtual int on_publish();
    virtual void on_unpublish();
    virtual int on_meta_data(SrsSharedPtrMessage* __metadata);
    virtual int on_audio(SrsSharedPtrMessage* __audio);
    virtual int on_video(SrsSharedPtrMessage* __video);
private:
    virtual int update_duration(SrsSharedPtrMessage* msg);
};

/**
* the api dvr pool.
*/
class SrsApiDvrPool
{
private:
    std::vector<SrsDvrApiPlan*> dvrs;
    static SrsApiDvrPool* _instance;
private:
    SrsApiDvrPool();
public:
    static SrsApiDvrPool* instance();
    virtual ~SrsApiDvrPool();
public:
    virtual int add_dvr(SrsDvrApiPlan* dvr);
public:
    virtual int dumps(std::string vhost, std::stringstream& ss);
    virtual int create(SrsJsonAny* json);
    virtual int stop(std::string vhost);
    virtual int rpc(SrsJsonAny* json);
};

/**
* dvr(digital video recorder) to record RTMP stream to flv file.
* TODO: FIXME: add utest for it.
*/
class SrsDvr
{
private:
    SrsSource* source;
private:
    SrsDvrPlan* plan;
public:
    SrsDvr(SrsSource* s);
    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(SrsRequest* r);
    /**
    * publish stream event, 
    * when encoder start to publish RTMP stream.
    */
    virtual int on_publish(SrsRequest* r);
    /**
    * the unpublish event.,
    * when encoder stop(unpublish) to publish RTMP stream.
    */
    virtual void on_unpublish();
    /**
    * get some information from metadata, it's optinal.
    */
    virtual int on_meta_data(SrsOnMetaDataPacket* m);
    /**
    * mux the audio packets to dvr.
    * @param __audio, directly ptr, copy it if need to save it.
    */
    virtual int on_audio(SrsSharedPtrMessage* __audio);
    /**
    * mux the video packets to dvr.
    * @param __video, directly ptr, copy it if need to save it.
    */
    virtual int on_video(SrsSharedPtrMessage* __video);
};

#endif

#endif