瀏覽代碼

obs-outputs: Improve librtmp timeouts on Linux

Replaces the usage of SO_SNDTIMEO with TCP_USER_TIMEOUT on Linux.
- Noted as more effective than SO_SNDTIMEO by multiple sources.
- Drops TCP connection on timeout instead of returning send() errors,
meaning more predictable reconnect timing and reconnect.
- Timeout period actually reflected in practice. SO_SNDTIMEO takes
double the timeout time than actually requested to time out on
unix, whereas WinSock actually waits the specified time.
tt2468 3 年之前
父節點
當前提交
70f33f7794
共有 1 個文件被更改,包括 14 次插入4 次删除
  1. 14 4
      plugins/obs-outputs/librtmp/rtmp.c

+ 14 - 4
plugins/obs-outputs/librtmp/rtmp.c

@@ -85,7 +85,16 @@ static const char *my_dhm_G = "4";
 #include <openssl/bio.h>
 #include <openssl/buffer.h>
 #endif
+#endif
 
+#if defined(TCP_USER_TIMEOUT)
+#define SOCKET_LEVEL IPPROTO_TCP
+#define SOCKET_TIMEOUT_OPT TCP_USER_TIMEOUT
+#define SOCKET_TIMEOUT_VAR(tv, s) int tv = s*1000
+#else
+#define SOCKET_LEVEL SOL_SOCKET
+#define SOCKET_TIMEOUT_OPT SO_SNDTIMEO
+#define SOCKET_TIMEOUT_VAR(tv, s) SET_RCVTIMEO(tv, s)
 #endif
 
 #define RTMP_SIG_SIZE 1536
@@ -931,13 +940,14 @@ RTMP_Connect0(RTMP *r, struct sockaddr * service, socklen_t addrlen)
                      __FUNCTION__, r->Link.receiveTimeout);
         }
 
-        SET_RCVTIMEO(tvs, r->Link.sendTimeout);
-        if (setsockopt
-                (r->m_sb.sb_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&tvs, sizeof(tvs)))
+#if defined(SOCKET_TIMEOUT_OPT)
+		SOCKET_TIMEOUT_VAR(to, r->Link.sendTimeout);
+		if (setsockopt(r->m_sb.sb_socket, SOCKET_LEVEL, SOCKET_TIMEOUT_OPT, &to, sizeof(to)))
         {
-                RTMP_Log(RTMP_LOGERROR, "%s, Setting socket send timeout to %ds failed!",
+                RTMP_Log(RTMP_LOGERROR, "%s, Setting socket SOCKET_TIMEOUT_OPT to %ds failed!",
                      __FUNCTION__, r->Link.sendTimeout);
         }
+#endif
     }
 
     if(!r->m_bUseNagle)