2014-04-15 06:01:57 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2014-04-16 01:28:02 +00:00
|
|
|
#include <srs_app_dvr.hpp>
|
2014-04-15 06:01:57 +00:00
|
|
|
|
|
|
|
#ifdef SRS_AUTO_DVR
|
|
|
|
|
2014-04-17 08:06:49 +00:00
|
|
|
#include <fcntl.h>
|
2014-04-17 09:35:21 +00:00
|
|
|
#include <sstream>
|
2014-04-17 08:06:49 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <srs_app_config.hpp>
|
2014-04-15 06:24:03 +00:00
|
|
|
#include <srs_kernel_error.hpp>
|
2014-04-17 08:06:49 +00:00
|
|
|
#include <srs_protocol_rtmp.hpp>
|
2014-04-16 07:58:06 +00:00
|
|
|
#include <srs_protocol_rtmp_stack.hpp>
|
2014-04-17 08:06:49 +00:00
|
|
|
#include <srs_app_source.hpp>
|
|
|
|
#include <srs_core_autofree.hpp>
|
|
|
|
#include <srs_kernel_stream.hpp>
|
2014-04-17 09:35:21 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2014-04-22 09:32:45 +00:00
|
|
|
#include <srs_app_http_hooks.hpp>
|
|
|
|
#include <srs_app_codec.hpp>
|
2014-04-17 08:06:49 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-17 10:13:59 +00:00
|
|
|
bool SrsFileStream::is_open()
|
|
|
|
{
|
|
|
|
return fd > 0;
|
|
|
|
}
|
|
|
|
|
2014-04-17 08:06:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-24 08:32:19 +00:00
|
|
|
int64_t SrsFileStream::tellg()
|
|
|
|
{
|
|
|
|
return (int64_t)::lseek(fd, 0, SEEK_CUR);
|
|
|
|
}
|
|
|
|
|
2014-04-17 08:06:49 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2014-04-17 08:08:56 +00:00
|
|
|
// 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.
|
2014-04-17 08:06:49 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-04-24 08:32:19 +00:00
|
|
|
int SrsFlvEncoder::write_audio(int64_t timestamp, char* data, int size)
|
2014-04-17 08:06:49 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-04-24 08:32:19 +00:00
|
|
|
timestamp &= 0x7fffffff;
|
|
|
|
|
2014-04-17 08:06:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-24 08:32:19 +00:00
|
|
|
int SrsFlvEncoder::write_video(int64_t timestamp, char* data, int size)
|
2014-04-17 08:06:49 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-04-24 08:32:19 +00:00
|
|
|
timestamp &= 0x7fffffff;
|
|
|
|
|
2014-04-17 08:06:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-17 08:16:17 +00:00
|
|
|
int SrsFlvEncoder::write_tag(char* header, int header_size, char* tag, int tag_size)
|
2014-04-17 08:06:49 +00:00
|
|
|
{
|
|
|
|
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.
|
2014-04-17 08:16:17 +00:00
|
|
|
if ((ret = _fs->write(tag, tag_size, NULL)) != ERROR_SUCCESS) {
|
2014-04-17 08:06:49 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-04-17 08:16:17 +00:00
|
|
|
tag_stream->write_4bytes(tag_size + header_size);
|
2014-04-17 08:06:49 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-04-15 06:24:03 +00:00
|
|
|
|
2014-04-23 08:33:42 +00:00
|
|
|
SrsFlvSegment::SrsFlvSegment()
|
|
|
|
{
|
2014-04-24 04:22:36 +00:00
|
|
|
path = "";
|
|
|
|
has_keyframe = false;
|
2014-04-23 08:33:42 +00:00
|
|
|
duration = 0;
|
|
|
|
starttime = -1;
|
2014-04-24 08:32:19 +00:00
|
|
|
sequence_header_offset = 0;
|
2014-04-23 09:53:14 +00:00
|
|
|
stream_starttime = 0;
|
2014-04-24 04:22:36 +00:00
|
|
|
stream_previous_pkt_time = -1;
|
|
|
|
stream_duration = 0;
|
2014-04-23 08:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SrsFlvSegment::reset()
|
|
|
|
{
|
2014-04-24 04:22:36 +00:00
|
|
|
has_keyframe = false;
|
2014-04-23 08:33:42 +00:00
|
|
|
starttime = -1;
|
2014-04-24 04:22:36 +00:00
|
|
|
duration = 0;
|
2014-04-24 08:32:19 +00:00
|
|
|
sequence_header_offset = 0;
|
2014-04-23 08:33:42 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 08:57:04 +00:00
|
|
|
SrsDvrPlan::SrsDvrPlan()
|
2014-04-15 06:24:03 +00:00
|
|
|
{
|
2014-04-17 08:57:04 +00:00
|
|
|
_source = NULL;
|
2014-04-17 10:13:59 +00:00
|
|
|
_req = NULL;
|
|
|
|
jitter = NULL;
|
2014-04-17 08:06:49 +00:00
|
|
|
dvr_enabled = false;
|
|
|
|
fs = new SrsFileStream();
|
|
|
|
enc = new SrsFlvEncoder();
|
2014-04-23 09:53:14 +00:00
|
|
|
segment = new SrsFlvSegment();
|
2014-04-15 06:24:03 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 08:57:04 +00:00
|
|
|
SrsDvrPlan::~SrsDvrPlan()
|
2014-04-15 06:24:03 +00:00
|
|
|
{
|
2014-04-17 10:13:59 +00:00
|
|
|
srs_freep(jitter);
|
2014-04-17 08:06:49 +00:00
|
|
|
srs_freep(fs);
|
|
|
|
srs_freep(enc);
|
2014-04-23 08:33:42 +00:00
|
|
|
srs_freep(segment);
|
2014-04-15 06:24:03 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 10:13:59 +00:00
|
|
|
int SrsDvrPlan::initialize(SrsSource* source, SrsRequest* req)
|
2014-04-15 06:24:03 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
2014-04-17 08:06:49 +00:00
|
|
|
|
2014-04-17 08:57:04 +00:00
|
|
|
_source = source;
|
2014-04-17 10:13:59 +00:00
|
|
|
_req = req;
|
2014-04-17 08:06:49 +00:00
|
|
|
|
2014-04-17 08:57:04 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-17 10:13:59 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-24 04:22:36 +00:00
|
|
|
// jitter when publish, ensure whole stream start from 0.
|
2014-04-17 10:13:59 +00:00
|
|
|
srs_freep(jitter);
|
|
|
|
jitter = new SrsRtmpJitter();
|
|
|
|
|
2014-04-17 10:23:49 +00:00
|
|
|
// always update time cache.
|
|
|
|
srs_update_system_time_ms();
|
|
|
|
|
2014-04-23 09:53:14 +00:00
|
|
|
// when republish, stream starting.
|
2014-04-24 04:22:36 +00:00
|
|
|
segment->stream_previous_pkt_time = -1;
|
2014-04-23 09:53:14 +00:00
|
|
|
segment->stream_starttime = srs_get_system_time_ms();
|
2014-04-24 04:22:36 +00:00
|
|
|
segment->stream_duration = 0;
|
2014-04-23 09:53:14 +00:00
|
|
|
|
|
|
|
if ((ret = open_new_segment()) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsDvrPlan::open_new_segment()
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
SrsRequest* req = _req;
|
|
|
|
|
2014-04-17 10:13:59 +00:00
|
|
|
// 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;
|
|
|
|
|
2014-04-24 08:32:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsDvrPlan::on_dvr_request_sh()
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-04-17 10:13:59 +00:00
|
|
|
// the dvr is enabled, notice the source to push the data.
|
2014-04-24 08:32:19 +00:00
|
|
|
if ((ret = _source->on_dvr_request_sh()) != ERROR_SUCCESS) {
|
2014-04-17 10:13:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-24 08:32:19 +00:00
|
|
|
int SrsDvrPlan::on_video_keyframe()
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t SrsDvrPlan::filter_timestamp(int64_t timestamp)
|
|
|
|
{
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
2014-04-17 08:57:04 +00:00
|
|
|
int SrsDvrPlan::on_meta_data(SrsOnMetaDataPacket* metadata)
|
2014-04-15 06:24:03 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
2014-04-17 08:06:49 +00:00
|
|
|
|
2014-04-17 08:22:21 +00:00
|
|
|
if (!dvr_enabled) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-17 08:06:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-15 06:24:03 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-17 08:57:04 +00:00
|
|
|
int SrsDvrPlan::on_audio(SrsSharedPtrMessage* audio)
|
2014-04-15 06:24:03 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
2014-04-17 08:06:49 +00:00
|
|
|
|
2014-04-17 08:22:21 +00:00
|
|
|
if (!dvr_enabled) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-17 10:13:59 +00:00
|
|
|
if ((jitter->correct(audio, 0, 0)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-17 08:06:49 +00:00
|
|
|
char* payload = (char*)audio->payload;
|
|
|
|
int size = (int)audio->size;
|
2014-04-24 08:32:19 +00:00
|
|
|
int64_t timestamp = filter_timestamp(audio->header.timestamp);
|
2014-04-17 08:06:49 +00:00
|
|
|
if ((ret = enc->write_audio(timestamp, payload, size)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-23 08:25:18 +00:00
|
|
|
if ((ret = update_duration(audio)) != ERROR_SUCCESS) {
|
2014-04-17 10:13:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-15 06:24:03 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-17 08:57:04 +00:00
|
|
|
int SrsDvrPlan::on_video(SrsSharedPtrMessage* video)
|
2014-04-15 06:24:03 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
2014-04-17 08:06:49 +00:00
|
|
|
|
2014-04-17 08:22:21 +00:00
|
|
|
if (!dvr_enabled) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-17 08:06:49 +00:00
|
|
|
char* payload = (char*)video->payload;
|
|
|
|
int size = (int)video->size;
|
|
|
|
|
2014-04-22 09:32:45 +00:00
|
|
|
#ifdef SRS_AUTO_HTTP_CALLBACK
|
2014-04-24 08:32:19 +00:00
|
|
|
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);
|
2014-04-22 09:32:45 +00:00
|
|
|
if (is_key_frame) {
|
2014-04-24 04:22:36 +00:00
|
|
|
segment->has_keyframe = true;
|
2014-04-24 08:32:19 +00:00
|
|
|
if ((ret = on_video_keyframe()) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-04-22 09:32:45 +00:00
|
|
|
}
|
2014-04-23 08:25:18 +00:00
|
|
|
srs_verbose("dvr video is key: %d", is_key_frame);
|
2014-04-22 09:32:45 +00:00
|
|
|
#endif
|
|
|
|
|
2014-04-24 08:32:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-23 08:25:18 +00:00
|
|
|
if ((ret = update_duration(video)) != ERROR_SUCCESS) {
|
2014-04-17 10:13:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-22 09:32:45 +00:00
|
|
|
int SrsDvrPlan::flv_open(string stream, string path)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-04-23 09:53:14 +00:00
|
|
|
segment->reset();
|
2014-04-23 08:33:42 +00:00
|
|
|
|
2014-04-22 09:50:22 +00:00
|
|
|
std::string tmp_file = path + ".tmp";
|
|
|
|
if ((ret = fs->open(tmp_file)) != ERROR_SUCCESS) {
|
2014-04-22 09:32:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-24 08:32:19 +00:00
|
|
|
if ((ret = write_flv_header()) != ERROR_SUCCESS) {
|
2014-04-22 09:32:45 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-24 04:22:36 +00:00
|
|
|
segment->path = path;
|
2014-04-23 08:25:18 +00:00
|
|
|
|
2014-04-22 09:32:45 +00:00
|
|
|
srs_trace("dvr stream %s to file %s", stream.c_str(), path.c_str());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsDvrPlan::flv_close()
|
|
|
|
{
|
2014-04-22 09:50:22 +00:00
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
if ((ret = fs->close()) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-24 04:22:36 +00:00
|
|
|
std::string tmp_file = segment->path + ".tmp";
|
|
|
|
if (rename(tmp_file.c_str(), segment->path.c_str()) < 0) {
|
2014-04-22 09:50:22 +00:00
|
|
|
ret = ERROR_SYSTEM_FILE_RENAME;
|
|
|
|
srs_error("rename flv file failed, %s => %s. ret=%d",
|
2014-04-24 04:22:36 +00:00
|
|
|
tmp_file.c_str(), segment->path.c_str(), ret);
|
2014-04-22 09:50:22 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-23 08:25:18 +00:00
|
|
|
#ifdef SRS_AUTO_HTTP_CALLBACK
|
2014-04-24 08:32:19 +00:00
|
|
|
if ((ret = on_dvr_reap_flv()) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
2014-04-23 08:25:18 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsDvrPlan::update_duration(SrsSharedPtrMessage* msg)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
2014-04-24 04:22:36 +00:00
|
|
|
|
|
|
|
// we must assumpt that the stream timestamp is monotonically increase,
|
|
|
|
// that is, always use time jitter to correct the timestamp.
|
2014-04-23 08:25:18 +00:00
|
|
|
|
2014-04-24 04:22:36 +00:00
|
|
|
// set the segment starttime at first time
|
|
|
|
if (segment->starttime < 0) {
|
2014-04-23 08:33:42 +00:00
|
|
|
segment->starttime = msg->header.timestamp;
|
2014-04-23 08:25:18 +00:00
|
|
|
}
|
2014-04-24 04:22:36 +00:00
|
|
|
|
|
|
|
// 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;
|
2014-04-23 08:25:18 +00:00
|
|
|
|
2014-04-22 09:50:22 +00:00
|
|
|
return ret;
|
2014-04-22 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 08:32:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsDvrPlan::on_dvr_reap_flv()
|
2014-04-22 09:32:45 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
#ifdef SRS_AUTO_HTTP_CALLBACK
|
2014-04-24 08:32:19 +00:00
|
|
|
// HTTP: on_dvr_reap_flv
|
|
|
|
SrsConfDirective* on_dvr_reap_flv = _srs_config->get_vhost_on_dvr_reap_flv(_req->vhost);
|
|
|
|
if (!on_dvr_reap_flv) {
|
|
|
|
srs_info("ignore the empty http callback: on_dvr_reap_flv");
|
2014-04-22 09:32:45 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-24 08:32:19 +00:00
|
|
|
for (int i = 0; i < (int)on_dvr_reap_flv->args.size(); i++) {
|
|
|
|
std::string url = on_dvr_reap_flv->args.at(i);
|
|
|
|
SrsHttpHooks::on_dvr_reap_flv(url, _req, segment);
|
2014-04-22 09:32:45 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-17 09:35:21 +00:00
|
|
|
SrsDvrPlan* SrsDvrPlan::create_plan(string vhost)
|
2014-04-17 08:57:04 +00:00
|
|
|
{
|
2014-04-17 09:35:21 +00:00
|
|
|
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();
|
2014-04-24 04:22:36 +00:00
|
|
|
} else if (plan == SRS_CONF_DEFAULT_DVR_PLAN_HSS) {
|
|
|
|
return new SrsDvrHssPlan();
|
2014-04-17 09:35:21 +00:00
|
|
|
} else {
|
|
|
|
return new SrsDvrSessionPlan();
|
|
|
|
}
|
2014-04-17 08:57:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-17 09:35:21 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-17 10:13:59 +00:00
|
|
|
int SrsDvrSegmentPlan::on_publish()
|
2014-04-17 09:35:21 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-04-17 10:13:59 +00:00
|
|
|
// if already opened, continue to dvr.
|
2014-04-23 09:53:14 +00:00
|
|
|
// 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.
|
2014-04-17 10:13:59 +00:00
|
|
|
if (fs->is_open()) {
|
|
|
|
dvr_enabled = true;
|
2014-04-17 09:35:21 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-17 10:13:59 +00:00
|
|
|
return SrsDvrPlan::on_publish();
|
2014-04-17 09:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SrsDvrSegmentPlan::on_unpublish()
|
|
|
|
{
|
|
|
|
// support multiple publish.
|
|
|
|
if (!dvr_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dvr_enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsDvrSegmentPlan::update_duration(SrsSharedPtrMessage* msg)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-04-23 08:25:18 +00:00
|
|
|
if ((ret = SrsDvrPlan::update_duration(msg)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
2014-04-17 09:35:21 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 08:33:42 +00:00
|
|
|
srs_assert(segment);
|
|
|
|
|
2014-04-17 09:35:21 +00:00
|
|
|
// reap if exceed duration.
|
2014-04-23 09:53:14 +00:00
|
|
|
if (segment_duration > 0 && segment->duration > segment_duration) {
|
2014-04-17 10:13:59 +00:00
|
|
|
if ((ret = flv_close()) != ERROR_SUCCESS) {
|
2014-04-23 09:53:14 +00:00
|
|
|
segment->reset();
|
2014-04-17 10:13:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2014-04-17 09:35:21 +00:00
|
|
|
on_unpublish();
|
2014-04-17 10:13:59 +00:00
|
|
|
|
2014-04-23 09:53:14 +00:00
|
|
|
if ((ret = open_new_segment()) != ERROR_SUCCESS) {
|
2014-04-17 10:13:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2014-04-17 09:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-24 04:22:36 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-24 09:38:57 +00:00
|
|
|
int SrsDvrHssPlan::on_meta_data(SrsOnMetaDataPacket* metadata)
|
2014-04-24 08:32:19 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
2014-04-24 09:38:57 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
if ((ret = on_dvr_reap_flv_header(path.str())) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-04-24 08:32:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-24 09:38:57 +00:00
|
|
|
int SrsDvrHssPlan::on_dvr_reap_flv_header(string path)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
#ifdef SRS_AUTO_HTTP_CALLBACK
|
|
|
|
// HTTP: on_dvr_reap_flv_header
|
|
|
|
SrsConfDirective* on_dvr_reap_flv = _srs_config->get_vhost_on_dvr_reap_flv(_req->vhost);
|
|
|
|
if (!on_dvr_reap_flv) {
|
|
|
|
srs_info("ignore the empty http callback: on_dvr_reap_flv");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < (int)on_dvr_reap_flv->args.size(); i++) {
|
|
|
|
std::string url = on_dvr_reap_flv->args.at(i);
|
|
|
|
SrsHttpHooks::on_dvr_reap_flv_header(url, _req, path);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-24 04:22:36 +00:00
|
|
|
int SrsDvrHssPlan::update_duration(SrsSharedPtrMessage* msg)
|
|
|
|
{
|
|
|
|
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) {
|
2014-04-24 06:26:24 +00:00
|
|
|
srs_verbose("hss reap start=%"PRId64", duration=%"PRId64", expect=%"PRId64
|
2014-04-24 08:32:19 +00:00
|
|
|
", segment(start=%"PRId64", duration=%"PRId64", file=%s",
|
2014-04-24 04:22:36 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-17 08:57:04 +00:00
|
|
|
SrsDvr::SrsDvr(SrsSource* source)
|
|
|
|
{
|
|
|
|
_source = source;
|
|
|
|
plan = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsDvr::~SrsDvr()
|
|
|
|
{
|
|
|
|
srs_freep(plan);
|
|
|
|
}
|
|
|
|
|
2014-04-17 09:35:21 +00:00
|
|
|
int SrsDvr::initialize(SrsRequest* req)
|
2014-04-17 08:57:04 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
srs_freep(plan);
|
2014-04-17 09:35:21 +00:00
|
|
|
plan = SrsDvrPlan::create_plan(req->vhost);
|
2014-04-17 08:57:04 +00:00
|
|
|
|
2014-04-17 09:35:21 +00:00
|
|
|
if ((ret = plan->initialize(_source, req)) != ERROR_SUCCESS) {
|
2014-04-17 08:57:04 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-17 10:13:59 +00:00
|
|
|
int SrsDvr::on_publish(SrsRequest* /*req*/)
|
2014-04-17 08:57:04 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-04-17 10:13:59 +00:00
|
|
|
if ((ret = plan->on_publish()) != ERROR_SUCCESS) {
|
2014-04-17 08:57:04 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsDvr::on_audio(SrsSharedPtrMessage* audio)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
SrsAutoFree(SrsSharedPtrMessage, audio, false);
|
|
|
|
|
|
|
|
if ((ret = plan->on_audio(audio)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsDvr::on_video(SrsSharedPtrMessage* video)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
SrsAutoFree(SrsSharedPtrMessage, video, false);
|
|
|
|
|
|
|
|
if ((ret = plan->on_video(video)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-15 06:01:57 +00:00
|
|
|
#endif
|
|
|
|
|