我开发了一个代理,可以让我通过 http 以 udp 单播 m2tp 流的形式使用 ISP 提供的回放视频。为此,我使用 python、asyncio 和 Sanic。
它工作得很好,但有一些我无法解释的事情。某些流使用一个 CPU 核心 (i7-3770) 的不到 8%,而其他特定流则消耗高达 30%。所有流原则上都是相同的,我不对它们进行任何处理,只是从 udp 套接字读取并写入流sanic.Response,基本上是这样的:
import asyncio_dgram
import socket
from contextlib import closing
from Sanic import Sanic, response
app = Sanic()
MIME = 'video/MP2T'
@app.get('/some/url')
async def handle_url(request):
host = socket.gethostbyname(socket.gethostname())
client_port = 43545
async def my_streaming_fn(response):
with closing(await asyncio_dgram.bind((host, client_port))) as stream:
while True:
data, remote_addr = await stream.recv()
await response.write(data)
return response.stream(my_streaming_fn, content_type=MIME)
Run Code Online (Sandbox Code Playgroud)
所有流均到达单独的 VLAN,所有 ts 数据包均为 1344 字节(无标头为 1316 …