正在显示
9 个修改的文件
包含
336 行增加
和
52 行删除
| @@ -70,7 +70,7 @@ SrsEdgeIngester::SrsEdgeIngester() | @@ -70,7 +70,7 @@ SrsEdgeIngester::SrsEdgeIngester() | ||
| 70 | origin_index = 0; | 70 | origin_index = 0; |
| 71 | stream_id = 0; | 71 | stream_id = 0; |
| 72 | stfd = NULL; | 72 | stfd = NULL; |
| 73 | - pthread = new SrsReusableThread("edge-igs", this, SRS_EDGE_INGESTER_SLEEP_US); | 73 | + pthread = new SrsReusableThread2("edge-igs", this, SRS_EDGE_INGESTER_SLEEP_US); |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | SrsEdgeIngester::~SrsEdgeIngester() | 76 | SrsEdgeIngester::~SrsEdgeIngester() |
| @@ -397,7 +397,7 @@ SrsEdgeForwarder::SrsEdgeForwarder() | @@ -397,7 +397,7 @@ SrsEdgeForwarder::SrsEdgeForwarder() | ||
| 397 | origin_index = 0; | 397 | origin_index = 0; |
| 398 | stream_id = 0; | 398 | stream_id = 0; |
| 399 | stfd = NULL; | 399 | stfd = NULL; |
| 400 | - pthread = new SrsReusableThread("edge-fwr", this, SRS_EDGE_FORWARDER_SLEEP_US); | 400 | + pthread = new SrsReusableThread2("edge-fwr", this, SRS_EDGE_FORWARDER_SLEEP_US); |
| 401 | queue = new SrsMessageQueue(); | 401 | queue = new SrsMessageQueue(); |
| 402 | send_error_code = ERROR_SUCCESS; | 402 | send_error_code = ERROR_SUCCESS; |
| 403 | } | 403 | } |
| @@ -75,7 +75,7 @@ enum SrsEdgeUserState | @@ -75,7 +75,7 @@ enum SrsEdgeUserState | ||
| 75 | /** | 75 | /** |
| 76 | * edge used to ingest stream from origin. | 76 | * edge used to ingest stream from origin. |
| 77 | */ | 77 | */ |
| 78 | -class SrsEdgeIngester : public ISrsReusableThreadHandler | 78 | +class SrsEdgeIngester : public ISrsReusableThread2Handler |
| 79 | { | 79 | { |
| 80 | private: | 80 | private: |
| 81 | int stream_id; | 81 | int stream_id; |
| @@ -83,7 +83,7 @@ private: | @@ -83,7 +83,7 @@ private: | ||
| 83 | SrsSource* _source; | 83 | SrsSource* _source; |
| 84 | SrsPlayEdge* _edge; | 84 | SrsPlayEdge* _edge; |
| 85 | SrsRequest* _req; | 85 | SrsRequest* _req; |
| 86 | - SrsReusableThread* pthread; | 86 | + SrsReusableThread2* pthread; |
| 87 | st_netfd_t stfd; | 87 | st_netfd_t stfd; |
| 88 | ISrsProtocolReaderWriter* io; | 88 | ISrsProtocolReaderWriter* io; |
| 89 | SrsKbps* kbps; | 89 | SrsKbps* kbps; |
| @@ -96,7 +96,7 @@ public: | @@ -96,7 +96,7 @@ public: | ||
| 96 | virtual int initialize(SrsSource* source, SrsPlayEdge* edge, SrsRequest* req); | 96 | virtual int initialize(SrsSource* source, SrsPlayEdge* edge, SrsRequest* req); |
| 97 | virtual int start(); | 97 | virtual int start(); |
| 98 | virtual void stop(); | 98 | virtual void stop(); |
| 99 | -// interface ISrsReusableThreadHandler | 99 | +// interface ISrsReusableThread2Handler |
| 100 | public: | 100 | public: |
| 101 | virtual int cycle(); | 101 | virtual int cycle(); |
| 102 | private: | 102 | private: |
| @@ -110,7 +110,7 @@ private: | @@ -110,7 +110,7 @@ private: | ||
| 110 | /** | 110 | /** |
| 111 | * edge used to forward stream to origin. | 111 | * edge used to forward stream to origin. |
| 112 | */ | 112 | */ |
| 113 | -class SrsEdgeForwarder : public ISrsReusableThreadHandler | 113 | +class SrsEdgeForwarder : public ISrsReusableThread2Handler |
| 114 | { | 114 | { |
| 115 | private: | 115 | private: |
| 116 | int stream_id; | 116 | int stream_id; |
| @@ -118,7 +118,7 @@ private: | @@ -118,7 +118,7 @@ private: | ||
| 118 | SrsSource* _source; | 118 | SrsSource* _source; |
| 119 | SrsPublishEdge* _edge; | 119 | SrsPublishEdge* _edge; |
| 120 | SrsRequest* _req; | 120 | SrsRequest* _req; |
| 121 | - SrsReusableThread* pthread; | 121 | + SrsReusableThread2* pthread; |
| 122 | st_netfd_t stfd; | 122 | st_netfd_t stfd; |
| 123 | ISrsProtocolReaderWriter* io; | 123 | ISrsProtocolReaderWriter* io; |
| 124 | SrsKbps* kbps; | 124 | SrsKbps* kbps; |
| @@ -144,7 +144,7 @@ public: | @@ -144,7 +144,7 @@ public: | ||
| 144 | virtual int initialize(SrsSource* source, SrsPublishEdge* edge, SrsRequest* req); | 144 | virtual int initialize(SrsSource* source, SrsPublishEdge* edge, SrsRequest* req); |
| 145 | virtual int start(); | 145 | virtual int start(); |
| 146 | virtual void stop(); | 146 | virtual void stop(); |
| 147 | -// interface ISrsReusableThreadHandler | 147 | +// interface ISrsReusableThread2Handler |
| 148 | public: | 148 | public: |
| 149 | virtual int cycle(); | 149 | virtual int cycle(); |
| 150 | public: | 150 | public: |
| @@ -59,7 +59,7 @@ SrsForwarder::SrsForwarder(SrsSource* _source) | @@ -59,7 +59,7 @@ SrsForwarder::SrsForwarder(SrsSource* _source) | ||
| 59 | kbps = new SrsKbps(); | 59 | kbps = new SrsKbps(); |
| 60 | stream_id = 0; | 60 | stream_id = 0; |
| 61 | 61 | ||
| 62 | - pthread = new SrsReusableThread("forward", this, SRS_FORWARDER_SLEEP_US); | 62 | + pthread = new SrsReusableThread2("forward", this, SRS_FORWARDER_SLEEP_US); |
| 63 | queue = new SrsMessageQueue(); | 63 | queue = new SrsMessageQueue(); |
| 64 | jitter = new SrsRtmpJitter(); | 64 | jitter = new SrsRtmpJitter(); |
| 65 | 65 |
| @@ -48,7 +48,7 @@ class SrsKbps; | @@ -48,7 +48,7 @@ class SrsKbps; | ||
| 48 | * forward the stream to other servers. | 48 | * forward the stream to other servers. |
| 49 | */ | 49 | */ |
| 50 | // TODO: FIXME: refine the error log, comments it. | 50 | // TODO: FIXME: refine the error log, comments it. |
| 51 | -class SrsForwarder : public ISrsReusableThreadHandler | 51 | +class SrsForwarder : public ISrsReusableThread2Handler |
| 52 | { | 52 | { |
| 53 | private: | 53 | private: |
| 54 | // the ep to forward, server[:port]. | 54 | // the ep to forward, server[:port]. |
| @@ -57,7 +57,7 @@ private: | @@ -57,7 +57,7 @@ private: | ||
| 57 | int stream_id; | 57 | int stream_id; |
| 58 | private: | 58 | private: |
| 59 | st_netfd_t stfd; | 59 | st_netfd_t stfd; |
| 60 | - SrsReusableThread* pthread; | 60 | + SrsReusableThread2* pthread; |
| 61 | private: | 61 | private: |
| 62 | SrsSource* source; | 62 | SrsSource* source; |
| 63 | ISrsProtocolReaderWriter* io; | 63 | ISrsProtocolReaderWriter* io; |
| @@ -95,7 +95,7 @@ public: | @@ -95,7 +95,7 @@ public: | ||
| 95 | * @param shared_video, directly ptr, copy it if need to save it. | 95 | * @param shared_video, directly ptr, copy it if need to save it. |
| 96 | */ | 96 | */ |
| 97 | virtual int on_video(SrsSharedPtrMessage* shared_video); | 97 | virtual int on_video(SrsSharedPtrMessage* shared_video); |
| 98 | -// interface ISrsReusableThreadHandler. | 98 | +// interface ISrsReusableThread2Handler. |
| 99 | public: | 99 | public: |
| 100 | virtual int cycle(); | 100 | virtual int cycle(); |
| 101 | private: | 101 | private: |
| @@ -156,26 +156,24 @@ int SrsUdpListener::listen() | @@ -156,26 +156,24 @@ int SrsUdpListener::listen() | ||
| 156 | int SrsUdpListener::cycle() | 156 | int SrsUdpListener::cycle() |
| 157 | { | 157 | { |
| 158 | int ret = ERROR_SUCCESS; | 158 | int ret = ERROR_SUCCESS; |
| 159 | + | ||
| 160 | + // TODO: FIXME: support ipv6, @see man 7 ipv6 | ||
| 161 | + sockaddr_in from; | ||
| 162 | + int nb_from = sizeof(sockaddr_in); | ||
| 163 | + int nread = 0; | ||
| 164 | + | ||
| 165 | + if ((nread = st_recvfrom(_stfd, buf, nb_buf, (sockaddr*)&from, &nb_from, ST_UTIME_NO_TIMEOUT)) <= 0) { | ||
| 166 | + srs_warn("ignore recv udp packet failed, nread=%d", nread); | ||
| 167 | + return ret; | ||
| 168 | + } | ||
| 159 | 169 | ||
| 160 | - while (!pthread->interrupted()) { | ||
| 161 | - // TODO: FIXME: support ipv6, @see man 7 ipv6 | ||
| 162 | - sockaddr_in from; | ||
| 163 | - int nb_from = sizeof(sockaddr_in); | ||
| 164 | - int nread = 0; | ||
| 165 | - | ||
| 166 | - if ((nread = st_recvfrom(_stfd, buf, nb_buf, (sockaddr*)&from, &nb_from, ST_UTIME_NO_TIMEOUT)) <= 0) { | ||
| 167 | - srs_warn("ignore recv udp packet failed, nread=%d", nread); | ||
| 168 | - continue; | ||
| 169 | - } | ||
| 170 | - | ||
| 171 | - if ((ret = handler->on_udp_packet(&from, buf, nread)) != ERROR_SUCCESS) { | ||
| 172 | - srs_warn("handle udp packet failed. ret=%d", ret); | ||
| 173 | - continue; | ||
| 174 | - } | ||
| 175 | - | ||
| 176 | - if (SRS_UDP_PACKET_RECV_CYCLE_INTERVAL_MS > 0) { | ||
| 177 | - st_usleep(SRS_UDP_PACKET_RECV_CYCLE_INTERVAL_MS * 1000); | ||
| 178 | - } | 170 | + if ((ret = handler->on_udp_packet(&from, buf, nread)) != ERROR_SUCCESS) { |
| 171 | + srs_warn("handle udp packet failed. ret=%d", ret); | ||
| 172 | + return ret; | ||
| 173 | + } | ||
| 174 | + | ||
| 175 | + if (SRS_UDP_PACKET_RECV_CYCLE_INTERVAL_MS > 0) { | ||
| 176 | + st_usleep(SRS_UDP_PACKET_RECV_CYCLE_INTERVAL_MS * 1000); | ||
| 179 | } | 177 | } |
| 180 | 178 | ||
| 181 | return ret; | 179 | return ret; |
| @@ -50,7 +50,7 @@ SrsRecvThread::SrsRecvThread(ISrsMessageHandler* msg_handler, SrsRtmpServer* rtm | @@ -50,7 +50,7 @@ SrsRecvThread::SrsRecvThread(ISrsMessageHandler* msg_handler, SrsRtmpServer* rtm | ||
| 50 | timeout = timeout_ms; | 50 | timeout = timeout_ms; |
| 51 | handler = msg_handler; | 51 | handler = msg_handler; |
| 52 | rtmp = rtmp_sdk; | 52 | rtmp = rtmp_sdk; |
| 53 | - trd = new SrsReusableThread("recv", this); | 53 | + trd = new SrsReusableThread2("recv", this); |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | SrsRecvThread::~SrsRecvThread() | 56 | SrsRecvThread::~SrsRecvThread() |
| @@ -72,6 +72,11 @@ void SrsRecvThread::stop() | @@ -72,6 +72,11 @@ void SrsRecvThread::stop() | ||
| 72 | trd->stop(); | 72 | trd->stop(); |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | +void SrsRecvThread::stop_loop() | ||
| 76 | +{ | ||
| 77 | + trd->interrupt(); | ||
| 78 | +} | ||
| 79 | + | ||
| 75 | int SrsRecvThread::cycle() | 80 | int SrsRecvThread::cycle() |
| 76 | { | 81 | { |
| 77 | int ret = ERROR_SUCCESS; | 82 | int ret = ERROR_SUCCESS; |
| @@ -109,11 +114,6 @@ int SrsRecvThread::cycle() | @@ -109,11 +114,6 @@ int SrsRecvThread::cycle() | ||
| 109 | return ret; | 114 | return ret; |
| 110 | } | 115 | } |
| 111 | 116 | ||
| 112 | -void SrsRecvThread::stop_loop() | ||
| 113 | -{ | ||
| 114 | - trd->interrupt(); | ||
| 115 | -} | ||
| 116 | - | ||
| 117 | void SrsRecvThread::on_thread_start() | 117 | void SrsRecvThread::on_thread_start() |
| 118 | { | 118 | { |
| 119 | // the multiple messages writev improve performance large, | 119 | // the multiple messages writev improve performance large, |
| @@ -79,10 +79,10 @@ public: | @@ -79,10 +79,10 @@ public: | ||
| 79 | /** | 79 | /** |
| 80 | * the recv thread, use message handler to handle each received message. | 80 | * the recv thread, use message handler to handle each received message. |
| 81 | */ | 81 | */ |
| 82 | -class SrsRecvThread : public ISrsReusableThreadHandler | 82 | +class SrsRecvThread : public ISrsReusableThread2Handler |
| 83 | { | 83 | { |
| 84 | protected: | 84 | protected: |
| 85 | - SrsReusableThread* trd; | 85 | + SrsReusableThread2* trd; |
| 86 | ISrsMessageHandler* handler; | 86 | ISrsMessageHandler* handler; |
| 87 | SrsRtmpServer* rtmp; | 87 | SrsRtmpServer* rtmp; |
| 88 | int timeout; | 88 | int timeout; |
| @@ -92,9 +92,10 @@ public: | @@ -92,9 +92,10 @@ public: | ||
| 92 | public: | 92 | public: |
| 93 | virtual int start(); | 93 | virtual int start(); |
| 94 | virtual void stop(); | 94 | virtual void stop(); |
| 95 | - virtual int cycle(); | ||
| 96 | virtual void stop_loop(); | 95 | virtual void stop_loop(); |
| 96 | +// interface ISrsReusableThread2Handler | ||
| 97 | public: | 97 | public: |
| 98 | + virtual int cycle(); | ||
| 98 | virtual void on_thread_start(); | 99 | virtual void on_thread_start(); |
| 99 | virtual void on_thread_stop(); | 100 | virtual void on_thread_stop(); |
| 100 | }; | 101 | }; |
| @@ -239,6 +239,24 @@ ISrsEndlessThreadHandler::~ISrsEndlessThreadHandler() | @@ -239,6 +239,24 @@ ISrsEndlessThreadHandler::~ISrsEndlessThreadHandler() | ||
| 239 | { | 239 | { |
| 240 | } | 240 | } |
| 241 | 241 | ||
| 242 | +void ISrsEndlessThreadHandler::on_thread_start() | ||
| 243 | +{ | ||
| 244 | +} | ||
| 245 | + | ||
| 246 | +int ISrsEndlessThreadHandler::on_before_cycle() | ||
| 247 | +{ | ||
| 248 | + return ERROR_SUCCESS; | ||
| 249 | +} | ||
| 250 | + | ||
| 251 | +int ISrsEndlessThreadHandler::on_end_cycle() | ||
| 252 | +{ | ||
| 253 | + return ERROR_SUCCESS; | ||
| 254 | +} | ||
| 255 | + | ||
| 256 | +void ISrsEndlessThreadHandler::on_thread_stop() | ||
| 257 | +{ | ||
| 258 | +} | ||
| 259 | + | ||
| 242 | SrsEndlessThread::SrsEndlessThread(const char* n, ISrsEndlessThreadHandler* h) | 260 | SrsEndlessThread::SrsEndlessThread(const char* n, ISrsEndlessThreadHandler* h) |
| 243 | { | 261 | { |
| 244 | handler = h; | 262 | handler = h; |
| @@ -261,6 +279,26 @@ int SrsEndlessThread::cycle() | @@ -261,6 +279,26 @@ int SrsEndlessThread::cycle() | ||
| 261 | return handler->cycle(); | 279 | return handler->cycle(); |
| 262 | } | 280 | } |
| 263 | 281 | ||
| 282 | +void SrsEndlessThread::on_thread_start() | ||
| 283 | +{ | ||
| 284 | + handler->on_thread_start(); | ||
| 285 | +} | ||
| 286 | + | ||
| 287 | +int SrsEndlessThread::on_before_cycle() | ||
| 288 | +{ | ||
| 289 | + return handler->on_before_cycle(); | ||
| 290 | +} | ||
| 291 | + | ||
| 292 | +int SrsEndlessThread::on_end_cycle() | ||
| 293 | +{ | ||
| 294 | + return handler->on_end_cycle(); | ||
| 295 | +} | ||
| 296 | + | ||
| 297 | +void SrsEndlessThread::on_thread_stop() | ||
| 298 | +{ | ||
| 299 | + handler->on_thread_stop(); | ||
| 300 | +} | ||
| 301 | + | ||
| 264 | ISrsOneCycleThreadHandler::ISrsOneCycleThreadHandler() | 302 | ISrsOneCycleThreadHandler::ISrsOneCycleThreadHandler() |
| 265 | { | 303 | { |
| 266 | } | 304 | } |
| @@ -269,6 +307,20 @@ ISrsOneCycleThreadHandler::~ISrsOneCycleThreadHandler() | @@ -269,6 +307,20 @@ ISrsOneCycleThreadHandler::~ISrsOneCycleThreadHandler() | ||
| 269 | { | 307 | { |
| 270 | } | 308 | } |
| 271 | 309 | ||
| 310 | +void ISrsOneCycleThreadHandler::on_thread_start() | ||
| 311 | +{ | ||
| 312 | +} | ||
| 313 | + | ||
| 314 | +int ISrsOneCycleThreadHandler::on_before_cycle() | ||
| 315 | +{ | ||
| 316 | + return ERROR_SUCCESS; | ||
| 317 | +} | ||
| 318 | + | ||
| 319 | +int ISrsOneCycleThreadHandler::on_end_cycle() | ||
| 320 | +{ | ||
| 321 | + return ERROR_SUCCESS; | ||
| 322 | +} | ||
| 323 | + | ||
| 272 | void ISrsOneCycleThreadHandler::on_thread_stop() | 324 | void ISrsOneCycleThreadHandler::on_thread_stop() |
| 273 | { | 325 | { |
| 274 | } | 326 | } |
| @@ -297,6 +349,21 @@ int SrsOneCycleThread::cycle() | @@ -297,6 +349,21 @@ int SrsOneCycleThread::cycle() | ||
| 297 | return ret; | 349 | return ret; |
| 298 | } | 350 | } |
| 299 | 351 | ||
| 352 | +void SrsOneCycleThread::on_thread_start() | ||
| 353 | +{ | ||
| 354 | + handler->on_thread_start(); | ||
| 355 | +} | ||
| 356 | + | ||
| 357 | +int SrsOneCycleThread::on_before_cycle() | ||
| 358 | +{ | ||
| 359 | + return handler->on_before_cycle(); | ||
| 360 | +} | ||
| 361 | + | ||
| 362 | +int SrsOneCycleThread::on_end_cycle() | ||
| 363 | +{ | ||
| 364 | + return handler->on_end_cycle(); | ||
| 365 | +} | ||
| 366 | + | ||
| 300 | void SrsOneCycleThread::on_thread_stop() | 367 | void SrsOneCycleThread::on_thread_stop() |
| 301 | { | 368 | { |
| 302 | handler->on_thread_stop(); | 369 | handler->on_thread_stop(); |
| @@ -310,6 +377,20 @@ ISrsReusableThreadHandler::~ISrsReusableThreadHandler() | @@ -310,6 +377,20 @@ ISrsReusableThreadHandler::~ISrsReusableThreadHandler() | ||
| 310 | { | 377 | { |
| 311 | } | 378 | } |
| 312 | 379 | ||
| 380 | +void ISrsReusableThreadHandler::on_thread_start() | ||
| 381 | +{ | ||
| 382 | +} | ||
| 383 | + | ||
| 384 | +int ISrsReusableThreadHandler::on_before_cycle() | ||
| 385 | +{ | ||
| 386 | + return ERROR_SUCCESS; | ||
| 387 | +} | ||
| 388 | + | ||
| 389 | +int ISrsReusableThreadHandler::on_end_cycle() | ||
| 390 | +{ | ||
| 391 | + return ERROR_SUCCESS; | ||
| 392 | +} | ||
| 393 | + | ||
| 313 | void ISrsReusableThreadHandler::on_thread_stop() | 394 | void ISrsReusableThreadHandler::on_thread_stop() |
| 314 | { | 395 | { |
| 315 | } | 396 | } |
| @@ -341,22 +422,116 @@ int SrsReusableThread::cid() | @@ -341,22 +422,116 @@ int SrsReusableThread::cid() | ||
| 341 | return pthread->cid(); | 422 | return pthread->cid(); |
| 342 | } | 423 | } |
| 343 | 424 | ||
| 344 | -void SrsReusableThread::interrupt() | 425 | +int SrsReusableThread::cycle() |
| 426 | +{ | ||
| 427 | + return handler->cycle(); | ||
| 428 | +} | ||
| 429 | + | ||
| 430 | +void SrsReusableThread::on_thread_start() | ||
| 431 | +{ | ||
| 432 | + handler->on_thread_start(); | ||
| 433 | +} | ||
| 434 | + | ||
| 435 | +int SrsReusableThread::on_before_cycle() | ||
| 436 | +{ | ||
| 437 | + return handler->on_before_cycle(); | ||
| 438 | +} | ||
| 439 | + | ||
| 440 | +int SrsReusableThread::on_end_cycle() | ||
| 441 | +{ | ||
| 442 | + return handler->on_end_cycle(); | ||
| 443 | +} | ||
| 444 | + | ||
| 445 | +void SrsReusableThread::on_thread_stop() | ||
| 446 | +{ | ||
| 447 | + handler->on_thread_stop(); | ||
| 448 | +} | ||
| 449 | + | ||
| 450 | +ISrsReusableThread2Handler::ISrsReusableThread2Handler() | ||
| 451 | +{ | ||
| 452 | +} | ||
| 453 | + | ||
| 454 | +ISrsReusableThread2Handler::~ISrsReusableThread2Handler() | ||
| 455 | +{ | ||
| 456 | +} | ||
| 457 | + | ||
| 458 | +void ISrsReusableThread2Handler::on_thread_start() | ||
| 459 | +{ | ||
| 460 | +} | ||
| 461 | + | ||
| 462 | +int ISrsReusableThread2Handler::on_before_cycle() | ||
| 463 | +{ | ||
| 464 | + return ERROR_SUCCESS; | ||
| 465 | +} | ||
| 466 | + | ||
| 467 | +int ISrsReusableThread2Handler::on_end_cycle() | ||
| 468 | +{ | ||
| 469 | + return ERROR_SUCCESS; | ||
| 470 | +} | ||
| 471 | + | ||
| 472 | +void ISrsReusableThread2Handler::on_thread_stop() | ||
| 473 | +{ | ||
| 474 | +} | ||
| 475 | + | ||
| 476 | +SrsReusableThread2::SrsReusableThread2(const char* n, ISrsReusableThread2Handler* h, int64_t interval_us) | ||
| 477 | +{ | ||
| 478 | + handler = h; | ||
| 479 | + pthread = new internal::SrsThread(n, this, interval_us, true); | ||
| 480 | +} | ||
| 481 | + | ||
| 482 | +SrsReusableThread2::~SrsReusableThread2() | ||
| 483 | +{ | ||
| 484 | + pthread->stop(); | ||
| 485 | + srs_freep(pthread); | ||
| 486 | +} | ||
| 487 | + | ||
| 488 | +int SrsReusableThread2::start() | ||
| 489 | +{ | ||
| 490 | + return pthread->start(); | ||
| 491 | +} | ||
| 492 | + | ||
| 493 | +void SrsReusableThread2::stop() | ||
| 494 | +{ | ||
| 495 | + pthread->stop(); | ||
| 496 | +} | ||
| 497 | + | ||
| 498 | +int SrsReusableThread2::cid() | ||
| 499 | +{ | ||
| 500 | + return pthread->cid(); | ||
| 501 | +} | ||
| 502 | + | ||
| 503 | +void SrsReusableThread2::interrupt() | ||
| 345 | { | 504 | { |
| 346 | pthread->stop_loop(); | 505 | pthread->stop_loop(); |
| 347 | } | 506 | } |
| 348 | 507 | ||
| 349 | -bool SrsReusableThread::interrupted() | 508 | +bool SrsReusableThread2::interrupted() |
| 350 | { | 509 | { |
| 351 | return !pthread->can_loop(); | 510 | return !pthread->can_loop(); |
| 352 | } | 511 | } |
| 353 | 512 | ||
| 354 | -int SrsReusableThread::cycle() | 513 | +int SrsReusableThread2::cycle() |
| 355 | { | 514 | { |
| 356 | return handler->cycle(); | 515 | return handler->cycle(); |
| 357 | } | 516 | } |
| 358 | 517 | ||
| 359 | -void SrsReusableThread::on_thread_stop() | 518 | +void SrsReusableThread2::on_thread_start() |
| 519 | +{ | ||
| 520 | + handler->on_thread_start(); | ||
| 521 | +} | ||
| 522 | + | ||
| 523 | +int SrsReusableThread2::on_before_cycle() | ||
| 524 | +{ | ||
| 525 | + return handler->on_before_cycle(); | ||
| 526 | +} | ||
| 527 | + | ||
| 528 | +int SrsReusableThread2::on_end_cycle() | ||
| 529 | +{ | ||
| 530 | + return handler->on_end_cycle(); | ||
| 531 | +} | ||
| 532 | + | ||
| 533 | +void SrsReusableThread2::on_thread_stop() | ||
| 360 | { | 534 | { |
| 361 | handler->on_thread_stop(); | 535 | handler->on_thread_stop(); |
| 362 | } | 536 | } |
| 537 | + |
| @@ -184,6 +184,15 @@ public: | @@ -184,6 +184,15 @@ public: | ||
| 184 | * @remark user must use block method in cycle method, for example, sleep or socket io. | 184 | * @remark user must use block method in cycle method, for example, sleep or socket io. |
| 185 | */ | 185 | */ |
| 186 | virtual int cycle() = 0; | 186 | virtual int cycle() = 0; |
| 187 | +public: | ||
| 188 | + /** | ||
| 189 | + * other callback for handler. | ||
| 190 | + * @remark all callback is optional, handler can ignore it. | ||
| 191 | + */ | ||
| 192 | + virtual void on_thread_start(); | ||
| 193 | + virtual int on_before_cycle(); | ||
| 194 | + virtual int on_end_cycle(); | ||
| 195 | + virtual void on_thread_stop(); | ||
| 187 | }; | 196 | }; |
| 188 | class SrsEndlessThread : public internal::ISrsThreadHandler | 197 | class SrsEndlessThread : public internal::ISrsThreadHandler |
| 189 | { | 198 | { |
| @@ -201,6 +210,10 @@ public: | @@ -201,6 +210,10 @@ public: | ||
| 201 | // interface internal::ISrsThreadHandler | 210 | // interface internal::ISrsThreadHandler |
| 202 | public: | 211 | public: |
| 203 | virtual int cycle(); | 212 | virtual int cycle(); |
| 213 | + virtual void on_thread_start(); | ||
| 214 | + virtual int on_before_cycle(); | ||
| 215 | + virtual int on_end_cycle(); | ||
| 216 | + virtual void on_thread_stop(); | ||
| 204 | }; | 217 | }; |
| 205 | 218 | ||
| 206 | /** | 219 | /** |
| @@ -236,10 +249,14 @@ public: | @@ -236,10 +249,14 @@ public: | ||
| 236 | * the cycle method for the one cycle thread. | 249 | * the cycle method for the one cycle thread. |
| 237 | */ | 250 | */ |
| 238 | virtual int cycle() = 0; | 251 | virtual int cycle() = 0; |
| 252 | +public: | ||
| 239 | /** | 253 | /** |
| 240 | - * when thread stop, the handler can do cleanup. | ||
| 241 | - * @remark this method is optional, handler can ignore it. | 254 | + * other callback for handler. |
| 255 | + * @remark all callback is optional, handler can ignore it. | ||
| 242 | */ | 256 | */ |
| 257 | + virtual void on_thread_start(); | ||
| 258 | + virtual int on_before_cycle(); | ||
| 259 | + virtual int on_end_cycle(); | ||
| 243 | virtual void on_thread_stop(); | 260 | virtual void on_thread_stop(); |
| 244 | }; | 261 | }; |
| 245 | class SrsOneCycleThread : public internal::ISrsThreadHandler | 262 | class SrsOneCycleThread : public internal::ISrsThreadHandler |
| @@ -258,6 +275,9 @@ public: | @@ -258,6 +275,9 @@ public: | ||
| 258 | // interface internal::ISrsThreadHandler | 275 | // interface internal::ISrsThreadHandler |
| 259 | public: | 276 | public: |
| 260 | virtual int cycle(); | 277 | virtual int cycle(); |
| 278 | + virtual void on_thread_start(); | ||
| 279 | + virtual int on_before_cycle(); | ||
| 280 | + virtual int on_end_cycle(); | ||
| 261 | virtual void on_thread_stop(); | 281 | virtual void on_thread_stop(); |
| 262 | }; | 282 | }; |
| 263 | 283 | ||
| @@ -266,11 +286,11 @@ public: | @@ -266,11 +286,11 @@ public: | ||
| 266 | * user can create thread and stop then start again and again, | 286 | * user can create thread and stop then start again and again, |
| 267 | * generally must provides a start and stop method, @see SrsIngester. | 287 | * generally must provides a start and stop method, @see SrsIngester. |
| 268 | * the step to create a thread stop by other thread: | 288 | * the step to create a thread stop by other thread: |
| 269 | - * 1. create SrsThread field, with joinable true. | ||
| 270 | - * 2. must use stop to stop and join the thread. | 289 | + * 1. create SrsReusableThread field. |
| 290 | + * 2. must manually stop the thread when started it. | ||
| 271 | * for example: | 291 | * for example: |
| 272 | - * class SrsIngester : public ISrsThreadHandler { | ||
| 273 | - * public: SrsIngester() { pthread = new SrsThread("ingest", this, SRS_AUTO_INGESTER_SLEEP_US, true); } | 292 | + * class SrsIngester : public ISrsReusableThreadHandler { |
| 293 | + * public: SrsIngester() { pthread = new SrsReusableThread("ingest", this, SRS_AUTO_INGESTER_SLEEP_US); } | ||
| 274 | * public: virtual int start() { return pthread->start(); } | 294 | * public: virtual int start() { return pthread->start(); } |
| 275 | * public: virtual void stop() { pthread->stop(); } | 295 | * public: virtual void stop() { pthread->stop(); } |
| 276 | * public: virtual int cycle() { | 296 | * public: virtual int cycle() { |
| @@ -290,10 +310,14 @@ public: | @@ -290,10 +310,14 @@ public: | ||
| 290 | * the thread is interrupted. | 310 | * the thread is interrupted. |
| 291 | */ | 311 | */ |
| 292 | virtual int cycle() = 0; | 312 | virtual int cycle() = 0; |
| 313 | +public: | ||
| 293 | /** | 314 | /** |
| 294 | - * when thread stop, the handler can do cleanup. | ||
| 295 | - * @remark this method is optional, handler can ignore it. | 315 | + * other callback for handler. |
| 316 | + * @remark all callback is optional, handler can ignore it. | ||
| 296 | */ | 317 | */ |
| 318 | + virtual void on_thread_start(); | ||
| 319 | + virtual int on_before_cycle(); | ||
| 320 | + virtual int on_end_cycle(); | ||
| 297 | virtual void on_thread_stop(); | 321 | virtual void on_thread_stop(); |
| 298 | }; | 322 | }; |
| 299 | class SrsReusableThread : public internal::ISrsThreadHandler | 323 | class SrsReusableThread : public internal::ISrsThreadHandler |
| @@ -321,6 +345,89 @@ public: | @@ -321,6 +345,89 @@ public: | ||
| 321 | * @remark when start thread, parent thread will block and wait for this id ready. | 345 | * @remark when start thread, parent thread will block and wait for this id ready. |
| 322 | */ | 346 | */ |
| 323 | virtual int cid(); | 347 | virtual int cid(); |
| 348 | +// interface internal::ISrsThreadHandler | ||
| 349 | +public: | ||
| 350 | + virtual int cycle(); | ||
| 351 | + virtual void on_thread_start(); | ||
| 352 | + virtual int on_before_cycle(); | ||
| 353 | + virtual int on_end_cycle(); | ||
| 354 | + virtual void on_thread_stop(); | ||
| 355 | +}; | ||
| 356 | + | ||
| 357 | +/** | ||
| 358 | + * the reuse thread is a thread stop and start by other thread. | ||
| 359 | + * the version 2, is the thread cycle has its inner loop, which should | ||
| 360 | + * check the intterrupt, and should interrupt thread when the inner loop want | ||
| 361 | + * to quit the thread. | ||
| 362 | + * user can create thread and stop then start again and again, | ||
| 363 | + * generally must provides a start and stop method, @see SrsIngester. | ||
| 364 | + * the step to create a thread stop by other thread: | ||
| 365 | + * 1. create SrsReusableThread field. | ||
| 366 | + * 2. must manually stop the thread when started it. | ||
| 367 | + * for example: | ||
| 368 | + * class SrsIngester : public ISrsReusableThreadHandler { | ||
| 369 | + * public: SrsIngester() { pthread = new SrsReusableThread("ingest", this, SRS_AUTO_INGESTER_SLEEP_US); } | ||
| 370 | + * public: virtual int start() { return pthread->start(); } | ||
| 371 | + * public: virtual void stop() { pthread->stop(); } | ||
| 372 | + * public: virtual int cycle() { | ||
| 373 | + * while (!pthread->interrupted()) { | ||
| 374 | + * // quit thread when error. | ||
| 375 | + * if (ret != ERROR_SUCCESS) { | ||
| 376 | + * pthread->interrupt(); | ||
| 377 | + * } | ||
| 378 | + * | ||
| 379 | + * // do something. | ||
| 380 | + * } | ||
| 381 | + * } | ||
| 382 | + * }; | ||
| 383 | + */ | ||
| 384 | +class ISrsReusableThread2Handler | ||
| 385 | +{ | ||
| 386 | +public: | ||
| 387 | + ISrsReusableThread2Handler(); | ||
| 388 | + virtual ~ISrsReusableThread2Handler(); | ||
| 389 | +public: | ||
| 390 | + /** | ||
| 391 | + * the cycle method for the one cycle thread. | ||
| 392 | + * @remark when the cycle has its inner loop, it must check whether | ||
| 393 | + * the thread is interrupted. | ||
| 394 | + */ | ||
| 395 | + virtual int cycle() = 0; | ||
| 396 | +public: | ||
| 397 | + /** | ||
| 398 | + * other callback for handler. | ||
| 399 | + * @remark all callback is optional, handler can ignore it. | ||
| 400 | + */ | ||
| 401 | + virtual void on_thread_start(); | ||
| 402 | + virtual int on_before_cycle(); | ||
| 403 | + virtual int on_end_cycle(); | ||
| 404 | + virtual void on_thread_stop(); | ||
| 405 | +}; | ||
| 406 | +class SrsReusableThread2 : public internal::ISrsThreadHandler | ||
| 407 | +{ | ||
| 408 | +private: | ||
| 409 | + internal::SrsThread* pthread; | ||
| 410 | + ISrsReusableThread2Handler* handler; | ||
| 411 | +public: | ||
| 412 | + SrsReusableThread2(const char* n, ISrsReusableThread2Handler* h, int64_t interval_us = 0); | ||
| 413 | + virtual ~SrsReusableThread2(); | ||
| 414 | +public: | ||
| 415 | + /** | ||
| 416 | + * for the reusable thread, start and stop by user. | ||
| 417 | + */ | ||
| 418 | + virtual int start(); | ||
| 419 | + /** | ||
| 420 | + * stop the thread, wait for the thread to terminate. | ||
| 421 | + * @remark user can stop multiple times, ignore if already stopped. | ||
| 422 | + */ | ||
| 423 | + virtual void stop(); | ||
| 424 | +public: | ||
| 425 | + /** | ||
| 426 | + * get the context id. @see: ISrsThreadContext.get_id(). | ||
| 427 | + * used for parent thread to get the id. | ||
| 428 | + * @remark when start thread, parent thread will block and wait for this id ready. | ||
| 429 | + */ | ||
| 430 | + virtual int cid(); | ||
| 324 | /** | 431 | /** |
| 325 | * interrupt the thread to stop loop. | 432 | * interrupt the thread to stop loop. |
| 326 | * we only set the loop flag to false, not really interrupt the thread. | 433 | * we only set the loop flag to false, not really interrupt the thread. |
| @@ -335,6 +442,9 @@ public: | @@ -335,6 +442,9 @@ public: | ||
| 335 | // interface internal::ISrsThreadHandler | 442 | // interface internal::ISrsThreadHandler |
| 336 | public: | 443 | public: |
| 337 | virtual int cycle(); | 444 | virtual int cycle(); |
| 445 | + virtual void on_thread_start(); | ||
| 446 | + virtual int on_before_cycle(); | ||
| 447 | + virtual int on_end_cycle(); | ||
| 338 | virtual void on_thread_stop(); | 448 | virtual void on_thread_stop(); |
| 339 | }; | 449 | }; |
| 340 | 450 |
-
请 注册 或 登录 后发表评论