Blame view

trunk/src/app/srs_app_dvr.cpp 24.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
#include <srs_app_dvr.hpp>
25 26 27

#ifdef SRS_AUTO_DVR
winlin authored
28
#include <fcntl.h>
29
#include <sstream>
winlin authored
30 31 32
using namespace std;

#include <srs_app_config.hpp>
winlin authored
33
#include <srs_kernel_error.hpp>
winlin authored
34
#include <srs_protocol_rtmp.hpp>
35
#include <srs_protocol_rtmp_stack.hpp>
winlin authored
36 37 38
#include <srs_app_source.hpp>
#include <srs_core_autofree.hpp>
#include <srs_kernel_stream.hpp>
39
#include <srs_kernel_utility.hpp>
40 41
#include <srs_app_http_hooks.hpp>
#include <srs_app_codec.hpp>
winlin authored
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

SrsFileStream::SrsFileStream()
{
    fd = -1;
}

SrsFileStream::~SrsFileStream()
{
    close();
}

int SrsFileStream::open(string file)
{
    int ret = ERROR_SUCCESS;
    
    if (fd > 0) {
        ret = ERROR_SYSTEM_FILE_ALREADY_OPENED;
        srs_error("file %s already opened. ret=%d", _file.c_str(), ret);
        return ret;
    }
    
    int flags = O_CREAT|O_WRONLY|O_TRUNC;
    mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH;

    if ((fd = ::open(file.c_str(), flags, mode)) < 0) {
        ret = ERROR_SYSTEM_FILE_OPENE;
        srs_error("open file %s failed. ret=%d", file.c_str(), ret);
        return ret;
    }
    
    _file = file;
    
    return ret;
}

int SrsFileStream::close()
{
    int ret = ERROR_SUCCESS;
    
    if (fd < 0) {
        return ret;
    }
    
    if (::close(fd) < 0) {
        ret = ERROR_SYSTEM_FILE_CLOSE;
        srs_error("close file %s failed. ret=%d", _file.c_str(), ret);
        return ret;
    }
    fd = -1;
    
    return ret;
}
winlin authored
95 96 97 98 99
bool SrsFileStream::is_open()
{
    return fd > 0;
}
winlin authored
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
int SrsFileStream::read(void* buf, size_t count, ssize_t* pnread)
{
    int ret = ERROR_SUCCESS;
    
    ssize_t nread;
    if ((nread = ::read(fd, buf, count)) < 0) {
        ret = ERROR_SYSTEM_FILE_READ;
        srs_error("read from file %s failed. ret=%d", _file.c_str(), ret);
        return ret;
    }
    
    if (nread == 0) {
        ret = ERROR_SYSTEM_FILE_EOF;
        return ret;
    }
    
    if (pnread != NULL) {
        *pnread = nread;
    }
    
    return ret;
}

int SrsFileStream::write(void* buf, size_t count, ssize_t* pnwrite)
{
    int ret = ERROR_SUCCESS;
    
    ssize_t nwrite;
    if ((nwrite = ::write(fd, buf, count)) < 0) {
        ret = ERROR_SYSTEM_FILE_WRITE;
        srs_error("write to file %s failed. ret=%d", _file.c_str(), ret);
        return ret;
    }
    
    if (pnwrite != NULL) {
        *pnwrite = nwrite;
    }
    
    return ret;
}
141 142 143 144 145
int64_t SrsFileStream::tellg()
{
    return (int64_t)::lseek(fd, 0, SEEK_CUR);
}
winlin authored
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
SrsFlvEncoder::SrsFlvEncoder()
{
    _fs = NULL;
    tag_stream = new SrsStream();
}

SrsFlvEncoder::~SrsFlvEncoder()
{
    srs_freep(tag_stream);
}

int SrsFlvEncoder::initialize(SrsFileStream* fs)
{
    int ret = ERROR_SUCCESS;
    
    _fs = fs;
    
    return ret;
}

