Exemplo n.º 1
0
 def create_peer_connection(self):
     if self.TURN_SERVER is not None:
         iceServers = []
         iceServers.append(RTCIceServer(self.STUN_SERVER))
         iceServers.append(RTCIceServer(self.TURN_SERVER, username=self.TURN_USERNAME, credential=self.TURN_PASSWORD))
         return RTCPeerConnection(RTCConfiguration(iceServers))
     return RTCPeerConnection()
Exemplo n.º 2
0
    def __init__(
        self,
        defaultChannelOrdered=True,
        loop=None,
        rtcConfiguration=RTCConfiguration(
            [RTCIceServer(urls="stun:stun.l.google.com:19302")]),
    ):
        super().__init__(
            directPutSubscriptionType=asyncio.Queue,
            defaultSubscriptionType=asyncio.Queue,
            logger=self._log,
        )
        self._loop = loop
        if self._loop is None:
            self._loop = asyncio.get_event_loop()

        self._dataChannels = {}

        # These allow us to easily signal when the given events happen
        self._dataChannelSubscriber = SubscriptionProducer(
            logger=self._log.getChild("dataChannelSubscriber"))
        self._rtc = RTCPeerConnection(configuration=rtcConfiguration)
        self._rtc.on("datachannel", self._onDatachannel)
        # self._rtc.on("iceconnectionstatechange", self._onIceConnectionStateChange)
        self._rtc.on("track", self._onTrack)

        self._hasRemoteDescription = False
        self._defaultChannelOrdered = defaultChannelOrdered

        self._videoHandler = ConnectionVideoHandler(self._rtc)
        self._audioHandler = ConnectionAudioHandler(self._rtc)
    def get_relay_config(self):

        relay_client = CommunicationRelayClient.from_connection_string(
            self.connection_string)

        print("Getting relay configuration")
        relay_configuration = relay_client.get_relay_configuration(
            route_type=RouteType.NEAREST)

        for iceServer in relay_configuration.ice_servers:
            print("Ice server:")
            print(iceServer)

        # You can now setup the RTCPeerConnection
        iceServersList = []

        # Create the list of RTCIceServers
        for iceServer in relay_configuration.ice_servers:
            iceServersList.append(
                RTCIceServer(username=iceServer.username,
                             credential=iceServer.credential,
                             urls=iceServer.urls))

        # Initialize the RTCConfiguration
        config = RTCConfiguration(iceServersList)

        # Initialize the RTCPeerConnection
        pc = RTCPeerConnection(config)
Exemplo n.º 4
0
async def record_webrtc(options, recorder):
    config = RTCConfiguration(iceServers=[])
    client = WebRTCClient(options.hostname,
                          options.username,
                          options.password,
                          options.sdp_port,
                          options.sdp_filename,
                          options.cam_ssl_cert,
                          config,
                          media_recorder=recorder,
                          recorder_type=options.track)
    await client.start()

    # wait for connection to be established before recording
    while client.pc.iceConnectionState != 'completed':
        await asyncio.sleep(0.1)

    # start recording
    await recorder.start()
    try:
        await asyncio.sleep(options.time)
    except KeyboardInterrupt:
        pass
    finally:
        # close everything
        await client.pc.close()
        await recorder.stop()
Exemplo n.º 5
0
    def get_relay_config(self):

        identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
        relay_client = CommunicationRelayClient.from_connection_string(self.connection_string)
        
        print("Creating new user")
        user = identity_client.create_user()
        
        print("User created with id:" + user.properties.get('id'))

        print("Getting relay configuration")
        relay_configuration = relay_client.get_relay_configuration(user)

        for iceServer in relay_configuration.ice_servers:
            print("Ice server:")
            print(iceServer)

        # You can now setup the RTCPeerConnection
        iceServersList = []

        # Create the list of RTCIceServers
        for iceServer in relay_configuration.ice_servers:
            iceServersList.append(RTCIceServer(username = iceServer.username, credential=iceServer.credential, urls = iceServer.urls))

        # Initialize the RTCConfiguration
        config = RTCConfiguration(iceServersList)

        # Initialize the RTCPeerConnection 
        pc = RTCPeerConnection(config)
