feat: update LiveKit configuration and enhance collaboration session handling

- Updated the `livekit-server-sdk` version to 2.9.2 for improved functionality.
- Refactored `LiveKitEgressService` to include validation for required environment variables and added a method to stop recording.
- Enhanced `LiveKitRoomService` to validate environment variables and dynamically set egress configurations for recording sessions.
- Modified `CollaborationSessionSchema` to correct the relation name for `chatRoom` and updated the schema to reflect the new structure.
- Improved error logging for better traceability during collaboration session creation.
This commit is contained in:
2024-12-05 16:51:19 +07:00
parent 7248ea257f
commit 0b3f4d6728
6 changed files with 76 additions and 783 deletions

View File

@@ -29,7 +29,7 @@ export class CollaborationSessionSchema extends PothosSchema {
chatRoomId: t.exposeString('chatRoomId', {
description: 'The ID of the chat room.',
}),
chatRoom: t.relation('chatRoom', {
chatRoom: t.relation('ChatRoom', {
description: 'The chat room.',
}),
collaboratorsIds: t.exposeStringList('collaboratorsIds', {
@@ -141,6 +141,7 @@ export class CollaborationSessionSchema extends PothosSchema {
})
if (!chatRoom) throw new Error('Chat room not found')
// create new one
Logger.log(`chatRoom: ${chatRoom.id}`)
const newCollaborationSession = await this.prisma.collaborationSession.create({
data: {
scheduleDateId: scheduleDate.id,

View File

@@ -1,4 +1,3 @@
// @ts-nocheck
import { Injectable } from '@nestjs/common'
import {
EgressClient,
@@ -6,6 +5,7 @@ import {
StreamOutput,
EncodedFileType,
EncodingOptionsPreset,
// @ts-expect-error
} from 'livekit-server-sdk'
@Injectable()
@@ -20,13 +20,25 @@ export class LiveKitEgressService {
output: {
case: 's3',
value: {
bucket: process.env.RECORDING_BUCKET_NAME as string,
accessKey: process.env.MINIO_ACCESS_KEY as string,
secret: process.env.MINIO_SECRET_KEY as string,
endpoint: process.env.BASE_URL,
region: process.env.MINIO_REGION,
bucket: 'objects',
accessKey: process.env.MINIO_ACCESS_KEY,
secret: process.env.MINIO_SECRET_KEY,
},
},
filepath: 'epess/records',
})
constructor() {}
constructor() {
if (
!process.env.MINIO_ACCESS_KEY ||
!process.env.MINIO_SECRET_KEY ||
!process.env.MINIO_ENDPOINT ||
!process.env.MINIO_REGION
) {
throw new Error('Missing required environment variables for recording')
}
}
async startRecording(roomId: string) {
const egress = await this.egressClient.startRoomCompositeEgress(roomId, this.output, {
@@ -37,4 +49,8 @@ export class LiveKitEgressService {
})
return egress
}
async stopRecording(egressId: string) {
await this.egressClient.stopEgress(egressId)
}
}

View File

@@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common'
// @ts-expect-error
import { Room, RoomServiceClient, RoomEgress } from 'livekit-server-sdk'
import { LiveKitEgressService } from './livekit.egress'
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
@Injectable()
export class LiveKitRoomService {
@@ -10,27 +11,37 @@ export class LiveKitRoomService {
process.env.LIVEKIT_API_KEY as string,
process.env.LIVEKIT_API_SECRET as string,
)
private readonly roomEgress = new RoomEgress({
tracks: {
output: {
case: 's3',
value: {
bucket: process.env.RECORDING_BUCKET_NAME as string,
accessKey: process.env.MINIO_ACCESS_KEY as string,
secret: process.env.MINIO_SECRET_KEY as string,
endpoint: process.env.MINIO_ENDPOINT as string,
},
},
filepath: '${roomName}/${roomName}-${start_time}',
},
})
constructor(private readonly egressService: LiveKitEgressService) {}
async createServiceMeetingRoom(roomId: string) {
// validate env
if (
!process.env.MINIO_ACCESS_KEY ||
!process.env.MINIO_SECRET_KEY ||
!process.env.MINIO_ENDPOINT ||
!process.env.MINIO_REGION
) {
throw new Error('Missing required environment variables for recording')
}
const room = await this.roomServiceClient.createRoom({
name: roomId,
maxParticipants: 2,
egress: this.roomEgress,
egress: new RoomEgress({
tracks: {
output: {
case: 's3',
value: {
bucket: 'objects',
accessKey: process.env.MINIO_ACCESS_KEY,
secret: process.env.MINIO_SECRET_KEY,
endpoint: process.env.BASE_URL,
region: process.env.MINIO_REGION,
},
},
filepath: `epess/records/${roomId}/${roomId}-${DateTimeUtils.now().toISO()}`,
},
}),
departureTimeout: 1000 * 60 * 10, // 10 minutes
})
return room
}
@@ -39,6 +50,21 @@ export class LiveKitRoomService {
const room = await this.roomServiceClient.createRoom({
name: roomId,
maxParticipants,
egress: new RoomEgress({
tracks: {
output: {
case: 's3',
value: {
bucket: 'objects',
accessKey: process.env.MINIO_ACCESS_KEY,
secret: process.env.MINIO_SECRET_KEY,
endpoint: process.env.BASE_URL,
region: process.env.MINIO_REGION,
},
},
filepath: `epess/records/${roomId}/${roomId}-${DateTimeUtils.now().toISO()}`,
},
}),
})
return room
}

File diff suppressed because one or more lines are too long