Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRTC with python

I would like to make a streaming server with python/twisted, which receives a WebRTC video stream and then applys some OpenCV algorithms to it.

However I cannot find a python module for WebRTC. How can I send and receive a WebRTC video stream with python/twisted?

Thanks!

like image 241
Mark Unruh Avatar asked Jul 12 '14 23:07

Mark Unruh


People also ask

Can I use WebRTC in Python?

WebRTC is an open-source project providing peer-to-peer, real-time communication capabilities to web browsers, mobile devices, and any other device that can run code using the available APIs for Javascript, Python, . NET, Golang, and more.

Can I use WebRTC without browser?

If you want to create any webrtc -based client application without using a browser , the native libraries are the way to go. No silly standalone javascript engine necessary.

What is Aiortc?

aiortc is a library for Web Real-Time Communication (WebRTC) and Object Real-Time Communication (ORTC) in Python. It is built on top of asyncio , Python's standard asynchronous I/O framework. The API closely follows its Javascript counterpart while using pythonic constructs: promises are replaced by coroutines.


Video Answer


3 Answers

I have started putting together the basic blocks needed to create a Python WebRTC endpoint.

One is an asyncio-based Interactive Connectivity Establishment module:

https://github.com/jlaine/aioice

Another one is a Python binding for libsrtp:

https://github.com/jlaine/pylibsrtp

We also need SRTP keying support in the OpenSSL bindings:

https://github.com/pyca/cryptography/pull/4099

On top of this, we can then build an asyncio-based WebRTC implementation:

https://github.com/jlaine/aiortc

I have been able to get both Chrome and Firefox to establish an audio and video stream to a Python-based server.

like image 152
Jeremy Avatar answered Oct 09 '22 05:10

Jeremy


What you can do is take screen shots continuously and push them to a websocket and allow your twisted server to take a gander at each one as it comes in.

I have modified some common recorders and my version takes Jpeg images and pushes them over a websocket. Feel free to use and modify how you want so that it fits your needs. Source code here. The example I use is pushing down to a libwebsocket server built in C but the same javascript could be used to send to any websocket server.

like image 42
Benjamin Trent Avatar answered Oct 09 '22 05:10

Benjamin Trent


I've had a similar issue and ended up creating a server that launches a headless chrome instance from which I can access the WebRTC streams, record chunks with a MediaRecorder and finally forward those chunks on via a WebSocket.

I'd love a python based solution so I wouldn't need the intermediary server launching headless chrome instances but haven't been able to find one.

I've been using Node.js and Puppeteer but one could launch the browser instances from your python server and then send the decoded data back via plain old sockets or whatever else tickles your fancy.

like image 1
Jay Avatar answered Oct 09 '22 05:10

Jay