async def test_audio_and_video(self): recorder = MediaBlackhole() recorder.addTrack(AudioStreamTrack()) recorder.addTrack(VideoStreamTrack()) await recorder.start() await asyncio.sleep(2) await recorder.stop()
def test_audio_remove_track(self): recorder = MediaBlackhole() track = AudioStreamTrack() recorder.addTrack(track) recorder.start() run(asyncio.sleep(1)) recorder.removeTrack(track) run(asyncio.sleep(1)) recorder.stop()
async def test_video_ended(self): track = VideoStreamTrack() recorder = MediaBlackhole() recorder.addTrack(track) await recorder.start() await asyncio.sleep(1) track.stop() await asyncio.sleep(1) await recorder.stop()
async def offer(params: Offer): offer = RTCSessionDescription(sdp=params.sdp, type=params.type) pc = RTCPeerConnection() pcs.add(pc) recorder = MediaBlackhole() @pc.on("connectionstatechange") async def on_connectionstatechange(): print("Connection state is %s" % pc.connectionState) if pc.connectionState == "failed": await pc.close() pcs.discard(pc) # open media source audio, video = create_local_tracks() # handle offer await pc.setRemoteDescription(offer) await recorder.start() # send answer answer = await pc.createAnswer() await pc.setRemoteDescription(offer) for t in pc.getTransceivers(): if t.kind == "audio" and audio: pc.addTrack(audio) elif t.kind == "video" and video: pc.addTrack(video) await pc.setLocalDescription(answer) return {"sdp": pc.localDescription.sdp, "type": pc.localDescription.type}
def __init__(self, uri, player=None, recorder=MediaBlackhole()): self._uri = uri self._player = player self._recorder = recorder # Save answers temporarily self._answers = {} self._websocket = None self._device = None self._tracks = [] if player and player.audio: audioTrack = player.audio else: audioTrack = AudioStreamTrack() if player and player.video: videoTrack = player.video else: videoTrack = VideoStreamTrack() self._videoTrack = videoTrack self._audioTrack = audioTrack self._tracks.append(videoTrack) self._tracks.append(audioTrack) self._sendTransport: Optional[Transport] = None self._recvTransport: Optional[Transport] = None self._producers = [] self._consumers = [] self._tasks = [] self._closed = False
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): if isinstance(message, str) and message.startswith('ping'): channel.send('pong' + message[4:]) @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 }))
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} ), )
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 }))
def __init__(self, room_id: int, q: Connection): self.room_id = room_id self.relay = MediaRelay() self.active_users: Dict[int, Tuple[RTCPeerConnection, MediaStreamTrack]] = dict() # outQ is listened by RootServer self.outQ = Producer(TMQ_HOST, TMQ_PORT, TOPIC_TO_ROOT_SERVER) self.inQ: Connection = q self.logger = logging.getLogger(f"LiveRoom_{self.room_id}") self.logger.setLevel(logging.DEBUG) self.blackhole = MediaBlackhole()
async def offer(request): params = await request.json() offer = RTCSessionDescription( sdp=params['sdp'], type=params['type']) pc = RTCPeerConnection() pcs.append(pc) # prepare local media player = MediaPlayer(path=os.path.join(ROOT, 'demo-instruct.wav')) if args.write_audio: recorder = MediaRecorder(path=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('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') def on_ended(): print('Track %s ended' % track.kind) recorder.stop() player.stop() await pc.setRemoteDescription(offer) answer = await pc.createAnswer() await pc.setLocalDescription(answer) player.start() recorder.start() return web.Response( content_type='application/json', text=json.dumps({ 'sdp': pc.localDescription.sdp, 'type': 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 }))
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 }), )
def test_audio_ended(self): track = AudioStreamTrack() recorder = MediaBlackhole() recorder.addTrack(track) run(recorder.start()) run(asyncio.sleep(1)) track.stop() run(asyncio.sleep(1)) run(recorder.stop())
async def offer(params: Offer): offer = RTCSessionDescription(sdp=params.sdp, type=params.type) pc = RTCPeerConnection() pcs.add(pc) recorder = MediaBlackhole() relay = MediaRelay() @pc.on("connectionstatechange") async def on_connectionstatechange(): print("Connection state is %s" % pc.connectionState) if pc.connectionState == "failed": await pc.close() pcs.discard(pc) # open media source # audio, video = create_local_tracks() @pc.on("track") def on_track(track): # if track.kind == "audio": # pc.addTrack(player.audio) # recorder.addTrack(track) if track.kind == "video": pc.addTrack( VideoTransformTrack(relay.subscribe(track), transform=params.video_transform)) # if args.record_to: # recorder.addTrack(relay.subscribe(track)) @track.on("ended") async def on_ended(): await recorder.stop() # handle offer await pc.setRemoteDescription(offer) await recorder.start() # send answer answer = await pc.createAnswer() await pc.setRemoteDescription(offer) await pc.setLocalDescription(answer) return {"sdp": pc.localDescription.sdp, "type": pc.localDescription.type}
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
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 player = MediaPlayer(os.path.join(ROOT, "demo-instruct.wav")) print(f"RECORDING TO {args.record_to}") if args.record_to: recorder = MediaRecorder(args.record_to) else: 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("connectionstatechange") async def on_connectionstatechange(): log_info("Connection state is %s", pc.connectionState) if pc.connectionState == "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": pc.addTrack(player.audio) recorder.addTrack(track) elif track.kind == "video": pc.addTrack( VideoTransformTrack( relay.subscribe(track), transform=params["video_transform"] ) ) if args.record_to: recorder.addTrack(relay.subscribe(track)) @track.on("ended") async def on_ended(): log_info("Track %s ended", track.kind) await recorder.stop() # handle offer await pc.setRemoteDescription(offer) print("START RECORDING") 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} ), )
parser.add_argument("--play-from", help="Read the media from a file and sent it.") parser.add_argument("--record-to", help="Write received media to a file.") args = parser.parse_args() if not args.room: args.room = secrets.token_urlsafe(8).lower() peerId = secrets.token_urlsafe(8).lower() uri = f'wss://v3demo.mediasoup.org:4443/?roomId={args.room}&peerId={peerId}' if args.play_from: player = MediaPlayer(args.play_from) else: player = None # create media sink if args.record_to: recorder = MediaRecorder(args.record_to) else: recorder = MediaBlackhole() # run event loop loop = asyncio.get_event_loop() try: demo = Demo(uri=uri, player=player, recorder=recorder) loop.run_until_complete(demo.run()) except KeyboardInterrupt: pass finally: loop.run_until_complete(demo.close())
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())
async def offer(request): params = await request.json() offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"]) pc = RTCPeerConnection() pc_id = "PeerConnection(%s)" % uuid.uuid4() print(pc_id) pcs.add(pc) # pcs_to_tracks[pc] = [] # add_tracks_to_pcs() def log_info(msg, *args): logger.info(pc_id + " " + msg, *args) log_info("Created for %s", request.remote) # prepare local media # player = MediaPlayer(os.path.join(ROOT, "demo-instruct.wav")) recorder = MediaBlackhole() @pc.on("datachannel") def on_datachannel(channel): @channel.on("message") def on_message(message): log_info(f"got 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) speaker_to_remove = speakers.get(pc) if speaker_to_remove: speakers.pop(pc, None) @pc.on("negotiationneeded") def on_negotiaion_needed(): log_info("negotiation needed") @pc.on("track") def on_track(track): log_info("Track %s received", track.kind) if pc not in speakers: speakers[pc] = set() if track.kind == "audio": local_audio = AudioTransformTrack(track) speakers[pc].add(local_audio) recorder.addTrack(local_audio) elif track.kind == "video": speakers[pc].add(track) pc.addTrack(track) @track.on("ended") async def on_ended(): log_info("Track %s ended", track.kind) # 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 }), )
def test_audio(self): recorder = MediaBlackhole() recorder.addTrack(AudioStreamTrack()) run(recorder.start()) run(asyncio.sleep(1)) run(recorder.stop())
def test_video(self): recorder = MediaBlackhole() recorder.addTrack(VideoStreamTrack()) run(recorder.start()) run(asyncio.sleep(2)) run(recorder.stop())
async def offer(): params = await request.get_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_addr) # prepare local media player = MediaPlayer(os.path.join(ROOT, "demo-instruct.wav")) if args.stream: recorder = MediaRecorder('rtmp://*****:*****@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) 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) recorder.addTrack(track) @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 jsonify({ "sdp": pc.localDescription.sdp, "type": pc.localDescription.type })
async def offer(request): params = await request.json() offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"]) #message2=[-1] 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): # message2[0]=message # #if cnt != int(message2[0])+1 : # #channel.send(str(count2)) # #print("ここです",count2) # message2[0]= darknet_video.i # global ab # if ab!=darknet_video.detections: # res= str(darknet_video.i) + " \n" +str(darknet_video.detections) # channel.send(res) # ab=darknet_video.detections ##tes=darknet_video.detections #tes=VideoTransform.d_res #print(tes) #channel.send("aaaaaaaaaaa") @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 }), )
async def addRemoteTrack(track: MediaStreamTrack) -> None: recvTracks[track.id] = track blackhole = MediaBlackhole() blackhole.addTrack(track) await blackhole.start()
async def offer(request): params = await request.json() offer = RTCSessionDescription( sdp=params['sdp'], type=params['type']) # Generate a random identifier userid = await generate_uuid() pc = RTCPeerConnection() pcs[userid] = pc # prepare local media recorder = MediaBlackhole() # ricezione evento datachannel (quando il peer all'altro capo effettua una createDataChannel) @pc.on('datachannel') def on_datachannel(channel): dcs[userid] = channel @channel.on('message') def on_message(message): try: print("Received message: " + message) except: print("Failed receiving module data.") channel.send("{}") @pc.on('iceconnectionstatechange') async def on_iceconnectionstatechange(): print('ICE connection state is %s' % pc.iceConnectionState) if pc.iceConnectionState == 'failed': await pc.close() pcs.pop(userid, None) dcs.pop(userid, None) objectDetectionConfigs.pop(userid, None) @pc.on('track') def on_track(track): print('Track %s received' % track.kind) if track.kind == 'video': local_video = VideoTransformTrack(track, userid) pc.addTrack(local_video) print("Added local video (cnn).") @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) # Start thread detection loop = asyncio.get_event_loop() detectionData = DetectionDataHolder(loop) detectionData.start() return web.Response( content_type='application/json', text=json.dumps({ 'sdp': pc.localDescription.sdp, 'type': pc.localDescription.type, 'userid': userid }))
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) player = MediaPlayer(os.path.join(ROOT, "demo-instruct.wav")) if args.write_audio: recorder = MediaRecorder(args.write_audio) else: recorder = MediaBlackhole() @pc.on("track") def on_track(track): global pier_id pier_id = track pier_id = id(pier_id) pier_id = str(pier_id) log_info("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(): log_info("Track %s ended", track.kind) await recorder.stop() @pc.on("datachannel") def on_datachannel(channel): @channel.on("message") def on_message(message): 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) await pc.setRemoteDescription(offer) await recorder.start() 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, "object_id": pier_id }), )
# create signaling and peer connection signaling = create_signaling(args) pc = RTCPeerConnection() # create media source if args.play_from: video_player = MediaPlayer("/dev/video0") audio_player = MediaPlayer("default", format="pulse") else: video_player = None audio_player = None # create media sink if args.record_to: audio_recorder = MediaRecorder(args.record_to) video_recorder = MediaBlackhole() else: video_recorder = MediaBlackhole() audio_recorder = MediaBlackhole() # run event loop loop = asyncio.get_event_loop() try: loop.run_until_complete( run(pc=pc, audio_player=audio_player, audio_recorder=audio_recorder, video_recorder=video_recorder, signaling=signaling, role=args.role, video_player=video_player,
async def join_room(room, play_from, record_to): # fetch room parameters async with aiohttp.ClientSession() as session: async with session.post('https://appr.tc/join/' + room) as response: # we cannot use response.json() due to: # https://github.com/webrtc/apprtc/issues/562 data = json.loads(await response.text()) assert data['result'] == 'SUCCESS' params = data['params'] # create peer conection pc = RTCPeerConnection() # setup media source if play_from: player = MediaPlayer(play_from) else: player = None if player and player.audio: pc.addTrack(player.audio) else: pc.addTrack(AudioStreamTrack()) if player and player.video: pc.addTrack(player.video) else: pc.addTrack(VideoImageTrack()) # setup media sink if record_to: recorder = MediaRecorder(record_to) else: recorder = MediaBlackhole() @pc.on('track') def on_track(track): print('Track %s received' % track.kind) recorder.addTrack(track) def on_ended(): print('Track %s ended' % track.kind) # connect to websocket and join signaling = Signaling() await signaling.connect(params) await signaling.send({ 'clientid': params['client_id'], 'cmd': 'register', 'roomid': params['room_id'], }) if params['is_initiator'] == 'true': # send offer await pc.setLocalDescription(await pc.createOffer()) await signaling.send_message(description_to_dict(pc.localDescription)) print('Please point a browser at %s' % params['room_link']) # receive 60s of media try: await asyncio.wait_for(consume_signaling( signaling, pc, player, recorder, params), timeout=60) except asyncio.TimeoutError: pass # shutdown print('Shutting down') await signaling.send_message({'type': 'bye'}) await pc.close()
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) # player = MediaPlayer(os.path.join(ROOT, "test.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): 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) if track.kind == "audio": # AUDIO_TRACK # local_audio = AudioTrack(track) # local_audio = AudioTrack() # pc.addTrack(local_audio) pc.addTrack(player.audio) # MediaStreamTrackTest # local_audio = MediaStreamTrackTest(track) # local_audio= MediaStreamTrackTest() # pc.addTrack(local_audio) # print(track) # local_audio = AudioTrack(track) # pc.addTrack(local_audio) # pc.addTrack(player.audio) # recorder.addTrack(track) # pc.addTrack(track) pass elif track.kind == "video": local_video = VideoTransformTrack(track) pc.addTrack(local_video) # pc.addTrack(player.audio) print("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} ), )
import paho.mqtt.client as mqtt from aiohttp import web from aiohttp import ClientSession from aiortc import MediaStreamTrack, RTCPeerConnection, RTCSessionDescription from aiortc.contrib.media import MediaBlackhole, MediaPlayer, MediaRecorder, MediaRelay ROOT = os.path.dirname(__file__) #logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger("pc") pcs = set() relay = MediaRelay() broadcast = None blackhole = MediaBlackhole() ## Pyydetään dispatcherilta WebRTC-streami async def ask_stream(interval): global blackhole await asyncio.sleep(interval) print("Haetaan streami") pc = RTCPeerConnection() pc_id = "PeerConnection(%s)" % uuid.uuid4() pcs.add(pc) def log_info(msg, *args): logger.info(pc_id + " " + msg, *args)