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

View file

@ -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()
{

View file

@ -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()

View file

@ -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();

View file

@ -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;
}