winlin

fix #713, refine source to avoid critical fetch and create. 2.0.222

@@ -344,6 +344,7 @@ Remark: @@ -344,6 +344,7 @@ Remark:
344 344
345 ## History 345 ## History
346 346
  347 +* v2.0, 2016-12-13, fix #713, refine source to avoid critical fetch and create. 2.0.222
347 * <strong>v2.0, 2016-11-09, [2.0 beta2(2.0.221)][r2.0b2] released. 86691 lines.</strong> 348 * <strong>v2.0, 2016-11-09, [2.0 beta2(2.0.221)][r2.0b2] released. 86691 lines.</strong>
348 * v2.0, 2016-11-05, fix #654, crash when source cleanup for edge. 2.0.221 349 * v2.0, 2016-11-05, fix #654, crash when source cleanup for edge. 2.0.221
349 * v2.0, 2016-10-26, fix #666, crash when source cleanup for http-flv. 2.0.220 350 * v2.0, 2016-10-26, fix #666, crash when source cleanup for http-flv. 2.0.220
@@ -1228,11 +1228,9 @@ int SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph) @@ -1228,11 +1228,9 @@ int SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph)
1228 } 1228 }
1229 } 1229 }
1230 1230
1231 - SrsSource* s = SrsSource::fetch(r);  
1232 - if (!s) {  
1233 - if ((ret = SrsSource::create(r, server, server, &s)) != ERROR_SUCCESS) {  
1234 - return ret;  
1235 - } 1231 + SrsSource* s = NULL;
  1232 + if ((ret = SrsSource::fetch_or_create(r, server, server, &s)) != ERROR_SUCCESS) {
  1233 + return ret;
1236 } 1234 }
1237 srs_assert(s != NULL); 1235 srs_assert(s != NULL);
1238 1236
@@ -486,11 +486,9 @@ int SrsRtmpConn::stream_service_cycle() @@ -486,11 +486,9 @@ int SrsRtmpConn::stream_service_cycle()
486 rtmp->set_send_timeout(SRS_CONSTS_RTMP_SEND_TIMEOUT_US); 486 rtmp->set_send_timeout(SRS_CONSTS_RTMP_SEND_TIMEOUT_US);
487 487
488 // find a source to serve. 488 // find a source to serve.
489 - SrsSource* source = SrsSource::fetch(req);  
490 - if (!source) {  
491 - if ((ret = SrsSource::create(req, server, server, &source)) != ERROR_SUCCESS) {  
492 - return ret;  
493 - } 489 + SrsSource* source = NULL;
  490 + if ((ret = SrsSource::fetch_or_create(req, server, server, &source)) != ERROR_SUCCESS) {
  491 + return ret;
494 } 492 }
495 srs_assert(source != NULL); 493 srs_assert(source != NULL);
496 494
@@ -731,17 +731,23 @@ ISrsSourceHandler::~ISrsSourceHandler() @@ -731,17 +731,23 @@ ISrsSourceHandler::~ISrsSourceHandler()
731 731
732 std::map<std::string, SrsSource*> SrsSource::pool; 732 std::map<std::string, SrsSource*> SrsSource::pool;
733 733
734 -int SrsSource::create(SrsRequest* r, ISrsSourceHandler* h, ISrsHlsHandler* hh, SrsSource** pps) 734 +int SrsSource::fetch_or_create(SrsRequest* r, ISrsSourceHandler* h, ISrsHlsHandler* hh, SrsSource** pps)
735 { 735 {
736 int ret = ERROR_SUCCESS; 736 int ret = ERROR_SUCCESS;
737 737
  738 + SrsSource* source = NULL;
  739 + if ((source = fetch(r)) != NULL) {
  740 + *pps = source;
  741 + return ret;
  742 + }
  743 +
738 string stream_url = r->get_stream_url(); 744 string stream_url = r->get_stream_url();
739 string vhost = r->vhost; 745 string vhost = r->vhost;
740 746
741 // should always not exists for create a source. 747 // should always not exists for create a source.
742 srs_assert (pool.find(stream_url) == pool.end()); 748 srs_assert (pool.find(stream_url) == pool.end());
743 749
744 - SrsSource* source = new SrsSource(); 750 + source = new SrsSource();
745 if ((ret = source->initialize(r, h, hh)) != ERROR_SUCCESS) { 751 if ((ret = source->initialize(r, h, hh)) != ERROR_SUCCESS) {
746 srs_freep(source); 752 srs_freep(source);
747 return ret; 753 return ret;
@@ -774,20 +780,6 @@ SrsSource* SrsSource::fetch(SrsRequest* r) @@ -774,20 +780,6 @@ SrsSource* SrsSource::fetch(SrsRequest* r)
774 return source; 780 return source;
775 } 781 }
776 782
777 -SrsSource* SrsSource::fetch(std::string vhost, std::string app, std::string stream)  
778 -{  
779 - SrsSource* source = NULL;  
780 - string stream_url = srs_generate_stream_url(vhost, app, stream);  
781 -  
782 - if (pool.find(stream_url) == pool.end()) {  
783 - return NULL;  
784 - }  
785 -  
786 - source = pool[stream_url];  
787 -  
788 - return source;  
789 -}  
790 -  
791 void SrsSource::dispose_all() 783 void SrsSource::dispose_all()
792 { 784 {
793 std::map<std::string, SrsSource*>::iterator it; 785 std::map<std::string, SrsSource*>::iterator it;
@@ -418,22 +418,20 @@ private: @@ -418,22 +418,20 @@ private:
418 static std::map<std::string, SrsSource*> pool; 418 static std::map<std::string, SrsSource*> pool;
419 public: 419 public:
420 /** 420 /**
421 - * find stream by vhost/app/stream. 421 + * create source when fetch from cache failed.
422 * @param r the client request. 422 * @param r the client request.
423 * @param h the event handler for source. 423 * @param h the event handler for source.
424 * @param hh the event handler for hls. 424 * @param hh the event handler for hls.
425 * @param pps the matched source, if success never be NULL. 425 * @param pps the matched source, if success never be NULL.
426 */ 426 */
427 - static int create(SrsRequest* r, ISrsSourceHandler* h, ISrsHlsHandler* hh, SrsSource** pps); 427 + static int fetch_or_create(SrsRequest* r, ISrsSourceHandler* h, ISrsHlsHandler* hh, SrsSource** pps);
  428 +private:
428 /** 429 /**
429 * get the exists source, NULL when not exists. 430 * get the exists source, NULL when not exists.
430 * update the request and return the exists source. 431 * update the request and return the exists source.
431 */ 432 */
432 static SrsSource* fetch(SrsRequest* r); 433 static SrsSource* fetch(SrsRequest* r);
433 - /**  
434 - * get the exists source by stream info(vhost, app, stream), NULL when not exists.  
435 - */  
436 - static SrsSource* fetch(std::string vhost, std::string app, std::string stream); 434 +public:
437 /** 435 /**
438 * dispose and cycle all sources. 436 * dispose and cycle all sources.
439 */ 437 */