Пример #1
0
def object_to_string(obj):
    if isinstance(obj, RTCSessionDescription):
        message = {'sdp': obj.sdp, 'type': obj.type}
    elif isinstance(obj, RTCIceCandidate):
        message = {
            'candidate': 'candidate:' + candidate_to_sdp(obj),
            'id': obj.sdpMid,
            'label': obj.sdpMLineIndex,
            'type': 'candidate',
        }
    else:
        message = {'type': 'bye'}
    return json.dumps(message, sort_keys=True)
Пример #2
0
def object_to_string(obj):
    if isinstance(obj, RTCSessionDescription):
        message = {"sdp": obj.sdp, "type": obj.type}
    elif isinstance(obj, RTCIceCandidate):
        message = {
            "candidate": "candidate:" + candidate_to_sdp(obj),
            "id": obj.sdpMid,
            "label": obj.sdpMLineIndex,
            "type": "candidate",
        }
    else:
        message = {"type": "bye"}
    return json.dumps(message, sort_keys=True)
async def object_to_string(obj):
    if isinstance(obj, RTCSessionDescription):
        message = {"sdp": {"sdp": obj.sdp, "type": obj.type}}
    elif isinstance(obj, RTCIceCandidate):
        if hasattr(obj, 'label'):
            message = {
                "candidate": "candidate:" + candidate_to_sdp(obj),
                "id": obj.id, #obj.sdpMid,
                "label": obj.label, #obj.sdpMLineIndex,
                "type": "candidate",
            }
        else:
            message = {
                "candidate": "candidate:" + candidate_to_sdp(obj),
                "id": obj.sdpMid,
                "label": obj.sdpMLineIndex,
                "type": "candidate",
            }

    else:
        return obj
    return message
Пример #4
0
def object_to_dict(obj):
    """Convert RTC object to dict."""
    if isinstance(obj, RTCSessionDescription):
        message = {"sdp": obj.sdp, "type": obj.type}
    elif isinstance(obj, RTCIceCandidate):
        message = {
            "candidate": "candidate:" + candidate_to_sdp(obj),
            "id": obj.sdpMid,
            "label": obj.sdpMLineIndex,
            "type": "candidate",
        }
    else:
        message = {"type": "bye"}
    return message
Пример #5
0
def object_to_string(obj, source: str):
    if isinstance(obj, RTCSessionDescription):
        data = {
            'sdp': obj.sdp,
            'type': obj.type
        }
    elif isinstance(obj, RTCIceCandidate):
        data = {
            'candidate': 'candidate:' + candidate_to_sdp(obj),
            'id': obj.sdpMid,
            'label': obj.sdpMLineIndex,
            'type': 'candidate',
        }
    else:
        raise ValueError('Can only send RTCSessionDescription or RTCIceCandidate')
    message = {
        'source': source,
        'data': data
    }
    return json.dumps(message, sort_keys=True)
Пример #6
0
    async def on_receive(self, websocket, message):
        if self.encoding == "json":
            iceTransport = websocket.state.iceTransport
            iceParameters = iceTransport.iceGatherer.getLocalParameters()
            dtlsTransport = websocket.state.dtlsTransport
            dtlsParameters = dtlsTransport.getLocalParameters()

            coros = map(
                iceTransport.addRemoteCandidate,
                map(candidate_from_sdp, message["candidates"]),
            )
            await asyncio.gather(*coros)

            await websocket.send_json({
                "ice": {
                    "usernameFragment": iceParameters.usernameFragment,
                    "password": iceParameters.password,
                },
                "dtls": {
                    "role":
                    "auto",
                    "fingerprints":
                    list(
                        map(
                            lambda fp: {
                                "algorithm": fp.algorithm,
                                "value": fp.value,
                            },
                            dtlsParameters.fingerprints,
                        )),
                },
                "candidates":
                list(
                    map(
                        lambda c: "candidate:" + candidate_to_sdp(c),
                        iceTransport.iceGatherer.getLocalCandidates(),
                    )),
            })

            remoteIceParameters = RTCIceParameters(
                usernameFragment=message["ice"]["usernameFragment"],
                password=message["ice"]["password"],
            )
            remoteDtlsParameters = RTCDtlsParameters(fingerprints=list(
                map(
                    lambda fp: RTCDtlsFingerprint(algorithm=fp["algorithm"],
                                                  value=fp["value"]),
                    message["dtls"]["fingerprints"],
                )))

            await iceTransport.iceGatherer.gather()

            iceTransport._connection.ice_controlling = False

            await iceTransport.start(remoteIceParameters)
            await dtlsTransport.start(remoteDtlsParameters)
            self.encoding = "bytes"
        elif self.encoding == "bytes":
            await websocket.state.dtlsTransport._send_rtp(message)
        else:
            print("Unhandled encoding", self.encoding)