winlin

refine ts code

@@ -301,6 +301,10 @@ public: @@ -301,6 +301,10 @@ public:
301 { 301 {
302 int ret = ERROR_SUCCESS; 302 int ret = ERROR_SUCCESS;
303 303
  304 + if (!buffer->bytes || buffer->size <= 0) {
  305 + return ret;
  306 + }
  307 +
304 char* last = buffer->bytes + buffer->size; 308 char* last = buffer->bytes + buffer->size;
305 char* pos = buffer->bytes; 309 char* pos = buffer->bytes;
306 310
@@ -550,6 +554,10 @@ int SrsTSMuxer::write_audio(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam @@ -550,6 +554,10 @@ int SrsTSMuxer::write_audio(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam
550 { 554 {
551 int ret = ERROR_SUCCESS; 555 int ret = ERROR_SUCCESS;
552 556
  557 + audio_frame.dts = audio_frame.pts = time * 90;
  558 + audio_frame.pid = TS_AUDIO_PID;
  559 + audio_frame.sid = TS_AUDIO_AAC;
  560 +
553 for (int i = 0; i < sample->nb_buffers; i++) { 561 for (int i = 0; i < sample->nb_buffers; i++) {
554 SrsCodecBuffer* buf = &sample->buffers[i]; 562 SrsCodecBuffer* buf = &sample->buffers[i];
555 int32_t size = buf->size; 563 int32_t size = buf->size;
@@ -610,10 +618,6 @@ int SrsTSMuxer::write_audio(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam @@ -610,10 +618,6 @@ int SrsTSMuxer::write_audio(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam
610 audio_buffer->append(buf->bytes, buf->size); 618 audio_buffer->append(buf->bytes, buf->size);
611 } 619 }
612 620
613 - audio_frame.dts = audio_frame.pts = time * 90;  
614 - audio_frame.pid = TS_AUDIO_PID;  
615 - audio_frame.sid = TS_AUDIO_AAC;  
616 -  
617 return ret; 621 return ret;
618 } 622 }
619 623
@@ -621,9 +625,16 @@ int SrsTSMuxer::write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam @@ -621,9 +625,16 @@ int SrsTSMuxer::write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam
621 { 625 {
622 int ret = ERROR_SUCCESS; 626 int ret = ERROR_SUCCESS;
623 627
  628 + video_frame.dts = time * 90;
  629 + video_frame.pts = video_frame.dts + sample->cts * 90;
  630 + video_frame.pid = TS_VIDEO_PID;
  631 + video_frame.sid = TS_VIDEO_AVC;
  632 + video_frame.key = sample->frame_type == SrsCodecVideoAVCFrameKeyFrame;
  633 +
624 static u_int8_t aud_nal[] = { 0x00, 0x00, 0x00, 0x01, 0x09, 0xf0 }; 634 static u_int8_t aud_nal[] = { 0x00, 0x00, 0x00, 0x01, 0x09, 0xf0 };
625 video_buffer->append(aud_nal, sizeof(aud_nal)); 635 video_buffer->append(aud_nal, sizeof(aud_nal));
626 636
  637 + bool sps_pps_sent = false;
627 for (int i = 0; i < sample->nb_buffers; i++) { 638 for (int i = 0; i < sample->nb_buffers; i++) {
628 SrsCodecBuffer* buf = &sample->buffers[i]; 639 SrsCodecBuffer* buf = &sample->buffers[i];
629 int32_t size = buf->size; 640 int32_t size = buf->size;
@@ -634,6 +645,14 @@ int SrsTSMuxer::write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam @@ -634,6 +645,14 @@ int SrsTSMuxer::write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam
634 return ret; 645 return ret;
635 } 646 }
636 647
  648 + if (video_frame.key && !sps_pps_sent) {
  649 + // sample start prefix
  650 + video_buffer->append(aud_nal, 4);
  651 + // sps and pps
  652 + // TODO: do in right way.
  653 + video_buffer->append(codec->avc_extra_data, codec->avc_extra_size);
  654 + }
  655 +
637 // sample start prefix, '00 00 00 01' or '00 00 01' 656 // sample start prefix, '00 00 00 01' or '00 00 01'
638 u_int8_t* p = aud_nal + 1; 657 u_int8_t* p = aud_nal + 1;
639 u_int8_t* end = p + 3; 658 u_int8_t* end = p + 3;
@@ -648,12 +667,6 @@ int SrsTSMuxer::write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam @@ -648,12 +667,6 @@ int SrsTSMuxer::write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam
648 video_buffer->append(buf->bytes, buf->size); 667 video_buffer->append(buf->bytes, buf->size);
649 } 668 }
650 669
651 - video_frame.dts = time * 90;  
652 - video_frame.pts = video_frame.dts + sample->cts * 90;  
653 - video_frame.pid = TS_VIDEO_PID;  
654 - video_frame.sid = TS_VIDEO_AVC;  
655 - video_frame.key = sample->frame_type == SrsCodecVideoAVCFrameKeyFrame;  
656 -  
657 if ((ret = SrsMpegtsWriter::write_frame(fd, &video_frame, video_buffer)) != ERROR_SUCCESS) { 670 if ((ret = SrsMpegtsWriter::write_frame(fd, &video_frame, video_buffer)) != ERROR_SUCCESS) {
658 return ret; 671 return ret;
659 } 672 }