5

I'm trying to create a RTCPeerConnection between a camera-service written in Python with aiortc and a web front-end in javascript without an internet connection.

The camera-service accesses the usb-camera and provides the stream for two other services. For the front-end written in javascript and an image-classification service written in python. All these services run on different ports on localhost. I need to accomplish this without an internet connection.

The RTCPeerConnection between the two python services works without an internet connection, but the connection between the front-end and the camera-service does not work.

Without an internet connection the ICEConnectionState of the camera-service is never complete only checking, so I think here is some kind of problem.

To reproduce this problem simply try the webcam example of aiortc (https://github.com/aiortc/aiortc/tree/master/examples/webcam) with and without an internet connection.

front-end in 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);
    });
}

camera-service in 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

image-classification service in 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")

Result with image-classification service: RTC Connection works with or without being connected to the internet.

Expected same result for the front-end but front-end RTC connection is not working without internet connection.

qube13
  • 85
  • 2
  • 11

0 Answers0