mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
for bug #194, add fds poll, just sleep to send without recv.
This commit is contained in:
parent
133cc62b51
commit
164f632b62
4 changed files with 146 additions and 14 deletions
|
@ -24,14 +24,17 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
#include <srs_app_poll.hpp>
|
#include <srs_app_poll.hpp>
|
||||||
|
|
||||||
#include <srs_kernel_error.hpp>
|
#include <srs_kernel_error.hpp>
|
||||||
|
#include <srs_kernel_log.hpp>
|
||||||
|
|
||||||
SrsPoll::SrsPoll()
|
SrsPoll::SrsPoll()
|
||||||
{
|
{
|
||||||
|
_pds = NULL;
|
||||||
pthread = new SrsThread(this, 0, false);
|
pthread = new SrsThread(this, 0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsPoll::~SrsPoll()
|
SrsPoll::~SrsPoll()
|
||||||
{
|
{
|
||||||
|
srs_freep(_pds);
|
||||||
srs_freep(pthread);
|
srs_freep(pthread);
|
||||||
fds.clear();
|
fds.clear();
|
||||||
}
|
}
|
||||||
|
@ -44,10 +47,84 @@ int SrsPoll::start()
|
||||||
int SrsPoll::cycle()
|
int SrsPoll::cycle()
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
// TODO: FIXME: implements it.
|
|
||||||
|
if (fds.size() == 0) {
|
||||||
|
st_usleep(SRS_CONSTS_RTMP_PULSE_TIMEOUT_US);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int nb_pds = (int)fds.size();
|
||||||
|
|
||||||
|
st_usleep(SRS_CONSTS_RTMP_PULSE_TIMEOUT_US);
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
srs_freep(_pds);
|
||||||
|
_pds = new pollfd[nb_pds];
|
||||||
|
|
||||||
|
if (true) {
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
std::map<int, SrsPollFD*>::iterator it;
|
||||||
|
for (it = fds.begin(); it != fds.end(); ++it) {
|
||||||
|
int fd = it->first;
|
||||||
|
|
||||||
|
pollfd& pfd = _pds[index++];
|
||||||
|
pfd.fd = fd;
|
||||||
|
pfd.events = POLLIN;
|
||||||
|
pfd.revents = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
srs_assert(index == (int)fds.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (st_poll(_pds, nb_pds, ST_UTIME_NO_TIMEOUT) <= 0) {
|
||||||
|
srs_warn("ignore st_poll failed, size=%d", nb_pds);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < nb_pds; i++) {
|
||||||
|
if (!(_pds[i].revents & POLLIN)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fd = _pds[i].fd;
|
||||||
|
if (fds.find(fd) == fds.end()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
SrsPollFD* owner = fds[fd];
|
||||||
|
owner->set_active(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int SrsPoll::add(st_netfd_t stfd, SrsPollFD* owner)
|
||||||
|
{
|
||||||
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
int fd = st_netfd_fileno(stfd);
|
||||||
|
if (fds.find(fd) != fds.end()) {
|
||||||
|
ret = ERROR_RTMP_POLL_FD_DUPLICATED;
|
||||||
|
srs_error("fd exists, fd=%d, ret=%d", fd, ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
fds[fd] = owner;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SrsPoll::remove(st_netfd_t stfd, SrsPollFD* owner)
|
||||||
|
{
|
||||||
|
std::map<int, SrsPollFD*>::iterator it;
|
||||||
|
|
||||||
|
int fd = st_netfd_fileno(stfd);
|
||||||
|
if ((it = fds.find(fd)) != fds.end()) {
|
||||||
|
fds.erase(it);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SrsPoll* SrsPoll::_instance = new SrsPoll();
|
SrsPoll* SrsPoll::_instance = new SrsPoll();
|
||||||
|
|
||||||
SrsPoll* SrsPoll::instance()
|
SrsPoll* SrsPoll::instance()
|
||||||
|
@ -58,10 +135,15 @@ SrsPoll* SrsPoll::instance()
|
||||||
SrsPollFD::SrsPollFD()
|
SrsPollFD::SrsPollFD()
|
||||||
{
|
{
|
||||||
_stfd = NULL;
|
_stfd = NULL;
|
||||||
|
_active = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsPollFD::~SrsPollFD()
|
SrsPollFD::~SrsPollFD()
|
||||||
{
|
{
|
||||||
|
if (_stfd) {
|
||||||
|
SrsPoll* poll = SrsPoll::instance();
|
||||||
|
poll->remove(_stfd, this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int SrsPollFD::initialize(st_netfd_t stfd)
|
int SrsPollFD::initialize(st_netfd_t stfd)
|
||||||
|
@ -70,6 +152,22 @@ int SrsPollFD::initialize(st_netfd_t stfd)
|
||||||
|
|
||||||
_stfd = stfd;
|
_stfd = stfd;
|
||||||
|
|
||||||
|
SrsPoll* poll = SrsPoll::instance();
|
||||||
|
if ((ret = poll->add(stfd, this)) != ERROR_SUCCESS) {
|
||||||
|
srs_error("add fd to poll failed. ret=%d", ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SrsPollFD::active()
|
||||||
|
{
|
||||||
|
return _active;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SrsPollFD::set_active(bool v)
|
||||||
|
{
|
||||||
|
_active = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,8 @@ class SrsPoll : public ISrsThreadHandler
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
SrsThread* pthread;
|
SrsThread* pthread;
|
||||||
std::map<st_netfd_t, SrsPollFD*> fds;
|
pollfd* _pds;
|
||||||
|
std::map<int, SrsPollFD*> fds;
|
||||||
public:
|
public:
|
||||||
SrsPoll();
|
SrsPoll();
|
||||||
virtual ~SrsPoll();
|
virtual ~SrsPoll();
|
||||||
|
@ -59,6 +60,15 @@ public:
|
||||||
* start an cycle thread.
|
* start an cycle thread.
|
||||||
*/
|
*/
|
||||||
virtual int cycle();
|
virtual int cycle();
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* add the fd to poll.
|
||||||
|
*/
|
||||||
|
virtual int add(st_netfd_t stfd, SrsPollFD* owner);
|
||||||
|
/**
|
||||||
|
* remove the fd to poll, ignore any error.
|
||||||
|
*/
|
||||||
|
virtual void remove(st_netfd_t stfd, SrsPollFD* owner);
|
||||||
// singleton
|
// singleton
|
||||||
private:
|
private:
|
||||||
static SrsPoll* _instance;
|
static SrsPoll* _instance;
|
||||||
|
@ -73,6 +83,8 @@ class SrsPollFD
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
st_netfd_t _stfd;
|
st_netfd_t _stfd;
|
||||||
|
// whether current fd is active.
|
||||||
|
bool _active;
|
||||||
public:
|
public:
|
||||||
SrsPollFD();
|
SrsPollFD();
|
||||||
virtual ~SrsPollFD();
|
virtual ~SrsPollFD();
|
||||||
|
@ -82,6 +94,15 @@ public:
|
||||||
* @param stfd the fd to poll.
|
* @param stfd the fd to poll.
|
||||||
*/
|
*/
|
||||||
virtual int initialize(st_netfd_t stfd);
|
virtual int initialize(st_netfd_t stfd);
|
||||||
|
/**
|
||||||
|
* whether fd is active.
|
||||||
|
*/
|
||||||
|
virtual bool active();
|
||||||
|
/**
|
||||||
|
* the poll will set to fd active when got data to read,
|
||||||
|
* the connection will set to deactive when data read.
|
||||||
|
*/
|
||||||
|
virtual void set_active(bool v);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -517,26 +517,31 @@ int SrsRtmpConn::playing(SrsSource* source)
|
||||||
SrsAutoFree(SrsConsumer, consumer);
|
SrsAutoFree(SrsConsumer, consumer);
|
||||||
srs_verbose("consumer created success.");
|
srs_verbose("consumer created success.");
|
||||||
|
|
||||||
rtmp->set_recv_timeout(SRS_CONSTS_RTMP_PULSE_TIMEOUT_US);
|
// use poll fd to manage the connection, read when active.
|
||||||
|
SrsPollFD poll_fd;
|
||||||
SrsPithyPrint pithy_print(SRS_CONSTS_STAGE_PLAY_USER);
|
if ((ret = poll_fd.initialize(stfd)) != ERROR_SUCCESS) {
|
||||||
|
|
||||||
SrsSharedPtrMessageArray msgs(SYS_MAX_PLAY_SEND_MSGS);
|
|
||||||
|
|
||||||
bool user_specified_duration_to_stop = (req->duration > 0);
|
|
||||||
int64_t starttime = -1;
|
|
||||||
|
|
||||||
SrsPollFD poll;
|
|
||||||
if ((ret = poll.initialize(stfd)) != ERROR_SUCCESS) {
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: FIXME: remove following.
|
||||||
|
//rtmp->set_recv_timeout(SRS_CONSTS_RTMP_PULSE_TIMEOUT_US);
|
||||||
|
rtmp->set_recv_timeout(ST_UTIME_NO_TIMEOUT);
|
||||||
|
rtmp->set_send_timeout(ST_UTIME_NO_TIMEOUT);
|
||||||
|
|
||||||
|
// initialize other components
|
||||||
|
SrsPithyPrint pithy_print(SRS_CONSTS_STAGE_PLAY_USER);
|
||||||
|
SrsSharedPtrMessageArray msgs(SYS_MAX_PLAY_SEND_MSGS);
|
||||||
|
bool user_specified_duration_to_stop = (req->duration > 0);
|
||||||
|
int64_t starttime = -1;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
// collect elapse for pithy print.
|
// collect elapse for pithy print.
|
||||||
pithy_print.elapse();
|
pithy_print.elapse();
|
||||||
|
|
||||||
// read from client.
|
// read from client.
|
||||||
if (true) {
|
if (poll_fd.active()) {
|
||||||
|
poll_fd.set_active(false);
|
||||||
|
|
||||||
SrsMessage* msg = NULL;
|
SrsMessage* msg = NULL;
|
||||||
ret = rtmp->recv_message(&msg);
|
ret = rtmp->recv_message(&msg);
|
||||||
srs_verbose("play loop recv message. ret=%d", ret);
|
srs_verbose("play loop recv message. ret=%d", ret);
|
||||||
|
@ -566,6 +571,13 @@ int SrsRtmpConn::playing(SrsSource* source)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// no data, sleep a while.
|
||||||
|
// for the poll_fd maybe not active, and no message.
|
||||||
|
// @see: https://github.com/winlinvip/simple-rtmp-server/issues/194
|
||||||
|
if (count <= 0) {
|
||||||
|
st_usleep(SRS_CONSTS_RTMP_PULSE_TIMEOUT_US);
|
||||||
|
}
|
||||||
|
|
||||||
// reportable
|
// reportable
|
||||||
if (pithy_print.can_print()) {
|
if (pithy_print.can_print()) {
|
||||||
kbps->sample();
|
kbps->sample();
|
||||||
|
|
|
@ -185,6 +185,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
#define ERROR_EDGE_VHOST_REMOVED 3039
|
#define ERROR_EDGE_VHOST_REMOVED 3039
|
||||||
#define ERROR_HLS_AVC_TRY_OTHERS 3040
|
#define ERROR_HLS_AVC_TRY_OTHERS 3040
|
||||||
#define ERROR_H264_API_NO_PREFIXED 3041
|
#define ERROR_H264_API_NO_PREFIXED 3041
|
||||||
|
#define ERROR_RTMP_POLL_FD_DUPLICATED 3042
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* whether the error code is an system control error.
|
* whether the error code is an system control error.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue