Blame view

trunk/src/app/srs_app_dvr.cpp 13.9 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 33 34
using namespace std;

#include <srs_app_config.hpp>
#include <srs_protocol_rtmp.hpp>
#include <srs_core_autofree.hpp>
35
#include <srs_kernel_utility.hpp>
36
#include <srs_app_http_hooks.hpp>
winlin authored
37
#include <srs_kernel_codec.hpp>
38
#include <srs_kernel_flv.hpp>
39
#include <srs_kernel_file.hpp>
winlin authored
40
41 42
SrsFlvSegment::SrsFlvSegment()
{
43 44
    path = "";
    has_keyframe = false;
45 46
    duration = 0;
    starttime = -1;
47
    stream_starttime = 0;
48 49
    stream_previous_pkt_time = -1;
    stream_duration = 0;
50 51
}
winlin authored
52 53 54 55
SrsFlvSegment::~SrsFlvSegment()
{
}
56 57
void SrsFlvSegment::reset()
{
58
    has_keyframe = false;
59
    starttime = -1;
60
    duration = 0;
61 62
}
63
SrsDvrPlan::SrsDvrPlan()
winlin authored
64
{
65
    _source = NULL;
winlin authored
66 67
    _req = NULL;
    jitter = NULL;
winlin authored
68
    dvr_enabled = false;
69
    fs = new SrsFileWriter();
winlin authored
70
    enc = new SrsFlvEncoder();
71
    segment = new SrsFlvSegment();
72 73 74
    jitter_algorithm = SrsRtmpJitterAlgorithmOFF;
    
    _srs_config->subscribe(this);
winlin authored
75 76
}
77
SrsDvrPlan::~SrsDvrPlan()
winlin authored
78
{
79 80
    _srs_config->unsubscribe(this);
    
winlin authored
81
    srs_freep(jitter);
winlin authored
82 83
    srs_freep(fs);
    srs_freep(enc);
84
    srs_freep(segment);
winlin authored
85 86
}
winlin authored
87
int SrsDvrPlan::initialize(SrsSource* source, SrsRequest* req)
winlin authored
88 89
{
    int ret = ERROR_SUCCESS;
winlin authored
90
    
91
    _source = source;
winlin authored
92
    _req = req;
93 94
    
    jitter_algorithm = (SrsRtmpJitterAlgorithm)_srs_config->get_dvr_time_jitter(_req->vhost);
winlin authored
95
96 97 98
    return ret;
}
winlin authored
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
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;
    }
    
114
    // jitter when publish, ensure whole stream start from 0.
winlin authored
115 116 117
    srs_freep(jitter);
    jitter = new SrsRtmpJitter();
    
118 119 120
    // always update time cache.
    srs_update_system_time_ms();
    
121
    // when republish, stream starting.
122
    segment->stream_previous_pkt_time = -1;
123
    segment->stream_starttime = srs_get_system_time_ms();
124
    segment->stream_duration = 0;
125 126 127 128 129 130 131 132 133 134 135 136 137 138
    
    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
139 140 141 142 143 144 145 146 147 148 149 150
    // 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;
    
151 152 153 154 155 156 157
    return ret;
}

int SrsDvrPlan::on_dvr_request_sh()
{
    int ret = ERROR_SUCCESS;
    
winlin authored
158
    // the dvr is enabled, notice the source to push the data.
159
    if ((ret = _source->on_dvr_request_sh()) != ERROR_SUCCESS) {
winlin authored
160 161 162 163 164 165
        return ret;
    }
    
    return ret;
}
166 167 168 169 170 171 172 173 174 175 176
int SrsDvrPlan::on_video_keyframe()
{
    int ret = ERROR_SUCCESS;
    return ret;
}

