我正在尝试在使用aiortc的Python编写的摄像头服务和没有互联网连接的javascript中的Web前端之间创建RTCPeerConnection。
相机服务访问usb相机并为其他两个服务提供流。对于用javascript编写的前端和用python编写的图像分类服务。所有这些服务都在localhost的不同端口上运行。我需要在没有互联网连接的情况下完成此操作。
两个python服务之间的RTCPeerConnection可以在没有Internet连接的情况下工作,但是前端和camera-service之间的连接不起作用。
没有互联网连接,相机服务的ICEConnectionState永远不会仅完成检查,因此我认为这是某种问题。
要重现此问题,只需尝试使用aiortc(https://github.com/aiortc/aiortc/tree/master/examples/webcam)的网络摄像头示例,无论有没有互联网连接。
JavaScript的前端
var pc = new RTCPeerConnection({
"iceServers": []
});
//start the negotiating process with camera-service(running on localhost:8080)
function negotiate() {
pc.addTransceiver('video', {direction: 'recvonly'});
return pc.createOffer().then(function(offer) {
return pc.setLocalDescription(offer);
}).then(function() {
// wait for ICE gathering to complete
return new Promise(function(resolve) {
if (pc.iceGatheringState === 'complete') {
resolve();
} else {
function checkState() {
if (pc.iceGatheringState === 'complete') {
pc.removeEventListener('icegatheringstatechange', checkState);
resolve();
}
}
pc.addEventListener('icegatheringstatechange', checkState);
}
});
}).then(function() {
var offer = pc.localDescription;
const url = 'http://localhost:8080/offer';
return fetch(url, {
body: JSON.stringify({
sdp: offer.sdp,
type: offer.type,
}),
headers: {
'Content-Type': 'application/json'
},
method: 'POST'
});
}).then(function(response) {
return response.json();
}).then(function(answer) {
return pc.setRemoteDescription(answer);
}).catch(function(e) {
alert(e);
});
}
python中的相机服务
# method gets called on POST request to localhost:8080/offer
async def offer(request):
params = await request.json()
offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])
pc = RTCPeerConnection(RTCConfiguration(iceServers=[]))
pcs.add(pc)
@pc.on("iceconnectionstatechange")
async def on_iceconnectionstatechange():
print("ICE connection state is %s" % pc.iceConnectionState)
if pc.iceConnectionState == "failed":
await pc.close()
pcs.discard(pc)
pc.addTrack(player.video)
await pc.setRemoteDescription(offer)
answer = await pc.createAnswer()
await pc.setLocalDescription(answer)
response = web.Response(
content_type="application/json",
body=json.dumps({
"sdp": pc.localDescription.sdp,
"type": pc.localDescription.type}))
return response
python中的图像分类服务
async def negotiate(request):
pc = RTCPeerConnection()
pcs.add(pc)
pc.addTransceiver('video',direction='recvonly')
offer = await pc.createOffer()
await pc.setLocalDescription(offer)
data = json.dumps({
"sdp": pc.localDescription.sdp,
"type": pc.localDescription.type})
response = requests.post("http://localhost:8080/offer",data=data).json()
answer = RTCSessionDescription(sdp=response["sdp"],type=response["type"])
await pc.setRemoteDescription(answer)
return web.Response(content_type="text/plain", text="Just text")
图像分类服务的结果: 无论是否连接到Internet,RTC Connection都可以工作。
对于前端,预期的结果相同,但是如果没有Internet连接,前端RTC连接将无法正常工作。