all files / src/core/stream/ Remote.js

0% Statements 0/63
0% Branches 0/37
0% Functions 0/16
0% Lines 0/59
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196                                                                                                                                                                                                                                                                                                                                                                                                       
import cache from '../util/cache';
import * as DataSync from '../util/DataSync';
import * as Log from '../util/Log';
import * as Events from '../../definitions/Events';
 
/**
 * A published Stream
 * @public
 */
export default class Remote {
	/**
	 * @access protected
	 * @param {object} values
	 */
	constructor(values) {
		Log.d('Remote~new', values);
		/**
		 * The uid of the room the stream is published in
		 * @type {string}
		 */
		this.roomId = values.roomId;
		/**
		 * The uid of this stream
		 * @type {string}
		 */
		this.uid = values.uid;
		/**
		 * The uid of the publisher of the stream
		 * @type {string}
		 */
		this.from = values.from;
		/**
		 * The type of the stream
		 * @type {string}
		 */
		this.type = values.type;
		/**
		 * @type {string}
		 */
		this.device = values.device;
		/**
		 * @type {string}
		 */
		this.height = values.height;
		/**
		 * @type {string}
		 */
		this.width = values.width;
		/**
		 * The local DOM container element where the {@link Local~media} is displayed
		 * @type {Element}
		 */
		this.container = cache.config.remoteStreamContainer;
		/**
		 * @type {{audio: boolean, video: boolean}}
		 */
		this.muted = Object.assign({audio: false, video: false}, values.muted);
		/**
		 * List of callbacks for mute status change
		 * @type {{MUTE: function[]}}
		 * @private
		 */
		this._callbacks = {};
		/**
		 * PeerConnections associated to this remote stream
		 * @type {PeerConnection}
		 */
		this.peerConnection = null;
	}
 
	/**
	 * DOM element where the MediaStream is displayed
	 * @returns {Element}
	 */
	get node() {
		return this.peerConnection ? this.peerConnection.node : null;
	}
 
	/**
	 * Subscribe to the stream
	 * @param {Element} [remoteStreamContainer] The element the stream is attached to. Can be null if already specified in ReachConfig.
	 * @returns {Promise}
	 */
	subscribe(remoteStreamContainer) {
		if(!cache.user) {
			return Promise.reject(new Error('Only an authenticated user can subscribe to a Room\'s stream.'));
		}
		// TODO: Test if not already subscribed ?
		this.container = remoteStreamContainer || cache.config.remoteStreamContainer;
		Log.d('Remote~subscribe', this.container);
		return cache.peerConnections.answer(this, this.container)
			.then(pc => {this.peerConnection = pc;})
			.then(() => DataSync.update(`_/rooms/${this.roomId}/subscribers/${this.uid}/${cache.device}`, {
				to: cache.user.uid,
				_created: DataSync.ts()
			}))
			.then(() => {
				DataSync.onDisconnect(`_/rooms/${this.roomId}/subscribers/${this.uid}/${cache.device}`).remove();
				let subscribed = false;
				DataSync.on(`_/rooms/${this.roomId}/streams/${this.uid}`, 'value', snapData => {
					const values = snapData.val();
					Log.d('Remote~updated', values);
					if(values) {
						// Update type
						this.type = values.type;
						//update stream size
						const height = values.height;
						const width = values.width;
						if((height || width) && (height !== this.height || width !== this.height)) {
							this.height = values.height;
							this.width = values.width;
							Log.w(this._callbacks[Events.stream.SIZE]);
							(this._callbacks[Events.stream.SIZE] || []).forEach(cb => cb(this.height, this.width));
						}
						// Update mute status
						const muted = values.muted;
						if(muted && (muted.audio !== this.muted.audio || muted.video !== this.muted.video)) {
							this.muted = muted;
							Log.w(this._callbacks[Events.stream.MUTE]);
							(this._callbacks[Events.stream.MUTE] || []).forEach(cb => cb(this.muted));
						}
						subscribed = true;
					} else if(subscribed) {
						Log.i('Remote#removed', this);
						this._close(true);
					}
				});
			})
			.catch(Log.r('Remote~subscribe'));
	}
 
	/**
	 * Unsubscribe from the stream
	 * @returns {Promise}
	 */
	unSubscribe() {
		return this._close(false);
	}
 
	/**
	 * Close the remote Stream
	 * @param {boolean} remote Close is initiated by publisher
	 * @returns {*}
	 * @private
	 */
	_close(remote) {
		// Cancel onDisconnect
		DataSync.onDisconnect(`_/rooms/${this.roomId}/subscribers/${this.uid}/${cache.device}`).cancel();
		// Stop listening to stream modifications
		DataSync.off(`_/rooms/${this.roomId}/streams/${this.uid}`, 'value');
		// Un-subscribe
		!remote && DataSync.remove(`_/rooms/${this.roomId}/subscribers/${this.uid}/${cache.device}`);
		// Close PeerConnection
		return Promise.resolve(cache.peerConnections.close(this.uid, this.device));
	}
 
	/**
	 * Register a callback for a specific event
	 * @param {string} event The event name ({@link Events/Stream})
	 * @param {function} callback The callback for the event
	 */
	on(event, callback) {
		if(Events.stream.supports(event)) {
			if(!this._callbacks[event]) {
				this._callbacks[event] = [];
			}
			this._callbacks[event].push(callback);
		}
	}
 
	/**
	 * Register a callback for a specific event
	 * @param {string} [event] The event name ({@link Events/Stream})
	 * @param {function} [callback] The callback for the event
	 */
	off(event, callback) {
		if(!event) {
			this._callbacks = {};
		} else if(Events.stream.supports(event)) {
			if(!callback) {
				this._callbacks[event] = [];
			} else {
				this._callbacks[event] = this._callbacks[event].filter(cb => cb !== callback);
			}
		}
	}
 
	/**
	 * @access protected
	 * @param {object} values
	 */
	update(values) {
		Object.keys(values).forEach(key => {this[key] = values[key];});
	}
}