winlin

Merge branch '2.0release' into develop

@@ -1070,11 +1070,6 @@ int SrsHlsCache::write_video(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t @@ -1070,11 +1070,6 @@ int SrsHlsCache::write_video(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t
1070 // a. wait keyframe and got keyframe. 1070 // a. wait keyframe and got keyframe.
1071 // b. always reap when not wait keyframe. 1071 // b. always reap when not wait keyframe.
1072 if (!muxer->wait_keyframe() || sample->frame_type == SrsCodecVideoAVCFrameKeyFrame) { 1072 if (!muxer->wait_keyframe() || sample->frame_type == SrsCodecVideoAVCFrameKeyFrame) {
1073 - // when wait keyframe, there must exists idr frame in sample, or NonIDR(open gop) specified.  
1074 - if (!sample->open_gop && !sample->has_idr && muxer->wait_keyframe()) {  
1075 - srs_warn("hls: ts starts without IDR, first nalu=%d, idr=%d", sample->first_nalu_type, sample->has_idr);  
1076 - }  
1077 -  
1078 // reap the segment, which will also flush the video. 1073 // reap the segment, which will also flush the video.
1079 if ((ret = reap_segment("video", muxer, cache->video->dts)) != ERROR_SUCCESS) { 1074 if ((ret = reap_segment("video", muxer, cache->video->dts)) != ERROR_SUCCESS) {
1080 return ret; 1075 return ret;
@@ -1271,8 +1266,6 @@ void SrsHls::on_unpublish() @@ -1271,8 +1266,6 @@ void SrsHls::on_unpublish()
1271 srs_error("ignore m3u8 muxer flush/close audio failed. ret=%d", ret); 1266 srs_error("ignore m3u8 muxer flush/close audio failed. ret=%d", ret);
1272 } 1267 }
1273 1268
1274 - sample->reset();  
1275 -  
1276 hls_enabled = false; 1269 hls_enabled = false;
1277 } 1270 }
1278 1271
@@ -324,20 +324,13 @@ SrsCodecSampleUnit::~SrsCodecSampleUnit() @@ -324,20 +324,13 @@ SrsCodecSampleUnit::~SrsCodecSampleUnit()
324 324
325 SrsCodecSample::SrsCodecSample() 325 SrsCodecSample::SrsCodecSample()
326 { 326 {
327 - reset(); 327 + clear();
328 } 328 }
329 329
330 SrsCodecSample::~SrsCodecSample() 330 SrsCodecSample::~SrsCodecSample()
331 { 331 {
332 } 332 }
333 333
334 -void SrsCodecSample::reset()  
335 -{  
336 - clear();  
337 -  
338 - open_gop = false;  
339 -}  
340 -  
341 void SrsCodecSample::clear() 334 void SrsCodecSample::clear()
342 { 335 {
343 is_video = false; 336 is_video = false;
@@ -346,7 +339,7 @@ void SrsCodecSample::clear() @@ -346,7 +339,7 @@ void SrsCodecSample::clear()
346 cts = 0; 339 cts = 0;
347 frame_type = SrsCodecVideoAVCFrameReserved; 340 frame_type = SrsCodecVideoAVCFrameReserved;
348 avc_packet_type = SrsCodecVideoAVCTypeReserved; 341 avc_packet_type = SrsCodecVideoAVCTypeReserved;
349 - has_aud = has_idr = false; 342 + has_sps_pps = has_aud = has_idr = false;
350 first_nalu_type = SrsAvcNaluTypeReserved; 343 first_nalu_type = SrsAvcNaluTypeReserved;
351 344
352 acodec = SrsCodecAudioReserved1; 345 acodec = SrsCodecAudioReserved1;
@@ -377,6 +370,8 @@ int SrsCodecSample::add_sample_unit(char* bytes, int size) @@ -377,6 +370,8 @@ int SrsCodecSample::add_sample_unit(char* bytes, int size)
377 370
378 if (nal_unit_type == SrsAvcNaluTypeIDR) { 371 if (nal_unit_type == SrsAvcNaluTypeIDR) {
379 has_idr = true; 372 has_idr = true;
  373 + } else if (nal_unit_type == SrsAvcNaluTypeSPS || nal_unit_type == SrsAvcNaluTypePPS) {
  374 + has_sps_pps = true;
380 } else if (nal_unit_type == SrsAvcNaluTypeAccessUnitDelimiter) { 375 } else if (nal_unit_type == SrsAvcNaluTypeAccessUnitDelimiter) {
381 has_aud = true; 376 has_aud = true;
382 } 377 }
@@ -773,11 +768,6 @@ int SrsAvcAacCodec::video_nalu_demux(SrsBuffer* stream, SrsCodecSample* sample) @@ -773,11 +768,6 @@ int SrsAvcAacCodec::video_nalu_demux(SrsBuffer* stream, SrsCodecSample* sample)
773 srs_info("hls decode avc payload in annexb format."); 768 srs_info("hls decode avc payload in annexb format.");
774 } 769 }
775 770
776 - // for keyframe, but not IDR, it's open gop.  
777 - if (sample->frame_type == SrsCodecVideoAVCFrameKeyFrame && !sample->has_idr) {  
778 - sample->open_gop = true;  
779 - }  
780 -  
781 return ret; 771 return ret;
782 } 772 }
783 773
@@ -387,13 +387,11 @@ public: @@ -387,13 +387,11 @@ public:
387 bool has_idr; 387 bool has_idr;
388 // Whether exists AUD NALU. 388 // Whether exists AUD NALU.
389 bool has_aud; 389 bool has_aud;
  390 + // Whether exists SPS/PPS NALU.
  391 + bool has_sps_pps;
