mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
refine the file stream, to file reader and writer. 0.9.142
This commit is contained in:
parent
ed3f9f0a40
commit
1a7735182f
13 changed files with 167 additions and 121 deletions
|
@ -121,14 +121,14 @@ int process(const char* in_flv_file, const char* out_flv_file, srs_flv_t* pic, s
|
|||
srs_amf0_t amf0_data = NULL;
|
||||
srs_amf0_t filepositions = NULL;
|
||||
|
||||
if ((ic = srs_flv_open_read(in_flv_file)) == NULL) {
|
||||
if ((ic = srs_flv_open(in_flv_file)) == NULL) {
|
||||
ret = 2;
|
||||
trace("open input flv file failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
*pic = ic;
|
||||
|
||||
if ((oc = srs_flv_open_write(out_flv_file)) == NULL) {
|
||||
if ((oc = srs_flv_open(out_flv_file)) == NULL) {
|
||||
ret = 2;
|
||||
trace("open output flv file failed. ret=%d", ret);
|
||||
return ret;
|
||||
|
|
|
@ -64,7 +64,7 @@ int main(int argc, char** argv)
|
|||
trace("version: %d.%d.%d", srs_version_major(), srs_version_minor(), srs_version_revision());
|
||||
trace("input: %s", in_flv_file);
|
||||
|
||||
if ((flv = srs_flv_open_read(in_flv_file)) == NULL) {
|
||||
if ((flv = srs_flv_open(in_flv_file)) == NULL) {
|
||||
ret = 2;
|
||||
trace("open flv file failed. ret=%d", ret);
|
||||
return ret;
|
||||
|
|
|
@ -93,7 +93,7 @@ int main(int argc, char** argv)
|
|||
trace("input: %s", in_flv_file);
|
||||
trace("output: %s", out_rtmp_url);
|
||||
|
||||
if ((flv = srs_flv_open_read(in_flv_file)) == NULL) {
|
||||
if ((flv = srs_flv_open(in_flv_file)) == NULL) {
|
||||
ret = 2;
|
||||
trace("open flv file failed. ret=%d", ret);
|
||||
return ret;
|
||||
|
|
|
@ -71,7 +71,7 @@ SrsDvrPlan::SrsDvrPlan()
|
|||
_req = NULL;
|
||||
jitter = NULL;
|
||||
dvr_enabled = false;
|
||||
fs = new SrsFileStream();
|
||||
fs = new SrsFileWriter();
|
||||
enc = new SrsFlvEncoder();
|
||||
segment = new SrsFlvSegment();
|
||||
jitter_algorithm = SrsRtmpJitterAlgorithmOFF;
|
||||
|
@ -283,7 +283,7 @@ int SrsDvrPlan::flv_open(string stream, string path)
|
|||
segment->reset();
|
||||
|
||||
std::string tmp_file = path + ".tmp";
|
||||
if ((ret = fs->open_write(tmp_file)) != ERROR_SUCCESS) {
|
||||
if ((ret = fs->open(tmp_file)) != ERROR_SUCCESS) {
|
||||
srs_error("open file stream for file %s failed. ret=%d", path.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
@ -569,8 +569,8 @@ int SrsDvrHssPlan::on_meta_data(SrsOnMetaDataPacket* metadata)
|
|||
<< "/" << req->app << "/"
|
||||
<< req->stream << ".header.flv";
|
||||
|
||||
SrsFileStream fs;
|
||||
if ((ret = fs.open_write(path.str().c_str())) != ERROR_SUCCESS) {
|
||||
SrsFileWriter fs;
|
||||
if ((ret = fs.open(path.str().c_str())) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ class SrsStream;
|
|||
class SrsRtmpJitter;
|
||||
class SrsOnMetaDataPacket;
|
||||
class SrsSharedPtrMessage;
|
||||
class SrsFileStream;
|
||||
class SrsFileWriter;
|
||||
class SrsFlvEncoder;
|
||||
|
||||
#include <srs_app_source.hpp>
|
||||
|
@ -114,7 +114,7 @@ protected:
|
|||
SrsFlvSegment* segment;
|
||||
SrsRequest* _req;
|
||||
bool dvr_enabled;
|
||||
SrsFileStream* fs;
|
||||
SrsFileWriter* fs;
|
||||
public:
|
||||
SrsDvrPlan();
|
||||
virtual ~SrsDvrPlan();
|
||||
|
|
|
@ -300,10 +300,10 @@ int SrsHttpVhost::response_flv_file2(SrsSocket* skt, SrsHttpMessage* req, string
|
|||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsFileStream fs;
|
||||
SrsFileReader fs;
|
||||
|
||||
// open flv file
|
||||
if ((ret = fs.open_read(fullpath)) != ERROR_SUCCESS) {
|
||||
if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
// current release version
|
||||
#define VERSION_MAJOR "0"
|
||||
#define VERSION_MINOR "9"
|
||||
#define VERSION_REVISION "141"
|
||||
#define VERSION_REVISION "142"
|
||||
#define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
|
||||
// server info.
|
||||
#define RTMP_SIG_SRS_KEY "SRS"
|
||||
|
|
|
@ -37,17 +37,16 @@ using namespace std;
|
|||
#define SRS_FLV_TAG_HEADER_SIZE 11
|
||||
#define SRS_FLV_PREVIOUS_TAG_SIZE 4
|
||||
|
||||
SrsFileStream::SrsFileStream()
|
||||
SrsFileWriter::SrsFileWriter()
|
||||
{
|
||||
fd = -1;
|
||||
}
|
||||
|
||||
SrsFileStream::~SrsFileStream()
|
||||
SrsFileWriter::~SrsFileWriter()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
int SrsFileStream::open_write(string file)
|
||||
int SrsFileWriter::open(string file)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
|
@ -71,7 +70,63 @@ int SrsFileStream::open_write(string file)
|
|||
return ret;
|
||||
}
|
||||
|
||||
int SrsFileStream::open_read(string file)
|
||||
void SrsFileWriter::close()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (fd < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (::close(fd) < 0) {
|
||||
ret = ERROR_SYSTEM_FILE_CLOSE;
|
||||
srs_error("close file %s failed. ret=%d", _file.c_str(), ret);
|
||||
return;
|
||||
}
|
||||
fd = -1;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool SrsFileWriter::is_open()
|
||||
{
|
||||
return fd > 0;
|
||||
}
|
||||
|
||||
int64_t SrsFileWriter::tellg()
|
||||
{
|
||||
return (int64_t)::lseek(fd, 0, SEEK_CUR);
|
||||
}
|
||||
|
||||
int SrsFileWriter::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;
|
||||
}
|
||||
|
||||
SrsFileReader::SrsFileReader()
|
||||
{
|
||||
fd = -1;
|
||||
}
|
||||
|
||||
SrsFileReader::~SrsFileReader()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
int SrsFileReader::open(string file)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
|
@ -92,7 +147,7 @@ int SrsFileStream::open_read(string file)
|
|||
return ret;
|
||||
}
|
||||
|
||||
void SrsFileStream::close()
|
||||
void SrsFileReader::close()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
|
@ -110,12 +165,30 @@ void SrsFileStream::close()
|
|||
return;
|
||||
}
|
||||
|
||||
bool SrsFileStream::is_open()
|
||||
int64_t SrsFileReader::tellg()
|
||||
{
|
||||
return fd > 0;
|
||||
return (int64_t)::lseek(fd, 0, SEEK_CUR);
|
||||
}
|
||||
|
||||
int SrsFileStream::read(void* buf, size_t count, ssize_t* pnread)
|
||||
void SrsFileReader::skip(int64_t size)
|
||||
{
|
||||
::lseek(fd, size, SEEK_CUR);
|
||||
}
|
||||
|
||||
int64_t SrsFileReader::lseek(int64_t offset)
|
||||
{
|
||||
return (int64_t)::lseek(fd, offset, SEEK_SET);
|
||||
}
|
||||
|
||||
int64_t SrsFileReader::filesize()
|
||||
{
|
||||
int64_t cur = tellg();
|
||||
int64_t size = (int64_t)::lseek(fd, 0, SEEK_END);
|
||||
::lseek(fd, cur, SEEK_SET);
|
||||
return size;
|
||||
}
|
||||
|
||||
int SrsFileReader::read(void* buf, size_t count, ssize_t* pnread)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
|
@ -138,47 +211,6 @@ int SrsFileStream::read(void* buf, size_t count, ssize_t* pnread)
|
|||
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;
|
||||
}
|
||||
|
||||
int64_t SrsFileStream::tellg()
|
||||
{
|
||||
return (int64_t)::lseek(fd, 0, SEEK_CUR);
|
||||
}
|
||||
|
||||
int64_t SrsFileStream::lseek(int64_t offset)
|
||||
{
|
||||
return (int64_t)::lseek(fd, offset, SEEK_SET);
|
||||
}
|
||||
|
||||
int64_t SrsFileStream::filesize()
|
||||
{
|
||||
int64_t cur = tellg();
|
||||
int64_t size = (int64_t)::lseek(fd, 0, SEEK_END);
|
||||
::lseek(fd, cur, SEEK_SET);
|
||||
return size;
|
||||
}
|
||||
|
||||
void SrsFileStream::skip(int64_t size)
|
||||
{
|
||||
::lseek(fd, size, SEEK_CUR);
|
||||
}
|
||||
|
||||
SrsFlvEncoder::SrsFlvEncoder()
|
||||
{
|
||||
_fs = NULL;
|
||||
|
@ -190,7 +222,7 @@ SrsFlvEncoder::~SrsFlvEncoder()
|
|||
srs_freep(tag_stream);
|
||||
}
|
||||
|
||||
int SrsFlvEncoder::initialize(SrsFileStream* fs)
|
||||
int SrsFlvEncoder::initialize(SrsFileWriter* fs)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
|
@ -377,7 +409,7 @@ SrsFlvFastDecoder::~SrsFlvFastDecoder()
|
|||
srs_freep(tag_stream);
|
||||
}
|
||||
|
||||
int SrsFlvFastDecoder::initialize(SrsFileStream* fs)
|
||||
int SrsFlvFastDecoder::initialize(SrsFileReader* fs)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
|
@ -538,7 +570,7 @@ SrsFlvDecoder::~SrsFlvDecoder()
|
|||
srs_freep(tag_stream);
|
||||
}
|
||||
|
||||
int SrsFlvDecoder::initialize(SrsFileStream* fs)
|
||||
int SrsFlvDecoder::initialize(SrsFileReader* fs)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
|
|
|
@ -34,37 +34,47 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
class SrsStream;
|
||||
|
||||
/**
|
||||
* file stream to read/write file.
|
||||
* file writer, to write to file.
|
||||
*/
|
||||
class SrsFileStream
|
||||
class SrsFileWriter
|
||||
{
|
||||
private:
|
||||
std::string _file;
|
||||
int fd;
|
||||
public:
|
||||
SrsFileStream();
|
||||
virtual ~SrsFileStream();
|
||||
SrsFileWriter();
|
||||
virtual ~SrsFileWriter();
|
||||
public:
|
||||
virtual int open_write(std::string file);
|
||||
virtual int open_read(std::string file);
|
||||
virtual int open(std::string file);
|
||||
virtual void close();
|
||||
virtual bool is_open();
|
||||
public:
|
||||
/**
|
||||
* @param pnread, return the read size. NULL to ignore.
|
||||
*/
|
||||
virtual int read(void* buf, size_t count, ssize_t* pnread);
|
||||
/**
|
||||
* @param pnwrite, return the write size. NULL to ignore.
|
||||
*/
|
||||
virtual int write(void* buf, size_t count, ssize_t* pnwrite);
|
||||
/**
|
||||
* tell current offset of stream.
|
||||
*/
|
||||
virtual bool is_open();
|
||||
virtual int64_t tellg();
|
||||
public:
|
||||
virtual int write(void* buf, size_t count, ssize_t* pnwrite);
|
||||
};
|
||||
|
||||
/**
|
||||
* file reader, to read from file.
|
||||
*/
|
||||
class SrsFileReader
|
||||
{
|
||||
private:
|
||||
std::string _file;
|
||||
int fd;
|
||||
public:
|
||||
SrsFileReader();
|
||||
virtual ~SrsFileReader();
|
||||
public:
|
||||
virtual int open(std::string file);
|
||||
virtual void close();
|
||||
public:
|
||||
virtual int64_t tellg();
|
||||
virtual void skip(int64_t size);
|
||||
virtual int64_t lseek(int64_t offset);
|
||||
virtual int64_t filesize();
|
||||
virtual void skip(int64_t size);
|
||||
public:
|
||||
virtual int read(void* buf, size_t count, ssize_t* pnread);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -73,7 +83,7 @@ public:
|
|||
class SrsFlvEncoder
|
||||
{
|
||||
private:
|
||||
SrsFileStream* _fs;
|
||||
SrsFileWriter* _fs;
|
||||
private:
|
||||
SrsStream* tag_stream;
|
||||
public:
|
||||
|
@ -84,7 +94,7 @@ public:
|
|||
* initialize the underlayer file stream,
|
||||
* user can initialize multiple times to encode multiple flv files.
|
||||
*/
|
||||
virtual int initialize(SrsFileStream* fs);
|
||||
virtual int initialize(SrsFileWriter* fs);
|
||||
public:
|
||||
/**
|
||||
* write flv header.
|
||||
|
@ -122,7 +132,7 @@ private:
|
|||
class SrsFlvFastDecoder
|
||||
{
|
||||
private:
|
||||
SrsFileStream* _fs;
|
||||
SrsFileReader* _fs;
|
||||
private:
|
||||
SrsStream* tag_stream;
|
||||
public:
|
||||
|
@ -133,7 +143,7 @@ public:
|
|||
* initialize the underlayer file stream,
|
||||
* user can initialize multiple times to encode multiple flv files.
|
||||
*/
|
||||
virtual int initialize(SrsFileStream* fs);
|
||||
virtual int initialize(SrsFileReader* fs);
|
||||
public:
|
||||
/**
|
||||
* read the flv header and size.
|
||||
|
@ -156,7 +166,7 @@ public:
|
|||
class SrsFlvDecoder
|
||||
{
|
||||
private:
|
||||
SrsFileStream* _fs;
|
||||
SrsFileReader* _fs;
|
||||
private:
|
||||
SrsStream* tag_stream;
|
||||
public:
|
||||
|
@ -167,7 +177,7 @@ public:
|
|||
* initialize the underlayer file stream,
|
||||
* user can initialize multiple times to decode multiple flv files.
|
||||
*/
|
||||
virtual int initialize(SrsFileStream* fs);
|
||||
virtual int initialize(SrsFileReader* fs);
|
||||
public:
|
||||
virtual int read_header(char header[9]);
|
||||
virtual int read_tag_header(char* ptype, int32_t* pdata_size, u_int32_t* ptime);
|
||||
|
|
|
@ -461,52 +461,34 @@ int64_t srs_get_nrecv_bytes(srs_rtmp_t rtmp)
|
|||
|
||||
struct FlvContext
|
||||
{
|
||||
SrsFileStream fs;
|
||||
SrsFileReader reader;
|
||||
SrsFileWriter writer;
|
||||
SrsFlvEncoder enc;
|
||||
SrsFlvDecoder dec;
|
||||
};
|
||||
|
||||
srs_flv_t srs_flv_open_read(const char* file)
|
||||
srs_flv_t srs_flv_open(const char* file)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
FlvContext* flv = new FlvContext();
|
||||
|
||||
if ((ret = flv->fs.open_read(file)) != ERROR_SUCCESS) {
|
||||
if ((ret = flv->reader.open(file)) != ERROR_SUCCESS) {
|
||||
srs_freep(flv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((ret = flv->enc.initialize(&flv->fs)) != ERROR_SUCCESS) {
|
||||
if ((ret = flv->dec.initialize(&flv->reader)) != ERROR_SUCCESS) {
|
||||
srs_freep(flv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((ret = flv->dec.initialize(&flv->fs)) != ERROR_SUCCESS) {
|
||||
if ((ret = flv->writer.open(file)) != ERROR_SUCCESS) {
|
||||
srs_freep(flv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return flv;
|
||||
}
|
||||
|
||||
srs_flv_t srs_flv_open_write(const char* file)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
FlvContext* flv = new FlvContext();
|
||||
|
||||
if ((ret = flv->fs.open_write(file)) != ERROR_SUCCESS) {
|
||||
srs_freep(flv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((ret = flv->enc.initialize(&flv->fs)) != ERROR_SUCCESS) {
|
||||
srs_freep(flv);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((ret = flv->dec.initialize(&flv->fs)) != ERROR_SUCCESS) {
|
||||
if ((ret = flv->enc.initialize(&flv->writer)) != ERROR_SUCCESS) {
|
||||
srs_freep(flv);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -602,13 +584,13 @@ int srs_flv_size_tag(int data_size)
|
|||
int64_t srs_flv_tellg(srs_flv_t flv)
|
||||
{
|
||||
FlvContext* context = (FlvContext*)flv;
|
||||
return context->fs.tellg();
|
||||
return context->reader.tellg();
|
||||
}
|
||||
|
||||
void srs_flv_lseek(srs_flv_t flv, int64_t offset)
|
||||
{
|
||||
FlvContext* context = (FlvContext*)flv;
|
||||
context->fs.lseek(offset);
|
||||
context->reader.lseek(offset);
|
||||
}
|
||||
|
||||
flv_bool srs_flv_is_eof(int error_code)
|
||||
|
|
|
@ -165,8 +165,7 @@ int64_t srs_get_nrecv_bytes(srs_rtmp_t rtmp);
|
|||
*/
|
||||
typedef void* srs_flv_t;
|
||||
typedef int flv_bool;
|
||||
srs_flv_t srs_flv_open_read(const char* file);
|
||||
srs_flv_t srs_flv_open_write(const char* file);
|
||||
srs_flv_t srs_flv_open(const char* file);
|
||||
void srs_flv_close(srs_flv_t flv);
|
||||
/* read the flv header. 9bytes header. drop the 4bytes zero previous tag size */
|
||||
int srs_flv_read_header(srs_flv_t flv, char header[9]);
|
||||
|
|
|
@ -25,6 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
using namespace std;
|
||||
|
||||
#include <srs_kernel_error.hpp>
|
||||
#include <srs_kernel_codec.hpp>
|
||||
|
||||
VOID TEST(KernelCodecTest, IsKeyFrame)
|
||||
{
|
||||
|
|
|
@ -30,6 +30,28 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
#include <srs_utest.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <srs_kernel_codec.hpp>
|
||||
#include <srs_kernel_flv.hpp>
|
||||
|
||||
/*
|
||||
class MockSrsFileStream : public SrsFileStream
|
||||
{
|
||||
public:
|
||||
MockSrsFileStream();
|
||||
virtual ~MockSrsFileStream();
|
||||
public:
|
||||
public:
|
||||
virtual int open_write(std::string file);
|
||||
virtual int open_read(std::string file);
|
||||
virtual void close();
|
||||
virtual bool is_open();
|
||||
public:
|
||||
virtual int read(void* buf, size_t count, ssize_t* pnread);
|
||||
virtual int write(void* buf, size_t count, ssize_t* pnwrite);
|
||||
virtual int64_t tellg();
|
||||
virtual int64_t lseek(int64_t offset);
|
||||
virtual int64_t filesize();
|
||||
virtual void skip(int64_t size);
|
||||
};
|
||||
*/
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue