2022-04-28 12:14:42 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { BehaviorSubject, Observable } from 'rxjs';
|
|
|
|
import { BackgroundEffect, EffectType } from '../../models/background-effect.model';
|
2022-11-09 10:35:48 +01:00
|
|
|
import { OpenViduService } from '../openvidu/openvidu.service';
|
2022-04-28 12:14:42 +02:00
|
|
|
import { ParticipantService } from '../participant/participant.service';
|
2022-11-07 11:48:43 +01:00
|
|
|
import { StorageService } from '../storage/storage.service';
|
2022-04-28 12:14:42 +02:00
|
|
|
|
2022-06-13 14:05:48 +02:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-04-28 12:14:42 +02:00
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class VirtualBackgroundService {
|
|
|
|
backgroundSelected = <BehaviorSubject<string>>new BehaviorSubject('');
|
|
|
|
backgroundSelectedObs: Observable<string>;
|
|
|
|
backgrounds: BackgroundEffect[] = [
|
2022-05-13 17:11:04 +02:00
|
|
|
{ id: 'no_effect', type: EffectType.NONE, thumbnail: 'block' },
|
|
|
|
{ id: 'soft_blur', type: EffectType.BLUR, thumbnail: 'blur_on' },
|
2022-04-28 12:14:42 +02:00
|
|
|
{ id: '1', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-1.jpg', src: 'assets/backgrounds/bg-1.jpg' },
|
|
|
|
{ id: '2', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-2.jpg', src: 'assets/backgrounds/bg-2.jpg' },
|
2022-04-29 10:39:48 +02:00
|
|
|
{ id: '3', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-3.jpg', src: 'assets/backgrounds/bg-3.jpg' },
|
|
|
|
{ id: '4', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-4.jpg', src: 'assets/backgrounds/bg-4.jpg' },
|
|
|
|
{ id: '19', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-19.jpg', src: 'assets/backgrounds/bg-19.jpg' },
|
|
|
|
{ id: '5', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-5.jpg', src: 'assets/backgrounds/bg-5.jpg' },
|
|
|
|
{ id: '6', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-6.jpg', src: 'assets/backgrounds/bg-6.jpg' },
|
|
|
|
{ id: '7', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-7.jpg', src: 'assets/backgrounds/bg-7.jpg' },
|
|
|
|
{ id: '8', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-8.jpg', src: 'assets/backgrounds/bg-8.jpg' },
|
|
|
|
{ id: '9', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-9.jpg', src: 'assets/backgrounds/bg-9.jpg' },
|
|
|
|
{ id: '10', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-10.jpg', src: 'assets/backgrounds/bg-10.jpg' },
|
|
|
|
{ id: '11', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-11.jpg', src: 'assets/backgrounds/bg-11.jpg' },
|
|
|
|
{ id: '12', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-12.jpg', src: 'assets/backgrounds/bg-12.jpg' },
|
|
|
|
{ id: '13', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-13.jpg', src: 'assets/backgrounds/bg-13.jpg' },
|
|
|
|
{ id: '14', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-14.jpg', src: 'assets/backgrounds/bg-14.jpg' },
|
|
|
|
{ id: '15', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-15.jpg', src: 'assets/backgrounds/bg-15.jpg' },
|
|
|
|
{ id: '16', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-16.jpg', src: 'assets/backgrounds/bg-16.jpg' },
|
|
|
|
{ id: '17', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-17.jpg', src: 'assets/backgrounds/bg-17.jpg' },
|
|
|
|
{ id: '18', type: EffectType.IMAGE, thumbnail: 'assets/backgrounds/thumbnails/bg-18.jpg', src: 'assets/backgrounds/bg-18.jpg' }
|
2022-04-28 12:14:42 +02:00
|
|
|
];
|
|
|
|
|
2022-11-07 11:48:43 +01:00
|
|
|
constructor(
|
|
|
|
private participantService: ParticipantService,
|
|
|
|
private storageService: StorageService,
|
2022-11-09 10:35:48 +01:00
|
|
|
private openviduService: OpenViduService
|
2022-11-07 11:48:43 +01:00
|
|
|
) {
|
2022-04-28 12:14:42 +02:00
|
|
|
this.backgroundSelectedObs = this.backgroundSelected.asObservable();
|
|
|
|
}
|
|
|
|
|
|
|
|
getBackgrounds(): any[] {
|
|
|
|
return this.backgrounds;
|
|
|
|
}
|
2022-05-05 14:03:41 +02:00
|
|
|
|
2022-05-09 13:20:33 +02:00
|
|
|
isBackgroundApplied(): boolean {
|
|
|
|
const bgSelected = this.backgroundSelected.getValue();
|
|
|
|
return !!bgSelected && bgSelected !== 'no_effect';
|
|
|
|
}
|
|
|
|
|
2022-11-07 11:48:43 +01:00
|
|
|
async applyBackgroundFromStorage() {
|
|
|
|
const bgId = this.storageService.getBackground();
|
|
|
|
if (!!bgId) {
|
|
|
|
const background = this.backgrounds.find((bg) => bg.id === bgId);
|
|
|
|
if (background) {
|
|
|
|
this.applyBackground(background);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async applyBackground(bg: BackgroundEffect) {
|
|
|
|
if (bg.id !== this.backgroundSelected.getValue()) {
|
2022-05-05 14:03:41 +02:00
|
|
|
const filter = this.participantService.getMyCameraPublisher().stream.filter;
|
|
|
|
const isBackgroundSelected = !!filter && filter.type.startsWith('VB:');
|
2022-11-09 10:35:48 +01:00
|
|
|
let options = { token: this.openviduService.getWebcamToken(), url: '' };
|
2022-11-07 11:48:43 +01:00
|
|
|
if (bg.type === EffectType.IMAGE) {
|
|
|
|
options.url = bg.src;
|
2022-04-29 10:39:48 +02:00
|
|
|
}
|
2022-04-28 12:14:42 +02:00
|
|
|
|
2022-11-07 11:48:43 +01:00
|
|
|
if (isBackgroundSelected && this.hasSameTypeAsAbove(bg.type)) {
|
|
|
|
this.replaceBackground(bg);
|
2022-04-29 10:39:48 +02:00
|
|
|
} else {
|
|
|
|
await this.removeBackground();
|
2022-11-07 11:48:43 +01:00
|
|
|
await this.participantService.getMyCameraPublisher().stream.applyFilter(`VB:${bg.type.toLowerCase()}`, options);
|
2022-04-29 10:39:48 +02:00
|
|
|
}
|
2022-11-07 11:48:43 +01:00
|
|
|
this.storageService.setBackground(bg.id);
|
|
|
|
this.backgroundSelected.next(bg.id);
|
2022-04-28 12:14:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeBackground() {
|
2022-05-09 13:20:33 +02:00
|
|
|
if (!!this.isBackgroundApplied()) {
|
2022-04-29 10:39:48 +02:00
|
|
|
this.backgroundSelected.next('no_effect');
|
|
|
|
await this.participantService.getMyCameraPublisher().stream.removeFilter();
|
2022-11-07 11:48:43 +01:00
|
|
|
this.storageService.removeBackground();
|
2022-04-29 10:39:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async replaceBackground(effect: BackgroundEffect) {
|
|
|
|
await this.participantService.getMyCameraPublisher().stream.filter.execMethod('update', { url: effect.src });
|
|
|
|
}
|
|
|
|
|
|
|
|
private hasSameTypeAsAbove(type: EffectType): boolean {
|
|
|
|
const oldEffect = this.backgrounds.find((bg) => bg.id === this.backgroundSelected.getValue());
|
|
|
|
return oldEffect?.type === type;
|
2022-04-28 12:14:42 +02:00
|
|
|
}
|
|
|
|
}
|