def test_setRemoteDescription_unexpected_offer(self):
     pc = RTCPeerConnection()
     pc.addTrack(AudioStreamTrack())
     offer = run(pc.createOffer())
     run(pc.setLocalDescription(offer))
     with self.assertRaises(InvalidStateError) as cm:
         run(pc.setRemoteDescription(RTCSessionDescription(sdp='', type='offer')))
     self.assertEqual(str(cm.exception),
                      'Cannot handle offer in signaling state "have-local-offer"')
Beispiel #2
0
def object_from_string(message_str):
    message = json.loads(message_str)
    if message['type'] in ['answer', 'offer']:
        return RTCSessionDescription(**message)
    elif message['type'] == 'candidate':
        candidate = candidate_from_sdp(message['candidate'].split(':', 1)[1])
        candidate.sdpMid = message['id']
        candidate.sdpMLineIndex = message['label']
        return candidate
Beispiel #3
0
def object_from_string(message_str):
    message = json.loads(message_str)
    if message["type"] in ["answer", "offer"]:
        return RTCSessionDescription(**message)
    elif message["type"] == "candidate":
        candidate = candidate_from_sdp(message["candidate"].split(":", 1)[1])
        candidate.sdpMid = message["id"]
        candidate.sdpMLineIndex = message["label"]
        return candidate
Beispiel #4
0
async def offer(request):
    params = await request.json()
    offer = RTCSessionDescription(sdp=params['sdp'], type=params['type'])

    pc = RTCPeerConnection()
    pcs.add(pc)

    # prepare local media
    player = MediaPlayer(os.path.join(ROOT, 'demo-instruct.wav'))
    if args.write_audio:
        recorder = MediaRecorder(args.write_audio)
    else:
        recorder = MediaBlackhole()

    @pc.on('datachannel')
    def on_datachannel(channel):
        @channel.on('message')
        def on_message(message):
            channel.send('pong')

    @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.on('track')
    def on_track(track):
        print('Track %s received' % track.kind)

        if track.kind == 'audio':
            pc.addTrack(player.audio)
            recorder.addTrack(track)
        elif track.kind == 'video':
            local_video = VideoTransformTrack(
                track, transform=params['video_transform'])
            pc.addTrack(local_video)

        @track.on('ended')
        async def on_ended():
            print('Track %s ended' % track.kind)
            await recorder.stop()

    # handle offer
    await pc.setRemoteDescription(offer)
    await recorder.start()

    # send answer
    answer = await pc.createAnswer()
    await pc.setLocalDescription(answer)

    return web.Response(content_type='application/json',
                        text=json.dumps({
                            'sdp': pc.localDescription.sdp,
                            'type': pc.localDescription.type
                        }))
Beispiel #5
0
    async def __offer(self, request):
        """
        Generates JSON Response with a WebRTC Peer Connection of Video Server.
        """
        # get offer from params
        params = await request.json()
        offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])

        # initiate stream
        if not (self.__default_rtc_server is
                None) and not (self.__default_rtc_server.is_launched):
            if self.__logging:
                logger.debug("Initiating Video Streaming.")
            self.__default_rtc_server.launch()

        # setup RTC peer connection - interface represents a WebRTC connection
        # between the local computer and a remote peer.
        pc = RTCPeerConnection()
        self.__pcs.add(pc)
        if self.__logging:
            logger.info("Created WebRTC Peer Connection.")

        # track ICE connection state changes
        @pc.on("iceconnectionstatechange")
        async def on_iceconnectionstatechange():
            logger.debug("ICE connection state is %s" % pc.iceConnectionState)
            if pc.iceConnectionState == "failed":
                logger.error("ICE connection state failed!")
                await pc.close()
                self.__pcs.discard(pc)

        # Change the remote description associated with the connection.
        await pc.setRemoteDescription(offer)
        # retrieve list of RTCRtpTransceiver objects that are currently attached to the connection
        for t in pc.getTransceivers():
            # Increments performance significantly, IDK why this works as H265 codec is not even supported :D
            capabilities = RTCRtpSender.getCapabilities("video")
            preferences = list(
                filter(lambda x: x.name == "H265", capabilities.codecs))
            t.setCodecPreferences(preferences)
            # add video server to peer track
            if t.kind == "video":
                pc.addTrack(
                    self.__relay.subscribe(self.config["server"]) if not (
                        self.__relay is None) else self.config["server"])

        # Create an SDP answer to an offer received from a remote peer
        answer = await pc.createAnswer()

        # Change the local description for the answer
        await pc.setLocalDescription(answer)

        # return Starlette json response
        return JSONResponse({
            "sdp": pc.localDescription.sdp,
            "type": pc.localDescription.type
        })
Beispiel #6
0
async def run_answer(pc):
    done = asyncio.Event()

    _consumers = []

    @pc.on('datachannel')
    def on_datachannel(channel):
        @channel.on('message')
        def on_message(message):
            # reply
            message = 'pong'
            channel_log(channel, '>', message)
            channel.send(message)

            # quit
            #done.set()

    @pc.on('track')
    def on_track(track):
        print("on_track")
        if track.kind == 'audio':
            _consumers.append(asyncio.ensure_future(consume_audio(track)))
        elif track.kind == 'video':
            _consumers.append(asyncio.ensure_future(consume_video(track)))

    # receive offer
    print('-- Please enter remote offer --')
    try:
        offer_json = json.loads(input())
    except:
        with open('offer.json', 'r') as f:
            offer_json = json.loads(f.read())
    await pc.setRemoteDescription(RTCSessionDescription(
        sdp=offer_json['sdp'],
        type=offer_json['type']))
    print()

    # send answer
    await pc.setLocalDescription(await pc.createAnswer())
    answer = pc.localDescription
    print('-- Your answer --')
    print(json.dumps({
        'sdp': answer.sdp,
        'type': answer.type
    }))
    print()
    with open('answer.json', 'w') as f:
        f.write(json.dumps({
            'sdp': answer.sdp,
            'type': answer.type
        }))

    await done.wait()

    for c in _consumers:
        c.cancel()
Beispiel #7
0
    async def offer(self, request):
        params = await request.json()
        offer = RTCSessionDescription(sdp=params['sdp'], type=params['type'])

        pc = RTCPeerConnection()
        pc_id = 'PeerConnection(%s)' % uuid.uuid4()
        self.__pcs.add(pc)
        logger.debug('%s: created for %s' % (pc_id, request.remote))

        @pc.on('datachannel')
        def on_datachannel(channel):
            logger.debug('%s: Data channel established (%s)' %
                         (pc_id, channel.id))
            self.__channels.add(channel)

            @channel.on('message')
            def on_message(message):
                logger.trace('Message received from %s: %s' % (pc_id, message))
                try:
                    parsed_message = json.loads(message)
                    request_id = parsed_message['request_id'] if (
                        'request_id' in parsed_message.keys()) else None
                    if request_id is not None:
                        self.__request_id_channels[request_id] = channel

                    if parsed_message and self.on_new_message_listener is not None:
                        self.on_new_message_listener(parsed_message,
                                                     request_id)
                except ValueError:
                    logger.warn('Invalid message received from %s: %s' %
                                (pc_id, message))

        @pc.on('iceconnectionstatechange')
        async def on_iceconnectionstatechange():
            logger.debug('%s: ICE connection state is %s' %
                         (pc_id, pc.iceConnectionState))
            if pc.iceConnectionState == 'failed':
                await pc.close()
                logger.debug('%s: ICE connection discarded with state %s' %
                             (pc_id, pc.iceConnectionState))
                self.__pcs.discard(pc)

        # player = MediaPlayer('src/test_video.mp4', options={'framerate': '30', 'video_size': '1920x1080'})
        await pc.setRemoteDescription(offer)
        for t in pc.getTransceivers():
            # pc.addTrack(player.video)
            pc.addTrack(self.__camera_preview)

        answer = await pc.createAnswer()
        await pc.setLocalDescription(answer)

        return web.Response(content_type='application/json',
                            text=json.dumps({
                                'sdp': pc.localDescription.sdp,
                                'type': pc.localDescription.type
                            }))
async def offer(request):
    params = await request.json()
    offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])

    pc = RTCPeerConnection()
    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)

    # prepare local media as a blackhole
    recorder = MediaBlackhole()

    @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("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("track")
    def on_track(track):
        log_info("Track %s received", track.kind)
        local_video = VideoTransform(
            track, transform=params["video_transform"]
        )
        pc.addTrack(local_video)
        @track.on("ended")
        async def on_ended():
            log_info("Track %s ended", track.kind)
            await recorder.stop()

    # handle offer
    await pc.setRemoteDescription(offer)
    await recorder.start()

    # send answer
    answer = await pc.createAnswer()
    await pc.setLocalDescription(answer)

    return web.Response(
        content_type="application/json",
        text=json.dumps(
            {"sdp": pc.localDescription.sdp, "type": pc.localDescription.type}
        ),
    )
Beispiel #9
0
async def give_back_answer(request):
    pc = pc_broadcast
    params = await request.json()
    answer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])
    await pc.setRemoteDescription(answer)
    return web.Response(content_type="application/json",
                        text=json.dumps({
                            "sdp": None,
                            "type": None
                        }))
Beispiel #10
0
async def run_offer(pc):
    done = asyncio.Event()

    channel = pc.createDataChannel('chat')
    channel_log(channel, '-', 'created by local party')
    channel_watch(channel)

    # add video track
    local_video_red = RedVideoStreamTrack()
    local_video_green = GreenVideoStreamTrack()
    local_video_blue = BlueVideoStreamTrack()
    local_video = CombinedVideoStreamTrack([local_video_red, local_video_green, local_video_blue])
    pc.addTrack(local_video)

    @channel.on('message')
    def on_message(message):
        # quit
        #done.set()
        pass

    # send offer
    await pc.setLocalDescription(await pc.createOffer())
    offer = pc.localDescription
    print('-- Your offer --')
    print(json.dumps({
        'sdp': offer.sdp,
        'type': offer.type
    }))
    print()
    with open('offer.json', 'w') as f:
        f.write(json.dumps({
            'sdp': offer.sdp,
            'type': offer.type
        }))

    # receive answer
    print('-- Please enter remote answer --')
    try:
        answer_json = json.loads(input())
    except:
        with open('answer.json', 'r') as f:
            answer_json = json.loads(f.read())
    await pc.setRemoteDescription(RTCSessionDescription(
        sdp=answer_json['sdp'],
        type=answer_json['type']))
    print()

    #await done.wait()
    while True:
        # send message
        message = 'ping'
        channel_log(channel, '>', message)
        channel.send(message)
        # sleep
        await asyncio.sleep(1)
async def stream(request):
    params = await request.json()
    offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])
    track_id = params["track_id"]

    pc = RTCPeerConnection()

    pc_id = "PeerConnection(%s)" % uuid.uuid4()
    index = len(pcs)
    pcs.append(pc)

    print('track_id ', track_id)

    #if len(tracks) != 0 :
    #current_track = main_track
    new_stream_track = CopiedVideoStreamTrack()
    new_stream_track.recv = main_config.main_track.recv
    pc.addTrack(new_stream_track)

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

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

    @pc.on("iceconnectionstatechange")
    async def on_iceconnectionstatechange():
        log_info("ICE connection state is %s", pc.iceConnectionState)
        if pc.iceConnectionState == 'closed':
            await pc.close()
            try:
                pcs.pop(index)
            except:
                pass

        if pc.iceConnectionState == "failed":
            await pc.close()
            try:
                pcs.pop(index)
            except:
                pass

    # handle offer

    await pc.setRemoteDescription(offer)
    # send answer
    answer = await pc.createAnswer()
    await pc.setLocalDescription(answer)

    return web.Response(
        content_type="application/json",
        text=json.dumps({
            "sdp": pc.localDescription.sdp,
            "type": pc.localDescription.type
        }),
    )
Beispiel #12
0
async def offer(request):
    params = await request.json()
    offer = RTCSessionDescription(sdp=params['sdp'], type=params['type'])

    pc = RTCPeerConnection()
    pcs.add(pc)

    # prepare local media
    player = MediaPlayer(os.path.join(ROOT, 'demo-instruct.wav'))
    if args.write_audio:
        recorder = MediaRecorder(args.write_audio)
    else:
        recorder = MediaBlackhole()

    @pc.on('datachannel')
    async def on_datachannel(channel):
        closed = asyncio.Event()
        queue = asyncio.Queue()

        print("Channel opened")

        @channel.on('close')
        def on_close():
            print("Channel closed")
            closed.set()

        @channel.on('message')
        async def on_message(message):
            await queue.put(message)
            #channel.send(message)
            print(message)

        await cloud_browser(channel, closed, queue)

    @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)

    # handle offer
    await pc.setRemoteDescription(offer)
    await recorder.start()

    # send answer
    answer = await pc.createAnswer()
    await pc.setLocalDescription(answer)

    return web.Response(content_type='application/json',
                        text=json.dumps({
                            'sdp': pc.localDescription.sdp,
                            'type': pc.localDescription.type
                        }))
Beispiel #13
0
    async def handle_message(message):
        print('<', message)

        if message['type'] == 'bye':
            return True

        if message['type'] == 'offer':
            await pc.setRemoteDescription(RTCSessionDescription(**message))
            await pc.setLocalDescription(await pc.createAnswer())
            await signaling.send_message(
                description_to_dict(pc.localDescription))
        elif message['type'] == 'answer':
            await pc.setRemoteDescription(RTCSessionDescription(**message))
        elif message['type'] == 'candidate':
            candidate = candidate_from_sdp(message['candidate'].split(':',
                                                                      1)[1])
            candidate.sdpMid = message['id']
            candidate.sdpMLineIndex = message['label']
            pc.addIceCandidate(candidate)
        return False
Beispiel #14
0
    def _webrtc_thread_impl(
        self,
        sdp: str,
        type_: str,
    ):
        logger.debug("_webrtc_thread_impl starts", )

        loop = asyncio.new_event_loop()
        self._loop = loop

        offer = RTCSessionDescription(sdp, type_)

        def callback(localDescription):
            self._answer_queue.put(localDescription)

        video_transformer = None
        if self.video_transformer_factory:
            video_transformer = self.video_transformer_factory()

        video_receiver = None
        if self.mode == WebRtcMode.SENDONLY:
            video_receiver = VideoReceiver(queue_maxsize=1)

        self._video_transformer = video_transformer
        self._video_receiver = video_receiver

        @self.pc.on("iceconnectionstatechange")
        async def on_iceconnectionstatechange():
            iceConnectionState = self.pc.iceConnectionState
            if iceConnectionState == "closed" or iceConnectionState == "failed":
                self._unset_transformers()

        loop.create_task(
            _process_offer(
                self.mode,
                self.pc,
                offer,
                player_factory=self.player_factory,
                in_recorder_factory=self.in_recorder_factory,
                out_recorder_factory=self.out_recorder_factory,
                video_transformer=video_transformer,
                video_receiver=video_receiver,
                async_transform=self.async_transform,
                callback=callback,
            ))

        try:
            loop.run_forever()
        finally:
            logger.debug("Event loop %s has stopped.", loop)
            loop.run_until_complete(self.pc.close())
            loop.run_until_complete(loop.shutdown_asyncgens())
            loop.close()
            logger.debug("Event loop %s cleaned up.", loop)
Beispiel #15
0
def object_from_string(message_str):
    obj = json.loads(message_str)
    data = obj['data']
    source = obj['source']

    if data['type'] in ['answer', 'offer']:
        return RTCSessionDescription(**data), source
    elif data['type'] == 'candidate':
        candidate = candidate_from_sdp(data['candidate'].split(':', 1)[1])
        candidate.sdpMid = data['id']
        candidate.sdpMLineIndex = data['label']
        return candidate, source
Beispiel #16
0
async def barcode_offer(request):
    values = await request.post()

    rtc_sdp = validate_string(values["sdp"])
    rtc_type = validate_string(values["type"])

    if not rtc_sdp or not rtc_type:
        return web.Response(content_type="text/json",
                            body=generate_type_error())

    offer = RTCSessionDescription(sdp=rtc_sdp, type=rtc_type)

    rtc_peer = RTCPeerConnection()
    session = RTCSession(rtc_peer)

    rtc_peer.addTransceiver("video", "recvonly")

    @rtc_peer.on("datachannel")
    def on_datachannel(channel):
        session.data_channel = channel

        @channel.on("message")
        def on_message(message):
            if message == "start":
                session.barcode_scanning = True
            else:
                session.barcode_scanning = False

    @rtc_peer.on("track")
    def on_track(track):
        # handle video tracks
        if track.kind == "video":
            rtc_peer.addTrack(VideoTransformTrack(session, track))

        # @track.on("ended")
        # def on_ended():
        # 	print("track ended")

    # set the options that the client sent us
    await rtc_peer.setRemoteDescription(offer)

    answer = await rtc_peer.createAnswer()
    # reply to the client with the options we generated
    await rtc_peer.setLocalDescription(answer)

    return web.Response(
        content_type="text/json",
        body=json.dumps({
            "sdp": rtc_peer.localDescription.sdp,
            "type": rtc_peer.localDescription.type
        }),
    )
Beispiel #17
0
    async def sdp(self, data):
        if data["type"] == "offer":
            # other people cammed up and sent offers
            return

        if data["type"] == "answer":
            if data["success"]:
                d = RTCSessionDescription(type="answer", sdp=data["sdp"])
                await self.pc.setRemoteDescription(d)
                self.connection.remote_password = ""
                self.connection.remote_username = ""
                asyncio.ensure_future(self.connection.connect(),
                                      loop=asyncio.get_event_loop())
Beispiel #18
0
    async def handle_offer(self, sdp, mode):

        connection = RTCPeerConnection()
        self.connections.append(connection)
        register_handlers(connection, mode)

        offer = RTCSessionDescription(sdp, type='offer')
        await connection.setRemoteDescription(offer)

        answer = await connection.createAnswer()
        await connection.setLocalDescription(answer)

        return answer
Beispiel #19
0
    async def start(self):
        # first get a token
        try:
            token = self.get_bearer_token()
        except:
            token = self.get_bearer_token(mock=True)

        offer_id, sdp_offer = self.get_sdp_offer_from_spot_cam(token)

        @self.pc.on('icegatheringstatechange')
        def _on_ice_gathering_state_change():
            print(
                f'ICE gathering state changed to {self.pc.iceGatheringState}')

        @self.pc.on('signalingstatechange')
        def _on_signaling_state_change():
            print(f'Signaling state changed to: {self.pc.signalingState}')

        @self.pc.on('icecandidate')
        def _on_ice_candidate(event):
            print(f'Received candidate: {event.candidate}')

        @self.pc.on('iceconnectionstatechange')
        async def _on_ice_connection_state_change():
            print(
                f'ICE connection state changed to: {self.pc.iceConnectionState}'
            )

            if self.pc.iceConnectionState == 'checking':
                self.send_sdp_answer_to_spot_cam(
                    token, offer_id, self.pc.localDescription.sdp.encode())

        @self.pc.on('track')
        def _on_track(track):
            print(f'Received track: {track.kind}')

            if self.media_recorder:
                if track.kind == self.recorder_type:
                    self.media_recorder.addTrack(track)

            if track.kind == 'video':
                video_track = SpotCAMMediaStreamTrack(track,
                                                      self.video_frame_queue)
                video_track.kind = 'video'
                self.pc.addTrack(video_track)

        desc = RTCSessionDescription(sdp_offer, 'offer')
        await self.pc.setRemoteDescription(desc)

        sdp_answer = await self.pc.createAnswer()
        await self.pc.setLocalDescription(sdp_answer)
Beispiel #20
0
async def run(pc, player, room, session):
    await session.create()

    # configure media
    media = {"audio": False, "video": True}
    if player and player.audio:
        pc.addTrack(player.audio)
        media["audio"] = True

    if player and player.video:
        pc.addTrack(player.video)
    else:
        pc.addTrack(VideoStreamTrack())

    # join video room
    plugin = await session.attach("janus.plugin.videoroom")
    await plugin.send(
        {
            "body": {
                "display": "aiortc",
                "ptype": "publisher",
                "request": "join",
                "room": room,
            }
        }
    )

    # send offer
    await pc.setLocalDescription(await pc.createOffer())
    request = {"request": "configure"}
    request.update(media)
    response = await plugin.send(
        {
            "body": request,
            "jsep": {
                "sdp": pc.localDescription.sdp,
                "trickle": False,
                "type": pc.localDescription.type,
            },
        }
    )

    # apply answer
    answer = RTCSessionDescription(
        sdp=response["jsep"]["sdp"], type=response["jsep"]["type"]
    )
    await pc.setRemoteDescription(answer)

    # exchange media for 10 minutes
    print("Exchanging media")
    await asyncio.sleep(600)
Beispiel #21
0
    async def sdp(self, data):
        if data["type"] == "offer":
            # other people cam up and sendoffers
            return

        if data["type"] == "answer":
            print(data)
            if data["success"]:

                d = RTCSessionDescription(type="answer", sdp=data["sdp"])
                await self.pc.setRemoteDescription(d)
                self.connection.remote_password = ""
                self.connection.remote_username = ""
                await self.connection.connect()
Beispiel #22
0
async def offer(request):
    request_url = request.match_info['stream']
    streams = await get_streams(args.nvr_token)

    if request_url not in streams:
        raise web.HTTPNotFound(text='No rtsp source related to this url')

    play_from = streams[request_url]
    if not play_from:
        raise web.HTTPBadGateway(
            text=
            'NVR response with cam rtsp link is empty. Contact NVR admins to fix it'
        )

    url = urlparse(play_from)
    if url.scheme == 'rtsp':
        await check_rtsp_availability(play_from, timeout=10)

    params = await request.json()
    offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])

    pc = RTCPeerConnection()
    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)

    player = MediaPlayer(play_from)

    await pc.setRemoteDescription(offer)
    for t in pc.getTransceivers():
        if t.kind == "audio" and player.audio:
            pc.addTrack(player.audio)
        elif t.kind == "video" and player.video:
            track = VideoTransformTrack(player.video, request_url)
            pc.addTrack(track)

    answer = await pc.createAnswer()
    await pc.setLocalDescription(answer)

    return web.Response(content_type="application/json",
                        headers=cors_headers,
                        text=json.dumps({
                            "sdp": pc.localDescription.sdp,
                            "type": pc.localDescription.type
                        }))
Beispiel #23
0
async def subscribe(session, room, feed, recorder, plugin_last):
    pc = RTCPeerConnection()
    pcs.add(pc)

    frame = None

    local_video = None

    @pc.on("track")
    async def on_track(track):
        print("Track %s received" % track.kind)
        if track.kind == "video":

            print(feed)

            local_video = VideoTransformTrack(track, transform='rotate')
            pc.addTrack(local_video)

            await publish(plugin=plugin_last, player=local_video)

    # subscribe
    plugin = await session.attach("janus.plugin.videoroom")
    response = await plugin.send({
        "body": {
            "request": "join",
            "ptype": "subscriber",
            "room": room,
            "feed": feed
        }
    })

    # apply offer
    await pc.setRemoteDescription(
        RTCSessionDescription(sdp=response["jsep"]["sdp"],
                              type=response["jsep"]["type"]))

    # send answer
    await pc.setLocalDescription(await pc.createAnswer())
    response = await plugin.send({
        "body": {
            "request": "start"
        },
        "jsep": {
            "sdp": pc.localDescription.sdp,
            "trickle": False,
            "type": pc.localDescription.type,
        },
    })
    # await recorder.start()
    return local_video
Beispiel #24
0
 async def offerOrAnswer(sdp):
     print("offer-or-answer", sdp["type"])
     if sdp["type"] == 'offer':
         await self.pc.setRemoteDescription(
             RTCSessionDescription(sdp['sdp'], sdp['type']))
         # print(self.pc.remoteDescription)
         answer = await self.pc.createAnswer()
         # print('answer:', answer)
         await self.pc.setLocalDescription(answer)
         await self.sendToPeer(
             "offer-or-answer", {
                 'sdp': self.pc.localDescription.sdp,
                 'type': self.pc.localDescription.type
             })
async def offer(
        request):  #receives post request from website and creates sdp offer
    params = await request.json()  #offer starts connecton
    offer = RTCSessionDescription(sdp=params['sdp'], type=params['type'])

    # recorder = MediaRecorder(os.path.join(ROOT, 'video.mp4'))
    recorder = MediaBlackhole()  #######
    pc = RTCPeerConnection()
    pcs.add(pc)

    @pc.on('datachannel')
    def on_datachannel(channel):
        @channel.on('message')
        def on_message(message):
            global set_msg
            global omsg
            global msg
            if set_msg == True and msg != omsg:
                send_msg(channel, msg)  #data transfer function
                print("msg sent", msg)
                omsg = msg
                set_msg = False

    @pc.on('track')
    def on_track(track):
        print('Track %s received' % track.kind)
        #video function call here
        video = ImageProcess(track)
        recorder.addTrack(video)

        @track.on('ended')
        async def on_ended():
            print('Track %s ended' % track.kind)
            cv2.destroyAllWindows()
            await recorder.stop()

    await pc.setRemoteDescription(offer)
    await recorder.start()
    answer = await pc.createAnswer()
    await pc.setLocalDescription(
        answer
    )  #RTCSessionDescription generated by createOffer() or createAnswer()

    return web.Response(  #send answer to website this will start data transfer
        content_type='application/json',
        text=json.dumps({
            'sdp': pc.localDescription.sdp,
            'type': pc.localDescription.type
        }))
Beispiel #26
0
async def offer(request):
    params = await request.json()
    print(type(params['sdp']))
    print(params['sdp'])
    print("------------------------")
    print(params['type'])
    offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])
    # print(offer)
    pc = RTCPeerConnection()

    pcs.add(pc)
    print("pcs add")

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

    print("after")
    # open webcam
    options = {"framerate": "30", "video_size": "640x480"}
    if platform.system() == "Darwin":
        player = MediaPlayer("default:none",
                             format="avfoundation",
                             options=options)
    else:
        player = MediaPlayer("/dev/video0", format="v4l2", options=options)

    await pc.setRemoteDescription(offer)
    for t in pc.getTransceivers():
        if t.kind == "audio" and player.audio:
            pc.addTrack(player.audio)
        elif t.kind == "video" and player.video:
            local_video = VideoTransformTrack(player.video)
            pc.addTrack(local_video)

    answer = await pc.createAnswer()
    await pc.setLocalDescription(answer)

    return web.Response(
        content_type="application/json",
        text=json.dumps({
            "sdp": pc.localDescription.sdp,
            "type": pc.localDescription.type
        }),
    )