390 // The first nalu type. 392 // The first nalu type.
391 SrsAvcNaluType first_nalu_type; 393 SrsAvcNaluType first_nalu_type;
392 public: 394 public:
393 - // Whether stream is open gop, which means the keyframe is not IDR but NonIDR.  
394 - // @remark we will identify whether stream is open-gop util reset.  
395 - bool open_gop;  
396 -public:  
397 // audio specified 395 // audio specified
398 SrsCodecAudio acodec; 396 SrsCodecAudio acodec;
399 // audio aac specified. 397 // audio aac specified.
@@ -405,8 +403,6 @@ public: @@ -405,8 +403,6 @@ public:
405 SrsCodecSample(); 403 SrsCodecSample();
406 virtual ~SrsCodecSample(); 404 virtual ~SrsCodecSample();
407 public: 405 public:
408 - // Reset the sample, clear the sample-base and stream-base data.  
409 - void reset();  
410 /** 406 /**
411 * clear all samples. 407 * clear all samples.
412 * the sample units never copy the bytes, it directly use the ptr, 408 * the sample units never copy the bytes, it directly use the ptr,
@@ -2939,10 +2939,8 @@ int SrsTsCache::do_cache_aac(SrsAvcAacCodec* codec, SrsCodecSample* sample) @@ -2939,10 +2939,8 @@ int SrsTsCache::do_cache_aac(SrsAvcAacCodec* codec, SrsCodecSample* sample)
2939 return ret; 2939 return ret;
2940 } 2940 }
2941 2941
2942 -int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample) 2942 +void srs_avc_insert_aud(SrsSimpleBuffer* payload, bool& aud_inserted)
2943 { 2943 {
2944 - int ret = ERROR_SUCCESS;  
2945 -  
2946 // mux the samples in annexb format, 2944 // mux the samples in annexb format,
2947 // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 324. 2945 // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 324.
2948 /** 2946 /**
@@ -2989,84 +2987,56 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample) @@ -2989,84 +2987,56 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample)
2989 static u_int8_t fresh_nalu_header[] = { 0x00, 0x00, 0x00, 0x01 }; 2987 static u_int8_t fresh_nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
2990 static u_int8_t cont_nalu_header[] = { 0x00, 0x00, 0x01 }; 2988 static u_int8_t cont_nalu_header[] = { 0x00, 0x00, 0x01 };
2991 2989
2992 - // the aud(access unit delimiter) before each frame.  
2993 - // 7.3.2.4 Access unit delimiter RBSP syntax  
2994 - // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 66.  
2995 - //  
2996 - // primary_pic_type u(3), the first 3bits, primary_pic_type indicates that the slice_type values  
2997 - // for all slices of the primary coded picture are members of the set listed in Table 7-5 for  
2998 - // the given value of primary_pic_type.  
2999 - // 0, slice_type 2, 7  
3000 - // 1, slice_type 0, 2, 5, 7  
3001 - // 2, slice_type 0, 1, 2, 5, 6, 7  
3002 - // 3, slice_type 4, 9  
3003 - // 4, slice_type 3, 4, 8, 9  
3004 - // 5, slice_type 2, 4, 7, 9  
3005 - // 6, slice_type 0, 2, 3, 4, 5, 7, 8, 9  
3006 - // 7, slice_type 0, 1, 2, 3, 4, 5, 6, 7, 8, 9  
3007 - // 7.4.2.4 Access unit delimiter RBSP semantics  
3008 - // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 102.  
3009 - //  
3010 - // slice_type specifies the coding type of the slice according to Table 7-6.  
3011 - // 0, P (P slice)  
3012 - // 1, B (B slice)  
3013 - // 2, I (I slice)  
3014 - // 3, SP (SP slice)  
3015 - // 4, SI (SI slice)  
3016 - // 5, P (P slice)  
3017 - // 6, B (B slice)  
3018 - // 7, I (I slice)  
3019 - // 8, SP (SP slice)  
3020 - // 9, SI (SI slice)  
3021 - // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 105.  
3022 - static u_int8_t aud_nalu_7[] = { 0x09, 0xf0};  
3023 -  
3024 - // For NonIDR(open gop), we directly appends all frames.  
3025 - if (sample->open_gop) {  
3026 - for (int i = 0; i < sample->nb_sample_units; i++) {  
3027 - SrsCodecSampleUnit* sample_unit = &sample->sample_units[i];  
3028 - int32_t size = sample_unit->size;  
3029 -  
3030 - if (!sample_unit->bytes || size <= 0) {  
3031 - ret = ERROR_HLS_AVC_SAMPLE_SIZE;  
3032 - srs_error("invalid avc sample length=%d, ret=%d", size, ret);  
3033 - return ret;  
3034 - }  
3035 -  
3036 - // insert nalu header before rbsp.  
3037 - if (i == 0) {  
3038 - video->payload->append((const char*)fresh_nalu_header, 4);  
3039 - } else {  
3040 - video->payload->append((const char*)cont_nalu_header, 3);  
3041 - }  
3042 -  
3043 - // sample data  
3044 - video->payload->append(sample_unit->bytes, sample_unit->size);  
3045 - }  
3046 -  
3047 - return ret; 2990 + if (!aud_inserted) {
  2991 + aud_inserted = true;
  2992 + payload->append((const char*)fresh_nalu_header, 4);
  2993 + } else {
  2994 + payload->append((const char*)cont_nalu_header, 3);
3048 } 2995 }
  2996 +}
  2997 +
  2998 +int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample)
  2999 +{
  3000 + int ret = ERROR_SUCCESS;
3049 3001
3050 - // always append a aud nalu for each frame.  
3051 - video->payload->append((const char*)fresh_nalu_header, 4);  
3052 - video->payload->append((const char*)aud_nalu_7, 2);  
3053 -  
3054 - // when ts message(samples) contains IDR, insert sps+pps.  
3055 - if (sample->has_idr) {  
3056 - // fresh nalu header before sps.  
3057 - if (codec->sequenceParameterSetLength > 0) {  
3058 - // AnnexB prefix, for sps always 4 bytes header  
3059 - video->payload->append((const char*)fresh_nalu_header, 4);  
3060 - // sps  
3061 - video->payload->append(codec->sequenceParameterSetNALUnit, codec->sequenceParameterSetLength);  
3062 - }  
3063 - // cont nalu header before pps.  
3064 - if (codec->pictureParameterSetLength > 0) {  
3065 - // AnnexB prefix, for pps always 3 bytes header  
3066 - video->payload->append((const char*)cont_nalu_header, 3);  
3067 - // pps  
3068 - video->payload->append(codec->pictureParameterSetNALUnit, codec->pictureParameterSetLength);  
3069 - } 3002 + // Whether aud inserted.
  3003 + bool aud_inserted = false;
  3004 +
  3005 + // Insert a default AUD NALU when no AUD in samples.
  3006 + if (!sample->has_aud) {
  3007 + // the aud(access unit delimiter) before each frame.
  3008 + // 7.3.2.4 Access unit delimiter RBSP syntax
  3009 + // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 66.
  3010 + //
  3011 + // primary_pic_type u(3), the first 3bits, primary_pic_type indicates that the slice_type values
  3012 + // for all slices of the primary coded picture are members of the set listed in Table 7-5 for
  3013 + // the given value of primary_pic_type.
  3014 + // 0, slice_type 2, 7
  3015 + // 1, slice_type 0, 2, 5, 7
  3016 + // 2, slice_type 0, 1, 2, 5, 6, 7
  3017 + // 3, slice_type 4, 9
  3018 + // 4, slice_type 3, 4, 8, 9
  3019 + // 5, slice_type 2, 4, 7, 9
  3020 + // 6, slice_type 0, 2, 3, 4, 5, 7, 8, 9
  3021 + // 7, slice_type 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  3022 + // 7.4.2.4 Access unit delimiter RBSP semantics
  3023 + // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 102.
  3024 + //
  3025 + // slice_type specifies the coding type of the slice according to Table 7-6.
  3026 + // 0, P (P slice)
  3027 + // 1, B (B slice)
  3028 + // 2, I (I slice)
  3029 + // 3, SP (SP slice)
  3030 + // 4, SI (SI slice)
  3031 + // 5, P (P slice)
  3032 + // 6, B (B slice)
  3033 + // 7, I (I slice)
  3034 + // 8, SP (SP slice)
  3035 + // 9, SI (SI slice)
  3036 + // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 105.
  3037 + static u_int8_t default_aud_nalu[] = { 0x09, 0xf0};
  3038 + srs_avc_insert_aud(video->payload, aud_inserted);
  3039 + video->payload->append((const char*)default_aud_nalu, 2);
3070 } 3040 }
3071 3041
3072 // all sample use cont nalu header, except the sps-pps before IDR frame. 3042 // all sample use cont nalu header, except the sps-pps before IDR frame.
@@ -3084,20 +3054,21 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample) @@ -3084,20 +3054,21 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample)
3084 // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 83. 3054 // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 83.
3085 SrsAvcNaluType nal_unit_type = (SrsAvcNaluType)(sample_unit->bytes[0] & 0x1f); 3055 SrsAvcNaluType nal_unit_type = (SrsAvcNaluType)(sample_unit->bytes[0] & 0x1f);
3086 3056
3087 - // ignore SPS/PPS/AUD  
3088 - switch (nal_unit_type) {  
3089 - case SrsAvcNaluTypeSPS:  
3090 - case SrsAvcNaluTypePPS:  
3091 - case SrsAvcNaluTypeAccessUnitDelimiter:  
3092 - continue;  
3093 - default:  
3094 - break; 3057 + // Insert sps/pps before IDR when there is no sps/pps in samples.
  3058 + // The sps/pps is parsed from sequence header(generally the first flv packet).
  3059 + if (nal_unit_type == SrsAvcNaluTypeIDR && !sample->has_sps_pps) {
  3060 + if (codec->sequenceParameterSetLength > 0) {
  3061 + srs_avc_insert_aud(video->payload, aud_inserted);
  3062 + video->payload->append(codec->sequenceParameterSetNALUnit, codec->sequenceParameterSetLength);
  3063 + }
  3064 + if (codec->pictureParameterSetLength > 0) {
  3065 + srs_avc_insert_aud(video->payload, aud_inserted);
  3066 + video->payload->append(codec->pictureParameterSetNALUnit, codec->pictureParameterSetLength);
  3067 + }
3095 } 3068 }
3096 3069
3097 - // insert cont nalu header before frame.  
3098 - video->payload->append((const char*)cont_nalu_header, 3);  
3099 -  
3100 - // sample data 3070 + // Insert the NALU to video in annexb.
  3071 + srs_avc_insert_aud(video->payload, aud_inserted);
3101 video->payload->append(sample_unit->bytes, sample_unit->size); 3072 video->payload->append(sample_unit->bytes, sample_unit->size);
3102 } 3073 }
3103 3074
@@ -3144,8 +3115,6 @@ int SrsTsEncoder::initialize(SrsFileWriter* fw) @@ -3144,8 +3115,6 @@ int SrsTsEncoder::initialize(SrsFileWriter* fw)
3144 return ret; 3115 return ret;
3145 } 3116 }
3146 3117
3147 - sample->reset();  
3148 -  
3149 return ret; 3118 return ret;
3150 } 3119 }
3151 3120