winlin

refine code, remove the base dts, donot drop start BP frames

@@ -151,7 +151,6 @@ int SrsClient::do_cycle() @@ -151,7 +151,6 @@ int SrsClient::do_cycle()
151 // find a source to publish. 151 // find a source to publish.
152 SrsSource* source = SrsSource::find(req->get_stream_url()); 152 SrsSource* source = SrsSource::find(req->get_stream_url());
153 srs_assert(source != NULL); 153 srs_assert(source != NULL);
154 - SrsHLS* hls = source->get_hls();  
155 154
156 bool enabled_cache = true; 155 bool enabled_cache = true;
157 conf = config->get_gop_cache(req->vhost); 156 conf = config->get_gop_cache(req->vhost);
@@ -182,7 +181,6 @@ int SrsClient::do_cycle() @@ -182,7 +181,6 @@ int SrsClient::do_cycle()
182 } 181 }
183 srs_info("start to publish stream %s success", req->stream.c_str()); 182 srs_info("start to publish stream %s success", req->stream.c_str());
184 ret = publish(source, true); 183 ret = publish(source, true);
185 - hls->on_unpublish();  
186 source->on_unpublish(); 184 source->on_unpublish();
187 return ret; 185 return ret;
188 } 186 }
@@ -195,7 +193,6 @@ int SrsClient::do_cycle() @@ -195,7 +193,6 @@ int SrsClient::do_cycle()
195 } 193 }
196 srs_info("flash start to publish stream %s success", req->stream.c_str()); 194 srs_info("flash start to publish stream %s success", req->stream.c_str());
197 ret = publish(source, false); 195 ret = publish(source, false);
198 - hls->on_unpublish();  
199 source->on_unpublish(); 196 source->on_unpublish();
200 return ret; 197 return ret;
201 } 198 }
@@ -333,10 +330,9 @@ int SrsClient::publish(SrsSource* source, bool is_fmle) @@ -333,10 +330,9 @@ int SrsClient::publish(SrsSource* source, bool is_fmle)
333 srs_verbose("check publish_refer success."); 330 srs_verbose("check publish_refer success.");
334 331
335 SrsPithyPrint pithy_print(SRS_STAGE_PUBLISH_USER); 332 SrsPithyPrint pithy_print(SRS_STAGE_PUBLISH_USER);
336 - SrsHLS* hls = source->get_hls();  
337 333
338 // notify the hls to prepare when publish start. 334 // notify the hls to prepare when publish start.
339 - if ((ret = hls->on_publish(req->vhost)) != ERROR_SUCCESS) { 335 + if ((ret = source->on_publish(req->vhost)) != ERROR_SUCCESS) {
340 srs_error("hls on_publish failed. ret=%d", ret); 336 srs_error("hls on_publish failed. ret=%d", ret);
341 return ret; 337 return ret;
342 } 338 }
@@ -362,7 +358,7 @@ int SrsClient::publish(SrsSource* source, bool is_fmle) @@ -362,7 +358,7 @@ int SrsClient::publish(SrsSource* source, bool is_fmle)
362 (int)(srs_get_system_time_ms()/1000), pithy_print.get_age(), rtmp->get_send_bytes(), rtmp->get_recv_bytes(), rtmp->get_send_kbps(), rtmp->get_recv_kbps()); 358 (int)(srs_get_system_time_ms()/1000), pithy_print.get_age(), rtmp->get_send_bytes(), rtmp->get_recv_bytes(), rtmp->get_send_kbps(), rtmp->get_recv_kbps());
363 } 359 }
364 360
365 - if ((ret = process_publish_message(source, hls, msg, is_fmle)) != ERROR_SUCCESS) { 361 + if ((ret = process_publish_message(source, msg, is_fmle)) != ERROR_SUCCESS) {
366 srs_error("process publish message failed. ret=%d", ret); 362 srs_error("process publish message failed. ret=%d", ret);
367 return ret; 363 return ret;
368 } 364 }
@@ -371,16 +367,12 @@ int SrsClient::publish(SrsSource* source, bool is_fmle) @@ -371,16 +367,12 @@ int SrsClient::publish(SrsSource* source, bool is_fmle)
371 return ret; 367 return ret;
372 } 368 }
373 369
374 -int SrsClient::process_publish_message(SrsSource* source, SrsHLS* hls, SrsCommonMessage* msg, bool is_fmle) 370 +int SrsClient::process_publish_message(SrsSource* source, SrsCommonMessage* msg, bool is_fmle)
375 { 371 {
376 int ret = ERROR_SUCCESS; 372 int ret = ERROR_SUCCESS;
377 373
378 // process audio packet 374 // process audio packet
379 if (msg->header.is_audio()) { 375 if (msg->header.is_audio()) {
380 - if ((ret = hls->on_audio(msg)) != ERROR_SUCCESS) {  
381 - srs_error("hls process audio message failed. ret=%d", ret);  
382 - return ret;  
383 - }  
384 if ((ret = source->on_audio(msg)) != ERROR_SUCCESS) { 376 if ((ret = source->on_audio(msg)) != ERROR_SUCCESS) {
385 srs_error("source process audio message failed. ret=%d", ret); 377 srs_error("source process audio message failed. ret=%d", ret);
386 return ret; 378 return ret;
@@ -388,10 +380,6 @@ int SrsClient::process_publish_message(SrsSource* source, SrsHLS* hls, SrsCommon @@ -388,10 +380,6 @@ int SrsClient::process_publish_message(SrsSource* source, SrsHLS* hls, SrsCommon
388 } 380 }
389 // process video packet 381 // process video packet
390 if (msg->header.is_video()) { 382 if (msg->header.is_video()) {
391 - if ((ret = hls->on_video(msg)) != ERROR_SUCCESS) {  
392 - srs_error("hls process video message failed. ret=%d", ret);  
393 - return ret;  
394 - }  
395 if ((ret = source->on_video(msg)) != ERROR_SUCCESS) { 383 if ((ret = source->on_video(msg)) != ERROR_SUCCESS) {
396 srs_error("source process video message failed. ret=%d", ret); 384 srs_error("source process video message failed. ret=%d", ret);
397 return ret; 385 return ret;
@@ -408,10 +396,6 @@ int SrsClient::process_publish_message(SrsSource* source, SrsHLS* hls, SrsCommon @@ -408,10 +396,6 @@ int SrsClient::process_publish_message(SrsSource* source, SrsHLS* hls, SrsCommon
408 SrsPacket* pkt = msg->get_packet(); 396 SrsPacket* pkt = msg->get_packet();
409 if (dynamic_cast<SrsOnMetaDataPacket*>(pkt)) { 397 if (dynamic_cast<SrsOnMetaDataPacket*>(pkt)) {
410 SrsOnMetaDataPacket* metadata = dynamic_cast<SrsOnMetaDataPacket*>(pkt); 398 SrsOnMetaDataPacket* metadata = dynamic_cast<SrsOnMetaDataPacket*>(pkt);
411 - if ((ret = hls->on_meta_data(metadata)) != ERROR_SUCCESS) {  
412 - srs_error("hls process onMetaData message failed. ret=%d", ret);  
413 - return ret;  
414 - }  
415 if ((ret = source->on_meta_data(msg, metadata)) != ERROR_SUCCESS) { 399 if ((ret = source->on_meta_data(msg, metadata)) != ERROR_SUCCESS) {
416 srs_error("source process onMetaData message failed. ret=%d", ret); 400 srs_error("source process onMetaData message failed. ret=%d", ret);
417 return ret; 401 return ret;
@@ -39,7 +39,6 @@ class SrsSource; @@ -39,7 +39,6 @@ class SrsSource;
39 class SrsRefer; 39 class SrsRefer;
40 class SrsConsumer; 40 class SrsConsumer;
41 class SrsCommonMessage; 41 class SrsCommonMessage;
42 -class SrsHLS;  
43 42
44 /** 43 /**
45 * the client provides the main logic control for RTMP clients. 44 * the client provides the main logic control for RTMP clients.
@@ -61,7 +60,7 @@ private: @@ -61,7 +60,7 @@ private:
61 virtual int check_vhost(); 60 virtual int check_vhost();
62 virtual int playing(SrsSource* source); 61 virtual int playing(SrsSource* source);
63 virtual int publish(SrsSource* source, bool is_fmle); 62 virtual int publish(SrsSource* source, bool is_fmle);
64 - virtual int process_publish_message(SrsSource* source, SrsHLS* hls, SrsCommonMessage* msg, bool is_fmle); 63 + virtual int process_publish_message(SrsSource* source, SrsCommonMessage* msg, bool is_fmle);
65 virtual int get_peer_ip(); 64 virtual int get_peer_ip();
66 virtual int process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg); 65 virtual int process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg);
67 }; 66 };
@@ -281,6 +281,24 @@ u_int8_t mpegts_header[] = { @@ -281,6 +281,24 @@ u_int8_t mpegts_header[] = {
281 // 63000: 700ms, ts_tbn=90000 281 // 63000: 700ms, ts_tbn=90000
282 #define SRS_HLS_DELAY 63000 282 #define SRS_HLS_DELAY 63000
283 283
  284 +// @see: ngx_rtmp_SrsMpegtsFrame_t
  285 +struct SrsMpegtsFrame
  286 +{
  287 + int64_t pts;
  288 + int64_t dts;
  289 + int pid;
  290 + int sid;
  291 + int cc;
  292 + bool key;
  293 +
  294 + SrsMpegtsFrame()
  295 + {
  296 + pts = dts = 0;
  297 + pid = sid = cc = 0;
  298 + key = false;
  299 + }
  300 +};
  301 +
284 // @see: ngx_rtmp_mpegts.c 302 // @see: ngx_rtmp_mpegts.c
285 // TODO: support full mpegts feature in future. 303 // TODO: support full mpegts feature in future.
286 class SrsMpegtsWriter 304 class SrsMpegtsWriter
@@ -298,7 +316,7 @@ public: @@ -298,7 +316,7 @@ public:
298 316
299 return ret; 317 return ret;
300 } 318 }
301 - static int write_frame(int fd, mpegts_frame* frame, SrsCodecBuffer* buffer) 319 + static int write_frame(int fd, SrsMpegtsFrame* frame, SrsCodecBuffer* buffer)
302 { 320 {
303 int ret = ERROR_SUCCESS; 321 int ret = ERROR_SUCCESS;
304 322
@@ -512,13 +530,11 @@ SrsTSMuxer::SrsTSMuxer() @@ -512,13 +530,11 @@ SrsTSMuxer::SrsTSMuxer()
512 { 530 {
513 fd = -1; 531 fd = -1;
514 532
515 - // ffmpeg set the start time to the delay time.  
516 - base_dts = SRS_HLS_DELAY;  
517 -  
518 audio_buffer = new SrsCodecBuffer(); 533 audio_buffer = new SrsCodecBuffer();
519 video_buffer = new SrsCodecBuffer(); 534 video_buffer = new SrsCodecBuffer();
520 -  
521 - got_iframe = false; 535 +
  536 + audio_frame = new SrsMpegtsFrame();
  537 + video_frame = new SrsMpegtsFrame();
522 } 538 }
523 539
524 SrsTSMuxer::~SrsTSMuxer() 540 SrsTSMuxer::~SrsTSMuxer()
@@ -530,6 +546,9 @@ SrsTSMuxer::~SrsTSMuxer() @@ -530,6 +546,9 @@ SrsTSMuxer::~SrsTSMuxer()
530 546
531 srs_freep(audio_buffer); 547 srs_freep(audio_buffer);
532 srs_freep(video_buffer); 548 srs_freep(video_buffer);
  549 +
  550 + srs_freep(audio_frame);
  551 + srs_freep(video_frame);
533 } 552 }
534 553
535 int SrsTSMuxer::open(std::string _path) 554 int SrsTSMuxer::open(std::string _path)
@@ -560,9 +579,12 @@ int SrsTSMuxer::write_audio(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam @@ -560,9 +579,12 @@ int SrsTSMuxer::write_audio(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam
560 { 579 {
561 int ret = ERROR_SUCCESS; 580 int ret = ERROR_SUCCESS;
562 581
563 - audio_frame.dts = audio_frame.pts = base_dts + time * 90;  
564 - audio_frame.pid = TS_AUDIO_PID;  
565 - audio_frame.sid = TS_AUDIO_AAC; 582 + if (!audio_frame) {
  583 + audio_frame = new SrsMpegtsFrame();
  584 + audio_frame->dts = audio_frame->pts = time * 90;
  585 + audio_frame->pid = TS_AUDIO_PID;
  586 + audio_frame->sid = TS_AUDIO_AAC;
  587 + }
566 588
567 for (int i = 0; i < sample->nb_buffers; i++) { 589 for (int i = 0; i < sample->nb_buffers; i++) {
568 SrsCodecBuffer* buf = &sample->buffers[i]; 590 SrsCodecBuffer* buf = &sample->buffers[i];
@@ -631,18 +653,11 @@ int SrsTSMuxer::write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam @@ -631,18 +653,11 @@ int SrsTSMuxer::write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam
631 { 653 {
632 int ret = ERROR_SUCCESS; 654 int ret = ERROR_SUCCESS;
633 655
634 - video_frame.dts = base_dts + time * 90;  
635 - video_frame.pts = video_frame.dts + sample->cts * 90;  
636 - video_frame.pid = TS_VIDEO_PID;  
637 - video_frame.sid = TS_VIDEO_AVC;  
638 - video_frame.key = sample->frame_type == SrsCodecVideoAVCFrameKeyFrame;  
639 -  
640 - if (video_frame.key) {  
641 - got_iframe = true;  
642 - }  
643 - if (!got_iframe) {  
644 - return ret;  
645 - } 656 + video_frame->dts = time * 90;
  657 + video_frame->pts = video_frame->dts + sample->cts * 90;
  658 + video_frame->pid = TS_VIDEO_PID;
  659 + video_frame->sid = TS_VIDEO_AVC;
  660 + video_frame->key = sample->frame_type == SrsCodecVideoAVCFrameKeyFrame;
646 661
647 static u_int8_t aud_nal[] = { 0x00, 0x00, 0x00, 0x01, 0x09, 0xf0 }; 662 static u_int8_t aud_nal[] = { 0x00, 0x00, 0x00, 0x01, 0x09, 0xf0 };
648 video_buffer->append(aud_nal, sizeof(aud_nal)); 663 video_buffer->append(aud_nal, sizeof(aud_nal));
@@ -669,8 +684,12 @@ int SrsTSMuxer::write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam @@ -669,8 +684,12 @@ int SrsTSMuxer::write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam
669 if (nal_unit_type == 1) { 684 if (nal_unit_type == 1) {
670 sps_pps_sent = false; 685 sps_pps_sent = false;
671 } 686 }
672 - // 5: Coded slice of an IDR picture 687 + // 5: Coded slice of an IDR picture.
  688 + // insert sps/pps before IDR or key frame is ok.
673 if (nal_unit_type == 5 && !sps_pps_sent) { 689 if (nal_unit_type == 5 && !sps_pps_sent) {
  690 + //if (video_frame->key && !sps_pps_sent) {
  691 + sps_pps_sent = true;
  692 +
674 // ngx_rtmp_hls_append_sps_pps 693 // ngx_rtmp_hls_append_sps_pps
675 if (codec->sequenceParameterSetLength > 0) { 694 if (codec->sequenceParameterSetLength > 0) {
676 // AnnexB prefix 695 // AnnexB prefix
@@ -700,12 +719,14 @@ int SrsTSMuxer::write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam @@ -700,12 +719,14 @@ int SrsTSMuxer::write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sam
700 video_buffer->append(buf->bytes, buf->size); 719 video_buffer->append(buf->bytes, buf->size);
701 } 720 }
702 721
703 - if ((ret = SrsMpegtsWriter::write_frame(fd, &video_frame, video_buffer)) != ERROR_SUCCESS) { 722 + if ((ret = SrsMpegtsWriter::write_frame(fd, video_frame, video_buffer)) != ERROR_SUCCESS) {
704 return ret; 723 return ret;
705 } 724 }
706 - if ((ret = SrsMpegtsWriter::write_frame(fd, &audio_frame, audio_buffer)) != ERROR_SUCCESS) { 725 +
  726 + if ((ret = SrsMpegtsWriter::write_frame(fd, audio_frame, audio_buffer)) != ERROR_SUCCESS) {
707 return ret; 727 return ret;
708 } 728 }
  729 + srs_freep(audio_frame);
709 730
710 return ret; 731 return ret;
711 } 732 }
@@ -35,6 +35,7 @@ class SrsOnMetaDataPacket; @@ -35,6 +35,7 @@ class SrsOnMetaDataPacket;
35 class SrsCommonMessage; 35 class SrsCommonMessage;
36 class SrsCodecSample; 36 class SrsCodecSample;
37 class SrsCodecBuffer; 37 class SrsCodecBuffer;
  38 +class SrsMpegtsFrame;
38 class SrsTSMuxer; 39 class SrsTSMuxer;
39 class SrsCodec; 40 class SrsCodec;
40 41
@@ -57,35 +58,15 @@ public: @@ -57,35 +58,15 @@ public:
57 virtual int on_video(SrsCommonMessage* video); 58 virtual int on_video(SrsCommonMessage* video);
58 }; 59 };
59 60
60 -// @see: ngx_rtmp_mpegts_frame_t  
61 -struct mpegts_frame  
62 -{  
63 - int64_t pts;  
64 - int64_t dts;  
65 - int pid;  
66 - int sid;  
67 - int cc;  
68 - bool key;  
69 -  
70 - mpegts_frame()  
71 - {  
72 - pts = dts = 0;  
73 - pid = sid = cc = 0;  
74 - key = false;  
75 - }  
76 -};  
77 -  
78 class SrsTSMuxer 61 class SrsTSMuxer
79 { 62 {
80 private: 63 private:
81 int fd; 64 int fd;
82 std::string path; 65 std::string path;
83 private: 66 private:
84 - bool got_iframe;  
85 - int64_t base_dts;  
86 - mpegts_frame audio_frame; 67 + SrsMpegtsFrame* audio_frame;
87 SrsCodecBuffer* audio_buffer; 68 SrsCodecBuffer* audio_buffer;
88 - mpegts_frame video_frame; 69 + SrsMpegtsFrame* video_frame;
89 SrsCodecBuffer* video_buffer; 70 SrsCodecBuffer* video_buffer;
90 public: 71 public:
91 SrsTSMuxer(); 72 SrsTSMuxer();
@@ -266,15 +266,15 @@ SrsSource::~SrsSource() @@ -266,15 +266,15 @@ SrsSource::~SrsSource()
266 srs_freep(hls); 266 srs_freep(hls);
267 } 267 }
268 268
269 -SrsHLS* SrsSource::get_hls()  
270 -{  
271 - return hls;  
272 -}  
273 -  
274 int SrsSource::on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata) 269 int SrsSource::on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata)
275 { 270 {
276 int ret = ERROR_SUCCESS; 271 int ret = ERROR_SUCCESS;
277 272
  273 + if ((ret = hls->on_meta_data(metadata)) != ERROR_SUCCESS) {
  274 + srs_error("hls process onMetaData message failed. ret=%d", ret);
  275 + return ret;
  276 + }
  277 +
278 metadata->metadata->set("server", new SrsAmf0String( 278 metadata->metadata->set("server", new SrsAmf0String(
279 RTMP_SIG_SRS_KEY" "RTMP_SIG_SRS_VERSION" ("RTMP_SIG_SRS_URL_SHORT")")); 279 RTMP_SIG_SRS_KEY" "RTMP_SIG_SRS_VERSION" ("RTMP_SIG_SRS_URL_SHORT")"));
280 280
@@ -336,6 +336,11 @@ int SrsSource::on_audio(SrsCommonMessage* audio) @@ -336,6 +336,11 @@ int SrsSource::on_audio(SrsCommonMessage* audio)
336 { 336 {
337 int ret = ERROR_SUCCESS; 337 int ret = ERROR_SUCCESS;
338 338
  339 + if ((ret = hls->on_audio(audio)) != ERROR_SUCCESS) {
  340 + srs_error("hls process audio message failed. ret=%d", ret);
  341 + return ret;
  342 + }
  343 +
339 SrsSharedPtrMessage* msg = new SrsSharedPtrMessage(); 344 SrsSharedPtrMessage* msg = new SrsSharedPtrMessage();
340 SrsAutoFree(SrsSharedPtrMessage, msg, false); 345 SrsAutoFree(SrsSharedPtrMessage, msg, false);
341 if ((ret = msg->initialize(audio, (char*)audio->payload, audio->size)) != ERROR_SUCCESS) { 346 if ((ret = msg->initialize(audio, (char*)audio->payload, audio->size)) != ERROR_SUCCESS) {
@@ -381,6 +386,11 @@ int SrsSource::on_video(SrsCommonMessage* video) @@ -381,6 +386,11 @@ int SrsSource::on_video(SrsCommonMessage* video)
381 { 386 {
382 int ret = ERROR_SUCCESS; 387 int ret = ERROR_SUCCESS;
383 388
  389 + if ((ret = hls->on_video(video)) != ERROR_SUCCESS) {
  390 + srs_error("hls process video message failed. ret=%d", ret);
  391 + return ret;
  392 + }
  393 +
384 SrsSharedPtrMessage* msg = new SrsSharedPtrMessage(); 394 SrsSharedPtrMessage* msg = new SrsSharedPtrMessage();
385 SrsAutoFree(SrsSharedPtrMessage, msg, false); 395 SrsAutoFree(SrsSharedPtrMessage, msg, false);
386 if ((ret = msg->initialize(video, (char*)video->payload, video->size)) != ERROR_SUCCESS) { 396 if ((ret = msg->initialize(video, (char*)video->payload, video->size)) != ERROR_SUCCESS) {
@@ -422,8 +432,15 @@ int SrsSource::on_video(SrsCommonMessage* video) @@ -422,8 +432,15 @@ int SrsSource::on_video(SrsCommonMessage* video)
422 return ret; 432 return ret;
423 } 433 }
424 434
  435 +int SrsSource::on_publish(std::string vhost)
  436 +{
  437 + return hls->on_publish(vhost);
  438 +}
  439 +
425 void SrsSource::on_unpublish() 440 void SrsSource::on_unpublish()
426 { 441 {
  442 + hls->on_unpublish();
  443 +
427 clear_gop_cache(); 444 clear_gop_cache();
428 445
429 srs_freep(cache_metadata); 446 srs_freep(cache_metadata);
@@ -143,15 +143,10 @@ public: @@ -143,15 +143,10 @@ public:
143 SrsSource(std::string _stream_url); 143 SrsSource(std::string _stream_url);
144 virtual ~SrsSource(); 144 virtual ~SrsSource();
145 public: 145 public:
146 - /**  
147 - * get the hls handler, which has a long lifecycle  
148 - * util the source destroyed.  
149 - */  
150 - virtual SrsHLS* get_hls();  
151 -public:  
152 virtual int on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata); 146 virtual int on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata);
153 virtual int on_audio(SrsCommonMessage* audio); 147 virtual int on_audio(SrsCommonMessage* audio);
154 virtual int on_video(SrsCommonMessage* video); 148 virtual int on_video(SrsCommonMessage* video);
  149 + virtual int on_publish(std::string vhost);
155 virtual void on_unpublish(); 150 virtual void on_unpublish();
156 public: 151 public:
157 virtual int create_consumer(SrsConsumer*& consumer); 152 virtual int create_consumer(SrsConsumer*& consumer);