Skip to content

Commit

Permalink
added video source switching
Browse files Browse the repository at this point in the history
  • Loading branch information
dusty-nv committed Apr 19, 2023
1 parent 6be5a51 commit 84cf88a
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 30 deletions.
40 changes: 33 additions & 7 deletions python/www/flask/static/webrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,38 @@ function playStream(url, videoElement) {
function sendStream(url, deviceId) {
console.log(`sending stream ${url} (deviceId=${deviceId})`);

connections[url] = {};

connections[url].type = 'outbound';
connections[url].deviceId = deviceId;
connections[url].webrtcConfig = { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }] };
if( url in connections && connections[url].type == 'outbound' ) {
// replace the outbound stream in the existing connection
replaceStream(url, deviceId);
return false;
}
else {
// create a new outbound connection
connections[url] = {};

connections[url].type = 'outbound';
connections[url].deviceId = deviceId;
connections[url].webrtcConfig = { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }] };

connections[url].websocket = new WebSocket(url);
connections[url].websocket.addEventListener('message', onServerMessage);

return true;
}
}

connections[url].websocket = new WebSocket(url);
connections[url].websocket.addEventListener('message', onServerMessage);
function replaceStream(url, deviceId) {
console.log(`replacing stream for outbound WebRTC connection to ${url}`);
console.log(`old device ID: ${connections[url].deviceId}`);
console.log(`new device ID: ${deviceId}`);

var constraints = {'audio': false, 'video': { deviceId: deviceId }};

navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
const [videoTrack] = stream.getVideoTracks();
const sender = connections[url].webrtcPeer.getSenders().find((s) => s.track.kind === videoTrack.kind);
console.log('found sender:', sender);
sender.replaceTrack(videoTrack);
connections[url].deviceId = deviceId;
}).catch(reportError);
}
4 changes: 2 additions & 2 deletions python/www/flask/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
};

send = function() {
sendStream(getWebsocketURL('input'), document.getElementById('send-stream').value);
play(); // autoplay browser stream
if( sendStream(getWebsocketURL('input'), document.getElementById('send-stream').value) )
play(); // autoplay browser stream
}

window.onload = function() {
Expand Down
4 changes: 2 additions & 2 deletions python/www/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
};

send = function() {
sendStream(getWebsocketURL('input'), document.getElementById('send-stream').value);
play(); // autoplay browser stream
if( sendStream(getWebsocketURL('input'), document.getElementById('send-stream').value) )
play(); // autoplay browser stream
}

window.onload = function() {
Expand Down
40 changes: 33 additions & 7 deletions python/www/html/webrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,38 @@ function playStream(url, videoElement) {
function sendStream(url, deviceId) {
console.log(`sending stream ${url} (deviceId=${deviceId})`);

connections[url] = {};

connections[url].type = 'outbound';
connections[url].deviceId = deviceId;
connections[url].webrtcConfig = { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }] };
if( url in connections && connections[url].type == 'outbound' ) {
// replace the outbound stream in the existing connection
replaceStream(url, deviceId);
return false;
}
else {
// create a new outbound connection
connections[url] = {};

connections[url].type = 'outbound';
connections[url].deviceId = deviceId;
connections[url].webrtcConfig = { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }] };

connections[url].websocket = new WebSocket(url);
connections[url].websocket.addEventListener('message', onServerMessage);

return true;
}
}

connections[url].websocket = new WebSocket(url);
connections[url].websocket.addEventListener('message', onServerMessage);
function replaceStream(url, deviceId) {
console.log(`replacing stream for outbound WebRTC connection to ${url}`);
console.log(`old device ID: ${connections[url].deviceId}`);
console.log(`new device ID: ${deviceId}`);

var constraints = {'audio': false, 'video': { deviceId: deviceId }};

navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
const [videoTrack] = stream.getVideoTracks();
const sender = connections[url].webrtcPeer.getSenders().find((s) => s.track.kind === videoTrack.kind);
console.log('found sender:', sender);
sender.replaceTrack(videoTrack);
connections[url].deviceId = deviceId;
}).catch(reportError);
}
40 changes: 33 additions & 7 deletions python/www/recognizer/static/webrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,38 @@ function playStream(url, videoElement) {
function sendStream(url, deviceId) {
console.log(`sending stream ${url} (deviceId=${deviceId})`);

connections[url] = {};

connections[url].type = 'outbound';
connections[url].deviceId = deviceId;
connections[url].webrtcConfig = { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }] };
if( url in connections && connections[url].type == 'outbound' ) {
// replace the outbound stream in the existing connection
replaceStream(url, deviceId);
return false;
}
else {
// create a new outbound connection
connections[url] = {};

connections[url].type = 'outbound';
connections[url].deviceId = deviceId;
connections[url].webrtcConfig = { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }] };

connections[url].websocket = new WebSocket(url);
connections[url].websocket.addEventListener('message', onServerMessage);

return true;
}
}

connections[url].websocket = new WebSocket(url);
connections[url].websocket.addEventListener('message', onServerMessage);
function replaceStream(url, deviceId) {
console.log(`replacing stream for outbound WebRTC connection to ${url}`);
console.log(`old device ID: ${connections[url].deviceId}`);
console.log(`new device ID: ${deviceId}`);

var constraints = {'audio': false, 'video': { deviceId: deviceId }};

navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
const [videoTrack] = stream.getVideoTracks();
const sender = connections[url].webrtcPeer.getSenders().find((s) => s.track.kind === videoTrack.kind);
console.log('found sender:', sender);
sender.replaceTrack(videoTrack);
connections[url].deviceId = deviceId;
}).catch(reportError);
}
11 changes: 6 additions & 5 deletions python/www/recognizer/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="/static/select2.min.css">

<script type='text/javascript' src="/static/jquery-3.6.3.min.js"></script>
<script type='text/javascript' src='/static/bootstrap.bundle.min.js'></script>
<script type='text/javascript' src="/static/select2.min.js"></script>
<script type='text/javascript' src='https://webrtc.github.io/adapter/adapter-latest.js'></script>
<script type='text/javascript' src='/static/webrtc.js'></script>
<script type='text/javascript' src='/static/rest.js'></script>
<script type='text/javascript' src='/static/debounce.js'></script>
<script type='text/javascript' src="/static/jquery-3.6.3.min.js"></script>
<script type='text/javascript' src='/static/bootstrap.bundle.min.js'></script>
<script type='text/javascript' src="/static/select2.min.js"></script>
<script type='text/javascript'>
play = function() {
playStream(document.getElementById('play-stream').value, document.getElementById('video-player'));
};

send = function() {
sendStream(getWebsocketURL('input'), document.getElementById('send-stream').value);
play(); // autoplay browser stream
if( sendStream(getWebsocketURL('input'), document.getElementById('send-stream').value) )
play(); // autoplay browser stream
}

window.onload = function() {
Expand Down

0 comments on commit 84cf88a

Please sign in to comment.