// List of sessions Janus.sessions = {}; Janus.isExtensionEnabled = function() { if(navigator.getDisplayMedia) { // No need for the extension, getDisplayMedia is supported return true; } if(window.navigator.userAgent.match('Chrome')) { var chromever = parseInt(window.navigator.userAgent.match(/Chrome\/(.*) /)[1], 10); var maxver = 33; if(window.navigator.userAgent.match('Linux')) maxver = 35; // "known" crash in chrome 34 and 35 on linux if(chromever >= 26 && chromever <= maxver) { // Older versions of Chrome don't support this extension-based approach, so lie return true; } return Janus.extension.isInstalled(); } else { // Firefox of others, no need for the extension (but this doesn't mean it will work) return true; } }; var defaultExtension = { // Screensharing Chrome Extension ID extensionId: 'hapfgfdkleiggjjpfpenajgdnfckjpaj', isInstalled: function() { return document.querySelector('#janus-extension-installed') !== null; }, getScreen: function (callback) { var pending = window.setTimeout(function () { error = new Error('NavigatorUserMediaError'); error.name = 'The required Chrome extension is not installed: click here to install it. (NOTE: this will need you to refresh the page)'; return callback(error); }, 1000); this.cache[pending] = callback; window.postMessage({ type: 'janusGetScreen', id: pending }, '*'); }, init: function () { var cache = {}; this.cache = cache; // Wait for events from the Chrome Extension window.addEventListener('message', function (event) { if(event.origin != window.location.origin) return; if(event.data.type == 'janusGotScreen' && cache[event.data.id]) { var callback = cache[event.data.id]; delete cache[event.data.id]; if (event.data.sourceId === '') { // user canceled var error = new Error('NavigatorUserMediaError'); error.name = 'You cancelled the request for permission, giving up...'; callback(error); } else { callback(null, event.data.sourceId); } } else if (event.data.type == 'janusGetScreenPending') { console.log('clearing ', event.data.id); window.clearTimeout(event.data.id); } }); } }; Janus.useDefaultDependencies = function (deps) { var f = (deps && deps.fetch) || fetch; var p = (deps && deps.Promise) || Promise; var socketCls = (deps && deps.WebSocket) || WebSocket; return { newWebSocket: function(server, proto) { return new socketCls(server, proto); }, extension: (deps && deps.extension) || defaultExtension, isArray: function(arr) { return Array.isArray(arr); }, webRTCAdapter: (deps && deps.adapter) || adapter, httpAPICall: function(url, options) { var fetchOptions = { method: options.verb, headers: { 'Accept': 'application/json, text/plain, */*' }, cache: 'no-cache' }; if(options.verb === "POST") { fetchOptions.headers['Content-Type'] = 'application/json'; } if(options.withCredentials !== undefined) { fetchOptions.credentials = options.withCredentials === true ? 'include' : (options.withCredentials ? options.withCredentials : 'omit'); } if(options.body !== undefined) { fetchOptions.body = JSON.stringify(options.body); } var fetching = f(url, fetchOptions).catch(function(error) { return p.reject({message: 'Probably a network error, is the server down?', error: error}); }); /* * fetch() does not natively support timeouts. * Work around this by starting a timeout manually, and racing it agains the fetch() to see which thing resolves first. */ if(options.timeout !== undefined) { var timeout = new p(function(resolve, reject) { var timerId = setTimeout(function() { clearTimeout(timerId); return reject({message: 'Request timed out', timeout: options.timeout}); }, options.timeout); }); fetching = p.race([fetching,timeout]); } fetching.then(function(response) { if(response.ok) { if(typeof(options.success) === typeof(Janus.noop)) { return response.json().then(function(parsed) { options.success(parsed); }).catch(function(error) { return p.reject({message: 'Failed to parse response body', error: error, response: response}); }); } } else { return p.reject({message: 'API call failed', response: response}); } }).catch(function(error) { if(typeof(options.error) === typeof(Janus.noop)) { options.error(error.message || '<< internal error >>', error); } }); return fetching; } } }; Janus.useOldDependencies = function (deps) { var jq = (deps && deps.jQuery) || jQuery; var socketCls = (deps && deps.WebSocket) || WebSocket; return { newWebSocket: function(server, proto) { return new socketCls(server, proto); }, isArray: function(arr) { return jq.isArray(arr); }, extension: (deps && deps.extension) || defaultExtension, webRTCAdapter: (deps && deps.adapter) || adapter, httpAPICall: function(url, options) { var payload = options.body !== undefined ? { contentType: 'application/json', data: JSON.stringify(options.body) } : {}; var credentials = options.withCredentials !== undefined ? {xhrFields: {withCredentials: options.withCredentials}} : {}; return jq.ajax(jq.extend(payload, credentials, { url: url, type: options.verb, cache: false, dataType: 'json', async: options.async, timeout: options.timeout, success: function(result) { if(typeof(options.success) === typeof(Janus.noop)) { options.success(result); } }, error: function(xhr, status, err) { if(typeof(options.error) === typeof(Janus.noop)) { options.error(status, err); } } })); }, }; }; Janus.noop = function() {}; // Initialization Janus.init = function(options) { options = options || {}; options.callback = (typeof options.callback == "function") ? options.callback : Janus.noop; if(Janus.initDone === true) { // Already initialized options.callback(); } else { if(typeof console == "undefined" || typeof console.log == "undefined") console = { log: function() {} }; // Console logging (all debugging disabled by default) Janus.trace = Janus.noop; Janus.debug = Janus.noop; Janus.vdebug = Janus.noop; Janus.log = Janus.noop; Janus.warn = Janus.noop; Janus.error = Janus.noop; if(options.debug === true || options.debug === "all") { // Enable all debugging levels Janus.trace = console.trace.bind(console); Janus.debug = console.debug.bind(console); Janus.vdebug = console.debug.bind(console); Janus.log = console.log.bind(console); Janus.warn = console.warn.bind(console); Janus.error = console.error.bind(console); } else if(Array.isArray(options.debug)) { for(var i in options.debug) { var d = options.debug[i]; switch(d) { case "trace": Janus.trace = console.trace.bind(console); break; case "debug": Janus.debug = console.debug.bind(console); break; case "vdebug": Janus.vdebug = console.debug.bind(console); break; case "log": Janus.log = console.log.bind(console); break; case "warn": Janus.warn = console.warn.bind(console); break; case "error": Janus.error = console.error.bind(console); break; default: console.error("Unknown debugging option '" + d + "' (supported: 'trace', 'debug', 'vdebug', 'log', warn', 'error')"); break; } } } Janus.log("Initializing library"); var usedDependencies = options.dependencies || Janus.useDefaultDependencies(); Janus.isArray = usedDependencies.isArray; Janus.webRTCAdapter = usedDependencies.webRTCAdapter; Janus.httpAPICall = usedDependencies.httpAPICall; Janus.newWebSocket = usedDependencies.newWebSocket; Janus.extension = usedDependencies.extension; Janus.extension.init(); // Helper method to enumerate devices Janus.listDevices = function(callback, config) { callback = (typeof callback == "function") ? callback : Janus.noop; if (config == null) config = { audio: true, video: true }; if(navigator.mediaDevices) { navigator.mediaDevices.getUserMedia(config) .then(function(stream) { navigator.mediaDevices.enumerateDevices().then(function(devices) { Janus.debug(devices); callback(devices); // Get rid of the now useless stream try { var tracks = stream.getTracks(); for(var i in tracks) { var mst = tracks[i]; if(mst !== null && mst !== undefined) mst.stop(); } } catch(e) {} }); }) .catch(function(err) { Janus.error(err); callback([]); }); } else { Janus.warn("navigator.mediaDevices unavailable"); callback([]); } } // Helper methods to attach/reattach a stream to a video element (previously part of adapter.js) Janus.attachMediaStream = function(element, stream) { if(Janus.webRTCAdapter.browserDetails.browser === 'chrome') { var chromever = Janus.webRTCAdapter.browserDetails.version; if(chromever >= 52) { element.srcObject = stream; } else if(typeof element.src !== 'undefined') { element.src = URL.createObjectURL(stream); } else { Janus.error("Error attaching stream to element"); } } else { element.srcObject = stream; } }; Janus.reattachMediaStream = function(to, from) { if(Janus.webRTCAdapter.browserDetails.browser === 'chrome') { var chromever = Janus.webRTCAdapter.browserDetails.version; if(chromever >= 52) { to.srcObject = from.srcObject; } else if(typeof to.src !== 'undefined') { to.src = from.src; } else { Janus.error("Error reattaching stream to element"); } } else { to.srcObject = from.srcObject; } }; // Detect tab close: make sure we don't loose existing onbeforeunload handlers // (note: for iOS we need to subscribe to a different event, 'pagehide', see // https://gist.github.com/thehunmonkgroup/6bee8941a49b86be31a787fe8f4b8cfe) var iOS = ['iPad', 'iPhone', 'iPod'].indexOf(navigator.platform) >= 0; var eventName = iOS ? 'pagehide' : 'beforeunload'; var oldOBF = window["on" + eventName]; window.addEventListener(eventName, function(event) { Janus.log("Closing window"); for(var s in Janus.sessions) { if(Janus.sessions[s] !== null && Janus.sessions[s] !== undefined && Janus.sessions[s].destroyOnUnload) { Janus.log("Destroying session " + s); Janus.sessions[s].destroy({asyncRequest: false, notifyDestroyed: false}); } } if(oldOBF && typeof oldOBF == "function") oldOBF(); }); Janus.initDone = true; options.callback(); } }; // Helper method to check whether WebRTC is supported by this browser Janus.isWebrtcSupported = function() { return window.RTCPeerConnection !== undefined && window.RTCPeerConnection !== null && navigator.getUserMedia !== undefined && navigator.getUserMedia !== null; }; // Helper method to create random identifiers (e.g., transaction) Janus.randomString = function(len) { var charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var randomString = ''; for (var i = 0; i < len; i++) { var randomPoz = Math.floor(Math.random() * charSet.length); randomString += charSet.substring(randomPoz,randomPoz+1); } return randomString; } function Janus(gatewayCallbacks) { if(Janus.initDone === undefined) { gatewayCallbacks.error("Library not initialized"); return {}; } if(!Janus.isWebrtcSupported()) { gatewayCallbacks.error("WebRTC not supported by this browser"); return {}; } Janus.log("Library initialized: " + Janus.initDone); gatewayCallbacks = gatewayCallbacks || {}; gatewayCallbacks.success = (typeof gatewayCallbacks.success == "function") ? gatewayCallbacks.success : Janus.noop; gatewayCallbacks.error = (typeof gatewayCallbacks.error == "function") ? gatewayCallbacks.error : Janus.noop; gatewayCallbacks.destroyed = (typeof gatewayCallbacks.destroyed == "function") ? gatewayCallbacks.destroyed : Janus.noop; if(gatewayCallbacks.server === null || gatewayCallbacks.server === undefined) { gatewayCallbacks.error("Invalid server url"); return {}; } var websockets = false; var ws = null; var wsHandlers = {}; var wsKeepaliveTimeoutId = null; var servers = null, serversIndex = 0; var server = gatewayCallbacks.server; if(Janus.isArray(server)) { Janus.log("Multiple servers provided (" + server.length + "), will use the first that works"); server = null; servers = gatewayCallbacks.server; Janus.debug(servers); } else { if(server.indexOf("ws") === 0) { websockets = true; Janus.log("Using WebSockets to contact Janus: " + server); } else { websockets = false; Janus.log("Using REST API to contact Janus: " + server); } } var iceServers = gatewayCallbacks.iceServers; if(iceServers === undefined || iceServers === null) iceServers = [{urls: "stun:stun.l.google.com:19302"}]; var iceTransportPolicy = gatewayCallbacks.iceTransportPolicy; var bundlePolicy = gatewayCallbacks.bundlePolicy; // Whether IPv6 candidates should be gathered var ipv6Support = gatewayCallbacks.ipv6; if(ipv6Support === undefined || ipv6Support === null) ipv6Support = false; // Whether we should enable the withCredentials flag for XHR requests var withCredentials = false; if(gatewayCallbacks.withCredentials !== undefined && gatewayCallbacks.withCredentials !== null) withCredentials = gatewayCallbacks.withCredentials === true; // Optional max events var maxev = null; if(gatewayCallbacks.max_poll_events !== undefined && gatewayCallbacks.max_poll_events !== null) maxev = gatewayCallbacks.max_poll_events; if(maxev < 1) maxev = 1; // Token to use (only if the token based authentication mechanism is enabled) var token = null; if(gatewayCallbacks.token !== undefined && gatewayCallbacks.token !== null) token = gatewayCallbacks.token; // API secret to use (only if the shared API secret is enabled) var apisecret = null; if(gatewayCallbacks.apisecret !== undefined && gatewayCallbacks.apisecret !== null) apisecret = gatewayCallbacks.apisecret; // Whether we should destroy this session when onbeforeunload is called this.destroyOnUnload = true; if(gatewayCallbacks.destroyOnUnload !== undefined && gatewayCallbacks.destroyOnUnload !== null) this.destroyOnUnload = (gatewayCallbacks.destroyOnUnload === true); // Some timeout-related values var keepAlivePeriod = 25000; if(gatewayCallbacks.keepAlivePeriod !== undefined && gatewayCallbacks.keepAlivePeriod !== null) keepAlivePeriod = gatewayCallbacks.keepAlivePeriod; if(isNaN(keepAlivePeriod)) keepAlivePeriod = 25000; var longPollTimeout = 60000; if(gatewayCallbacks.longPollTimeout !== undefined && gatewayCallbacks.longPollTimeout !== null) longPollTimeout = gatewayCallbacks.longPollTimeout; if(isNaN(longPollTimeout)) longPollTimeout = 60000; var connected = false; var sessionId = null; var pluginHandles = {}; var that = this; var retries = 0; var transactions = {}; createSession(gatewayCallbacks); // Public methods this.getServer = function() { return server; }; this.isConnected = function() { return connected; }; this.reconnect = function(callbacks) { callbacks = callbacks || {}; callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop; callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : Janus.noop; callbacks["reconnect"] = true; createSession(callbacks); }; this.getSessionId = function() { return sessionId; }; this.destroy = function(callbacks) { destroySession(callbacks); }; this.attach = function(callbacks) { createHandle(callbacks); }; function eventHandler() { if(sessionId == null) return; Janus.debug('Long poll...'); if(!connected) { Janus.warn("Is the server down? (connected=false)"); return; } var longpoll = server + "/" + sessionId + "?rid=" + new Date().getTime(); if(maxev !== undefined && maxev !== null) longpoll = longpoll + "&maxev=" + maxev; if(token !== null && token !== undefined) longpoll = longpoll + "&token=" + encodeURIComponent(token); if(apisecret !== null && apisecret !== undefined) longpoll = longpoll + "&apisecret=" + encodeURIComponent(apisecret); Janus.httpAPICall(longpoll, { verb: 'GET', withCredentials: withCredentials, success: handleEvent, timeout: longPollTimeout, error: function(textStatus, errorThrown) { Janus.error(textStatus + ":", errorThrown); retries++; if(retries > 3) { // Did we just lose the server? :-( connected = false; gatewayCallbacks.error("Lost connection to the server (is it down?)"); return; } eventHandler(); } }); } // Private event handler: this will trigger plugin callbacks, if set function handleEvent(json, skipTimeout) { retries = 0; if(!websockets && sessionId !== undefined && sessionId !== null && skipTimeout !== true) setTimeout(eventHandler, 200); if(!websockets && Janus.isArray(json)) { // We got an array: it means we passed a maxev > 1, iterate on all objects for(var i=0; i= 59) { // Firefox >= 59 uses Transceivers Janus.log((media.replaceVideo ? "Replacing" : "Adding") + " video track:", stream.getVideoTracks()[0]); var audioTransceiver = null; var transceivers = config.pc.getTransceivers(); if(transceivers && transceivers.length > 0) { for(var i in transceivers) { var t = transceivers[i]; if((t.sender && t.sender.track && t.sender.track.kind === "audio") || (t.receiver && t.receiver.track && t.receiver.track.kind === "audio")) { audioTransceiver = t; break; } } } if(audioTransceiver && audioTransceiver.sender) { audioTransceiver.sender.replaceTrack(stream.getVideoTracks()[0]); } else { config.pc.addTrack(stream.getVideoTracks()[0], stream); } } else { Janus.log((media.replaceAudio ? "Replacing" : "Adding") + " audio track:", stream.getAudioTracks()[0]); config.pc.addTrack(stream.getAudioTracks()[0], stream); } } } if(((!media.update && isVideoSendEnabled(media)) || (media.update && (media.addVideo || media.replaceVideo))) && stream.getVideoTracks() && stream.getVideoTracks().length) { config.myStream.addTrack(stream.getVideoTracks()[0]); if(media.replaceVideo && Janus.webRTCAdapter.browserDetails.browser === "firefox") { Janus.log("Replacing video track:", stream.getVideoTracks()[0]); for(var index in config.pc.getSenders()) { var s = config.pc.getSenders()[index]; if(s && s.track && s.track.kind === "video") { s.replaceTrack(stream.getVideoTracks()[0]); } } } else { if(Janus.webRTCAdapter.browserDetails.browser === "firefox" && Janus.webRTCAdapter.browserDetails.version >= 59) { // Firefox >= 59 uses Transceivers Janus.log((media.replaceVideo ? "Replacing" : "Adding") + " video track:", stream.getVideoTracks()[0]); var videoTransceiver = null; var transceivers = config.pc.getTransceivers(); if(transceivers && transceivers.length > 0) { for(var i in transceivers) { var t = transceivers[i]; if((t.sender && t.sender.track && t.sender.track.kind === "video") || (t.receiver && t.receiver.track && t.receiver.track.kind === "video")) { videoTransceiver = t; break; } } } if(videoTransceiver && videoTransceiver.sender) { videoTransceiver.sender.replaceTrack(stream.getVideoTracks()[0]); } else { config.pc.addTrack(stream.getVideoTracks()[0], stream); } } else { Janus.log((media.replaceVideo ? "Replacing" : "Adding") + " video track:", stream.getVideoTracks()[0]); config.pc.addTrack(stream.getVideoTracks()[0], stream); } } } } // If we still need to create a PeerConnection, let's do that if(!config.pc) { var pc_config = {"iceServers": iceServers, "iceTransportPolicy": iceTransportPolicy, "bundlePolicy": bundlePolicy}; //~ var pc_constraints = {'mandatory': {'MozDontOfferDataChannel':true}}; var pc_constraints = { "optional": [{"DtlsSrtpKeyAgreement": true}] }; if(ipv6Support === true) { pc_constraints.optional.push({"googIPv6":true}); } // Any custom constraint to add? if(callbacks.rtcConstraints && typeof callbacks.rtcConstraints === 'object') { Janus.debug("Adding custom PeerConnection constraints:", callbacks.rtcConstraints); for(var i in callbacks.rtcConstraints) { pc_constraints.optional.push(callbacks.rtcConstraints[i]); } } if(Janus.webRTCAdapter.browserDetails.browser === "edge") { // This is Edge, enable BUNDLE explicitly pc_config.bundlePolicy = "max-bundle"; } Janus.log("Creating PeerConnection"); Janus.debug(pc_constraints); config.pc = new RTCPeerConnection(pc_config, pc_constraints); Janus.debug(config.pc); if(config.pc.getStats) { // FIXME config.volume = {}; config.bitrate.value = "0 kbits/sec"; } Janus.log("Preparing local SDP and gathering candidates (trickle=" + config.trickle + ")"); config.pc.oniceconnectionstatechange = function(e) { if(config.pc) pluginHandle.iceState(config.pc.iceConnectionState); }; config.pc.onicecandidate = function(event) { if (event.candidate == null || (Janus.webRTCAdapter.browserDetails.browser === 'edge' && event.candidate.candidate.indexOf('endOfCandidates') > 0)) { Janus.log("End of candidates."); config.iceDone = true; if(config.trickle === true) { // Notify end of candidates sendTrickleCandidate(handleId, {"completed": true}); } else { // No trickle, time to send the complete SDP (including all candidates) sendSDP(handleId, callbacks); } } else { // JSON.stringify doesn't work on some WebRTC objects anymore // See https://code.google.com/p/chromium/issues/detail?id=467366 var candidate = { "candidate": event.candidate.candidate, "sdpMid": event.candidate.sdpMid, "sdpMLineIndex": event.candidate.sdpMLineIndex }; if(config.trickle === true) { // Send candidate sendTrickleCandidate(handleId, candidate); } } }; config.pc.ontrack = function(event) { Janus.log("Handling Remote Track"); Janus.debug(event); if(!event.streams) return; config.remoteStream = event.streams[0]; pluginHandle.onremotestream(config.remoteStream); if(event.track && !event.track.onended) { Janus.log("Adding onended callback to track:", event.track); event.track.onended = function(ev) { Janus.log("Remote track removed:", ev); if(config.remoteStream) { config.remoteStream.removeTrack(ev.target); pluginHandle.onremotestream(config.remoteStream); } } } }; } if(addTracks && stream !== null && stream !== undefined) { Janus.log('Adding local stream'); stream.getTracks().forEach(function(track) { Janus.log('Adding local track:', track); config.pc.addTrack(track, stream); }); } // Any data channel to create? if(isDataEnabled(media) && !config.dataChannel) { Janus.log("Creating data channel"); var onDataChannelMessage = function(event) { Janus.log('Received message on data channel: ' + event.data); pluginHandle.ondata(event.data); // FIXME } var onDataChannelStateChange = function() { var dcState = config.dataChannel !== null ? config.dataChannel.readyState : "null"; Janus.log('State change on data channel: ' + dcState); if(dcState === 'open') { pluginHandle.ondataopen(); // FIXME } } var onDataChannelError = function(error) { Janus.error('Got error on data channel:', error); // TODO } // Until we implement the proxying of open requests within the Janus core, we open a channel ourselves whatever the case config.dataChannel = config.pc.createDataChannel("JanusDataChannel", {ordered:false}); // FIXME Add options (ordered, maxRetransmits, etc.) config.dataChannel.onmessage = onDataChannelMessage; config.dataChannel.onopen = onDataChannelStateChange; config.dataChannel.onclose = onDataChannelStateChange; config.dataChannel.onerror = onDataChannelError; } // If there's a new local stream, let's notify the application if(config.myStream) pluginHandle.onlocalstream(config.myStream); // Create offer/answer now if(jsep === null || jsep === undefined) { createOffer(handleId, media, callbacks); } else { config.pc.setRemoteDescription(jsep) .then(function() { Janus.log("Remote description accepted!"); config.remoteSdp = jsep.sdp; // Any trickle candidate we cached? if(config.candidates && config.candidates.length > 0) { for(var i in config.candidates) { var candidate = config.candidates[i]; Janus.debug("Adding remote candidate:", candidate); if(!candidate || candidate.completed === true) { // end-of-candidates config.pc.addIceCandidate(); } else { // New candidate config.pc.addIceCandidate(candidate); } } config.candidates = []; } // Create the answer now createAnswer(handleId, media, callbacks); }, callbacks.error); } } function prepareWebrtc(handleId, callbacks) { callbacks = callbacks || {}; callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop; callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : webrtcError; var jsep = callbacks.jsep; callbacks.media = callbacks.media || { audio: true, video: true }; var media = callbacks.media; var pluginHandle = pluginHandles[handleId]; if(pluginHandle === null || pluginHandle === undefined || pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) { Janus.warn("Invalid handle"); callbacks.error("Invalid handle"); return; } var config = pluginHandle.webrtcStuff; config.trickle = isTrickleEnabled(callbacks.trickle); // Are we updating a session? if(config.pc === undefined || config.pc === null) { // Nope, new PeerConnection media.update = false; media.keepAudio = false; media.keepVideo = false; } else if(config.pc !== undefined && config.pc !== null) { Janus.log("Updating existing media session"); media.update = true; // Check if there's anything to add/remove/replace, or if we // can go directly to preparing the new SDP offer or answer if(callbacks.stream !== null && callbacks.stream !== undefined) { // External stream: is this the same as the one we were using before? if(callbacks.stream !== config.myStream) { Janus.log("Renegotiation involves a new external stream"); } } else { // Check if there are changes on audio if(media.addAudio) { media.keepAudio = false; media.replaceAudio = false; media.removeAudio = false; media.audioSend = true; if(config.myStream && config.myStream.getAudioTracks() && config.myStream.getAudioTracks().length) { Janus.error("Can't add audio stream, there already is one"); callbacks.error("Can't add audio stream, there already is one"); return; } } else if(media.removeAudio) { media.keepAudio = false; media.replaceAudio = false; media.addAudio = false; media.audioSend = false; } else if(media.replaceAudio) { media.keepAudio = false; media.addAudio = false; media.removeAudio = false; media.audioSend = true; } if(config.myStream === null || config.myStream === undefined) { // No media stream: if we were asked to replace, it's actually an "add" if(media.replaceAudio) { media.keepAudio = false; media.replaceAudio = false; media.addAudio = true; media.audioSend = true; } if(isAudioSendEnabled(media)) { media.keepAudio = false; media.addAudio = true; } } else { if(config.myStream.getAudioTracks() === null || config.myStream.getAudioTracks() === undefined || config.myStream.getAudioTracks().length === 0) { // No audio track: if we were asked to replace, it's actually an "add" if(media.replaceAudio) { media.keepAudio = false; media.replaceAudio = false; media.addAudio = true; media.audioSend = true; } if(isAudioSendEnabled(media)) { media.keepVideo = false; media.addAudio = true; } } else { // We have an audio track: should we keep it as it is? if(isAudioSendEnabled(media) && !media.removeAudio && !media.replaceAudio) { media.keepAudio = true; } } } // Check if there are changes on video if(media.addVideo) { media.keepVideo = false; media.replaceVideo = false; media.removeVideo = false; media.videoSend = true; if(config.myStream && config.myStream.getVideoTracks() && config.myStream.getVideoTracks().length) { Janus.error("Can't add video stream, there already is one"); callbacks.error("Can't add video stream, there already is one"); return; } } else if(media.removeVideo) { media.keepVideo = false; media.replaceVideo = false; media.addVideo = false; media.videoSend = false; } else if(media.replaceVideo) { media.keepVideo = false; media.addVideo = false; media.removeVideo = false; media.videoSend = true; } if(config.myStream === null || config.myStream === undefined) { // No media stream: if we were asked to replace, it's actually an "add" if(media.replaceVideo) { media.keepVideo = false; media.replaceVideo = false; media.addVideo = true; media.videoSend = true; } if(isVideoSendEnabled(media)) { media.keepVideo = false; media.addVideo = true; } } else { if(config.myStream.getVideoTracks() === null || config.myStream.getVideoTracks() === undefined || config.myStream.getVideoTracks().length === 0) { // No video track: if we were asked to replace, it's actually an "add" if(media.replaceVideo) { media.keepVideo = false; media.replaceVideo = false; media.addVideo = true; media.videoSend = true; } if(isVideoSendEnabled(media)) { media.keepVideo = false; media.addVideo = true; } } else { // We have a video track: should we keep it as it is? if(isVideoSendEnabled(media) && !media.removeVideo && !media.replaceVideo) { media.keepVideo = true; } } } // Data channels can only be added if(media.addData) media.data = true; } // If we're updating and keeping all tracks, let's skip the getUserMedia part if((isAudioSendEnabled(media) && media.keepAudio) && (isVideoSendEnabled(media) && media.keepVideo)) { streamsDone(handleId, jsep, media, callbacks, config.myStream); return; } } // If we're updating, check if we need to remove/replace one of the tracks if(media.update && !config.streamExternal) { if(media.removeAudio || media.replaceAudio) { if(config.myStream && config.myStream.getAudioTracks() && config.myStream.getAudioTracks().length) { var s = config.myStream.getAudioTracks()[0]; Janus.log("Removing audio track:", s); config.myStream.removeTrack(s); try { s.stop(); } catch(e) {}; } if(config.pc.getSenders() && config.pc.getSenders().length) { var ra = true; if(media.replaceAudio && Janus.webRTCAdapter.browserDetails.browser === "firefox") { // On Firefox we can use replaceTrack ra = false; } if(ra) { for(var index in config.pc.getSenders()) { var s = config.pc.getSenders()[index]; if(s && s.track && s.track.kind === "audio") { Janus.log("Removing audio sender:", s); config.pc.removeTrack(s); } } } } } if(media.removeVideo || media.replaceVideo) { if(config.myStream && config.myStream.getVideoTracks() && config.myStream.getVideoTracks().length) { var s = config.myStream.getVideoTracks()[0]; Janus.log("Removing video track:", s); config.myStream.removeTrack(s); try { s.stop(); } catch(e) {}; } if(config.pc.getSenders() && config.pc.getSenders().length) { var rv = true; if(media.replaceVideo && Janus.webRTCAdapter.browserDetails.browser === "firefox") { // On Firefox we can use replaceTrack rv = false; } if(rv) { for(var index in config.pc.getSenders()) { var s = config.pc.getSenders()[index]; if(s && s.track && s.track.kind === "video") { Janus.log("Removing video sender:", s); config.pc.removeTrack(s); } } } } } } // Was a MediaStream object passed, or do we need to take care of that? if(callbacks.stream !== null && callbacks.stream !== undefined) { var stream = callbacks.stream; Janus.log("MediaStream provided by the application"); Janus.debug(stream); // If this is an update, let's check if we need to release the previous stream if(media.update) { if(config.myStream && config.myStream !== callbacks.stream && !config.streamExternal) { // We're replacing a stream we captured ourselves with an external one try { // Try a MediaStreamTrack.stop() for each track var tracks = config.myStream.getTracks(); for(var i in tracks) { var mst = tracks[i]; Janus.log(mst); if(mst !== null && mst !== undefined) mst.stop(); } } catch(e) { // Do nothing if this fails } config.myStream = null; } } // Skip the getUserMedia part config.streamExternal = true; streamsDone(handleId, jsep, media, callbacks, stream); return; } if(isAudioSendEnabled(media) || isVideoSendEnabled(media)) { var constraints = { mandatory: {}, optional: []}; pluginHandle.consentDialog(true); var audioSupport = isAudioSendEnabled(media); if(audioSupport === true && media != undefined && media != null) { if(typeof media.audio === 'object') { audioSupport = media.audio; } } var videoSupport = isVideoSendEnabled(media); if(videoSupport === true && media != undefined && media != null) { var simulcast = callbacks.simulcast === true ? true : false; if(simulcast && !jsep && (media.video === undefined || media.video === false)) media.video = "hires"; if(media.video && media.video != 'screen' && media.video != 'window') { if(typeof media.video === 'object') { videoSupport = media.video; } else { var width = 0; var height = 0, maxHeight = 0; if(media.video === 'lowres') { // Small resolution, 4:3 height = 240; maxHeight = 240; width = 320; } else if(media.video === 'lowres-16:9') { // Small resolution, 16:9 height = 180; maxHeight = 180; width = 320; } else if(media.video === 'hires' || media.video === 'hires-16:9' || media.video === 'hdres') { // High(HD) resolution is only 16:9 height = 720; maxHeight = 720; width = 1280; } else if(media.video === 'fhdres') { // Full HD resolution is only 16:9 height = 1080; maxHeight = 1080; width = 1920; } else if(media.video === '4kres') { // 4K resolution is only 16:9 height = 2160; maxHeight = 2160; width = 3840; } else if(media.video === 'stdres') { // Normal resolution, 4:3 height = 480; maxHeight = 480; width = 640; } else if(media.video === 'stdres-16:9') { // Normal resolution, 16:9 height = 360; maxHeight = 360; width = 640; } else { Janus.log("Default video setting is stdres 4:3"); height = 480; maxHeight = 480; width = 640; } Janus.log("Adding media constraint:", media.video); videoSupport = { 'height': {'ideal': height}, 'width': {'ideal': width} }; Janus.log("Adding video constraint:", videoSupport); } } else if(media.video === 'screen' || media.video === 'window') { if(!media.screenshareFrameRate) { media.screenshareFrameRate = 3; } if(navigator.getDisplayMedia) { // The new experimental getDisplayMedia API is available, let's use that // https://groups.google.com/forum/#!topic/discuss-webrtc/Uf0SrR4uxzk // https://webrtchacks.com/chrome-screensharing-getdisplaymedia/ navigator.getDisplayMedia({ video: true }) .then(function(stream) { pluginHandle.consentDialog(false); if(isAudioSendEnabled(media) && !media.keepAudio) { navigator.mediaDevices.getUserMedia({ audio: true, video: false }) .then(function (audioStream) { stream.addTrack(audioStream.getAudioTracks()[0]); streamsDone(handleId, jsep, media, callbacks, stream); }) } else { streamsDone(handleId, jsep, media, callbacks, stream); } }, function (error) { pluginHandle.consentDialog(false); callbacks.error(error); }); return; } // We're going to try and use the extension for Chrome 34+, the old approach // for older versions of Chrome, or the experimental support in Firefox 33+ function callbackUserMedia (error, stream) { pluginHandle.consentDialog(false); if(error) { callbacks.error(error); } else { streamsDone(handleId, jsep, media, callbacks, stream); } }; function getScreenMedia(constraints, gsmCallback, useAudio) { Janus.log("Adding media constraint (screen capture)"); Janus.debug(constraints); navigator.mediaDevices.getUserMedia(constraints) .then(function(stream) { if(useAudio) { navigator.mediaDevices.getUserMedia({ audio: true, video: false }) .then(function (audioStream) { stream.addTrack(audioStream.getAudioTracks()[0]); gsmCallback(null, stream); }) } else { gsmCallback(null, stream); } }) .catch(function(error) { pluginHandle.consentDialog(false); gsmCallback(error); }); }; if(Janus.webRTCAdapter.browserDetails.browser === 'chrome') { var chromever = Janus.webRTCAdapter.browserDetails.version; var maxver = 33; if(window.navigator.userAgent.match('Linux')) maxver = 35; // "known" crash in chrome 34 and 35 on linux if(chromever >= 26 && chromever <= maxver) { // Chrome 26->33 requires some awkward chrome://flags manipulation constraints = { video: { mandatory: { googLeakyBucket: true, maxWidth: window.screen.width, maxHeight: window.screen.height, minFrameRate: media.screenshareFrameRate, maxFrameRate: media.screenshareFrameRate, chromeMediaSource: 'screen' } }, audio: isAudioSendEnabled(media) && !media.keepAudio }; getScreenMedia(constraints, callbackUserMedia); } else { // Chrome 34+ requires an extension Janus.extension.getScreen(function (error, sourceId) { if (error) { pluginHandle.consentDialog(false); return callbacks.error(error); } constraints = { audio: false, video: { mandatory: { chromeMediaSource: 'desktop', maxWidth: window.screen.width, maxHeight: window.screen.height, minFrameRate: media.screenshareFrameRate, maxFrameRate: media.screenshareFrameRate, }, optional: [ {googLeakyBucket: true}, {googTemporalLayeredScreencast: true} ] } }; constraints.video.mandatory.chromeMediaSourceId = sourceId; getScreenMedia(constraints, callbackUserMedia, isAudioSendEnabled(media) && !media.keepAudio); }); } } else if (window.navigator.userAgent.match('Firefox')) { var ffver = parseInt(window.navigator.userAgent.match(/Firefox\/(.*)/)[1], 10); if(ffver >= 33) { // Firefox 33+ has experimental support for screen sharing constraints = { video: { mozMediaSource: media.video, mediaSource: media.video }, audio: isAudioSendEnabled(media) && !media.keepAudio }; getScreenMedia(constraints, function (err, stream) { callbackUserMedia(err, stream); // Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=1045810 if (!err) { var lastTime = stream.currentTime; var polly = window.setInterval(function () { if(!stream) window.clearInterval(polly); if(stream.currentTime == lastTime) { window.clearInterval(polly); if(stream.onended) { stream.onended(); } } lastTime = stream.currentTime; }, 500); } }); } else { var error = new Error('NavigatorUserMediaError'); error.name = 'Your version of Firefox does not support screen sharing, please install Firefox 33 (or more recent versions)'; pluginHandle.consentDialog(false); callbacks.error(error); return; } } return; } } // If we got here, we're not screensharing if(media === null || media === undefined || media.video !== 'screen') { // Check whether all media sources are actually available or not navigator.mediaDevices.enumerateDevices().then(function(devices) { var audioExist = devices.some(function(device) { return device.kind === 'audioinput'; }), videoExist = isScreenSendEnabled(media) || devices.some(function(device) { return device.kind === 'videoinput'; }); // Check whether a missing device is really a problem var audioSend = isAudioSendEnabled(media); var videoSend = isVideoSendEnabled(media); var needAudioDevice = isAudioSendRequired(media); var needVideoDevice = isVideoSendRequired(media); if(audioSend || videoSend || needAudioDevice || needVideoDevice) { // We need to send either audio or video var haveAudioDevice = audioSend ? audioExist : false; var haveVideoDevice = videoSend ? videoExist : false; if(!haveAudioDevice && !haveVideoDevice) { // FIXME Should we really give up, or just assume recvonly for both? pluginHandle.consentDialog(false); callbacks.error('No capture device found'); return false; } else if(!haveAudioDevice && needAudioDevice) { pluginHandle.consentDialog(false); callbacks.error('Audio capture is required, but no capture device found'); return false; } else if(!haveVideoDevice && needVideoDevice) { pluginHandle.consentDialog(false); callbacks.error('Video capture is required, but no capture device found'); return false; } } var gumConstraints = { audio: (audioExist && !media.keepAudio) ? audioSupport : false, video: (videoExist && !media.keepVideo) ? videoSupport : false }; Janus.debug("getUserMedia constraints", gumConstraints); navigator.mediaDevices.getUserMedia(gumConstraints) .then(function(stream) { pluginHandle.consentDialog(false); streamsDone(handleId, jsep, media, callbacks, stream); }).catch(function(error) { pluginHandle.consentDialog(false); callbacks.error({code: error.code, name: error.name, message: error.message}); }); }) .catch(function(error) { pluginHandle.consentDialog(false); callbacks.error('enumerateDevices error', error); }); } } else { // No need to do a getUserMedia, create offer/answer right away streamsDone(handleId, jsep, media, callbacks); } } function prepareWebrtcPeer(handleId, callbacks) { callbacks = callbacks || {}; callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop; callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : webrtcError; var jsep = callbacks.jsep; var pluginHandle = pluginHandles[handleId]; if(pluginHandle === null || pluginHandle === undefined || pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) { Janus.warn("Invalid handle"); callbacks.error("Invalid handle"); return; } var config = pluginHandle.webrtcStuff; if(jsep !== undefined && jsep !== null) { if(config.pc === null) { Janus.warn("Wait, no PeerConnection?? if this is an answer, use createAnswer and not handleRemoteJsep"); callbacks.error("No PeerConnection: if this is an answer, use createAnswer and not handleRemoteJsep"); return; } config.pc.setRemoteDescription(jsep) .then(function() { Janus.log("Remote description accepted!"); config.remoteSdp = jsep.sdp; // Any trickle candidate we cached? if(config.candidates && config.candidates.length > 0) { for(var i in config.candidates) { var candidate = config.candidates[i]; Janus.debug("Adding remote candidate:", candidate); if(!candidate || candidate.completed === true) { // end-of-candidates config.pc.addIceCandidate(); } else { // New candidate config.pc.addIceCandidate(candidate); } } config.candidates = []; } // Done callbacks.success(); }, callbacks.error); } else { callbacks.error("Invalid JSEP"); } } function createOffer(handleId, media, callbacks) { callbacks = callbacks || {}; callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop; callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : Janus.noop; var pluginHandle = pluginHandles[handleId]; if(pluginHandle === null || pluginHandle === undefined || pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) { Janus.warn("Invalid handle"); callbacks.error("Invalid handle"); return; } var config = pluginHandle.webrtcStuff; var simulcast = callbacks.simulcast === true ? true : false; if(!simulcast) { Janus.log("Creating offer (iceDone=" + config.iceDone + ")"); } else { Janus.log("Creating offer (iceDone=" + config.iceDone + ", simulcast=" + simulcast + ")"); } // https://code.google.com/p/webrtc/issues/detail?id=3508 var mediaConstraints = {}; if(Janus.webRTCAdapter.browserDetails.browser === "firefox" && Janus.webRTCAdapter.browserDetails.version >= 59) { // Firefox >= 59 uses Transceivers var audioTransceiver = null, videoTransceiver = null; var transceivers = config.pc.getTransceivers(); if(transceivers && transceivers.length > 0) { for(var i in transceivers) { var t = transceivers[i]; if((t.sender && t.sender.track && t.sender.track.kind === "audio") || (t.receiver && t.receiver.track && t.receiver.track.kind === "audio")) { if(!audioTransceiver) audioTransceiver = t; continue; } if((t.sender && t.sender.track && t.sender.track.kind === "video") || (t.receiver && t.receiver.track && t.receiver.track.kind === "video")) { if(!videoTransceiver) videoTransceiver = t; continue; } } } // Handle audio (and related changes, if any) var audioSend = isAudioSendEnabled(media); var audioRecv = isAudioRecvEnabled(media); if(!audioSend && !audioRecv) { // Audio disabled: have we removed it? if(media.removeAudio && audioTransceiver) { audioTransceiver.direction = "inactive"; Janus.log("Setting audio transceiver to inactive:", audioTransceiver); } } else { // Take care of audio m-line if(audioSend && audioRecv) { if(audioTransceiver) { audioTransceiver.direction = "sendrecv"; Janus.log("Setting audio transceiver to sendrecv:", audioTransceiver); } } else if(audioSend && !audioRecv) { if(audioTransceiver) { audioTransceiver.direction = "sendonly"; Janus.log("Setting audio transceiver to sendonly:", audioTransceiver); } } else if(!audioSend && audioRecv) { if(audioTransceiver) { audioTransceiver.direction = "recvonly"; Janus.log("Setting audio transceiver to recvonly:", audioTransceiver); } else { // In theory, this is the only case where we might not have a transceiver yet audioTransceiver = config.pc.addTransceiver("audio", { direction: "recvonly" }); Janus.log("Adding recvonly audio transceiver:", audioTransceiver); } } } // Handle video (and related changes, if any) var videoSend = isVideoSendEnabled(media); var videoRecv = isVideoRecvEnabled(media); if(!videoSend && !videoRecv) { // Video disabled: have we removed it? if(media.removeVideo && videoTransceiver) { videoTransceiver.direction = "inactive"; Janus.log("Setting video transceiver to inactive:", videoTransceiver); } } else { // Take care of video m-line if(videoSend && videoRecv) { if(videoTransceiver) { videoTransceiver.direction = "sendrecv"; Janus.log("Setting video transceiver to sendrecv:", videoTransceiver); } } else if(videoSend && !videoRecv) { if(videoTransceiver) { videoTransceiver.direction = "sendonly"; Janus.log("Setting video transceiver to sendonly:", videoTransceiver); } } else if(!videoSend && videoRecv) { if(videoTransceiver) { videoTransceiver.direction = "recvonly"; Janus.log("Setting video transceiver to recvonly:", videoTransceiver); } else { // In theory, this is the only case where we might not have a transceiver yet videoTransceiver = config.pc.addTransceiver("video", { direction: "recvonly" }); Janus.log("Adding recvonly video transceiver:", videoTransceiver); } } } } else { mediaConstraints["offerToReceiveAudio"] = isAudioRecvEnabled(media); mediaConstraints["offerToReceiveVideo"] = isVideoRecvEnabled(media); } var iceRestart = callbacks.iceRestart === true ? true : false; if(iceRestart) { mediaConstraints["iceRestart"] = true; } Janus.debug(mediaConstraints); // Check if this is Firefox and we've been asked to do simulcasting var sendVideo = isVideoSendEnabled(media); if(sendVideo && simulcast && Janus.webRTCAdapter.browserDetails.browser === "firefox") { // FIXME Based on https://gist.github.com/voluntas/088bc3cc62094730647b Janus.log("Enabling Simulcasting for Firefox (RID)"); var sender = config.pc.getSenders()[1]; Janus.log(sender); var parameters = sender.getParameters(); Janus.log(parameters); sender.setParameters({encodings: [ { rid: "high", active: true, priority: "high", maxBitrate: 1000000 }, { rid: "medium", active: true, priority: "medium", maxBitrate: 300000 }, { rid: "low", active: true, priority: "low", maxBitrate: 100000 } ]}); } config.pc.createOffer(mediaConstraints) .then(function(offer) { Janus.debug(offer); Janus.log("Setting local description"); if(sendVideo && simulcast) { // This SDP munging only works with Chrome if(Janus.webRTCAdapter.browserDetails.browser === "chrome") { Janus.log("Enabling Simulcasting for Chrome (SDP munging)"); offer.sdp = mungeSdpForSimulcasting(offer.sdp); } else if(Janus.webRTCAdapter.browserDetails.browser !== "firefox") { Janus.warn("simulcast=true, but this is not Chrome nor Firefox, ignoring"); } } config.mySdp = offer.sdp; config.pc.setLocalDescription(offer) .catch(callbacks.error); config.mediaConstraints = mediaConstraints; if(!config.iceDone && !config.trickle) { // Don't do anything until we have all candidates Janus.log("Waiting for all candidates..."); return; } Janus.log("Offer ready"); Janus.debug(callbacks); // JSON.stringify doesn't work on some WebRTC objects anymore // See https://code.google.com/p/chromium/issues/detail?id=467366 var jsep = { "type": offer.type, "sdp": offer.sdp }; callbacks.success(jsep); }, callbacks.error); } function createAnswer(handleId, media, callbacks) { callbacks = callbacks || {}; callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop; callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : Janus.noop; var pluginHandle = pluginHandles[handleId]; if(pluginHandle === null || pluginHandle === undefined || pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) { Janus.warn("Invalid handle"); callbacks.error("Invalid handle"); return; } var config = pluginHandle.webrtcStuff; var simulcast = callbacks.simulcast === true ? true : false; if(!simulcast) { Janus.log("Creating answer (iceDone=" + config.iceDone + ")"); } else { Janus.log("Creating answer (iceDone=" + config.iceDone + ", simulcast=" + simulcast + ")"); } var mediaConstraints = null; if(Janus.webRTCAdapter.browserDetails.browser === "firefox" && Janus.webRTCAdapter.browserDetails.version >= 59) { // Firefox >= 59 uses Transceivers mediaConstraints = {}; var audioTransceiver = null, videoTransceiver = null; var transceivers = config.pc.getTransceivers(); if(transceivers && transceivers.length > 0) { for(var i in transceivers) { var t = transceivers[i]; if((t.sender && t.sender.track && t.sender.track.kind === "audio") || (t.receiver && t.receiver.track && t.receiver.track.kind === "audio")) { if(!audioTransceiver) audioTransceiver = t; continue; } if((t.sender && t.sender.track && t.sender.track.kind === "video") || (t.receiver && t.receiver.track && t.receiver.track.kind === "video")) { if(!videoTransceiver) videoTransceiver = t; continue; } } } // Handle audio (and related changes, if any) var audioSend = isAudioSendEnabled(media); var audioRecv = isAudioRecvEnabled(media); if(!audioSend && !audioRecv) { // Audio disabled: have we removed it? if(media.removeAudio && audioTransceiver) { audioTransceiver.direction = "inactive"; Janus.log("Setting audio transceiver to inactive:", audioTransceiver); } } else { // Take care of audio m-line if(audioSend && audioRecv) { if(audioTransceiver) { audioTransceiver.direction = "sendrecv"; Janus.log("Setting audio transceiver to sendrecv:", audioTransceiver); } } else if(audioSend && !audioRecv) { if(audioTransceiver) { audioTransceiver.direction = "sendonly"; Janus.log("Setting audio transceiver to sendonly:", audioTransceiver); } } else if(!audioSend && audioRecv) { if(audioTransceiver) { audioTransceiver.direction = "recvonly"; Janus.log("Setting audio transceiver to recvonly:", audioTransceiver); } else { // In theory, this is the only case where we might not have a transceiver yet audioTransceiver = config.pc.addTransceiver("audio", { direction: "recvonly" }); Janus.log("Adding recvonly audio transceiver:", audioTransceiver); } } } // Handle video (and related changes, if any) var videoSend = isVideoSendEnabled(media); var videoRecv = isVideoRecvEnabled(media); if(!videoSend && !videoRecv) { // Video disabled: have we removed it? if(media.removeVideo && videoTransceiver) { videoTransceiver.direction = "inactive"; Janus.log("Setting video transceiver to inactive:", videoTransceiver); } } else { // Take care of video m-line if(videoSend && videoRecv) { if(videoTransceiver) { videoTransceiver.direction = "sendrecv"; Janus.log("Setting video transceiver to sendrecv:", videoTransceiver); } } else if(videoSend && !videoRecv) { if(videoTransceiver) { videoTransceiver.direction = "sendonly"; Janus.log("Setting video transceiver to sendonly:", videoTransceiver); } } else if(!videoSend && videoRecv) { if(videoTransceiver) { videoTransceiver.direction = "recvonly"; Janus.log("Setting video transceiver to recvonly:", videoTransceiver); } else { // In theory, this is the only case where we might not have a transceiver yet videoTransceiver = config.pc.addTransceiver("video", { direction: "recvonly" }); Janus.log("Adding recvonly video transceiver:", videoTransceiver); } } } } else { if(Janus.webRTCAdapter.browserDetails.browser == "firefox" || Janus.webRTCAdapter.browserDetails.browser == "edge") { mediaConstraints = { offerToReceiveAudio: isAudioRecvEnabled(media), offerToReceiveVideo: isVideoRecvEnabled(media) }; } else { mediaConstraints = { mandatory: { OfferToReceiveAudio: isAudioRecvEnabled(media), OfferToReceiveVideo: isVideoRecvEnabled(media) } }; } } Janus.debug(mediaConstraints); // Check if this is Firefox and we've been asked to do simulcasting var sendVideo = isVideoSendEnabled(media); if(sendVideo && simulcast && Janus.webRTCAdapter.browserDetails.browser === "firefox") { // FIXME Based on https://gist.github.com/voluntas/088bc3cc62094730647b Janus.log("Enabling Simulcasting for Firefox (RID)"); var sender = config.pc.getSenders()[1]; Janus.log(sender); var parameters = sender.getParameters(); Janus.log(parameters); sender.setParameters({encodings: [ { rid: "high", active: true, priority: "high", maxBitrate: 1000000 }, { rid: "medium", active: true, priority: "medium", maxBitrate: 300000 }, { rid: "low", active: true, priority: "low", maxBitrate: 100000 } ]}); } config.pc.createAnswer(mediaConstraints) .then(function(answer) { Janus.debug(answer); Janus.log("Setting local description"); if(sendVideo && simulcast) { // This SDP munging only works with Chrome if(Janus.webRTCAdapter.browserDetails.browser === "chrome") { // FIXME Apparently trying to simulcast when answering breaks video in Chrome... //~ Janus.log("Enabling Simulcasting for Chrome (SDP munging)"); //~ answer.sdp = mungeSdpForSimulcasting(answer.sdp); Janus.warn("simulcast=true, but this is an answer, and video breaks in Chrome if we enable it"); } else if(Janus.webRTCAdapter.browserDetails.browser !== "firefox") { Janus.warn("simulcast=true, but this is not Chrome nor Firefox, ignoring"); } } config.mySdp = answer.sdp; config.pc.setLocalDescription(answer) .catch(callbacks.error); config.mediaConstraints = mediaConstraints; if(!config.iceDone && !config.trickle) { // Don't do anything until we have all candidates Janus.log("Waiting for all candidates..."); return; } // JSON.stringify doesn't work on some WebRTC objects anymore // See https://code.google.com/p/chromium/issues/detail?id=467366 var jsep = { "type": answer.type, "sdp": answer.sdp }; callbacks.success(jsep); }, callbacks.error); } function sendSDP(handleId, callbacks) { callbacks = callbacks || {}; callbacks.success = (typeof callbacks.success == "function") ? callbacks.success : Janus.noop; callbacks.error = (typeof callbacks.error == "function") ? callbacks.error : Janus.noop; var pluginHandle = pluginHandles[handleId]; if(pluginHandle === null || pluginHandle === undefined || pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) { Janus.warn("Invalid handle, not sending anything"); return; } var config = pluginHandle.webrtcStuff; Janus.log("Sending offer/answer SDP..."); if(config.mySdp === null || config.mySdp === undefined) { Janus.warn("Local SDP instance is invalid, not sending anything..."); return; } config.mySdp = { "type": config.pc.localDescription.type, "sdp": config.pc.localDescription.sdp }; if(config.trickle === false) config.mySdp["trickle"] = false; Janus.debug(callbacks); config.sdpSent = true; callbacks.success(config.mySdp); } function getVolume(handleId, remote) { var pluginHandle = pluginHandles[handleId]; if(pluginHandle === null || pluginHandle === undefined || pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) { Janus.warn("Invalid handle"); return 0; } var stream = remote ? "remote" : "local"; var config = pluginHandle.webrtcStuff; if(!config.volume[stream]) config.volume[stream] = { value: 0 }; // Start getting the volume, if getStats is supported if(config.pc.getStats && Janus.webRTCAdapter.browserDetails.browser === "chrome") { if(remote && (config.remoteStream === null || config.remoteStream === undefined)) { Janus.warn("Remote stream unavailable"); return 0; } else if(!remote && (config.myStream === null || config.myStream === undefined)) { Janus.warn("Local stream unavailable"); return 0; } if(config.volume[stream].timer === null || config.volume[stream].timer === undefined) { Janus.log("Starting " + stream + " volume monitor"); config.volume[stream].timer = setInterval(function() { config.pc.getStats(function(stats) { var results = stats.result(); for(var i=0; i -1) && res.type === "inbound-rtp" && res.id.indexOf("rtcp") < 0) { // New stats inStats = true; } else if(res.type == 'ssrc' && res.bytesReceived && (res.googCodecName === "VP8" || res.googCodecName === "")) { // Older Chromer versions inStats = true; } // Parse stats now if(inStats) { config.bitrate.bsnow = res.bytesReceived; config.bitrate.tsnow = res.timestamp; if(config.bitrate.bsbefore === null || config.bitrate.tsbefore === null) { // Skip this round config.bitrate.bsbefore = config.bitrate.bsnow; config.bitrate.tsbefore = config.bitrate.tsnow; } else { // Calculate bitrate var timePassed = config.bitrate.tsnow - config.bitrate.tsbefore; if(Janus.webRTCAdapter.browserDetails.browser == "safari") timePassed = timePassed/1000; // Apparently the timestamp is in microseconds, in Safari var bitRate = Math.round((config.bitrate.bsnow - config.bitrate.bsbefore) * 8 / timePassed); config.bitrate.value = bitRate + ' kbits/sec'; //~ Janus.log("Estimated bitrate is " + config.bitrate.value); config.bitrate.bsbefore = config.bitrate.bsnow; config.bitrate.tsbefore = config.bitrate.tsnow; } } }); }); }, 1000); return "0 kbits/sec"; // We don't have a bitrate value yet } return config.bitrate.value; } else { Janus.warn("Getting the video bitrate unsupported by browser"); return "Feature unsupported by browser"; } } function webrtcError(error) { Janus.error("WebRTC error:", error); } function cleanupWebrtc(handleId, hangupRequest) { Janus.log("Cleaning WebRTC stuff"); var pluginHandle = pluginHandles[handleId]; if(pluginHandle === null || pluginHandle === undefined) { // Nothing to clean return; } var config = pluginHandle.webrtcStuff; if(config !== null && config !== undefined) { if(hangupRequest === true) { // Send a hangup request (we don't really care about the response) var request = { "janus": "hangup", "transaction": Janus.randomString(12) }; if(pluginHandle.token !== null && pluginHandle.token !== undefined) request["token"] = pluginHandle.token; if(apisecret !== null && apisecret !== undefined) request["apisecret"] = apisecret; Janus.debug("Sending hangup request (handle=" + handleId + "):"); Janus.debug(request); if(websockets) { request["session_id"] = sessionId; request["handle_id"] = handleId; ws.send(JSON.stringify(request)); } else { Janus.httpAPICall(server + "/" + sessionId + "/" + handleId, { verb: 'POST', withCredentials: withCredentials, body: request }); } } // Cleanup stack config.remoteStream = null; if(config.volume) { if(config.volume["local"] && config.volume["local"].timer) clearInterval(config.volume["local"].timer); if(config.volume["remote"] && config.volume["remote"].timer) clearInterval(config.volume["remote"].timer); } config.volume = {}; if(config.bitrate.timer) clearInterval(config.bitrate.timer); config.bitrate.timer = null; config.bitrate.bsnow = null; config.bitrate.bsbefore = null; config.bitrate.tsnow = null; config.bitrate.tsbefore = null; config.bitrate.value = null; try { // Try a MediaStreamTrack.stop() for each track if(!config.streamExternal && config.myStream !== null && config.myStream !== undefined) { Janus.log("Stopping local stream tracks"); var tracks = config.myStream.getTracks(); for(var i in tracks) { var mst = tracks[i]; Janus.log(mst); if(mst !== null && mst !== undefined) mst.stop(); } } } catch(e) { // Do nothing if this fails } config.streamExternal = false; config.myStream = null; // Close PeerConnection try { config.pc.close(); } catch(e) { // Do nothing } config.pc = null; config.candidates = null; config.mySdp = null; config.remoteSdp = null; config.iceDone = false; config.dataChannel = null; config.dtmfSender = null; } pluginHandle.oncleanup(); } // Helper method to munge an SDP to enable simulcasting (Chrome only) function mungeSdpForSimulcasting(sdp) { // Let's munge the SDP to add the attributes for enabling simulcasting // (based on https://gist.github.com/ggarber/a19b4c33510028b9c657) var lines = sdp.split("\r\n"); var video = false; var ssrc = [ -1 ], ssrc_fid = [ -1 ]; var cname = null, msid = null, mslabel = null, label = null; var insertAt = -1; for(var i=0; i -1) { // We're done, let's add the new attributes here insertAt = i; break; } } continue; } if(!video) continue; var fid = lines[i].match(/a=ssrc-group:FID (\d+) (\d+)/); if(fid) { ssrc[0] = fid[1]; ssrc_fid[0] = fid[2]; lines.splice(i, 1); i--; continue; } if(ssrc[0]) { var match = lines[i].match('a=ssrc:' + ssrc[0] + ' cname:(.+)') if(match) { cname = match[1]; } match = lines[i].match('a=ssrc:' + ssrc[0] + ' msid:(.+)') if(match) { msid = match[1]; } match = lines[i].match('a=ssrc:' + ssrc[0] + ' mslabel:(.+)') if(match) { mslabel = match[1]; } match = lines[i].match('a=ssrc:' + ssrc + ' label:(.+)') if(match) { label = match[1]; } if(lines[i].indexOf('a=ssrc:' + ssrc_fid) === 0) { lines.splice(i, 1); i--; continue; } if(lines[i].indexOf('a=ssrc:' + ssrc[0]) === 0) { lines.splice(i, 1); i--; continue; } } if(lines[i].length == 0) { lines.splice(i, 1); i--; continue; } } if(ssrc[0] < 0) { // Couldn't find a FID attribute, let's just take the first video SSRC we find insertAt = -1; video = false; for(var i=0; i -1) { // We're done, let's add the new attributes here insertAt = i; break; } } continue; } if(!video) continue; if(ssrc[0] < 0) { var value = lines[i].match(/a=ssrc:(\d+)/); if(value) { ssrc[0] = value[1]; lines.splice(i, 1); i--; continue; } } else { var match = lines[i].match('a=ssrc:' + ssrc[0] + ' cname:(.+)') if(match) { cname = match[1]; } match = lines[i].match('a=ssrc:' + ssrc[0] + ' msid:(.+)') if(match) { msid = match[1]; } match = lines[i].match('a=ssrc:' + ssrc[0] + ' mslabel:(.+)') if(match) { mslabel = match[1]; } match = lines[i].match('a=ssrc:' + ssrc[0] + ' label:(.+)') if(match) { label = match[1]; } if(lines[i].indexOf('a=ssrc:' + ssrc_fid[0]) === 0) { lines.splice(i, 1); i--; continue; } if(lines[i].indexOf('a=ssrc:' + ssrc[0]) === 0) { lines.splice(i, 1); i--; continue; } } if(lines[i].length == 0) { lines.splice(i, 1); i--; continue; } } } if(ssrc[0] < 0) { // Still nothing, let's just return the SDP we were asked to munge Janus.warn("Couldn't find the video SSRC, simulcasting NOT enabled"); return sdp; } if(insertAt < 0) { // Append at the end insertAt = lines.length; } // Generate a couple of SSRCs (for retransmissions too) // Note: should we check if there are conflicts, here? ssrc[1] = Math.floor(Math.random()*0xFFFFFFFF); ssrc[2] = Math.floor(Math.random()*0xFFFFFFFF); ssrc_fid[1] = Math.floor(Math.random()*0xFFFFFFFF); ssrc_fid[2] = Math.floor(Math.random()*0xFFFFFFFF); // Add attributes to the SDP for(var i=0; i