1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

refine http/dvr/hls to use file reader/writer. 0.9.146

This commit is contained in:
winlin 2014-07-05 07:33:18 +08:00
parent ba2e7bbf76
commit 032118581a
14 changed files with 447 additions and 382 deletions

2
trunk/configure vendored
View file

@ -447,7 +447,7 @@ MODULE_ID="KERNEL"
MODULE_DEPENDS=("CORE") MODULE_DEPENDS=("CORE")
ModuleLibIncs=(${SRS_OBJS}) ModuleLibIncs=(${SRS_OBJS})
MODULE_FILES=("srs_kernel_error" "srs_kernel_log" "srs_kernel_stream" "srs_kernel_buffer" 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_INCS="src/kernel"; MODULE_DIR=${KERNEL_INCS} . auto/modules.sh
KERNEL_OBJS="${MODULE_OBJS[@]}" KERNEL_OBJS="${MODULE_OBJS[@]}"
# #

View file

@ -40,6 +40,7 @@ using namespace std;
#include <srs_app_http_hooks.hpp> #include <srs_app_http_hooks.hpp>
#include <srs_kernel_codec.hpp> #include <srs_kernel_codec.hpp>
#include <srs_kernel_flv.hpp> #include <srs_kernel_flv.hpp>
#include <srs_kernel_file.hpp>
SrsFlvSegment::SrsFlvSegment() SrsFlvSegment::SrsFlvSegment()
{ {

View file

@ -45,6 +45,7 @@ using namespace std;
#include <srs_app_pithy_print.hpp> #include <srs_app_pithy_print.hpp>
#include <srs_kernel_utility.hpp> #include <srs_kernel_utility.hpp>
#include <srs_app_avc_aac.hpp> #include <srs_app_avc_aac.hpp>
#include <srs_kernel_file.hpp>
// max PES packets size to flush the video. // max PES packets size to flush the video.
#define SRS_AUTO_HLS_AUDIO_CACHE_SIZE 1024 * 1024 #define SRS_AUTO_HLS_AUDIO_CACHE_SIZE 1024 * 1024
@ -150,12 +151,11 @@ public:
class SrsMpegtsWriter class SrsMpegtsWriter
{ {
public: public:
static int write_header(int fd) static int write_header(SrsFileWriter* writer)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
// TODO: FIXME: maybe should use st_write. if ((ret = writer->write(mpegts_header, sizeof(mpegts_header), NULL)) != ERROR_SUCCESS) {
if (::write(fd, mpegts_header, sizeof(mpegts_header)) != sizeof(mpegts_header)) {
ret = ERROR_HLS_WRITE_FAILED; ret = ERROR_HLS_WRITE_FAILED;
srs_error("write ts file header failed. ret=%d", ret); srs_error("write ts file header failed. ret=%d", ret);
return ret; return ret;
@ -163,7 +163,7 @@ public:
return ret; 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; int ret = ERROR_SUCCESS;
@ -279,8 +279,7 @@ public:
} }
// write ts packet // write ts packet
// TODO: FIXME: maybe should use st_write. if ((ret = writer->write(packet, sizeof(packet), NULL)) != ERROR_SUCCESS) {
if (::write(fd, packet, sizeof(packet)) != sizeof(packet)) {
ret = ERROR_HLS_WRITE_FAILED; ret = ERROR_HLS_WRITE_FAILED;
srs_error("write ts file failed. ret=%d", ret); srs_error("write ts file failed. ret=%d", ret);
return ret; return ret;
@ -414,12 +413,13 @@ void SrsHlsAacJitter::on_buffer_continue()
SrsTSMuxer::SrsTSMuxer() SrsTSMuxer::SrsTSMuxer()
{ {
fd = -1; writer = new SrsFileWriter();
} }
SrsTSMuxer::~SrsTSMuxer() SrsTSMuxer::~SrsTSMuxer()
{ {
close(); close();
srs_freep(writer);
} }
int SrsTSMuxer::open(string _path) int SrsTSMuxer::open(string _path)
@ -430,17 +430,12 @@ int SrsTSMuxer::open(string _path)
close(); close();
int flags = O_CREAT|O_WRONLY|O_TRUNC; if ((ret = writer->open(path)) != ERROR_SUCCESS) {
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);
return ret; return ret;
} }
// write mpegts header // write mpegts header
if ((ret = SrsMpegtsWriter::write_header(fd)) != ERROR_SUCCESS) { if ((ret = SrsMpegtsWriter::write_header(writer)) != ERROR_SUCCESS) {
return ret; return ret;
} }
@ -451,7 +446,7 @@ int SrsTSMuxer::write_audio(SrsMpegtsFrame* af, SrsCodecBuffer* ab)
{ {
int ret = ERROR_SUCCESS; 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; return ret;
} }
@ -462,7 +457,7 @@ int SrsTSMuxer::write_video(SrsMpegtsFrame* vf, SrsCodecBuffer* vb)
{ {
int ret = ERROR_SUCCESS; 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; return ret;
} }
@ -471,10 +466,7 @@ int SrsTSMuxer::write_video(SrsMpegtsFrame* vf, SrsCodecBuffer* vb)
void SrsTSMuxer::close() void SrsTSMuxer::close()
{ {
if (fd > 0) { writer->close();
::close(fd);
fd = -1;
}
} }
SrsHlsSegment::SrsHlsSegment() SrsHlsSegment::SrsHlsSegment()

View file

@ -45,6 +45,7 @@ class SrsAvcAacCodec;
class SrsRequest; class SrsRequest;
class SrsPithyPrint; class SrsPithyPrint;
class SrsSource; class SrsSource;
class SrsFileWriter;
/** /**
* jitter correct for audio, * jitter correct for audio,
@ -83,7 +84,7 @@ public:
class SrsTSMuxer class SrsTSMuxer
{ {
private: private:
int fd; SrsFileWriter* writer;
std::string path; std::string path;
public: public:
SrsTSMuxer(); SrsTSMuxer();

View file

@ -42,6 +42,7 @@ using namespace std;
#include <srs_app_config.hpp> #include <srs_app_config.hpp>
#include <srs_kernel_flv.hpp> #include <srs_kernel_flv.hpp>
#include <srs_kernel_utility.hpp> #include <srs_kernel_utility.hpp>
#include <srs_kernel_file.hpp>
#define SRS_HTTP_DEFAULT_PAGE "index.html" #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; int ret = ERROR_SUCCESS;
// TODO: FIXME: refine the file stream. SrsFileReader fs;
int fd = ::open(fullpath.c_str(), O_RDONLY);
if (fd < 0) { if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) {
ret = ERROR_HTTP_OPEN_FILE;
srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret); srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret);
return ret; return ret;
} }
int64_t length = (int64_t)::lseek(fd, 0, SEEK_END); int64_t length = fs.filesize();
::lseek(fd, 0, SEEK_SET);
char* buf = new char[length]; char* buf = new char[length];
SrsAutoFree(char, buf); SrsAutoFree(char, buf);
// TODO: FIXME: use st_read. if ((ret = fs.read(buf, length, NULL)) != ERROR_SUCCESS) {
if (::read(fd, buf, length) < 0) {
::close(fd);
ret = ERROR_HTTP_READ_FILE;
srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret); srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret);
return ret; return ret;
} }
::close(fd);
std::string str; std::string str;
str.append(buf, length); 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 SrsHttpVhost::response_flv_file(SrsSocket* skt, SrsHttpMessage* req, string fullpath)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
SrsFileReader fs;
// TODO: FIXME: use more advance cache. // TODO: FIXME: use more advance cache.
// for ts video large file, use bytes to write it. if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) {
int fd = ::open(fullpath.c_str(), O_RDONLY);
if (fd < 0) {
ret = ERROR_HTTP_OPEN_FILE;
srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret); srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret);
return ret; return ret;
} }
int64_t length = (int64_t)::lseek(fd, 0, SEEK_END); int64_t length = fs.filesize();
::lseek(fd, 0, SEEK_SET);
// write http header for ts. // write http header for ts.
std::stringstream ss; std::stringstream ss;
@ -279,9 +272,7 @@ int SrsHttpVhost::response_flv_file(SrsSocket* skt, SrsHttpMessage* req, string
while (left > 0) { while (left > 0) {
ssize_t nread = -1; ssize_t nread = -1;
// TODO: FIXME: use st_read. if ((ret = fs.read(buf, HTTP_TS_SEND_BUFFER_SIZE, &nread)) != ERROR_SUCCESS) {
if ((nread = ::read(fd, buf, HTTP_TS_SEND_BUFFER_SIZE)) < 0) {
ret = ERROR_HTTP_READ_FILE;
srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret); srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret);
break; break;
} }
@ -291,7 +282,6 @@ int SrsHttpVhost::response_flv_file(SrsSocket* skt, SrsHttpMessage* req, string
break; break;
} }
} }
::close(fd);
return ret; return ret;
} }
@ -406,17 +396,15 @@ int SrsHttpVhost::response_ts_file(SrsSocket* skt, SrsHttpMessage* req, string f
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
SrsFileReader fs;
// TODO: FIXME: use more advance cache. // TODO: FIXME: use more advance cache.
// for ts video large file, use bytes to write it. if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) {
int fd = ::open(fullpath.c_str(), O_RDONLY);
if (fd < 0) {
ret = ERROR_HTTP_OPEN_FILE;
srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret); srs_warn("open file %s failed, ret=%d", fullpath.c_str(), ret);
return ret; return ret;
} }
int64_t length = (int64_t)::lseek(fd, 0, SEEK_END); int64_t length = fs.filesize();
::lseek(fd, 0, SEEK_SET);
// write http header for ts. // write http header for ts.
std::stringstream ss; std::stringstream ss;
@ -441,9 +429,7 @@ int SrsHttpVhost::response_ts_file(SrsSocket* skt, SrsHttpMessage* req, string f
while (left > 0) { while (left > 0) {
ssize_t nread = -1; ssize_t nread = -1;
// TODO: FIXME: use st_read. if ((ret = fs.read(buf, HTTP_TS_SEND_BUFFER_SIZE, &nread)) != ERROR_SUCCESS) {
if ((nread = ::read(fd, buf, HTTP_TS_SEND_BUFFER_SIZE)) < 0) {
ret = ERROR_HTTP_READ_FILE;
srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret); srs_warn("read file %s failed, ret=%d", fullpath.c_str(), ret);
break; break;
} }
@ -453,7 +439,6 @@ int SrsHttpVhost::response_ts_file(SrsSocket* skt, SrsHttpMessage* req, string f
break; break;
} }
} }
::close(fd);
return ret; return ret;
} }

198
trunk/src/core/srs_core.hpp Normal file → Executable file
View file

@ -1,99 +1,99 @@
/* /*
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2013-2014 winlin Copyright (c) 2013-2014 winlin
Permission is hereby granted, free of charge, to any person obtaining a copy of 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 this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 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, the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions: subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 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 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 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. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#ifndef SRS_CORE_HPP #ifndef SRS_CORE_HPP
#define SRS_CORE_HPP #define SRS_CORE_HPP
/* /*
#include <srs_core.hpp> #include <srs_core.hpp>
*/ */
// current release version // current release version
#define VERSION_MAJOR "0" #define VERSION_MAJOR "0"
#define VERSION_MINOR "9" #define VERSION_MINOR "9"
#define VERSION_REVISION "145" #define VERSION_REVISION "146"
#define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
// server info. // server info.
#define RTMP_SIG_SRS_KEY "SRS" #define RTMP_SIG_SRS_KEY "SRS"
#define RTMP_SIG_SRS_ROLE "origin/edge server" #define RTMP_SIG_SRS_ROLE "origin/edge server"
#define RTMP_SIG_SRS_NAME RTMP_SIG_SRS_KEY"(Simple RTMP 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_SHORT "github.com/winlinvip/simple-rtmp-server"
#define RTMP_SIG_SRS_URL "https://"RTMP_SIG_SRS_URL_SHORT #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_WEB "http://blog.csdn.net/win_lin"
#define RTMP_SIG_SRS_EMAIL "winlin@vip.126.com" #define RTMP_SIG_SRS_EMAIL "winlin@vip.126.com"
#define RTMP_SIG_SRS_LICENSE "The MIT License (MIT)" #define RTMP_SIG_SRS_LICENSE "The MIT License (MIT)"
#define RTMP_SIG_SRS_COPYRIGHT "Copyright (c) 2013-2014 winlin" #define RTMP_SIG_SRS_COPYRIGHT "Copyright (c) 2013-2014 winlin"
#define RTMP_SIG_SRS_PRIMARY_AUTHROS "winlin,wenjie.zhao" #define RTMP_SIG_SRS_PRIMARY_AUTHROS "winlin,wenjie.zhao"
#define RTMP_SIG_SRS_CONTRIBUTORS_URL RTMP_SIG_SRS_URL"/blob/master/AUTHORS.txt" #define RTMP_SIG_SRS_CONTRIBUTORS_URL RTMP_SIG_SRS_URL"/blob/master/AUTHORS.txt"
/** /**
* the core provides the common defined macros, utilities, * the core provides the common defined macros, utilities,
* user must include the srs_core.hpp before any header, or maybe * user must include the srs_core.hpp before any header, or maybe
* build failed. * build failed.
*/ */
// for 32bit os, 2G big file limit for unistd io, // for 32bit os, 2G big file limit for unistd io,
// ie. read/write/lseek to use 64bits size for huge file. // ie. read/write/lseek to use 64bits size for huge file.
#ifndef _FILE_OFFSET_BITS #ifndef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64 #define _FILE_OFFSET_BITS 64
#endif #endif
// for int64_t print using PRId64 format. // for int64_t print using PRId64 format.
#ifndef __STDC_FORMAT_MACROS #ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS #define __STDC_FORMAT_MACROS
#endif #endif
#include <inttypes.h> #include <inttypes.h>
#include <assert.h> #include <assert.h>
#define srs_assert(expression) assert(expression) #define srs_assert(expression) assert(expression)
#include <stddef.h> #include <stddef.h>
#include <sys/types.h> #include <sys/types.h>
// generated by configure. // generated by configure.
#include <srs_auto_headers.hpp> #include <srs_auto_headers.hpp>
// free the p and set to NULL. // free the p and set to NULL.
// p must be a T*. // p must be a T*.
#define srs_freep(p) \ #define srs_freep(p) \
if (p) { \ if (p) { \
delete p; \ delete p; \
p = NULL; \ p = NULL; \
} \ } \
(void)0 (void)0
// sometimes, the freepa is useless, // sometimes, the freepa is useless,
// it's recomments to free each elem explicit. // it's recomments to free each elem explicit.
// so we remove the srs_freepa utility. // so we remove the srs_freepa utility.
/** /**
* disable copy constructor of class * disable copy constructor of class
*/ */
#define disable_default_copy(className)\ #define disable_default_copy(className)\
private:\ private:\
/** \ /** \
* disable the copy constructor and operator=, donot allow directly copy. \ * disable the copy constructor and operator=, donot allow directly copy. \
*/ \ */ \
className(const className&); \ className(const className&); \
className& operator= (const className&) className& operator= (const className&)
#endif #endif

View file

@ -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_PARSE_HEADER 802
#define ERROR_HTTP_HANDLER_MATCH_URL 803 #define ERROR_HTTP_HANDLER_MATCH_URL 803
#define ERROR_HTTP_HANDLER_INVALID 804 #define ERROR_HTTP_HANDLER_INVALID 804
#define ERROR_HTTP_OPEN_FILE 805 #define ERROR_HTTP_API_LOGS 805
#define ERROR_HTTP_READ_FILE 806 #define ERROR_HTTP_FLV_SEQUENCE_HEADER 806
#define ERROR_HTTP_API_LOGS 807 #define ERROR_HTTP_FLV_OFFSET_OVERFLOW 807
#define ERROR_HTTP_FLV_SEQUENCE_HEADER 808
#define ERROR_HTTP_FLV_OFFSET_OVERFLOW 809
// system control message, // system control message,
// not an error, but special control logic. // not an error, but special control logic.

View file

@ -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 <srs_kernel_file.hpp>
#include <fcntl.h>
#include <unistd.h>
#include <sstream>
using namespace std;
#include <srs_kernel_log.hpp>
#include <srs_kernel_error.hpp>
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;
}

View file

@ -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 <srs_kernel_file.hpp>
*/
#include <srs_core.hpp>
#include <string>
/**
* 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

View file

@ -33,189 +33,11 @@ using namespace std;
#include <srs_core_autofree.hpp> #include <srs_core_autofree.hpp>
#include <srs_kernel_stream.hpp> #include <srs_kernel_stream.hpp>
#include <srs_kernel_utility.hpp> #include <srs_kernel_utility.hpp>
#include <srs_kernel_file.hpp>
#define SRS_FLV_TAG_HEADER_SIZE 11 #define SRS_FLV_TAG_HEADER_SIZE 11
#define SRS_FLV_PREVIOUS_TAG_SIZE 4 #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() SrsFlvEncoder::SrsFlvEncoder()
{ {
_fs = NULL; _fs = NULL;

View file

@ -32,51 +32,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <string> #include <string>
class SrsStream; class SrsStream;
class SrsFileWriter;
/** class SrsFileReader;
* 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);
};
/** /**
* encode data to flv file. * encode data to flv file.

View file

@ -41,6 +41,7 @@ using namespace std;
#include <srs_protocol_amf0.hpp> #include <srs_protocol_amf0.hpp>
#include <srs_kernel_flv.hpp> #include <srs_kernel_flv.hpp>
#include <srs_kernel_codec.hpp> #include <srs_kernel_codec.hpp>
#include <srs_kernel_file.hpp>
// if user want to define log, define the folowing macro. // if user want to define log, define the folowing macro.
#ifndef SRS_RTMP_USER_DEFINED_LOG #ifndef SRS_RTMP_USER_DEFINED_LOG

View file

@ -21,6 +21,8 @@ file
..\kernel\srs_kernel_codec.cpp, ..\kernel\srs_kernel_codec.cpp,
..\kernel\srs_kernel_error.hpp, ..\kernel\srs_kernel_error.hpp,
..\kernel\srs_kernel_error.cpp, ..\kernel\srs_kernel_error.cpp,
..\kernel\srs_kernel_file.hpp,
..\kernel\srs_kernel_file.cpp,
..\kernel\srs_kernel_flv.hpp, ..\kernel\srs_kernel_flv.hpp,
..\kernel\srs_kernel_flv.cpp, ..\kernel\srs_kernel_flv.cpp,
..\kernel\srs_kernel_log.hpp, ..\kernel\srs_kernel_log.hpp,

View file

@ -30,7 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_utest.hpp> #include <srs_utest.hpp>
#include <string> #include <string>
#include <srs_kernel_flv.hpp> #include <srs_kernel_file.hpp>
class MockSrsFileWriter : public SrsFileWriter class MockSrsFileWriter : public SrsFileWriter
{ {