winlin

fix bug of edge, refine state to user state.

@@ -309,6 +309,7 @@ int SrsEdgeIngester::connect_server() @@ -309,6 +309,7 @@ int SrsEdgeIngester::connect_server()
309 SrsEdge::SrsEdge() 309 SrsEdge::SrsEdge()
310 { 310 {
311 state = SrsEdgeStateInit; 311 state = SrsEdgeStateInit;
  312 + user_state = SrsEdgeUserStateInit;
312 ingester = new SrsEdgeIngester(); 313 ingester = new SrsEdgeIngester();
313 } 314 }
314 315
@@ -333,9 +334,10 @@ int SrsEdge::on_client_play() @@ -333,9 +334,10 @@ int SrsEdge::on_client_play()
333 int ret = ERROR_SUCCESS; 334 int ret = ERROR_SUCCESS;
334 335
335 // error state. 336 // error state.
336 - if (state == SrsEdgeStateAborting || state == SrsEdgeStateReloading) { 337 + if (user_state != SrsEdgeUserStateInit) {
337 ret = ERROR_RTMP_EDGE_PLAY_STATE; 338 ret = ERROR_RTMP_EDGE_PLAY_STATE;
338 - srs_error("invalid state for client to play stream on edge. state=%d, ret=%d", state, ret); 339 + srs_error("invalid state for client to play stream on edge. "
  340 + "state=%d, user_state=%d, ret=%d", state, user_state, ret);
339 return ret; 341 return ret;
340 } 342 }
341 343
@@ -350,22 +352,32 @@ int SrsEdge::on_client_play() @@ -350,22 +352,32 @@ int SrsEdge::on_client_play()
350 352
351 void SrsEdge::on_all_client_stop() 353 void SrsEdge::on_all_client_stop()
352 { 354 {
353 - if (state == SrsEdgeStateIngestConnected) { 355 + // when all client disconnected,
  356 + // and edge is ingesting origin stream, abort it.
  357 + if (state == SrsEdgeStatePlay || state == SrsEdgeStateIngestConnected) {
354 ingester->stop(); 358 ingester->stop();
355 - }  
356 359
357 - SrsEdgeState pstate = state;  
358 - state = SrsEdgeStateInit;  
359 - srs_trace("edge change from %d to state %d (init).", pstate, state); 360 + SrsEdgeState pstate = state;
  361 + state = SrsEdgeStateInit;
  362 + srs_trace("edge change from %d to state %d (init).", pstate, state);
  363 +
  364 + return;
  365 + }
360 } 366 }
361 367
362 int SrsEdge::on_ingest_play() 368 int SrsEdge::on_ingest_play()
363 { 369 {
364 int ret = ERROR_SUCCESS; 370 int ret = ERROR_SUCCESS;
365 371
  372 + // when already connected(for instance, reconnect for error), ignore.
  373 + if (state == SrsEdgeStateIngestConnected) {
  374 + return ret;
  375 + }
  376 +
  377 + srs_assert(state == SrsEdgeStatePlay);
  378 +
366 SrsEdgeState pstate = state; 379 SrsEdgeState pstate = state;
367 state = SrsEdgeStateIngestConnected; 380 state = SrsEdgeStateIngestConnected;
368 -  
369 srs_trace("edge change from %d to state %d (ingest connected).", pstate, state); 381 srs_trace("edge change from %d to state %d (ingest connected).", pstate, state);
370 382
371 return ret; 383 return ret;
@@ -41,7 +41,7 @@ class SrsCommonMessage; @@ -41,7 +41,7 @@ class SrsCommonMessage;
41 class ISrsProtocolReaderWriter; 41 class ISrsProtocolReaderWriter;
42 42
43 /** 43 /**
44 -* the state of edge 44 +* the state of edge, auto machine
45 */ 45 */
46 enum SrsEdgeState 46 enum SrsEdgeState
47 { 47 {
@@ -52,8 +52,15 @@ enum SrsEdgeState @@ -52,8 +52,15 @@ enum SrsEdgeState
52 SrsEdgeStateIngestConnected, 52 SrsEdgeStateIngestConnected,
53 // publish stream to edge, forward to origin 53 // publish stream to edge, forward to origin
54 SrsEdgeStateForwardConnected, 54 SrsEdgeStateForwardConnected,
55 - SrsEdgeStateAborting,  
56 - SrsEdgeStateReloading, 55 +};
  56 +
  57 +/**
  58 +* the state of edge from user, manual machine
  59 +*/
  60 +enum SrsEdgeUserState
  61 +{
  62 + SrsEdgeUserStateInit = 0,
  63 + SrsEdgeUserStateReloading = 100,
57 }; 64 };
58 65
59 /** 66 /**
@@ -96,6 +103,7 @@ class SrsEdge @@ -96,6 +103,7 @@ class SrsEdge
96 { 103 {
97 private: 104 private:
98 SrsEdgeState state; 105 SrsEdgeState state;
  106 + SrsEdgeUserState user_state;
99 SrsEdgeIngester* ingester; 107 SrsEdgeIngester* ingester;
100 public: 108 public:
101 SrsEdge(); 109 SrsEdge();
@@ -89,6 +89,9 @@ int SrsThread::start() @@ -89,6 +89,9 @@ int SrsThread::start()
89 return ret; 89 return ret;
90 } 90 }
91 91
  92 + // we set to loop to true for thread to run.
  93 + loop = true;
  94 +
92 // wait for cid to ready, for parent thread to get the cid. 95 // wait for cid to ready, for parent thread to get the cid.
93 while (_cid < 0) { 96 while (_cid < 0) {
94 st_usleep(10 * SRS_TIME_MILLISECONDS); 97 st_usleep(10 * SRS_TIME_MILLISECONDS);
@@ -130,7 +133,6 @@ void SrsThread::thread_cycle() @@ -130,7 +133,6 @@ void SrsThread::thread_cycle()
130 srs_assert(handler); 133 srs_assert(handler);
131 handler->on_thread_start(); 134 handler->on_thread_start();
132 135
133 - loop = true;  
134 while (loop) { 136 while (loop) {
135 if ((ret = handler->on_before_cycle()) != ERROR_SUCCESS) { 137 if ((ret = handler->on_before_cycle()) != ERROR_SUCCESS) {
136 srs_warn("thread on before cycle failed, ignored and retry, ret=%d", ret); 138 srs_warn("thread on before cycle failed, ignored and retry, ret=%d", ret);
@@ -109,6 +109,7 @@ public: @@ -109,6 +109,7 @@ public:
109 * user stop the thread. 109 * user stop the thread.
110 * @remark ignore any error of cycle of handler. 110 * @remark ignore any error of cycle of handler.
111 * @remark user can start multiple times, ignore if already started. 111 * @remark user can start multiple times, ignore if already started.
  112 + * @remark wait for the cid is set by thread pfn.
112 */ 113 */
113 virtual int start(); 114 virtual int start();
114 /** 115 /**