int SrsFlvEncoder::write_header()
{
    int ret = ERROR_SUCCESS;
    
    static char flv_header[] = {
        'F', 'L', 'V', // Signatures "FLV"
        (char)0x01, // File version (for example, 0x01 for FLV version 1)
        (char)0x00, // 4, audio; 1, video; 5 audio+video.
        (char)0x00, (char)0x00, (char)0x00, (char)0x09, // DataOffset UI32 The length of this header in bytes
        (char)0x00, (char)0x00, (char)0x00, (char)0x00// PreviousTagSize0 UI32 Always 0
    };
    
178 179 180
    // flv specification should set the audio and video flag,
    // actually in practise, application generally ignore this flag,
    // so we generally set the audio/video to 0.
winlin authored
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
    
    // write data.
    if ((ret = _fs->write(flv_header, sizeof(flv_header), NULL)) != ERROR_SUCCESS) {
        srs_error("write flv header failed. ret=%d", ret);
        return ret;
    }
    
    return ret;
}

int SrsFlvEncoder::write_metadata(char* data, int size)
{
    int ret = ERROR_SUCCESS;
    
    static char tag_header[] = {
        (char)18, // TagType UB [5], 18 = script data
        (char)0x00, (char)0x00, (char)0x00, // DataSize UI24 Length of the message.
        (char)0x00, (char)0x00, (char)0x00, // Timestamp UI24 Time in milliseconds at which the data in this tag applies.
        (char)0x00, // TimestampExtended UI8
        (char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0.
    };
    
    // write data size.
    if ((ret = tag_stream->initialize(tag_header + 1, 3)) != ERROR_SUCCESS) {
        return ret;
    }
    tag_stream->write_3bytes(size);
    
    if ((ret = write_tag(tag_header, sizeof(tag_header), data, size)) != ERROR_SUCCESS) {
        srs_error("write flv data tag failed. ret=%d", ret);
        return ret;
    }
    
    return ret;
}
217
int SrsFlvEncoder::write_audio(int64_t timestamp, char* data, int size)
winlin authored
218 219 220
{
    int ret = ERROR_SUCCESS;
    
221 222
    timestamp &= 0x7fffffff;
    
winlin authored
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
    static char tag_header[] = {
        (char)8, // TagType UB [5], 8 = audio
        (char)0x00, (char)0x00, (char)0x00, // DataSize UI24 Length of the message.
        (char)0x00, (char)0x00, (char)0x00, // Timestamp UI24 Time in milliseconds at which the data in this tag applies.
        (char)0x00, // TimestampExtended UI8
        (char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0.
    };
    
    // write data size.
    if ((ret = tag_stream->initialize(tag_header + 1, 7)) != ERROR_SUCCESS) {
        return ret;
    }
    tag_stream->write_3bytes(size);
    tag_stream->write_3bytes(timestamp);
    // default to little-endian
    tag_stream->write_1bytes((timestamp >> 24) & 0xFF);
    
    if ((ret = write_tag(tag_header, sizeof(tag_header), data, size)) != ERROR_SUCCESS) {
        srs_error("write flv audio tag failed. ret=%d", ret);
        return ret;
    }
    
    return ret;
}
248
int SrsFlvEncoder::write_video(int64_t timestamp, char* data, int size)
winlin authored
249 250 251
{
    int ret = ERROR_SUCCESS;
    
252 253
    timestamp &= 0x7fffffff;
    
winlin authored
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
    static char tag_header[] = {
        (char)9, // TagType UB [5], 9 = video
        (char)0x00, (char)0x00, (char)0x00, // DataSize UI24 Length of the message.
        (char)0x00, (char)0x00, (char)0x00, // Timestamp UI24 Time in milliseconds at which the data in this tag applies.
        (char)0x00, // TimestampExtended UI8
        (char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0.
    };
    
    // write data size.
    if ((ret = tag_stream->initialize(tag_header + 1, 7)) != ERROR_SUCCESS) {
        return ret;
    }
    tag_stream->write_3bytes(size);
    tag_stream->write_3bytes(timestamp);
    // default to little-endian
    tag_stream->write_1bytes((timestamp >> 24) & 0xFF);
    
    if ((ret = write_tag(tag_header, sizeof(tag_header), data, size)) != ERROR_SUCCESS) {
        srs_error("write flv video tag failed. ret=%d", ret);
        return ret;
    }
    
    return ret;
}
winlin authored
279
int SrsFlvEncoder::write_tag(char* header, int header_size, char* tag, int tag_size)
winlin authored
280 281 282 283 284 285 286 287 288 289
{
    int ret = ERROR_SUCCESS;
    
    // write tag header.
    if ((ret = _fs->write(header, header_size, NULL)) != ERROR_SUCCESS) {
        srs_error("write flv tag header failed. ret=%d", ret);
        return ret;
    }
    
    // write tag data.
winlin authored
290
    if ((ret = _fs->write(tag, tag_size, NULL)) != ERROR_SUCCESS) {
winlin authored
291 292 293 294 295 296 297 298 299
        srs_error("write flv tag failed. ret=%d", ret);
        return ret;
    }
    
    // PreviousTagSizeN UI32 Size of last tag, including its header, in bytes.
    static char pre_size[4];
    if ((ret = tag_stream->initialize(pre_size, 4)) != ERROR_SUCCESS) {
        return ret;
    }
winlin authored
300
    tag_stream->write_4bytes(tag_size + header_size);
winlin authored
301 302 303 304 305 306 307
    if ((ret = _fs->write(pre_size, sizeof(pre_size), NULL)) != ERROR_SUCCESS) {
        srs_error("write flv previous tag size failed. ret=%d", ret);
        return ret;
    }
    
    return ret;
}
winlin authored
308
309 310
SrsFlvSegment::SrsFlvSegment()
{
311 312
    path = "";
    has_keyframe = false;
313 314
    duration = 0;
    starttime = -1;
315
    sequence_header_offset = 0;
316
    stream_starttime = 0;
317 318
    stream_previous_pkt_time = -1;
    stream_duration = 0;
319 320 321 322
}

void SrsFlvSegment::reset()
{
323
    has_keyframe = false;
324
    starttime = -1;
325
    duration = 0;
326
    sequence_header_offset = 0;
327 328
}
329
SrsDvrPlan::SrsDvrPlan()
winlin authored
330
{
331
    _source = NULL;
winlin authored
332 333
    _req = NULL;
    jitter = NULL;
winlin authored
334 335 336
    dvr_enabled = false;
    fs = new SrsFileStream();
    enc = new SrsFlvEncoder();
337
    segment = new SrsFlvSegment();
winlin authored
338 339
}
340
SrsDvrPlan::~SrsDvrPlan()
winlin authored
341
{
winlin authored
342
    srs_freep(jitter);
winlin authored
343 344
    srs_freep(fs);
    srs_freep(enc);
345
    srs_freep(segment);
winlin authored
346 347
}
winlin authored
348
int SrsDvrPlan::initialize(SrsSource* source, SrsRequest* req)
winlin authored
349 350
{
    int ret = ERROR_SUCCESS;
winlin authored
351
    
352
    _source = source;
winlin authored
353
    _req = req;
winlin authored
354
355 356 357
    return ret;
}
winlin authored
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
int SrsDvrPlan::on_publish()
{
    int ret = ERROR_SUCCESS;
    
    // support multiple publish.
    if (dvr_enabled) {
        return ret;
    }
    
    SrsRequest* req = _req;

    if (!_srs_config->get_dvr_enabled(req->vhost)) {
        return ret;
    }
    
373
    // jitter when publish, ensure whole stream start from 0.
winlin authored
374 375 376
    srs_freep(jitter);
    jitter = new SrsRtmpJitter();
    
377 378 379
    // always update time cache.
    srs_update_system_time_ms();
    
380
    // when republish, stream starting.
381
    segment->stream_previous_pkt_time = -1;
382
    segment->stream_starttime = srs_get_system_time_ms();
383
    segment->stream_duration = 0;
384 385 386 387 388 389 390 391 392 393 394 395 396 397
    
    if ((ret = open_new_segment()) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
}

int SrsDvrPlan::open_new_segment()
{
    int ret = ERROR_SUCCESS;
    
    SrsRequest* req = _req;
    
winlin authored
398 399 400 401 402 403 404 405 406 407 408 409
    // new flv file
    std::stringstream path;
    
    path << _srs_config->get_dvr_path(req->vhost)
        << "/" << req->app << "/" 
        << req->stream << "." << srs_get_system_time_ms() << ".flv";
    
    if ((ret = flv_open(req->get_stream_url(), path.str())) != ERROR_SUCCESS) {
        return ret;
    }
    dvr_enabled = true;
    
410 411 412 413 414 415 416
    return ret;
}

int SrsDvrPlan::on_dvr_request_sh()
{
    int ret = ERROR_SUCCESS;
    
winlin authored
417
    // the dvr is enabled, notice the source to push the data.
418
    if ((ret = _source->on_dvr_request_sh()) != ERROR_SUCCESS) {
winlin authored
419 420 421 422 423 424
        return ret;
    }
    
    return ret;
}
425 426 427 428 429 430 431 432 433 434 435
int SrsDvrPlan::on_video_keyframe()
{
    int ret = ERROR_SUCCESS;
    return ret;
}

int64_t SrsDvrPlan::filter_timestamp(int64_t timestamp)
{
    return timestamp;
}
436
int SrsDvrPlan::on_meta_data(SrsOnMetaDataPacket* metadata)
winlin authored
437 438
{
    int ret = ERROR_SUCCESS;
winlin authored
439
    
winlin authored
440 441 442 443
    if (!dvr_enabled) {
        return ret;
    }
    
winlin authored
444 445 446 447 448 449 450 451 452 453 454
    int size = 0;
    char* payload = NULL;
    if ((ret = metadata->encode(size, payload)) != ERROR_SUCCESS) {
        return ret;
    }
    SrsAutoFree(char, payload, true);
    
    if ((ret = enc->write_metadata(payload, size)) != ERROR_SUCCESS) {
        return ret;
    }
    
winlin authored
455 456 457
    return ret;
}
458
int SrsDvrPlan::on_audio(SrsSharedPtrMessage* audio)
winlin authored
459 460
{
    int ret = ERROR_SUCCESS;
winlin authored
461
    
winlin authored
462 463 464 465
    if (!dvr_enabled) {
        return ret;
    }
    
winlin authored
466 467 468 469
    if ((jitter->correct(audio, 0, 0)) != ERROR_SUCCESS) {
        return ret;
    }
    
winlin authored
470 471
    char* payload = (char*)audio->payload;
    int size = (int)audio->size;
472
    int64_t timestamp = filter_timestamp(audio->header.timestamp);
winlin authored
473 474 475 476
    if ((ret = enc->write_audio(timestamp, payload, size)) != ERROR_SUCCESS) {
        return ret;
    }
    
477
    if ((ret = update_duration(audio)) != ERROR_SUCCESS) {
winlin authored
478 479 480
        return ret;
    }
    
winlin authored
481 482 483
    return ret;
}
484
int SrsDvrPlan::on_video(SrsSharedPtrMessage* video)
winlin authored
485 486
{
    int ret = ERROR_SUCCESS;
winlin authored
487
    
winlin authored
488 489 490 491
    if (!dvr_enabled) {
        return ret;
    }
    
winlin authored
492 493 494
    char* payload = (char*)video->payload;
    int size = (int)video->size;
    
495
#ifdef SRS_AUTO_HTTP_CALLBACK
496 497 498
    bool is_key_frame = SrsCodec::video_is_h264((int8_t*)payload, size) 
        && SrsCodec::video_is_keyframe((int8_t*)payload, size) 
        && !SrsCodec::video_is_sequence_header((int8_t*)payload, size);
499
    if (is_key_frame) {
500
        segment->has_keyframe = true;
501 502 503
        if ((ret = on_video_keyframe()) != ERROR_SUCCESS) {
            return ret;
        }
504
    }
505
    srs_verbose("dvr video is key: %d", is_key_frame);
506 507
#endif
    
508 509 510 511 512 513 514 515 516
    if ((jitter->correct(video, 0, 0)) != ERROR_SUCCESS) {
        return ret;
    }
    
    int32_t timestamp = filter_timestamp(video->header.timestamp);
    if ((ret = enc->write_video(timestamp, payload, size)) != ERROR_SUCCESS) {
        return ret;
    }
    
517
    if ((ret = update_duration(video)) != ERROR_SUCCESS) {
winlin authored
518 519 520 521 522 523
        return ret;
    }
    
    return ret;
}
524 525 526 527
int SrsDvrPlan::flv_open(string stream, string path)
{
    int ret = ERROR_SUCCESS;
    
528
    segment->reset();
529
    
530 531
    std::string tmp_file = path + ".tmp";
    if ((ret = fs->open(tmp_file)) != ERROR_SUCCESS) {
532 533 534 535 536 537 538 539 540
        srs_error("open file stream for file %s failed. ret=%d", path.c_str(), ret);
        return ret;
    }
    
    if ((ret = enc->initialize(fs)) != ERROR_SUCCESS) {
        srs_error("initialize enc by fs for file %s failed. ret=%d", path.c_str(), ret);
        return ret;
    }
    
541
    if ((ret = write_flv_header()) != ERROR_SUCCESS) {
542 543 544
        return ret;
    }
    
545
    segment->path = path;
546
    
547 548 549 550 551 552
    srs_trace("dvr stream %s to file %s", stream.c_str(), path.c_str());
    return ret;
}

int SrsDvrPlan::flv_close()
{
553 554 555 556 557 558
    int ret = ERROR_SUCCESS;
    
    if ((ret = fs->close()) != ERROR_SUCCESS) {
        return ret;
    }
    
559 560
    std::string tmp_file = segment->path + ".tmp";
    if (rename(tmp_file.c_str(), segment->path.c_str()) < 0) {
561 562
        ret = ERROR_SYSTEM_FILE_RENAME;
        srs_error("rename flv file failed, %s => %s. ret=%d", 
563
            tmp_file.c_str(), segment->path.c_str(), ret);
564 565 566
        return ret;
    }
    
567
#ifdef SRS_AUTO_HTTP_CALLBACK
winlin authored
568
    if ((ret = on_dvr_hss_reap_flv()) != ERROR_SUCCESS) {
569
        return ret;
570 571 572 573 574 575
    }
#endif
    
    return ret;
}
576
int SrsDvrPlan::update_duration(SrsSharedPtrMessage* msg)
577 578
{
    int ret = ERROR_SUCCESS;
579 580 581

    // we must assumpt that the stream timestamp is monotonically increase,
    // that is, always use time jitter to correct the timestamp.
582
    
583 584
    // set the segment starttime at first time
    if (segment->starttime < 0) {
585
        segment->starttime = msg->header.timestamp;
586
    }
587 588 589 590 591 592 593 594 595 596 597 598
    
    // no previous packet or timestamp overflow.
    if (segment->stream_previous_pkt_time < 0 || segment->stream_previous_pkt_time > msg->header.timestamp) {
        segment->stream_previous_pkt_time = msg->header.timestamp;
    }
    
    // collect segment and stream duration, timestamp overflow is ok.
    segment->duration += msg->header.timestamp - segment->stream_previous_pkt_time;
    segment->stream_duration += msg->header.timestamp - segment->stream_previous_pkt_time;
    
    // update previous packet time
    segment->stream_previous_pkt_time = msg->header.timestamp;
599
    
600
    return ret;
601 602
}
603 604 605 606 607 608 609 610 611 612 613 614
int SrsDvrPlan::write_flv_header()
{
    int ret = ERROR_SUCCESS;
    
    if ((ret = enc->write_header()) != ERROR_SUCCESS) {
        srs_error("write flv header failed. ret=%d", ret);
        return ret;
    }
    
    return ret;
}
winlin authored
615
int SrsDvrPlan::on_dvr_hss_reap_flv()
616 617 618 619
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_CALLBACK
winlin authored
620 621 622 623
    // HTTP: on_dvr_hss_reap_flv 
    SrsConfDirective* on_dvr_hss_reap_flv = _srs_config->get_vhost_on_dvr_hss_reap_flv(_req->vhost);
    if (!on_dvr_hss_reap_flv) {
        srs_info("ignore the empty http callback: on_dvr_hss_reap_flv");
624 625 626
        return ret;
    }
    
winlin authored
627 628 629
    for (int i = 0; i < (int)on_dvr_hss_reap_flv->args.size(); i++) {
        std::string url = on_dvr_hss_reap_flv->args.at(i);
        SrsHttpHooks::on_dvr_hss_reap_flv(url, _req, segment);
630 631 632 633 634 635
    }
#endif
    
    return ret;
}
636
SrsDvrPlan* SrsDvrPlan::create_plan(string vhost)
637
{
638 639 640 641 642
    std::string plan = _srs_config->get_dvr_plan(vhost);
    if (plan == SRS_CONF_DEFAULT_DVR_PLAN_SEGMENT) {
        return new SrsDvrSegmentPlan();
    } else if (plan == SRS_CONF_DEFAULT_DVR_PLAN_SESSION) {
        return new SrsDvrSessionPlan();
643 644
    } else if (plan == SRS_CONF_DEFAULT_DVR_PLAN_HSS) {
        return new SrsDvrHssPlan();
645 646 647
    } else {
        return new SrsDvrSessionPlan();
    }
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
}

SrsDvrSessionPlan::SrsDvrSessionPlan()
{
}

SrsDvrSessionPlan::~SrsDvrSessionPlan()
{
}

void SrsDvrSessionPlan::on_unpublish()
{
    // support multiple publish.
    if (!dvr_enabled) {
        return;
    }
    
    // ignore error.
    int ret = flv_close();
    if (ret != ERROR_SUCCESS) {
        srs_warn("ignore flv close error. ret=%d", ret);
    }
    
    dvr_enabled = false;
}
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
SrsDvrSegmentPlan::SrsDvrSegmentPlan()
{
    segment_duration = -1;
}

SrsDvrSegmentPlan::~SrsDvrSegmentPlan()
{
}

int SrsDvrSegmentPlan::initialize(SrsSource* source, SrsRequest* req)
{
    int ret = ERROR_SUCCESS;
    
    if ((ret = SrsDvrPlan::initialize(source, req)) != ERROR_SUCCESS) {
        return ret;
    }
    
    segment_duration = _srs_config->get_dvr_duration(req->vhost);
    // to ms
    segment_duration *= 1000;
    
    return ret;
}
winlin authored
698
int SrsDvrSegmentPlan::on_publish()
699 700 701
{
    int ret = ERROR_SUCCESS;
    
winlin authored
702
    // if already opened, continue to dvr.
703 704 705
    // the segment plan maybe keep running longer than the encoder.
    // for example, segment running, encoder restart,
    // the segment plan will just continue going and donot open new segment.
winlin authored
706 707
    if (fs->is_open()) {
        dvr_enabled = true;
708 709 710
        return ret;
    }
    
winlin authored
711
    return SrsDvrPlan::on_publish();
712 713 714 715 716 717 718 719 720 721 722
}

void SrsDvrSegmentPlan::on_unpublish()
{
    // support multiple publish.
    if (!dvr_enabled) {
        return;
    }
    dvr_enabled = false;
}
723
int SrsDvrSegmentPlan::update_duration(SrsSharedPtrMessage* msg)
724 725 726
{
    int ret = ERROR_SUCCESS;
    
727 728
    if ((ret = SrsDvrPlan::update_duration(msg)) != ERROR_SUCCESS) {
        return ret;
729 730
    }
    
731 732
    srs_assert(segment);
    
733
    // reap if exceed duration.
734
    if (segment_duration > 0 && segment->duration > segment_duration) {
winlin authored
735
        if ((ret = flv_close()) != ERROR_SUCCESS) {
736
            segment->reset();
winlin authored
737 738
            return ret;
        }
739
        on_unpublish();
winlin authored
740
        
741
        if ((ret = open_new_segment()) != ERROR_SUCCESS) {
winlin authored
742 743
            return ret;
        }
744 745 746 747 748
    }
    
    return ret;
}
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
SrsDvrHssPlan::SrsDvrHssPlan()
{
    segment_duration = -1;
    expect_reap_time = 0;
}

SrsDvrHssPlan::~SrsDvrHssPlan()
{
}

int SrsDvrHssPlan::initialize(SrsSource* source, SrsRequest* req)
{
    int ret = ERROR_SUCCESS;
    
    if ((ret = SrsDvrPlan::initialize(source, req)) != ERROR_SUCCESS) {
        return ret;
    }
    
    // TODO: FIXME: support reload
    segment_duration = _srs_config->get_dvr_duration(req->vhost);
    // to ms
    segment_duration *= 1000;
    
    return ret;
}

int SrsDvrHssPlan::on_publish()
{
    int ret = ERROR_SUCCESS;
    
    // if already opened, continue to dvr.
    // the segment plan maybe keep running longer than the encoder.
    // for example, segment running, encoder restart,
    // the segment plan will just continue going and donot open new segment.
    if (fs->is_open()) {
        dvr_enabled = true;
        return ret;
    }
    
    if ((ret = SrsDvrPlan::on_publish()) != ERROR_SUCCESS) {
        return ret;
    }
    
    // expect reap flv time
    expect_reap_time = segment->stream_starttime + segment_duration;
    
    return ret;
}

void SrsDvrHssPlan::on_unpublish()
{
    // support multiple publish.
    if (!dvr_enabled) {
        return;
    }
    dvr_enabled = false;
}
807
int SrsDvrHssPlan::on_meta_data(SrsOnMetaDataPacket* metadata)
808 809
{
    int ret = ERROR_SUCCESS;
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
    
    SrsRequest* req = _req;
    
    // new flv file
    std::stringstream path;
    path << _srs_config->get_dvr_path(req->vhost)
        << "/" << req->app << "/" 
        << req->stream << ".header.flv";
        
    SrsFileStream fs;
    if ((ret = fs.open(path.str().c_str())) != ERROR_SUCCESS) {
        return ret;
    }
    
    SrsFlvEncoder enc;
    if ((ret = enc.initialize(&fs)) != ERROR_SUCCESS) {
        return ret;
    }
    
    if ((ret = enc.write_header()) != ERROR_SUCCESS) {
        return ret;
    }
    
    int size = 0;
    char* payload = NULL;
    if ((ret = metadata->encode(size, payload)) != ERROR_SUCCESS) {
        return ret;
    }
    SrsAutoFree(char, payload, true);
    
    if ((ret = enc.write_metadata(payload, size)) != ERROR_SUCCESS) {
        return ret;
    }
    
#ifdef SRS_AUTO_HTTP_CALLBACK
winlin authored
845
    if ((ret = on_dvr_hss_reap_flv_header(path.str())) != ERROR_SUCCESS) {
846 847 848 849
        return ret;
    }
#endif
    
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
    return ret;
}

int SrsDvrHssPlan::write_flv_header()
{
    int ret = ERROR_SUCCESS;
    return ret;
}

int SrsDvrHssPlan::on_dvr_request_sh()
{
    int ret = ERROR_SUCCESS;
    return ret;
}

int SrsDvrHssPlan::on_video_keyframe()
{
    int ret = ERROR_SUCCESS;
    
    segment->sequence_header_offset = fs->tellg();
    if ((ret = SrsDvrPlan::on_dvr_request_sh()) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
}

int64_t SrsDvrHssPlan::filter_timestamp(int64_t timestamp)
{
    //return timestamp;
    srs_assert(segment);
    srs_verbose("filter timestamp from %"PRId64" to %"PRId64, timestamp, segment->stream_starttime + timestamp);
    return segment->stream_starttime + timestamp;
}
winlin authored
885
int SrsDvrHssPlan::on_dvr_hss_reap_flv_header(string path)
886 887 888 889
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_CALLBACK
winlin authored
890 891 892 893
    // HTTP: on_dvr_hss_reap_flv_header 
    SrsConfDirective* on_dvr_hss_reap_flv = _srs_config->get_vhost_on_dvr_hss_reap_flv(_req->vhost);
    if (!on_dvr_hss_reap_flv) {
        srs_info("ignore the empty http callback: on_dvr_hss_reap_flv");
894 895 896
        return ret;
    }
    
winlin authored
897 898 899
    for (int i = 0; i < (int)on_dvr_hss_reap_flv->args.size(); i++) {
        std::string url = on_dvr_hss_reap_flv->args.at(i);
        SrsHttpHooks::on_dvr_hss_reap_flv_header(url, _req, path);
900 901 902 903 904 905
    }
#endif
    
    return ret;
}
906
int SrsDvrHssPlan::update_duration(SrsSharedPtrMessage* msg)
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
{
    int ret = ERROR_SUCCESS;
    
    if ((ret = SrsDvrPlan::update_duration(msg)) != ERROR_SUCCESS) {
        return ret;
    }
    
    srs_assert(segment);
    
    // if not initialized, ignore reap.
    if (expect_reap_time <= 0 
        || segment->stream_starttime <= 0 
        || segment->stream_duration <= 0
    ) {
        return ret;
    }
    
    // reap if exceed atc expect time.
    if (segment->stream_starttime + segment->stream_duration > expect_reap_time) {
926
        srs_verbose("hss reap start=%"PRId64", duration=%"PRId64", expect=%"PRId64
927
            ", segment(start=%"PRId64", duration=%"PRId64", file=%s", 
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
            segment->stream_starttime, segment->stream_duration, expect_reap_time,
            segment->stream_starttime + segment->starttime, 
            segment->duration, segment->path.c_str());
            
        // update expect reap time
        expect_reap_time += segment_duration;
        
        if ((ret = flv_close()) != ERROR_SUCCESS) {
            segment->reset();
            return ret;
        }
        on_unpublish();
        
        if ((ret = open_new_segment()) != ERROR_SUCCESS) {
            return ret;
        }
    }
    
    return ret;
}
949 950 951 952 953 954 955 956 957 958 959
SrsDvr::SrsDvr(SrsSource* source)
{
    _source = source;
    plan = NULL;
}

SrsDvr::~SrsDvr()
{
    srs_freep(plan);
}
960
int SrsDvr::initialize(SrsRequest* req)
961 962 963 964
{
    int ret = ERROR_SUCCESS;
    
    srs_freep(plan);
965
    plan = SrsDvrPlan::create_plan(req->vhost);
966
967
    if ((ret = plan->initialize(_source, req)) != ERROR_SUCCESS) {
968 969 970 971 972 973
        return ret;
    }
    
    return ret;
}
winlin authored
974
int SrsDvr::on_publish(SrsRequest* /*req*/)
975 976 977
{
    int ret = ERROR_SUCCESS;
    
winlin authored
978
    if ((ret = plan->on_publish()) != ERROR_SUCCESS) {
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
        return ret;
    }
    
    return ret;
}

void SrsDvr::on_unpublish()
{
    plan->on_unpublish();
}

int SrsDvr::on_meta_data(SrsOnMetaDataPacket* metadata)
{
    int ret = ERROR_SUCCESS;
    
    if ((ret = plan->on_meta_data(metadata)) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
}
1001
int SrsDvr::on_audio(SrsSharedPtrMessage* audio)
1002 1003 1004
{
    int ret = ERROR_SUCCESS;
    
1005
    SrsAutoFree(SrsSharedPtrMessage, audio, false);
1006 1007 1008 1009 1010 1011 1012 1013
    
    if ((ret = plan->on_audio(audio)) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
}
1014
int SrsDvr::on_video(SrsSharedPtrMessage* video)
1015 1016 1017
{
    int ret = ERROR_SUCCESS;
    
1018
    SrsAutoFree(SrsSharedPtrMessage, video, false);
1019 1020 1021 1022 1023 1024 1025 1026
    
    if ((ret = plan->on_video(video)) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
}
1027 1028
#endif