|
@ -9,7 +9,11 @@ |
|
|
</head> |
|
|
</head> |
|
|
<body> |
|
|
<body> |
|
|
<h1>Video Streaming Demonstration</h1> |
|
|
<h1>Video Streaming Demonstration</h1> |
|
|
<img src="{{ url_for('frontend.video_feed') }}"> |
|
|
{# <!--<img src="{{ url_for('frontend.video_feed') }}">--> #} |
|
|
|
|
|
|
|
|
|
|
|
<div id="video"> |
|
|
|
|
|
<video id="stream" autoplay playsinline muted>Your browser does not support video</video> |
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
|
<h2>Send:</h2> |
|
|
<h2>Send:</h2> |
|
|
<form id="emit" method="POST" action='#'> |
|
|
<form id="emit" method="POST" action='#'> |
|
@ -30,18 +34,121 @@ |
|
|
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script> |
|
|
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script> |
|
|
|
|
|
|
|
|
<script src="{{ url_for('static', filename='js/socket.io.js') }}"></script> |
|
|
<script src="{{ url_for('static', filename='js/socket.io.js') }}"></script> |
|
|
|
|
|
<!--<script type="text/javascript" src="https://webrtc.github.io/adapter/adapter-latest.js"></script>--> |
|
|
|
|
|
|
|
|
|
|
|
<script type="text/javascript"> |
|
|
|
|
|
'use strict'; |
|
|
|
|
|
var html5VideoElement; |
|
|
|
|
|
var wsUrl = "wss://" + window.location.hostname + ":" + window.location.port + "/webrtc"; |
|
|
|
|
|
var socket = io.connect(wsUrl, { autoConnect: false, transports: [ 'websocket' ] }); |
|
|
|
|
|
var webrtcPeerConnection; |
|
|
|
|
|
var webrtcConfiguration; |
|
|
|
|
|
var reportError; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function onLocalDescription(desc) { |
|
|
|
|
|
console.log("Local description: " + JSON.stringify(desc)); |
|
|
|
|
|
webrtcPeerConnection.setLocalDescription(desc).then(function() { |
|
|
|
|
|
socket.emit('message', { type: "sdp", "data": webrtcPeerConnection.localDescription }); |
|
|
|
|
|
}).catch(reportError); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function onIncomingSDP(sdp) { |
|
|
|
|
|
console.log("Incoming SDP: " + JSON.stringify(sdp)); |
|
|
|
|
|
webrtcPeerConnection.setRemoteDescription(sdp).catch(reportError); |
|
|
|
|
|
webrtcPeerConnection.createAnswer().then(onLocalDescription).catch(reportError); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function onIncomingICE(ice) { |
|
|
|
|
|
var candidate = new RTCIceCandidate(ice); |
|
|
|
|
|
console.log("Incoming ICE: " + JSON.stringify(ice)); |
|
|
|
|
|
webrtcPeerConnection.addIceCandidate(candidate).catch(reportError); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function onAddRemoteStream(event) { |
|
|
|
|
|
html5VideoElement.srcObject = event.streams[0]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function onIceCandidate(event) { |
|
|
|
|
|
if (event.candidate == null) |
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
console.log("Sending ICE candidate out: " + JSON.stringify(event.candidate)); |
|
|
|
|
|
socket.emit('message', { "type": "ice", "data": event.candidate }); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
socket.on('connect', function(){ |
|
|
|
|
|
console.log("Connected...!", socket.connected) |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
socket.on('message', (data) => { |
|
|
|
|
|
console.log("got message: ",data); |
|
|
|
|
|
var msg; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
msg = JSON.parse(data); |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
console.log("ERROR parsing message"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!webrtcPeerConnection) { |
|
|
|
|
|
webrtcPeerConnection = new RTCPeerConnection(webrtcConfiguration); |
|
|
|
|
|
webrtcPeerConnection.ontrack = onAddRemoteStream; |
|
|
|
|
|
webrtcPeerConnection.onicecandidate = onIceCandidate; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switch (msg.type) { |
|
|
|
|
|
case "sdp": onIncomingSDP(msg.data); break; |
|
|
|
|
|
case "ice": onIncomingICE(msg.data); break; |
|
|
|
|
|
default: break; |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
socket.on('data', (data) => { |
|
|
|
|
|
console.log('Data received: ',data); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
function playStream(videoElement, hostname, port, path, configuration, reportErrorCB) { |
|
|
|
|
|
var l = window.location; |
|
|
|
|
|
var wsHost = (hostname != undefined) ? hostname : l.hostname; |
|
|
|
|
|
var wsPort = (port != undefined) ? port : l.port; |
|
|
|
|
|
var wsPath = (path != undefined) ? path : "webrtc"; |
|
|
|
|
|
if (wsPort) |
|
|
|
|
|
wsPort = ":" + wsPort; |
|
|
|
|
|
var wsUrl = "wss://" + wsHost + wsPort + "/" + wsPath; |
|
|
|
|
|
|
|
|
|
|
|
html5VideoElement = videoElement; |
|
|
|
|
|
webrtcConfiguration = configuration; |
|
|
|
|
|
reportError = (reportErrorCB != undefined) ? reportErrorCB : function(text) {}; |
|
|
|
|
|
|
|
|
|
|
|
socket.connect(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
window.onload = function() { |
|
|
|
|
|
var vidstream = document.getElementById("stream"); |
|
|
|
|
|
var config = { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }] }; |
|
|
|
|
|
playStream(vidstream, null, null, null, config, function (errmsg) { console.error(errmsg); }); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
</script> |
|
|
|
|
|
|
|
|
<script type="text/javascript" charset="utf-8"> |
|
|
<script type="text/javascript" charset="utf-8"> |
|
|
$(document).ready(function(){ |
|
|
$(document).ready(function(){ |
|
|
var socket = io.connect('http://' + document.domain + ':' + location.port + '/test'); |
|
|
var test_socket = io.connect('wss://' + document.domain + ':' + location.port + '/test', { transports: [ 'websocket' ] }); |
|
|
socket.on('my response', function(msg) { |
|
|
test_socket.on('my response', function(msg) { |
|
|
$('#log').append('<p>Received: ' + msg.data + '</p>'); |
|
|
$('#log').append('<p>Received: ' + msg.data + '</p>'); |
|
|
}); |
|
|
}); |
|
|
$('form#emit').submit(function(event) { |
|
|
$('form#emit').submit(function(event) { |
|
|
socket.emit('my event', {data: $('#emit_data').val()}); |
|
|
test_socket.emit('my event', {data: $('#emit_data').val()}); |
|
|
return false; |
|
|
return false; |
|
|
}); |
|
|
}); |
|
|
$('form#broadcast').submit(function(event) { |
|
|
$('form#broadcast').submit(function(event) { |
|
|
socket.emit('my broadcast event', {data: $('#broadcast_data').val()}); |
|
|
test_socket.emit('my broadcast event', {data: $('#broadcast_data').val()}); |
|
|
return false; |
|
|
return false; |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|