winlin

refine code, use decoder to parse flv stream

@@ -36,6 +36,7 @@ using namespace std; @@ -36,6 +36,7 @@ using namespace std;
36 #include <srs_app_http.hpp> 36 #include <srs_app_http.hpp>
37 #include <srs_app_http_conn.hpp> 37 #include <srs_app_http_conn.hpp>
38 #include <srs_core_autofree.hpp> 38 #include <srs_core_autofree.hpp>
  39 +#include <srs_kernel_flv.hpp>
39 40
40 #define SRS_HTTP_FLV_STREAM_BUFFER 4096 41 #define SRS_HTTP_FLV_STREAM_BUFFER 4096
41 42
@@ -92,6 +93,13 @@ int SrsAppCasterFlv::serve_http(ISrsHttpResponseWriter* w, SrsHttpMessage* r) @@ -92,6 +93,13 @@ int SrsAppCasterFlv::serve_http(ISrsHttpResponseWriter* w, SrsHttpMessage* r)
92 SrsAutoFree(char, buffer); 93 SrsAutoFree(char, buffer);
93 94
94 ISrsHttpResponseReader* rr = r->body_reader(); 95 ISrsHttpResponseReader* rr = r->body_reader();
  96 + SrsHttpFileReader reader(rr);
  97 + SrsFlvDecoder dec;
  98 +
  99 + if ((ret = dec.initialize(&reader)) != ERROR_SUCCESS) {
  100 + return ret;
  101 + }
  102 +
95 while (!rr->eof()) { 103 while (!rr->eof()) {
96 int nb_read = 0; 104 int nb_read = 0;
97 if ((ret = rr->read(buffer, SRS_HTTP_FLV_STREAM_BUFFER, &nb_read)) != ERROR_SUCCESS) { 105 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) @@ -118,4 +126,68 @@ int SrsDynamicHttpConn::on_got_http_message(SrsHttpMessage* msg)
118 return ret; 126 return ret;
119 } 127 }
120 128
  129 +SrsHttpFileReader::SrsHttpFileReader(ISrsHttpResponseReader* h)
  130 +{
  131 + http = h;
  132 +}
  133 +
  134 +SrsHttpFileReader::~SrsHttpFileReader()
  135 +{
  136 +}
  137 +
  138 +int SrsHttpFileReader::open(std::string /*file*/)
  139 +{
  140 + return ERROR_SUCCESS;
  141 +}
  142 +
  143 +void SrsHttpFileReader::close()
  144 +{
  145 +}
  146 +
  147 +bool SrsHttpFileReader::is_open()
  148 +{
  149 + return false;
  150 +}
  151 +
  152 +int64_t SrsHttpFileReader::tellg()
  153 +{
  154 + return 0;
  155 +}
  156 +
  157 +void SrsHttpFileReader::skip(int64_t /*size*/)
  158 +{
  159 +}
  160 +
  161 +int64_t SrsHttpFileReader::lseek(int64_t offset)
  162 +{
  163 + return offset;
  164 +}
  165 +
  166 +int64_t SrsHttpFileReader::filesize()
  167 +{
  168 + return 0;
  169 +}
  170 +
  171 +int SrsHttpFileReader::read(void* buf, size_t count, ssize_t* pnread)
  172 +{
  173 + int ret = ERROR_SUCCESS;
  174 +
  175 + if (http->eof()) {
  176 + ret = ERROR_HTTP_REQUEST_EOF;
  177 + srs_error("flv: encoder EOF. ret=%d", ret);
  178 + return ret;
  179 + }
  180 +
  181 + int nread = 0;
  182 + if ((ret = http->read((char*)buf, (int)count, &nread)) != ERROR_SUCCESS) {
  183 + return ret;
  184 + }
  185 +
  186 + if (pnread) {
  187 + *pnread = nread;
  188 + }
  189 +
  190 + return ret;
  191 +}
  192 +
121 #endif 193 #endif
@@ -44,7 +44,11 @@ class SrsHttpConn; @@ -44,7 +44,11 @@ class SrsHttpConn;
44 #include <srs_app_conn.hpp> 44 #include <srs_app_conn.hpp>
45 #include <srs_app_http.hpp> 45 #include <srs_app_http.hpp>
46 #include <srs_app_http_conn.hpp> 46 #include <srs_app_http_conn.hpp>
  47 +#include <srs_kernel_file.hpp>
47 48
  49 +/**
  50 + * the stream caster for flv stream over HTTP POST.
  51 + */
48 class SrsAppCasterFlv : virtual public ISrsTcpHandler 52 class SrsAppCasterFlv : virtual public ISrsTcpHandler
49 , virtual public IConnectionManager, virtual public ISrsHttpHandler 53 , virtual public IConnectionManager, virtual public ISrsHttpHandler
50 { 54 {
@@ -68,6 +72,9 @@ public: @@ -68,6 +72,9 @@ public:
68 virtual int serve_http(ISrsHttpResponseWriter* w, SrsHttpMessage* r); 72 virtual int serve_http(ISrsHttpResponseWriter* w, SrsHttpMessage* r);
69 }; 73 };
70 74
  75 +/**
  76 + * the dynamic http connection, never drop the body.
  77 + */
71 class SrsDynamicHttpConn : public SrsHttpConn 78 class SrsDynamicHttpConn : public SrsHttpConn
72 { 79 {
73 public: 80 public:
@@ -77,6 +84,38 @@ public: @@ -77,6 +84,38 @@ public:
77 virtual int on_got_http_message(SrsHttpMessage* msg); 84 virtual int on_got_http_message(SrsHttpMessage* msg);
78 }; 85 };
79 86
  87 +/**
  88 + * the http wrapper for file reader,
  89 + * to read http post stream like a file.
  90 + */
  91 +class SrsHttpFileReader : public SrsFileReader
  92 +{
  93 +private:
  94 + ISrsHttpResponseReader* http;
  95 +public:
  96 + SrsHttpFileReader(ISrsHttpResponseReader* h);
  97 + virtual ~SrsHttpFileReader();
  98 +public:
  99 + /**
  100 + * open file reader, can open then close then open...
  101 + */
  102 + virtual int open(std::string file);
  103 + virtual void close();
  104 +public:
  105 + // TODO: FIXME: extract interface.
  106 + virtual bool is_open();
  107 + virtual int64_t tellg();
  108 + virtual void skip(int64_t size);
  109 + virtual int64_t lseek(int64_t offset);
  110 + virtual int64_t filesize();
  111 +public:
  112 + /**
  113 + * read from file.
  114 + * @param pnread the output nb_read, NULL to ignore.
  115 + */
  116 + virtual int read(void* buf, size_t count, ssize_t* pnread);
  117 +};
  118 +
80 #endif 119 #endif
81 120
82 #endif 121 #endif
@@ -255,6 +255,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -255,6 +255,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
255 #define ERROR_HTTP_INVALID_CHUNK_HEADER 4026 255 #define ERROR_HTTP_INVALID_CHUNK_HEADER 4026
256 #define ERROR_AVC_NALU_UEV 4027 256 #define ERROR_AVC_NALU_UEV 4027
257 #define ERROR_AAC_BYTES_INVALID 4028 257 #define ERROR_AAC_BYTES_INVALID 4028
  258 +#define ERROR_HTTP_REQUEST_EOF 4029
258 259
259 /////////////////////////////////////////////////////// 260 ///////////////////////////////////////////////////////
260 // user-define error. 261 // user-define error.