Janus WebRTC async client in Python.
Project description
Janus Client in Python
A Janus WebRTC client in Python asyncio.
Install
pip install janus-client
Description
This is a client that can communicate with Janus WebRTC server to use provided services.
The VideoRoom plugin implemented in plugin_video_room_ffmpeg.py uses FFmpeg. It depends on the ffmpeg-cli, and that is required to be installed separately.
Features
:heavy_check_mark: Connect to Janus server through:
- Websocket API (websockets)
- HTTP (aiohttp)
:heavy_check_mark: Manage Janus client connection, session, and plugins
:heavy_check_mark: Multiple connections in parallel
:heavy_check_mark: Direct message transactions to correct senders asynchronously
from janus_client import JanusSession, JanusEchoTestPlugin, JanusVideoRoomPlugin
# Protocol will be derived from base_url
session = JanusSession(
base_url="wss://janusmy.josephgetmyip.com/janusbasews/janus",
)
# OR
session = JanusSession(
base_url="https://janusmy.josephgetmyip.com/janusbase/janus",
)
plugin_handle_1 = JanusVideoRoomPlugin()
plugin_handle_2 = JanusEchoTestPlugin()
# Attach to Janus session
await plugin_handle_1.attach(session=session)
await plugin_handle_2.attach(session=session)
# Destroy plugin handles in parallel
await asyncio.gather(
plugin_handle_1.destroy(), plugin_handle_2.destroy()
)
:heavy_check_mark: Support authentication with shared static secret (API key) and/or stored token
:heavy_check_mark: Expose Admin/Monitor API client
:heavy_check_mark: Janus VideoRoom plugin with FFmpeg (Partial: Only sends video)
from janus_client import JanusVideoRoomPlugin
import ffmpeg
room_id = 1234
publisher_id = 333
display_name = "qweqwe"
width = 640
height = 480
ffmpeg_input = ffmpeg.input(
"desktop",
format="gdigrab",
framerate=30,
offset_x=20,
offset_y=30,
video_size=[
width,
height,
],
show_region=1,
)
plugin_handle_1 = JanusVideoRoomPlugin()
await plugin_handle_1.join(room_id, publisher_id, display_name)
await plugin_handle_1.publish(ffmpeg_input=ffmpeg_input, width=width, height=height)
await asyncio.sleep(60)
await plugin_handle_1.unpublish()
:heavy_check_mark: Janus EchoTest plugin
from janus_client import JanusEchoTestPlugin
plugin_handle_2 = JanusEchoTestPlugin()
await plugin_handle_2.start(
play_from="./Into.the.Wild.2007.mp4", record_to="./asdasd.mp4"
)
await asyncio.sleep(15)
await plugin_handle_2.close_stream()
FFmpeg Stream To WebRTC (:warning: WARNING !!!)
This FFmpeg stream to WebRTC solution is a hack. The fact is that FFmpeg doesn't support WebRTC and aiortc is implemented using PyAV. PyAV has much less features than a full fledged installed FFmpeg, so to support more features and keep things simple, I hacked about a solution without the use of neither WHIP server nor UDP nor RTMP.
First the ffmpeg input part should be constructed by the user, before passing it to JanusVideoRoomPlugin.publish
. When the media player needs to stream the video, the following happens:
- A thread will be created and a ffmpeg process will be created. Output of ffmpeg is hardcoded to be
rawvideo rgb24
. - Thread reads output of ffmpeg process.
- Coverts the output data to numpy array and then to
av.VideoFrame
frame. - Hack the
pts
andtime_base
parameter of the frame. I don't know what it is and just found a value that works. - Put the frame into video track queue to be received and sent by
aiortc.mediastreams.MediaStreamTrack
.
References:
TODO
:clock3: Publish audio with FFmpeg VideoRoom plugin
:clock3: Subscribe to stream with FFmpeg VideoRoom plugin
:clock3: Disable keepalive recurring task for HTTP transport
:clock3: Handle error when fail to join room because of "User ID _ already exists" error
:clock3: Gracefully detach plugin. Handle "detached" response
Usage
Use test_ffmpeg.py to try streaming a portion of monitor display to Janus videoroom demo.
Result:
Delay of 0.175s
Server ping:
Support for GStreamer VideoRoom plugin has been deprecated since v0.2.5
Contributions to migrate the plugin to latest JanusPlugin
API would be greatly appreciated.
Documentation
:construction: Under construction :construction:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file janus_client-0.4.4.tar.gz
.
File metadata
- Download URL: janus_client-0.4.4.tar.gz
- Upload date:
- Size: 19.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.4 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0614472e343f19f19eb9dd36688c270db5cdbcd72c7b4a4dceea3f9673cbc71a |
|
MD5 | fdb55940ffeb496647dc6ee708c77d36 |
|
BLAKE2b-256 | 494618c7344e4c5e8bfae4a3cfcfc12c2ee700dd478dedd06d5898ca714ce238 |
File details
Details for the file janus_client-0.4.4-py3-none-any.whl
.
File metadata
- Download URL: janus_client-0.4.4-py3-none-any.whl
- Upload date:
- Size: 25.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.4 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18dca4885a26ba932b5cc40172fa72a5a638a7475ea04f882b580cae1445eb78 |
|
MD5 | a9c149d1f214e50975833b907618dedd |
|
BLAKE2b-256 | 067e97567882997776a03d18be994f69cd6d2923da754449c5df3d08485fc46d |