Beispiel #27
0
async def offer(request):
    params = await request.json()
    logger.debug('got json %s', params)

    offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])
    logger.info('created offer')
    pc = RTCPeerConnection()
    logger.info('created RTCPeerConnection')
    pcs.add(pc)

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

    # open media source
    if args.play_from:
        player = MediaPlayer(args.play_from)
    else:
        options = {"framerate": "30", "video_size": "640x480"}
        if platform.system() == "Darwin":
            logger.info('creating MediPlayer for Darwin...')
            player = MediaPlayer("default:none",
                                 format="avfoundation",
                                 options=options)
            logger.info('create MediaPlayer')
        else:
            player = MediaPlayer("/dev/video0", format="v4l2", options=options)
    logger.info('setRemoteDescription starting...')
    await pc.setRemoteDescription(offer)
    logger.info('setRemoteDescription complete')
    for t in pc.getTransceivers():
        if t.kind == "audio" and player.audio:
            pc.addTrack(player.audio)
        elif t.kind == "video" and player.video:
            pc.addTrack(player.video)

    answer = await pc.createAnswer()
    await pc.setLocalDescription(answer)

    return web.Response(
        content_type="application/json",
        text=json.dumps({
            "sdp": pc.localDescription.sdp,
            "type": pc.localDescription.type
        }),
    )
Beispiel #28
0
async def offer(request):
    params = await request.json()
    offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])
    pc = RTCPeerConnection()
    # 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)

    # prepare local media
    if args.write_audio:
        recorder = MediaRecorder(args.write_audio)
    else:
        recorder = MediaBlackhole()

    @pc.on("track")
    def on_track(track):
        # log_info("Track %s received", track.kind)

        local_video = VideoTransformTrack(track,
                                          powidth=params["powidth"],
                                          poheight=params["poheight"],
                                          powidth2=params["powidth2"],
                                          poheight2=params["poheight2"])
        pc.addTrack(local_video)

        @track.on("ended")
        async def on_ended():
            # log_info("Track %s ended", track.kind)
            await recorder.stop()

    # handle offer
    await pc.setRemoteDescription(offer)
    await recorder.start()

    # send answer
    answer = await pc.createAnswer()
    await pc.setLocalDescription(answer)

    return web.Response(
        content_type="application/json",
        text=json.dumps({
            "sdp": pc.localDescription.sdp,
            "type": pc.localDescription.type
        }),
    )
    async def offer_view(self, request):
        params = await request.json()
        offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])

        pc = RTCPeerConnection()
        pc_id = "PeerConnection(%s)" % uuid.uuid4()
        self.pcs.add(pc)

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

        log_info("Receiving for %s", request.remote)

        @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("iceconnectionstatechange")
        async def on_iceconnectionstatechange():
            log_info("ICE connection state is %s", pc.iceConnectionState)
            if pc.iceConnectionState == "failed":
                await pc.close()
                pcs.discard(pc)

        # handle offer
        await pc.setRemoteDescription(offer)
        #await recorder.start()

        for t in pc.getTransceivers():
            # if t.kind == "audio" and player.audio:
            #     pc.addTrack(player.audio)
            if t.kind == "video":
                print(self.local_video)
                pc.addTrack(self.local_video)

        # send answer
        answer = await pc.createAnswer()
        await pc.setLocalDescription(answer)

        return web.Response(
            content_type="application/json",
            text=json.dumps({
                "sdp": pc.localDescription.sdp,
                "type": pc.localDescription.type
            }),
        )
Beispiel #30
0
async def offer(request):
    params = await request.json()

    offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])

    pc = RTCPeerConnection()
    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("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("track")
    def on_track(track):
        log_info("Track %s received", track.kind)

        if track.kind == "audio":
            local_audio = EchoTrack(track)
            pc.addTrack(local_audio)
        elif track.kind == "video":
            local_video = EchoTrack(track)
            pc.addTrack(local_video)

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

    # handle offer
    await pc.setRemoteDescription(offer)

    # send answer
    answer = await pc.createAnswer()
    await pc.setLocalDescription(answer)

    return web.Response(
        content_type="application/json",
        text=json.dumps(
            {"sdp": pc.localDescription.sdp, "type": pc.localDescription.type}
        ),
    )