mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
refine code for #277, extract the flv vod stream.
This commit is contained in:
parent
e71bc0cbc5
commit
c695a8fcbd
4 changed files with 147 additions and 103 deletions
|
@ -39,6 +39,101 @@ using namespace std;
|
|||
#include <srs_core_autofree.hpp>
|
||||
#include <srs_app_config.hpp>
|
||||
#include <srs_kernel_utility.hpp>
|
||||
#include <srs_kernel_file.hpp>
|
||||
#include <srs_kernel_flv.hpp>
|
||||
|
||||
SrsVodStream::SrsVodStream(string root_dir)
|
||||
: SrsGoHttpFileServer(root_dir)
|
||||
{
|
||||
}
|
||||
|
||||
SrsVodStream::~SrsVodStream()
|
||||
{
|
||||
}
|
||||
|
||||
int SrsVodStream::serve_flv_stream(ISrsGoHttpResponseWriter* w, SrsHttpMessage* r, string fullpath, int offset)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsFileReader fs;
|
||||
|
||||
// open flv file
|
||||
if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (offset > fs.filesize()) {
|
||||
ret = ERROR_HTTP_FLV_OFFSET_OVERFLOW;
|
||||
srs_warn("http flv streaming %s overflow. size=%"PRId64", offset=%d, ret=%d",
|
||||
fullpath.c_str(), fs.filesize(), offset, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
SrsFlvVodStreamDecoder ffd;
|
||||
|
||||
// open fast decoder
|
||||
if ((ret = ffd.initialize(&fs)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// save header, send later.
|
||||
char flv_header[13];
|
||||
|
||||
// send flv header
|
||||
if ((ret = ffd.read_header_ext(flv_header)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// save sequence header, send later
|
||||
char* sh_data = NULL;
|
||||
int sh_size = 0;
|
||||
|
||||
if (true) {
|
||||
// send sequence header
|
||||
int64_t start = 0;
|
||||
if ((ret = ffd.read_sequence_header_summary(&start, &sh_size)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
if (sh_size <= 0) {
|
||||
ret = ERROR_HTTP_FLV_SEQUENCE_HEADER;
|
||||
srs_warn("http flv streaming no sequence header. size=%d, ret=%d", sh_size, ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
sh_data = new char[sh_size];
|
||||
SrsAutoFree(char, sh_data);
|
||||
if ((ret = fs.read(sh_data, sh_size, NULL)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// seek to data offset
|
||||
int64_t left = fs.filesize() - offset;
|
||||
|
||||
// write http header for ts.
|
||||
w->header()->set_content_length((int)(sizeof(flv_header) + sh_size + left));
|
||||
w->header()->set_content_type("video/x-flv");
|
||||
|
||||
// write flv header and sequence header.
|
||||
if ((ret = w->write(flv_header, sizeof(flv_header))) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
if (sh_size > 0 && (ret = w->write(sh_data, sh_size)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// write body.
|
||||
if ((ret = ffd.lseek(offset)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// send data
|
||||
if ((ret = copy(&fs, w, r, left)) != ERROR_SUCCESS) {
|
||||
srs_warn("read flv=%s size=%d failed, ret=%d", fullpath.c_str(), left, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SrsHttpServer::SrsHttpServer()
|
||||
{
|
||||
|
@ -71,7 +166,7 @@ int SrsHttpServer::initialize()
|
|||
std::string mount = _srs_config->get_vhost_http_mount(vhost);
|
||||
std::string dir = _srs_config->get_vhost_http_dir(vhost);
|
||||
|
||||
if ((ret = mux.handle(mount, new SrsGoHttpFileServer(dir))) != ERROR_SUCCESS) {
|
||||
if ((ret = mux.handle(mount, new SrsVodStream(dir))) != ERROR_SUCCESS) {
|
||||
srs_error("http: mount dir=%s for vhost=%s failed. ret=%d", dir.c_str(), vhost.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
@ -84,7 +179,7 @@ int SrsHttpServer::initialize()
|
|||
if (!default_root_exists) {
|
||||
// add root
|
||||
std::string dir = _srs_config->get_http_stream_dir();
|
||||
if ((ret = mux.handle("/", new SrsGoHttpFileServer(dir))) != ERROR_SUCCESS) {
|
||||
if ((ret = mux.handle("/", new SrsVodStream(dir))) != ERROR_SUCCESS) {
|
||||
srs_error("http: mount root dir=%s failed. ret=%d", dir.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue