You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
3.8 KiB
83 lines
3.8 KiB
5 years ago
|
from random import randint
|
||
|
from string import digits, ascii_uppercase, ascii_lowercase
|
||
|
from urllib.parse import urljoin
|
||
|
|
||
|
import requests
|
||
|
|
||
|
|
||
|
class Janus:
|
||
|
url: str = None
|
||
|
admin_key: str = None
|
||
|
session_id: int = None
|
||
|
attach_id: int = None
|
||
|
|
||
|
def __init__(self, url: str, admin_key: str = None) -> None:
|
||
|
self.url = url
|
||
|
self.admin_key = admin_key
|
||
|
|
||
|
def attach(self):
|
||
|
session_uri = urljoin(self.url, "{}/".format(self.session_id))
|
||
|
request_body = {"janus": "attach", "plugin": "janus.plugin.videoroom",
|
||
|
"opaque_id": "room-{}".format(self.create_transaction_id()),
|
||
|
"transaction": self.create_transaction_id()}
|
||
|
response = requests.post(session_uri, json=request_body).json()
|
||
|
if response.get('janus') == "success":
|
||
|
self.attach_id = response.get('data').get('id')
|
||
|
return True
|
||
|
return False
|
||
|
def rtp_forward(self,room, publisher):
|
||
|
attach_uri = urljoin(self.url, "{}/{}".format(self.session_id, self.attach_id))
|
||
|
|
||
|
request_body = {"janus": "message",
|
||
|
"body": {"request": "rtp_forward", 'secret': 'adminpwd', "admin_key": self.admin_key, "room": room,
|
||
|
"publisher_id": publisher, "audio_port": 10033, "audiopt": 111, "video_port": 10038, "videopt": 100, "host": "127.0.0.1"
|
||
|
},
|
||
|
"transaction": self.create_transaction_id()}
|
||
|
response = requests.post(attach_uri, json=request_body).json()
|
||
|
print(response)
|
||
|
def kick(self, room, publisher):
|
||
|
attach_uri = urljoin(self.url, "{}/{}".format(self.session_id, self.attach_id))
|
||
|
request_body = {"janus": "message",
|
||
|
"body": {"request": "kick", 'secret': 'adminpwd', "admin_key": self.admin_key, "room": room, "id": publisher
|
||
|
},
|
||
|
"transaction": self.create_transaction_id()}
|
||
|
response = requests.post(attach_uri, json=request_body).json()
|
||
|
print(response)
|
||
|
|
||
|
def create_room(self, name: str = "", video_codec: str = "vp9", audio_codec: str = "opus",
|
||
|
publisher_count: int = 16,
|
||
|
bitrate: int = 500000, data: bool = False) -> int:
|
||
|
attach_uri = urljoin(self.url, "{}/{}".format(self.session_id, self.attach_id))
|
||
|
request_body = {"janus": "message",
|
||
|
"body": {"request": "create", "admin_key": self.admin_key, "audiolevel_ext": True,
|
||
|
"audiolevel_event": True,
|
||
|
"description": name, "bitrate": bitrate,
|
||
|
"publishers": publisher_count,
|
||
|
"audiocodec": audio_codec,
|
||
|
"fir_freq": 10,
|
||
|
"videocodec": video_codec,
|
||
|
"bitrate_cap": True,
|
||
|
"data": data
|
||
|
},
|
||
|
"transaction": self.create_transaction_id()}
|
||
|
print(request_body)
|
||
|
response = requests.post(attach_uri, json=request_body).json()
|
||
|
print(response)
|
||
|
return response.get("plugindata").get('data').get('room')
|
||
|
|
||
|
@staticmethod
|
||
|
def create_transaction_id(length: int = 16) -> str:
|
||
|
letters = ascii_lowercase + ascii_uppercase + digits
|
||
|
transaction_id = ''.join([letters[randint(0, len(letters) - 1)] for rid in range(length)])
|
||
|
return transaction_id
|
||
|
|
||
|
def connect(self) -> bool:
|
||
|
transaction_id = self.create_transaction_id()
|
||
|
request_data = {
|
||
|
"janus": "create",
|
||
|
"transaction": transaction_id
|
||
|
}
|
||
|
response = requests.post(self.url, json=request_data).json()
|
||
|
self.session_id = response.get('data').get('id')
|
||
|
return self.attach()
|