openvidu/openvidu-testapp/src/app/components/track/track.component.ts

172 lines
4.7 KiB
TypeScript
Raw Normal View History

2024-07-02 19:19:05 +02:00
import { Component, EventEmitter, Input, Output } from '@angular/core';
import {
TrackPublication,
LocalParticipant,
Track,
TrackEvent,
LocalTrack,
RemoteTrack,
TrackEventCallbacks,
LocalTrackPublication,
RemoteTrackPublication,
} from 'livekit-client';
import {
TestAppEvent,
TestFeedService,
} from 'src/app/services/test-feed.service';
2024-07-02 19:19:05 +02:00
@Component({
selector: 'app-track',
template: '',
styleUrls: [],
2024-07-02 19:19:05 +02:00
})
export class TrackComponent {
@Output()
newTrackEvent = new EventEmitter<{
eventType: TrackEvent;
eventCategory: 'TrackEvent';
eventContent: any;
eventDescription: string;
}>();
2024-07-02 19:19:05 +02:00
@Input()
trackPublication: TrackPublication;
@Input()
localParticipant: LocalParticipant | undefined;
@Input()
index: number;
protected track: Track | undefined;
trackSubscribed: boolean = true;
trackEnabled: boolean = true;
constructor(protected testFeedService: TestFeedService) {}
2024-07-02 19:19:05 +02:00
protected async unpublishTrack() {
await this.localParticipant?.unpublishTrack(this.track as LocalTrack);
}
protected async toggleSubscribeTrack() {
this.trackSubscribed = !this.trackSubscribed;
await (this.trackPublication as RemoteTrackPublication).setSubscribed(
this.trackSubscribed
);
}
2024-07-02 19:19:05 +02:00
protected async toggleEnableTrack() {
this.trackEnabled = !this.trackEnabled;
await (this.trackPublication as RemoteTrackPublication).setEnabled(
this.trackEnabled
);
}
protected setupTrackEventListeners() {
2024-07-02 19:19:05 +02:00
// This is a link to the complete list of Track events
let callbacks: TrackEventCallbacks;
let events: TrackEvent;
this.track
?.on(TrackEvent.Message, () => {
this.newTrackEvent.emit({
eventType: TrackEvent.Message,
eventCategory: 'TrackEvent',
eventContent: {},
eventDescription: this.track!.source,
});
})
.on(TrackEvent.Muted, () => {
this.newTrackEvent.emit({
eventType: TrackEvent.Muted,
eventCategory: 'TrackEvent',
eventContent: {},
eventDescription: this.track!.source,
});
})
.on(TrackEvent.Unmuted, () => {
this.newTrackEvent.emit({
eventType: TrackEvent.Unmuted,
eventCategory: 'TrackEvent',
eventContent: {},
eventDescription: this.track!.source,
});
})
.on(TrackEvent.AudioSilenceDetected, () => {
this.newTrackEvent.emit({
eventType: TrackEvent.AudioSilenceDetected,
eventCategory: 'TrackEvent',
eventContent: {},
eventDescription: this.track!.source,
});
})
.on(TrackEvent.Restarted, () => {
this.newTrackEvent.emit({
eventType: TrackEvent.Restarted,
eventCategory: 'TrackEvent',
eventContent: {},
eventDescription: this.track!.source,
});
})
.on(TrackEvent.Ended, () => {
this.newTrackEvent.emit({
eventType: TrackEvent.Ended,
eventCategory: 'TrackEvent',
eventContent: {},
eventDescription: this.track!.source,
});
})
.on(TrackEvent.VisibilityChanged, (visible: boolean) => {
this.newTrackEvent.emit({
eventType: TrackEvent.VisibilityChanged,
eventCategory: 'TrackEvent',
eventContent: { visible, track: this.track },
eventDescription: `${this.track!.source} is visible: ${visible}`,
});
})
.on(TrackEvent.VideoDimensionsChanged, (dimensions: Track.Dimensions) => {
this.newTrackEvent.emit({
eventType: TrackEvent.VideoDimensionsChanged,
eventCategory: 'TrackEvent',
eventContent: { dimensions, track: this.track },
eventDescription: `${this.track?.source} ${JSON.stringify(
dimensions
)}`,
});
})
.on(TrackEvent.UpstreamPaused, () => {
this.newTrackEvent.emit({
eventType: TrackEvent.UpstreamPaused,
eventCategory: 'TrackEvent',
eventContent: {},
eventDescription: this.track!.source,
});
})
.on(TrackEvent.UpstreamResumed, () => {
this.newTrackEvent.emit({
eventType: TrackEvent.UpstreamResumed,
eventCategory: 'TrackEvent',
eventContent: {},
eventDescription: this.track!.source,
});
});
2024-07-02 19:19:05 +02:00
}
protected getTrackOrigin(): string {
let origin: string;
if (this.track instanceof RemoteTrack) {
origin = 'remote';
} else if (this.track instanceof LocalTrack) {
origin = 'local';
} else {
origin = 'unknown';
}
return origin;
}
updateEventList(event: TestAppEvent) {
this.testFeedService.pushNewEvent({ user: this.index, event });
}
}