From 032118581a66ba63a5b7cf41100f573e92866cfe Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 5 Jul 2014 07:33:18 +0800 Subject: [PATCH] refine http/dvr/hls to use file reader/writer. 0.9.146 --- trunk/configure | 2 +- trunk/src/app/srs_app_dvr.cpp | 1 + trunk/src/app/srs_app_hls.cpp | 32 ++-- trunk/src/app/srs_app_hls.hpp | 3 +- trunk/src/app/srs_app_http_conn.cpp | 47 ++---- trunk/src/core/srs_core.hpp | 198 ++++++++++++------------ trunk/src/kernel/srs_kernel_error.hpp | 8 +- trunk/src/kernel/srs_kernel_file.cpp | 213 ++++++++++++++++++++++++++ trunk/src/kernel/srs_kernel_file.hpp | 93 +++++++++++ trunk/src/kernel/srs_kernel_flv.cpp | 180 +--------------------- trunk/src/kernel/srs_kernel_flv.hpp | 47 +----- trunk/src/libs/srs_librtmp.cpp | 1 + trunk/src/srs/srs.upp | 2 + trunk/src/utest/srs_utest_kernel.hpp | 2 +- 14 files changed, 447 insertions(+), 382 deletions(-) mode change 100644 => 100755 trunk/src/core/srs_core.hpp create mode 100644 trunk/src/kernel/srs_kernel_file.cpp create mode 100644 trunk/src/kernel/srs_kernel_file.hpp diff --git a/trunk/configure b/trunk/configure index 4203ac4d6..ae7eac9f6 100755 --- a/trunk/configure +++ b/trunk/configure @@ -447,7 +447,7 @@ MODULE_ID="KERNEL" MODULE_DEPENDS=("CORE") ModuleLibIncs=(${SRS_OBJS}) MODULE_FILES=("srs_kernel_error" "srs_kernel_log" "srs_kernel_stream" "srs_kernel_buffer" - "srs_kernel_utility" "srs_kernel_flv" "srs_kernel_codec") + "srs_kernel_utility" "srs_kernel_flv" "srs_kernel_codec" "srs_kernel_file") KERNEL_INCS="src/kernel"; MODULE_DIR=${KERNEL_INCS} . auto/modules.sh KERNEL_OBJS="${MODULE_OBJS[@]}" # diff --git a/trunk/src/app/srs_app_dvr.cpp b/trunk/src/app/srs_app_dvr.cpp index e97ee1751..3ca70c599 100644 --- a/trunk/src/app/srs_app_dvr.cpp +++ b/trunk/src/app/srs_app_dvr.cpp @@ -40,6 +40,7 @@ using namespace std; #include #include #include +#include SrsFlvSegment::SrsFlvSegment() { diff --git a/trunk/src/app/srs_app_hls.cpp b/trunk/src/app/srs_app_hls.cpp index c93bb0276..06c1b2e4f 100644 --- a/trunk/src/app/srs_app_hls.cpp +++ b/trunk/src/app/srs_app_hls.cpp @@ -45,6 +45,7 @@ using namespace std; #include #include #include +#include // max PES packets size to flush the video. #define SRS_AUTO_HLS_AUDIO_CACHE_SIZE 1024 * 1024 @@ -150,12 +151,11 @@ public: class SrsMpegtsWriter { public: - static int write_header(int fd) + static int write_header(SrsFileWriter* writer) { int ret = ERROR_SUCCESS; - // TODO: FIXME: maybe should use st_write. - if (::write(fd, mpegts_header, sizeof(mpegts_header)) != sizeof(mpegts_header)) { + if ((ret = writer->write(mpegts_header, sizeof(mpegts_header), NULL)) != ERROR_SUCCESS) { ret = ERROR_HLS_WRITE_FAILED; srs_error("write ts file header failed. ret=%d", ret); return ret; @@ -163,7 +163,7 @@ public: return ret; } - static int write_frame(int fd, SrsMpegtsFrame* frame, SrsCodecBuffer* buffer) + static int write_frame(SrsFileWriter* writer, SrsMpegtsFrame* frame, SrsCodecBuffer* buffer) { int ret = ERROR_SUCCESS; @@ -279,8 +279,7 @@ public: } // write ts packet - // TODO: FIXME: maybe should use st_write. - if (::write(fd, packet, sizeof(packet)) != sizeof(packet)) { + if ((ret = writer->write(packet, sizeof(packet), NULL)) != ERROR_SUCCESS) { ret = ERROR_HLS_WRITE_FAILED; srs_error("write ts file failed. ret=%d", ret); return ret; @@ -414,12 +413,13 @@ void SrsHlsAacJitter::on_buffer_continue() SrsTSMuxer::SrsTSMuxer() { - fd = -1; + writer = new SrsFileWriter(); } SrsTSMuxer::~SrsTSMuxer() { close(); + srs_freep(writer); } int SrsTSMuxer::open(string _path) @@ -430,17 +430,12 @@ int SrsTSMuxer::open(string _path) close(); - int flags = O_CREAT|O_WRONLY|O_TRUNC; - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH; - // TODO: FIXME: refine the file stream. - if ((fd = ::open(path.c_str(), flags, mode)) < 0) { - ret = ERROR_HLS_OPEN_FAILED; - srs_error("open ts file %s failed. ret=%d", path.c_str(), ret); + if ((ret = writer->open(path)) != ERROR_SUCCESS) { return ret; } // write mpegts header - if ((ret = SrsMpegtsWriter::write_header(fd)) != ERROR_SUCCESS) { + if ((ret = SrsMpegtsWriter::write_header(writer)) != ERROR_SUCCESS) { return ret; } @@ -451,7 +446,7 @@ int SrsTSMuxer::write_audio(SrsMpegtsFrame* af, SrsCodecBuffer* ab) { int ret = ERROR_SUCCESS; - if ((ret = SrsMpegtsWriter::write_frame(fd, af, ab)) != ERROR_SUCCESS) { + if ((ret = SrsMpegtsWriter::write_frame(writer, af, ab)) != ERROR_SUCCESS) { return ret; } @@ -462,7 +457,7 @@ int SrsTSMuxer::write_video(SrsMpegtsFrame* vf, SrsCodecBuffer* vb) { int ret = ERROR_SUCCESS; - if ((ret = SrsMpegtsWriter::write_frame(fd, vf, vb)) != ERROR_SUCCESS) { + if ((ret = SrsMpegtsWriter::write_frame(writer, vf, vb)) != ERROR_SUCCESS) { return ret; } @@ -471,10 +466,7 @@ int SrsTSMuxer::write_video(SrsMpegtsFrame* vf, SrsCodecBuffer* vb) void SrsTSMuxer::close() { - if (fd > 0) { - ::close(fd); - fd = -1; - } + writer->close(); } SrsHlsSegment::SrsHlsSegment() diff --git a/trunk/src/app/srs_app_hls.hpp b/trunk/src/app/srs_app_hls.hpp index fdb76364b..ec7c31643 100644 --- a/trunk/src/app/srs_app_hls.hpp +++ b/trunk/src/app/srs_app_hls.hpp @@ -45,6 +45,7 @@ class SrsAvcAacCodec; class SrsRequest; class SrsPithyPrint; class SrsSource; +class SrsFileWriter; /** * jitter correct for audio, @@ -83,7 +84,7 @@ public: class SrsTSMuxer { private: - int fd; + SrsFileWriter* writer; std::string path; public: SrsTSMuxer(); diff --git a/trunk/src/app/srs_app_http_conn.cpp b/trunk/src/app/srs_app_http_conn.cpp index d3d2faec9..ad2954663 100644 --- a/trunk/src/app/srs_app_http_conn.cpp +++ b/trunk/src/app/srs_app_http_conn.cpp @@ -42,6 +42,7 @@ using namespace std; #include #include #include +#include #define SRS_HTTP_DEFAULT_PAGE "index.html" @@ -191,28 +192,22 @@ int SrsHttpVhost::response_regular_file(SrsSocket* skt, SrsHttpMessage* req, str { int ret = ERROR_SUCCESS; - // TODO: FIXME: refine the file stream. - int fd = ::open(fullpath.c_str(), O_RDONLY); - if (fd < 0) { - ret = ERROR_HTTP_OPEN_FILE; + SrsFileReader fs; + + if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) { srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret); return ret; } - int64_t length = (int64_t)::lseek(fd, 0, SEEK_END); - ::lseek(fd, 0, SEEK_SET); + int64_t length = fs.filesize(); char* buf = new char[length]; SrsAutoFree(char, buf); - // TODO: FIXME: use st_read. - if (::read(fd, buf, length) < 0) { - ::close(fd); - ret = ERROR_HTTP_READ_FILE; + if ((ret = fs.read(buf, length, NULL)) != ERROR_SUCCESS) { srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret); return ret; } - ::close(fd); std::string str; str.append(buf, length); @@ -243,18 +238,16 @@ int SrsHttpVhost::response_regular_file(SrsSocket* skt, SrsHttpMessage* req, str int SrsHttpVhost::response_flv_file(SrsSocket* skt, SrsHttpMessage* req, string fullpath) { int ret = ERROR_SUCCESS; + + SrsFileReader fs; // TODO: FIXME: use more advance cache. - // for ts video large file, use bytes to write it. - int fd = ::open(fullpath.c_str(), O_RDONLY); - if (fd < 0) { - ret = ERROR_HTTP_OPEN_FILE; + if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) { srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret); return ret; } - int64_t length = (int64_t)::lseek(fd, 0, SEEK_END); - ::lseek(fd, 0, SEEK_SET); + int64_t length = fs.filesize(); // write http header for ts. std::stringstream ss; @@ -279,9 +272,7 @@ int SrsHttpVhost::response_flv_file(SrsSocket* skt, SrsHttpMessage* req, string while (left > 0) { ssize_t nread = -1; - // TODO: FIXME: use st_read. - if ((nread = ::read(fd, buf, HTTP_TS_SEND_BUFFER_SIZE)) < 0) { - ret = ERROR_HTTP_READ_FILE; + if ((ret = fs.read(buf, HTTP_TS_SEND_BUFFER_SIZE, &nread)) != ERROR_SUCCESS) { srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret); break; } @@ -291,7 +282,6 @@ int SrsHttpVhost::response_flv_file(SrsSocket* skt, SrsHttpMessage* req, string break; } } - ::close(fd); return ret; } @@ -406,17 +396,15 @@ int SrsHttpVhost::response_ts_file(SrsSocket* skt, SrsHttpMessage* req, string f { int ret = ERROR_SUCCESS; + SrsFileReader fs; + // TODO: FIXME: use more advance cache. - // for ts video large file, use bytes to write it. - int fd = ::open(fullpath.c_str(), O_RDONLY); - if (fd < 0) { - ret = ERROR_HTTP_OPEN_FILE; + if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) { srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret); return ret; } - int64_t length = (int64_t)::lseek(fd, 0, SEEK_END); - ::lseek(fd, 0, SEEK_SET); + int64_t length = fs.filesize(); // write http header for ts. std::stringstream ss; @@ -441,9 +429,7 @@ int SrsHttpVhost::response_ts_file(SrsSocket* skt, SrsHttpMessage* req, string f while (left > 0) { ssize_t nread = -1; - // TODO: FIXME: use st_read. - if ((nread = ::read(fd, buf, HTTP_TS_SEND_BUFFER_SIZE)) < 0) { - ret = ERROR_HTTP_READ_FILE; + if ((ret = fs.read(buf, HTTP_TS_SEND_BUFFER_SIZE, &nread)) != ERROR_SUCCESS) { srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret); break; } @@ -453,7 +439,6 @@ int SrsHttpVhost::response_ts_file(SrsSocket* skt, SrsHttpMessage* req, string f break; } } - ::close(fd); return ret; } diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp old mode 100644 new mode 100755 index 78286a414..5ac15c89b --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -1,99 +1,99 @@ -/* -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. -*/ - -#ifndef SRS_CORE_HPP -#define SRS_CORE_HPP - -/* -#include -*/ - -// current release version -#define VERSION_MAJOR "0" -#define VERSION_MINOR "9" -#define VERSION_REVISION "145" -#define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION -// server info. -#define RTMP_SIG_SRS_KEY "SRS" -#define RTMP_SIG_SRS_ROLE "origin/edge server" -#define RTMP_SIG_SRS_NAME RTMP_SIG_SRS_KEY"(Simple RTMP Server)" -#define RTMP_SIG_SRS_URL_SHORT "github.com/winlinvip/simple-rtmp-server" -#define RTMP_SIG_SRS_URL "https://"RTMP_SIG_SRS_URL_SHORT -#define RTMP_SIG_SRS_WEB "http://blog.csdn.net/win_lin" -#define RTMP_SIG_SRS_EMAIL "winlin@vip.126.com" -#define RTMP_SIG_SRS_LICENSE "The MIT License (MIT)" -#define RTMP_SIG_SRS_COPYRIGHT "Copyright (c) 2013-2014 winlin" -#define RTMP_SIG_SRS_PRIMARY_AUTHROS "winlin,wenjie.zhao" -#define RTMP_SIG_SRS_CONTRIBUTORS_URL RTMP_SIG_SRS_URL"/blob/master/AUTHORS.txt" - -/** -* the core provides the common defined macros, utilities, -* user must include the srs_core.hpp before any header, or maybe -* build failed. -*/ - -// for 32bit os, 2G big file limit for unistd io, -// ie. read/write/lseek to use 64bits size for huge file. -#ifndef _FILE_OFFSET_BITS - #define _FILE_OFFSET_BITS 64 -#endif - -// for int64_t print using PRId64 format. -#ifndef __STDC_FORMAT_MACROS - #define __STDC_FORMAT_MACROS -#endif -#include - -#include -#define srs_assert(expression) assert(expression) - -#include -#include - -// generated by configure. -#include - -// free the p and set to NULL. -// p must be a T*. -#define srs_freep(p) \ - if (p) { \ - delete p; \ - p = NULL; \ - } \ - (void)0 -// sometimes, the freepa is useless, -// it's recomments to free each elem explicit. -// so we remove the srs_freepa utility. - -/** -* disable copy constructor of class -*/ -#define disable_default_copy(className)\ - private:\ - /** \ - * disable the copy constructor and operator=, donot allow directly copy. \ - */ \ - className(const className&); \ - className& operator= (const className&) - -#endif +/* +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. +*/ + +#ifndef SRS_CORE_HPP +#define SRS_CORE_HPP + +/* +#include +*/ + +// current release version +#define VERSION_MAJOR "0" +#define VERSION_MINOR "9" +#define VERSION_REVISION "146" +#define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION +// server info. +#define RTMP_SIG_SRS_KEY "SRS" +#define RTMP_SIG_SRS_ROLE "origin/edge server" +#define RTMP_SIG_SRS_NAME RTMP_SIG_SRS_KEY"(Simple RTMP Server)" +#define RTMP_SIG_SRS_URL_SHORT "github.com/winlinvip/simple-rtmp-server" +#define RTMP_SIG_SRS_URL "https://"RTMP_SIG_SRS_URL_SHORT +#define RTMP_SIG_SRS_WEB "http://blog.csdn.net/win_lin" +#define RTMP_SIG_SRS_EMAIL "winlin@vip.126.com" +#define RTMP_SIG_SRS_LICENSE "The MIT License (MIT)" +#define RTMP_SIG_SRS_COPYRIGHT "Copyright (c) 2013-2014 winlin" +#define RTMP_SIG_SRS_PRIMARY_AUTHROS "winlin,wenjie.zhao" +#define RTMP_SIG_SRS_CONTRIBUTORS_URL RTMP_SIG_SRS_URL"/blob/master/AUTHORS.txt" + +/** +* the core provides the common defined macros, utilities, +* user must include the srs_core.hpp before any header, or maybe +* build failed. +*/ + +// for 32bit os, 2G big file limit for unistd io, +// ie. read/write/lseek to use 64bits size for huge file. +#ifndef _FILE_OFFSET_BITS + #define _FILE_OFFSET_BITS 64 +#endif + +// for int64_t print using PRId64 format. +#ifndef __STDC_FORMAT_MACROS + #define __STDC_FORMAT_MACROS +#endif +#include + +#include +#define srs_assert(expression) assert(expression) + +#include +#include + +// generated by configure. +#include + +// free the p and set to NULL. +// p must be a T*. +#define srs_freep(p) \ + if (p) { \ + delete p; \ + p = NULL; \ + } \ + (void)0 +// sometimes, the freepa is useless, +// it's recomments to free each elem explicit. +// so we remove the srs_freepa utility. + +/** +* disable copy constructor of class +*/ +#define disable_default_copy(className)\ + private:\ + /** \ + * disable the copy constructor and operator=, donot allow directly copy. \ + */ \ + className(const className&); \ + className& operator= (const className&) + +#endif diff --git a/trunk/src/kernel/srs_kernel_error.hpp b/trunk/src/kernel/srs_kernel_error.hpp index 8e6c0ad6e..edf09e085 100644 --- a/trunk/src/kernel/srs_kernel_error.hpp +++ b/trunk/src/kernel/srs_kernel_error.hpp @@ -185,11 +185,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define ERROR_HTTP_PARSE_HEADER 802 #define ERROR_HTTP_HANDLER_MATCH_URL 803 #define ERROR_HTTP_HANDLER_INVALID 804 -#define ERROR_HTTP_OPEN_FILE 805 -#define ERROR_HTTP_READ_FILE 806 -#define ERROR_HTTP_API_LOGS 807 -#define ERROR_HTTP_FLV_SEQUENCE_HEADER 808 -#define ERROR_HTTP_FLV_OFFSET_OVERFLOW 809 +#define ERROR_HTTP_API_LOGS 805 +#define ERROR_HTTP_FLV_SEQUENCE_HEADER 806 +#define ERROR_HTTP_FLV_OFFSET_OVERFLOW 807 // system control message, // not an error, but special control logic. diff --git a/trunk/src/kernel/srs_kernel_file.cpp b/trunk/src/kernel/srs_kernel_file.cpp new file mode 100644 index 000000000..3e7193252 --- /dev/null +++ b/trunk/src/kernel/srs_kernel_file.cpp @@ -0,0 +1,213 @@ +/* +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. +*/ + +#include + +#include +#include +#include +using namespace std; + +#include +#include + +SrsFileWriter::SrsFileWriter() +{ +} + +SrsFileWriter::~SrsFileWriter() +{ + close(); +} + +int SrsFileWriter::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; +} + +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; + // TODO: FIXME: use st_write. + 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; + + if (fd > 0) { + ret = ERROR_SYSTEM_FILE_ALREADY_OPENED; + srs_error("file %s already opened. ret=%d", _file.c_str(), ret); + return ret; + } + + if ((fd = ::open(file.c_str(), O_RDONLY)) < 0) { + ret = ERROR_SYSTEM_FILE_OPENE; + srs_error("open file %s failed. ret=%d", file.c_str(), ret); + return ret; + } + + _file = file; + + return ret; +} + +void SrsFileReader::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 SrsFileReader::is_open() +{ + return fd > 0; +} + +int64_t SrsFileReader::tellg() +{ + return (int64_t)::lseek(fd, 0, SEEK_CUR); +} + +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; + + ssize_t nread; + // TODO: FIXME: use st_read. + 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; +} diff --git a/trunk/src/kernel/srs_kernel_file.hpp b/trunk/src/kernel/srs_kernel_file.hpp new file mode 100644 index 000000000..9046a0e44 --- /dev/null +++ b/trunk/src/kernel/srs_kernel_file.hpp @@ -0,0 +1,93 @@ +/* +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. +*/ + +#ifndef SRS_KERNEL_FILE_HPP +#define SRS_KERNEL_FILE_HPP + +/* +#include +*/ +#include + +#include + +/** +* file writer, to write to file. +*/ +class SrsFileWriter +{ +private: + std::string _file; + int fd; +public: + SrsFileWriter(); + virtual ~SrsFileWriter(); +public: + /** + * open file writer, can open then close then open... + */ + virtual int open(std::string file); + virtual void close(); +public: + virtual bool is_open(); + virtual int64_t tellg(); +public: + /** + * write to file. + * @param pnwrite the output nb_write, NULL to ignore. + */ + 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: + /** + * open file reader, can open then close then open... + */ + 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); + virtual int64_t filesize(); +public: + /** + * read from file. + * @param pnread the output nb_read, NULL to ignore. + */ + virtual int read(void* buf, size_t count, ssize_t* pnread); +}; + +#endif diff --git a/trunk/src/kernel/srs_kernel_flv.cpp b/trunk/src/kernel/srs_kernel_flv.cpp index 2757afc58..55521ae60 100644 --- a/trunk/src/kernel/srs_kernel_flv.cpp +++ b/trunk/src/kernel/srs_kernel_flv.cpp @@ -33,189 +33,11 @@ using namespace std; #include #include #include +#include #define SRS_FLV_TAG_HEADER_SIZE 11 #define SRS_FLV_PREVIOUS_TAG_SIZE 4 -SrsFileWriter::SrsFileWriter() -{ -} - -SrsFileWriter::~SrsFileWriter() -{ - close(); -} - -int SrsFileWriter::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; -} - -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; - - if (fd > 0) { - ret = ERROR_SYSTEM_FILE_ALREADY_OPENED; - srs_error("file %s already opened. ret=%d", _file.c_str(), ret); - return ret; - } - - if ((fd = ::open(file.c_str(), O_RDONLY)) < 0) { - ret = ERROR_SYSTEM_FILE_OPENE; - srs_error("open file %s failed. ret=%d", file.c_str(), ret); - return ret; - } - - _file = file; - - return ret; -} - -void SrsFileReader::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 SrsFileReader::is_open() -{ - return fd > 0; -} - -int64_t SrsFileReader::tellg() -{ - return (int64_t)::lseek(fd, 0, SEEK_CUR); -} - -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; - - 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; -} - SrsFlvEncoder::SrsFlvEncoder() { _fs = NULL; diff --git a/trunk/src/kernel/srs_kernel_flv.hpp b/trunk/src/kernel/srs_kernel_flv.hpp index e982bc968..b085c5b20 100644 --- a/trunk/src/kernel/srs_kernel_flv.hpp +++ b/trunk/src/kernel/srs_kernel_flv.hpp @@ -32,51 +32,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include class SrsStream; - -/** -* file writer, to write to file. -*/ -class SrsFileWriter -{ -private: - std::string _file; - int fd; -public: - SrsFileWriter(); - virtual ~SrsFileWriter(); -public: - virtual int open(std::string file); - virtual void close(); -public: - 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 bool is_open(); - virtual int64_t tellg(); - virtual void skip(int64_t size); - virtual int64_t lseek(int64_t offset); - virtual int64_t filesize(); -public: - virtual int read(void* buf, size_t count, ssize_t* pnread); -}; +class SrsFileWriter; +class SrsFileReader; /** * encode data to flv file. diff --git a/trunk/src/libs/srs_librtmp.cpp b/trunk/src/libs/srs_librtmp.cpp index 14ce1bf40..baecfebb8 100644 --- a/trunk/src/libs/srs_librtmp.cpp +++ b/trunk/src/libs/srs_librtmp.cpp @@ -41,6 +41,7 @@ using namespace std; #include #include #include +#include // if user want to define log, define the folowing macro. #ifndef SRS_RTMP_USER_DEFINED_LOG diff --git a/trunk/src/srs/srs.upp b/trunk/src/srs/srs.upp index 5a09fa8d7..f1583d3bc 100755 --- a/trunk/src/srs/srs.upp +++ b/trunk/src/srs/srs.upp @@ -21,6 +21,8 @@ file ..\kernel\srs_kernel_codec.cpp, ..\kernel\srs_kernel_error.hpp, ..\kernel\srs_kernel_error.cpp, + ..\kernel\srs_kernel_file.hpp, + ..\kernel\srs_kernel_file.cpp, ..\kernel\srs_kernel_flv.hpp, ..\kernel\srs_kernel_flv.cpp, ..\kernel\srs_kernel_log.hpp, diff --git a/trunk/src/utest/srs_utest_kernel.hpp b/trunk/src/utest/srs_utest_kernel.hpp index f32543261..3e6723f69 100644 --- a/trunk/src/utest/srs_utest_kernel.hpp +++ b/trunk/src/utest/srs_utest_kernel.hpp @@ -30,7 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include -#include +#include class MockSrsFileWriter : public SrsFileWriter {