Skip to content

Instantly share code, notes, and snippets.

@theleon
Created November 28, 2016 08:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save theleon/fb83624f3de5c9076eb76c76dc638722 to your computer and use it in GitHub Desktop.
Save theleon/fb83624f3de5c9076eb76c76dc638722 to your computer and use it in GitHub Desktop.
Flussonic WebRTC Publishing
<!doctype html>
<html>
<head>
<!-- https://github.com/webrtc/adapter is used for cross-browser interop -->
<script type="text/javascript" src="https://unpkg.com/webrtc-adapter@2.0.8/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