mirror of https://github.com/OpenVidu/openvidu.git
openvidu-components: Decoupled replaceTrack from participant service
- Refactored replace track method avoiding to use participant service - Split method in two replaceScreenTrack and replaceCameraTrackpull/809/head
parent
c52f632d7c
commit
197b46f212
|
@ -3,7 +3,6 @@ import { PublisherProperties } from 'openvidu-browser';
|
|||
import { Subscription } from 'rxjs';
|
||||
import { CustomDevice } from '../../../models/device.model';
|
||||
import { ParticipantAbstractModel } from '../../../models/participant.model';
|
||||
import { VideoType } from '../../../models/video-type.model';
|
||||
import { DeviceService } from '../../../services/device/device.service';
|
||||
import { OpenViduService } from '../../../services/openvidu/openvidu.service';
|
||||
import { ParticipantService } from '../../../services/participant/participant.service';
|
||||
|
@ -66,7 +65,8 @@ export class AudioDevicesComponent implements OnInit, OnDestroy {
|
|||
const audioSource = event?.value;
|
||||
if (this.deviceSrv.needUpdateAudioTrack(audioSource)) {
|
||||
const pp: PublisherProperties = { audioSource, videoSource: false };
|
||||
await this.openviduService.replaceTrack(VideoType.CAMERA, pp);
|
||||
const publisher = this.participantService.getMyCameraPublisher();
|
||||
await this.openviduService.replaceCameraTrack(publisher, pp);
|
||||
this.deviceSrv.setMicSelected(audioSource);
|
||||
this.microphoneSelected = this.deviceSrv.getMicrophoneSelected();
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ import { Subscription } from 'rxjs';
|
|||
import { CustomDevice } from '../../../models/device.model';
|
||||
import { PanelType } from '../../../models/panel.model';
|
||||
import { ParticipantAbstractModel } from '../../../models/participant.model';
|
||||
import { VideoType } from '../../../models/video-type.model';
|
||||
import { DeviceService } from '../../../services/device/device.service';
|
||||
import { OpenViduService } from '../../../services/openvidu/openvidu.service';
|
||||
import { PanelService } from '../../../services/panel/panel.service';
|
||||
|
@ -88,7 +87,8 @@ export class VideoDevicesComponent implements OnInit, OnDestroy {
|
|||
await this.backgroundService.removeBackground();
|
||||
}
|
||||
const pp: PublisherProperties = { videoSource: device.device, audioSource: false, mirror };
|
||||
await this.openviduService.replaceTrack(VideoType.CAMERA, pp);
|
||||
const publisher = this.participantService.getMyCameraPublisher();
|
||||
await this.openviduService.replaceCameraTrack(publisher, pp);
|
||||
|
||||
if (isBackgroundApplied) {
|
||||
const bgSelected = this.backgroundService.backgrounds.find((b) => b.id === backgroundSelected);
|
||||
|
|
|
@ -252,7 +252,8 @@ export class StreamComponent implements OnInit {
|
|||
publishAudio: !this.participantService.isMyCameraActive(),
|
||||
mirror: false
|
||||
};
|
||||
await this.openviduService.replaceTrack(VideoType.SCREEN, properties);
|
||||
const publisher = this.participantService.getMyScreenPublisher();
|
||||
await this.openviduService.replaceScreenTrack(publisher, properties);
|
||||
}
|
||||
|
||||
private checkVideoEnlarged() {
|
||||
|
|
|
@ -411,32 +411,40 @@ export class OpenViduService {
|
|||
|
||||
/**
|
||||
* @internal
|
||||
* @param cameraPublisher
|
||||
* @param props
|
||||
*/
|
||||
async replaceTrack(videoType: VideoType, props: PublisherProperties) {
|
||||
const participantService = this.injector.get(ParticipantService);
|
||||
const screenPublisher = participantService.getMyScreenPublisher();
|
||||
const cameraPublisher = participantService.getMyCameraPublisher();
|
||||
|
||||
try {
|
||||
this.log.d(`Replacing ${videoType} track`, props);
|
||||
|
||||
if (videoType === VideoType.CAMERA) {
|
||||
let mediaStream: MediaStream;
|
||||
async replaceCameraTrack(cameraPublisher: Publisher, props: PublisherProperties) {
|
||||
const isReplacingAudio = !!props.audioSource;
|
||||
const isReplacingVideo = !!props.videoSource;
|
||||
let mediaStream: MediaStream | undefined;
|
||||
let track: MediaStreamTrack | undefined;
|
||||
|
||||
try {
|
||||
if (isReplacingVideo || isReplacingAudio) {
|
||||
mediaStream = await this.createMediaStream(props);
|
||||
}
|
||||
|
||||
if (isReplacingVideo) {
|
||||
mediaStream = await this.createMediaStream(props);
|
||||
// Replace video track
|
||||
const videoTrack: MediaStreamTrack = mediaStream.getVideoTracks()[0];
|
||||
await cameraPublisher.replaceTrack(videoTrack);
|
||||
track = mediaStream?.getVideoTracks()[0];
|
||||
} else if (isReplacingAudio) {
|
||||
mediaStream = await this.createMediaStream(props);
|
||||
// Replace audio track
|
||||
const audioTrack: MediaStreamTrack = mediaStream.getAudioTracks()[0];
|
||||
await cameraPublisher.replaceTrack(audioTrack);
|
||||
track = mediaStream?.getAudioTracks()[0];
|
||||
}
|
||||
} else if (videoType === VideoType.SCREEN) {
|
||||
|
||||
if (track) {
|
||||
await cameraPublisher.replaceTrack(track);
|
||||
}
|
||||
} catch (error) {
|
||||
this.log.e('Error replacing track ', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @param screenPublisher
|
||||
* @param props
|
||||
*/
|
||||
async replaceScreenTrack(screenPublisher: Publisher, props: PublisherProperties) {
|
||||
try {
|
||||
let newScreenMediaStream = await this.OVScreen.getUserMedia(props);
|
||||
screenPublisher.stream.getMediaStream().getVideoTracks()[0].stop();
|
||||
|
@ -445,10 +453,6 @@ export class OpenViduService {
|
|||
this.log.w('Cannot create the new MediaStream', error);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
this.log.e('Error replacing track ', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
|
|
Loading…
Reference in New Issue