If you're new to WebRTC; please read this document.
|
Old WebRTC video broadcasting experiment was splitted in 5 separate files.
|
-
Helper.js - contains simple reusable code.
-
RTC-Functions.js - contains 3 functions to send offer/answer SDP, send ICE, and get remote stream.
-
ui.js - obviously user interface relevant code like create/join room buttons click event handlers and search private room events etc.
-
Answer-Socket.js - the person who joins your room is the "answerer"...this file is for him!!
-
Master-Socket.js - You, the master, who creates room. This file handles all broadcasting stuff!
|
Process was like this:
|
- Master socket creates a new room and starts broadcasting room over "main-public" channel for its lifetime.
Note: "Main-Public" channel is a default channel that is opened for all on page load.
- If a person wants to join your room; he will send you join request over "Main-Public" channel.
- Master socket will "open a new socket" and use participant's token (unique ID) as "channel name". Participant will also open "same channel"; so now both can transfer SDP/ICE over that "private" channel.
- Now, it is newly created sockets job to exchange SDP/ICE with participant and share "same client stream" with him.
|
In simple words; there are more than one sockets opened (using the power of socket.io multiplexing). Also, more than one peers are created to share "stream" with unlimited participants.
|
In Master-Socket.js file; following code opens new socket for each participant:
|
function masterSocket(channel, onopen) {
...
socket.master.on('connect', connect);
socket.master.on('message', callback);
...
function callback(data) {
...
if (data.participant) {
openSocket(data.userToken);
}
}
}
|
And here is "openSocket " function:
|
function openSocket(channel) {
...
socket.on('connect', opened);
socket.on('message', callback);
function opened() {
...
video = document.createElement('video');
...
peer = RTCPeerConnection(config);
}
...
function callback(response) {
...
if (response.firstPart || response.secondPart) { ... }
if (response.candidate && !isGotRemoteStream) {
peer && peer.addICE({
sdpMLineIndex: response.candidate.sdpMLineIndex,
candidate: JSON.parse(response.candidate.candidate)
});
}
...
}
function gotstream(...) { ... }
}
|