winlin

for #635, refine hls open/close gop.

@@ -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;
@@ -1274,8 +1269,6 @@ void SrsHls::on_unpublish() @@ -1274,8 +1269,6 @@ void SrsHls::on_unpublish()
1274 srs_error("ignore m3u8 muxer flush/close audio failed. ret=%d", ret); 1269 srs_error("ignore m3u8 muxer flush/close audio failed. ret=%d", ret);
1275 } 1270 }
1276 1271
1277 - sample->reset();  
1278 -  
1279 hls_enabled = false; 1272 hls_enabled = false;
1280 } 1273 }
1281 1274
@@ -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(SrsStream* stream, SrsCodecSample* sample) @@ -773,11 +768,6 @@ int SrsAvcAacCodec::video_nalu_demux(SrsStream* 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,
@@ -2927,10 +2927,8 @@ int SrsTsCache::do_cache_aac(SrsAvcAacCodec* codec, SrsCodecSample* sample) @@ -2927,10 +2927,8 @@ int SrsTsCache::do_cache_aac(SrsAvcAacCodec* codec, SrsCodecSample* sample)
2927 return ret; 2927 return ret;
2928 } 2928 }
2929 2929
2930 -int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample) 2930 +void srs_avc_insert_aud(SrsSimpleBuffer* payload, bool& aud_inserted)
2931 { 2931 {
2932 - int ret = ERROR_SUCCESS;  
2933 -  
2934 // mux the samples in annexb format, 2932 // mux the samples in annexb format,
2935 // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 324. 2933 // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 324.
2936 /** 2934 /**
@@ -2977,6 +2975,23 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample) @@ -2977,6 +2975,23 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample)
2977 static u_int8_t fresh_nalu_header[] = { 0x00, 0x00, 0x00, 0x01 }; 2975 static u_int8_t fresh_nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
2978 static u_int8_t cont_nalu_header[] = { 0x00, 0x00, 0x01 }; 2976 static u_int8_t cont_nalu_header[] = { 0x00, 0x00, 0x01 };
2979 2977
  2978 + if (!aud_inserted) {
  2979 + aud_inserted = true;
  2980 + payload->append((const char*)fresh_nalu_header, 4);
  2981 + } else {
  2982 + payload->append((const char*)cont_nalu_header, 3);
  2983 + }
  2984 +}
  2985 +
  2986 +int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample)
  2987 +{
  2988 + int ret = ERROR_SUCCESS;
  2989 +
  2990 + // Whether aud inserted.
  2991 + bool aud_inserted = false;
  2992 +
  2993 + // Insert a default AUD NALU when no AUD in samples.
  2994 + if (!sample->has_aud) {
2980 // the aud(access unit delimiter) before each frame. 2995 // the aud(access unit delimiter) before each frame.
2981 // 7.3.2.4 Access unit delimiter RBSP syntax 2996 // 7.3.2.4 Access unit delimiter RBSP syntax
2982 // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 66. 2997 // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 66.
@@ -3007,10 +3022,12 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample) @@ -3007,10 +3022,12 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample)
3007 // 8, SP (SP slice) 3022 // 8, SP (SP slice)
3008 // 9, SI (SI slice) 3023 // 9, SI (SI slice)
3009 // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 105. 3024 // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 105.
3010 - static u_int8_t aud_nalu_7[] = { 0x09, 0xf0}; 3025 + static u_int8_t default_aud_nalu[] = { 0x09, 0xf0};
  3026 + srs_avc_insert_aud(video->payload, aud_inserted);
  3027 + video->payload->append((const char*)default_aud_nalu, 2);
  3028 + }
3011 3029
3012 - // For NonIDR(open gop), we directly appends all frames.  
3013 - if (sample->open_gop) { 3030 + // all sample use cont nalu header, except the sps-pps before IDR frame.
3014 for (int i = 0; i < sample->nb_sample_units; i++) { 3031 for (int i = 0; i < sample->nb_sample_units; i++) {
3015 SrsCodecSampleUnit* sample_unit = &sample->sample_units[i]; 3032 SrsCodecSampleUnit* sample_unit = &sample->sample_units[i];
3016 int32_t size = sample_unit->size; 3033 int32_t size = sample_unit->size;
@@ -3021,71 +3038,25 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample) @@ -3021,71 +3038,25 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample)
3021 return ret; 3038 return ret;
3022 } 3039 }
3023 3040
3024 - // insert nalu header before rbsp.  
3025 - if (i == 0) {  
3026 - video->payload->append((const char*)fresh_nalu_header, 4);  
3027 - } else {  
3028 - video->payload->append((const char*)cont_nalu_header, 3);  
3029 - }  
3030 -  
3031 - // sample data  
3032 - video->payload->append(sample_unit->bytes, sample_unit->size);  
3033 - }  
3034 -  
3035 - return ret;  
3036 - }  
3037 -  
3038 - // always append a aud nalu for each frame.  
3039 - video->payload->append((const char*)fresh_nalu_header, 4);  
3040 - video->payload->append((const char*)aud_nalu_7, 2); 3041 + // 5bits, 7.3.1 NAL unit syntax,
  3042 + // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 83.
  3043 + SrsAvcNaluType nal_unit_type = (SrsAvcNaluType)(sample_unit->bytes[0] & 0x1f);
3041 3044
3042 - // when ts message(samples) contains IDR, insert sps+pps.  
3043 - if (sample->has_idr) {  
3044 - // fresh nalu header before sps. 3045 + // Insert sps/pps before IDR when there is no sps/pps in samples.
  3046 + // The sps/pps is parsed from sequence header(generally the first flv packet).
  3047 + if (nal_unit_type == SrsAvcNaluTypeIDR && !sample->has_sps_pps) {
3045 if (codec->sequenceParameterSetLength > 0) { 3048 if (codec->sequenceParameterSetLength > 0) {
3046 - // AnnexB prefix, for sps always 4 bytes header  
3047 - video->payload->append((const char*)fresh_nalu_header, 4);  
3048 - // sps 3049 + srs_avc_insert_aud(video->payload, aud_inserted);
3049 video->payload->append(codec->sequenceParameterSetNALUnit, codec->sequenceParameterSetLength); 3050 video->payload->append(codec->sequenceParameterSetNALUnit, codec->sequenceParameterSetLength);
3050 } 3051 }
3051 - // cont nalu header before pps.  
3052 if (codec->pictureParameterSetLength > 0) { 3052 if (codec->pictureParameterSetLength > 0) {
3053 - // AnnexB prefix, for pps always 3 bytes header  
3054 - video->payload->append((const char*)cont_nalu_header, 3);  
3055 - // pps 3053 + srs_avc_insert_aud(video->payload, aud_inserted);
3056 video->payload->append(codec->pictureParameterSetNALUnit, codec->pictureParameterSetLength); 3054 video->payload->append(codec->pictureParameterSetNALUnit, codec->pictureParameterSetLength);
3057 } 3055 }
3058 } 3056 }
3059 3057
3060 - // all sample use cont nalu header, except the sps-pps before IDR frame.  
3061 - for (int i = 0; i < sample->nb_sample_units; i++) {  
3062 - SrsCodecSampleUnit* sample_unit = &sample->sample_units[i];  
3063 - int32_t size = sample_unit->size;  
3064 -  
3065 - if (!sample_unit->bytes || size <= 0) {  
3066 - ret = ERROR_HLS_AVC_SAMPLE_SIZE;  
3067 - srs_error("invalid avc sample length=%d, ret=%d", size, ret);  
3068 - return ret;  
3069 - }  
3070 -  
3071 - // 5bits, 7.3.1 NAL unit syntax,  
3072 - // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 83.  
3073 - SrsAvcNaluType nal_unit_type = (SrsAvcNaluType)(sample_unit->bytes[0] & 0x1f);  
3074 -  
3075 - // ignore SPS/PPS/AUD  
3076 - switch (nal_unit_type) {  
3077 - case SrsAvcNaluTypeSPS:  
3078 - case SrsAvcNaluTypePPS:  
3079 - case SrsAvcNaluTypeAccessUnitDelimiter:  
3080 - continue;  
3081 - default:  
3082 - break;  
3083 - }  
3084 -  
3085 - // insert cont nalu header before frame.  
3086 - video->payload->append((const char*)cont_nalu_header, 3);  
3087 -  
3088 - // sample data 3058 + // Insert the NALU to video in annexb.
  3059 + srs_avc_insert_aud(video->payload, aud_inserted);
3089 video->payload->append(sample_unit->bytes, sample_unit->size); 3060 video->payload->append(sample_unit->bytes, sample_unit->size);
3090 } 3061 }
3091 3062
@@ -3132,8 +3103,6 @@ int SrsTsEncoder::initialize(SrsFileWriter* fw) @@ -3132,8 +3103,6 @@ int SrsTsEncoder::initialize(SrsFileWriter* fw)
3132 return ret; 3103 return ret;
3133 } 3104 }
3134 3105
3135 - sample->reset();  
3136 -  
3137 return ret; 3106 return ret;
3138 } 3107 }
3139 3108