之前做过几次公网推流的尝试, 今天试了UDP推到公网, 再用TCP从公网拉下来, 发现不行, 就直接改用TCP转TCP了.
中间中转使用的python脚本, 感谢GPT提供技术支持:
import socket
import threading
def tcp_receiver(port, forward_queue):
"""接收TCP数据并将其放入队列"""
receiver_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
receiver_socket.bind(('', port))
receiver_socket.listen(1)
print(f"TCP receiver ready on port {port}")
while True:
client_socket, addr = receiver_socket.accept()
print(f"Connected by {addr} for receiving data on port {port}")
try:
while True:
data = client_socket.recv(1024)
if not data:
break
forward_queue.put(data)
finally:
client_socket.close()
print(f"Receiver connection closed by {addr}")
def tcp_forwarder(port, forward_queue):
"""从队列中获取数据并转发到另一个TCP连接"""
forwarder_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
forwarder_socket.bind(('', port))
forwarder_socket.listen(1)
print(f"TCP forwarder ready on port {port}")
while True:
client_socket, addr = forwarder_socket.accept()
print(f"Connected by {addr} for forwarding data on port {port}")
try:
while True:
data = forward_queue.get() # This blocks until data is available
try:
client_socket.sendall(data)
except socket.error:
print("Failed to send data, discarding packet")
break # End connection if sending fails
finally:
client_socket.close()
print(f"Forwarder connection closed by {addr}")
if __name__ == '__main__':
from queue import Queue
data_queue = Queue()
receiver_thread = threading.Thread(target=tcp_receiver, args=(9998, data_queue))
forwarder_thread = threading.Thread(target=tcp_forwarder, args=(9997, data_queue))
receiver_thread.start()
forwarder_thread.start()
就是从9998端口转发数据到9997端口, 下一步就用nginx做这个好了. 不用脚本了.
rtsp的部分, 源码在下面:
https://github.com/MontaukLaw/1126_rtsp
基于官方的vi_venc_rtsp sample code, 将rtsp的部分改成直接往公网推.
播放流依然是老办法:
ffplay -i tcp://wan_ip:9997
因为我不需要缓存, 所以之前用ZLMedia转的方法, 一直会导致延迟慢慢变大, 可能是缓存的关系.
目前, 1126使用的WiFi连接的Internet, 延迟大约在1秒左右
带宽2Mbps
每秒数据约 250KB:
下一步就是使用4G网络推推看了…