Skip to content

Commit

Permalink
for #738, refine flv codec to use io interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jan 30, 2017
1 parent d3bb8b6 commit 01d401c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 43 deletions.
1 change: 1 addition & 0 deletions trunk/src/kernel/srs_kernel_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define ERROR_RESPONSE_DATA 3065
#define ERROR_REQUEST_DATA 3066
#define ERROR_EDGE_PORT_INVALID 3067
#define ERROR_EXPECT_FILE_IO 3068

///////////////////////////////////////////////////////
// HTTP/StreamCaster/KAFKA protocol error.
Expand Down
54 changes: 19 additions & 35 deletions trunk/src/kernel/srs_kernel_flv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ SrsSharedPtrMessage* SrsSharedPtrMessage::copy()

SrsFlvEncoder::SrsFlvEncoder()
{
reader = NULL;
writer = NULL;
tag_stream = new SrsBuffer();

#ifdef SRS_PERF_FAST_FLV_ENCODER
Expand All @@ -370,21 +370,11 @@ SrsFlvEncoder::~SrsFlvEncoder()
#endif
}

int SrsFlvEncoder::initialize(SrsFileWriter* fr)
int SrsFlvEncoder::initialize(ISrsWriter* fw)
{
int ret = ERROR_SUCCESS;

srs_assert(fr);

if (!fr->is_open()) {
ret = ERROR_KERNEL_FLV_STREAM_CLOSED;
srs_warn("stream is not open for encoder. ret=%d", ret);
return ret;
}

reader = fr;

return ret;
srs_assert(fw);
writer = fw;
return ERROR_SUCCESS;
}

int SrsFlvEncoder::write_header()
Expand Down Expand Up @@ -416,14 +406,14 @@ int SrsFlvEncoder::write_header(char flv_header[9])
int ret = ERROR_SUCCESS;

// write data.
if ((ret = reader->write(flv_header, 9, NULL)) != ERROR_SUCCESS) {
if ((ret = writer->write(flv_header, 9, NULL)) != ERROR_SUCCESS) {
srs_error("write flv header failed. ret=%d", ret);
return ret;
}

// previous tag size.
char pts[] = { (char)0x00, (char)0x00, (char)0x00, (char)0x00 };
if ((ret = reader->write(pts, 4, NULL)) != ERROR_SUCCESS) {
if ((ret = writer->write(pts, 4, NULL)) != ERROR_SUCCESS) {
return ret;
}

Expand Down Expand Up @@ -566,7 +556,7 @@ int SrsFlvEncoder::write_tags(SrsSharedPtrMessage** msgs, int count)
iovs += 3;
}

if ((ret = reader->writev(iovss, nb_iovss, NULL)) != ERROR_SUCCESS) {
if ((ret = writer->writev(iovss, nb_iovss, NULL)) != ERROR_SUCCESS) {
if (!srs_is_client_gracefully_close(ret)) {
srs_error("write flv tags failed. ret=%d", ret);
}
Expand Down Expand Up @@ -697,7 +687,7 @@ int SrsFlvEncoder::write_tag(char* header, int header_size, char* tag, int tag_s
iovs[2].iov_base = pre_size;
iovs[2].iov_len = SRS_FLV_PREVIOUS_TAG_SIZE;

if ((ret = reader->writev(iovs, 3, NULL)) != ERROR_SUCCESS) {
if ((ret = writer->writev(iovs, 3, NULL)) != ERROR_SUCCESS) {
if (!srs_is_client_gracefully_close(ret)) {
srs_error("write flv tag failed. ret=%d", ret);
}
Expand All @@ -718,21 +708,11 @@ SrsFlvDecoder::~SrsFlvDecoder()
srs_freep(tag_stream);
}

int SrsFlvDecoder::initialize(SrsFileReader* fr)
int SrsFlvDecoder::initialize(ISrsReader* fr)
{
int ret = ERROR_SUCCESS;

srs_assert(fr);

if (!fr->is_open()) {
ret = ERROR_KERNEL_FLV_STREAM_CLOSED;
srs_warn("stream is not open for decoder. ret=%d", ret);
return ret;
}

reader = fr;

return ret;
return ERROR_SUCCESS;
}

int SrsFlvDecoder::read_header(char header[9])
Expand Down Expand Up @@ -842,20 +822,24 @@ SrsFlvVodStreamDecoder::~SrsFlvVodStreamDecoder()
srs_freep(tag_stream);
}

int SrsFlvVodStreamDecoder::initialize(SrsFileReader* fr)
int SrsFlvVodStreamDecoder::initialize(ISrsReader* fr)
{
int ret = ERROR_SUCCESS;

srs_assert(fr);
reader = dynamic_cast<SrsFileReader*>(fr);
if (!reader) {
ret = ERROR_EXPECT_FILE_IO;
srs_error("stream is not file io. ret=%d", ret);
return ret;
}

if (!fr->is_open()) {
if (!reader->is_open()) {
ret = ERROR_KERNEL_FLV_STREAM_CLOSED;
srs_warn("stream is not open for decoder. ret=%d", ret);
return ret;
}

reader = fr;

return ret;
}

Expand Down
17 changes: 9 additions & 8 deletions trunk/src/kernel/srs_kernel_flv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#endif

class SrsBuffer;
class SrsFileWriter;
class ISrsWriter;
class ISrsReader;
class SrsFileReader;

#define SRS_FLV_TAG_HEADER_SIZE 11
Expand Down Expand Up @@ -442,7 +443,7 @@ class SrsSharedPtrMessage
class SrsFlvEncoder
{
private:
SrsFileWriter* reader;
ISrsWriter* writer;
private:
SrsBuffer* tag_stream;
char tag_header[SRS_FLV_TAG_HEADER_SIZE];
Expand All @@ -453,9 +454,9 @@ class SrsFlvEncoder
/**
* initialize the underlayer file stream.
* @remark user can initialize multiple times to encode multiple flv files.
* @remark, user must free the @param fr, flv encoder never close/free it.
* @remark, user must free the @param fw, flv encoder never close/free it.
*/
virtual int initialize(SrsFileWriter* fr);
virtual int initialize(ISrsWriter* fw);
public:
/**
* write flv header.
Expand Down Expand Up @@ -520,7 +521,7 @@ class SrsFlvEncoder
class SrsFlvDecoder
{
private:
SrsFileReader* reader;
ISrsReader* reader;
private:
SrsBuffer* tag_stream;
public:
Expand All @@ -530,9 +531,9 @@ class SrsFlvDecoder
/**
* initialize the underlayer file stream
* @remark user can initialize multiple times to decode multiple flv files.
* @remark user must free the @param fr, flv decoder never close/free it.
* @remark user must free the @param fr, flv decoder never close/free it
*/
virtual int initialize(SrsFileReader* fr);
virtual int initialize(ISrsReader* fr);
public:
/**
* read the flv header, donot including the 4bytes previous tag size.
Expand Down Expand Up @@ -576,7 +577,7 @@ class SrsFlvVodStreamDecoder
* @remark user can initialize multiple times to decode multiple flv files.
* @remark user must free the @param fr, flv decoder never close/free it.
*/
virtual int initialize(SrsFileReader* fr);
virtual int initialize(ISrsReader* fr);
public:
/**
* read the flv header and its size.
Expand Down

0 comments on commit 01d401c

Please sign in to comment.