Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
muaz-khan committed Jan 11, 2016
1 parent 42859d3 commit cc170df
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 30 deletions.
42 changes: 31 additions & 11 deletions RTCMultiConnection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Last time updated at Thursday, January 7th, 2016, 10:14:16 PM
// Last time updated at Monday, January 11th, 2016, 4:20:40 PM

// ______________________________
// RTCMultiConnection-v3.0 (Beta)
Expand Down Expand Up @@ -533,6 +533,12 @@
}],
mandatory: {}
};

if (isFirefox) {
connection.mediaConstraints.audio = {
deviceId: device.id
};
}
}

if (device.kind === 'videoinput') {
Expand All @@ -542,6 +548,12 @@
}],
mandatory: {}
};

if (isFirefox) {
connection.mediaConstraints.video = {
deviceId: device.id
};
}
}
})
});
Expand Down Expand Up @@ -771,18 +783,18 @@
});
};

function replaceTrack(track, remoteUserId) {
function replaceTrack(track, remoteUserId, isVideoTrack) {
if (remoteUserId) {
mPeer.replaceTrack(track, remoteUserId);
mPeer.replaceTrack(track, remoteUserId, isVideoTrack);
return;
}

connection.peers.getAllParticipants().forEach(function(participant) {
mPeer.replaceTrack(track, participant);
mPeer.replaceTrack(track, participant, isVideoTrack);
});
}

connection.replaceTrack = function(session) {
connection.replaceTrack = function(session, remoteUserId, isVideoTrack) {
session = session || {};

if (!RTCPeerConnection.prototype.getSenders) {
Expand All @@ -791,12 +803,12 @@
}

if (session instanceof MediaStreamTrack) {
replaceTrack(session);
replaceTrack(session, remoteUserId, isVideoTrack);
return;
}

if (session instanceof MediaStream) {
replaceTrack(session.getVideoTracks()[0]);
replaceTrack(session.getVideoTracks()[0], remoteUserId, isVideoTrack);
return;
}

Expand Down Expand Up @@ -830,7 +842,7 @@
return callback();
}

connection.replaceTrack(stream);
connection.replaceTrack(stream, remoteUserId, isVideoTrack || session.video || session.screen);
},
onLocalMediaError: function(error) {
mPeer.onLocalMediaError(error);
Expand Down Expand Up @@ -1719,15 +1731,23 @@
connection.peers[remoteUserId] = new PeerInitiator(localConfig);
};

this.replaceTrack = function(track, remoteUserId) {
this.replaceTrack = function(track, remoteUserId, isVideoTrack) {
if (!connection.peers[remoteUserId]) {
throw 'This peer (' + remoteUserId + ') does not exists.';
}

var peer = connection.peers[remoteUserId].peer;

if (!!peer.getSenders && typeof peer.getSenders === 'function' && peer.getSenders()[0] && peer.getSenders()[0].replaceTrack) {
peer.getSenders()[0].replaceTrack(track);
if (!!peer.getSenders && typeof peer.getSenders === 'function' && peer.getSenders().length) {
peer.getSenders().forEach(function(rtpSender) {
if (isVideoTrack && rtpSender.track instanceof VideoStreamTrack) {
rtpSender.replaceTrack(track);
}

if (!isVideoTrack && rtpSender.track instanceof AudioStreamTrack) {
rtpSender.replaceTrack(track);
}
});
return;
}

Expand Down
10 changes: 5 additions & 5 deletions RTCMultiConnection.min.js

Large diffs are not rendered by default.

68 changes: 65 additions & 3 deletions demos/Password-Protected-Rooms.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,32 @@ <h1>Password+Protected+Rooms using RTCMultiConnection-v3.0</h1>

<section class="experiment">
<div class="make-center">
<input type="text" id="room-id" value="abcdef">
Room-id:
<input type="text" id="room-id" value="abcdef"><br>

Password:
<input type="text" id="room-password" value="xyz">
<br>

<button id="open-room">Open Room</button>
<button id="join-room">Join Room</button>
<button id="open-or-join-room">Auto Open Or Join Room</button>

<br><br>
<input type="text" id="input-text-chat" placeholder="Enter Text Chat" disabled>
<button id="share-file" disabled>Share File</button>
</div>

<div id="chat-container">
<div id="file-container"></div>
<div class="chat-output"></div>
</div>

<div id="videos-container"></div>
</section>

<script src="/RTCMultiConnection.min.js"></script>
<script src="/dev/FileBufferReader.js"></script>

<!-- socket.io for signaling -->
<script src="/socket.io/socket.io.js"></script>
Expand All @@ -90,11 +106,12 @@ <h1>Password+Protected+Rooms using RTCMultiConnection-v3.0</h1>

var connection = new RTCMultiConnection();

document.getElementById('room-id').value = connection.token();
document.getElementById('room-id').value = document.getElementById('room-password').value = connection.token();

connection.session = {
audio: true,
video: true
video: true,
data: true
};

connection.sdpConstraints.mandatory = {
Expand All @@ -120,6 +137,51 @@ <h1>Password+Protected+Rooms using RTCMultiConnection-v3.0</h1>
connection.onPasswordMaxTriesOver = function(remoteUserId) {
alert(remoteUserId + ' is password protected. Your max password tries exceeded the limit.');
};

// ......................................................
// ................FileSharing/TextChat Code.............
// ......................................................

connection.enableFileSharing = true;

document.getElementById('share-file').onclick = function() {
var fileSelector = new FileSelector();
fileSelector.selectSingleFile(function(file) {
connection.send(file);
});
};

document.getElementById('input-text-chat').onkeyup = function(e) {
if (e.keyCode != 13) return;

// removing trailing/leading whitespace
this.value = this.value.replace(/^\s+|\s+$/g, '');
if (!this.value.length) return;

connection.send(this.value);
appendDIV(this.value);
this.value = '';
};

var chatContainer = document.querySelector('.chat-output');

function appendDIV(event) {
var div = document.createElement('div');
div.innerHTML = event.data || event;
chatContainer.insertBefore(div, chatContainer.firstChild);
div.tabIndex = 0;
div.focus();

document.getElementById('input-text-chat').focus();
}

connection.onmessage = appendDIV;
connection.filesContainer = document.getElementById('file-container');

connection.onopen = function() {
document.getElementById('share-file').disabled = false;
document.getElementById('input-text-chat').disabled = false;
};
</script>

<section class="experiment own-widgets latest-commits">
Expand Down
14 changes: 11 additions & 3 deletions dev/MultiPeersHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,23 @@ function MultiPeers(connection) {
connection.peers[remoteUserId] = new PeerInitiator(localConfig);
};

this.replaceTrack = function(track, remoteUserId) {
this.replaceTrack = function(track, remoteUserId, isVideoTrack) {
if (!connection.peers[remoteUserId]) {
throw 'This peer (' + remoteUserId + ') does not exists.';
}

var peer = connection.peers[remoteUserId].peer;

if (!!peer.getSenders && typeof peer.getSenders === 'function' && peer.getSenders()[0] && peer.getSenders()[0].replaceTrack) {
peer.getSenders()[0].replaceTrack(track);
if (!!peer.getSenders && typeof peer.getSenders === 'function' && peer.getSenders().length) {
peer.getSenders().forEach(function(rtpSender) {
if (isVideoTrack && rtpSender.track instanceof VideoStreamTrack) {
rtpSender.replaceTrack(track);
}

if (!isVideoTrack && rtpSender.track instanceof AudioStreamTrack) {
rtpSender.replaceTrack(track);
}
});
return;
}

Expand Down
26 changes: 19 additions & 7 deletions dev/RTCMultiConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,12 @@ function RTCMultiConnection(roomid) {
}],
mandatory: {}
};

if (isFirefox) {
connection.mediaConstraints.audio = {
deviceId: device.id
};
}
}

if (device.kind === 'videoinput') {
Expand All @@ -533,6 +539,12 @@ function RTCMultiConnection(roomid) {
}],
mandatory: {}
};

if (isFirefox) {
connection.mediaConstraints.video = {
deviceId: device.id
};
}
}
})
});
Expand Down Expand Up @@ -762,18 +774,18 @@ function RTCMultiConnection(roomid) {
});
};

function replaceTrack(track, remoteUserId) {
function replaceTrack(track, remoteUserId, isVideoTrack) {
if (remoteUserId) {
mPeer.replaceTrack(track, remoteUserId);
mPeer.replaceTrack(track, remoteUserId, isVideoTrack);
return;
}

connection.peers.getAllParticipants().forEach(function(participant) {
mPeer.replaceTrack(track, participant);
mPeer.replaceTrack(track, participant, isVideoTrack);
});
}

connection.replaceTrack = function(session) {
connection.replaceTrack = function(session, remoteUserId, isVideoTrack) {
session = session || {};

if (!RTCPeerConnection.prototype.getSenders) {
Expand All @@ -782,12 +794,12 @@ function RTCMultiConnection(roomid) {
}

if (session instanceof MediaStreamTrack) {
replaceTrack(session);
replaceTrack(session, remoteUserId, isVideoTrack);
return;
}

if (session instanceof MediaStream) {
replaceTrack(session.getVideoTracks()[0]);
replaceTrack(session.getVideoTracks()[0], remoteUserId, isVideoTrack);
return;
}

Expand Down Expand Up @@ -821,7 +833,7 @@ function RTCMultiConnection(roomid) {
return callback();
}

connection.replaceTrack(stream);
connection.replaceTrack(stream, remoteUserId, isVideoTrack || session.video || session.screen);
},
onLocalMediaError: function(error) {
mPeer.onLocalMediaError(error);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rtcmulticonnection-v3",
"preferGlobal": false,
"version": "3.2.80",
"version": "3.2.81",
"author": {
"name": "Muaz Khan",
"email": "[email protected]",
Expand Down

0 comments on commit cc170df

Please sign in to comment.