diff --git a/openvidu-basic-python/.env b/openvidu-basic-python/.env index 76dfb4e8..053449e8 100644 --- a/openvidu-basic-python/.env +++ b/openvidu-basic-python/.env @@ -1,3 +1,4 @@ SERVER_PORT=5000 -OPENVIDU_URL=http://localhost:4443/ -OPENVIDU_SECRET=MY_SECRET \ No newline at end of file +LIVEKIT_URL=ws://localhost:7880/ +LIVEKIT_API_KEY=devkey +LIVEKIT_API_SECRET=secret \ No newline at end of file diff --git a/openvidu-basic-python/app.py b/openvidu-basic-python/app.py index 66dc4da6..d9c854f8 100644 --- a/openvidu-basic-python/app.py +++ b/openvidu-basic-python/app.py @@ -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//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) diff --git a/openvidu-basic-python/requirements.txt b/openvidu-basic-python/requirements.txt index bc5b5502..f72ba6d3 100644 --- a/openvidu-basic-python/requirements.txt +++ b/openvidu-basic-python/requirements.txt @@ -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 \ No newline at end of file +pyopenssl==22.0.0 +livekit-server-sdk-python==1.0.0 \ No newline at end of file