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,6 +2987,23 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample) @@ -2989,6 +2987,23 @@ 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
  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);
  2995 + }
  2996 +}
  2997 +
  2998 +int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample)
  2999 +{
  3000 + int ret = ERROR_SUCCESS;
  3001 +
  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) {
2992 // the aud(access unit delimiter) before each frame. 3007 // the aud(access unit delimiter) before each frame.
2993 // 7.3.2.4 Access unit delimiter RBSP syntax 3008 // 7.3.2.4 Access unit delimiter RBSP syntax
2994 // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 66. 3009 // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 66.
@@ -3019,10 +3034,12 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample) @@ -3019,10 +3034,12 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample)
3019 // 8, SP (SP slice) 3034 // 8, SP (SP slice)
3020 // 9, SI (SI slice) 3035 // 9, SI (SI slice)
3021 // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 105. 3036 // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 105.
3022 - static u_int8_t aud_nalu_7[] = { 0x09, 0xf0}; 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);
  3040 + }
3023 3041
3024 - // For NonIDR(open gop), we directly appends all frames.  
3025 - if (sample->open_gop) { 3042 + // all sample use cont nalu header, except the sps-pps before IDR frame.
3026 for (int i = 0; i < sample->nb_sample_units; i++) { 3043 for (int i = 0; i < sample->nb_sample_units; i++) {
3027 SrsCodecSampleUnit* sample_unit = &sample->sample_units[i]; 3044 SrsCodecSampleUnit* sample_unit = &sample->sample_units[i];
3028 int32_t size = sample_unit->size; 3045 int32_t size = sample_unit->size;
@@ -3033,71 +3050,25 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample) @@ -3033,71 +3050,25 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample)
3033 return ret; 3050 return ret;
3034 } 3051 }
3035 3052
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;  
3048 - }  
3049 -  
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 + // 5bits, 7.3.1 NAL unit syntax,
  3054 + // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 83.
  3055 + SrsAvcNaluType nal_unit_type = (SrsAvcNaluType)(sample_unit->bytes[0] & 0x1f);
3053 3056
3054 - // when ts message(samples) contains IDR, insert sps+pps.  
3055 - if (sample->has_idr) {  
3056 - // fresh nalu header before sps. 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) {
3057 if (codec->sequenceParameterSetLength > 0) { 3060 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 + srs_avc_insert_aud(video->payload, aud_inserted);
3061 video->payload->append(codec->sequenceParameterSetNALUnit, codec->sequenceParameterSetLength); 3062 video->payload->append(codec->sequenceParameterSetNALUnit, codec->sequenceParameterSetLength);
3062 } 3063 }
3063 - // cont nalu header before pps.  
3064 if (codec->pictureParameterSetLength > 0) { 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 3065 + srs_avc_insert_aud(video->payload, aud_inserted);
3068 video->payload->append(codec->pictureParameterSetNALUnit, codec->pictureParameterSetLength); 3066 video->payload->append(codec->pictureParameterSetNALUnit, codec->pictureParameterSetLength);
3069 } 3067 }
3070 } 3068 }
3071 3069
3072 - // all sample use cont nalu header, except the sps-pps before IDR frame.  
3073 - for (int i = 0; i < sample->nb_sample_units; i++) {  
3074 - SrsCodecSampleUnit* sample_unit = &sample->sample_units[i];  
3075 - int32_t size = sample_unit->size;  
3076 -  
3077 - if (!sample_unit->bytes || size <= 0) {  
3078 - ret = ERROR_HLS_AVC_SAMPLE_SIZE;  
3079 - srs_error("invalid avc sample length=%d, ret=%d", size, ret);  
3080 - return ret;  
3081 - }  
3082 -  
3083 - // 5bits, 7.3.1 NAL unit syntax,  
3084 - // H.264-AVC-ISO_IEC_14496-10-2012.pdf, page 83.  
3085 - SrsAvcNaluType nal_unit_type = (SrsAvcNaluType)(sample_unit->bytes[0] & 0x1f);  
3086 -  
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;  
3095 - }  
3096 -  
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