winlin

refine code, notice api when segment close

@@ -305,6 +305,9 @@ SrsDvrPlan::SrsDvrPlan() @@ -305,6 +305,9 @@ SrsDvrPlan::SrsDvrPlan()
305 dvr_enabled = false; 305 dvr_enabled = false;
306 fs = new SrsFileStream(); 306 fs = new SrsFileStream();
307 enc = new SrsFlvEncoder(); 307 enc = new SrsFlvEncoder();
  308 + segment_has_keyframe = true;
  309 + starttime = -1;
  310 + duration = 0;
308 } 311 }
309 312
310 SrsDvrPlan::~SrsDvrPlan() 313 SrsDvrPlan::~SrsDvrPlan()
@@ -407,7 +410,7 @@ int SrsDvrPlan::on_audio(SrsSharedPtrMessage* audio) @@ -407,7 +410,7 @@ int SrsDvrPlan::on_audio(SrsSharedPtrMessage* audio)
407 return ret; 410 return ret;
408 } 411 }
409 412
410 - if ((ret = on_audio_msg(audio)) != ERROR_SUCCESS) { 413 + if ((ret = update_duration(audio)) != ERROR_SUCCESS) {
411 return ret; 414 return ret;
412 } 415 }
413 416
@@ -435,15 +438,13 @@ int SrsDvrPlan::on_video(SrsSharedPtrMessage* video) @@ -435,15 +438,13 @@ int SrsDvrPlan::on_video(SrsSharedPtrMessage* video)
435 438
436 #ifdef SRS_AUTO_HTTP_CALLBACK 439 #ifdef SRS_AUTO_HTTP_CALLBACK
437 bool is_key_frame = SrsCodec::video_is_keyframe((int8_t*)payload, size); 440 bool is_key_frame = SrsCodec::video_is_keyframe((int8_t*)payload, size);
438 - srs_verbose("dvr video is key: %d", is_key_frame);  
439 if (is_key_frame) { 441 if (is_key_frame) {
440 - if ((ret = on_dvr_keyframe()) != ERROR_SUCCESS) {  
441 - return ret;  
442 - } 442 + segment_has_keyframe = true;
443 } 443 }
  444 + srs_verbose("dvr video is key: %d", is_key_frame);
444 #endif 445 #endif
445 446
446 - if ((ret = on_video_msg(video)) != ERROR_SUCCESS) { 447 + if ((ret = update_duration(video)) != ERROR_SUCCESS) {
447 return ret; 448 return ret;
448 } 449 }
449 450
@@ -471,22 +472,12 @@ int SrsDvrPlan::flv_open(string stream, string path) @@ -471,22 +472,12 @@ int SrsDvrPlan::flv_open(string stream, string path)
471 return ret; 472 return ret;
472 } 473 }
473 474
  475 + segment_has_keyframe = false;
  476 +
