File size: 1,230 Bytes
5c1a0cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
token_server.py — Génération de tokens LiveKit pour l'interface Gradio
"""

import os
import uuid
from dotenv import load_dotenv
from livekit.api import AccessToken, VideoGrants

load_dotenv()


def get_connection_details(room_name: str | None = None) -> dict:
    api_key    = os.environ.get("LIVEKIT_API_KEY")
    api_secret = os.environ.get("LIVEKIT_API_SECRET")
    livekit_url = os.environ.get("LIVEKIT_URL")

    if not api_key or not api_secret or not livekit_url:
        raise ValueError(
            "Variables manquantes dans .env : LIVEKIT_URL, LIVEKIT_API_KEY, LIVEKIT_API_SECRET"
        )

    room = room_name or f"voice-room-{uuid.uuid4().hex[:8]}"
    identity = f"user-{uuid.uuid4().hex[:6]}"

    token = (
        AccessToken(api_key=api_key, api_secret=api_secret)
        .with_identity(identity)
        .with_name(identity)
        .with_grants(
            VideoGrants(
                room_join=True,
                room=room,
                can_publish=True,
                can_subscribe=True,
            )
        )
        .to_jwt()
    )

    return {
        "success": True,
        "url": livekit_url,
        "token": token,
        "room": room,
        "identity": identity,
    }