winlin

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

... ... @@ -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[@]}"
#
... ...
... ... @@ -40,6 +40,7 @@ using namespace std;
#include <srs_app_http_hooks.hpp>
#include <srs_kernel_codec.hpp>
#include <srs_kernel_flv.hpp>
#include <srs_kernel_file.hpp>
SrsFlvSegment::SrsFlvSegment()
{
... ...
... ... @@ -45,6 +45,7 @@ using namespace std;
#include <srs_app_pithy_print.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_app_avc_aac.hpp>
#include <srs_kernel_file.hpp>
// 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()
... ...
... ... @@ -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();
... ...
... ... @@ -42,6 +42,7 @@ using namespace std;
#include <srs_app_config.hpp>
#include <srs_kernel_flv.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_kernel_file.hpp>
#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;
}
... ...
/*
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 <srs_core.hpp>
*/
// 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 <inttypes.h>
#include <assert.h>
#define srs_assert(expression) assert(expression)
#include <stddef.h>
#include <sys/types.h>
// generated by configure.
#include <srs_auto_headers.hpp>
// 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 <srs_core.hpp>
*/
// 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 <inttypes.h>
#include <assert.h>
#define srs_assert(expression) assert(expression)
#include <stddef.h>
#include <sys/types.h>
// generated by configure.
#include <srs_auto_headers.hpp>
// 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
... ...
... ... @@ -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.
... ...
/*
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;
}
... ...
/*
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
... ...
... ... @@ -33,189 +33,11 @@ using namespace std;
#include <srs_core_autofree.hpp>
#include <srs_kernel_stream.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_kernel_file.hpp>
#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;
... ...
... ... @@ -32,51 +32,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <string>
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.
... ...
... ... @@ -41,6 +41,7 @@ using namespace std;
#include <srs_protocol_amf0.hpp>
#include <srs_kernel_flv.hpp>
#include <srs_kernel_codec.hpp>
#include <srs_kernel_file.hpp>
// if user want to define log, define the folowing macro.
#ifndef SRS_RTMP_USER_DEFINED_LOG
... ...
... ... @@ -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,
... ...
... ... @@ -30,7 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_utest.hpp>
#include <string>
#include <srs_kernel_flv.hpp>
#include <srs_kernel_file.hpp>
class MockSrsFileWriter : public SrsFileWriter
{
... ...