1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

fix the timeout for librtmp

This commit is contained in:
winlin 2016-08-10 22:04:01 +08:00
parent 3fbe9d2442
commit e00928557e
4 changed files with 86 additions and 14 deletions

View file

@ -88,7 +88,15 @@ int main(int argc, char** argv)
exit(-2); exit(-2);
} }
rtmp = srs_rtmp_create(rtmp_url); if ((rtmp = srs_rtmp_create(rtmp_url)) == NULL) {
srs_human_trace("create rtmp failed");
ret = -1;
goto rtmp_destroy;
}
if ((ret = srs_rtmp_set_timeout(rtmp, timeout * 1000, timeout * 1000)) != 0) {
srs_human_trace("set timeout for rtmp failed. errno=%d", ret);
goto rtmp_destroy;
}
if ((ret = srs_rtmp_dns_resolve(rtmp)) != 0) { if ((ret = srs_rtmp_dns_resolve(rtmp)) != 0) {
srs_human_trace("dns resolve failed. ret=%d", ret); srs_human_trace("dns resolve failed. ret=%d", ret);

View file

@ -27,7 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// for srs-librtmp, @see https://github.com/ossrs/srs/issues/213 // for srs-librtmp, @see https://github.com/ossrs/srs/issues/213
#ifndef _WIN32 #ifndef _WIN32
#define SOCKET_ETIME ETIME #define SOCKET_ETIME EWOULDBLOCK
#define SOCKET_ECONNRESET ECONNRESET #define SOCKET_ECONNRESET ECONNRESET
#define SOCKET_ERRNO() errno #define SOCKET_ERRNO() errno
@ -160,10 +160,23 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
return ret; return ret;
} }
void srs_hijack_io_set_recv_timeout(srs_hijack_io_t ctx, int64_t timeout_us) int srs_hijack_io_set_recv_timeout(srs_hijack_io_t ctx, int64_t timeout_us)
{ {
SrsBlockSyncSocket* skt = (SrsBlockSyncSocket*)ctx; SrsBlockSyncSocket* skt = (SrsBlockSyncSocket*)ctx;
int sec = timeout_us / (1000 * 1000LL);
int microsec = timeout_us / 1000LL % 1000;
sec = srs_max(0, sec);
microsec = srs_max(0, microsec);
struct timeval tv = { sec , microsec };
if (setsockopt(skt->fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
return SOCKET_ERRNO();
}
skt->recv_timeout = timeout_us; skt->recv_timeout = timeout_us;
return ERROR_SUCCESS;
} }
int64_t srs_hijack_io_get_recv_timeout(srs_hijack_io_t ctx) int64_t srs_hijack_io_get_recv_timeout(srs_hijack_io_t ctx)
{ {
@ -175,10 +188,24 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SrsBlockSyncSocket* skt = (SrsBlockSyncSocket*)ctx; SrsBlockSyncSocket* skt = (SrsBlockSyncSocket*)ctx;
return skt->recv_bytes; return skt->recv_bytes;
} }
void srs_hijack_io_set_send_timeout(srs_hijack_io_t ctx, int64_t timeout_us) int srs_hijack_io_set_send_timeout(srs_hijack_io_t ctx, int64_t timeout_us)
{ {
SrsBlockSyncSocket* skt = (SrsBlockSyncSocket*)ctx; SrsBlockSyncSocket* skt = (SrsBlockSyncSocket*)ctx;
int sec = timeout_us / (1000 * 1000LL);
int microsec = timeout_us / 1000LL % 1000;
sec = srs_max(0, sec);
microsec = srs_max(0, microsec);
struct timeval tv = { sec , microsec };
if (setsockopt(skt->fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
return SOCKET_ERRNO();
}
skt->send_timeout = timeout_us; skt->send_timeout = timeout_us;
return ERROR_SUCCESS;
} }
int64_t srs_hijack_io_get_send_timeout(srs_hijack_io_t ctx) int64_t srs_hijack_io_get_send_timeout(srs_hijack_io_t ctx)
{ {

View file

@ -484,14 +484,6 @@ int srs_librtmp_context_resolve_host(Context* context)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
// create socket
srs_freep(context->skt);
context->skt = new SimpleSocketStream();
if ((ret = context->skt->create_socket()) != ERROR_SUCCESS) {
return ret;
}
// connect to server:port // connect to server:port
context->ip = srs_dns_resolve(context->host); context->ip = srs_dns_resolve(context->host);
if (context->ip.empty()) { if (context->ip.empty()) {
@ -540,6 +532,17 @@ srs_rtmp_t srs_rtmp_create(const char* url)
{ {
Context* context = new Context(); Context* context = new Context();
context->url = url; context->url = url;
// create socket
srs_freep(context->skt);
context->skt = new SimpleSocketStream();
if (context->skt->create_socket() != ERROR_SUCCESS) {
// free the context and return NULL
srs_freep(context);
return NULL;
}
return context; return context;
} }
@ -552,9 +555,35 @@ srs_rtmp_t srs_rtmp_create2(const char* url)
// auto append stream. // auto append stream.
context->url += "/livestream"; context->url += "/livestream";
// create socket
srs_freep(context->skt);
context->skt = new SimpleSocketStream();
if (context->skt->create_socket() != ERROR_SUCCESS) {
// free the context and return NULL
srs_freep(context);
return NULL;
}
return context; return context;
} }
int srs_rtmp_set_timeout(srs_rtmp_t rtmp, int recv_timeout_ms, int send_timeout_ms)
{
int ret = ERROR_SUCCESS;
if (!rtmp) {
return ret;
}
Context* context = (Context*)rtmp;
context->skt->set_recv_timeout(recv_timeout_ms * 1000LL);
context->skt->set_send_timeout(send_timeout_ms * 1000LL);
return ret;
}
void srs_rtmp_destroy(srs_rtmp_t rtmp) void srs_rtmp_destroy(srs_rtmp_t rtmp)
{ {
if (!rtmp) { if (!rtmp) {

View file

@ -107,6 +107,14 @@ extern srs_rtmp_t srs_rtmp_create(const char* url);
* @return a rtmp handler, or NULL if error occured. * @return a rtmp handler, or NULL if error occured.
*/ */
extern srs_rtmp_t srs_rtmp_create2(const char* url); extern srs_rtmp_t srs_rtmp_create2(const char* url);
/**
* set socket timeout
* @param recv_timeout_ms the timeout for receiving messages in ms.
* @param send_timeout_ms the timeout for sending message in ms.
*
* @return 0, success; otherswise, failed.
*/
extern int srs_rtmp_set_timeout(srs_rtmp_t rtmp, int recv_timeout_ms, int send_timeout_ms);
/** /**
* close and destroy the rtmp stack. * close and destroy the rtmp stack.
* @remark, user should never use the rtmp again. * @remark, user should never use the rtmp again.
@ -982,7 +990,7 @@ typedef void* srs_hijack_io_t;
* set the socket recv timeout. * set the socket recv timeout.
* @return 0, success; otherswise, failed. * @return 0, success; otherswise, failed.
*/ */
extern void srs_hijack_io_set_recv_timeout(srs_hijack_io_t ctx, int64_t timeout_us); extern int srs_hijack_io_set_recv_timeout(srs_hijack_io_t ctx, int64_t timeout_us);
/** /**
* get the socket recv timeout. * get the socket recv timeout.
* @return 0, success; otherswise, failed. * @return 0, success; otherswise, failed.
@ -997,7 +1005,7 @@ typedef void* srs_hijack_io_t;
* set the socket send timeout. * set the socket send timeout.
* @return 0, success; otherswise, failed. * @return 0, success; otherswise, failed.
*/ */
extern void srs_hijack_io_set_send_timeout(srs_hijack_io_t ctx, int64_t timeout_us); extern int srs_hijack_io_set_send_timeout(srs_hijack_io_t ctx, int64_t timeout_us);
/** /**
* get the socket send timeout. * get the socket send timeout.
* @return 0, success; otherswise, failed. * @return 0, success; otherswise, failed.