int64_t SrsDvrPlan::filter_timestamp(int64_t timestamp)
{
    return timestamp;
}
177
int SrsDvrPlan::on_meta_data(SrsOnMetaDataPacket* metadata)
winlin authored
178 179
{
    int ret = ERROR_SUCCESS;
winlin authored
180
    
winlin authored
181 182 183 184
    if (!dvr_enabled) {
        return ret;
    }
    
winlin authored
185 186 187 188 189
    int size = 0;
    char* payload = NULL;
    if ((ret = metadata->encode(size, payload)) != ERROR_SUCCESS) {
        return ret;
    }
190
    SrsAutoFree(char, payload);
winlin authored
191 192 193 194 195
    
    if ((ret = enc->write_metadata(payload, size)) != ERROR_SUCCESS) {
        return ret;
    }
    
winlin authored
196 197 198
    return ret;
}
199
int SrsDvrPlan::on_audio(SrsSharedPtrMessage* audio)
winlin authored
200 201
{
    int ret = ERROR_SUCCESS;
winlin authored
202
    
winlin authored
203 204 205 206
    if (!dvr_enabled) {
        return ret;
    }
    
207
    if ((jitter->correct(audio, 0, 0, jitter_algorithm)) != ERROR_SUCCESS) {
winlin authored
208 209 210
        return ret;
    }
    
211 212
    char* payload = audio->payload;
    int size = audio->size;
213
    int64_t timestamp = filter_timestamp(audio->header.timestamp);
winlin authored
214 215 216 217
    if ((ret = enc->write_audio(timestamp, payload, size)) != ERROR_SUCCESS) {
        return ret;
    }
    
218
    if ((ret = update_duration(audio)) != ERROR_SUCCESS) {
winlin authored
219 220 221
        return ret;
    }
    
winlin authored
222 223 224
    return ret;
}
225
int SrsDvrPlan::on_video(SrsSharedPtrMessage* video)
winlin authored
226 227
{
    int ret = ERROR_SUCCESS;
winlin authored
228
    
winlin authored
229 230 231 232
    if (!dvr_enabled) {
        return ret;
    }
    
233 234
    char* payload = video->payload;
    int size = video->size;
winlin authored
235
    
236
#ifdef SRS_AUTO_HTTP_CALLBACK
237 238 239
    bool is_key_frame = SrsFlvCodec::video_is_h264(payload, size) 
        && SrsFlvCodec::video_is_keyframe(payload, size) 
        && !SrsFlvCodec::video_is_sequence_header(payload, size);
240
    if (is_key_frame) {
241
        segment->has_keyframe = true;
242 243 244
        if ((ret = on_video_keyframe()) != ERROR_SUCCESS) {
            return ret;
        }
245
    }
246
    srs_verbose("dvr video is key: %d", is_key_frame);
247 248
#endif
    
249
    if ((jitter->correct(video, 0, 0, jitter_algorithm)) != ERROR_SUCCESS) {
250 251 252
        return ret;
    }
    
253 254 255
    // update segment duration, session plan just update the duration,
    // the segment plan will reap segment if exceed, this video will write to next segment.
    if ((ret = update_duration(video)) != ERROR_SUCCESS) {
256 257 258
        return ret;
    }
    
259 260
    int32_t timestamp = filter_timestamp(video->header.timestamp);
    if ((ret = enc->write_video(timestamp, payload, size)) != ERROR_SUCCESS) {
winlin authored
261 262 263 264 265 266
        return ret;
    }
    
    return ret;
}
267
int SrsDvrPlan::on_reload_vhost_dvr(std::string /*vhost*/)
268 269 270 271 272 273 274 275
{
    int ret = ERROR_SUCCESS;
    
    jitter_algorithm = (SrsRtmpJitterAlgorithm)_srs_config->get_dvr_time_jitter(_req->vhost);
    
    return ret;
}
276 277 278 279
int SrsDvrPlan::flv_open(string stream, string path)
{
    int ret = ERROR_SUCCESS;
    
280
    segment->reset();
281
    
282
    std::string tmp_file = path + ".tmp";
283
    if ((ret = fs->open(tmp_file)) != ERROR_SUCCESS) {
284 285 286 287 288 289 290 291 292
        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;
    }
    
293
    if ((ret = write_flv_header()) != ERROR_SUCCESS) {
294 295 296
        return ret;
    }
    
297
    segment->path = path;
298
    
299 300 301 302 303 304
    srs_trace("dvr stream %s to file %s", stream.c_str(), path.c_str());
    return ret;
}

int SrsDvrPlan::flv_close()
{
305 306
    int ret = ERROR_SUCCESS;
    
307
    fs->close();
308
    
309 310
    std::string tmp_file = segment->path + ".tmp";
    if (rename(tmp_file.c_str(), segment->path.c_str()) < 0) {
311 312
        ret = ERROR_SYSTEM_FILE_RENAME;
        srs_error("rename flv file failed, %s => %s. ret=%d", 
313
            tmp_file.c_str(), segment->path.c_str(), ret);
314 315 316
        return ret;
    }
    
317 318 319
    return ret;
}
320
int SrsDvrPlan::update_duration(SrsSharedPtrMessage* msg)
321 322
{
    int ret = ERROR_SUCCESS;
323 324 325

    // we must assumpt that the stream timestamp is monotonically increase,
    // that is, always use time jitter to correct the timestamp.
326
    
327 328
    // set the segment starttime at first time
    if (segment->starttime < 0) {
329
        segment->starttime = msg->header.timestamp;
330
    }
331 332 333 334 335 336 337 338 339 340 341 342
    
    // 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;
343
    
344
    return ret;
345 346
}
347 348 349 350 351 352 353 354 355 356 357 358
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;
}
359
SrsDvrPlan* SrsDvrPlan::create_plan(string vhost)
360
{
361 362 363 364 365 366 367 368
    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();
    } else {
        return new SrsDvrSessionPlan();
    }
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
}

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;
}
395 396 397
SrsDvrSegmentPlan::SrsDvrSegmentPlan()
{
    segment_duration = -1;
398
    sh_video = sh_audio = NULL;
399 400 401 402
}

SrsDvrSegmentPlan::~SrsDvrSegmentPlan()
{
403 404
    srs_freep(sh_video);
    srs_freep(sh_audio);
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
}

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
422
int SrsDvrSegmentPlan::on_publish()
423 424 425
{
    int ret = ERROR_SUCCESS;
    
winlin authored
426
    // if already opened, continue to dvr.
427 428 429
    // 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
430 431
    if (fs->is_open()) {
        dvr_enabled = true;
432 433 434
        return ret;
    }
    
winlin authored
435
    return SrsDvrPlan::on_publish();
436 437 438 439 440 441 442 443 444 445 446
}

void SrsDvrSegmentPlan::on_unpublish()
{
    // support multiple publish.
    if (!dvr_enabled) {
        return;
    }
    dvr_enabled = false;
}
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
int SrsDvrSegmentPlan::on_audio(SrsSharedPtrMessage* audio)
{
    if (SrsFlvCodec::audio_is_sequence_header(audio->payload, audio->size)) {
        srs_freep(sh_audio);
        sh_audio = audio->copy();
    }
    
    return SrsDvrPlan::on_audio(audio);
}

int SrsDvrSegmentPlan::on_video(SrsSharedPtrMessage* video)
{
    if (SrsFlvCodec::video_is_sequence_header(video->payload, video->size)) {
        srs_freep(sh_video);
        sh_video = video->copy();
    }
    
    return SrsDvrPlan::on_video(video);
}
467
int SrsDvrSegmentPlan::update_duration(SrsSharedPtrMessage* msg)
468 469 470
{
    int ret = ERROR_SUCCESS;
    
471 472
    if ((ret = SrsDvrPlan::update_duration(msg)) != ERROR_SUCCESS) {
        return ret;
473 474
    }
    
475 476
    srs_assert(segment);
    
477 478 479 480 481 482 483 484 485
    // ignore if duration ok.
    if (segment_duration <= 0 || segment->duration < segment_duration) {
        return ret;
    }
    
    // when wait keyframe, ignore if no frame arrived.
    // @see https://github.com/winlinvip/simple-rtmp-server/issues/177
    if (_srs_config->get_dvr_wait_keyframe(_req->vhost)) {
        if (!msg->header.is_video()) {
winlin authored
486 487
            return ret;
        }
488
        
489 490
        char* payload = msg->payload;
        int size = msg->size;
491 492 493 494
        bool is_key_frame = SrsFlvCodec::video_is_h264(payload, size) 
            && SrsFlvCodec::video_is_keyframe(payload, size) 
            && !SrsFlvCodec::video_is_sequence_header(payload, size);
        if (!is_key_frame) {
495 496
            return ret;
        }
497 498
    }
    
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
    // reap segment
    if ((ret = flv_close()) != ERROR_SUCCESS) {
        segment->reset();
        return ret;
    }
    on_unpublish();
    
    // open new flv file
    if ((ret = open_new_segment()) != ERROR_SUCCESS) {
        return ret;
    }
    
    // update sequence header
    if (sh_video && (ret = SrsDvrPlan::on_video(sh_video)) != ERROR_SUCCESS) {
        return ret;
    }
    if (sh_audio && (ret = SrsDvrPlan::on_audio(sh_audio)) != ERROR_SUCCESS) {
        return ret;
    }
    
519 520 521
    return ret;
}
522 523 524 525 526 527 528 529 530 531 532
SrsDvr::SrsDvr(SrsSource* source)
{
    _source = source;
    plan = NULL;
}

SrsDvr::~SrsDvr()
{
    srs_freep(plan);
}
533
int SrsDvr::initialize(SrsRequest* req)
534 535 536 537
{
    int ret = ERROR_SUCCESS;
    
    srs_freep(plan);
538
    plan = SrsDvrPlan::create_plan(req->vhost);
539
540
    if ((ret = plan->initialize(_source, req)) != ERROR_SUCCESS) {
541 542 543 544 545 546
        return ret;
    }
    
    return ret;
}
winlin authored
547
int SrsDvr::on_publish(SrsRequest* /*req*/)
548 549 550
{
    int ret = ERROR_SUCCESS;
    
winlin authored
551
    if ((ret = plan->on_publish()) != ERROR_SUCCESS) {
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
        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;
}
574
int SrsDvr::on_audio(SrsSharedPtrMessage* audio)
575 576 577
{
    int ret = ERROR_SUCCESS;
    
578
    SrsAutoFree(SrsSharedPtrMessage, audio);
579 580 581 582 583 584 585 586
    
    if ((ret = plan->on_audio(audio)) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
}
587
int SrsDvr::on_video(SrsSharedPtrMessage* video)
588 589 590
{
    int ret = ERROR_SUCCESS;
    
591
    SrsAutoFree(SrsSharedPtrMessage, video);
592 593 594 595 596 597 598 599
    
    if ((ret = plan->on_video(video)) != ERROR_SUCCESS) {
        return ret;
    }
    
    return ret;
}
600 601
#endif
602