Let's ensure that the implementation provides a functional live streaming system that allows a streamer to broadcast their webcam video to multiple viewers using WebRTC. I will provide detailed steps for setting up both the streamer and viewer pages to work correctly.
Complete Implementation of Streaming System
Step 1: Server Setup
Ensure you have the Node.js server up and running. If you've already set up the server in previous steps, you can keep it as is. Here's a recap to ensure all parts work together.
Server Code (server.js)
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
app.use(express.static('public'));
io.on('connection', (socket) => {
console.log('A user connected');
socket.on('offer', (offer) => {
socket.broadcast.emit('offer', offer);
});
socket.on('answer', (answer) => {
socket.broadcast.emit('answer', answer);
});
socket.on('ice-candidate', (candidate) => {
socket.broadcast.emit('ice-candidate', candidate);
});
socket.on('disconnect', () => {
console.log('A user disconnected');
});
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Step 2: Streamer Page
Create or replace streamer.html in the public directory:
public/streamer.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Streamer Page</title>
<style>
video {
width: 100%;
height: auto;
border: 1px solid black;
}
</style>
</head>
<body>
<h1>Streamer Page</h1>
<video id="video" autoplay muted></video>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
const video = document.getElementById('video');
const peerConnection = new RTCPeerConnection();
async function startStream() {
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
video.srcObject = stream;
stream.getTracks().forEach(track => {
peerConnection.addTrack(track, stream);
});
// Handle ICE Candidate exchange
peerConnection.onicecandidate = async (event) => {
if (event.candidate) {
socket.emit('ice-candidate', event.candidate);
}
};
// Create offer and send to viewer
const offer = await peerConnection.createOffer();
await peerConnection.setLocalDescription(offer);
socket.emit('offer', offer);
}
socket.on('answer', async (answer) => {
await peerConnection.setRemoteDescription(new RTCSessionDescription(answer));
});
// Start the streaming
startStream();
</script>
</body>
</html>
Step 3: Viewer Page
Create or replace viewer.html in the public directory:
public/viewer.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Viewer Page</title>
<style>
video {
width: 100%;
height: auto;
border: 1px solid black;
}
</style>
</head>
<body>
<h1>Viewer Page</h1>
<video id="video" autoplay></video>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
const video = document.getElementById('video');
const peerConnection = new RTCPeerConnection();
socket.on('offer', async (offer) => {
await peerConnection.setRemoteDescription(new RTCSessionDescription(offer));
const answer = await peerConnection.createAnswer();
await peerConnection.setLocalDescription(answer);
socket.emit('answer', answer);
});
peerConnection.onicecandidate = (event) => {
if (event.candidate) {
socket.emit('ice-candidate', event.candidate);
}
};
peerConnection.ontrack = (event) => {
video.srcObject = event.streams[0];
};
socket.on('ice-candidate', (candidate) => {
peerConnection.addIceCandidate(new RTCIceCandidate(candidate));
});
</script>
</body>
</html>
Step 4: Running the Application
-
Start the server:
node server.js
-
Open the streamer page in one tab:
- Go to
http://localhost:3000/streamer.html
- Allow camera access.
-
Open the viewer page in another tab:
- Go to
http://localhost:3000/viewer.html
- You may open this in a new tab or a different browser.
Debugging Steps
If the viewer can’t see the stream, try the following:
-
Network: Ensure that both the streamer and viewer are connected to the same local network (especially if they are on different devices).
-
Browser: Make sure you are using browsers that support WebRTC, such as the latest versions of Chrome, Firefox, or Edge.
-
Permissions: Check if both browser windows have permission to access the camera.
-
Console Logs: Inspect the developer console (F12) in both tabs. Look for any errors regarding WebRTC, ICE candidates, or console logs that might give you insight into what’s going wrong.
-
Cross-Origin Issues: Ensure that you're testing on localhost or a server configured with HTTPS if required. Browsers have strict policies regarding WebRTC with HTTP connections in many cases.
-
Refresh: Sometimes, refreshing both pages helps establish the connection properly.
Remarks
This implementation utilizes WebRTC correctly to facilitate the live streaming, ensuring that both the streamer and viewer have a working connection. If issues persist after running through these checks, please let me know so that we can troubleshoot further!