I trying to do an server side code in python that will stream desktop stream and audio via WebRTC aiortc. My server code:
import asyncio
import json
import os
import ssl
import aiohttp_cors
from aiohttp import web
from aiortc import MediaStreamTrack, RTCPeerConnection, RTCSessionDescription
from aiortc.contrib.media import MediaPlayer
ROOT = os.path.dirname(__file__)
pcs = set()
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, "audio.wav"))
player1 = MediaPlayer(os.path.join(ROOT, "video.mp4"))
@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():
if pc.iceConnectionState == "failed":
await pc.close()
pcs.discard(pc)
@pc.on("track")
def on_track(track):
if track.kind == "audio":
pc.addTrack(player.audio)
elif track.kind == "video":
pc.addTrack(player1.video)
# 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}
),
)
async def on_shutdown(app):
# close peer connections
coros = [pc.close() for pc in pcs]
await asyncio.gather(*coros)
pcs.clear()
if __name__ == "__main__":
if False:
ssl_context = ssl.SSLContext()
ssl_context.load_cert_chain("cert_file", "key_file")
else:
ssl_context = None
app = web.Application()
app.on_shutdown.append(on_shutdown)
app.router.add_post("/offer", offer)
cors = aiohttp_cors.setup(app, defaults={
"*": aiohttp_cors.ResourceOptions(
allow_credentials=True,
expose_headers="*",
allow_headers="*"
)
})
for route in list(app.router.routes()):
cors.add(route)
web.run_app(
app, access_log=None, host="0.0.0.0", port=8080, ssl_context=ssl_context
)
In this code I have two files video and audio file that the server streams. But how I can implement desktop stream video and audio
I tried only load files and stream them and I expecting that the code will stream desktop video and desktop audio in real-time.