Skip to content

Commit

Permalink
Introduce a plugin turms-plugin-livekit to support video conferencing
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesChenX committed May 17, 2024
1 parent 0c3daf1 commit 409c88d
Show file tree
Hide file tree
Showing 21 changed files with 2,620 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<module>turms-service</module>

<module>turms-plugins/turms-plugin-antispam</module>
<module>turms-plugins/turms-plugin-livekit</module>
<module>turms-plugins/turms-plugin-minio</module>
<module>turms-plugins/turms-plugin-push</module>
<module>turms-plugins/turms-plugin-rasa</module>
Expand Down
118 changes: 118 additions & 0 deletions turms-plugins/turms-plugin-livekit/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>im.turms</groupId>
<artifactId>turms-parent</artifactId>
<version>${revision}</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>turms-plugin-livekit</artifactId>
<version>${revision}</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>im.turms</groupId>
<artifactId>turms-service</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- Testing -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>${os-maven-plugin.version}</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessors>
<annotationProcessor>lombok.launch.AnnotationProcessorHider$AnnotationProcessor
</annotationProcessor>
</annotationProcessors>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<!-- Prevent generating the "original" jar file -->
<finalName>${project.artifactId}-${project.version}</finalName>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>artifact-protobuf</id>
<build>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}
</protocArtifact>
<protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
<outputDirectory>${project.build.sourceDirectory}</outputDirectory>
<clearOutputDirectory>false</clearOutputDirectory>
<temporaryProtoFileDirectory>${project.build.directory}/protoc-dependencies
</temporaryProtoFileDirectory>
<!-- prevent Command line is too long errors -->
<useArgumentFile>true</useArgumentFile>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2019 The Turms Project
* https://github.com/turms-im/turms
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.turms.plugin.livekit;

import java.util.Set;

import im.turms.server.common.infra.plugin.TurmsExtension;
import im.turms.server.common.infra.plugin.TurmsPlugin;

/**
* @author James Chen
*/
public class LiveKitConferencePlugin extends TurmsPlugin {

@Override
public Set<Class<? extends TurmsExtension>> getExtensions() {
return Set.of(LiveKitConferenceServiceProvider.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (C) 2019 The Turms Project
* https://github.com/turms-im/turms
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.turms.plugin.livekit;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import jakarta.validation.constraints.NotNull;

import reactor.core.publisher.Mono;

import im.turms.plugin.livekit.core.LiveKitClient;
import im.turms.plugin.livekit.core.webhook.WebhookEventNameConst;
import im.turms.plugin.livekit.property.LiveKitProperties;
import im.turms.server.common.infra.lang.MathUtil;
import im.turms.server.common.infra.plugin.ExtensionPointMethod;
import im.turms.server.common.infra.plugin.TurmsExtension;
import im.turms.service.domain.conference.po.Meeting;
import im.turms.service.infra.plugin.extension.ConferenceServiceProvider;
import im.turms.service.infra.plugin.extension.model.AcceptMeetingInvitationResult;
import im.turms.service.infra.plugin.extension.model.ConferenceEvent;
import im.turms.service.infra.plugin.extension.model.CreateMeetingOptions;
import im.turms.service.infra.plugin.extension.model.CreateMeetingResult;
import im.turms.service.infra.plugin.extension.model.MeetingEndedEvent;

/**
* The class is an bridge between Turms and LiveKit.
*
* @author James Chen
*/
public class LiveKitConferenceServiceProvider extends TurmsExtension
implements ConferenceServiceProvider {

private LiveKitClient liveKitClient;
private Map<String, Consumer<? extends ConferenceEvent>> eventToListener;

@Override
protected Mono<Void> start() {
eventToListener = new ConcurrentHashMap<>(8);
LiveKitProperties properties = loadProperties(LiveKitProperties.class);
liveKitClient = new LiveKitClient(properties, webhookEvent -> {
Consumer consumer = eventToListener.get(webhookEvent.getEvent());
if (consumer != null) {
consumer.accept(webhookEvent);
}
});
return liveKitClient.start();
}

@Override
protected Mono<Void> stop() {
eventToListener.clear();
return liveKitClient.stop();
}

@ExtensionPointMethod
@Override
public Mono<Void> addMeetingEndedEventListener(@NotNull Consumer<MeetingEndedEvent> listener) {
eventToListener.put(WebhookEventNameConst.ROOM_FINISHED, listener);
return Mono.empty();
}

@ExtensionPointMethod
@Override
public Mono<CreateMeetingResult> createMeeting(
@NotNull Long requesterId,
@NotNull Meeting meeting,
@NotNull CreateMeetingOptions options) {
String roomName = getRoomName(meeting.getId());
Long idleTimeoutMillis = options.idleTimeoutMillis();
return liveKitClient.getRoomService()
// TODO: support configuring these parameters.
.createRoom(roomName,
idleTimeoutMillis == null
? null
: MathUtil.toInt(idleTimeoutMillis / 1000),
options.maxParticipants(),
null,
null,
null,
null,
null,
null)
.map(room -> new CreateMeetingResult(
liveKitClient.generateRoomAccessToken(roomName)));
}

@ExtensionPointMethod
@Override
public Mono<Void> cancelMeeting(@NotNull Long requesterId, @NotNull Long meetingId) {
return liveKitClient.getRoomService()
.deleteRoom(String.valueOf(meetingId));
}

@ExtensionPointMethod
@Override
public Mono<Integer> countActiveMeetingsByUserId(@NotNull Long userId) {
// TODO: the latest version of livekit (v1.6.1) does not support this feature.
return Mono.empty();
}

@ExtensionPointMethod
@Override
public Mono<AcceptMeetingInvitationResult> acceptMeetingInvitation(
@NotNull Long requesterId,
@NotNull Long meetingId) {
String accessToken = liveKitClient.generateRoomAccessToken(getRoomName(meetingId));
return Mono.just(new AcceptMeetingInvitationResult(accessToken));
}

/**
* @implNote Note that LiveKit uses the room name as the ID internally (e.g. LiveKit server uses
* the room name as the key of the room in Redis), so we use the Turms meeting ID as
* the LiveKit room name.
*/
private String getRoomName(Long meetingId) {
return String.valueOf(meetingId);
}
}
Loading

0 comments on commit 409c88d

Please sign in to comment.