2014-05-23 05:56:40 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2015-04-29 09:38:23 +00:00
|
|
|
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
|
2014-05-23 05:56:40 +00:00
|
|
|
|
|
|
|
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-05-28 07:37:06 +00:00
|
|
|
#include <srs_kernel_flv.hpp>
|
2014-05-23 05:56:40 +00:00
|
|
|
|
2015-04-29 09:06:32 +00:00
|
|
|
// for srs-librtmp, @see https://github.com/simple-rtmp-server/srs/issues/213
|
2014-11-19 08:16:04 +00:00
|
|
|
#ifndef _WIN32
|
2014-06-14 08:24:15 +00:00
|
|
|
#include <unistd.h>
|
2014-11-19 08:16:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
2014-05-23 05:56:40 +00:00
|
|
|
#include <sstream>
|
|
|
|
using namespace std;
|
|
|
|
|
2014-05-28 07:37:06 +00:00
|
|
|
#include <srs_kernel_log.hpp>
|
2014-05-23 05:56:40 +00:00
|
|
|
#include <srs_kernel_error.hpp>
|
|
|
|
#include <srs_kernel_stream.hpp>
|
2014-07-04 23:33:18 +00:00
|
|
|
#include <srs_kernel_file.hpp>
|
2015-03-10 09:04:02 +00:00
|
|
|
#include <srs_kernel_codec.hpp>
|
2014-05-23 05:56:40 +00:00
|
|
|
|
|
|
|
SrsFlvEncoder::SrsFlvEncoder()
|
|
|
|
{
|
|
|
|
_fs = NULL;
|
|
|
|
tag_stream = new SrsStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsFlvEncoder::~SrsFlvEncoder()
|
|
|
|
{
|
|
|
|
srs_freep(tag_stream);
|
|
|
|
}
|
|
|
|
|
2014-06-29 09:05:26 +00:00
|
|
|
int SrsFlvEncoder::initialize(SrsFileWriter* fs)
|
2014-05-23 05:56:40 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-07-05 09:30:13 +00:00
|
|
|
srs_assert(fs);
|
|
|
|
|
2014-07-05 03:10:42 +00:00
|
|
|
if (!fs->is_open()) {
|
|
|
|
ret = ERROR_KERNEL_FLV_STREAM_CLOSED;
|
2015-01-18 14:56:01 +00:00
|
|
|
srs_warn("stream is not open for encoder. ret=%d", ret);
|
2014-07-05 03:10:42 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-23 05:56:40 +00:00
|
|
|
_fs = fs;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsFlvEncoder::write_header()
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-05-26 05:57:08 +00:00
|
|
|
// 9bytes header and 4bytes first previous-tag-size
|
2015-03-23 13:19:04 +00:00
|
|
|
char flv_header[] = {
|
2014-05-23 05:56:40 +00:00
|
|
|
'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.
|
2014-05-28 11:01:47 +00:00
|
|
|
(char)0x00, (char)0x00, (char)0x00, (char)0x09 // DataOffset UI32 The length of this header in bytes
|
2014-05-23 05:56:40 +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-05-28 11:01:47 +00:00
|
|
|
// write 9bytes header.
|
|
|
|
if ((ret = write_header(flv_header)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsFlvEncoder::write_header(char flv_header[9])
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-05-23 05:56:40 +00:00
|
|
|
// write data.
|
2014-05-28 11:01:47 +00:00
|
|
|
if ((ret = _fs->write(flv_header, 9, NULL)) != ERROR_SUCCESS) {
|
2014-05-23 05:56:40 +00:00
|
|
|
srs_error("write flv header failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-26 03:07:45 +00:00
|
|
|
// previous tag size.
|
2014-07-05 03:10:42 +00:00
|
|
|
char pts[] = { (char)0x00, (char)0x00, (char)0x00, (char)0x00 };
|
2014-05-28 11:01:47 +00:00
|
|
|
if ((ret = _fs->write(pts, 4, NULL)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-23 05:56:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-07 07:01:58 +00:00
|
|
|
int SrsFlvEncoder::write_metadata(char type, char* data, int size)
|
2014-05-23 05:56:40 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-07-05 09:30:13 +00:00
|
|
|
srs_assert(data);
|
|
|
|
|
2014-07-05 03:10:42 +00:00
|
|
|
// 11 bytes tag header
|
2015-05-24 15:16:56 +00:00
|
|
|
/*char tag_header[] = {
|
2015-01-07 07:01:58 +00:00
|
|
|
(char)type, // TagType UB [5], 18 = script data
|
2014-05-23 05:56:40 +00:00
|
|
|
(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.
|
2015-05-24 15:16:56 +00:00
|
|
|
};*/
|
2014-05-23 05:56:40 +00:00
|
|
|
|
|
|
|
// write data size.
|
2015-05-24 15:16:56 +00:00
|
|
|
if ((ret = tag_stream->initialize(tag_header, 8)) != ERROR_SUCCESS) {
|
2014-05-23 05:56:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2015-05-24 15:16:56 +00:00
|
|
|
tag_stream->write_1bytes(type);
|
2014-05-23 05:56:40 +00:00
|
|
|
tag_stream->write_3bytes(size);
|
2015-05-24 15:16:56 +00:00
|
|
|
tag_stream->write_3bytes(0x00);
|
|
|
|
tag_stream->write_1bytes(0x00);
|
2014-05-23 05:56:40 +00:00
|
|
|
|
|
|
|
if ((ret = write_tag(tag_header, sizeof(tag_header), data, size)) != ERROR_SUCCESS) {
|
2015-05-24 07:19:09 +00:00
|
|
|
if (!srs_is_client_gracefully_close(ret)) {
|
|
|
|
srs_error("write flv data tag failed. ret=%d", ret);
|
|
|
|
}
|
2014-05-23 05:56:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsFlvEncoder::write_audio(int64_t timestamp, char* data, int size)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-07-05 09:30:13 +00:00
|
|
|
srs_assert(data);
|
|
|
|
|
2014-05-23 05:56:40 +00:00
|
|
|
timestamp &= 0x7fffffff;
|
|
|
|
|
2014-05-26 05:57:08 +00:00
|
|
|
// 11bytes tag header
|
2015-05-24 15:16:56 +00:00
|
|
|
/*char tag_header[] = {
|
2015-03-10 09:04:02 +00:00
|
|
|
(char)SrsCodecFlvTagAudio, // TagType UB [5], 8 = audio
|
2014-05-23 05:56:40 +00:00
|
|
|
(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.
|
2015-05-24 15:16:56 +00:00
|
|
|
};*/
|
2014-05-23 05:56:40 +00:00
|
|
|
|
|
|
|
// write data size.
|
2015-05-24 15:16:56 +00:00
|
|
|
if ((ret = tag_stream->initialize(tag_header, 8)) != ERROR_SUCCESS) {
|
2014-05-23 05:56:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2015-05-24 15:16:56 +00:00
|
|
|
tag_stream->write_1bytes(SrsCodecFlvTagAudio);
|
2014-05-23 05:56:40 +00:00
|
|
|
tag_stream->write_3bytes(size);
|
2014-11-19 08:16:04 +00:00
|
|
|
tag_stream->write_3bytes((int32_t)timestamp);
|
2014-05-23 05:56:40 +00:00
|
|
|
// default to little-endian
|
|
|
|
tag_stream->write_1bytes((timestamp >> 24) & 0xFF);
|
|
|
|
|
|
|
|
if ((ret = write_tag(tag_header, sizeof(tag_header), data, size)) != ERROR_SUCCESS) {
|
2015-05-24 07:19:09 +00:00
|
|
|
if (!srs_is_client_gracefully_close(ret)) {
|
|
|
|
srs_error("write flv audio tag failed. ret=%d", ret);
|
|
|
|
}
|
2014-05-23 05:56:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsFlvEncoder::write_video(int64_t timestamp, char* data, int size)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-07-05 09:30:13 +00:00
|
|
|
srs_assert(data);
|
|
|
|
|
2014-05-23 05:56:40 +00:00
|
|
|
timestamp &= 0x7fffffff;
|
|
|
|
|
2014-05-26 05:57:08 +00:00
|
|
|
// 11bytes tag header
|
2015-05-24 15:16:56 +00:00
|
|
|
/*char tag_header[] = {
|
2015-03-10 09:04:02 +00:00
|
|
|
(char)SrsCodecFlvTagVideo, // TagType UB [5], 9 = video
|
2014-05-23 05:56:40 +00:00
|
|
|
(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.
|
2015-05-24 15:16:56 +00:00
|
|
|
};*/
|
2014-05-23 05:56:40 +00:00
|
|
|
|
|
|
|
// write data size.
|
2015-05-24 15:16:56 +00:00
|
|
|
if ((ret = tag_stream->initialize(tag_header, 8)) != ERROR_SUCCESS) {
|
2014-05-23 05:56:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2015-05-24 15:16:56 +00:00
|
|
|
tag_stream->write_1bytes(SrsCodecFlvTagVideo);
|
2014-05-23 05:56:40 +00:00
|
|
|
tag_stream->write_3bytes(size);
|
2014-11-19 08:16:04 +00:00
|
|
|
tag_stream->write_3bytes((int32_t)timestamp);
|
2014-05-23 05:56:40 +00:00
|
|
|
// 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-05-29 04:09:26 +00:00
|
|
|
int SrsFlvEncoder::size_tag(int data_size)
|
|
|
|
{
|
2014-07-05 09:30:13 +00:00
|
|
|
srs_assert(data_size >= 0);
|
2014-05-29 04:09:26 +00:00
|
|
|
return SRS_FLV_TAG_HEADER_SIZE + data_size + SRS_FLV_PREVIOUS_TAG_SIZE;
|
|
|
|
}
|
|
|
|
|
2014-05-23 05:56:40 +00:00
|
|
|
int SrsFlvEncoder::write_tag(char* header, int header_size, char* tag, int tag_size)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
// PreviousTagSizeN UI32 Size of last tag, including its header, in bytes.
|
2015-03-23 13:19:04 +00:00
|
|
|
char pre_size[SRS_FLV_PREVIOUS_TAG_SIZE];
|
2014-05-26 05:57:08 +00:00
|
|
|
if ((ret = tag_stream->initialize(pre_size, SRS_FLV_PREVIOUS_TAG_SIZE)) != ERROR_SUCCESS) {
|
2014-05-23 05:56:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
tag_stream->write_4bytes(tag_size + header_size);
|
2015-05-24 14:43:02 +00:00
|
|
|
|
|
|
|
iovec iovs[3];
|
|
|
|
iovs[0].iov_base = header;
|
|
|
|
iovs[0].iov_len = header_size;
|
|
|
|
iovs[1].iov_base = tag;
|
|
|
|
iovs[1].iov_len = tag_size;
|
|
|
|
iovs[2].iov_base = pre_size;
|
|
|
|
iovs[2].iov_len = sizeof(SRS_FLV_PREVIOUS_TAG_SIZE);
|
|
|
|
|
|
|
|
if ((ret = _fs->writev(iovs, 3, NULL)) != ERROR_SUCCESS) {
|
2015-05-24 07:19:09 +00:00
|
|
|
if (!srs_is_client_gracefully_close(ret)) {
|
2015-05-24 14:43:02 +00:00
|
|
|
srs_error("write flv tag failed. ret=%d", ret);
|
2015-05-24 07:19:09 +00:00
|
|
|
}
|
2014-05-23 05:56:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-04 23:40:55 +00:00
|
|
|
SrsFlvDecoder::SrsFlvDecoder()
|
|
|
|
{
|
|
|
|
_fs = NULL;
|
|
|
|
tag_stream = new SrsStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsFlvDecoder::~SrsFlvDecoder()
|
|
|
|
{
|
|
|
|
srs_freep(tag_stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsFlvDecoder::initialize(SrsFileReader* fs)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-07-05 09:30:13 +00:00
|
|
|
srs_assert(fs);
|
|
|
|
|
2014-07-05 03:10:42 +00:00
|
|
|
if (!fs->is_open()) {
|
|
|
|
ret = ERROR_KERNEL_FLV_STREAM_CLOSED;
|
|
|
|
srs_warn("stream is not open for decoder. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-04 23:40:55 +00:00
|
|
|
_fs = fs;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsFlvDecoder::read_header(char header[9])
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-07-05 09:30:13 +00:00
|
|
|
srs_assert(header);
|
|
|
|
|
2014-07-04 23:40:55 +00:00
|
|
|
if ((ret = _fs->read(header, 9, NULL)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* h = header;
|
|
|
|
if (h[0] != 'F' || h[1] != 'L' || h[2] != 'V') {
|
2014-07-05 03:10:42 +00:00
|
|
|
ret = ERROR_KERNEL_FLV_HEADER;
|
2014-07-04 23:40:55 +00:00
|
|
|
srs_warn("flv header must start with FLV. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsFlvDecoder::read_tag_header(char* ptype, int32_t* pdata_size, u_int32_t* ptime)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-07-05 09:30:13 +00:00
|
|
|
srs_assert(ptype);
|
|
|
|
srs_assert(pdata_size);
|
|
|
|
srs_assert(ptime);
|
|
|
|
|
2014-07-04 23:40:55 +00:00
|
|
|
char th[11]; // tag header
|
|
|
|
|
|
|
|
// read tag header
|
|
|
|
if ((ret = _fs->read(th, 11, NULL)) != ERROR_SUCCESS) {
|
|
|
|
if (ret != ERROR_SYSTEM_FILE_EOF) {
|
|
|
|
srs_error("read flv tag header failed. ret=%d", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reserved UB [2]
|
|
|
|
// Filter UB [1]
|
|
|
|
// TagType UB [5]
|
2014-09-26 08:34:13 +00:00
|
|
|
*ptype = (th[0] & 0x1F);
|
2014-07-04 23:40:55 +00:00
|
|
|
|
|
|
|
// DataSize UI24
|
|
|
|
char* pp = (char*)pdata_size;
|
2015-01-02 01:05:16 +00:00
|
|
|
pp[3] = 0;
|
2014-07-04 23:40:55 +00:00
|
|
|
pp[2] = th[1];
|
|
|
|
pp[1] = th[2];
|
|
|
|
pp[0] = th[3];
|
|
|
|
|
|
|
|
// Timestamp UI24
|
|
|
|
pp = (char*)ptime;
|
|
|
|
pp[2] = th[4];
|
|
|
|
pp[1] = th[5];
|
|
|
|
pp[0] = th[6];
|
|
|
|
|
|
|
|
// TimestampExtended UI8
|
|
|
|
pp[3] = th[7];
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsFlvDecoder::read_tag_data(char* data, int32_t size)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
2014-07-05 09:30:13 +00:00
|
|
|
|
|
|
|
srs_assert(data);
|
2014-07-04 23:40:55 +00:00
|
|
|
|
|
|
|
if ((ret = _fs->read(data, size, NULL)) != ERROR_SUCCESS) {
|
|
|
|
if (ret != ERROR_SYSTEM_FILE_EOF) {
|
|
|
|
srs_error("read flv tag header failed. ret=%d", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-05 09:30:13 +00:00
|
|
|
int SrsFlvDecoder::read_previous_tag_size(char previous_tag_size[4])
|
2014-07-04 23:40:55 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
2014-07-05 09:30:13 +00:00
|
|
|
|
|
|
|
srs_assert(previous_tag_size);
|
2014-07-04 23:40:55 +00:00
|
|
|
|
|
|
|
// ignore 4bytes tag size.
|
2014-07-05 09:30:13 +00:00
|
|
|
if ((ret = _fs->read(previous_tag_size, 4, NULL)) != ERROR_SUCCESS) {
|
2014-07-04 23:40:55 +00:00
|
|
|
if (ret != ERROR_SYSTEM_FILE_EOF) {
|
|
|
|
srs_error("read flv previous tag size failed. ret=%d", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsFlvVodStreamDecoder::SrsFlvVodStreamDecoder()
|
2014-05-26 05:57:08 +00:00
|
|
|
{
|
|
|
|
_fs = NULL;
|
|
|
|
tag_stream = new SrsStream();
|
|
|
|
}
|
|
|
|
|
2014-07-04 23:40:55 +00:00
|
|
|
SrsFlvVodStreamDecoder::~SrsFlvVodStreamDecoder()
|
2014-05-26 05:57:08 +00:00
|
|
|
{
|
|
|
|
srs_freep(tag_stream);
|
|
|
|
}
|
|
|
|
|
2014-07-04 23:40:55 +00:00
|
|
|
int SrsFlvVodStreamDecoder::initialize(SrsFileReader* fs)
|
2014-05-26 05:57:08 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-07-05 09:30:13 +00:00
|
|
|
srs_assert(fs);
|
|
|
|
|
2014-07-05 03:10:42 +00:00
|
|
|
if (!fs->is_open()) {
|
|
|
|
ret = ERROR_KERNEL_FLV_STREAM_CLOSED;
|
|
|
|
srs_warn("stream is not open for decoder. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-26 05:57:08 +00:00
|
|
|
_fs = fs;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-05 03:10:42 +00:00
|
|
|
int SrsFlvVodStreamDecoder::read_header_ext(char header[13])
|
2014-05-26 05:57:08 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-07-05 09:30:13 +00:00
|
|
|
srs_assert(header);
|
2014-05-26 05:57:08 +00:00
|
|
|
|
2014-07-05 03:10:42 +00:00
|
|
|
// @remark, always false, for sizeof(char[13]) equals to sizeof(char*)
|
|
|
|
//srs_assert(13 == sizeof(header));
|
|
|
|
|
2014-05-26 05:57:08 +00:00
|
|
|
// 9bytes header and 4bytes first previous-tag-size
|
|
|
|
int size = 13;
|
|
|
|
|
2014-07-05 03:10:42 +00:00
|
|
|
if ((ret = _fs->read(header, size, NULL)) != ERROR_SUCCESS) {
|
2014-05-26 05:57:08 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-05 03:10:42 +00:00
|
|
|
int SrsFlvVodStreamDecoder::read_sequence_header_summary(int64_t* pstart, int* psize)
|
2014-05-26 05:57:08 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-07-05 09:30:13 +00:00
|
|
|
srs_assert(pstart);
|
|
|
|
srs_assert(psize);
|
2014-05-26 05:57:08 +00:00
|
|
|
|
|
|
|
// simply, the first video/audio must be the sequence header.
|
|
|
|
// and must be a sequence video and audio.
|
|
|
|
|
|
|
|
// 11bytes tag header
|
2015-03-23 13:19:04 +00:00
|
|
|
char tag_header[] = {
|
2014-05-26 05:57:08 +00:00
|
|
|
(char)0x00, // TagType UB [5], 9 = video, 8 = audio, 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.
|
|
|
|
};
|
|
|
|
|
|
|
|
// discovery the sequence header video and audio.
|
|
|
|
// @remark, maybe no video or no audio.
|
|
|
|
bool got_video = false;
|
|
|
|
bool got_audio = false;
|
|
|
|
// audio/video sequence and data offset.
|
|
|
|
int64_t av_sequence_offset_start = -1;
|
|
|
|
int64_t av_sequence_offset_end = -1;
|
|
|
|
for (;;) {
|
|
|
|
if ((ret = _fs->read(tag_header, SRS_FLV_TAG_HEADER_SIZE, NULL)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = tag_stream->initialize(tag_header, SRS_FLV_TAG_HEADER_SIZE)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t tag_type = tag_stream->read_1bytes();
|
|
|
|
int32_t data_size = tag_stream->read_3bytes();
|
|
|
|
|
|
|
|
bool is_video = tag_type == 0x09;
|
|
|
|
bool is_audio = tag_type == 0x08;
|
|
|
|
bool is_not_av = !is_video && !is_audio;
|
|
|
|
if (is_not_av) {
|
|
|
|
// skip body and tag size.
|
|
|
|
_fs->skip(data_size + SRS_FLV_PREVIOUS_TAG_SIZE);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if video duplicated, no audio
|
|
|
|
if (is_video && got_video) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// if audio duplicated, no video
|
|
|
|
if (is_audio && got_audio) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// video
|
|
|
|
if (is_video) {
|
|
|
|
srs_assert(!got_video);
|
|
|
|
got_video = true;
|
|
|
|
|
|
|
|
if (av_sequence_offset_start < 0) {
|
|
|
|
av_sequence_offset_start = _fs->tellg() - SRS_FLV_TAG_HEADER_SIZE;
|
|
|
|
}
|
|
|
|
av_sequence_offset_end = _fs->tellg() + data_size + SRS_FLV_PREVIOUS_TAG_SIZE;
|
|
|
|
_fs->skip(data_size + SRS_FLV_PREVIOUS_TAG_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// audio
|
|
|
|
if (is_audio) {
|
|
|
|
srs_assert(!got_audio);
|
|
|
|
got_audio = true;
|
|
|
|
|
|
|
|
if (av_sequence_offset_start < 0) {
|
|
|
|
av_sequence_offset_start = _fs->tellg() - SRS_FLV_TAG_HEADER_SIZE;
|
|
|
|
}
|
|
|
|
av_sequence_offset_end = _fs->tellg() + data_size + SRS_FLV_PREVIOUS_TAG_SIZE;
|
|
|
|
_fs->skip(data_size + SRS_FLV_PREVIOUS_TAG_SIZE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// seek to the sequence header start offset.
|
|
|
|
if (av_sequence_offset_start > 0) {
|
|
|
|
_fs->lseek(av_sequence_offset_start);
|
|
|
|
*pstart = av_sequence_offset_start;
|
|
|
|
*psize = (int)(av_sequence_offset_end - av_sequence_offset_start);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-04 23:40:55 +00:00
|
|
|
int SrsFlvVodStreamDecoder::lseek(int64_t offset)
|
2014-05-26 05:57:08 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
if (offset >= _fs->filesize()) {
|
|
|
|
ret = ERROR_SYSTEM_FILE_EOF;
|
|
|
|
srs_warn("flv fast decoder seek overflow file, "
|
|
|
|
"size=%"PRId64", offset=%"PRId64", ret=%d",
|
|
|
|
_fs->filesize(), offset, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_fs->lseek(offset) < 0) {
|
|
|
|
ret = ERROR_SYSTEM_FILE_SEEK;
|
|
|
|
srs_warn("flv fast decoder seek error, "
|
|
|
|
"size=%"PRId64", offset=%"PRId64", ret=%d",
|
|
|
|
_fs->filesize(), offset, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2014-05-28 07:37:06 +00:00
|
|
|
|
2014-08-02 14:18:39 +00:00
|
|
|
|