Skip to content
Merged
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
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ dependencies {
// Deploy Health-Check
implementation 'org.springframework.boot:spring-boot-starter-actuator'

//Openvidu
implementation 'io.livekit:livekit-server:0.9.0'


}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.example.be.web.controller;

import com.example.be.web.dto.OpenviduDTO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import io.livekit.server.AccessToken;
import io.livekit.server.RoomJoin;
import io.livekit.server.RoomName;
import io.livekit.server.WebhookReceiver;
import livekit.LivekitWebhook.WebhookEvent;

@CrossOrigin(origins = "*")
@RestController
public class OpenviduController {

@Value("${livekit.api.key}")
private String LIVEKIT_API_KEY;

@Value("${livekit.api.secret}")
private String LIVEKIT_API_SECRET;


@PostMapping(value = "/token")
public ResponseEntity<Map<String, String>> createToken(@RequestBody OpenviduDTO.TokenRequestDto request) {
String roomName = request.getRoomName();
String participantName = request.getParticipantName();

if (roomName == null || participantName == null) {
return ResponseEntity.badRequest().body(Map.of("errorMessage", "roomName and participantName are required"));
}

AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
token.setName(participantName);
token.setIdentity(participantName);
token.addGrants(new RoomJoin(true), new RoomName(roomName));

return ResponseEntity.ok(Map.of("token", token.toJwt()));
}

@PostMapping(value = "/livekit/webhook", consumes = "application/webhook+json")
public ResponseEntity<String> receiveWebhook(@RequestHeader("Authorization") String authHeader, @RequestBody String body) {
WebhookReceiver webhookReceiver = new WebhookReceiver(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
try {
WebhookEvent event = webhookReceiver.receive(body, authHeader);
System.out.println("LiveKit Webhook: " + event.toString());
} catch (Exception e) {
System.err.println("Error validating webhook event: " + e.getMessage());
}
return ResponseEntity.ok("ok");
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/example/be/web/dto/OpenviduDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.be.web.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

public class OpenviduDTO {

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class TokenRequestDto {
private String roomName;
private String participantName;
}
}
5 changes: 5 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,8 @@ server:
protocol: https
host: 8080
port: 8081

livekit:
api:
key: ${LIVEKIT_API_KEY}
secret: ${LIVEKIT_API_SECRET}