Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Integrate LiveKit for doctor connect #1227

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions care/facility/api/viewsets/livekit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import hashlib

from django.conf import settings
from livekit import AccessToken, VideoGrant
from rest_framework.decorators import action
from rest_framework.exceptions import ValidationError
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet


def generate_room_code(source_user, target_user):
combined = "".join(sorted([source_user, target_user]))
hash_object = hashlib.sha256(combined.encode("utf-8"))
hash_hex = hash_object.hexdigest()
return settings.LIVEKIT_ROOM_NAME_PREFIX + hash_hex


class LiveKitViewSet(
GenericViewSet,
):
# permission_classes = (IsAuthenticated,)

@action(detail=False, methods=["POST"])
def get_token(self, request):
source_username = request.data.get("source")
target_username = request.data.get("target")

if not source_username or not target_username:
raise ValidationError("source_username and target_username are required")

room_code = generate_room_code(source_username, target_username)

grant = VideoGrant(room_join=True, room=room_code)
access_token = AccessToken(
settings.LIVEKIT_API_KEY,
settings.LIVEKIT_API_SECRET,
grant=grant,
identity=source_username,
name=source_username,
)
token = access_token.to_jwt()

return Response({"access": str(token), "room_code": room_code})
10 changes: 7 additions & 3 deletions config/api_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
FacilityInventoryMinQuantityViewSet,
FacilityInventorySummaryViewSet,
)
from care.facility.api.viewsets.livekit import LiveKitViewSet
from care.facility.api.viewsets.notification import NotificationViewSet
from care.facility.api.viewsets.patient import (
FacilityPatientStatsHistoryViewSet,
Expand Down Expand Up @@ -69,6 +70,9 @@
from care.facility.summarisation.patient_summary import PatientSummaryViewSet
from care.facility.summarisation.tests_summary import TestsSummaryViewSet
from care.facility.summarisation.triage_summary import TriageSummaryViewSet
from care.hcx.api.viewsets.claim import ClaimViewSet
from care.hcx.api.viewsets.gateway import HcxGatewayViewSet
from care.hcx.api.viewsets.policy import PolicyViewSet
from care.users.api.viewsets.lsg import (
DistrictViewSet,
LocalBodyViewSet,
Expand All @@ -78,9 +82,6 @@
from care.users.api.viewsets.skill import SkillViewSet
from care.users.api.viewsets.users import UserViewSet
from care.users.api.viewsets.userskill import UserSkillViewSet
from care.hcx.api.viewsets.policy import PolicyViewSet
from care.hcx.api.viewsets.claim import ClaimViewSet
from care.hcx.api.viewsets.gateway import HcxGatewayViewSet

if settings.DEBUG:
router = DefaultRouter()
Expand Down Expand Up @@ -198,6 +199,9 @@
router.register("hcx/claim", ClaimViewSet)
router.register("hcx", HcxGatewayViewSet)

# LiveKit
router.register("livekit", LiveKitViewSet, basename="livekit")

# Public endpoints
router.register("public/asset", AssetPublicViewSet)

Expand Down
6 changes: 6 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,9 @@ def GETKEY(group, request):
JWKS = JsonWebKey.import_key_set(
json.loads(base64.b64decode(env("JWKS_BASE64", default=generate_encoded_jwks())))
)

# Livekit Settings
LIVEKIT_API_URL = env("LIVEKIT_API_URL", default="wss://livekit.ohc.network")
LIVEKIT_API_KEY = env("LIVEKIT_API_KEY", default="")
LIVEKIT_API_SECRET = env("LIVEKIT_API_SECRET", default="")
LIVEKIT_ROOM_NAME_PREFIX = env("LIVEKIT_ROOM_NAME_PREFIX", default="care-")