示例#1
0
async def answer(options):
    connection = aioice.Connection(ice_controlling=False,
                                   components=options.components,
                                   stun_server=STUN_SERVER)
    await connection.gather_candidates()

    websocket = await websockets.connect(WEBSOCKET_URI)

    # await offer
    message = json.loads(await websocket.recv())
    print('received offer', message)
    connection.remote_candidates = [aioice.Candidate.from_sdp(c) for c in message['candidates']]
    connection.remote_username = message['username']
    connection.remote_password = message['password']

    # send answer
    await websocket.send(json.dumps({
        'candidates': [c.to_sdp() for c in connection.local_candidates],
        'password': connection.local_password,
        'username': connection.local_username,
    }))

    await websocket.close()

    await connection.connect()
    print('connected')

    # echo data back
    data, component = await connection.recvfrom()
    print('echoing %s on component %d' % (repr(data), component))
    await connection.sendto(data, component)

    await asyncio.sleep(5)
    await connection.close()
示例#2
0
 def __createTransport(self, transceiver, controlling):
     transceiver._iceConnection = aioice.Connection(
         ice_controlling=controlling)
     transceiver._dtlsSession = dtls.DtlsSrtpSession(
         self.__dtlsContext,
         is_server=controlling,
         transport=transceiver._iceConnection)
示例#3
0
    async def offer(self, data):
        s_server = data['servers'][0].split(":")
        self.pc = RTCPeerConnection()
        self.connection = aioice.Connection(ice_controlling=True, )
        # todo play with other extensions

        if self.video.type == Types.HTTPS:
            options = {"volume": "33", "video_size": "640x480"}
            self.player = MediaPlayer(self.video.url,
                                      format="mp4",
                                      options=options)

        elif self.video.type == Types.DESKTOP:
            options = {
                "volume": "33",
                "video_size": f"{self.video.height}x{self.video.width}"
            }
            self.player = MediaPlayer(self.video.out_location,
                                      format="v4l2",
                                      options=options)

        if self.player and self.player.audio:
            self.pc.addTrack(self.player.audio)
        if self.player and self.player.video:
            self.pc.addTrack(self.player.video)

        @self.pc.on("iceconnectionstatechange")
        async def on_iceconnectionstatechange():
            print("ICE connection state is %s" % self.pc.iceConnectionState)

            if self.pc.iceConnectionState == "failed":
                await self.pc.close()
                await self.connection.close()

            if self.pc.iceConnectionState == 'completed':
                print("completed")

        STUN_SERVER = (s_server[1], int(s_server[2]))
        self.connection.stun_server = STUN_SERVER
        self.stun_server = STUN_SERVER
        await self.connection.gather_candidates()

        await self.pc.setLocalDescription(await self.pc.createOffer())
        data = {
            "tc": "sdp",
            "req": self.get_req(),
            "type": "offer",
            "sdp": self.pc.localDescription.sdp,
            "handle": 0
        }
        await self.bot.wsend(json.dumps(data))
        for c in self.connection.local_candidates:
            data = {
                "tc": "trickle",
                "req": self.get_req(),
                "candidate": "candidate:" + aioice.Candidate.to_sdp(c),
                "handle": self.bot.handle
            }
            await self.bot.wsend(json.dumps(data))
示例#4
0
async def answer(options):
    connection = aioice.Connection(ice_controlling=False,
                                   components=options.components,
                                   stun_server=STUN_SERVER)
    await connection.gather_candidates()

    websocket = await websockets.connect(WEBSOCKET_URI)

    # await offer
    message = json.loads(await websocket.recv())
    print('received offer', message)
    connection.remote_candidates = [aioice.Candidate.from_sdp(c) for c in message['candidates']]
    connection.remote_username = message['username']
    connection.remote_password = message['password']

    # send answer
    await websocket.send(json.dumps({
        'candidates': [c.to_sdp() for c in connection.local_candidates],
        'password': connection.local_password,
        'username': connection.local_username,
    }))

    await websocket.close()

    await connection.connect()
    print('connected')

    # echo data back
    data, component = await connection.recvfrom()
    print('echoing %s on component %d' % (repr(data), component))
    await connection.sendto(data, component)

    # print candidate info
    local_candidates = connection.local_candidates
    remote_candidates = connection.remote_candidates

    for candidate in local_candidates:
        logger.info(f'''local info: {candidate.host} 
                    local port: {candidate.port}
                    local type: {candidate.type}
                    local transport: {candidate.transport}
                    local priority: {candidate.priority}
                    local foundation: {candidate.foundation}
                    local component: {candidate.component}
                    ''')

    for candidate in remote_candidates:
        logger.info(f'''remote info: {candidate.host}
                    remote port: {candidate.port}
                    remote type: {candidate.type}
                    remote transport: {candidate.transport}
                    remote priority: {candidate.priority}
                    remote foundation: {candidate.foundation}
                    remote component: {candidate.component}
                    ''')

    await asyncio.sleep(5)
    await connection.close()
