1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-13 11:51:57 +00:00

SRT: Refine the schedule resolution to 10ms if idle.

This commit is contained in:
winlin 2022-05-28 10:46:05 +08:00
parent b507a080b2
commit 4899be9c34
3 changed files with 17 additions and 7 deletions

View file

@ -374,15 +374,21 @@ srs_error_t SrsSrtEventLoop::cycle()
return srs_error_wrap(err, "srt listener"); return srs_error_wrap(err, "srt listener");
} }
// Check events fired, return directly. // Check and notify fired SRT events by epoll.
if ((err = srt_poller_->wait(0)) != srs_success) { //
srs_error("srt poll wait failed, err=%s", srs_error_desc(err).c_str()); // Note that the SRT poller use a dedicated and isolated epoll, which is not the same as the one of SRS, in
// short, the wait won't switch to other coroutines when no fd is active, so we must use timeout(0) to make sure
// to return directly, then use srs_usleep to do the coroutine switch.
int n_fds = 0;
if ((err = srt_poller_->wait(0, &n_fds)) != srs_success) {
srs_warn("srt poll wait failed, n_fds=%d, err=%s", n_fds, srs_error_desc(err).c_str());
srs_error_reset(err); srs_error_reset(err);
} }
// Schedule srt event by state-thread. // We use sleep to switch to other coroutines, because the SRT poller is not possible to do this.
srs_usleep(1 * SRS_UTIME_MILLISECONDS); srs_usleep((n_fds ? 1 : 10) * SRS_UTIME_MILLISECONDS);
} }
return err; return err;
} }

View file

@ -450,12 +450,14 @@ srs_error_t SrsSrtPoller::del_socket(SrsSrtSocket* srt_skt)
return err; return err;
} }
srs_error_t SrsSrtPoller::wait(int timeout_ms) srs_error_t SrsSrtPoller::wait(int timeout_ms, int* pn_fds)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
// wait srt event fired, will timeout after `timeout_ms` milliseconds. // wait srt event fired, will timeout after `timeout_ms` milliseconds.
int ret = srt_epoll_uwait(srt_epoller_fd_, events_.data(), events_.size(), timeout_ms); int ret = srt_epoll_uwait(srt_epoller_fd_, events_.data(), events_.size(), timeout_ms);
*pn_fds = ret;
if (ret < 0) { if (ret < 0) {
return srs_error_new(ERROR_SRT_EPOLL, "srt_epoll_uwait, ret=%d, err=%s", ret, srt_getlasterror_str()); return srs_error_new(ERROR_SRT_EPOLL, "srt_epoll_uwait, ret=%d, err=%s", ret, srt_getlasterror_str());
} }

View file

@ -77,7 +77,9 @@ public:
srs_error_t add_socket(SrsSrtSocket* srt_skt); srs_error_t add_socket(SrsSrtSocket* srt_skt);
srs_error_t mod_socket(SrsSrtSocket* srt_skt); srs_error_t mod_socket(SrsSrtSocket* srt_skt);
srs_error_t del_socket(SrsSrtSocket* srt_skt); srs_error_t del_socket(SrsSrtSocket* srt_skt);
srs_error_t wait(int timeout_ms); // Wait for the fds in its epoll to be fired in specified timeout_ms, where the pn_fds is the number of active fds.
// Note that for ST, please always use timeout_ms(0) and switch coroutine by yourself.
srs_error_t wait(int timeout_ms, int* pn_fds);
private: private:
// Find SrsSrtSocket* context by SRTSOCKET. // Find SrsSrtSocket* context by SRTSOCKET.
std::map<SRTSOCKET, SrsSrtSocket*> fd_sockets_; std::map<SRTSOCKET, SrsSrtSocket*> fd_sockets_;