Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,76 @@
import { getLogger } from '@jitsi/logger';
import { $iq } from 'strophe.js';

import $ from '../../modules/util/XMLParser';
import JitsiConnection from '../../JitsiConnection';
import { MediaType } from '../../service/RTC/MediaType';
import { getSourceNameForJitsiTrack } from '../../service/RTC/SignalingLayer';
import { VideoType } from '../../service/RTC/VideoType';
import JitsiLocalTrack from '../RTC/JitsiLocalTrack';
import JitsiRemoteTrack from '../RTC/JitsiRemoteTrack';
import RTC from '../RTC/RTC';
import $ from '../util/XMLParser';

import ProxyConnectionPC from './ProxyConnectionPC';
import { ACTIONS } from './constants';

const logger = getLogger('proxyconnection:ProxyConnectionService');

/**
* Interface for ProxyConnectionService constructor options
*/
interface IProxyConnectionServiceOptions {
convertVideoToDesktop?: boolean;
jitsiConnection?: JitsiConnection;
onConnectionClosed?: () => void;
onRemoteStream?: (track: JitsiLocalTrack) => void;
onSendMessage?: (peerJid: string, message: { iq: string; }) => void;
pcConfig?: RTCConfiguration;
}

/**
* Interface for internal options with computed values
*/
interface IInternalOptions {
convertVideoToDesktop?: boolean;
onConnectionClosed?: () => void;
onRemoteStream?: (track: JitsiLocalTrack) => void;
onSendMessage?: (peerJid: string, message: { iq: string; }) => void;
pcConfig?: RTCConfiguration;
}

/**
* Interface for message object containing proxy connection information
*/
interface IProxyMessage {
data: {
iq: string;
};
from: string;
}

/**
* Interface for ProxyConnectionPC creation options
*/
interface IProxyConnectionPCOptions {
isInitiator?: boolean;
onError?: (peerJid: string, errorType: string, details?: string) => void;
onRemoteStream?: (track: JitsiRemoteTrack) => void;
onSendMessage?: (peerJid: string, iq: any) => void;
pcConfig?: RTCConfiguration;
peerJid?: string;
receiveAudio?: boolean;
receiveVideo?: boolean;
}

/**
* Instantiates a new ProxyConnectionPC and ensures only one exists at a given
* time. Currently it assumes ProxyConnectionPC is used only for screensharing
* and assumes IQs to be used for communication.
*/
export default class ProxyConnectionService {

private _options: IInternalOptions;
private _peerConnection: Nullable<ProxyConnectionPC>;
/**
* Initializes a new {@code ProxyConnectionService} instance.
*
Expand All @@ -32,7 +85,7 @@ export default class ProxyConnectionService {
* @param {Function} options.onSendMessage - Callback to invoke when a message has to be sent (signaled) out. The
* arguments passed in are the jid to send the message to and the message.
*/
constructor(options = {}) {
constructor(options: IProxyConnectionServiceOptions = {}) {
const {
jitsiConnection,
...otherOptions
Expand All @@ -44,7 +97,7 @@ export default class ProxyConnectionService {
* @type {Object}
*/
this._options = {
pcConfig: jitsiConnection && jitsiConnection.xmpp.connection.jingle.p2pIceConfig,
pcConfig: jitsiConnection?.xmpp.connection.jingle.p2pIceConfig,
...otherOptions
};

Expand Down Expand Up @@ -75,7 +128,7 @@ export default class ProxyConnectionService {
* sending replies.
* @returns {void}
*/
processMessage(message) {
private _processMessage(message: IProxyMessage): void {
const peerJid = message.from;

if (!peerJid) {
Expand All @@ -97,8 +150,8 @@ export default class ProxyConnectionService {
}

const iq = this._convertStringToXML(message.data.iq);
const $jingle = iq && iq.find('jingle');
const action = $jingle && $jingle.attr('action');
const $jingle = iq?.find('jingle');
const action = $jingle?.attr('action');

if (action === ACTIONS.INITIATE) {
this._peerConnection = this._createPeerConnection(peerJid, {
Expand Down Expand Up @@ -133,7 +186,7 @@ export default class ProxyConnectionService {
* send through to the peer.
* @returns {void}
*/
start(peerJid, localTracks = []) {
private _start(peerJid: string, localTracks: JitsiLocalTrack[] = []): void {
this._peerConnection = this._createPeerConnection(peerJid, {
isInitiator: true,
receiveVideo: false
Expand All @@ -153,7 +206,7 @@ export default class ProxyConnectionService {
*
* @returns {void}
*/
stop() {
private _stop(): void {
if (this._peerConnection) {
this._peerConnection.stop();
}
Expand All @@ -169,7 +222,7 @@ export default class ProxyConnectionService {
* @returns {Object|null} An element version of the xml. Null will be returned
* if an error is encountered during transformation.
*/
_convertStringToXML(xml) {
private _convertStringToXML(xml: string): Nullable<ReturnType<typeof $>> {
try {
const xmlDom = new DOMParser().parseFromString(xml, 'text/xml');

Expand All @@ -192,7 +245,7 @@ export default class ProxyConnectionService {
* @private
* @returns {ProxyConnectionPC}
*/
_createPeerConnection(peerJid, options = {}) {
private _createPeerConnection(peerJid: string, options: Partial<IProxyConnectionPCOptions> = {}): ProxyConnectionPC {
if (!peerJid) {
throw new Error('Cannot create ProxyConnectionPC without a peer.');
}
Expand Down Expand Up @@ -223,7 +276,7 @@ export default class ProxyConnectionService {
* @private
* @returns {void}
*/
_onFatalError(peerJid, errorType, details = '') {
private _onFatalError(peerJid: string, errorType: string, details: string = ''): void {
logger.error(
'Received a proxy connection error', peerJid, errorType, details);

Expand Down Expand Up @@ -258,7 +311,7 @@ export default class ProxyConnectionService {
* @private
* @returns {void}
*/
_onRemoteStream(jitsiRemoteTrack) {
private _onRemoteStream(jitsiRemoteTrack: JitsiRemoteTrack): void {
if (!this._options.onRemoteStream) {
logger.error('Remote track received without callback.');
jitsiRemoteTrack.dispose();
Expand All @@ -267,7 +320,7 @@ export default class ProxyConnectionService {
}

const isVideo = jitsiRemoteTrack.isVideoTrack();
let videoType;
let videoType: Optional<VideoType>;

if (isVideo) {
videoType = this._options.convertVideoToDesktop
Expand Down Expand Up @@ -301,7 +354,7 @@ export default class ProxyConnectionService {
* @private
* @returns {void}
*/
_onSendMessage(peerJid, iq) {
private _onSendMessage(peerJid: string, iq: any): void {
if (!this._options.onSendMessage) {
return;
}
Expand All @@ -322,8 +375,8 @@ export default class ProxyConnectionService {
* @private
* @returns {void}
*/
_selfCloseConnection() {
this.stop();
private _selfCloseConnection(): void {
this._stop();

this._options.onConnectionClosed
&& this._options.onConnectionClosed();
Expand Down