From 0d6fd3adf0b1c9d5c1cb01542da9bd28f30e40ed Mon Sep 17 00:00:00 2001 From: Tamas Szoke Date: Sun, 13 Jan 2019 17:43:07 +0100 Subject: [PATCH] [UPDATE]: ESLint standard style --- src/index.js | 475 +++++++++++++++++++++++++-------------------------- 1 file changed, 233 insertions(+), 242 deletions(-) diff --git a/src/index.js b/src/index.js index 7aae883..b6e293d 100644 --- a/src/index.js +++ b/src/index.js @@ -5,35 +5,34 @@ * @license * Copyright © 2019 Tamas Szoke * Licensed under the MIT License - * @version */ /** * Adapter.js */ // eslint-disable-next-line -import adapter from 'webrtc-adapter'; - +import adapter from 'webrtc-adapter' + /** * Main function * @param {object} config * @param {function} sendCallback */ const NeatRTC = (config, sendCallback) => { - const DEVELOPMENT_MODE = config.devMode; - const DATACHANNEL_CONFIG = config.datachannels; - let peerConnection = []; - let datachannelList = []; - let streamLocal = null; - let streamRemote = null; - let streamLocalConnected = false; - let streamRemoteConnected = false; - let videoIdLocal = config.videoIdLocal; - let videoIdRemote = config.videoIdRemote; - let connected = config.connected; - let mediaStreamConnected = config.mediaStreamConnected; - let mediaStreamRemoved = config.mediaStreamRemoved; - let mediaStreamRemoteRemoved = config.mediaStreamRemoteRemoved; + const DEVELOPMENT_MODE = config.devMode + const DATACHANNEL_CONFIG = config.datachannels + let peerConnection = [] + let datachannelList = [] + let streamLocal = null + let streamRemote = null + let streamLocalConnected = false + let streamRemoteConnected = false + let videoIdLocal = config.videoIdLocal + let videoIdRemote = config.videoIdRemote + let connected = config.connected + let mediaStreamConnected = config.mediaStreamConnected + let mediaStreamRemoved = config.mediaStreamRemoved + let mediaStreamRemoteRemoved = config.mediaStreamRemoteRemoved /** * Private functions @@ -45,25 +44,25 @@ const NeatRTC = (config, sendCallback) => { */ const initConnection = (connectionType) => { const CONFIGURATION = { - "iceServers": [ + iceServers: [ { - "url": "stun:stun.l.google.com:19302" + url: 'stun:stun.l.google.com:19302' } ] - }; + } const OPTIONAL = { - 'optional': [ + optional: [ { - 'DtlsSrtpKeyAgreement': true + DtlsSrtpKeyAgreement: true }, { - 'RtpDataChannels': false + RtpDataChannels: false } ] - }; - peerConnection[connectionType] = new RTCPeerConnection(CONFIGURATION, OPTIONAL); - peerConnection[connectionType].datachannelList = []; - log('init connection: ' + connectionType); + } + peerConnection[connectionType] = new RTCPeerConnection(CONFIGURATION, OPTIONAL) + peerConnection[connectionType].datachannelList = [] + log('init connection: ' + connectionType) } /** @@ -71,29 +70,29 @@ const NeatRTC = (config, sendCallback) => { */ const initDisconnection = () => { // Stop and reset remote video - let videoElementRemote = document.getElementById(videoIdRemote); - videoElementRemote.pause(); + let videoElementRemote = document.getElementById(videoIdRemote) + videoElementRemote.pause() if (streamRemote) { - streamRemote = null; + streamRemote = null } // videoElementRemote.removeAttribute('src'); // Chrome 71... - videoElementRemote.removeAttribute('srcObject'); - videoElementRemote.load(); - streamRemoteConnected = false; + videoElementRemote.removeAttribute('srcObject') + videoElementRemote.load() + streamRemoteConnected = false // Remove datachannels for (let i = 0; i < DATACHANNEL_CONFIG.length; i++) { - datachannel('remove', DATACHANNEL_CONFIG[i].name); + datachannel('remove', DATACHANNEL_CONFIG[i].name) } // Close and reset peerconnections if (peerConnection['media']) { - peerConnection['media'].close(); - peerConnection['media'] = null; + peerConnection['media'].close() + peerConnection['media'] = null } if (peerConnection['data']) { - peerConnection['data'].close(); - peerConnection['data'] = null; + peerConnection['data'].close() + peerConnection['data'] = null } - log('disconnected'); + log('disconnected') } /** @@ -107,19 +106,19 @@ const NeatRTC = (config, sendCallback) => { 'OfferToReceiveAudio': false, 'OfferToReceiveVideo': false } - }; - initConnection('data'); - initDatachannel(); + } + initConnection('data') + initDatachannel() if (messageType === 'sendOffer') { - onIceCandidate('data'); - onDatachannel('data'); - createOffer('data', SDP_CONSTRAINTS); + onIceCandidate('data') + onDatachannel('data') + createOffer('data', SDP_CONSTRAINTS) } else if (messageType === 'receiveOffer') { - onIceCandidate('data'); - onDatachannel('data'); - peerConnection['data'].setRemoteDescription(new RTCSessionDescription(data)); - createAnswer('data', SDP_CONSTRAINTS); - log('data stream -> offer received'); + onIceCandidate('data') + onDatachannel('data') + peerConnection['data'].setRemoteDescription(new RTCSessionDescription(data)) + createAnswer('data', SDP_CONSTRAINTS) + log('data stream -> offer received') } } @@ -134,22 +133,22 @@ const NeatRTC = (config, sendCallback) => { 'OfferToReceiveAudio': true, 'OfferToReceiveVideo': true } - }; - initConnection('media'); - onStreamAdd('media'); - // onStreamEnd('media'); - onIceCandidate('media'); - // onRenegotiationNeeded('media'); + } + initConnection('media') + onStreamAdd('media') + // onStreamEnd('media') + onIceCandidate('media') + // onRenegotiationNeeded('media') if (streamLocal) { - peerConnection['media'].addStream(streamLocal); - log('media stream -> local stream added'); + peerConnection['media'].addStream(streamLocal) + log('media stream -> local stream added') } if (messageType === 'sendOffer') { - createOffer('media', MEDIA_CONSTRAINTS); + createOffer('media', MEDIA_CONSTRAINTS) } else if (messageType === 'receiveOffer') { - peerConnection['media'].setRemoteDescription(new RTCSessionDescription(data)); - createAnswer('media', MEDIA_CONSTRAINTS); - log('media stream -> offer received'); + peerConnection['media'].setRemoteDescription(new RTCSessionDescription(data)) + createAnswer('media', MEDIA_CONSTRAINTS) + log('media stream -> offer received') } } @@ -158,10 +157,10 @@ const NeatRTC = (config, sendCallback) => { */ const initDatachannel = () => { // Add default datachannel to handle media events - datachannel('add', '_default'); + datachannel('add', '_default') if (DATACHANNEL_CONFIG && DATACHANNEL_CONFIG.length > 0) { // check if theres a custom channel list for (let i = 0; i < DATACHANNEL_CONFIG.length; i++) { // loop through the pre-defined datachannel list, and start them - datachannel('add', DATACHANNEL_CONFIG[i].name, DATACHANNEL_CONFIG[i].callbacks, DATACHANNEL_CONFIG[i].binaryType); + datachannel('add', DATACHANNEL_CONFIG[i].name, DATACHANNEL_CONFIG[i].callbacks, DATACHANNEL_CONFIG[i].binaryType) } } } @@ -174,99 +173,97 @@ const NeatRTC = (config, sendCallback) => { * @param {string} binaryType */ const datachannel = (type, channelName, callbacks, binaryType) => { - if (type.toLowerCase() === "add") { - let channel = peerConnection['data'].createDataChannel(channelName, { reliable: true }); // reliable: true to be sure - datachannelList[channelName] = channel; - peerConnection['data'].datachannelList[channelName] = channel; + if (type.toLowerCase() === 'add') { + let channel = peerConnection['data'].createDataChannel(channelName, { reliable: true }) + datachannelList[channelName] = channel + peerConnection['data'].datachannelList[channelName] = channel // Set default datachannel event listeners if (channelName === '_default') { - onDefaultDatachannelOpen(channelName); - onDefaultDatachannelMessage(channelName); - onDefaultDatachannelError(channelName); - onDefaultDatachannelClose(channelName); + onDefaultDatachannelOpen(channelName) + onDefaultDatachannelMessage(channelName) + onDefaultDatachannelError(channelName) + onDefaultDatachannelClose(channelName) } // Set callback on datachannel events if (callbacks) { - onDatachannelOpen(channelName, callbacks.open); - onDatachannelMessage(channelName, callbacks.message); - onDatachannelError(channelName, callbacks.error); - onDatachannelClose(channelName, callbacks.close); + onDatachannelOpen(channelName, callbacks.open) + onDatachannelMessage(channelName, callbacks.message) + onDatachannelError(channelName, callbacks.error) + onDatachannelClose(channelName, callbacks.close) } - log('datachannel added: ' + channelName); - - } else if (type.toLowerCase() === "remove") { - datachannelList[channelName].close(); - peerConnection['data'].datachannelList[channelName].close(); - log('datachannel removed: ' + channelName); + log('datachannel added: ' + channelName) + } else if (type.toLowerCase() === 'remove') { + datachannelList[channelName].close() + peerConnection['data'].datachannelList[channelName].close() + log('datachannel removed: ' + channelName) } } /** * Create offer to connect - * @param {string} connectionType - * @param {object} SDP_CONSTRAINTS + * @param {string} connectionType + * @param {object} SDP_CONSTRAINTS */ const createOffer = (connectionType, SDP_CONSTRAINTS) => { - peerConnection[connectionType].createOffer(function(description) { - peerConnection[connectionType].setLocalDescription(description); - log(connectionType + ' -> offer sent'); + peerConnection[connectionType].createOffer((description) => { + peerConnection[connectionType].setLocalDescription(description) + log(connectionType + ' -> offer sent') sendCallback({ type: 'receiveOffer', connectionType: connectionType, data: description }) - }, function(err) { - log(err); + }, (err) => { + log(err) }, SDP_CONSTRAINTS) } /** * Create answer to offer - * @param {string} connectionType - * @param {object} SDP_CONSTRAINTS + * @param {string} connectionType + * @param {object} SDP_CONSTRAINTS */ const createAnswer = (connectionType, SDP_CONSTRAINTS) => { - peerConnection[connectionType].createAnswer(function(description) { - peerConnection[connectionType].setLocalDescription(description); - log(connectionType + ' -> answer sent'); + peerConnection[connectionType].createAnswer((description) => { + peerConnection[connectionType].setLocalDescription(description) + log(connectionType + ' -> answer sent') sendCallback({ type: 'receiveAnswer', connectionType: connectionType, data: description }) - }, function(err) { - log(err); + }, (err) => { + log(err) }, SDP_CONSTRAINTS) } - + /** * Custom logging for development * @param {string} text */ const log = (text) => { if (DEVELOPMENT_MODE) { - const d = new Date(); - const hours = d.getHours() < 10 ? '0' + d.getHours() : d.getHours(); - const minutes = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes(); - const seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds(); - const time = hours + ':' + minutes + ':' + seconds; - console.log('%c ' + time + ' - ' + text + ' ', 'background: #222; color: #BADA55'); + const d = new Date() + const hours = d.getHours() < 10 ? '0' + d.getHours() : d.getHours() + const minutes = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes() + const seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds() + const time = hours + ':' + minutes + ':' + seconds + console.log('%c ' + time + ' - ' + text + ' ', 'background: #222; color: #BADA55') } } /** * Event handlers */ - /** * Handle received answer from signaling * @param {string} message */ const handleReceivedAnswer = (message) => { - log(message.connectionType + ' -> answer received'); - streamRemote = message.data; - peerConnection[message.connectionType].setRemoteDescription(new RTCSessionDescription(streamRemote)); + log(message.connectionType + ' -> answer received') + streamRemote = message.data + peerConnection[message.connectionType].setRemoteDescription(new RTCSessionDescription(streamRemote)) } /** @@ -274,23 +271,21 @@ const NeatRTC = (config, sendCallback) => { * @param {object} message */ const handleReceivedCandidate = (message) => { - log(message.connectionType + ' -> answer received'); + log(message.connectionType + ' -> answer received') const CANDIDATE = new RTCIceCandidate({ sdpMLineIndex: message.data.label, candidate: message.data.candidate - }); - peerConnection[message.connectionType].addIceCandidate(CANDIDATE); + }) + peerConnection[message.connectionType].addIceCandidate(CANDIDATE) } /** * Ice candidate event - * @param {string} connectionType - */ + * @param {string} connectionType + */ const onIceCandidate = (connectionType) => { - peerConnection[connectionType].onicecandidate = function(e) { - if (!peerConnection[connectionType] || !e || !e.candidate) { - return - } else { + peerConnection[connectionType].onicecandidate = (e) => { + if (peerConnection[connectionType] && e && e.candidate) { sendCallback({ type: 'receiveCandidate', connectionType: connectionType, @@ -306,33 +301,33 @@ const NeatRTC = (config, sendCallback) => { /** * Datachannel received event - * @param {string} connectionType + * @param {string} connectionType */ const onDatachannel = (connectionType) => { - peerConnection[connectionType].ondatachannel = function(e) { - let channel = e.channel; - datachannelList[channel.label] = channel; + peerConnection[connectionType].ondatachannel = (e) => { + let channel = e.channel + datachannelList[channel.label] = channel if (streamLocalConnected) { - initMediaConnection('sendOffer'); + initMediaConnection('sendOffer') } } } /** * Stream added event - * @param {string} connectionType + * @param {string} connectionType */ const onStreamAdd = (connectionType) => { - peerConnection[connectionType].onaddstream = function(e) { - log('streamRemoteConnected: ' + streamRemoteConnected); + peerConnection[connectionType].onaddstream = (e) => { + log('streamRemoteConnected: ' + streamRemoteConnected) if (!streamRemoteConnected) { - streamRemote = e.stream; - let videoElementRemote = document.getElementById(videoIdRemote); + streamRemote = e.stream + let videoElementRemote = document.getElementById(videoIdRemote) // videoElementRemote.src = streamRemote; // Chrome 71... - videoElementRemote.srcObject = streamRemote; - videoElementRemote.play(); - streamRemoteConnected = true; - log('media stream -> stream added'); + videoElementRemote.srcObject = streamRemote + videoElementRemote.play() + streamRemoteConnected = true + log('media stream -> stream added') } } } @@ -343,8 +338,8 @@ const NeatRTC = (config, sendCallback) => { * @param {function} callback */ const onDefaultDatachannelOpen = (channel) => { - peerConnection['data'].datachannelList[channel].onopen = function(e) { - log('Datachannel opened "' + channel + '"'); + peerConnection['data'].datachannelList[channel].onopen = (e) => { + log('Datachannel opened "' + channel + '"') return connected() } } @@ -355,50 +350,50 @@ const NeatRTC = (config, sendCallback) => { * @param {function} callback */ const onDefaultDatachannelMessage = (channel) => { - peerConnection['data'].datachannelList[channel].onmessage = function(e) { - log('Datachannel message "' + channel + '"'); - let message = JSON.parse(e.data); - message = message.message; + peerConnection['data'].datachannelList[channel].onmessage = (e) => { + log('Datachannel message "' + channel + '"') + let message = JSON.parse(e.data) + message = message.message switch (message.type) { case 'mediaStreamStart': - return mediaStreamConnected(); + return mediaStreamConnected() case 'mediaStreamStop': // Partner stopped it's stream, so // stop and reset remote video - streamRemoteConnected = false; - let videoElementRemote = document.getElementById(videoIdRemote); - videoElementRemote.pause(); - if (streamRemote) streamRemote = null; + streamRemoteConnected = false + let videoElementRemote = document.getElementById(videoIdRemote) + videoElementRemote.pause() + if (streamRemote) streamRemote = null if (!message.NO_RESET) { // videoElementRemote.removeAttribute('src'); // Chrome 71... - videoElementRemote.removeAttribute('srcObject'); - videoElementRemote.load(); + videoElementRemote.removeAttribute('srcObject') + videoElementRemote.load() } - return mediaStreamRemoved(); + return mediaStreamRemoved() case 'mediaStreamStopRemote': // Peer stopped our stream, so // stop and reset local video - streamLocalConnected = false; - log('Local stream false'); + streamLocalConnected = false + log('Local stream false') if (peerConnection['media']) { - peerConnection['media'].removeStream(streamLocal); + peerConnection['media'].removeStream(streamLocal) } - let videoElementLocal = document.getElementById(videoIdLocal); - videoElementLocal.pause(); - if (streamLocal && streamLocal.getVideoTracks() && streamLocal.getVideoTracks()[0]) streamLocal.getVideoTracks()[0].stop(); - if (streamLocal && streamLocal.getAudioTracks() && streamLocal.getAudioTracks()[0]) streamLocal.getAudioTracks()[0].stop(); - if (streamLocal) streamLocal = null; + let videoElementLocal = document.getElementById(videoIdLocal) + videoElementLocal.pause() + if (streamLocal && streamLocal.getVideoTracks() && streamLocal.getVideoTracks()[0]) streamLocal.getVideoTracks()[0].stop() + if (streamLocal && streamLocal.getAudioTracks() && streamLocal.getAudioTracks()[0]) streamLocal.getAudioTracks()[0].stop() + if (streamLocal) streamLocal = null if (!message.NO_RESET) { // videoElementLocal.removeAttribute('src'); // Chrome 71... - videoElementLocal.removeAttribute('srcObject'); - videoElementLocal.load(); + videoElementLocal.removeAttribute('srcObject') + videoElementLocal.load() } - return mediaStreamRemoteRemoved(); - + return mediaStreamRemoteRemoved() + default: - return false; + return false } } } @@ -409,8 +404,8 @@ const NeatRTC = (config, sendCallback) => { * @param {function} callback */ const onDefaultDatachannelError = (channel) => { - peerConnection['data'].datachannelList[channel].onerror = function(e) { - log('Datachannel error "' + channel + '"'); + peerConnection['data'].datachannelList[channel].onerror = (e) => { + log('Datachannel error "' + channel + '"') } } @@ -420,8 +415,8 @@ const NeatRTC = (config, sendCallback) => { * @param {function} callback */ const onDefaultDatachannelClose = (channel) => { - peerConnection['data'].datachannelList[channel].onclose = function(e) { - log('Datachannel closed "' + channel + '"'); + peerConnection['data'].datachannelList[channel].onclose = (e) => { + log('Datachannel closed "' + channel + '"') } } @@ -431,9 +426,9 @@ const NeatRTC = (config, sendCallback) => { * @param {function} callback */ const onDatachannelOpen = (channel, callback) => { - peerConnection['data'].datachannelList[channel].onopen = function(e) { - log('Datachannel opened "' + channel + '"'); - callback(channel); + peerConnection['data'].datachannelList[channel].onopen = (e) => { + log('Datachannel opened "' + channel + '"') + callback(channel) } } @@ -443,10 +438,10 @@ const NeatRTC = (config, sendCallback) => { * @param {function} callback */ const onDatachannelMessage = (channel, callback) => { - peerConnection['data'].datachannelList[channel].onmessage = function(e) { - log('Datachannel message "' + channel + '"'); - let message = JSON.parse(e.data); - callback(channel, message); + peerConnection['data'].datachannelList[channel].onmessage = (e) => { + log('Datachannel message "' + channel + '"') + let message = JSON.parse(e.data) + callback(channel, message) } } @@ -456,9 +451,9 @@ const NeatRTC = (config, sendCallback) => { * @param {function} callback */ const onDatachannelError = (channel, callback) => { - peerConnection['data'].datachannelList[channel].onerror = function(e) { - log('Datachannel error "' + channel + '"'); - callback(channel, e); + peerConnection['data'].datachannelList[channel].onerror = (e) => { + log('Datachannel error "' + channel + '"') + callback(channel, e) } } @@ -468,9 +463,9 @@ const NeatRTC = (config, sendCallback) => { * @param {function} callback */ const onDatachannelClose = (channel, callback) => { - peerConnection['data'].datachannelList[channel].onclose = function(e) { - log('Datachannel closed "' + channel + '"'); - callback(channel, e); + peerConnection['data'].datachannelList[channel].onclose = (e) => { + log('Datachannel closed "' + channel + '"') + callback(channel, e) } } @@ -485,15 +480,15 @@ const NeatRTC = (config, sendCallback) => { const connect = (message) => { if (!message) { // Start peerconnection (data) - log('Connecting...'); - initDataConnection('sendOffer', ''); + log('Connecting...') + initDataConnection('sendOffer', '') } else { // Received the offer - log('Connecting (offer)...'); + log('Connecting (offer)...') if (message.connectionType === 'data') { - initDataConnection(message.type, message.data); + initDataConnection(message.type, message.data) } else if (message.connectionType === 'media') { - initMediaConnection(message.type, message.data); + initMediaConnection(message.type, message.data) } } } @@ -502,28 +497,28 @@ const NeatRTC = (config, sendCallback) => { * Disconnect stream */ const disconnect = () => { - log('disconnecting...'); - initDisconnection(); + log('disconnecting...') + initDisconnection() } /** * Handle signaling messages - * @param {object} message + * @param {object} message */ const handleSignaling = (message) => { - const { type } = message; + const { type } = message switch (type) { case 'receiveOffer': - connect(message); - break; + connect(message) + break case 'receiveAnswer': - handleReceivedAnswer(message); - break; + handleReceivedAnswer(message) + break case 'receiveCandidate': - handleReceivedCandidate(message); - break; + handleReceivedCandidate(message) + break default: - return; + break } } @@ -535,23 +530,22 @@ const NeatRTC = (config, sendCallback) => { const checkConnection = (channel) => { if (peerConnection['data']) { let channelData = channel ? peerConnection['data'].datachannelList[channel] : peerConnection['data'].datachannelList['_default'] - let alive = channelData.readyState === 'open' ? true : false - return alive + return channelData.readyState === 'open' } else { return false } } - + /** * Start/stop local camera, microphone * @param {string} type * @param {object} options (resolution, voice, NO_MESSAGE, NO_RESET) */ - const media = (type, options) => { // - const { resolution, voice, NO_MESSAGE, NO_RESET } = options || false; + const media = (type, options) => { + const { resolution, voice, NO_MESSAGE, NO_RESET } = options || false if (type.toLowerCase() === 'start') { - navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; - // Config getUserMedia + navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia + // Configure getUserMedia let video = { mandatory: { minWidth: 320, @@ -559,8 +553,8 @@ const NeatRTC = (config, sendCallback) => { maxWidth: 640, maxHeight: 480 } - }; - let videoElementLocal = document.getElementById(videoIdLocal); + } + let videoElementLocal = document.getElementById(videoIdLocal) if (resolution && resolution.toLowerCase() === 'hd720') { video = { mandatory: { @@ -579,19 +573,19 @@ const NeatRTC = (config, sendCallback) => { maxHeight: 1080 } } - }; - if (voice) video = false; + } + if (voice) video = false const CONSTRAINTS = { audio: true, video: video - }; - let getUserMediaSuccess = function(stream) { - streamLocal = stream; - videoElementLocal.srcObject = stream; - videoElementLocal.play(); - streamLocalConnected = true; - log('Success access media'); - initMediaConnection('sendOffer'); + } + let getUserMediaSuccess = (stream) => { + streamLocal = stream + videoElementLocal.srcObject = stream + videoElementLocal.play() + streamLocalConnected = true + log('Success access media') + initMediaConnection('sendOffer') if (datachannelList && datachannelList['_default']) { const DATA = { channel: '_default', @@ -600,32 +594,31 @@ const NeatRTC = (config, sendCallback) => { voice } } - datachannelList['_default'].send(JSON.stringify(DATA)); + datachannelList['_default'].send(JSON.stringify(DATA)) } - log('Media stream -> offer sent'); + log('Media stream -> offer sent') } - let getUserMediaError = function(error) { - log('Error access media'); - return false; + let getUserMediaError = (error) => { + log('Error access media', error) + return false } - navigator.getUserMedia(CONSTRAINTS, getUserMediaSuccess, getUserMediaError); - + navigator.getUserMedia(CONSTRAINTS, getUserMediaSuccess, getUserMediaError) } else if (type.toLowerCase() === 'stop') { if (streamLocalConnected) { - streamLocalConnected = false; - log('Local stream false'); + streamLocalConnected = false + log('Local stream false') if (peerConnection['media']) { - peerConnection['media'].removeStream(streamLocal); + peerConnection['media'].removeStream(streamLocal) } - let videoElementLocal = document.getElementById(videoIdLocal); - videoElementLocal.pause(); - if (streamLocal && streamLocal.getVideoTracks() && streamLocal.getVideoTracks()[0]) streamLocal.getVideoTracks()[0].stop(); - if (streamLocal && streamLocal.getAudioTracks() && streamLocal.getAudioTracks()[0]) streamLocal.getAudioTracks()[0].stop(); - if (streamLocal) streamLocal = null; + let videoElementLocal = document.getElementById(videoIdLocal) + videoElementLocal.pause() + if (streamLocal && streamLocal.getVideoTracks() && streamLocal.getVideoTracks()[0]) streamLocal.getVideoTracks()[0].stop() + if (streamLocal && streamLocal.getAudioTracks() && streamLocal.getAudioTracks()[0]) streamLocal.getAudioTracks()[0].stop() + if (streamLocal) streamLocal = null if (!NO_RESET) { // videoElementLocal.removeAttribute('src'); // Chrome 71... - videoElementLocal.removeAttribute('srcObject'); - videoElementLocal.load(); + videoElementLocal.removeAttribute('srcObject') + videoElementLocal.load() } if (!NO_MESSAGE && datachannelList && datachannelList['_default']) { const DATA = { @@ -636,21 +629,19 @@ const NeatRTC = (config, sendCallback) => { NO_RESET } } - datachannelList['_default'].send(JSON.stringify(DATA)); - log('MediaStreamStop message sent'); + datachannelList['_default'].send(JSON.stringify(DATA)) + log('MediaStreamStop message sent') } } } else if (type.toLowerCase() === 'stopremote') { - streamRemoteConnected = false; - let videoElementRemote = document.getElementById(videoIdRemote); - videoElementRemote.pause(); - // if (streamRemote && streamRemote.getVideoTracks() && streamRemote.getVideoTracks()[0]) streamRemote.getVideoTracks()[0].stop(); - // if (streamRemote && streamRemote.getAudioTracks() && streamRemote.getAudioTracks()[0]) streamRemote.getAudioTracks()[0].stop(); - if (streamRemote) streamRemote = null; + streamRemoteConnected = false + let videoElementRemote = document.getElementById(videoIdRemote) + videoElementRemote.pause() + if (streamRemote) streamRemote = null if (!NO_RESET) { - // videoElementRemote.removeAttribute('src'); // Chrome 71... - videoElementRemote.removeAttribute('srcObject'); - videoElementRemote.load(); + // videoElementRemote.removeAttribute('src') // Chrome 71... + videoElementRemote.removeAttribute('srcObject') + videoElementRemote.load() } if (!NO_MESSAGE && datachannelList && datachannelList['_default']) { const DATA = { @@ -661,8 +652,8 @@ const NeatRTC = (config, sendCallback) => { NO_RESET } } - datachannelList['_default'].send(JSON.stringify(DATA)); - log('MediaStreamStop message sent'); + datachannelList['_default'].send(JSON.stringify(DATA)) + log('MediaStreamStop message sent') } } } @@ -673,8 +664,8 @@ const NeatRTC = (config, sendCallback) => { * @param {object} message */ const send = (channel, message) => { - const DATA = message; - datachannelList[channel].send(JSON.stringify(DATA)); + const DATA = message + datachannelList[channel].send(JSON.stringify(DATA)) } return { @@ -687,4 +678,4 @@ const NeatRTC = (config, sendCallback) => { } } -export default NeatRTC; \ No newline at end of file +export default NeatRTC