示例#5
0
    def __init__(self, bot):
        super().__init__(bot)

        self.create_id = transaction_id()
        self._attach_id = transaction_id()
        self._attach_plugin_id = transaction_id()
        self.publish_transaction_id = transaction_id()
        self.message_id = transaction_id()
        self.candidates_complete = transaction_id()

        self.transactions = {
            self.create_id: 'create',
            self._attach_id: 'attach',
            self._attach_plugin_id: 'attach_plugin',
            self.publish_transaction_id: 'publish',
            self.message_id: 'message',
            self.candidates_complete: 'candidates_complete',
        }

        self._plugins = {}

        self.token = ""
        self.room = ""
        self.janus_id = ""
        self.handle_id = ""
        self.username = ""
        self.password = ""
        self._session_url = None
        self._session = None

        self.player = None

        self.turnservers = None
        p = RTCIceParameters(usernameFragment="", password="", iceLite=False)
        self.connection = aioice.Connection(ice_controlling=True, )
        self.pc = RTCPeerConnection()

        @self.pc.on("signalingstatechange")
        async def on_signalingstatechange():
            print("ICE signaling state is %s" % self.pc.signalingState)

            if self.pc.iceConnectionState == "failed":
                await self.connection.close()

            if self.pc.iceConnectionState == 'completed':
                print("completed")

        @self.pc.on("iceconnectionstatechange")
        async def on_iceconnectionstatechange():
            print("ICE connection state is %s" % self.pc.iceConnectionState)

            if self.pc.iceConnectionState == "failed":
                await self.connection.close()

            if self.pc.iceConnectionState == 'completed':
                print("completed")
    async def main():
        conn_a = aioice.Connection(ice_controlling=True, stun_server=SERVEUR_STUN)
        conn_b = aioice.Connection(ice_controlling=False, stun_server=SERVEUR_STUN)

        # les candidats distants de b sont les locaux de a
        await conn_a.gather_candidates()

        for i in range(len(conn_a.local_candidates)):
            await conn_b.add_remote_candidate(Candidate.from_sdp(conn_a.local_candidates[i].to_sdp()))

        print (conn_b.remote_candidates)

        conn_b.remote_username = conn_a.local_username
        conn_b.remote_password = conn_a.local_password

        # respectivement
        await conn_b.gather_candidates()
        for j in range(len(conn_b.local_candidates)):
            await conn_a.add_remote_candidate(Candidate.from_sdp(conn_b.local_candidates[j].to_sdp()))

        print (conn_a.remote_candidates)
        conn_a.remote_username = conn_b.local_username
        conn_a.remote_password = conn_b.local_password

        # connection
        await asyncio.gather(conn_a.connect(), conn_b.connect())


        # envoie de données de a vers b
        with open(fichier_mdp_chiffres,'rb') as ef:
            donnees=ef.read()
        await conn_a.send(donnees)

        data = await conn_b.recv()
        with open (fichier_mdp_chiffres,'wb') as ef:
            ef.write(data)
        print("L'appareil distant a reçu le fichier")

        # fermeture de la connexion
        await asyncio.gather(conn_a.close(), conn_b.close())
示例#7
0
async def offer(options):
    connection = aioice.Connection(ice_controlling=True,
                                   components=options.components,
                                   stun_server=STUN_SERVER,
                                   turn_server=TURN_SERVER[0],
                                   turn_username=TURN_SERVER[1],
                                   turn_password=TURN_SERVER[2],
                                   use_ipv4=True,
                                   use_ipv6=False)
    await connection.gather_candidates()

    dump_candidates(connection)

    await asyncio.sleep(5)

    websocket = await websockets.connect(WEBSOCKET_URI)

    # send offer
    await websocket.send(
        json.dumps({
            'candidates': [c.to_sdp() for c in connection.local_candidates],
            'password':
            connection.local_password,
            'username':
            connection.local_username,
        }))

    # await answer
    message = json.loads(await websocket.recv())
    print('received answer', message)
    connection.remote_candidates = ([
        aioice.Candidate.from_sdp(c) for c in message['candidates']
    ])
    connection.remote_username = message['username']
    connection.remote_password = message['password']

    await websocket.close()

    await connection.connect()
    print('connected')

    # send data
    data = b'hello'
    component = 1
    print('sending %s on component %d' % (repr(data), component))
    await connection.sendto(data, component)
    data, component = await connection.recvfrom()
    print('received %s on component %d' % (repr(data), component))

    await asyncio.sleep(5)
    await connection.close()
示例#8
0
    def __init__(self, bot):
        super().__init__(bot)
        self.candidates = []
        self.pc = RTCPeerConnection()
        self.connection = aioice.Connection(ice_controlling=True)
        self.stun_server = None
        self.cammed = False

        @self.pc.on("iceconnectionstatechange")
        async def on_iceconnectionstatechange():
            print("ICE connection state is %s" % self.pc.iceConnectionState)

            if self.pc.iceConnectionState == "failed":
                await self.pc.close()
            if self.pc.iceGatheringState == 'complete':
                return
