Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import Logger from './Logger.js';
import IceCandidate from './misc/RTCIceCandidate.js';
import PeerConnection from './misc/RTCPeerConnection.js';
import SessionDescription from './misc/RTCSessionDescription.js';
const defaultPortalOptions = {
shouldBroadcast: true,
portal: true,
video: false,
audio: true,
};
export default class Portal {
/**
* Creates a video room instance
* @param {*} channel
* @param {*} identity
*
* @return {void} Returns an instance of the Portal
*/
constructor(channel, identity) {
this.channel = channel;
this.logger = new Logger(identity);
this.identity = {...defaultPortalOptions, ...identity};
this.localStream = null;
this.displayStream = null;
this.peerConnectionConfig = {
iceServers: [
{urls: 'stun:stun.stunprotocol.org:3478'},
{urls: 'stun:stun.l.google.com:19302'},
],
};
this.constraints = {
video: identity.video,
audio: identity.audio,
};
this.participants = [];
this.isNegotiating = [];
this.logger.log('Initializing video room');
this.init();
}
/**
* Initialize local video
* @return {void}
*/
init() {
Iif (!this.constraints.video && !this.constraints.audio) {
this.requestPeerVideo();
return;
}
Iif (
typeof navigator != 'undefined' &&
navigator.mediaDevices.getUserMedia
) {
navigator.mediaDevices
.getUserMedia(this.constraints)
.then(this.getUserMediaSuccess.bind(this))
.catch(this.errorHandler.bind(this));
return true;
} else {
this.logger.error('Your browser does not support getUserMedia API');
return false;
}
}
shareVideo(signal, isCaller = true) {
Iif (!this.identity.shouldBroadcast && isCaller && !signal.isBroadcasting) {
console.log('Refusing to call, denied broadcast request');
return;
}
const rtcConnection = new PeerConnection(this.peerConnectionConfig);
rtcConnection.onicecandidate = (event) => {
if (event.candidate != null) {
this.channel.publish('system:portal_candidate', {
from: this.channel.uuid,
to: signal.from,
ice: event.candidate,
});
}
};
rtcConnection.ontrack = (event) => {
if (event.track.kind != 'video') {
return;
}
this.participants[signal.from].streams = event.streams;
if (typeof this.identity.onParticipantJoined == 'function') {
this.identity.onParticipantJoined(signal.from, event.streams[0]);
}
};
rtcConnection.onsignalingstatechange = (e) => {
// Workaround for Chrome: skip nested negotiations
this.isNegotiating[signal.from] =
rtcConnection.signalingState != 'stable';
};
Eif (this.localStream) {
this.localStream.getTracks().forEach((track) => {
rtcConnection.addTrack(track, this.localStream);
});
}
Iif (this.displayStream) {
this.displayStream.getTracks().forEach((track) => {
rtcConnection.addTrack(track, this.displayStream);
});
}
this.isNegotiating[signal.from] = false;
rtcConnection.onnegotiationneeded = async () => {
await this.sendVideoOffer(signal, rtcConnection, isCaller);
};
this.participants[signal.from] = {
rtc: rtcConnection,
};
}
async onRemoteScreenStopped(uuid, streamId) {
if (typeof this.identity.onScreenSharingStopped == 'function') {
this.identity.onScreenSharingStopped(uuid, streamId);
}
}
async onLocalScreen(screenStream) {
// Register stop handler
screenStream.getVideoTracks()[0].addEventListener('ended', () => {
this.channel.publish('system:stopped_screen', {
from: this.channel.uuid,
streamId: screenStream.id,
});
});
// Send it to other peers
this.displayStream = screenStream;
const participants = this.participants;
const participantsIds = Object.keys(participants);
participantsIds.forEach((id) => {
const rtc = participants[id].rtc;
screenStream.getTracks().forEach((track) => {
rtc.addTrack(track, screenStream);
});
});
}
async shareScreen() {
navigator.mediaDevices
.getDisplayMedia()
.then(this.onLocalScreen.bind(this))
.catch(this.errorHandler.bind(this));
}
async sendVideoOffer(signal, rtcConnection, isCaller) {
// if (!isCaller) {
// console.log("Skipped, not the caller");
// return;
// }
if (this.isNegotiating[signal.from]) {
console.log('SKIP nested negotiations');
return;
}
this.isNegotiating[signal.from] = true;
const description = await rtcConnection.createOffer();
await rtcConnection.setLocalDescription(description);
console.log('Making offer');
// Send a call offer
this.channel.publish('system:video_offer', {
from: this.channel.uuid,
to: signal.from,
sdp: rtcConnection.localDescription,
});
}
removeParticipant(uuid) {
delete this.participants[uuid];
Eif (typeof this.identity.onParticipantLeft == 'function') {
this.identity.onParticipantLeft(uuid);
}
}
addIceCandidate(signal) {
const rtcConnection = this.participants[signal.from].rtc;
rtcConnection.addIceCandidate(new IceCandidate(signal.ice));
}
createAnswer(signal) {
return new Promise(async (resolve, reject) => {
Iif (
!this.participants[signal.from] ||
!this.participants[signal.from].rtc
) {
console.log('Starting call in createAnswer');
this.shareVideo(signal, false);
}
await this.participants[signal.from].rtc.setRemoteDescription(
new SessionDescription(signal.sdp),
);
// Only create answers in response to offers
if (signal.sdp.type == 'offer') {
this.logger.log('Got an offer from ' + signal.from, signal);
const description = await this.participants[
signal.from
].rtc.createAnswer();
await this.participants[signal.from].rtc.setLocalDescription(
description,
);
this.channel.publish('system:video_answer', {
from: this.channel.uuid,
to: signal.from,
sdp: this.participants[signal.from].rtc.localDescription,
});
resolve();
} else E{
this.logger.log('Got an asnwer from ' + signal.from);
resolve();
}
});
}
handleAnswer(signal) {
this.participants[signal.from].rtc.setRemoteDescription(
new SessionDescription(signal.sdp),
);
}
/**
* Callback to handle local stream
* @param {*} stream
*/
getUserMediaSuccess(stream) {
this.localStream = stream;
Eif (typeof this.identity.onLocalVideo == 'function') {
this.identity.onLocalVideo(stream, this);
}
this.requestPeerVideo();
}
requestPeerVideo() {
let eventName = 'system:portal_broadcaster';
Iif (!this.identity.shouldBroadcast) {
eventName = 'system:portal_watcher';
}
this.channel.publish(eventName, {
from: this.channel.uuid,
isBroadcasting: this.identity.shouldBroadcast,
});
}
requestOfferFromPeer() {
this.channel.publish('system:video_request', {
from: this.channel.uuid,
isBroadcasting: this.identity.shouldBroadcast,
});
}
errorHandler(e) {
this.logger.error('Portal error', e);
}
}
|