winlin

donot mix the read and write for librtmp

@@ -121,14 +121,14 @@ int process(const char* in_flv_file, const char* out_flv_file, srs_flv_t* pic, s @@ -121,14 +121,14 @@ int process(const char* in_flv_file, const char* out_flv_file, srs_flv_t* pic, s
121 srs_amf0_t amf0_data = NULL; 121 srs_amf0_t amf0_data = NULL;
122 srs_amf0_t filepositions = NULL; 122 srs_amf0_t filepositions = NULL;
123 123
124 - if ((ic = srs_flv_open(in_flv_file)) == NULL) { 124 + if ((ic = srs_flv_open_read(in_flv_file)) == NULL) {
125 ret = 2; 125 ret = 2;
126 trace("open input flv file failed. ret=%d", ret); 126 trace("open input flv file failed. ret=%d", ret);
127 return ret; 127 return ret;
128 } 128 }
129 *pic = ic; 129 *pic = ic;
130 130
131 - if ((oc = srs_flv_open(out_flv_file)) == NULL) { 131 + if ((oc = srs_flv_open_write(out_flv_file)) == NULL) {
132 ret = 2; 132 ret = 2;
133 trace("open output flv file failed. ret=%d", ret); 133 trace("open output flv file failed. ret=%d", ret);
134 return ret; 134 return ret;
@@ -64,7 +64,7 @@ int main(int argc, char** argv) @@ -64,7 +64,7 @@ int main(int argc, char** argv)
64 trace("version: %d.%d.%d", srs_version_major(), srs_version_minor(), srs_version_revision()); 64 trace("version: %d.%d.%d", srs_version_major(), srs_version_minor(), srs_version_revision());
65 trace("input: %s", in_flv_file); 65 trace("input: %s", in_flv_file);
66 66
67 - if ((flv = srs_flv_open(in_flv_file)) == NULL) { 67 + if ((flv = srs_flv_open_read(in_flv_file)) == NULL) {
68 ret = 2; 68 ret = 2;
69 trace("open flv file failed. ret=%d", ret); 69 trace("open flv file failed. ret=%d", ret);
70 return ret; 70 return ret;
@@ -93,7 +93,7 @@ int main(int argc, char** argv) @@ -93,7 +93,7 @@ int main(int argc, char** argv)
93 trace("input: %s", in_flv_file); 93 trace("input: %s", in_flv_file);
94 trace("output: %s", out_rtmp_url); 94 trace("output: %s", out_rtmp_url);
95 95
96 - if ((flv = srs_flv_open(in_flv_file)) == NULL) { 96 + if ((flv = srs_flv_open_read(in_flv_file)) == NULL) {
97 ret = 2; 97 ret = 2;
98 trace("open flv file failed. ret=%d", ret); 98 trace("open flv file failed. ret=%d", ret);
99 return ret; 99 return ret;
@@ -120,6 +120,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -120,6 +120,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
120 #define ERROR_SYSTEM_CREATE_PIPE 430 120 #define ERROR_SYSTEM_CREATE_PIPE 430
121 #define ERROR_SYSTEM_FILE_SEEK 431 121 #define ERROR_SYSTEM_FILE_SEEK 431
122 #define ERROR_SYSTEM_FLV_HEADER 432 122 #define ERROR_SYSTEM_FLV_HEADER 432
  123 +#define ERROR_SYSTEM_IO_INVALID 433
123 124
124 // see librtmp. 125 // see librtmp.
125 // failed when open ssl create the dh 126 // failed when open ssl create the dh
@@ -165,6 +165,11 @@ void SrsFileReader::close() @@ -165,6 +165,11 @@ void SrsFileReader::close()
165 return; 165 return;
166 } 166 }
167 167
  168 +bool SrsFileReader::is_open()
  169 +{
  170 + return fd > 0;
  171 +}
  172 +
168 int64_t SrsFileReader::tellg() 173 int64_t SrsFileReader::tellg()
169 { 174 {
170 return (int64_t)::lseek(fd, 0, SEEK_CUR); 175 return (int64_t)::lseek(fd, 0, SEEK_CUR);
@@ -69,6 +69,7 @@ public: @@ -69,6 +69,7 @@ public:
69 virtual int open(std::string file); 69 virtual int open(std::string file);
70 virtual void close(); 70 virtual void close();
71 public: 71 public:
  72 + virtual bool is_open();
72 virtual int64_t tellg(); 73 virtual int64_t tellg();
73 virtual void skip(int64_t size); 74 virtual void skip(int64_t size);
74 virtual int64_t lseek(int64_t offset); 75 virtual int64_t lseek(int64_t offset);
@@ -467,28 +467,37 @@ struct FlvContext @@ -467,28 +467,37 @@ struct FlvContext
467 SrsFlvDecoder dec; 467 SrsFlvDecoder dec;
468 }; 468 };
469 469
470 -srs_flv_t srs_flv_open(const char* file) 470 +srs_flv_t srs_flv_open_read(const char* file)
471 { 471 {
472 int ret = ERROR_SUCCESS; 472 int ret = ERROR_SUCCESS;
473 473
474 FlvContext* flv = new FlvContext(); 474 FlvContext* flv = new FlvContext();
475 475
476 - if ((ret = flv->writer.open(file)) != ERROR_SUCCESS) { 476 + if ((ret = flv->reader.open(file)) != ERROR_SUCCESS) {
477 srs_freep(flv); 477 srs_freep(flv);
478 return NULL; 478 return NULL;
479 } 479 }
480 480
481 - if ((ret = flv->enc.initialize(&flv->writer)) != ERROR_SUCCESS) { 481 + if ((ret = flv->dec.initialize(&flv->reader)) != ERROR_SUCCESS) {
482 srs_freep(flv); 482 srs_freep(flv);
483 return NULL; 483 return NULL;
484 } 484 }
485 485
486 - if ((ret = flv->reader.open(file)) != ERROR_SUCCESS) { 486 + return flv;
  487 +}
  488 +
  489 +srs_flv_t srs_flv_open_write(const char* file)
  490 +{
  491 + int ret = ERROR_SUCCESS;
  492 +
  493 + FlvContext* flv = new FlvContext();
  494 +
  495 + if ((ret = flv->writer.open(file)) != ERROR_SUCCESS) {
487 srs_freep(flv); 496 srs_freep(flv);
488 return NULL; 497 return NULL;
489 } 498 }
490 499
491 - if ((ret = flv->dec.initialize(&flv->reader)) != ERROR_SUCCESS) { 500 + if ((ret = flv->enc.initialize(&flv->writer)) != ERROR_SUCCESS) {
492 srs_freep(flv); 501 srs_freep(flv);
493 return NULL; 502 return NULL;
494 } 503 }
@@ -507,6 +516,11 @@ int srs_flv_read_header(srs_flv_t flv, char header[9]) @@ -507,6 +516,11 @@ int srs_flv_read_header(srs_flv_t flv, char header[9])
507 int ret = ERROR_SUCCESS; 516 int ret = ERROR_SUCCESS;
508 517
509 FlvContext* context = (FlvContext*)flv; 518 FlvContext* context = (FlvContext*)flv;
  519 +
  520 + if (!context->reader.is_open()) {
  521 + return ERROR_SYSTEM_IO_INVALID;
  522 + }
  523 +
510 if ((ret = context->dec.read_header(header)) != ERROR_SUCCESS) { 524 if ((ret = context->dec.read_header(header)) != ERROR_SUCCESS) {
511 return ret; 525 return ret;
512 } 526 }
@@ -524,6 +538,11 @@ int srs_flv_read_tag_header(srs_flv_t flv, char* ptype, int32_t* pdata_size, u_i @@ -524,6 +538,11 @@ int srs_flv_read_tag_header(srs_flv_t flv, char* ptype, int32_t* pdata_size, u_i
524 int ret = ERROR_SUCCESS; 538 int ret = ERROR_SUCCESS;
525 539
526 FlvContext* context = (FlvContext*)flv; 540 FlvContext* context = (FlvContext*)flv;
  541 +
  542 + if (!context->reader.is_open()) {
  543 + return ERROR_SYSTEM_IO_INVALID;
  544 + }
  545 +
527 if ((ret = context->dec.read_tag_header(ptype, pdata_size, ptime)) != ERROR_SUCCESS) { 546 if ((ret = context->dec.read_tag_header(ptype, pdata_size, ptime)) != ERROR_SUCCESS) {
528 return ret; 547 return ret;
529 } 548 }
@@ -536,6 +555,11 @@ int srs_flv_read_tag_data(srs_flv_t flv, char* data, int32_t size) @@ -536,6 +555,11 @@ int srs_flv_read_tag_data(srs_flv_t flv, char* data, int32_t size)
536 int ret = ERROR_SUCCESS; 555 int ret = ERROR_SUCCESS;
537 556
538 FlvContext* context = (FlvContext*)flv; 557 FlvContext* context = (FlvContext*)flv;
  558 +
  559 + if (!context->reader.is_open()) {
  560 + return ERROR_SYSTEM_IO_INVALID;
  561 + }
  562 +
539 if ((ret = context->dec.read_tag_data(data, size)) != ERROR_SUCCESS) { 563 if ((ret = context->dec.read_tag_data(data, size)) != ERROR_SUCCESS) {
540 return ret; 564 return ret;
541 } 565 }
@@ -553,6 +577,11 @@ int srs_flv_write_header(srs_flv_t flv, char header[9]) @@ -553,6 +577,11 @@ int srs_flv_write_header(srs_flv_t flv, char header[9])
553 int ret = ERROR_SUCCESS; 577 int ret = ERROR_SUCCESS;
554 578
555 FlvContext* context = (FlvContext*)flv; 579 FlvContext* context = (FlvContext*)flv;
  580 +
  581 + if (!context->writer.is_open()) {
  582 + return ERROR_SYSTEM_IO_INVALID;
  583 + }
  584 +
556 if ((ret = context->enc.write_header(header)) != ERROR_SUCCESS) { 585 if ((ret = context->enc.write_header(header)) != ERROR_SUCCESS) {
557 return ret; 586 return ret;
558 } 587 }
@@ -565,6 +594,11 @@ int srs_flv_write_tag(srs_flv_t flv, char type, int32_t time, char* data, int si @@ -565,6 +594,11 @@ int srs_flv_write_tag(srs_flv_t flv, char type, int32_t time, char* data, int si
565 int ret = ERROR_SUCCESS; 594 int ret = ERROR_SUCCESS;
566 595
567 FlvContext* context = (FlvContext*)flv; 596 FlvContext* context = (FlvContext*)flv;
  597 +
  598 + if (!context->writer.is_open()) {
  599 + return ERROR_SYSTEM_IO_INVALID;
  600 + }
  601 +
568 if (type == SRS_RTMP_TYPE_AUDIO) { 602 if (type == SRS_RTMP_TYPE_AUDIO) {
569 return context->enc.write_audio(time, data, size); 603 return context->enc.write_audio(time, data, size);
570 } else if (type == SRS_RTMP_TYPE_VIDEO) { 604 } else if (type == SRS_RTMP_TYPE_VIDEO) {
@@ -166,7 +166,8 @@ int64_t srs_get_nrecv_bytes(srs_rtmp_t rtmp); @@ -166,7 +166,8 @@ int64_t srs_get_nrecv_bytes(srs_rtmp_t rtmp);
166 typedef void* srs_flv_t; 166 typedef void* srs_flv_t;
167 typedef int flv_bool; 167 typedef int flv_bool;
168 /* open flv file for both read/write. */ 168 /* open flv file for both read/write. */
169 -srs_flv_t srs_flv_open(const char* file); 169 +srs_flv_t srs_flv_open_read(const char* file);
  170 +srs_flv_t srs_flv_open_write(const char* file);
170 void srs_flv_close(srs_flv_t flv); 171 void srs_flv_close(srs_flv_t flv);
171 /* read the flv header. 9bytes header. drop the 4bytes zero previous tag size */ 172 /* read the flv header. 9bytes header. drop the 4bytes zero previous tag size */
172 int srs_flv_read_header(srs_flv_t flv, char header[9]); 173 int srs_flv_read_header(srs_flv_t flv, char header[9]);