winlin

refine code, use decoder to parse flv stream

... ... @@ -36,6 +36,7 @@ using namespace std;
#include <srs_app_http.hpp>
#include <srs_app_http_conn.hpp>
#include <srs_core_autofree.hpp>
#include <srs_kernel_flv.hpp>
#define SRS_HTTP_FLV_STREAM_BUFFER 4096
... ... @@ -92,6 +93,13 @@ int SrsAppCasterFlv::serve_http(ISrsHttpResponseWriter* w, SrsHttpMessage* r)
SrsAutoFree(char, buffer);
ISrsHttpResponseReader* rr = r->body_reader();
SrsHttpFileReader reader(rr);
SrsFlvDecoder dec;
if ((ret = dec.initialize(&reader)) != ERROR_SUCCESS) {
return ret;
}
while (!rr->eof()) {
int nb_read = 0;
if ((ret = rr->read(buffer, SRS_HTTP_FLV_STREAM_BUFFER, &nb_read)) != ERROR_SUCCESS) {
... ... @@ -118,4 +126,68 @@ int SrsDynamicHttpConn::on_got_http_message(SrsHttpMessage* msg)
return ret;
}
SrsHttpFileReader::SrsHttpFileReader(ISrsHttpResponseReader* h)
{
http = h;
}
SrsHttpFileReader::~SrsHttpFileReader()
{
}
int SrsHttpFileReader::open(std::string /*file*/)
{
return ERROR_SUCCESS;
}
void SrsHttpFileReader::close()
{
}
bool SrsHttpFileReader::is_open()
{
return false;
}
int64_t SrsHttpFileReader::tellg()
{
return 0;
}
void SrsHttpFileReader::skip(int64_t /*size*/)
{
}
int64_t SrsHttpFileReader::lseek(int64_t offset)
{
return offset;
}
int64_t SrsHttpFileReader::filesize()
{
return 0;
}
int SrsHttpFileReader::read(void* buf, size_t count, ssize_t* pnread)
{
int ret = ERROR_SUCCESS;
if (http->eof()) {
ret = ERROR_HTTP_REQUEST_EOF;
srs_error("flv: encoder EOF. ret=%d", ret);
return ret;
}
int nread = 0;
if ((ret = http->read((char*)buf, (int)count, &nread)) != ERROR_SUCCESS) {
return ret;
}
if (pnread) {
*pnread = nread;
}
return ret;
}
#endif
... ...
... ... @@ -44,7 +44,11 @@ class SrsHttpConn;
#include <srs_app_conn.hpp>
#include <srs_app_http.hpp>
#include <srs_app_http_conn.hpp>
#include <srs_kernel_file.hpp>
/**
* the stream caster for flv stream over HTTP POST.
*/
class SrsAppCasterFlv : virtual public ISrsTcpHandler
, virtual public IConnectionManager, virtual public ISrsHttpHandler
{
... ... @@ -68,6 +72,9 @@ public:
virtual int serve_http(ISrsHttpResponseWriter* w, SrsHttpMessage* r);
};
/**
* the dynamic http connection, never drop the body.
*/
class SrsDynamicHttpConn : public SrsHttpConn
{
public:
... ... @@ -77,6 +84,38 @@ public:
virtual int on_got_http_message(SrsHttpMessage* msg);
};
/**
* the http wrapper for file reader,
* to read http post stream like a file.
*/
class SrsHttpFileReader : public SrsFileReader
{
private:
ISrsHttpResponseReader* http;
public:
SrsHttpFileReader(ISrsHttpResponseReader* h);
virtual ~SrsHttpFileReader();
public:
/**
* open file reader, can open then close then open...
*/
virtual int open(std::string file);
virtual void close();
public:
// TODO: FIXME: extract interface.
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
#endif
... ...
... ... @@ -255,6 +255,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define ERROR_HTTP_INVALID_CHUNK_HEADER 4026
#define ERROR_AVC_NALU_UEV 4027
#define ERROR_AAC_BYTES_INVALID 4028
#define ERROR_HTTP_REQUEST_EOF 4029
///////////////////////////////////////////////////////
// user-define error.
... ...