Skip to main content

Janus webrtc client, to be used with asyncio.

Project description

Janus Client for Python

License: MIT Development Stage

This is a Janus webrtc client written in Python, to be used with asyncio.


Installing

pip install janus-client

Description

Features

:heavy_check_mark: Connect to Janus server through websocket (using websockets)
:heavy_check_mark: Handle transactions with Janus
:heavy_check_mark: Create/destroy sessions
:heavy_check_mark: Create/destroy plugins
:heavy_check_mark: Handle multiple sessions and or multiple plugins at the same time
:heavy_check_mark: Support authentication with shared static secret (API key) and/or stored token
:heavy_check_mark: Expose Admin/Monitor API client

In Progress

:clock3: Emit events to respective session and plugin handlers
:clock3: Create plugin for videoroom plugin

Dependencies


Development

The package hopes to implement a general purpose client that can communicate with a Janus server. Examples like VideoRoom plugin is not part of their core features, so it's not included in the package.
But it can still be included as a default example though. It's up for discussion. :D The reason stopping me from doing that is because I'm depending on GStreamer to use WebRTC and media streaming, and it's not trivial to install it. Please refer to Quirks section.

You can refer to video_room_plugin.py to see how a specific plugin handle is implemented.

And in main.py, you will be able to find references on how to use the client in general such as connecting and creating sessions. Essence:

import asyncio
import ssl
import pathlib
from janus_client import JanusClient, JanusSession
from video_room_plugin import JanusVideoRoomPlugin

ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
localhost_pem = pathlib.Path(__file__).with_name("lt_limmengkiat_name_my.crt")
ssl_context.load_verify_locations(localhost_pem)

async def main():
    # Connect to server
    client = JanusClient("wss://lt.limmengkiat.name.my/janusws/")
    await client.connect(ssl=ssl_context)
    # Create session
    session = await client.create_session(JanusSession)
    # Create plugin
    plugin_handle = await session.create_plugin_handle(JanusVideoRoomPlugin)

    participants = await plugin_handle.list_participants(1234)
    if len(participants) > 0:
        # Publishers available
        participants_data_1 = participants[0]
        participant_id = participants_data_1["id"]

        # Subscribe to publisher, will get jsep (sdp offer)
        await plugin_handle.subscribe(1234, participant_id)
        # WebRTC streaming not implemented  for subscriber yet
        await asyncio.sleep(5)
        # Unsubscribe from the publisher
        await plugin_handle.unsubscribe()

    # Destroy plugin
    await plugin_handle.destroy()
    # Destroy session
    await session.destroy()
    # Destroy connection
    await client.disconnect()

asyncio.run(main())

Quirks

On my RPI 2 Raspbian Buster, there's a problem with GStreamer installed from distribution repository. It's complaining about ssl and then failing DTLS.
Referring to this PR: webrtcbin: fix DTLS when receivebin is set to DROP
I believe there is a bug in the distributed GStreamer version (v1.14.4) thus I recompiled it on my RPI 2
There's also a chance that it's a problem with openssl itself, an incompatibility. Refering to this gist: OpenSSL DTLS problem in Debian buster

Because of these, please recompile GStreamer with version above 1.14.4.

If recompiling GStreamer on RPI, there's this issue: rpicamsrc 1.18.3 failed.
You can patch the build with this PR: rpicamsrc: depend on posix threads and vchiq_arm or build with master branch.

And then another quirk, the example was still unable to setup a peer connection to my janus server at lt.limmengkiat.name.my. I had to enable ice_tcp (ice_tcp=true) in janus.jcfg for it to work. I don't know why yet.
Janus Enable ICE TCP

Compiling GStreamer

Clone gst-build repo and build it. Warning, I'm in Malaysia and the download speed is very low when cloning projects in meson. One useful practice I learned is to enable VNC server on the RPI, and then use VNC viewer to open desktop and open terminal to start meson build. With this, your build won't stop when you close VNC Viewer so you can let it run overnight. It's better than doing direct SSH, and saves a bit electricity if you have a monitor.
For advanced users, you can manually change the meson build file to pull from github mirror if the gitlab url is slow for you too.
GStreamer mirror

We will be using gst-build to recompile GStreamer. Later it's up to you to install the build or not, but I recommend using their development environment instead.

Below is a summary of commands to build GStreamer, please refer to Building from source using meson for more info.

# Clone build repo from Github mirror
git clone https://github.com/GStreamer/gst-build.git
cd gst-build
# Initialise build
meson builddir
# Configure build
meson configure -Dpython=enabled -Dgst-plugins-bad:webrtc=enabled -Dgst-plugins-base:opus=enabled \
  -Dgst-plugins-bad:srtp=enabled -Ddoc=disabled -Dgst-plugins-good:vpx=enabled builddir/
# Note: Check that cairo will not be compiled after configuring, as it may break your desktop
#       Install libcairo2-dev to prevent recompiling it
# Build
ninja -C builddir/
# Install (not recommended)
#ninja -C builddir/ install
# Use gst-build provided development environment for GStreamer.
# Option 1:
# Opens a new shell, not friendly to Visual Studio Code
ninja -C builddir/ devenv
# Option 2:
# Override environment variables to look for the recompiled GStreamer. Possible to add into .bashrc
eval $(~/gst-build/gst-env.py --builddir=$HOME/gst-build/builddir --srcdir=$HOME/gst-build --only-environment)
# Option 2.1:
# Getting the environment variable from the python script takes significant amount of time on my RPI 2.
# Since I believe it is static until the next time you recompile it, I save it into a file and load from there instead.
~/gst-build/gst-env.py --builddir=$HOME/gst-build/builddir --srcdir=$HOME/gst-build --only-environment \
  > ~/gst-env-static.txt
# There's a line setting PWD and OLDPWD inside gst-env-static.txt, and that is not static.
# Let's just comment them out or delete them manually. Then:
echo "source ~/gst-env-static.txt" >> ~/.bashrc

For reference, here are some extra external libraries I installed for the compilation (far from exhaustive, some might be optional):

apt-get install libmount-dev flex bison nasm libssl-dev libavfilter-dev gobject-introspection \
  libgirepository1.0-dev libsrtp2-dev libjpeg-dev libvpx-dev libcairo2-dev

Test your installation by running "webrtc/janus/janusvideoroom.py" from gst-examples repo.

Raspbian Stretch GStreamer

In case you are wondering about other versions of Raspbian, I've tested with Raspbian Stretch.
The Gstreamer version distributed with it is v1.10.1, and webrtcbin is first introduced in v1.13.1, referencing here.
So, still need to recompile GStreamer.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

janus-client-0.2.0.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

janus_client-0.2.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file janus-client-0.2.0.tar.gz.

File metadata

  • Download URL: janus-client-0.2.0.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.3

File hashes

Hashes for janus-client-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0cfa52ff6c43e2331655cb6f48e9da5ba6f36a0668f91ff9b4b06bbc6400ad68
MD5 1a53a01c5f966b5fb13642655718dea8
BLAKE2b-256 5dc4d370fb147243e65f1a22899706b2c38185fe71e3c0646f6557595d4dd8f5

See more details on using hashes here.

File details

Details for the file janus_client-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: janus_client-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.3

File hashes

Hashes for janus_client-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5b6b5198f248466f66f283eb97a17e5e101697288f1af6d55a07f21d3b5c711b
MD5 22ad64cf54520b924ba0b8c7f47cdfff
BLAKE2b-256 756c55afaf8cb92de8c4157ed244ad767d5359b07c136ab4884b8f3e1914a3f9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page