Exemplo n.º 6
0
 async def do_sdp_offer(self):
     width = 320
     height = 240
     iceservers = RTCIceServer(self.ice_servers)
     self.ice_env = RTCPeerConnection(configuration=RTCConfiguration(
         iceServers=[iceservers]))
     #self.ice_env = RTCPeerConnection()
     print(iceservers)
     gatherer = RTCIceGatherer(iceServers=[iceservers])
     await gatherer.gather()
     __ice = RTCIceTransport(gatherer)
     print(__ice.iceGatherer)
     print(gatherer.getLocalParameters())
     print(__ice.getRemoteCandidates())
     local_video = CombinedVideoStreamTrack(tracks=[
         ColorVideoStreamTrack(width=width, height=height, color=BLUE),
         ColorVideoStreamTrack(width=width, height=height, color=GREEN),
         ColorVideoStreamTrack(width=width, height=height, color=RED),
     ])
     self.ice_env.addTrack(local_video)
     await self.ice_env.setLocalDescription(await
                                            self.ice_env.createOffer())
     print(self.ice_env.localDescription)
     payload = {
         'tc': 'sdp',
         'type': 'offer',
         'sdp':
         self.description_to_dict(self.ice_env.localDescription)['sdp'],
         'handle': self.client_id
     }
     await self.send(payload)
     await asyncio.sleep(5)
Exemplo n.º 7
0
async def create_connection(is_caller=True, desc=None):
    # def add_tracks():
    #     if player and player.audio:
    #         print("ADD AUDIO")
    #         pc.addTrack(player.audio)

    #     if player and player.video:
    #         print("ADD VIDEO")
    #         pc.addTrack(player.video)

    pc = RTCPeerConnection(configuration=RTCConfiguration(
        iceServers=[RTCIceServer(urls=['stun:stun.l.google.com:19302'])]))
    player = MediaPlayer("output.wav")
    recorder = MediaRecorder(
        '/Users/pjc/Server/webrtc-connection-test/result.wav')
    pc.addTrack(player.audio)

    @pc.on("track")
    def on_track(track):
        print(f"Track received {track.kind}")
        if track.kind == "audio":
            recorder.addTrack(track)

        @track.on("ended")
        async def on_ended():
            print(f"Track {track.kind} ended")
            await recorder.stop()

    @pc.on("datachannel")
    def on_datachannel(channel):
        @channel.on("message")
        def on_message(message):
            if isinstance(message, str) and message.startswith("ping"):
                channel.send("pong" + message[4:])

    @pc.on('icecandidate')
    def icecandidate(item):
        print("icecandidate")

    @pc.on("iceconnectionstatechange")
    async def on_iceconnectionstatechange():
        print(f"ICE connection state is {pc.iceConnectionState}")
        if pc.iceConnectionState == "failed":
            await pc.close()

    if is_caller:
        print("createOffer With Bind")
        sd = await pc.createOffer()
        await pc.setLocalDescription(sd)
        await recorder.start()
    else:
        print("Bind Offer With Create Answer")
        await pc.setRemoteDescription(desc)
        sd = await pc.createAnswer()
        await pc.setLocalDescription(sd)
        await recorder.start()

    return pc.localDescription, pc, recorder
Exemplo n.º 8
0
def start_webrtc(shutdown_flag, options):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    config = RTCConfiguration(iceServers=[])
    client = SpotCAMWebRTCClient(options, config)

    asyncio.gather(client.start(), process_frame(client, options,
                                                 shutdown_flag),
                   monitor_shutdown(shutdown_flag, client))
    loop.run_forever()
def create_connection():
    pc = RTCPeerConnection(configuration=RTCConfiguration(
            iceServers=[RTCIceServer(
                urls=['stun:stun.l.google.com:19302'])]))
    #pc = RTCPeerConnection()
    @pc.on("iceconnectionstatechange")
    async def on_iceconnectionstatechange():
        print("ICE connection state is %s", pc.iceConnectionState)
        if pc.iceConnectionState == "failed":
            await pc.close()
            pcs.clear()
    return pc
