Skip to content

Commit

Permalink
Released [email protected] Fixed many important bugs.
Browse files Browse the repository at this point in the history
getAudioTracks/getVideoTracks/createObjectURL/unified-plan/plan-b

"addTrack" is now default and is the only supporetd option.
"ontrack" is now default and is the only supporetd option.

Which means that following items are removed:
1) nativePeer.addStream
2) nativePeer.onaddstream
This is happening internally. Codes will be untouched.
  • Loading branch information
muaz-khan committed Dec 24, 2018
1 parent fb70be2 commit 2228262
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 247 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rtcmulticonnection",
"description": "RTCMultiConnection is a WebRTC JavaScript wrapper library runs top over RTCPeerConnection API to provide multi-session establishment scenarios.",
"version": "3.6.1",
"version": "3.6.3",
"authors": [
{
"name": "Muaz Khan",
Expand Down
2 changes: 1 addition & 1 deletion dev/MultiPeersHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function MultiPeers(connection) {
self.shareFile(data, remoteUserId);
return;
}

if (typeof data !== 'string') {
data = JSON.stringify(data);
}
Expand Down
28 changes: 18 additions & 10 deletions dev/RTCMultiConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,11 @@
};

connection.processSdp = function(sdp) {
// ignore SDP modification if unified-pan is supported
if (isUnifiedPlanSupportedDefault()) {
return sdp;
}

if (DetectRTC.browser.name === 'Safari') {
return sdp;
}
Expand Down Expand Up @@ -832,7 +837,10 @@
}]
};

connection.rtcpMuxPolicy = 'require'; // "require" or "negotiate"
connection.sdpSemantics = null; // "unified-plan" or "plan-b", ref: webrtc.org/web-apis/chrome/unified-plan/
connection.iceCandidatePoolSize = null; // 0
connection.bundlePolicy = null; // max-bundle
connection.rtcpMuxPolicy = null; // "require" or "negotiate"
connection.iceTransportPolicy = null; // "relay" or "all"
connection.optionalArgument = {
optional: [{
Expand Down Expand Up @@ -990,7 +998,7 @@
};

connection.addStream = function(session, remoteUserId) {
if (!!session.getAudioTracks) {
if (!!session.getTracks) {
if (connection.attachStreams.indexOf(session) === -1) {
if (!session.streamid) {
session.streamid = session.id;
Expand Down Expand Up @@ -1092,8 +1100,8 @@
}

if (!stream.isScreen) {
stream.isVideo = stream.getVideoTracks().length;
stream.isAudio = !stream.isVideo && stream.getAudioTracks().length;
stream.isVideo = !!getTracks(stream, 'video').length;
stream.isAudio = !stream.isVideo && getTracks(stream, 'audio').length;
}

mPeer.onGettingLocalMedia(stream, function() {
Expand Down Expand Up @@ -1121,13 +1129,13 @@
}

if (mediaConstraints.audio) {
stream.getAudioTracks().forEach(function(track) {
getTracks(stream, 'audio').forEach(function(track) {
track.applyConstraints(mediaConstraints.audio);
});
}

if (mediaConstraints.video) {
stream.getVideoTracks().forEach(function(track) {
getTracks(stream, 'video').forEach(function(track) {
track.applyConstraints(mediaConstraints.video);
});
}
Expand Down Expand Up @@ -1178,12 +1186,12 @@
}

if (session instanceof MediaStream) {
if (session.getVideoTracks().length) {
replaceTrack(session.getVideoTracks()[0], remoteUserId, true);
if (getTracks(session, 'video').length) {
replaceTrack(getTracks(session, 'video')[0], remoteUserId, true);
}

if (session.getAudioTracks().length) {
replaceTrack(session.getAudioTracks()[0], remoteUserId, false);
if (getTracks(session, 'audio').length) {
replaceTrack(getTracks(session, 'audio')[0], remoteUserId, false);
}
return;
}
Expand Down
132 changes: 61 additions & 71 deletions dev/RTCPeerConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,6 @@ function setSdpConstraints(config) {
OfferToReceiveVideo: !!config.OfferToReceiveVideo
};

var oldBrowser = !window.enableAdapter;

if (DetectRTC.browser.name === 'Chrome' && DetectRTC.browser.version >= 60) {
// oldBrowser = false;
}

if (DetectRTC.browser.name === 'Firefox' && DetectRTC.browser.version >= 54) {
oldBrowser = false;
}

if (oldBrowser) {
sdpConstraints = {
mandatory: sdpConstraints,
optional: [{
VoiceActivityDetection: false
}]
};
}

return sdpConstraints;
}

Expand Down Expand Up @@ -112,23 +93,29 @@ function PeerInitiator(config) {
}

try {
var params = {};
// ref: developer.mozilla.org/en-US/docs/Web/API/RTCConfiguration
var params = {
iceServers: connection.iceServers,
iceTransportPolicy: connection.iceTransportPolicy || iceTransports
};

if (typeof connection.iceCandidatePoolSize !== 'undefined') {
params.iceCandidatePoolSize = connection.iceCandidatePoolSize;
}

if (typeof connection.bundlePolicy !== 'undefined') {
params.bundlePolicy = connection.bundlePolicy;
}

if (DetectRTC.browser.name !== 'Chrome') {
params.iceServers = connection.iceServers;
if (typeof connection.rtcpMuxPolicy !== 'undefined') {
params.rtcpMuxPolicy = connection.rtcpMuxPolicy;
}

if (DetectRTC.browser.name === 'Chrome') {
params = {
iceServers: connection.iceServers,
iceTransportPolicy: connection.iceTransportPolicy || iceTransports,
// rtcpMuxPolicy: connection.rtcpMuxPolicy || 'require', // or negotiate
// bundlePolicy: 'max-bundle',
// iceCandidatePoolSize: connection.iceCandidatePoolSize || 0
};
params.sdpSemantics = connection.sdpSemantics || 'unified-plan';
}

if (!connection.iceServers.length) {
if (!connection.iceServers || !connection.iceServers.length) {
params = null;
connection.optionalArgument = null;
}
Expand Down Expand Up @@ -214,24 +201,14 @@ function PeerInitiator(config) {
}
});

if (localStream && typeof peer.addTrack === 'function') {
if (localStream && localStream.getTracks) {
localStream.getTracks().forEach(function(track) {
try {
// last parameter is redundant for unified-plan
// starting from chrome version 72
peer.addTrack(track, localStream);
} catch (e) {}
});
} else if (localStream && typeof peer.addStream === 'function') {
peer.addStream(localStream);
} else {
try {
peer.addStream(localStream);
} catch (e) {
localStream && localStream.getTracks().forEach(function(track) {
try {
peer.addTrack(track, localStream);
} catch (e) {}
});
}
}
});

Expand Down Expand Up @@ -278,20 +255,24 @@ function PeerInitiator(config) {
var streamObject;
var dontDuplicate = {};

var incomingStreamEvent = 'track';
peer.ontrack = function(event) {
if (!event || event.type !== 'track') return;

if (!window.enableAdapter) {
incomingStreamEvent = 'addstream';
}
event.stream = event.streams[event.streams.length - 1];

peer.addEventListener(incomingStreamEvent, function(event) {
if (!event) return;
if (!event.stream.id) {
event.stream.id = event.track.id;
}

if (incomingStreamEvent === 'track') {
event.stream = event.streams[event.streams.length - 1];
if (dontDuplicate[event.stream.id] && DetectRTC.browser.name !== 'Safari') {
if (event.track) {
event.track.onended = function() { // event.track.onmute =
peer.onremovestream(event);
};
}
return;
}

if (dontDuplicate[event.stream.id] && DetectRTC.browser.name !== 'Safari') return;
dontDuplicate[event.stream.id] = event.stream.id;

var streamsToShare = {};
Expand All @@ -307,25 +288,26 @@ function PeerInitiator(config) {
event.stream.isVideo = streamToShare.isVideo;
event.stream.isScreen = streamToShare.isScreen;
} else {
event.stream.isVideo = !!event.stream.getVideoTracks().length;
event.stream.isVideo = !!getTracks(event.stream, 'video').length;
event.stream.isAudio = !event.stream.isVideo;
event.stream.isScreen = false;
}

event.stream.streamid = event.stream.id;
if (DetectRTC.browser.name == 'Firefox' || !event.stream.stop) {
event.stream.stop = function() {
var streamEndedEvent = 'ended';

if ('oninactive' in event.stream) {
streamEndedEvent = 'inactive';
}
fireEvent(event.stream, streamEndedEvent);
};
}
allRemoteStreams[event.stream.id] = event.stream;
config.onRemoteStream(event.stream);
}, false);

event.stream.getTracks().forEach(function(track) {
track.onended = function() { // track.onmute =
peer.onremovestream(event);
};
});

event.stream.onremovetrack = function() {
peer.onremovestream(event);
};
};

peer.onremovestream = function(event) {
// this event doesn't works anymore
Expand All @@ -338,6 +320,15 @@ function PeerInitiator(config) {
config.onRemoteStreamRemoved(event.stream);
};

if (typeof peer.removeStream !== 'function') {
// removeStream backward compatibility
peer.removeStream = function(stream) {
stream.getTracks().forEach(function(track) {
peer.removeTrack(track, stream);
});
};
}

this.addRemoteCandidate = function(remoteCandidate) {
peer.addIceCandidate(new RTCIceCandidate(remoteCandidate));
};
Expand All @@ -360,18 +351,21 @@ function PeerInitiator(config) {
this.addRemoteSdp = function(remoteSdp, cb) {
cb = cb || function() {};

if (!window.enableAdapter) {
return oldAddRemoteSdp(remoteSdp, cb);
}

if (DetectRTC.browser.name !== 'Safari') {
remoteSdp.sdp = connection.processSdp(remoteSdp.sdp);
}

peer.setRemoteDescription(new RTCSessionDescription(remoteSdp)).then(cb, function(error) {
if (!!connection.enableLogs) {
console.error('setRemoteDescription failed', '\n', error, '\n', remoteSdp.sdp);
}

cb();
}).catch(function(error) {
if (!!connection.enableLogs) {
console.error('setRemoteDescription failed', '\n', error, '\n', remoteSdp.sdp);
}

cb();
});
};
Expand Down Expand Up @@ -505,10 +499,6 @@ function PeerInitiator(config) {
}

function createOfferOrAnswer(_method) {
if (!window.enableAdapter) {
return oldCreateOfferOrAnswer(_method);
}

peer[_method](defaults.sdpConstraints).then(function(localSdp) {
if (DetectRTC.browser.name !== 'Safari') {
localSdp.sdp = connection.processSdp(localSdp.sdp);
Expand Down
8 changes: 4 additions & 4 deletions dev/StreamsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ var StreamsHandler = (function() {
}

if (typeof type == 'undefined' || type == 'audio') {
stream.getAudioTracks().forEach(function(track) {
getTracks(stream, 'audio').forEach(function(track) {
track.enabled = false;
connection.streamEvents[stream.streamid].isAudioMuted = true;
});
}

if (typeof type == 'undefined' || type == 'video') {
stream.getVideoTracks().forEach(function(track) {
getTracks(stream, 'video').forEach(function(track) {
track.enabled = false;
});
}
Expand All @@ -79,14 +79,14 @@ var StreamsHandler = (function() {
graduallyIncreaseVolume();

if (typeof type == 'undefined' || type == 'audio') {
stream.getAudioTracks().forEach(function(track) {
getTracks(stream, 'audio').forEach(function(track) {
track.enabled = true;
connection.streamEvents[stream.streamid].isAudioMuted = false;
});
}

if (typeof type == 'undefined' || type == 'video') {
stream.getVideoTracks().forEach(function(track) {
getTracks(stream, 'video').forEach(function(track) {
track.enabled = true;
});

Expand Down
Loading

0 comments on commit 2228262

Please sign in to comment.