-
-
Save chai2/df422819b4bb42a0f8dbf4a607f34a31 to your computer and use it in GitHub Desktop.
webrtc_adaptor changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/app/assets/javascripts/webrtc_adaptor.js b/app/assets/javascripts/webrtc_adaptor.js | |
index 94a91cf..5931a3d 100644 | |
--- a/app/assets/javascripts/webrtc_adaptor.js | |
+++ b/app/assets/javascripts/webrtc_adaptor.js | |
@@ -25,11 +25,6 @@ | |
this.isWebSocketTriggered = false; | |
this.webSocketAdaptor = null; | |
this.isPlayMode = false; | |
- this.audioContext = null; | |
- this.smallVideoTrack = null; | |
- this.videoTrack = null; | |
- this.audioTrack = null; | |
- this.onlyDataChannel = false; | |
this.debug = false; | |
this.composedStream = new MediaStream(); | |
@@ -99,38 +94,32 @@ | |
*/ | |
this.checkBrowserScreenShareSupported(); | |
- if (!this.isPlayMode && !this.onlyDataChannel && typeof this.mediaConstraints != "undefined" && this.localStream == null) | |
- { | |
- this.checkWebRTCPermissions(); | |
- | |
- // Get devices only in publish mode. | |
- //this.getDevices(); | |
- this.trackDeviceChange(); | |
- | |
- if (typeof this.mediaConstraints.video != "undefined" && this.mediaConstraints.video != false) | |
- { | |
- this.openStream(this.mediaConstraints, this.mode); | |
- } | |
- else { | |
- // get only audio | |
- var media_audio_constraint = { audio: this.mediaConstraints.audio }; | |
- this.navigatorUserMedia(media_audio_constraint , stream => { | |
- this.gotStream(stream); | |
- }, true) | |
- } | |
- } | |
- else { | |
- //just playing, it does not open any stream | |
- this.checkWebSocketConnection(); | |
- } | |
- } | |
+ if (!this.isPlayMode && typeof this.mediaConstraints != "undefined" && this.localStream == null) | |
+ { | |
+ if (typeof this.mediaConstraints.video != "undefined" && this.mediaConstraints.video != false) | |
+ { | |
+ this.openStream(this.mediaConstraints, this.mode); | |
+ } | |
+ else { | |
+ // get only audio | |
+ var media_audio_constraint = { audio: this.mediaConstraints.audio }; | |
+ this.navigatorUserMedia(media_audio_constraint , stream => { | |
+ this.gotStream(stream); | |
+ }, true) | |
+ } | |
+ } | |
+ else { | |
+ //just playing, it does not open any stream | |
+ if (this.webSocketAdaptor == null || this.webSocketAdaptor.isConnected() == false) { | |
+ this.webSocketAdaptor = new WebSocketAdaptor({websocket_url : this.websocket_url, webrtcadaptor : this, callback : this.callback, callbackError : this.callbackError}); | |
+ } | |
+ } | |
+ } | |
setDesktopwithCameraSource(stream, streamId, audioStream, onEndedCallback) | |
{ | |
this.desktopStream = stream; | |
this.navigatorUserMedia({video: true, audio: false},cameraStream => { | |
- | |
- this.smallVideoTrack = cameraStream.getVideoTracks()[0]; | |
- | |
+ | |
//create a canvas element | |
var canvas = document.createElement("canvas"); | |
var canvasContext = canvas.getContext("2d"); | |
@@ -184,11 +173,6 @@ | |
}, 66); | |
}, true) | |
} | |
- trackDeviceChange(){ | |
- navigator.mediaDevices.ondevicechange = () => { | |
- this.getDevices(); | |
- } | |
- } | |
getDevices(){ | |
navigator.mediaDevices.enumerateDevices().then(devices => { | |
let deviceArray = new Array(); | |
@@ -258,7 +242,6 @@ | |
} | |
this.gotStream(stream); | |
} | |
- this.checkWebSocketConnection(); | |
}, true) | |
} | |
else { | |
@@ -289,33 +272,10 @@ | |
*/ | |
getUserMedia(mediaConstraints, audioConstraint, streamId) | |
{ | |
- | |
- const resetTrack = (stream) => { | |
- let videoTracks = stream.getVideoTracks(); | |
- let audioTracks = stream.getAudioTracks(); | |
- | |
- if (videoTracks.length > 0) { | |
- if (this.videoTrack !== null) | |
- this.videoTrack.stop(); | |
- this.videoTrack = videoTracks[0]; | |
- } | |
- | |
- if (audioTracks.length > 0) { | |
- if (this.audioTrack !== null) | |
- this.audioTrack.stop(); | |
- this.audioTrack = audioTracks[0]; | |
- } | |
- | |
- if (this.smallVideoTrack) | |
- this.smallVideoTrack.stop(); | |
- return stream; | |
- } | |
- | |
// Check Media Constraint video value screen or screen + camera | |
if(this.publishMode == "screen+camera" || this.publishMode == "screen"){ | |
navigator.mediaDevices.getDisplayMedia(mediaConstraints) | |
.then(stream =>{ | |
- resetTrack(stream); | |
this.prepareStreamTracks(mediaConstraints,audioConstraint,stream, streamId); | |
}) | |
@@ -344,7 +304,6 @@ | |
// If mediaConstraints only user camera | |
else { | |
this.navigatorUserMedia(mediaConstraints, (stream =>{ | |
- resetTrack(stream); | |
this.prepareStreamTracks(mediaConstraints,audioConstraint,stream, streamId); | |
}),true); | |
} | |
@@ -384,38 +343,7 @@ | |
track.onended = null; | |
track.stop(); | |
}); | |
- if (this.videoTrack !== null) { | |
- this.videoTrack.stop(); | |
- } | |
- | |
- if (this.audioTrack !== null) { | |
- this.audioTrack.stop(); | |
- } | |
- | |
- if (this.smallVideoTrack !== null) { | |
- this.smallVideoTrack.stop(); | |
- } | |
} | |
- | |
- /* | |
- * Checks if we is permitted from browser | |
- */ | |
- checkWebRTCPermissions(){ | |
- if (!("WebSocket" in window)) { | |
- console.log("WebSocket not supported."); | |
- this.callbackError("WebSocketNotSupported"); | |
- return; | |
- } | |
- | |
- if (typeof navigator.mediaDevices == "undefined" && this.isPlayMode == false) { | |
- console.log("Cannot open camera and mic because of unsecure context. Please Install SSL(https)"); | |
- this.callbackError("UnsecureContext"); | |
- return; | |
- } | |
- if (typeof navigator.mediaDevices == "undefined" || navigator.mediaDevices == undefined || navigator.mediaDevices == null ) { | |
- this.callbackError("getUserMediaIsNotAllowed"); | |
- } | |
- } | |
/** | |
* Checks browser supports screen share feature | |
@@ -443,17 +371,8 @@ | |
publish(streamId, token) | |
{ | |
- if (this.onlyDataChannel) { | |
- var jsCmd = { | |
- command : "publish", | |
- streamId : streamId, | |
- token : token, | |
- video: false, | |
- audio: false, | |
- }; | |
- } | |
- //If it started with playOnly mode and wants to publish now | |
- else if(this.localStream == null){ | |
+ //If it started with playOnly mode and wants to publish now | |
+ if(this.localStream == null){ | |
this.navigatorUserMedia(this.mediaConstraints, (stream => { | |
this.gotStream(stream); | |
var jsCmd = { | |
@@ -461,7 +380,7 @@ | |
streamId : streamId, | |
token : token, | |
video: this.localStream.getVideoTracks().length > 0 ? true : false, | |
- audio: this.localStream.getAudioTracks().length > 0 ? true : false, | |
+ audio: this.localStream.getAudioTracks().length > 0 ? true : false, | |
}; | |
this.webSocketAdaptor.send(JSON.stringify(jsCmd)); | |
}), false); | |
@@ -471,7 +390,7 @@ | |
streamId : streamId, | |
token : token, | |
video: this.localStream.getVideoTracks().length > 0 ? true : false, | |
- audio: this.localStream.getAudioTracks().length > 0 ? true : false, | |
+ audio: this.localStream.getAudioTracks().length > 0 ? true : false, | |
}; | |
} | |
this.webSocketAdaptor.send(JSON.stringify(jsCmd)); | |
@@ -605,52 +524,13 @@ | |
gotStream(stream) | |
{ | |
this.localStream = stream; | |
- this.localVideo.srcObject = stream; | |
- this.checkWebSocketConnection(); | |
+ this.localVideo.srcObject = stream; | |
+ | |
+ if (this.webSocketAdaptor == null || this.webSocketAdaptor.isConnected() == false) { | |
+ this.webSocketAdaptor = new WebSocketAdaptor({websocket_url : this.websocket_url, webrtcadaptor : this, callback : this.callback, callbackError : this.callbackError}) | |
+ } | |
this.getDevices() | |
}; | |
- | |
- | |
- /** | |
- * Toggle video track on the server side. | |
- * | |
- * streamId is the id of the stream | |
- * trackId is the id of the track. streamId is also one of the trackId of the stream. If you are having just a single track on your | |
- * stream, you need to give streamId as trackId parameter as well. | |
- * enabled is the enable/disable video track. If it's true, server sends video track. If it's false, server does not send video | |
- | |
- */ | |
- toggleVideo(streamId, trackId, enabled) | |
- { | |
- var jsCmd = { | |
- command : "toggleVideo", | |
- streamId: streamId, | |
- trackId: trackId, | |
- enabled: enabled, | |
- }; | |
- this.webSocketAdaptor.send(JSON.stringify(jsCmd)); | |
- } | |
- | |
- /** | |
- * Toggle audio track on the server side. | |
- * | |
- * streamId is the id of the stream | |
- * trackId is the id of the track. streamId is also one of the trackId of the stream. If you are having just a single track on your | |
- * stream, you need to give streamId as trackId parameter as well. | |
- * enabled is the enable/disable video track. If it's true, server sends audio track. If it's false, server does not send audio | |
- * | |
- */ | |
- toggleAudio(streamId, trackId, enabled) | |
- { | |
- var jsCmd = { | |
- command : "toggleAudio", | |
- streamId: streamId, | |
- trackId: trackId, | |
- enabled: enabled, | |
- }; | |
- this.webSocketAdaptor.send(JSON.stringify(jsCmd)); | |
- } | |
- | |
switchDesktopCapture(streamId){ | |
this.publishMode = "screen"; | |
@@ -674,23 +554,23 @@ | |
composedStream.addTrack(videoTrack); | |
}); | |
- this.audioContext = new AudioContext(); | |
- var desktopSoundGainNode = this.audioContext.createGain(); | |
+ var audioContext = new AudioContext(); | |
+ var desktopSoundGainNode = audioContext.createGain(); | |
//Adjust the gain for screen sound | |
desktopSoundGainNode.gain.value = 1; | |
- var audioDestionation = this.audioContext.createMediaStreamDestination(); | |
- var audioSource = this.audioContext.createMediaStreamSource(stream); | |
+ var audioDestionation = audioContext.createMediaStreamDestination(); | |
+ var audioSource = audioContext.createMediaStreamSource(stream); | |
audioSource.connect(desktopSoundGainNode).connect(audioDestionation);; | |
- this.micGainNode = this.audioContext.createGain(); | |
+ this.micGainNode = audioContext.createGain(); | |
//Adjust the gain for microphone sound | |
this.micGainNode.gain.value = 0; | |
- var audioSource2 = this.audioContext.createMediaStreamSource(micStream); | |
+ var audioSource2 = audioContext.createMediaStreamSource(micStream); | |
audioSource2.connect(this.micGainNode).connect(audioDestionation);; | |
audioDestionation.stream.getAudioTracks().forEach(function(track) { | |
@@ -711,10 +591,7 @@ | |
} | |
if (typeof deviceId != "undefined" ) { | |
- if(this.mediaConstraints.audio !== true) | |
- this.mediaConstraints.audio.deviceId = deviceId; | |
- else | |
- this.mediaConstraints.audio = { "deviceId": deviceId }; | |
+ this.mediaConstraints.audio = { "deviceId": deviceId }; | |
} | |
this.setAudioInputSource(streamId, this.mediaConstraints, null, true, deviceId); | |
} | |
@@ -733,10 +610,7 @@ | |
this.publishMode = "camera"; | |
if (typeof deviceId != "undefined" ) { | |
- if(this.mediaConstraints.video !== true) | |
- this.mediaConstraints.video.deviceId = { exact: deviceId }; | |
- else | |
- this.mediaConstraints.video = { deviceId: { exact: deviceId } }; | |
+ this.mediaConstraints.video = { "deviceId": deviceId }; | |
} | |
this.setVideoCameraSource(streamId, this.mediaConstraints, null, true, deviceId); | |
} | |
@@ -758,27 +632,11 @@ | |
*/ | |
updateLocalAudioStream(stream, onEndedCallback) | |
{ | |
- var newAudioTrack = stream.getAudioTracks()[0]; | |
- | |
- if (this.localStream != null && this.localStream.getAudioTracks()[0] != null) | |
- { | |
- var audioTrack = this.localStream.getAudioTracks()[0]; | |
- this.localStream.removeTrack(audioTrack); | |
- audioTrack.stop(); | |
- this.localStream.addTrack(newAudioTrack); | |
- } | |
- else if(this.localStream != null){ | |
- this.localStream.addTrack(newAudioTrack); | |
- } | |
- else{ | |
- this.localStream = stream; | |
- } | |
- | |
- | |
- if (this.localVideo != null) | |
- { //it can be null | |
- this.localVideo.srcObject = this.localStream; | |
- } | |
+ var audioTrack = this.localStream.getAudioTracks()[0]; | |
+ this.localStream.removeTrack(audioTrack); | |
+ audioTrack.stop(); | |
+ this.localStream.addTrack(stream.getAudioTracks()[0]); | |
+ this.localVideo.srcObject = this.localStream; | |
if (onEndedCallback != null) { | |
stream.getAudioTracks()[0].onended = function(event) { | |
@@ -797,15 +655,10 @@ | |
this.desktopStream.getVideoTracks()[0].stop(); | |
} | |
- if(this.localStream != null){ | |
- var videoTrack = this.localStream.getVideoTracks()[0]; | |
- this.localStream.removeTrack(videoTrack); | |
- videoTrack.stop(); | |
- this.localStream.addTrack(stream.getVideoTracks()[0]); | |
- } | |
- else{ | |
- this.localStream = stream; | |
- } | |
+ var videoTrack = this.localStream.getVideoTracks()[0]; | |
+ this.localStream.removeTrack(videoTrack); | |
+ videoTrack.stop(); | |
+ this.localStream.addTrack(stream.getVideoTracks()[0]); | |
this.localVideo.srcObject = this.localStream; | |
if (onEndedCallback != null) { | |
@@ -1619,12 +1472,6 @@ | |
this.webSocketAdaptor.close(); | |
} | |
- checkWebSocketConnection() | |
- { | |
- if (this.webSocketAdaptor == null || this.webSocketAdaptor.isConnected() == false) { | |
- this.webSocketAdaptor = new WebSocketAdaptor({websocket_url : this.websocket_url, webrtcadaptor : this, callback : this.callback, callbackError : this.callbackError, debug : this.debug}); | |
- } | |
- } | |
peerMessage(streamId, definition, data) | |
{ | |
var jsCmd = { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment