winlin

fix #184, support AnnexB in RTMP body for HLS. 2.0.2

@@ -201,8 +201,9 @@ Supported operating systems and hardware: @@ -201,8 +201,9 @@ Supported operating systems and hardware:
201 * 2013-10-17, Created.<br/> 201 * 2013-10-17, Created.<br/>
202 202
203 ## History 203 ## History
204 -* v1.0, 2014-10-18, remove supports for OSX(darwin). 2.0.1.  
205 -* v1.0, 2014-10-16, revert github srs README to English. 2.0.0. 204 +* v2.0, 2014-10-19, fix [#184](https://github.com/winlinvip/simple-rtmp-server/issues/184), support AnnexB in RTMP body for HLS. 2.0.2
  205 +* v2.0, 2014-10-18, remove supports for OSX(darwin). 2.0.1.
  206 +* v2.0, 2014-10-16, revert github srs README to English. 2.0.0.
206 * <strong>v1.0, 2014-10-09, [1.0 beta(1.0.0)](https://github.com/winlinvip/simple-rtmp-server/releases/tag/1.0.beta) released. 59316 lines.</strong> 207 * <strong>v1.0, 2014-10-09, [1.0 beta(1.0.0)](https://github.com/winlinvip/simple-rtmp-server/releases/tag/1.0.beta) released. 59316 lines.</strong>
207 * v1.0, 2014-10-08, fix [#151](https://github.com/winlinvip/simple-rtmp-server/issues/151), always reap ts whatever audio or video packet. 0.9.223. 208 * v1.0, 2014-10-08, fix [#151](https://github.com/winlinvip/simple-rtmp-server/issues/151), always reap ts whatever audio or video packet. 0.9.223.
208 * v1.0, 2014-10-08, fix [#162](https://github.com/winlinvip/simple-rtmp-server/issues/162), failed if no epoll. 0.9.222. 209 * v1.0, 2014-10-08, fix [#162](https://github.com/winlinvip/simple-rtmp-server/issues/162), failed if no epoll. 0.9.222.
@@ -27,6 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -27,6 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <srs_kernel_log.hpp> 27 #include <srs_kernel_log.hpp>
28 #include <srs_kernel_stream.hpp> 28 #include <srs_kernel_stream.hpp>
29 #include <srs_protocol_amf0.hpp> 29 #include <srs_protocol_amf0.hpp>
  30 +#include <srs_app_utility.hpp>
30 31
31 SrsCodecSampleUnit::SrsCodecSampleUnit() 32 SrsCodecSampleUnit::SrsCodecSampleUnit()
32 { 33 {
@@ -358,6 +359,45 @@ int SrsAvcAacCodec::video_avc_demux(char* data, int size, SrsCodecSample* sample @@ -358,6 +359,45 @@ int SrsAvcAacCodec::video_avc_demux(char* data, int size, SrsCodecSample* sample
358 sample->avc_packet_type = (SrsCodecVideoAVCType)avc_packet_type; 359 sample->avc_packet_type = (SrsCodecVideoAVCType)avc_packet_type;
359 360
360 if (avc_packet_type == SrsCodecVideoAVCTypeSequenceHeader) { 361 if (avc_packet_type == SrsCodecVideoAVCTypeSequenceHeader) {
  362 + if ((ret = avc_demux_sps_pps(stream)) != ERROR_SUCCESS) {
  363 + return ret;
  364 + }
  365 + } else if (avc_packet_type == SrsCodecVideoAVCTypeNALU){
  366 + // ensure the sequence header demuxed
  367 + if (avc_extra_size <= 0 || !avc_extra_data) {
  368 + ret = ERROR_HLS_DECODE_ERROR;
  369 + srs_error("hls decode video avc failed, sequence header not found. ret=%d", ret);
  370 + return ret;
  371 + }
  372 +
  373 + // One or more NALUs (Full frames are required)
  374 + // try "AnnexB" from H.264-AVC-ISO_IEC_14496-10.pdf, page 211.
  375 + if ((ret = avc_demux_annexb_format(stream, sample)) != ERROR_SUCCESS) {
  376 + // stop try when system error.
  377 + if (ret != ERROR_HLS_AVC_TRY_OTHERS) {
  378 + srs_error("avc demux for annexb failed. ret=%d", ret);
  379 + return ret;
  380 + }
  381 +
  382 + // try "ISO Base Media File Format" from H.264-AVC-ISO_IEC_14496-15.pdf, page 20
  383 + if ((ret = avc_demux_ibmf_format(stream, sample)) != ERROR_SUCCESS) {
  384 + return ret;
  385 + }
  386 + }
  387 + } else {
  388 + // ignored.
  389 + }
  390 +
  391 + srs_info("video decoded, type=%d, codec=%d, avc=%d, time=%d, size=%d",
  392 + frame_type, video_codec_id, avc_packet_type, composition_time, size);
  393 +
  394 + return ret;
  395 +}
  396 +
  397 +int SrsAvcAacCodec::avc_demux_sps_pps(SrsStream* stream)
  398 +{
  399 + int ret = ERROR_SUCCESS;
  400 +
361 // AVCDecoderConfigurationRecord 401 // AVCDecoderConfigurationRecord
362 // 5.2.4.1.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 16 402 // 5.2.4.1.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 16
363 avc_extra_size = stream->size() - stream->pos(); 403 avc_extra_size = stream->size() - stream->pos();
@@ -386,6 +426,17 @@ int SrsAvcAacCodec::video_avc_demux(char* data, int size, SrsCodecSample* sample @@ -386,6 +426,17 @@ int SrsAvcAacCodec::video_avc_demux(char* data, int size, SrsCodecSample* sample
386 lengthSizeMinusOne &= 0x03; 426 lengthSizeMinusOne &= 0x03;
387 NAL_unit_length = lengthSizeMinusOne; 427 NAL_unit_length = lengthSizeMinusOne;
388 428
  429 + // 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 16
  430 + // 5.2.4.1 AVC decoder configuration record
  431 + // 5.2.4.1.2 Semantics
  432 + // The value of this field shall be one of 0, 1, or 3 corresponding to a
  433 + // length encoded with 1, 2, or 4 bytes, respectively.
  434 + if (NAL_unit_length == 2) {
  435 + ret = ERROR_HLS_DECODE_ERROR;
  436 + srs_error("sps lengthSizeMinusOne should never be 2. ret=%d", ret);
  437 + return ret;
  438 + }
  439 +
389 // 1 sps 440 // 1 sps
390 if (!stream->require(1)) { 441 if (!stream->require(1)) {
391 ret = ERROR_HLS_DECODE_ERROR; 442 ret = ERROR_HLS_DECODE_ERROR;
@@ -446,18 +497,79 @@ int SrsAvcAacCodec::video_avc_demux(char* data, int size, SrsCodecSample* sample @@ -446,18 +497,79 @@ int SrsAvcAacCodec::video_avc_demux(char* data, int size, SrsCodecSample* sample
446 memcpy(pictureParameterSetNALUnit, stream->data() + stream->pos(), pictureParameterSetLength); 497 memcpy(pictureParameterSetNALUnit, stream->data() + stream->pos(), pictureParameterSetLength);
447 stream->skip(pictureParameterSetLength); 498 stream->skip(pictureParameterSetLength);
448 } 499 }
449 - } else if (avc_packet_type == SrsCodecVideoAVCTypeNALU){  
450 - // ensure the sequence header demuxed  
451 - if (avc_extra_size <= 0 || !avc_extra_data) {  
452 - ret = ERROR_HLS_DECODE_ERROR;  
453 - srs_error("hls decode video avc failed, sequence header not found. ret=%d", ret); 500 +
454 return ret; 501 return ret;
  502 +}
  503 +
  504 +int SrsAvcAacCodec::avc_demux_annexb_format(SrsStream* stream, SrsCodecSample* sample)
  505 +{
  506 + int ret = ERROR_SUCCESS;
  507 +
  508 + // not annexb, try others
  509 + if (!srs_avc_startswith_annexb(stream, NULL)) {
  510 + return ERROR_HLS_AVC_TRY_OTHERS;
455 } 511 }
456 512
457 - // One or more NALUs (Full frames are required)  
458 - // 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 20 513 + // AnnexB
  514 + // B.1.1 Byte stream NAL unit syntax,
  515 + // H.264-AVC-ISO_IEC_14496-10.pdf, page 211.
  516 + while (!stream->empty()) {
  517 + // find start code
  518 + int nb_start_code = 0;
  519 + if (!srs_avc_startswith_annexb(stream, &nb_start_code)) {
  520 + return ret;
  521 + }
  522 +
  523 + // skip the start code.
  524 + if (nb_start_code > 0) {
  525 + stream->skip(nb_start_code);
  526 + }
  527 +
  528 + // the NALU start bytes.
  529 + char* p = stream->data() + stream->pos();
  530 +
  531 + // get the last matched NALU
  532 + while (!stream->empty()) {
  533 + if (srs_avc_startswith_annexb(stream, &nb_start_code)) {
  534 + break;
  535 + }
  536 +
  537 + stream->skip(1);
  538 + }
  539 +
  540 + char* pp = stream->data() + stream->pos();
  541 +
  542 + // skip the empty.
  543 + if (pp - p <= 0) {
  544 + continue;
  545 + }
  546 +
  547 + // got the NALU.
  548 + if ((ret = sample->add_sample_unit(p, pp - p)) != ERROR_SUCCESS) {
  549 + srs_error("annexb add video sample failed. ret=%d", ret);
  550 + return ret;
  551 + }
  552 + }
  553 +
  554 + return ret;
  555 +}
  556 +
  557 +int SrsAvcAacCodec::avc_demux_ibmf_format(SrsStream* stream, SrsCodecSample* sample)
  558 +{
  559 + int ret = ERROR_SUCCESS;
  560 +
459 int PictureLength = stream->size() - stream->pos(); 561 int PictureLength = stream->size() - stream->pos();
  562 +
  563 + // 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 16
  564 + // 5.2.4.1 AVC decoder configuration record
  565 + // 5.2.4.1.2 Semantics
  566 + // The value of this field shall be one of 0, 1, or 3 corresponding to a
  567 + // length encoded with 1, 2, or 4 bytes, respectively.
  568 + srs_assert(NAL_unit_length != 2);
  569 +
  570 + // 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 20
460 for (int i = 0; i < PictureLength;) { 571 for (int i = 0; i < PictureLength;) {
  572 + // unsigned int((NAL_unit_length+1)*8) NALUnitLength;
461 if (!stream->require(NAL_unit_length + 1)) { 573 if (!stream->require(NAL_unit_length + 1)) {
462 ret = ERROR_HLS_DECODE_ERROR; 574 ret = ERROR_HLS_DECODE_ERROR;
463 srs_error("hls decode video avc NALU size failed. ret=%d", ret); 575 srs_error("hls decode video avc NALU size failed. ret=%d", ret);
@@ -466,13 +578,20 @@ int SrsAvcAacCodec::video_avc_demux(char* data, int size, SrsCodecSample* sample @@ -466,13 +578,20 @@ int SrsAvcAacCodec::video_avc_demux(char* data, int size, SrsCodecSample* sample
466 int32_t NALUnitLength = 0; 578 int32_t NALUnitLength = 0;
467 if (NAL_unit_length == 3) { 579 if (NAL_unit_length == 3) {
468 NALUnitLength = stream->read_4bytes(); 580 NALUnitLength = stream->read_4bytes();
469 - } else if (NAL_unit_length == 2) {  
470 - NALUnitLength = stream->read_3bytes();  
471 } else if (NAL_unit_length == 1) { 581 } else if (NAL_unit_length == 1) {
472 NALUnitLength = stream->read_2bytes(); 582 NALUnitLength = stream->read_2bytes();
473 } else { 583 } else {
474 NALUnitLength = stream->read_1bytes(); 584 NALUnitLength = stream->read_1bytes();
475 } 585 }
  586 +
  587 + // maybe stream is invalid format.
  588 + // see: https://github.com/winlinvip/simple-rtmp-server/issues/183
  589 + if (NALUnitLength < 0) {
  590 + ret = ERROR_HLS_DECODE_ERROR;
  591 + srs_error("maybe stream is AnnexB format. ret=%d", ret);
  592 + return ret;
  593 + }
  594 +
476 // NALUnit 595 // NALUnit
477 if (!stream->require(NALUnitLength)) { 596 if (!stream->require(NALUnitLength)) {
478 ret = ERROR_HLS_DECODE_ERROR; 597 ret = ERROR_HLS_DECODE_ERROR;
@@ -488,12 +607,6 @@ int SrsAvcAacCodec::video_avc_demux(char* data, int size, SrsCodecSample* sample @@ -488,12 +607,6 @@ int SrsAvcAacCodec::video_avc_demux(char* data, int size, SrsCodecSample* sample
488 607
489 i += NAL_unit_length + 1 + NALUnitLength; 608 i += NAL_unit_length + 1 + NALUnitLength;
490 } 609 }
491 - } else {  
492 - // ignored.  
493 - }  
494 -  
495 - srs_info("video decoded, type=%d, codec=%d, avc=%d, time=%d, size=%d",  
496 - frame_type, video_codec_id, avc_packet_type, composition_time, size);  
497 610
498 return ret; 611 return ret;
499 } 612 }
@@ -278,6 +278,22 @@ public: @@ -278,6 +278,22 @@ public:
278 * demux the h.264 NALUs to sampe units. 278 * demux the h.264 NALUs to sampe units.
279 */ 279 */
280 virtual int video_avc_demux(char* data, int size, SrsCodecSample* sample); 280 virtual int video_avc_demux(char* data, int size, SrsCodecSample* sample);
  281 +private:
  282 + /**
  283 + * when avc packet type is SrsCodecVideoAVCTypeSequenceHeader,
  284 + * decode the sps and pps.
  285 + */
  286 + virtual int avc_demux_sps_pps(SrsStream* stream);
  287 + /**
  288 + * demux the avc NALU in "AnnexB"
  289 + * from H.264-AVC-ISO_IEC_14496-10.pdf, page 211.
  290 + */
  291 + virtual int avc_demux_annexb_format(SrsStream* stream, SrsCodecSample* sample);
  292 + /**
  293 + * demux the avc NALU in "ISO Base Media File Format"
  294 + * from H.264-AVC-ISO_IEC_14496-15.pdf, page 20
  295 + */
  296 + virtual int avc_demux_ibmf_format(SrsStream* stream, SrsCodecSample* sample);
281 }; 297 };
282 298
283 #endif 299 #endif
@@ -36,8 +36,9 @@ using namespace std; @@ -36,8 +36,9 @@ using namespace std;
36 #include <srs_kernel_error.hpp> 36 #include <srs_kernel_error.hpp>
37 #include <srs_app_kbps.hpp> 37 #include <srs_app_kbps.hpp>
38 #include <srs_app_json.hpp> 38 #include <srs_app_json.hpp>
  39 +#include <srs_kernel_stream.hpp>
39 40
40 -int srs_socket_connect(std::string server, int port, int64_t timeout, st_netfd_t* pstfd) 41 +int srs_socket_connect(string server, int port, int64_t timeout, st_netfd_t* pstfd)
41 { 42 {
42 int ret = ERROR_SUCCESS; 43 int ret = ERROR_SUCCESS;
43 44
@@ -89,7 +90,7 @@ failed: @@ -89,7 +90,7 @@ failed:
89 return ret; 90 return ret;
90 } 91 }
91 92
92 -int srs_get_log_level(std::string level) 93 +int srs_get_log_level(string level)
93 { 94 {
94 if ("verbose" == level) { 95 if ("verbose" == level) {
95 return SrsLogLevel::Verbose; 96 return SrsLogLevel::Verbose;
@@ -106,6 +107,35 @@ int srs_get_log_level(std::string level) @@ -106,6 +107,35 @@ int srs_get_log_level(std::string level)
106 } 107 }
107 } 108 }
108 109
  110 +bool srs_avc_startswith_annexb(SrsStream* stream, int* pnb_start_code)
  111 +{
  112 + char* bytes = stream->data() + stream->pos();
  113 + char* p = bytes;
  114 +
  115 + for (;;) {
  116 + if (!stream->require(p - bytes + 3)) {
  117 + return false;
  118 + }
  119 +
  120 + // not match
  121 + if (p[0] != 0x00 || p[1] != 0x00) {
  122 + return false;
  123 + }
  124 +
  125 + // match N[00] 00 00 01, where N>=0
  126 + if (p[2] == 0x01) {
  127 + if (pnb_start_code) {
  128 + *pnb_start_code = (int)(p - bytes) + 3;
  129 + }
  130 + return true;
  131 + }
  132 +
  133 + p++;
  134 + }
  135 +
  136 + return false;
  137 +}
  138 +
109 static SrsRusage _srs_system_rusage; 139 static SrsRusage _srs_system_rusage;
110 140
111 SrsRusage::SrsRusage() 141 SrsRusage::SrsRusage()
@@ -39,6 +39,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -39,6 +39,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 #include <srs_app_st.hpp> 39 #include <srs_app_st.hpp>
40 40
41 class SrsKbps; 41 class SrsKbps;
  42 +class SrsStream;
42 43
43 // client open socket and connect to server. 44 // client open socket and connect to server.
44 extern int srs_socket_connect(std::string server, int port, int64_t timeout, st_netfd_t* pstfd); 45 extern int srs_socket_connect(std::string server, int port, int64_t timeout, st_netfd_t* pstfd);
@@ -49,6 +50,15 @@ extern int srs_socket_connect(std::string server, int port, int64_t timeout, st_ @@ -49,6 +50,15 @@ extern int srs_socket_connect(std::string server, int port, int64_t timeout, st_
49 */ 50 */
50 extern int srs_get_log_level(std::string level); 51 extern int srs_get_log_level(std::string level);
51 52
  53 +/**
  54 +* whether stream starts with the avc NALU in "AnnexB"
  55 +* from H.264-AVC-ISO_IEC_14496-10.pdf, page 211.
  56 +* start code must be "N[00] 00 00 01" where N>=0
  57 +* @param pnb_start_code output the size of start code, must >=3.
  58 +* NULL to ignore.
  59 +*/
  60 +extern bool srs_avc_startswith_annexb(SrsStream* stream, int* pnb_start_code = NULL);
  61 +
52 // current process resouce usage. 62 // current process resouce usage.
53 // @see: man getrusage 63 // @see: man getrusage
54 class SrsRusage 64 class SrsRusage
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR "2" 32 #define VERSION_MAJOR "2"
33 #define VERSION_MINOR "0" 33 #define VERSION_MINOR "0"
34 -#define VERSION_REVISION "1" 34 +#define VERSION_REVISION "2"
35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION 35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "SRS" 37 #define RTMP_SIG_SRS_KEY "SRS"
@@ -183,6 +183,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -183,6 +183,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
183 #define ERROR_KERNEL_FLV_STREAM_CLOSED 3037 183 #define ERROR_KERNEL_FLV_STREAM_CLOSED 3037
184 #define ERROR_KERNEL_STREAM_INIT 3038 184 #define ERROR_KERNEL_STREAM_INIT 3038
185 #define ERROR_EDGE_VHOST_REMOVED 3039 185 #define ERROR_EDGE_VHOST_REMOVED 3039
  186 +#define ERROR_HLS_AVC_TRY_OTHERS 3040
186 187
187 /** 188 /**
188 * whether the error code is an system control error. 189 * whether the error code is an system control error.