474 srs_trace("dvr stream %s to file %s", stream.c_str(), path.c_str()); 477 srs_trace("dvr stream %s to file %s", stream.c_str(), path.c_str());
475 return ret; 478 return ret;
476 } 479 }
477 480
478 -int SrsDvrPlan::on_audio_msg(SrsSharedPtrMessage* /*audio*/)  
479 -{  
480 - int ret = ERROR_SUCCESS;  
481 - return ret;  
482 -}  
483 -  
484 -int SrsDvrPlan::on_video_msg(SrsSharedPtrMessage* /*video*/)  
485 -{  
486 - int ret = ERROR_SUCCESS;  
487 - return ret;  
488 -}  
489 -  
490 int SrsDvrPlan::flv_close() 481 int SrsDvrPlan::flv_close()
491 { 482 {
492 int ret = ERROR_SUCCESS; 483 int ret = ERROR_SUCCESS;
@@ -503,6 +494,28 @@ int SrsDvrPlan::flv_close() @@ -503,6 +494,28 @@ int SrsDvrPlan::flv_close()
503 return ret; 494 return ret;
504 } 495 }
505 496
  497 +#ifdef SRS_AUTO_HTTP_CALLBACK
  498 + if (segment_has_keyframe) {
  499 + if ((ret = on_dvr_keyframe()) != ERROR_SUCCESS) {
  500 + return ret;
  501 + }
  502 + }
  503 +#endif
  504 +
  505 + return ret;
  506 +}
  507 +
  508 +int SrsDvrPlan::update_duration(SrsSharedPtrMessage* msg)
  509 +{
  510 + int ret = ERROR_SUCCESS;
  511 +
  512 + // foreach msg, collect the duration.
  513 + if (starttime < 0 || starttime > msg->header.timestamp) {
  514 + starttime = msg->header.timestamp;
  515 + }
  516 + duration += msg->header.timestamp - starttime;
  517 + starttime = msg->header.timestamp;
  518 +
506 return ret; 519 return ret;
507 } 520 }
508 521
@@ -565,8 +578,6 @@ void SrsDvrSessionPlan::on_unpublish() @@ -565,8 +578,6 @@ void SrsDvrSessionPlan::on_unpublish()
565 578
566 SrsDvrSegmentPlan::SrsDvrSegmentPlan() 579 SrsDvrSegmentPlan::SrsDvrSegmentPlan()
567 { 580 {
568 - starttime = -1;  
569 - duration = 0;  
570 segment_duration = -1; 581 segment_duration = -1;
571 } 582 }
572 583
@@ -611,38 +622,13 @@ void SrsDvrSegmentPlan::on_unpublish() @@ -611,38 +622,13 @@ void SrsDvrSegmentPlan::on_unpublish()
611 dvr_enabled = false; 622 dvr_enabled = false;
612 } 623 }
613 624
614 -int SrsDvrSegmentPlan::on_audio_msg(SrsSharedPtrMessage* audio)  
615 -{  
616 - int ret = ERROR_SUCCESS;  
617 -  
618 - if ((ret = update_duration(audio)) != ERROR_SUCCESS) {  
619 - return ret;  
620 - }  
621 -  
622 - return ret;  
623 -}  
624 -  
625 -int SrsDvrSegmentPlan::on_video_msg(SrsSharedPtrMessage* video)  
626 -{  
627 - int ret = ERROR_SUCCESS;  
628 -  
629 - if ((ret = update_duration(video)) != ERROR_SUCCESS) {  
630 - return ret;  
631 - }  
632 -  
633 - return ret;  
634 -}  
635 -  
636 int SrsDvrSegmentPlan::update_duration(SrsSharedPtrMessage* msg) 625 int SrsDvrSegmentPlan::update_duration(SrsSharedPtrMessage* msg)
637 { 626 {
638 int ret = ERROR_SUCCESS; 627 int ret = ERROR_SUCCESS;
639 628
640 - // foreach msg, collect the duration.  
641 - if (starttime < 0 || starttime > msg->header.timestamp) {  
642 - starttime = msg->header.timestamp; 629 + if ((ret = SrsDvrPlan::update_duration(msg)) != ERROR_SUCCESS) {
  630 + return ret;
643 } 631 }
644 - duration += msg->header.timestamp - starttime;  
645 - starttime = msg->header.timestamp;  
646 632
647 // reap if exceed duration. 633 // reap if exceed duration.
648 if (duration > 0 && segment_duration > 0 && duration > segment_duration) { 634 if (duration > 0 && segment_duration > 0 && duration > segment_duration) {
@@ -127,10 +127,20 @@ protected: @@ -127,10 +127,20 @@ protected:
127 SrsSource* _source; 127 SrsSource* _source;
128 SrsRequest* _req; 128 SrsRequest* _req;
129 SrsRtmpJitter* jitter; 129 SrsRtmpJitter* jitter;
  130 +protected:
130 /** 131 /**
131 * current flv file path. 132 * current flv file path.
132 */ 133 */
133 std::string current_flv_path; 134 std::string current_flv_path;
  135 + /**
  136 + * whether current segment has keyframe.
  137 + */
  138 + bool segment_has_keyframe;
  139 + /**
  140 + * current segment duration and starttime.
  141 + */
  142 + int64_t duration;
  143 + int64_t starttime;
134 public: 144 public:
135 SrsDvrPlan(); 145 SrsDvrPlan();
136 virtual ~SrsDvrPlan(); 146 virtual ~SrsDvrPlan();
@@ -143,14 +153,13 @@ public: @@ -143,14 +153,13 @@ public:
143 virtual int on_video(SrsSharedPtrMessage* video); 153 virtual int on_video(SrsSharedPtrMessage* video);
144 protected: 154 protected:
145 virtual int flv_open(std::string stream, std::string path); 155 virtual int flv_open(std::string stream, std::string path);
146 - /**  
147 - * user should override this method.  
148 - * for the audio/video is corrected by jitter.  
149 - */  
150 - virtual int on_audio_msg(SrsSharedPtrMessage* audio);  
151 - virtual int on_video_msg(SrsSharedPtrMessage* video);  
152 virtual int flv_close(); 156 virtual int flv_close();
  157 + virtual int update_duration(SrsSharedPtrMessage* msg);
153 private: 158 private:
  159 + /**
  160 + * when srs reap the flv(close the segment),
  161 + * if has keyframe, notice the api.
  162 + */
154 virtual int on_dvr_keyframe(); 163 virtual int on_dvr_keyframe();
155 public: 164 public:
156 static SrsDvrPlan* create_plan(std::string vhost); 165 static SrsDvrPlan* create_plan(std::string vhost);
@@ -174,8 +183,6 @@ public: @@ -174,8 +183,6 @@ public:
174 class SrsDvrSegmentPlan : public SrsDvrPlan 183 class SrsDvrSegmentPlan : public SrsDvrPlan
175 { 184 {
176 private: 185 private:
177 - int64_t duration;  
178 - int64_t starttime;  
179 // in config, in ms 186 // in config, in ms
180 int segment_duration; 187 int segment_duration;
181 public: 188 public:
@@ -185,8 +192,6 @@ public: @@ -185,8 +192,6 @@ public:
185 virtual int initialize(SrsSource* source, SrsRequest* req); 192 virtual int initialize(SrsSource* source, SrsRequest* req);
186 virtual int on_publish(); 193 virtual int on_publish();
187 virtual void on_unpublish(); 194 virtual void on_unpublish();
188 - virtual int on_audio_msg(SrsSharedPtrMessage* audio);  
189 - virtual int on_video_msg(SrsSharedPtrMessage* video);  
190 private: 195 private:
191 virtual int update_duration(SrsSharedPtrMessage* msg); 196 virtual int update_duration(SrsSharedPtrMessage* msg);
192 }; 197 };