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.
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.
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
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.
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.
This was my first article on Qiita. Please point out any mistakes.
Recommended Posts