mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
Add test for vod stream handler
This commit is contained in:
parent
97f2c5bf0c
commit
8cdb7cc727
4 changed files with 214 additions and 53 deletions
|
@ -51,8 +51,7 @@ using namespace std;
|
|||
#include <srs_app_source.hpp>
|
||||
#include <srs_app_server.hpp>
|
||||
|
||||
SrsVodStream::SrsVodStream(string root_dir)
|
||||
: SrsHttpFileServer(root_dir)
|
||||
SrsVodStream::SrsVodStream(string root_dir) : SrsHttpFileServer(root_dir)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -64,22 +63,23 @@ srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMe
|
|||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
SrsFileReader fs;
|
||||
SrsFileReader* fs = fs_factory->create_file_reader();
|
||||
SrsAutoFree(SrsFileReader, fs);
|
||||
|
||||
// open flv file
|
||||
if ((err = fs.open(fullpath)) != srs_success) {
|
||||
if ((err = fs->open(fullpath)) != srs_success) {
|
||||
return srs_error_wrap(err, "open file");
|
||||
}
|
||||
|
||||
if (offset > fs.filesize()) {
|
||||
if (offset > fs->filesize()) {
|
||||
return srs_error_new(ERROR_HTTP_REMUX_OFFSET_OVERFLOW, "http flv streaming %s overflow. size=%" PRId64 ", offset=%d",
|
||||
fullpath.c_str(), fs.filesize(), offset);
|
||||
fullpath.c_str(), fs->filesize(), offset);
|
||||
}
|
||||
|
||||
SrsFlvVodStreamDecoder ffd;
|
||||
|
||||
// open fast decoder
|
||||
if ((err = ffd.initialize(&fs)) != srs_success) {
|
||||
if ((err = ffd.initialize(fs)) != srs_success) {
|
||||
return srs_error_wrap(err, "init ffd");
|
||||
}
|
||||
|
||||
|
@ -107,12 +107,12 @@ srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMe
|
|||
}
|
||||
sh_data = new char[sh_size];
|
||||
SrsAutoFreeA(char, sh_data);
|
||||
if ((err = fs.read(sh_data, sh_size, NULL)) != srs_success) {
|
||||
if ((err = fs->read(sh_data, sh_size, NULL)) != srs_success) {
|
||||
return srs_error_wrap(err, "fs read");
|
||||
}
|
||||
|
||||
// seek to data offset
|
||||
int64_t left = fs.filesize() - offset;
|
||||
int64_t left = fs->filesize() - offset;
|
||||
|
||||
// write http header for ts.
|
||||
w->header()->set_content_length((int)(sizeof(flv_header) + sh_size + left));
|
||||
|
@ -132,7 +132,7 @@ srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMe
|
|||
}
|
||||
|
||||
// send data
|
||||
if ((err = copy(w, &fs, r, (int)left)) != srs_success) {
|
||||
if ((err = copy(w, fs, r, (int)left)) != srs_success) {
|
||||
return srs_error_wrap(err, "read flv=%s size=%d", fullpath.c_str(), left);
|
||||
}
|
||||
|
||||
|
@ -146,21 +146,22 @@ srs_error_t SrsVodStream::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMe
|
|||
srs_assert(start >= 0);
|
||||
srs_assert(end == -1 || end >= 0);
|
||||
|
||||
SrsFileReader fs;
|
||||
SrsFileReader* fs = fs_factory->create_file_reader();
|
||||
SrsAutoFree(SrsFileReader, fs);
|
||||
|
||||
// open flv file
|
||||
if ((err = fs.open(fullpath)) != srs_success) {
|
||||
if ((err = fs->open(fullpath)) != srs_success) {
|
||||
return srs_error_wrap(err, "fs open");
|
||||
}
|
||||
|
||||
// parse -1 to whole file.
|
||||
if (end == -1) {
|
||||
end = (int)fs.filesize();
|
||||
end = (int)fs->filesize();
|
||||
}
|
||||
|
||||
if (end > fs.filesize() || start > end) {
|
||||
if (end > fs->filesize() || start > end) {
|
||||
return srs_error_new(ERROR_HTTP_REMUX_OFFSET_OVERFLOW, "http mp4 streaming %s overflow. size=%" PRId64 ", offset=%d",
|
||||
fullpath.c_str(), fs.filesize(), start);
|
||||
fullpath.c_str(), fs->filesize(), start);
|
||||
}
|
||||
|
||||
// seek to data offset, [start, end] for range.
|
||||
|
@ -174,15 +175,16 @@ srs_error_t SrsVodStream::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMe
|
|||
w->write_header(SRS_CONSTS_HTTP_PartialContent);
|
||||
|
||||
// response the content range header.
|
||||
// https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Range_requests
|
||||
std::stringstream content_range;
|
||||
content_range << "bytes " << start << "-" << end << "/" << fs.filesize();
|
||||
content_range << "bytes " << start << "-" << end << "/" << fs->filesize();
|
||||
w->header()->set("Content-Range", content_range.str());
|
||||
|
||||
// write body.
|
||||
fs.seek2(start);
|
||||
fs->seek2(start);
|
||||
|
||||
// send data
|
||||
if ((err = copy(w, &fs, r, (int)left)) != srs_success) {
|
||||
if ((err = copy(w, fs, r, (int)left)) != srs_success) {
|
||||
return srs_error_wrap(err, "read mp4=%s size=%d", fullpath.c_str(), left);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue