2018-04-26 15:33:47 +02:00
|
|
|
/*
|
2022-01-13 11:18:47 +01:00
|
|
|
* (C) Copyright 2017-2022 OpenVidu (https://openvidu.io)
|
2018-04-26 15:33:47 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-05-08 13:01:34 +02:00
|
|
|
import { Stream } from './Stream';
|
2018-05-29 18:28:58 +02:00
|
|
|
import { StreamManager } from './StreamManager';
|
2018-04-26 15:33:47 +02:00
|
|
|
import { SubscriberProperties } from '../OpenViduInternal/Interfaces/Public/SubscriberProperties';
|
2020-05-04 20:01:56 +02:00
|
|
|
import { OpenViduLogger } from '../OpenViduInternal/Logger/OpenViduLogger';
|
2018-04-26 15:33:47 +02:00
|
|
|
|
2020-05-04 20:01:56 +02:00
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
|
|
|
const logger: OpenViduLogger = OpenViduLogger.getInstance();
|
2018-04-26 15:33:47 +02:00
|
|
|
|
|
|
|
/**
|
2022-11-22 16:15:07 +01:00
|
|
|
* Packs remote media streams. Participants automatically receive them when others publish their streams. Initialized with {@link Session.subscribe} method
|
2022-08-17 18:04:05 +02:00
|
|
|
*
|
2022-11-22 16:15:07 +01:00
|
|
|
* See available event listeners at {@link StreamManagerEventMap}.
|
2018-04-26 15:33:47 +02:00
|
|
|
*/
|
2018-05-29 18:28:58 +02:00
|
|
|
export class Subscriber extends StreamManager {
|
2019-01-27 14:42:46 +01:00
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
|
|
|
properties: SubscriberProperties;
|
2018-04-26 15:33:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
2022-07-15 10:41:12 +02:00
|
|
|
constructor(stream: Stream, targEl: string | HTMLElement | undefined, properties: SubscriberProperties) {
|
2018-05-23 15:01:40 +02:00
|
|
|
super(stream, targEl);
|
|
|
|
this.element = this.targetElement;
|
2018-04-26 15:33:47 +02:00
|
|
|
this.stream = stream;
|
|
|
|
this.properties = properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe or unsubscribe from the audio stream (if available). Calling this method twice in a row passing same value will have no effect
|
|
|
|
* @param value `true` to subscribe to the audio stream, `false` to unsubscribe from it
|
|
|
|
*/
|
|
|
|
subscribeToAudio(value: boolean): Subscriber {
|
2022-08-17 18:04:05 +02:00
|
|
|
this.stream
|
|
|
|
.getMediaStream()
|
|
|
|
.getAudioTracks()
|
|
|
|
.forEach((track) => {
|
|
|
|
track.enabled = value;
|
|
|
|
});
|
2020-04-21 16:44:14 +02:00
|
|
|
this.stream.audioActive = value;
|
2020-05-04 20:01:56 +02:00
|
|
|
logger.info("'Subscriber' has " + (value ? 'subscribed to' : 'unsubscribed from') + ' its audio stream');
|
2018-04-26 15:33:47 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe or unsubscribe from the video stream (if available). Calling this method twice in a row passing same value will have no effect
|
|
|
|
* @param value `true` to subscribe to the video stream, `false` to unsubscribe from it
|
|
|
|
*/
|
|
|
|
subscribeToVideo(value: boolean): Subscriber {
|
2022-08-17 18:04:05 +02:00
|
|
|
this.stream
|
|
|
|
.getMediaStream()
|
|
|
|
.getVideoTracks()
|
|
|
|
.forEach((track) => {
|
|
|
|
track.enabled = value;
|
|
|
|
});
|
2020-04-21 16:44:14 +02:00
|
|
|
this.stream.videoActive = value;
|
2020-05-04 20:01:56 +02:00
|
|
|
logger.info("'Subscriber' has " + (value ? 'subscribed to' : 'unsubscribed from') + ' its video stream');
|
2018-04-26 15:33:47 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:10:06 +02:00
|
|
|
/* Hidden methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
2022-06-23 13:22:50 +02:00
|
|
|
replaceTrackInMediaStream(track: MediaStreamTrack, updateLastConstraints: boolean): void {
|
2022-04-06 13:10:06 +02:00
|
|
|
const mediaStream: MediaStream = this.stream.getMediaStream();
|
|
|
|
let removedTrack: MediaStreamTrack;
|
|
|
|
if (track.kind === 'video') {
|
|
|
|
removedTrack = mediaStream.getVideoTracks()[0];
|
2022-05-04 19:55:08 +02:00
|
|
|
if (updateLastConstraints) {
|
|
|
|
this.stream.lastVideoTrackConstraints = track.getConstraints();
|
|
|
|
}
|
2022-04-06 13:10:06 +02:00
|
|
|
} else {
|
|
|
|
removedTrack = mediaStream.getAudioTracks()[0];
|
|
|
|
}
|
|
|
|
mediaStream.removeTrack(removedTrack);
|
|
|
|
removedTrack.stop();
|
|
|
|
mediaStream.addTrack(track);
|
|
|
|
}
|
2022-08-17 18:04:05 +02:00
|
|
|
}
|