[PYTHON] Try using Janus gateway's Admin API

Overview

There are many articles about the WebRTC media server called Janus gateway, There is almost no non-official usage of the provided API. I had a hard time finding out, so I would like to summarize it here.

Reference site

-Official site

API type

There are two main types of APIs provided by Janus.

An API for managing and monitoring Janus. I will use it in this article.

-** Plugin API **

Janus has a number of plugins for video calling, streaming, SIP related and more. API for operating the plugin. I will use it in the next article.

Try using Admin API via Websocket

Set Janus config

Set the password to use when calling the Admin API.

/usr/local/etc/janus/janus.cfg


[general]
admin_secret = password

Set it so that it can be used via Websocket. This time we are setting ws, but if encryption is required, let's set wss and prepare the certificate. Also, the port specified here should not be blocked by FW etc.

/usr/local/etc/janus/janus.transport.websockets.cfg


[admin]
admin_ws = yes
admin_ws_port = 7188

Write code in python

The Python version is 2.7. This time, I will monitor the information of the user who accessed Janus.

First, get the list of sessions you are accessing.

python


import websocket
import json
import random
import string

#Janus endpoint
janus_admin_url = 'ws://10.0.0.1:7188/janus'

#Administrative password
janus_admin_secret = 'password'

#Random character generation
def random_string(length, seq=string.digits):
    sr = random.SystemRandom()
    return ''.join([sr.choice(seq) for i in range(length)])

#8 random characters
transaction = random_string(8)

# list_Create a Json that calls the sessions method
data = {"janus":'list_sessions', "admin_secret": janus_admin_secret, "transaction": transaction}

#Create a Websocket connection
websock = websocket.create_connection(janus_admin_url,subprotocols=["janus-admin-protocol"])

#Send
websock.send(json.dumps(data))

#Receive
rejson =  websock.recv()

#Display reception result
print rejson
{
   "janus": "success",
   "transaction": "73677159",
   "sessions": [
      311091039069809
   ]
}

Then get the list that the session handles. Multiple handles are returned, for example if multiple plugins are used.

python


sessions_list = json.loads(rejson).get('sessions',[])

# list_Create a Json that calls the handles method
data = {"janus":'list_handles', "admin_secret": janus_admin_secret, "transaction": transaction, "session_id": sessions_list[0]}

websock.send(json.dumps(data))
rejson =  websock.recv()
print rejson

{
   "janus": "success",
   "transaction": "73677159",
   "session_id": 311091039069809,
   "handles": [
      1721128026873430
   ]
}

Finally, get the information for that handle.

python


handles_list = json.loads(rejson).get('handles',[])

# handle_Create a Json that calls the info method
data = {"janus":'handle_info', "admin_secret": janus_admin_secret, "transaction": transaction, "session_id": sessions_list[0], "handle_id": handles_list[0]}

websock.send(json.dumps(data))
rejson =  websock.recv()
print rejson
{
   "janus": "success",
   "transaction": "73677159",
   "session_id": 311091039069809,
   "handle_id": 1721128026873430,
   "info": {
      "session_id": 311091039069809,
      "session_last_activity": 1367674698,
      "session_transport": "janus.transport.http",
      "handle_id": 1721128026873430,
      "opaque_id": "videoroomtest-L4YiOtywt1nm",
      "created": 464048923,
      "send_thread_created": true,
      "current_time": 1368713443,
      "plugin": "janus.plugin.videoroom",
      "plugin_specific": {
         "type": "publisher",
         "room": 1234,
         "id": 4565592288551424,
         "private_id": 3477273478,
         "display": "You",
         "media": {
            "audio": true,
            "audio_codec": "opus",
            "video": true,
            "video_codec": "h264",
            "data": false
         },
         "bitrate": 128000,
         "audio-level-dBov": 0,
         "talking": false,
         "destroyed": 0
      },
      
・ ・ ・
(abridgement)
・ ・ ・
   }
}

I will explain the above information. Since the plugin of info is "janus.plugin.videoroom" You can see that this user is accessing the videoroom. Also, the details of plugin can be found in plugin_specific. You can see that the room number is "1234" and the name is "You". In addition, you can also get information such as codec, bit rate, and SDP.

Finally

This time, I explained the basic usage of Admin API. There are many other methods in the Admin API. Please check the official website. Next time, I would like to write an article about the plugin API.

Digression

This was my first article on Qiita. Please point out any mistakes.

Recommended Posts

Try using Janus gateway's Admin API
Try using the Twitter API
Try using the Twitter API
Try using the PeeringDB 2.0 API
Try using Pleasant's API (python / FastAPI)
Try using Python argparse's action API
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Try using Dropbox API v2 with Go
Try using Tkinter
Try using docker-py
Try using cookiecutter
Try using PDFMiner
Try using geopandas
Try using Selenium
Try using scipy
Try using pandas.DataFrame
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try using PyODE
Try using the BitFlyer Ligntning API in Python
Try using kabu station API of kabu.com Securities
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
Try drawing a social graph using Twitter API v2
Try using virtualenv (virtualenvwrapper)
Try to make RESTful API with MVC using Flask 1.0.2
[Azure] Try using Azure Functions
Try using virtualenv now
Try to delete tweets in bulk using Twitter API
Try using W & B
Try using Django templates.html
[Kaggle] Try using LGBM
Try using Python's feedparser.
Try using Python's Tkinter
Try NNabla's C ++ API
Try using Tweepy [Python2.7]
Try using Pytorch's collate_fn
Try to determine food photos using Google Cloud Vision API
For the time being, try using the docomo chat dialogue API
Try using PythonTex with Texpad.
[Python] Try using Tkinter's canvas
Try using Jupyter's Docker image
Try using scikit-learn (1) --K-means clustering
Try function optimization using Hyperopt
Test CloudStack API Using Simulator
Try using matplotlib with PyCharm
Try using Azure Logic Apps
Age recognition using Pepper's API
Try using Kubernetes Client -Python-
[Kaggle] Try using xg boost
Try using OpenCV on Windows
Upload videos using YouTube API
Try using Jupyter Notebook dynamically
Try using AWS SageMaker Studio
Try tweeting automatically using Selenium.
Try using SQLAlchemy + MySQL (Part 1)
Try using SQLAlchemy + MySQL (Part 2)
Try using Django's template feature
Try using Pelican's draft feature