Created
November 28, 2016 08:04
-
-
Save theleon/fb83624f3de5c9076eb76c76dc638722 to your computer and use it in GitHub Desktop.
Flussonic WebRTC Publishing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<!-- https://github.com/webrtc/adapter is used for cross-browser interop --> | |
<script type="text/javascript" src="https://unpkg.com/[email protected]/out/adapter.js"></script> | |
</head> | |
<body> | |
<video id="container" controls autoplay></video> | |
<script> | |
window.onload = function() { | |
var websocket; | |
var peerConnection; | |
var video = document.getElementById("container"); | |
function sendWebSocketMessage(data) { | |
websocket.send(JSON.stringify(data)); | |
} | |
function onWebSocketMessage(event) { | |
var message = JSON.parse(event.data); | |
switch (message.type) { | |
case "answer": | |
var description = new window.RTCSessionDescription(message); | |
peerConnection.setRemoteDescription(description); | |
break; | |
case "candidate": | |
var candidate = new window.RTCIceCandidate(message.candidate); | |
peerConnection.addIceCandidate(candidate); | |
break; | |
} | |
} | |
function openWebSocketConnection(options) { | |
var url = | |
options.protocol + "://" + | |
options.server + ":" + | |
options.port + "/" + | |
options.stream + "/webrtc/publish"; | |
websocket = new WebSocket(url); | |
websocket.onopen = initPeerConnection; | |
websocket.onmessage = onWebSocketMessage; | |
} | |
function getMedia() { | |
return navigator.mediaDevices.getUserMedia({ | |
audio: true, | |
video: true | |
}); | |
} | |
function gotMedia(stream) { | |
video.srcObject = stream; | |
peerConnection.addStream(stream); | |
peerConnection.createOffer({ | |
"offerToReceiveAudio": true, | |
"offerToReceiveVideo":true | |
}).then(function(description) { | |
return peerConnection.setLocalDescription(description); | |
}).then(function() { | |
sendWebSocketMessage(peerConnection.localDescription) | |
}) | |
} | |
function initPeerConnection() { | |
peerConnection = new window.RTCPeerConnection(null); | |
peerConnection.stream_id = "local1"; | |
peerConnection.onicecandidate = gotIceCandidate; | |
getMedia().then(gotMedia); | |
} | |
function gotIceCandidate(event){ | |
var candidate = event.candidate; | |
if (candidate) { | |
sendWebSocketMessage({ | |
type: 'candidate', | |
stream_id : "local1", | |
label: candidate.sdpMLineIndex, | |
id: candidate.sdpMid, | |
candidate: candidate | |
}); | |
} | |
} | |
openWebSocketConnection({ | |
"protocol": "ws", | |
"server": "192.168.2.3", | |
"port": "8080", | |
"stream": "STREAMNAME" | |
}); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment