openvidu/openvidu-testapp/src/app/services/room-api.service.ts

319 lines
8.8 KiB
TypeScript
Raw Normal View History

2024-07-02 19:19:05 +02:00
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import * as _ from 'lodash';
import {
AccessToken,
CreateIngressOptions,
DirectFileOutput,
EgressClient,
EgressInfo,
EncodedOutputs,
EncodingOptions,
EncodingOptionsPreset,
IngressClient,
IngressInfo,
IngressInput,
IngressVideoEncodingPreset,
ParticipantInfo,
Room,
RoomCompositeOptions,
RoomServiceClient,
TrackCompositeOptions,
TrackInfo,
VideoGrant,
} from 'livekit-server-sdk';
import { LivekitParamsService } from './livekit-params.service';
import { VideoQuality } from 'livekit-client';
import {
IngressVideoEncodingOptions,
VideoCodec,
VideoLayer,
} from '@livekit/protocol';
2024-07-02 19:19:05 +02:00
@Injectable({
providedIn: 'root',
2024-07-02 19:19:05 +02:00
})
export class RoomApiService {
private roomServiceClient: RoomServiceClient;
private egressClient: EgressClient;
private ingressClient: IngressClient;
2024-07-02 19:19:05 +02:00
constructor(
private http: HttpClient,
private livekitParamsService: LivekitParamsService
) {
this.roomServiceClient = new RoomServiceClient(
this.getRestUrl(),
this.livekitParamsService.getParams().livekitApiKey,
this.livekitParamsService.getParams().livekitApiSecret
);
this.egressClient = new EgressClient(
this.getRestUrl(),
this.livekitParamsService.getParams().livekitApiKey,
this.livekitParamsService.getParams().livekitApiSecret
);
this.ingressClient = new IngressClient(
this.getRestUrl(),
this.livekitParamsService.getParams().livekitApiKey,
this.livekitParamsService.getParams().livekitApiSecret
);
}
2024-07-02 19:19:05 +02:00
async createToken(
permissions: VideoGrant,
participantName?: string,
roomName?: string
): Promise<string> {
const at = new AccessToken(
this.livekitParamsService.getParams().livekitApiKey,
this.livekitParamsService.getParams().livekitApiSecret,
{ identity: participantName }
);
if (roomName) {
permissions.room = roomName;
2024-07-02 19:19:05 +02:00
}
at.addGrant(permissions);
return at.toJwt();
}
2024-07-02 19:19:05 +02:00
/*
* RoomService API
* https://docs.livekit.io/reference/server/server-apis/
*/
2024-07-02 19:19:05 +02:00
async listRooms(): Promise<Room[]> {
return this.roomServiceClient.listRooms();
}
2024-07-02 19:19:05 +02:00
async deleteRoom(roomName: string): Promise<void> {
return await this.roomServiceClient.deleteRoom(roomName);
}
2024-07-02 19:19:05 +02:00
async listParticipants(roomName: string): Promise<ParticipantInfo[]> {
return await this.roomServiceClient.listParticipants(roomName);
}
2024-07-02 19:19:05 +02:00
async getParticipant(
roomName: string,
participantIdentity: string
): Promise<ParticipantInfo> {
return await this.roomServiceClient.getParticipant(
roomName,
participantIdentity
);
}
2024-07-02 19:19:05 +02:00
async removeParticipant(
roomName: string,
participantIdentity: string
): Promise<void> {
return await this.roomServiceClient.removeParticipant(
roomName,
participantIdentity
);
}
2024-07-02 19:19:05 +02:00
async mutePublishedTrack(
roomName: string,
participantIdentity: string,
track_sid: string,
muted: boolean
): Promise<TrackInfo> {
return await this.roomServiceClient.mutePublishedTrack(
roomName,
participantIdentity,
track_sid,
muted
);
}
2024-07-02 19:19:05 +02:00
async listEgress(): Promise<EgressInfo[]> {
return await this.egressClient.listEgress({});
}
2024-07-02 19:19:05 +02:00
async startRoomCompositeEgress(
roomName: string,
roomCompositeOptions: RoomCompositeOptions,
encodedOutputs: EncodedOutputs,
encodingOptions?: EncodingOptionsPreset | EncodingOptions
): Promise<EgressInfo> {
if (encodedOutputs.file) {
encodedOutputs.file.filepath =
'RoomComposite-{room_id}-{room_name}-{time}';
2024-07-02 19:19:05 +02:00
}
if (encodingOptions) {
roomCompositeOptions.encodingOptions = encodingOptions;
2024-07-02 19:19:05 +02:00
}
return await this.egressClient.startRoomCompositeEgress(
roomName,
encodedOutputs,
roomCompositeOptions
);
}
2024-07-02 19:19:05 +02:00
async startTrackCompositeEgress(
roomName: string,
audioTrackId: string,
videoTrackId: string,
encodedOutputs: EncodedOutputs,
encodingOptions?: EncodingOptionsPreset | EncodingOptions
): Promise<EgressInfo> {
if (encodedOutputs.file) {
encodedOutputs.file.filepath =
'TrackComposite-{room_id}-{room_name}-{time}-{publisher_identity}';
2024-07-02 19:19:05 +02:00
}
const trackCompositeOptions: TrackCompositeOptions = {
audioTrackId,
videoTrackId,
};
if (encodingOptions) {
trackCompositeOptions.encodingOptions = encodingOptions;
2024-07-02 19:19:05 +02:00
}
return await this.egressClient.startTrackCompositeEgress(
roomName,
encodedOutputs,
trackCompositeOptions
);
}
2024-07-02 19:19:05 +02:00
async startTrackEgress(
roomName: string,
track_id: string,
output?: DirectFileOutput | string
): Promise<EgressInfo> {
if (!output) {
let outputAux = {
filepath:
'Track-{room_id}-{room_name}-{time}-{publisher_identity}-{track_id}-{track_type}-{track_source}',
};
output = outputAux as DirectFileOutput;
2024-07-02 19:19:05 +02:00
}
return await this.egressClient.startTrackEgress(roomName, output, track_id);
}
2024-07-02 19:19:05 +02:00
async stopEgress(egressId: string): Promise<EgressInfo> {
return await this.egressClient.stopEgress(egressId);
}
2024-07-02 19:19:05 +02:00
async listIngress(): Promise<IngressInfo[]> {
const ingressClient: IngressClient = new IngressClient(
this.getRestUrl(),
this.livekitParamsService.getParams().livekitApiKey,
this.livekitParamsService.getParams().livekitApiSecret
);
return await ingressClient.listIngress({});
}
2024-07-02 19:19:05 +02:00
async createIngress(
room_name: string,
inputType: IngressInput,
urlInputType: string,
withAudio: boolean,
withVideo: boolean,
codec: VideoCodec,
simulcast: boolean,
enableTranscoding: boolean,
preset?: IngressVideoEncodingPreset
): Promise<IngressInfo> {
let url;
switch (urlInputType) {
case 'HTTP':
if (!withVideo) {
url =
'https://s3.eu-west-1.amazonaws.com/public.openvidu.io/bbb_sunflower_1080p_60fps_normal_onlyaudio.mp3';
} else {
url = withAudio
? 'https://s3.eu-west-1.amazonaws.com/public.openvidu.io/bbb_sunflower_1080p_60fps_normal.mp4'
: 'https://s3.eu-west-1.amazonaws.com/public.openvidu.io/bbb_sunflower_1080p_60fps_normal_noaudio.mp4';
}
break;
case 'SRT':
url = 'srt://host.docker.internal:8554/';
break;
case 'RTSP':
url = 'rtsp://host.docker.internal:8554/';
break;
}
let options: CreateIngressOptions = {
name: inputType + '-' + room_name,
roomName: room_name,
participantIdentity: 'IngressParticipantIdentity',
participantName: 'MyIngress',
participantMetadata: 'IngressParticipantMetadata',
url,
};
if (inputType === IngressInput.WHIP_INPUT) {
options.enableTranscoding = enableTranscoding;
}
if (inputType != IngressInput.WHIP_INPUT || enableTranscoding) {
options.video = {
encodingOptions: {
case: preset != undefined ? 'preset' : 'options',
value: {},
},
} as any;
if (preset != undefined) {
options.video!.encodingOptions.value = preset;
} else {
const encodingOptions = options.video!.encodingOptions
.value! as IngressVideoEncodingOptions;
encodingOptions.videoCodec = codec;
encodingOptions.frameRate = 30;
let layers: VideoLayer[] = [];
if (simulcast) {
layers = [
{
quality: VideoQuality.HIGH,
width: 1920,
height: 1080,
},
{
quality: VideoQuality.MEDIUM,
width: 1280,
height: 720,
},
{
quality: VideoQuality.LOW,
width: 640,
height: 360,
},
] as VideoLayer[];
} else {
layers = [
{
quality: VideoQuality.HIGH,
width: 1920,
height: 1080,
} as VideoLayer,
];
}
encodingOptions.layers = layers;
}
}
const ingressInfo = await this.ingressClient.createIngress(
inputType,
options
);
return ingressInfo;
}
2024-07-02 19:19:05 +02:00
async deleteIngress(ingressId: string): Promise<IngressInfo> {
const ingressClient: IngressClient = new IngressClient(
this.getRestUrl(),
this.livekitParamsService.getParams().livekitApiKey,
this.livekitParamsService.getParams().livekitApiSecret
);
return await ingressClient.deleteIngress(ingressId);
}
2024-07-02 19:19:05 +02:00
private getRestUrl() {
const wsUrl = this.livekitParamsService.getParams().livekitUrl;
const protocol =
wsUrl.startsWith('wss:') || wsUrl.startsWith('https:') ? 'https' : 'http';
return `${protocol}://${wsUrl
.substring(wsUrl.indexOf('//') + 2)
.replace(/\/$/, '')}`;
}
}