示例#9
0
async def offer(options):
    connection = aioice.Connection(
        ice_controlling=True, components=options.components, stun_server=STUN_SERVER
    )
    await connection.gather_candidates()

    websocket = await websockets.connect(WEBSOCKET_URI)

    # send offer
    await websocket.send(
        json.dumps(
            {
                "candidates": [c.to_sdp() for c in connection.local_candidates],
                "password": connection.local_password,
                "username": connection.local_username,
            }
        )
    )

    # await answer
    message = json.loads(await websocket.recv())
    print("received answer", message)
    connection.remote_candidates = [
        aioice.Candidate.from_sdp(c) for c in message["candidates"]
    ]
    connection.remote_username = message["username"]
    connection.remote_password = message["password"]

    await websocket.close()

    await connection.connect()
    print("connected")

    # send data
    data = b"hello"
    component = 1
    print("sending %s on component %d" % (repr(data), component))
    await connection.sendto(data, component)
    data, component = await connection.recvfrom()
    print("received %s on component %d" % (repr(data), component))

    await asyncio.sleep(5)
    await connection.close()
示例#10
0
    async def init_singaling(self):
        connection = aioice.Connection(ice_controlling=True,
                                       components=1,
                                       stun_server=self.STUN_SERVER)
        self.connection = connection
        websocket = await websockets.connect(self.SIGNAL_SERVER)
        joinCmd = json.dumps({"cmd": "joinRoom", "roomId": self.roomId})
        await websocket.send(joinCmd)
        print("> {}".format(joinCmd))
        print("Waiting other user to join..")
        response = await websocket.recv()
        print("< {}".format(response))

        resp = json.loads(response)
        respCmd = resp["cmd"]
        if respCmd == "playStream":
            print('playstream cmd received')
            await connection.gather_candidates()
            await websocket.send(
                json.dumps({
                    "cmd":
                    "candidate",
                    "candidates":
                    [c.to_sdp() for c in connection.local_candidates],
                    "password":
                    connection.local_password,
                    "username":
                    connection.local_username,
                }))
            response = await websocket.recv()
            print("< {}".format(response))

            resp = json.loads(response)
            for c in resp["candidates"]:
                await connection.add_remote_candidate(
                    aioice.Candidate.from_sdp(c))

            await connection.add_remote_candidate(None)
            connection.remote_username = resp["username"]
            connection.remote_password = resp["password"]

            await connection.connect()
            print("Connected!")

        elif respCmd == "candidate":
            print('Candidate cmd received')
            print(resp)
            for c in resp["candidates"]:
                await connection.add_remote_candidate(
                    aioice.Candidate.from_sdp(c))

            await connection.add_remote_candidate(None)
            connection.remote_username = resp["username"]
            connection.remote_password = resp["password"]

            await connection.gather_candidates()
            await websocket.send(
                json.dumps({
                    "cmd":
                    "candidate",
                    "candidates":
                    [c.to_sdp() for c in connection.local_candidates],
                    "password":
                    connection.local_password,
                    "username":
                    connection.local_username,
                }))
            await connection.connect()
            print("Connected!")
示例#11
0
async def handler():
    connection = aioice.Connection(
        ice_controlling=True, components=1, stun_server=STUN_SERVER
    )
    websocket = await websockets.connect('ws://localhost:8765')
    joinCmd = json.dumps({
        "cmd": "joinRoom",
        "roomId": "1"
    })
    await websocket.send(joinCmd)
    print("> {}".format(joinCmd))

    response = await websocket.recv()
    print("< {}".format(response))

    resp = json.loads(response)

    respCmd = resp["cmd"]
    if respCmd == "playStream":
        print('playstream')
        await connection.gather_candidates()
        await websocket.send(
            json.dumps(
                {
                    "cmd" : "candidate",
                    "candidates": [c.to_sdp() for c in connection.local_candidates],
                    "password": connection.local_password,
                    "username": connection.local_username,
                }
            )
        )
        response = await websocket.recv()
        print("< {}".format(response))

        resp = json.loads(response)
        for c in resp["candidates"]:
            await connection.add_remote_candidate(aioice.Candidate.from_sdp(c))

        await connection.add_remote_candidate(None)
        connection.remote_username = resp["username"]
        connection.remote_password = resp["password"]


        await connection.connect()
        print("connected")
        # echo data back
        data, component = await connection.recvfrom()
        print("echoing %s on component %d" % (repr(data), component))
        await connection.sendto(data, component)

    elif respCmd == "candidate":
        print('candidate')
        print(resp)
        for c in resp["candidates"]:
            await connection.add_remote_candidate(aioice.Candidate.from_sdp(c))

        await connection.add_remote_candidate(None)
        connection.remote_username = resp["username"]
        connection.remote_password = resp["password"]

        await connection.gather_candidates()
        await websocket.send(
            json.dumps(
                {
                    "cmd": "candidate",
                    "candidates": [c.to_sdp() for c in connection.local_candidates],
                    "password": connection.local_password,
                    "username": connection.local_username,
                }
            )
        )
        await connection.connect()
        print("connected")
        data = b"hello"
        component = 1
        print("sending %s on component %d" % (repr(data), component))
        await connection.sendto(data, component)
        data, component = await connection.recvfrom()
        print("received %s on component %d" % (repr(data), component))

    elif respCmd == "description":
        print('description')
        print(resp)