From a888e52df150f61f82431dcf21a7d3624f63d8b5 Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 29 Jun 2014 17:17:50 +0800 Subject: [PATCH] donot mix the read and write for librtmp --- trunk/research/librtmp/srs_flv_injecter.c | 4 +-- trunk/research/librtmp/srs_flv_parser.c | 2 +- trunk/research/librtmp/srs_ingest_flv.c | 2 +- trunk/src/kernel/srs_kernel_error.hpp | 1 + trunk/src/kernel/srs_kernel_flv.cpp | 5 +++ trunk/src/kernel/srs_kernel_flv.hpp | 1 + trunk/src/libs/srs_librtmp.cpp | 44 ++++++++++++++++++++--- trunk/src/libs/srs_librtmp.hpp | 3 +- 8 files changed, 52 insertions(+), 10 deletions(-) diff --git a/trunk/research/librtmp/srs_flv_injecter.c b/trunk/research/librtmp/srs_flv_injecter.c index f2138ba9b..85a813647 100644 --- a/trunk/research/librtmp/srs_flv_injecter.c +++ b/trunk/research/librtmp/srs_flv_injecter.c @@ -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(in_flv_file)) == NULL) { + if ((ic = srs_flv_open_read(in_flv_file)) == NULL) { ret = 2; trace("open input flv file failed. ret=%d", ret); return ret; } *pic = ic; - if ((oc = srs_flv_open(out_flv_file)) == NULL) { + if ((oc = srs_flv_open_write(out_flv_file)) == NULL) { ret = 2; trace("open output flv file failed. ret=%d", ret); return ret; diff --git a/trunk/research/librtmp/srs_flv_parser.c b/trunk/research/librtmp/srs_flv_parser.c index 1b5804117..638257eb6 100644 --- a/trunk/research/librtmp/srs_flv_parser.c +++ b/trunk/research/librtmp/srs_flv_parser.c @@ -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(in_flv_file)) == NULL) { + if ((flv = srs_flv_open_read(in_flv_file)) == NULL) { ret = 2; trace("open flv file failed. ret=%d", ret); return ret; diff --git a/trunk/research/librtmp/srs_ingest_flv.c b/trunk/research/librtmp/srs_ingest_flv.c index 562a11add..04f588b34 100644 --- a/trunk/research/librtmp/srs_ingest_flv.c +++ b/trunk/research/librtmp/srs_ingest_flv.c @@ -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(in_flv_file)) == NULL) { + if ((flv = srs_flv_open_read(in_flv_file)) == NULL) { ret = 2; trace("open flv file failed. ret=%d", ret); return ret; diff --git a/trunk/src/kernel/srs_kernel_error.hpp b/trunk/src/kernel/srs_kernel_error.hpp index cd57899d2..8e6c0ad6e 100644 --- a/trunk/src/kernel/srs_kernel_error.hpp +++ b/trunk/src/kernel/srs_kernel_error.hpp @@ -120,6 +120,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define ERROR_SYSTEM_CREATE_PIPE 430 #define ERROR_SYSTEM_FILE_SEEK 431 #define ERROR_SYSTEM_FLV_HEADER 432 +#define ERROR_SYSTEM_IO_INVALID 433 // see librtmp. // failed when open ssl create the dh diff --git a/trunk/src/kernel/srs_kernel_flv.cpp b/trunk/src/kernel/srs_kernel_flv.cpp index 0dee3830a..2757afc58 100644 --- a/trunk/src/kernel/srs_kernel_flv.cpp +++ b/trunk/src/kernel/srs_kernel_flv.cpp @@ -165,6 +165,11 @@ void SrsFileReader::close() return; } +bool SrsFileReader::is_open() +{ + return fd > 0; +} + int64_t SrsFileReader::tellg() { return (int64_t)::lseek(fd, 0, SEEK_CUR); diff --git a/trunk/src/kernel/srs_kernel_flv.hpp b/trunk/src/kernel/srs_kernel_flv.hpp index 35b049e8d..e982bc968 100644 --- a/trunk/src/kernel/srs_kernel_flv.hpp +++ b/trunk/src/kernel/srs_kernel_flv.hpp @@ -69,6 +69,7 @@ public: virtual int open(std::string file); virtual void close(); public: + virtual bool is_open(); virtual int64_t tellg(); virtual void skip(int64_t size); virtual int64_t lseek(int64_t offset); diff --git a/trunk/src/libs/srs_librtmp.cpp b/trunk/src/libs/srs_librtmp.cpp index 7a60183b5..14ce1bf40 100644 --- a/trunk/src/libs/srs_librtmp.cpp +++ b/trunk/src/libs/srs_librtmp.cpp @@ -467,28 +467,37 @@ struct FlvContext SrsFlvDecoder dec; }; -srs_flv_t srs_flv_open(const char* file) +srs_flv_t srs_flv_open_read(const char* file) { int ret = ERROR_SUCCESS; FlvContext* flv = new FlvContext(); - if ((ret = flv->writer.open(file)) != ERROR_SUCCESS) { + if ((ret = flv->reader.open(file)) != ERROR_SUCCESS) { srs_freep(flv); return NULL; } - if ((ret = flv->enc.initialize(&flv->writer)) != ERROR_SUCCESS) { + if ((ret = flv->dec.initialize(&flv->reader)) != ERROR_SUCCESS) { srs_freep(flv); return NULL; } - if ((ret = flv->reader.open(file)) != ERROR_SUCCESS) { + return flv; +} + +srs_flv_t srs_flv_open_write(const char* file) +{ + int ret = ERROR_SUCCESS; + + FlvContext* flv = new FlvContext(); + + if ((ret = flv->writer.open(file)) != ERROR_SUCCESS) { srs_freep(flv); return NULL; } - if ((ret = flv->dec.initialize(&flv->reader)) != ERROR_SUCCESS) { + if ((ret = flv->enc.initialize(&flv->writer)) != ERROR_SUCCESS) { srs_freep(flv); return NULL; } @@ -507,6 +516,11 @@ int srs_flv_read_header(srs_flv_t flv, char header[9]) int ret = ERROR_SUCCESS; FlvContext* context = (FlvContext*)flv; + + if (!context->reader.is_open()) { + return ERROR_SYSTEM_IO_INVALID; + } + if ((ret = context->dec.read_header(header)) != ERROR_SUCCESS) { return ret; } @@ -524,6 +538,11 @@ int srs_flv_read_tag_header(srs_flv_t flv, char* ptype, int32_t* pdata_size, u_i int ret = ERROR_SUCCESS; FlvContext* context = (FlvContext*)flv; + + if (!context->reader.is_open()) { + return ERROR_SYSTEM_IO_INVALID; + } + if ((ret = context->dec.read_tag_header(ptype, pdata_size, ptime)) != ERROR_SUCCESS) { return ret; } @@ -536,6 +555,11 @@ int srs_flv_read_tag_data(srs_flv_t flv, char* data, int32_t size) int ret = ERROR_SUCCESS; FlvContext* context = (FlvContext*)flv; + + if (!context->reader.is_open()) { + return ERROR_SYSTEM_IO_INVALID; + } + if ((ret = context->dec.read_tag_data(data, size)) != ERROR_SUCCESS) { return ret; } @@ -553,6 +577,11 @@ int srs_flv_write_header(srs_flv_t flv, char header[9]) int ret = ERROR_SUCCESS; FlvContext* context = (FlvContext*)flv; + + if (!context->writer.is_open()) { + return ERROR_SYSTEM_IO_INVALID; + } + if ((ret = context->enc.write_header(header)) != ERROR_SUCCESS) { return ret; } @@ -565,6 +594,11 @@ int srs_flv_write_tag(srs_flv_t flv, char type, int32_t time, char* data, int si int ret = ERROR_SUCCESS; FlvContext* context = (FlvContext*)flv; + + if (!context->writer.is_open()) { + return ERROR_SYSTEM_IO_INVALID; + } + if (type == SRS_RTMP_TYPE_AUDIO) { return context->enc.write_audio(time, data, size); } else if (type == SRS_RTMP_TYPE_VIDEO) { diff --git a/trunk/src/libs/srs_librtmp.hpp b/trunk/src/libs/srs_librtmp.hpp index ae87de6e9..d5b0f9c7f 100644 --- a/trunk/src/libs/srs_librtmp.hpp +++ b/trunk/src/libs/srs_librtmp.hpp @@ -166,7 +166,8 @@ int64_t srs_get_nrecv_bytes(srs_rtmp_t rtmp); typedef void* srs_flv_t; typedef int flv_bool; /* open flv file for both read/write. */ -srs_flv_t srs_flv_open(const char* file); +srs_flv_t srs_flv_open_read(const char* file); +srs_flv_t srs_flv_open_write(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]);