Exemplo n.º 10
0
def start_webrtc(shutdown_flag, options):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    config = RTCConfiguration(iceServers=[])
    client = WebRTCClient(options.hostname, options.username, options.password,
                          options.sdp_port, options.sdp_filename,
                          options.cam_ssl_cert, config)

    asyncio.gather(client.start(), process_frame(client, options,
                                                 shutdown_flag),
                   monitor_shutdown(shutdown_flag, client))
    loop.run_forever()
Exemplo n.º 11
0
 def set_pc(self):
     if self.iceServerInfo is None:
         self.pc = RTCPeerConnection()
     else:
         config = [
             RTCIceServer(self.iceServerInfo['stun']),
             RTCIceServer(
                 self.iceServerInfo['turn'],
                 self.iceServerInfo['credentials']['username'],
                 self.iceServerInfo['credentials']['password']
             )
         ]
         self.pc = RTCPeerConnection(RTCConfiguration(config))
     return self.pc
Exemplo n.º 12
0
    async def connect(self, host, port, turn=None):
        ice_servers = [RTCIceServer('stun:stun.l.google.com:19302')]
        if turn:
            ice_servers.append(turn)
        config = RTCConfiguration(ice_servers)
        self.pc = RTCPeerConnection(config)

        if not self.__video:
            self.__video = await self.__get_tracks()
        self.pc.addTrack(MediaRelay().subscribe(self.__video))

        offer = await self.pc.createOffer()
        await self.pc.setLocalDescription(offer)

        self.signaling = WebSocketClient(host, port)

        async def send_offer():
            logger.debug(f"Ice Gathering State: {self.pc.iceGatheringState}")
            if self.pc.iceGatheringState == 'complete':
                logger.debug("Offer sent")
                await self.signaling.send_data({
                    "sdp":
                    self.pc.localDescription.sdp,
                    "type":
                    self.pc.localDescription.type
                })
            else:
                self.pc.once("icegatheringstatechange", send_offer)

        @self.signaling.on_connected
        async def on_connected(_):
            await send_offer()

        @self.signaling.on_message
        async def on_message(message):
            logger.debug(f"{message.get('type')} received")
            if message.get("type") == "answer":
                answer = RTCSessionDescription(sdp=message["sdp"],
                                               type=message["type"])
                await self.pc.setRemoteDescription(answer)

        @self.pc.on("connectionstatechange")
        async def on_connectionstatechange():
            if self.pc.connectionState == "failed":
                await self.pc.close()
            logger.info(f"Connection state: {self.pc.connectionState}")
Exemplo n.º 13
0
    def create_connection(self):
        pc = RTCPeerConnection(configuration=RTCConfiguration(
            iceServers=[RTCIceServer(urls=['stun:stun.l.google.com:19302'])]))
        #pc = RTCPeerConnection()

        @pc.on("iceconnectionstatechange")
        async def on_iceconnectionstatechange():
            print("ICE connection state is %s", pc.iceConnectionState)
            if pc.iceConnectionState == "failed":
                await pc.close()
                pcs.clear()
                new_pc = self.create_connection()
                pcs.add(new_pc)
                # run event loop
                await self.run(pc=new_pc, player=None)

        return pc
Exemplo n.º 14
0
    def __init__(self, config: Config, track: ScreenCaptureTrack,
                 input_handler: InputHandler):
        self._config = config

        peer_connection_config = config.get_peer_connection_config()
        ice_servers: List[RTCIceServer] = [
            RTCIceServer(ice.url, ice.username, ice.credential)
            for ice in peer_connection_config.ice_servers
        ]
        self._pc = RTCPeerConnection(RTCConfiguration(iceServers=ice_servers))
        self._track = track
        self._input_handler = input_handler

        self._pc.addTrack(self._track)
        self._control_channel: RTCDataChannel = self._pc.createDataChannel(
            "control")
        self._control_channel.on("message", self._on_message)

        self._lock = asyncio.Lock()
        self._connection_task: Optional[asyncio.Task[None]] = None
Exemplo n.º 15
0
def start_peer(room=None, signaling_folder=None, play_from=None, record_to=None, 
               frame_transformer=None, verbose=False, ice_servers=None, multiprocess=False):
    
    if verbose:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    if ice_servers:
        logger.debug('Using ICE servers:', ice_servers)
        servers = [RTCIceServer(*server) if type(server) == tuple else RTCIceServer(server) for server in ice_servers]
        pc = RTCPeerConnection(
            configuration=RTCConfiguration(servers))
    else:
        pc = RTCPeerConnection()
    
    # room = str(room)
    if signaling_folder:
        signaling = ColabSignaling(signaling_folder=signaling_folder, room=room)
    else:
        signaling = ColabApprtcSignaling(room=room)
        
    # create media source
    if play_from:
        player = MediaPlayer(play_from)
    else:
        player = None

    # create media sink
    if record_to:
        recorder = MediaRecorder(record_to)
    else:
        recorder = MediaBlackhole()
        
    if multiprocess:
        p = Process(target=run_process, args=(pc, player, recorder, signaling, frame_transformer))
        p.start()
        return signaling.room, p
    else:
        run_process(pc, player, recorder, signaling, frame_transformer)
        return signaling.room, None
Exemplo n.º 16
0
async def get_RTCPeer_payload():
    pc = RTCPeerConnection(
        RTCConfiguration(
            iceServers=[RTCIceServer("stun:stun.l.google.com:19302")]))

    @pc.on("track")
    async def on_track(track):
        logger.debug("Receiving %s" % track.kind)
        if track.kind == "video":
            pc.addTrack(VideoTransformTrack(track))

        @track.on("ended")
        async def on_ended():
            logger.info("Track %s ended", track.kind)

    pc.addTransceiver("video", direction="recvonly")
    offer = await pc.createOffer()
    await pc.setLocalDescription(offer)
    new_offer = pc.localDescription
    payload = {"sdp": new_offer.sdp, "type": new_offer.type}
    return (pc, json.dumps(payload, separators=(",", ":")))
Exemplo n.º 17
0
    async def _negotiate(self, initiator):
        """
        (*Coroutine*) Handle the establishment of the WebRTC peer connection.
        """
        ice_servers = [
            RTCIceServer('stun:stun.l.google.com:19302'),
            RTCIceServer('stun:stun2.l.google.com:19302'),
            RTCIceServer('stun:stunserver.org:3478')
        ]

        self._pc = RTCPeerConnection(RTCConfiguration(ice_servers))

        async def add_datachannel_listeners():
            """
            Set the listeners to handle data channel events
            """
            @self._datachannel.on('message')
            async def on_message(message):
                try:
                    data = json.loads(message)
                except:
                    raise TypeError('Received an invalid json message data')
                self._data = data
                try:
                    for handler in self._data_handlers:
                        if inspect.iscoroutinefunction(handler):
                            await handler(data)
                        else:
                            handler(data)
                except Exception as e:
                    logging.exception(e, traceback.format_exc())
                    raise e

            @self._datachannel.on('close')
            async def on_close():
                if self.readyState == PeerState.CONNECTED:
                    logging.info('Datachannel lost, disconnecting...')
                    self.disconnection_event.set()

            @self._datachannel.on('error')
            async def on_error(error):
                logging.error('Datachannel error: ' + str(error))
                self.disconnection_event.set()

        @self._pc.on('track')
        def on_track(track):
            """
            Set the consumer or destination of the incomming video and audio tracks
            """
            logging.info('Track %s received' % track.kind)

            if track.kind == 'audio':
                #webrtc_connection.addTrack(player.audio)
                #recorder.addTrack(track)
                pass
            elif track.kind == 'video':
                #local_video = VideoTransformTrack(track, transform=signal['video_transform'])
                #webrtc_connection.addTrack(local_video)
                if self._frame_consumer_feeder:
                    self._track_consumer_task = asyncio.create_task(
                        self._frame_consumer_feeder.feed_with(track))

            @track.on('ended')
            async def on_ended():
                logging.info('Remote track %s ended' % track.kind)
                if self.readyState == PeerState.CONNECTED:
                    logging.info('Remote media track ended, disconnecting...')
                    self.disconnection_event.set()
                #await recorder.stop()

        @self._pc.on('iceconnectionstatechange')
        async def on_iceconnectionstatechange():
            """
            Monitor the ICE connection state
            """
            logging.info('ICE connection state of peer (%s) is %s', self.id,
                         self._pc.iceConnectionState)
            if self._pc.iceConnectionState == 'failed':
                self.disconnection_event.set()
            elif self._pc.iceConnectionState == 'completed':
                # self._set_readyState(PeerState.CONNECTED)
                pass

        # Add media tracks
        if self._media_source:
            if self._media_source_format:
                self._player = MediaPlayer(self._media_source,
                                           format=self._media_source_format)
            else:
                self._player = MediaPlayer(self._media_source)
            if self._player.audio:
                self._pc.addTrack(self._player.audio)
            if self._player.video:
                self._pc.addTrack(self._player.video)

                @self._player.video.on('ended')
                async def on_ended():
                    logging.info('Local track %s ended' %
                                 self._player.video.kind)
                    if self.readyState == PeerState.CONNECTED:
                        logging.info('disconnecting...')
                        self.disconnection_event.set()

                logging.info('Video player track added')
        elif self._frame_generator:
            if inspect.isgeneratorfunction(self._frame_generator):
                self._pc.addTrack(
                    FrameGeneratorTrack(self._frame_generator,
                                        frame_rate=self._frame_rate))
                logging.info('Video frame generator track added')
            else:
                logging.info('No video track to add')

        if initiator:
            logging.info('Initiating peer connection...')
            do = self._datachannel_options
            if do:
                self._datachannel = self._pc.createDataChannel(
                    do['label'], do['maxPacketLifeTime'], do['maxRetransmits'],
                    do['ordered'], do['protocol'])
            else:
                self._datachannel = self._pc.createDataChannel('data_channel')
            await self._pc.setLocalDescription(await self._pc.createOffer())

            signal = {
                'sdp': self._pc.localDescription.sdp,
                'type': self._pc.localDescription.type
            }
            await self._send(signal)
            signal = await self._get_signal()
            if signal['type'] != 'answer':
                raise Exception('Expected answer from remote peer', signal)
            answer = RTCSessionDescription(sdp=signal['sdp'],
                                           type=signal['type'])
            await self._pc.setRemoteDescription(answer)

            @self._datachannel.on('open')
            async def on_open():
                self._set_readyState(PeerState.CONNECTED)
                await add_datachannel_listeners()
                pass  #asyncio.ensure_future(send_pings())
        else:
            logging.info('Waiting for peer connection...')

            @self._pc.on('datachannel')
            async def on_datachannel(channel):
                self._datachannel = channel
                self._set_readyState(PeerState.CONNECTED)
                await add_datachannel_listeners()

            signal = await self._get_signal()
            if signal['type'] != 'offer':
                raise Exception('Expected offer from remote peer', signal)
            offer = RTCSessionDescription(sdp=signal['sdp'],
                                          type=signal['type'])
            await self._pc.setRemoteDescription(offer)
            answer = await self._pc.createAnswer()
            await self._pc.setLocalDescription(answer)
            answer = {
                'sdp': self._pc.localDescription.sdp,
                'type': self._pc.localDescription.type
            }
            await self._send(answer)

        logging.info('starting _handle_candidates_task...')
        self._handle_candidates_task = asyncio.create_task(
            self._handle_ice_candidates())
        logging.info('sending local ice candidates...')

        # ice_servers = RTCIceGatherer.getDefaultIceServers()
        logging.debug(f'ice_servers: {ice_servers}')
        ice_gatherer = RTCIceGatherer(ice_servers)
        local_candidates = ice_gatherer.getLocalCandidates()
        logging.debug(f'local_candidates: {local_candidates}')
        for candidate in local_candidates:
            sdp = (
                f"{candidate.foundation} {candidate.component} {candidate.protocol} "
                f"{candidate.priority} {candidate.ip} {candidate.port} typ {candidate.type}"
            )

            if candidate.relatedAddress is not None:
                sdp += f" raddr {candidate.relatedAddress}"
            if candidate.relatedPort is not None:
                sdp += f" rport {candidate.relatedPort}"
            if candidate.tcpType is not None:
                sdp += f" tcptype {candidate.tcpType}"
            message = {
                "candidate": "candidate:" + sdp,
                "id": candidate.sdpMid,
                "label": candidate.sdpMLineIndex,
                "type": "candidate",
            }
            logging.info(message)
            await self._send(message)

        while self.readyState == PeerState.CONNECTING:
            await asyncio.sleep(0.2)

        if self._track_consumer_task:
            logging.info('starting _remote_track_monitor_task...')
            self._remote_track_monitor_task = asyncio.create_task(
                self._remote_track_monitor())

        self._connection_monitor_task = asyncio.create_task(
            self._connection_monitor())
Exemplo n.º 18
0
    async def accept(self, port, segment_time, server=None, turn=None):
        ice_servers = [RTCIceServer('stun:stun.l.google.com:19302')]
        if turn:
            ice_servers.append(turn)
        config = RTCConfiguration(ice_servers)
        self.pc = RTCPeerConnection(config)
        if server:
            self.signaling = WebSocketClient(server, port)
        else:
            self.signaling = WebSocketServer(port)

        recorder_options = {
            "segment_time": segment_time,
            "reset_timestamps": "1",
            "strftime": "1",
        }
        self.recorder = MediaRecorder('video/%Y-%m-%d_%H-%M-%S.mkv',
                                      format="segment",
                                      options=recorder_options)

        async def send_answer():
            logger.debug(f"Ice Gathering State: {self.pc.iceGatheringState}")
            # отправка происходит если были собраны все IceCandidate
            if self.pc.iceGatheringState == 'complete':
                logger.debug("Answer sent")
                await self.signaling.send_data({
                    "sdp":
                    self.pc.localDescription.sdp,
                    "type":
                    self.pc.localDescription.type
                })
            else:
                # если IceCandidate не собраны, то ожидается их сбор
                self.pc.once("icegatheringstatechange", send_answer)

        @self.signaling.on_message
        async def on_message(message):
            logger.debug(f"{message.get('type')} received")
            if message.get("type") == "offer":
                offer = RTCSessionDescription(sdp=message["sdp"],
                                              type=message["type"])
                await self.pc.setRemoteDescription(offer)
                answer = await self.pc.createAnswer()
                await self.pc.setLocalDescription(answer)
                await send_answer()

        @self.pc.on("connectionstatechange")
        async def on_connectionstatechange():
            if self.pc.connectionState == "failed":
                await self.pc.close()
            elif self.pc.connectionState == "connected":
                await self.recorder.start()
            elif self.pc.connectionState == "closed":
                await self.recorder.stop()
                logger.info("Recorder closed")
            logger.info(f"Connection state: {self.pc.connectionState}")

        @self.pc.on("track")
        async def on_track(track):
            if track.kind == "audio":
                self.recorder.addTrack(track)
            elif track.kind == "video":
                self.__video = track
                self.recorder.addTrack(
                    FixedPtsTrack(MediaRelay().subscribe(track)))
            logger.info(f"Track {track.kind} added")

            @track.on("ended")
            async def on_ended():
                await self.recorder.stop()
                logger.info(f"Track {track.kind} ended")
Exemplo n.º 19
0
    async def processRequest(request: Request) -> Any:
        Logger.debug(f"worker: processRequest() [method:{request.method}]")

        if request.method == "dump":
            result = {
                "pid": getpid(),
                "players": [],
                "handlers": []
            }

            for playerId, player in players.items():
                playerDump = {
                    "id": playerId
                }  # type: Dict[str, Any]
                if player.audio:
                    playerDump["audioTrack"] = {
                        "id": player.audio.id,
                        "kind": player.audio.kind,
                        "readyState": player.audio.readyState
                    }
                if player.video:
                    playerDump["videoTrack"] = {
                        "id": player.video.id,
                        "kind": player.video.kind,
                        "readyState": player.video.readyState
                    }
                result["players"].append(playerDump)  # type: ignore

            for handler in handlers.values():
                result["handlers"].append(handler.dump())  # type: ignore

            return result

        elif request.method == "createPlayer":
            internal = request.internal
            playerId = internal["playerId"]
            data = request.data
            player = MediaPlayer(
                data["file"],
                data["format"] if "format" in data else None,
                data["options"] if "options" in data else None
            )

            # store the player in the map
            players[playerId] = player

            result = {}
            if player.audio:
                result["audioTrackId"] = player.audio.id
            if player.video:
                result["videoTrackId"] = player.video.id
            return result

        elif request.method == "getRtpCapabilities":
            pc = RTCPeerConnection()
            pc.addTransceiver("audio", "sendonly")
            pc.addTransceiver("video", "sendonly")
            offer = await pc.createOffer()
            await pc.close()
            return offer.sdp

        elif request.method == "createHandler":
            internal = request.internal
            handlerId = internal["handlerId"]
            data = request.data

            # use RTCConfiguration if given
            jsonRtcConfiguration = data.get("rtcConfiguration")
            rtcConfiguration = None

            if jsonRtcConfiguration and "iceServers" in jsonRtcConfiguration:
                iceServers = []
                for entry in jsonRtcConfiguration["iceServers"]:
                    iceServer = RTCIceServer(
                        urls=entry.get("urls"),
                        username=entry.get("username"),
                        credential=entry.get("credential"),
                        credentialType=entry.get("credentialType")
                    )
                    iceServers.append(iceServer)
                rtcConfiguration = RTCConfiguration(iceServers)

            handler = Handler(
                handlerId,
                channel,
                loop,
                getTrack,
                addRemoteTrack,
                getRemoteTrack,
                rtcConfiguration
            )

            handlers[handlerId] = handler
            return

        else:
            internal = request.internal
            handler = handlers.get(internal["handlerId"])
            if handler is None:
                raise Exception("hander not found")

            return await handler.processRequest(request)
Exemplo n.º 20
0
# %% imports
from MQTTDevice import MQTTDevice
import paho.mqtt.client as mqtt
from random import randint
from time import time, sleep

import sys
import asyncio
from rtcbot import Websocket, RTCConnection, PiCamera
from aiortc import RTCConfiguration, RTCIceServer

cam = PiCamera()

conn = RTCConnection(rtcConfiguration=RTCConfiguration([
    RTCIceServer(urls="stun:stun.l.google.com:19302"),
    RTCIceServer(urls="turn:io.scilifelab.se:5349",
                 username="******",
                 credential="3a53d013d9996594d591")
]))

# set parameters
setup_name = "S015"
device_ID = "RAS01"
device_MQTT_name = "RASPI_" + str(randint(0, 100000))
mqtt_broker_ip = "localhost"
mqtt_client_name = "raspi1"  # not necessary
mqtt_client_pass = "******"  # not necessary
mqtt_port = 1883
mqtt_keepalive = 60
mqtt_uselogin = False

# conn = RTCConnection()
Exemplo n.º 21
0
            await pc.addIceCandidate(obj)
        elif obj is BYE:
            print("Exiting")
            break


if __name__ == "__main__":
    room = "".join([random.choice("0123456789") for x in range(10)])
    # room = input("输入房间号:")
    urls = "https://wangxin1210.xyz"
    signaling = ApprtcSignaling(room)
    signaling._origin = urls

    config = RTCConfiguration([
        RTCIceServer("turn:42.194.190.40:3478",
                     username='******',
                     credential='926492',
                     credentialType='password')
    ])
    pc = RTCPeerConnection(configuration=config)
    recorder = MediaBlackhole()
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(run(pc, recorder, signaling))
    except KeyboardInterrupt:
        pass
    finally:
        loop.run_until_complete(recorder.stop())
        loop.run_until_complete(signaling.close())
        loop.run_until_complete(pc.close())
Exemplo n.º 22
0
async def setup_peer():
    print("creating peer")
    global PC, GLOBAL

    config = RTCConfiguration(iceServers=[
        RTCIceServer(urls=["stun:coturn.retrowave.tech"]),
        RTCIceServer(urls=["stun:stun.l.google.com:19302"]),
        RTCIceServer(urls=['turn:coturn.retrowave.tech'],
                     credential='password1',
                     username='******')
    ])
    PC = RTCPeerConnection(config)
    pc_id = "PeerConnection(%s)" % uuid.uuid4()
    pcs.add(PC)

    def log_info(msg, *args):
        logger.info(pc_id + " " + msg, *args)

    # log_info("Created for %s", request.remote)

    @PC.on("datachannel")
    async def on_datachannel(channel):
        @channel.on("message")
        def on_message(message):
            # print(channel.label)
            if channel.label == 'gamepad':
                pass
                # print("GAMEPAD MESSAGE", message)
                # Example Message
                # {"id":"046d-c21f-Logitech Gamepad F710","state":{"FACE_1":0,"FACE_2":0,"FACE_3":0,"FACE_4":0,
                #  "LEFT_TOP_SHOULDER":0,"RIGHT_TOP_SHOULDER":0,"LEFT_BOTTOM_SHOULDER":0,"RIGHT_BOTTOM_SHOULDER":1,
                #  "SELECT_BACK":0,"START_FORWARD":0,"LEFT_STICK":0,"RIGHT_STICK":0,"DPAD_UP":0,"DPAD_DOWN":0,
                #  "DPAD_LEFT":0,"DPAD_RIGHT":0,"HOME":0,"LEFT_STICK_X":0,"LEFT_STICK_Y":0,"RIGHT_STICK_X":0,"RIGHT_STICK_Y":0},"ts":87090}
                #
                # msg_data = json.loads(message)
                # gp_state = msg_data['state']
                # send over LCM
                # msg = twist_t()
                # msg.timestamp = msg_data['ts']
                # msg.linear = (gp_state['LEFT_STICK_X'], gp_state['LEFT_STICK_Y'], 0.0)
                # msg.angular = (gp_state['RIGHT_STICK_X'], gp_state['RIGHT_STICK_Y'], 0.0)
                # msg.name = msg_data['id']

            else:
                if isinstance(message, str) and message.startswith("ping"):
                    channel.send("pong" + message[4:])

    @PC.on("iceconnectionstatechange")
    async def on_iceconnectionstatechange():
        log_info("ICE connection state is %s", PC.iceConnectionState)
        if PC.iceConnectionState == "failed":
            await PC.close()
            pcs.discard(PC)

    @PC.on("icegatheringstatechange")
    async def icegatheringstatechange():
        print("icegatherstatechange")
        print(PC.iceGatheringState)
        await asyncio.sleep(0.1)

    print("peer created")
    print(PC)
    return PC
Exemplo n.º 23
0
    videoUrl = None
    #     videoUrl = 'https://youtu.be/uuQlMCMT71I' #uncomment to overide with a Youtube video source, set skipFrames to False for Youtube streaming

    if videoUrl is not None:
        #install youtube-dl for this to work: pip install youtube-dl
        command = "youtube-dl -f 'bestvideo[height<1100]' -g '" + videoUrl + "'"
        videoUrl = subprocess.check_output(command,
                                           shell=True).decode("utf-8").strip()
        args.play_from = videoUrl

    print('videoUrl=', videoUrl)

    signaling = RaceOssdcSignaling(args.room)

    configuration = RTCConfiguration()

    stunServer = RTCIceServer("stun:race.ossdc.org:5349")

    configuration.iceServers = []
    configuration.iceServers.append(stunServer)

    pc = RTCPeerConnection(configuration=configuration)

    # create media source
    if args.play_from:
        player = MediaPlayer(args.play_from)
    else:
        player = None

    # create media sink
Exemplo n.º 24
0
from aiortc.contrib.signaling import object_from_string, object_to_string
from aiortc import (
    RTCIceCandidate,
    RTCConfiguration,
    RTCIceCandidate,
    RTCPeerConnection,
    RTCSessionDescription,
    RTCIceServer,
)

MY_IP = 'your ip here'
JWT = "Pl4idC7F2020"

rtcConfiguration = RTCConfiguration(iceServers=[
    RTCIceServer("turn:45.79.56.244", username="******", credential="passpass")
])


def run(coro):
    return asyncio.get_event_loop().run_until_complete(coro)


def get(url, username):
    url = "https://chat.mooz.pwni.ng%s" % url
    token = {'ipaddr': MY_IP, 'username': username}
    headers = {
        "x-chat-authorization": jwt.encode(token, JWT),
    }
    r = requests.get(url, headers=headers)
    assert r.status_code in (200, 201), r.text