openvidu-basic-python to LiveKit

This commit is contained in:
Carlos Santos 2023-10-16 14:22:48 +02:00
parent a6e9dba601
commit 5acef39835
3 changed files with 26 additions and 36 deletions

View File

@ -1,3 +1,4 @@
SERVER_PORT=5000
OPENVIDU_URL=http://localhost:4443/
OPENVIDU_SECRET=MY_SECRET
LIVEKIT_URL=ws://localhost:7880/
LIVEKIT_API_KEY=devkey
LIVEKIT_API_SECRET=secret

View File

@ -1,5 +1,6 @@
import os
import requests
import livekit
from flask import Flask, request
from flask_cors import CORS
@ -10,42 +11,29 @@ cors = CORS(app, resources={r"/*": {"origins": "*"}})
# Load env variables
SERVER_PORT = os.environ.get("SERVER_PORT")
OPENVIDU_URL = os.environ.get("OPENVIDU_URL")
OPENVIDU_SECRET = os.environ.get("OPENVIDU_SECRET")
LIVEKIT_URL = os.environ.get("LIVEKIT_URL")
LIVEKIT_API_KEY = os.environ.get("LIVEKIT_API_KEY")
LIVEKIT_API_SECRET = os.environ.get("LIVEKIT_API_SECRET")
@app.route("/api/sessions", methods=['POST'])
def initializeSession():
try:
body = request.json if request.data else {}
response = requests.post(
OPENVIDU_URL + "openvidu/api/sessions",
verify=False,
auth=("OPENVIDUAPP", OPENVIDU_SECRET),
headers={'Content-type': 'application/json'},
json=body
)
response.raise_for_status()
return response.json()["sessionId"]
except requests.exceptions.HTTPError as err:
if (err.response.status_code == 409):
# Session already exists in OpenVidu
return request.json["customSessionId"]
else:
return err
@app.route("/token", methods=['POST'])
def getToken():
room_name = request.json.get("roomName")
participant_name = request.json.get("participantName")
@app.route("/api/sessions/<sessionId>/connections", methods=['POST'])
def createConnection(sessionId):
body = request.json if request.data else {}
return requests.post(
OPENVIDU_URL + "openvidu/api/sessions/" + sessionId + "/connection",
verify=False,
auth=("OPENVIDUAPP", OPENVIDU_SECRET),
headers={'Content-type': 'application/json'},
json=body
).json()["token"]
if not room_name or not participant_name:
return "roomName and participantName are required", 400
# Create a VideoGrant with the necessary permissions
grant = livekit.VideoGrant(room_join=True, room=room_name)
# Create an AccessToken with your API key, secret, and the VideoGrant
access_token = livekit.AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, grant=grant, identity=participant_name)
access_token.metadata = {"livekitUrl": LIVEKIT_URL}
# Generate the token
return access_token.to_jwt()
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=SERVER_PORT)

View File

@ -1,5 +1,6 @@
flask==2.1.2
flask-cors==3.0.10
flask==3.0.0
flask-cors==4.0.0
requests==2.28.0
python-dotenv==0.20.0
pyopenssl==22.0.0
pyopenssl==22.0.0
livekit-server-sdk-python==1.0.0