Skip to content

Commit

Permalink
Switch-Cameras and File-Sharing demos updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
muaz-khan committed Nov 22, 2016
1 parent 9191d3c commit caa6348
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 12 deletions.
16 changes: 8 additions & 8 deletions demos/file-sharing.html
Original file line number Diff line number Diff line change
Expand Up @@ -699,19 +699,12 @@
document.querySelector('.overlay').style.display = 'none';
iframe.style.display = 'block';

var fileNameMatches = (file.name || '').match(/.3gp|.mp3|.mp4|.m4a|.m3u8|.ts|.wav|.ogg|.aac|.flac|.mid|.xmf|.mxmf|.rtx|.mkv|.flv|.jpg|.jpeg|.png|.gif|.webp|.bmp|.webm|.txt|.rtf|.doc|docx|.js|.css|.cs/g);
if (file.type.match(/image|video|audio|pdf|txt|javascript|css|php|py/g) || fileNameMatches) {
iframe.src = URL.createObjectURL(file);
} else {
iframe.src = 'https://cdn.webrtc-experiment.com/images/folder-icon.png';
}

iframe.onload = function() {
Array.prototype.slice.call(iframe.contentWindow.document.body.querySelectorAll('*')).forEach(function(element) {
element.style.maxWidth = '100%';
});

if (!file.type || fileNameMatches || file.type.match(/image|video|audio|pdf/g) || iframe.src.indexOf('data:image/png') !== -1) {
if (!file.type || fileNameMatches || file.type.match(/image|video|audio|pdf/g) || iframe.src.indexOf('data:image/png') !== -1 || iframe.src.toLowerCase().search(/.png|.jpeg|.jpg|.gif/g) !== -1) {
iframe.contentWindow.document.body.style.textAlign = 'center';
iframe.contentWindow.document.body.style.background = 'black';
iframe.contentWindow.document.body.style.color = 'white';
Expand All @@ -721,6 +714,13 @@
iframe.contentWindow.document.body.style.background = 'white';
iframe.contentWindow.document.body.style.color = 'black';
};

var fileNameMatches = (file.name || '').toLowerCase().match(/.webm|.wav|.pdf|.txt|.js|.css|.cs|.png|.jpg|.jpeg|.gif/g);
if (fileNameMatches) {
iframe.src = URL.createObjectURL(file);
} else {
iframe.src = 'https://rtcxp.com/fs/unknown-file.png';
}
}

setupWebRTCConnection();
Expand Down
31 changes: 31 additions & 0 deletions demos/switch-cameras.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ <h1>Switch+Cameras using RTCMultiConnection</h1>

var videosContainer = document.getElementById('videos-container');
connection.onstream = function(event) {
event.mediaElement.id = '';

if(event.mediaElement) {
event.mediaElement.muted = true;
delete event.mediaElement;
Expand Down Expand Up @@ -234,6 +236,7 @@ <h1>Switch+Cameras using RTCMultiConnection</h1>

var video = document.createElement('video');
video.src = URL.createObjectURL(event.stream);

video.controls = true;
videoParentNode.appendChild(video);
video.play();
Expand All @@ -252,6 +255,34 @@ <h1>Switch+Cameras using RTCMultiConnection</h1>
}
};

connection.onSettingLocalDescription = function(event) {
var tries = 0;
(function looper() {
if(tries > 10) return; // try till 10 seconds
tries++;

connection.getAllParticipants().forEach(function(p) {
var div = document.getElementById(p);
if(!div) return;

var video = div.querySelector('video');
if(!video || !!video.currentTime) return;

video.src = URL.createObjectURL(connection.peers[p].peer.getRemoteStreams()[0]);
video.play();
});

setTimeout(looper, 1000);
})();
};

connection.onstreamended = function(event) {
var div = document.getElementById(event.userid);
if(div && div.parentNode) {
div.parentNode.removeChild(div);
}
};

var videoDevices = document.getElementById('video-devices');
var audioDevices = document.getElementById('audio-devices');

Expand Down
6 changes: 6 additions & 0 deletions dev/RTCMultiConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1586,4 +1586,10 @@ function RTCMultiConnection(roomid, forceOptions) {

connection.trickleIce = true;
connection.version = '@@version';

connection.onSettingLocalDescription = function(event) {
if (connection.enableLogs) {
console.info('Set local description for remote user', event.userid);
}
};
}
2 changes: 2 additions & 0 deletions dev/RTCPeerConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ function PeerInitiator(config) {
streamsToShare: streamsToShare,
isFirefoxOffered: isFirefox
});

connection.onSettingLocalDescription(self);
}, function(error) {
if (!connection.enableLogs) return;
console.error('setLocalDescription error', error);
Expand Down
10 changes: 9 additions & 1 deletion dist/RTCMultiConnection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Last time updated: 2016-11-16 2:15:21 PM UTC
// Last time updated: 2016-11-22 12:00:27 PM UTC

// _________________________
// RTCMultiConnection v3.4.2
Expand Down Expand Up @@ -1602,6 +1602,12 @@

connection.trickleIce = true;
connection.version = '3.4.2';

connection.onSettingLocalDescription = function(event) {
if (connection.enableLogs) {
console.info('Set local description for remote user', event.userid);
}
};
}

function SocketConnection(connection, connectCallback) {
Expand Down Expand Up @@ -4228,6 +4234,8 @@
streamsToShare: streamsToShare,
isFirefoxOffered: isFirefox
});

connection.onSettingLocalDescription(self);
}, function(error) {
if (!connection.enableLogs) return;
console.error('setLocalDescription error', error);
Expand Down
6 changes: 3 additions & 3 deletions dist/RTCMultiConnection.min.js

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,57 @@ By default, it is `false`. Which means that RTCMultiConnection will always get r
connection.dontGetRemoteStream = true;
```

## `onSettingLocalDescription`

This event is fired as soon as RTCMultiConnection calls the `nativePeer.setLocalDescription` method.

This event helps you say: "incoming call" or debug peers if connection didn't establish till next 3 seconds.

This method is helpful if you switch between cameras or you add screen or add other camera or change anything:

```javascript
connection.onSettingLocalDescription = fucntion(event) {
console.log('Trying to connect with', event.userid);

var nativePeer = event.peer;
var localStreams = nativePeer.getLocalStreams();
var remoteStreams = nativePeer.getRemoteStreams();

// make sure that you are correctly displaying all remote videos
var tries = 0;
(function looper() {
if(tries > 10) return; // throw error here

// make sure that each user's video.id == hisUserID
var video = document.getElementById(event.userid);

// skip: if user left or if user video is playing
if(!video || video.currentTIme > 0) return;

video.src = URL.createObjectURL ( nativePeer.getRemoteStreams()[0] );
video.play();

setTimeout(looper, 1000); // repeat till 10-seconds
})();
};
```

## `beforeAddingStream`

You can skip any stream or allow RTCMultiConnection to share a stream with remote users.

`nativePeer.addStream` method will be called only if below event permits the `MediaStream` object:

```javascript
connection.beforeAddingStream = function(stream) {
if(stream.id == 'any-streamid') return; // skip
if(stream.isScreen) return; // skip
if(stream.inactive) return; // skip

return stream; // otherwise allow RTCMultiConnection to share this stream with remote users
};
```

## `getScreenConstraints`

This method allows you get full control over screen-parameters:
Expand Down

0 comments on commit caa6348

Please sign in to comment.