Skip to content

Commit e8a83fb

Browse files
committed
Tweak broadcaster example
1 parent 56530ab commit e8a83fb

File tree

7 files changed

+49
-74
lines changed

7 files changed

+49
-74
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This project presents a few example applications using node-webrtc.
1616
[node-canvas](https://github.com/Automattic/node-canvas), and RTCVideoSource
1717
to draw spinning text on top of an incoming video.
1818
- [Stream recording](examples/stream-record) using ffmpeg and RTCVideoSink
19-
- SFU example with one [broadcaster](examples/sfu-broadcast) which forward stream to many [clients](examples/sfu-watch)
19+
- Broadcast example with one ["broadcaster"](examples/broadcaster) forwarding to many ["viewers"](examples/viewers)
2020

2121
Usage
2222
-----
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
const createExample = require('../../lib/browser/example');
44

5-
const description = 'Example of <a href="https://webrtcglossary.com/sfu/" target="_blank">SFU</a> <br/><br/>\
6-
Start broadcast and then your stream will be forward using RTCAudioSink, RTCVideoSink, RTCAudioSource, RTCVideoSource';
5+
const description = 'Start a broadcast. Your stream will be forwarded to \
6+
multiple "watchers". Although you can prototype such a system with \
7+
node-webrtc, you should consider using an \
8+
<a href="https://webrtcglossary.com/sfu/" target="_blank">SFU</a>.';
79

810
const localVideo = document.createElement('video');
911
localVideo.autoplay = true;
@@ -32,7 +34,7 @@ async function beforeAnswer(peerConnection) {
3234
};
3335
}
3436

35-
createExample('sfu-broadcast', description, { beforeAnswer });
37+
createExample('broadcaster', description, { beforeAnswer });
3638

3739
const videos = document.createElement('div');
3840
videos.className = 'grid';

examples/broadcaster/server.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const { EventEmitter } = require('events');
4+
5+
const broadcaster = new EventEmitter();
6+
const { on } = broadcaster;
7+
8+
function beforeOffer(peerConnection) {
9+
const audioTrack = broadcaster.audioTrack = peerConnection.addTransceiver('audio').receiver.track;
10+
const videoTrack = broadcaster.videoTrack = peerConnection.addTransceiver('video').receiver.track;
11+
12+
broadcaster.emit('newBroadcast', {
13+
audioTrack,
14+
videoTrack
15+
});
16+
17+
const { close } = peerConnection;
18+
peerConnection.close = function() {
19+
audioTrack.stop()
20+
videoTrack.stop()
21+
return close.apply(this, arguments);
22+
};
23+
}
24+
25+
module.exports = {
26+
beforeOffer,
27+
broadcaster
28+
};

examples/sfu-broadcast/server.js

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
const createExample = require('../../lib/browser/example');
44

5-
const description = 'Example of <a href="https://webrtcglossary.com/sfu/" target="_blank">SFU</a> <br/><br/>\
6-
Start receive sfu-broadcaster stream.';
5+
const description = 'Watch a broadcast. You should have already started the \
6+
broadcast example. Although you can prototype such a system with node-webrtc, \
7+
you should consider using an \
8+
<a href="https://webrtcglossary.com/sfu/" target="_blank">SFU</a>.';
79

810
const remoteVideo = document.createElement('video');
911
remoteVideo.autoplay = true;
@@ -22,7 +24,7 @@ async function beforeAnswer(peerConnection) {
2224
};
2325
}
2426

25-
createExample('sfu-watch', description, { beforeAnswer });
27+
createExample('viewer', description, { beforeAnswer });
2628

2729
const videos = document.createElement('div');
2830
videos.className = 'grid';
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
'use strict';
22

3-
const { event } = require('../sfu-broadcast/server')
3+
const { broadcaster } = require('../broadcaster/server')
44

55
function beforeOffer(peerConnection) {
66
const audioTransceiver = peerConnection.addTransceiver('audio');
77
const videoTransceiver = peerConnection.addTransceiver('video');
88

9-
const onNewBroadcast = ({ audioTrack, videoTrack })=>{
9+
function onNewBroadcast({ audioTrack, videoTrack }) {
1010
audioTransceiver.sender.replaceTrack(audioTrack),
1111
videoTransceiver.sender.replaceTrack(videoTrack)
12-
};
12+
}
13+
14+
broadcaster.on('newBroadcast', onNewBroadcast)
1315

14-
event.on('newBroadcast', onNewBroadcast)
16+
if (broadcaster.audioTrack && broadcaster.videoTrack) {
17+
onNewBroadcast(broadcaster);
18+
}
1519

1620
const { close } = peerConnection;
1721
peerConnection.close = function() {
18-
event.removeListener('newBroadcast', onNewBroadcast);
22+
broadcaster.removeListener('newBroadcast', onNewBroadcast);
1923
return close.apply(this, arguments);
2024
}
2125
}

html/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ <h1>node-webrtc examples</h1>
4848
<li><a href="/sine-wave-stereo/index.html">sine-wave-stereo
4949
<li><a href="/video-compositing/index.html">video-compositing
5050
<li><a href="/stream-record/index.html">record-audio-video-stream
51-
<li><a href="/sfu-broadcast/index.html">sfu-broadcast</a> & <a href="/sfu-watch/index.html">sfu-watch
51+
<li><a href="/broadcaster/index.html">broadcaster</a> &amp; <a href="/viewer/index.html">viewer
5252
<script src="index.js"></script>

0 commit comments

Comments
 (0)