Python: how to use WebRTC

Python: how to use WebRTC

In this guide, we will explore how to use WebRTC in Python to create a simple video calling application using the aiortc library.

WebRTC, an acronym for Web Real-Time Communication, is an open-source technology that allows real-time communication between browsers and web applications without the need for additional plugins. This technology has become increasingly popular for the development of video calling, chat and real-time audio/video transmission applications over the Internet.

Python is one of the most popular programming languages in the world, and you can use it together with WebRTC to create advanced, interactive web applications. In this guide, we will explore how to use WebRTC in Python to create a simple video calling application using the aiortc library.

What is aiortc?

aiortc is a Python library that provides an interface for using WebRTC in Python applications. It offers functionality to create both WebRTC clients and servers, allowing developers to easily implement real-time communication in their applications.

Installing aiortc

To use aiortc, you need to install the library using pip, the Python package manager. Run the following command in terminal:


pip install aiortc

With aiortc installed in our virtual environment, we are ready to create our video calling application.

Creating a simple video call server

Below, we provide an example of how to create a simple video call server using aiortc. This server will allow two users to connect and communicate with each other via video and audio.


import asyncio
from aiohttp import web
from aiortc import RTCPeerConnection, RTCSessionDescription
from aiortc.contrib.aiohttp import websockets

async def index(request):
     content = open("index.html", "r").read()
     return web.Response(content_type="text/html", text=content)

async def websocket_handler(request):
     ws = web.WebSocketResponse()
     await ws.prepare(request)

     pc = RTCPeerConnection()
    
     @pc.on("track")
     def on_track(track):
         print("Track received")
         pc.addTrack(track)

     async for msg in ws:
         if msg.type == web.WSMsgType.TEXT:
             if msg.data == 'offer':
                 offer = await ws.receive_json()
                 await pc.setRemoteDescription(RTCSessionDescription(offer))
                 answer = await pc.createAnswer()
                 await pc.setLocalDescription(answer)
                 await ws.send_json(answer)
             elif msg.data == 'answer':
                 answer = await ws.receive_json()
                 await pc.setRemoteDescription(RTCSessionDescription(answer))
             elif msg.data == 'candidate':
                 candidate = await ws.receive_json()
                 candidate = candidate['candidate']
                 await pc.addIceCandidate(candidate)
         elif msg.type == web.WSMsgType.ERROR:
             print('ws connection closed with exception %s' %
                   ws.exception())

     return ws

app = web.Application()
app.router.add_get("/", index)
app.router.add_get("/ws", websocket_handler)

if __name__ == "__main__":
     web.run_app(app)

Creating the HTML file for the client


<!DOCTYPE html>
<html lang="en">
<head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>WebRTC Video Chat</title>
</head>
<body>
     <video id="localVideo" autoplay></video>
     <video id="remoteVideo" autoplay></video>
     <script>
         const localVideo = document.getElementById('localVideo');
         const remoteVideo = document.getElementById('remoteVideo');

         const ws = new WebSocket('ws://' + window.location.host + '/ws');

         const pc = new RTCPeerConnection();

         pc.ontrack = function(event) {
             console.log("Track received");
             remoteVideo.srcObject = event.streams[0];
         };

         pc.onicecandidate = function(event) {
             if (event.candidate) {
                 ws.send(JSON.stringify({
                     type: 'candidate',
                     candidate: event.candidate
                 }));
             }
         };

         async function startCall() {
             const offer = await pc.createOffer();
             await pc.setLocalDescription(offer);
             ws.send('offer');
             ws.send(JSON.stringify(offer));
         }

         ws.onmessage = async function(event) {
             const data = JSON.parse(event.data);
             if (data.type === 'answer') {
                 await pc.setRemoteDescription(new RTCSessionDescription(data));
             }
         };

         startCall();
     </script>
</body>
</html>

Running the application

To run the application, save the Python code in a file called server.py and the HTML code in a file called index.html. Make sure both files are in the same directory.

Next, run the following command in terminal:


python server.py

Then open your browser and access the address http://localhost:8080. You should see two videos, one for your local webcam and one for the remote webcam (of your video call partner, if any).

Conclusions

In this guide, we explored how to use WebRTC in Python to create a simple video calling server using the aiortc library. This is just one example of how you can use WebRTC to implement real-time communication in your Python applications. By knowing these fundamentals, you can further explore the capabilities of WebRTC and develop increasingly advanced and interactive web applications.