Files

359 lines
16 KiB
HTML
Raw Permalink Normal View History

2026-11-17 16:15:01 -05:00
<!--
> Muaz Khan - wwww.MuazKhan.com
> MIT License - www.WebRTC-Experiment.com/licence
> Documentation - github.com/muaz-khan/WebRTC-Experiment/tree/master/video-broadcasting
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>WebRTC Video Broadcasting</title>
<script>
if(!location.hash.replace('#', '').length) {
location.href = location.href.split('#')[0] + '#' + (Math.random() * 100).toString().replace('.', '');
location.reload();
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel="author" type="text/html" href="https://plus.google.com/+MuazKhan">
<meta name="author" content="Muaz Khan">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="https://cdn.webrtc-experiment.com/style.css">
<style>
audio, video {
-moz-transition: all 1s ease;
-ms-transition: all 1s ease;
-o-transition: all 1s ease;
-webkit-transition: all 1s ease;
transition: all 1s ease;
vertical-align: top;
width: 100%;
}
input {
border: 1px solid #d9d9d9;
border-radius: 1px;
font-size: 2em;
margin: .2em;
width: 30%;
}
select {
border: 1px solid #d9d9d9;
border-radius: 1px;
height: 50px;
margin-left: 1em;
margin-right: -12px;
padding: 1.1em;
vertical-align: 6px;
width: 18%;
}
.setup {
border-bottom-left-radius: 0;
border-top-left-radius: 0;
font-size: 102%;
height: 47px;
margin-left: -9px;
margin-top: 8px;
position: absolute;
}
p { padding: 1em; }
li {
border-bottom: 1px solid rgb(189, 189, 189);
border-left: 1px solid rgb(189, 189, 189);
padding: .5em;
}
</style>
<script>
document.createElement('article');
document.createElement('footer');
</script>
<!-- This Library is used to detect WebRTC features -->
<script src="https://cdn.webrtc-experiment.com/DetectRTC.js"></script>
<script src="https://cdn.webrtc-experiment.com/socket.io.js"> </script>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script src="https://cdn.webrtc-experiment.com/IceServersHandler.js"></script>
<script src="https://cdn.webrtc-experiment.com/CodecsHandler.js"></script>
<script src="https://cdn.webrtc-experiment.com/RTCPeerConnection-v1.5.js"> </script>
<script src="https://cdn.webrtc-experiment.com/webrtc-broadcasting/broadcast.js"> </script>
</head>
<body>
<article>
<header style="text-align: center;">
<h1>
WebRTC Video Broadcasting
</h1>
<p>
<a href="https://www.webrtc-experiment.com/">HOME</a>
<span> &copy; </span>
<a href="http://www.MuazKhan.com/" target="_blank">Muaz Khan</a>
.
<a href="http://twitter.com/WebRTCWeb" target="_blank" title="Twitter profile for WebRTC Experiments">@WebRTCWeb</a>
.
<a href="https://github.com/muaz-khan?tab=repositories" target="_blank" title="Github Profile">Github</a>
.
<a href="https://github.com/muaz-khan/WebRTC-Experiment/issues?state=open" target="_blank">Latest issues</a>
.
<a href="https://github.com/muaz-khan/WebRTC-Experiment/commits/master" target="_blank">What's New?</a>
</p>
</header>
<div class="github-stargazers"></div>
<!-- copy this <section> and next <script> -->
<section class="experiment">
<section>
<select id="broadcasting-option">
<option>Audio + Video</option>
<option>Only Audio</option>
<option>Screen</option>
</select>
<input type="text" id="broadcast-name">
<button id="setup-new-broadcast" class="setup">Setup New Broadcast</button>
</section>
<!-- list of all available broadcasting rooms -->
<table style="width: 100%;" id="rooms-list"></table>
<!-- local/remote videos container -->
<div id="videos-container"></div>
</section>
<script>
// Muaz Khan - https://github.com/muaz-khan
// MIT License - https://www.webrtc-experiment.com/licence/
// Documentation - https://github.com/muaz-khan/WebRTC-Experiment/tree/master/webrtc-broadcasting
var config = {
openSocket: function(config) {
var SIGNALING_SERVER = 'https://socketio-over-nodejs2.herokuapp.com:443/';
var SIGNALING_SERVER = 'https://webrtcweb.com:9559/';
config.channel = config.channel || location.href.replace(/\/|:|#|%|\.|\[|\]/g, '');
var sender = Math.round(Math.random() * 999999999) + 999999999;
io.connect(SIGNALING_SERVER).emit('new-channel', {
channel: config.channel,
sender: sender
});
var socket = io.connect(SIGNALING_SERVER + config.channel);
socket.channel = config.channel;
socket.on('connect', function () {
if (config.callback) config.callback(socket);
});
socket.send = function (message) {
socket.emit('message', {
sender: sender,
data: message
});
};
socket.on('message', config.onmessage);
},
onRemoteStream: function(htmlElement) {
videosContainer.appendChild(htmlElement);
rotateInCircle(htmlElement);
},
onRoomFound: function(room) {
var alreadyExist = document.querySelector('button[data-broadcaster="' + room.broadcaster + '"]');
if (alreadyExist) return;
if (typeof roomsList === 'undefined') roomsList = document.body;
var tr = document.createElement('tr');
tr.innerHTML = '<td><strong>' + room.roomName + '</strong> is broadcasting his media!</td>' +
'<td><button class="join">Join</button></td>';
roomsList.appendChild(tr);
var joinRoomButton = tr.querySelector('.join');
joinRoomButton.setAttribute('data-broadcaster', room.broadcaster);
joinRoomButton.setAttribute('data-roomToken', room.broadcaster);
joinRoomButton.onclick = function() {
this.disabled = true;
var broadcaster = this.getAttribute('data-broadcaster');
var roomToken = this.getAttribute('data-roomToken');
broadcastUI.joinRoom({
roomToken: roomToken,
joinUser: broadcaster
});
hideUnnecessaryStuff();
};
},
onNewParticipant: function(numberOfViewers) {
document.title = 'Viewers: ' + numberOfViewers;
},
onReady: function() {
console.log('now you can open or join rooms');
}
};
function setupNewBroadcastButtonClickHandler() {
document.getElementById('broadcast-name').disabled = true;
document.getElementById('setup-new-broadcast').disabled = true;
DetectRTC.load(function() {
captureUserMedia(function() {
var shared = 'video';
if (window.option == 'Only Audio') {
shared = 'audio';
}
if (window.option == 'Screen') {
shared = 'screen';
}
broadcastUI.createRoom({
roomName: (document.getElementById('broadcast-name') || { }).value || 'Anonymous',
isAudio: shared === 'audio'
});
});
hideUnnecessaryStuff();
});
}
function captureUserMedia(callback) {
var constraints = null;
window.option = broadcastingOption ? broadcastingOption.value : '';
if (option === 'Only Audio') {
constraints = {
audio: true,
video: false
};
if(DetectRTC.hasMicrophone !== true) {
alert('DetectRTC library is unable to find microphone; maybe you denied microphone access once and it is still denied or maybe microphone device is not attached to your system or another app is using same microphone.');
}
}
if (option === 'Screen') {
var video_constraints = {
mandatory: {
chromeMediaSource: 'screen'
},
optional: []
};
constraints = {
audio: false,
video: video_constraints
};
if(DetectRTC.isScreenCapturingSupported !== true) {
alert('DetectRTC library is unable to find screen capturing support. You MUST run chrome with command line flag "chrome --enable-usermedia-screen-capturing"');
}
}
if (option != 'Only Audio' && option != 'Screen' && DetectRTC.hasWebcam !== true) {
alert('DetectRTC library is unable to find webcam; maybe you denied webcam access once and it is still denied or maybe webcam device is not attached to your system or another app is using same webcam.');
}
var htmlElement = document.createElement(option === 'Only Audio' ? 'audio' : 'video');
htmlElement.muted = true;
htmlElement.volume = 0;
try {
htmlElement.setAttributeNode(document.createAttribute('autoplay'));
htmlElement.setAttributeNode(document.createAttribute('playsinline'));
htmlElement.setAttributeNode(document.createAttribute('controls'));
} catch (e) {
htmlElement.setAttribute('autoplay', true);
htmlElement.setAttribute('playsinline', true);
htmlElement.setAttribute('controls', true);
}
var mediaConfig = {
video: htmlElement,
onsuccess: function(stream) {
config.attachStream = stream;
videosContainer.appendChild(htmlElement);
rotateInCircle(htmlElement);
callback && callback();
},
onerror: function() {
if (option === 'Only Audio') alert('unable to get access to your microphone');
else if (option === 'Screen') {
if (location.protocol === 'http:') alert('Please test this WebRTC experiment on HTTPS.');
else alert('Screen capturing is either denied or not supported. Are you enabled flag: "Enable screen capture support in getUserMedia"?');
} else alert('unable to get access to your webcam');
}
};
if (constraints) mediaConfig.constraints = constraints;
getUserMedia(mediaConfig);
}
var broadcastUI = broadcast(config);
/* UI specific */
var videosContainer = document.getElementById('videos-container') || document.body;
var setupNewBroadcast = document.getElementById('setup-new-broadcast');
var roomsList = document.getElementById('rooms-list');
var broadcastingOption = document.getElementById('broadcasting-option');
if (setupNewBroadcast) setupNewBroadcast.onclick = setupNewBroadcastButtonClickHandler;
function hideUnnecessaryStuff() {
var visibleElements = document.getElementsByClassName('visible'),
length = visibleElements.length;
for (var i = 0; i < length; i++) {
visibleElements[i].style.display = 'none';
}
}
function rotateInCircle(video) {
video.style[navigator.mozGetUserMedia ? 'transform' : '-webkit-transform'] = 'rotate(0deg)';
setTimeout(function() {
video.style[navigator.mozGetUserMedia ? 'transform' : '-webkit-transform'] = 'rotate(360deg)';
}, 1000);
}
</script>
<section class="experiment">
<h2 class="header">How it works?</h2>
<ol>
<li>It broadcasts media in one-way direction over number of peers.</li>
<li>All peers can view/watch/listen the broadcast, anonymously.</li>
<li>"Anonymously" means viewers will NEVER be prompted to allow webcam or mic or screen.</li>
<li>Assume like a radio station inside the browser. Remember, it works only browser-to-browser.</li>
</ol>
</section>
<section class="experiment"><small id="send-message"></small></section>
</article>
<a href="https://github.com/muaz-khan/WebRTC-Experiment/tree/master/webrtc-broadcasting" class="fork-left"></a>
<footer>
<p>
<a href="https://www.webrtc-experiment.com/">WebRTC Experiments</a>
© <a href="https://plus.google.com/+MuazKhan" rel="author" target="_blank">Muaz Khan</a>
<a href="mailto:muazkh@gmail.com" target="_blank">muazkh@gmail.com</a>
</p>
</footer>
<!-- commits.js is useless for you! It is not part of this WebRTC Experiment. -->
<script src="https://cdn.webrtc-experiment.com/commits.js" async> </script>
</body>
</html>