openvidu-basic-java to Livekit
This commit is contained in:
parent
dc1edc5b5b
commit
a154f555b3
@ -31,9 +31,14 @@
|
|||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.openvidu</groupId>
|
<groupId>io.livekit</groupId>
|
||||||
<artifactId>openvidu-java-client</artifactId>
|
<artifactId>livekit-server</artifactId>
|
||||||
<version>2.27.0</version>
|
<version>0.5.7</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
<version>20231013</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
|||||||
@ -2,70 +2,57 @@ package io.openvidu.basic.java;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import io.openvidu.java.client.Connection;
|
import io.livekit.server.*;
|
||||||
import io.openvidu.java.client.ConnectionProperties;
|
|
||||||
import io.openvidu.java.client.OpenVidu;
|
|
||||||
import io.openvidu.java.client.OpenViduHttpException;
|
|
||||||
import io.openvidu.java.client.OpenViduJavaClientException;
|
|
||||||
import io.openvidu.java.client.Session;
|
|
||||||
import io.openvidu.java.client.SessionProperties;
|
|
||||||
|
|
||||||
@CrossOrigin(origins = "*")
|
@CrossOrigin(origins = "*")
|
||||||
@RestController
|
@RestController
|
||||||
public class Controller {
|
public class Controller {
|
||||||
|
|
||||||
@Value("${OPENVIDU_URL}")
|
@Value("${LIVEKIT_URL}")
|
||||||
private String OPENVIDU_URL;
|
private String LIVEKIT_URL;
|
||||||
|
|
||||||
@Value("${OPENVIDU_SECRET}")
|
@Value("${LIVEKIT_API_KEY}")
|
||||||
private String OPENVIDU_SECRET;
|
private String LIVEKIT_API_KEY;
|
||||||
|
|
||||||
private OpenVidu openvidu;
|
@Value("${LIVEKIT_API_SECRET}")
|
||||||
|
private String LIVEKIT_API_SECRET;
|
||||||
@PostConstruct
|
|
||||||
public void init() {
|
|
||||||
this.openvidu = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param params The Session properties
|
* @param params The JSON object with roomName and participantName
|
||||||
* @return The Session ID
|
* @return The JWT token
|
||||||
*/
|
*/
|
||||||
@PostMapping("/api/sessions")
|
@PostMapping("/token")
|
||||||
public ResponseEntity<String> initializeSession(@RequestBody(required = false) Map<String, Object> params)
|
public ResponseEntity<String> getToken(@RequestBody(required = true) Map<String, String> params) {
|
||||||
throws OpenViduJavaClientException, OpenViduHttpException {
|
String roomName = params.get("roomName");
|
||||||
SessionProperties properties = SessionProperties.fromJson(params).build();
|
String participantName = params.get("participantName");
|
||||||
Session session = openvidu.createSession(properties);
|
|
||||||
return new ResponseEntity<>(session.getSessionId(), HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
if(roomName == null || participantName == null) {
|
||||||
* @param sessionId The Session in which to create the Connection
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||||
* @param params The Connection properties
|
|
||||||
* @return The Token associated to the Connection
|
|
||||||
*/
|
|
||||||
@PostMapping("/api/sessions/{sessionId}/connections")
|
|
||||||
public ResponseEntity<String> createConnection(@PathVariable("sessionId") String sessionId,
|
|
||||||
@RequestBody(required = false) Map<String, Object> params)
|
|
||||||
throws OpenViduJavaClientException, OpenViduHttpException {
|
|
||||||
Session session = openvidu.getActiveSession(sessionId);
|
|
||||||
if (session == null) {
|
|
||||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
||||||
}
|
}
|
||||||
ConnectionProperties properties = ConnectionProperties.fromJson(params).build();
|
|
||||||
Connection connection = session.createConnection(properties);
|
// By default, tokens expire 6 hours after generation.
|
||||||
return new ResponseEntity<>(connection.getToken(), HttpStatus.OK);
|
// You may override this by using token.setTtl(long millis).
|
||||||
|
AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
|
||||||
|
token.setName(participantName);
|
||||||
|
token.setIdentity(participantName);
|
||||||
|
|
||||||
|
|
||||||
|
JSONObject metadata = new JSONObject();
|
||||||
|
metadata.put("livekitUrl", LIVEKIT_URL);
|
||||||
|
// add metadata to the token, which will be available in the participant's metadata
|
||||||
|
token.setMetadata(metadata.toString());
|
||||||
|
token.addGrants(new RoomJoin(true), new RoomName(roomName));
|
||||||
|
|
||||||
|
return new ResponseEntity<>(token.toJwt(), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
server.port: 5000
|
server.port: 5000
|
||||||
server.ssl.enabled: false
|
server.ssl.enabled: false
|
||||||
|
|
||||||
OPENVIDU_URL: http://localhost:4443/
|
LIVEKIT_URL: ws://localhost:7880/
|
||||||
OPENVIDU_SECRET: MY_SECRET
|
LIVEKIT_API_KEY: http://localhost:4443/
|
||||||
|
LIVEKIT_API_SECRET: MY_SECRET
|
||||||
Loading…
x
Reference in New Issue
Block a user