Skip to content

Commit d5a2e19

Browse files
committed
Tweak recording example
1 parent e8a83fb commit d5a2e19

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ This project presents a few example applications using node-webrtc.
1515
- [video-compositing](examples/video-compositing): uses RTCVideoSink,
1616
[node-canvas](https://github.com/Automattic/node-canvas), and RTCVideoSource
1717
to draw spinning text on top of an incoming video.
18-
- [Stream recording](examples/stream-record) using ffmpeg and RTCVideoSink
19-
- Broadcast example with one ["broadcaster"](examples/broadcaster) forwarding to many ["viewers"](examples/viewers)
18+
- [record-audio-video-stream](examples/record-audio-video-stream) using [FFmpeg](https://www.ffmpeg.org) and RTCVideoSink.
19+
- Broadcast example with one [broadcaster](examples/broadcaster) forwarding to many [viewers](examples/viewers).
2020

2121
Usage
2222
-----

‎examples/stream-record/client.js renamed to ‎examples/record-audio-video-stream/client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function beforeAnswer(peerConnection) {
3131
};
3232
}
3333

34-
createExample('stream-record', description, { beforeAnswer });
34+
createExample('record-audio-video-stream', description, { beforeAnswer });
3535

3636
const videos = document.createElement('div');
3737
videos.className = 'grid';

‎examples/stream-record/server.js renamed to ‎examples/record-audio-video-stream/server.js

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

33
const { PassThrough } = require('stream')
4+
const fs = require('fs')
45

56
const { RTCAudioSink, RTCVideoSink } = require('wrtc').nonstandard;
67

78
const ffmpeg = require('fluent-ffmpeg')
89
const { StreamInput } = require('fluent-ffmpeg-multistream')
9-
const fs = require('fs')
1010

1111
const VIDEO_OUTPUT_SIZE = '320x240'
1212
const VIDEO_OUTPUT_FILE = './recording.mp4'
@@ -22,9 +22,9 @@ function beforeOffer(peerConnection) {
2222

2323
const streams = [];
2424

25-
videoSink.addEventListener('frame', function({ frame: { width, height, data }}){
25+
videoSink.addEventListener('frame', ({ frame: { width, height, data }}) => {
2626
const size = width + 'x' + height;
27-
if(!streams[0] || (streams[0] && streams[0].size !== size)) {
27+
if (!streams[0] || (streams[0] && streams[0].size !== size)) {
2828
UID++;
2929

3030
const stream = {
@@ -34,24 +34,24 @@ function beforeOffer(peerConnection) {
3434
audio: new PassThrough()
3535
};
3636

37-
const onAudioData = function({ samples: { buffer } }) {
38-
if(!stream.end) {
37+
const onAudioData = ({ samples: { buffer } }) => {
38+
if (!stream.end) {
3939
stream.audio.push(Buffer.from(buffer));
4040
}
4141
};
4242

43-
audioSink.addEventListener('data', onAudioData)
43+
audioSink.addEventListener('data', onAudioData);
4444

45-
stream.audio.on('end', ()=>{
46-
audioSink.removeEventListener('data', onAudioData)
47-
})
45+
stream.audio.on('end', () => {
46+
audioSink.removeEventListener('data', onAudioData);
47+
});
4848

49-
streams.unshift(stream)
49+
streams.unshift(stream);
5050

5151
streams.forEach(item=>{
52-
if(item !== stream && !item.end) {
52+
if (item !== stream && !item.end) {
5353
item.end = true;
54-
if(item.audio) {
54+
if (item.audio) {
5555
item.audio.end();
5656
}
5757
item.video.end();
@@ -80,34 +80,34 @@ function beforeOffer(peerConnection) {
8080
console.log('Stop recording >> ', stream.recordPath)
8181
})
8282
.size(VIDEO_OUTPUT_SIZE)
83-
.output(stream.recordPath)
83+
.output(stream.recordPath);
8484

85-
stream.proc.run()
85+
stream.proc.run();
8686
}
8787

8888
streams[0].video.push(Buffer.from(data));
89-
})
89+
});
9090

9191
const { close } = peerConnection;
9292
peerConnection.close = function() {
9393
audioSink.stop();
9494
videoSink.stop();
9595

9696
streams.forEach(({ audio, video, end, proc, recordPath })=>{
97-
if(!end) {
98-
if(audio) {
97+
if (!end) {
98+
if (audio) {
9999
audio.end();
100100
}
101101
video.end();
102102
}
103-
})
103+
});
104104

105105
let totalEnd = 0;
106106
const timer = setInterval(()=>{
107107
streams.forEach(stream=>{
108-
if(stream.recordEnd) {
108+
if (stream.recordEnd) {
109109
totalEnd++;
110-
if(totalEnd === streams.length) {
110+
if (totalEnd === streams.length) {
111111
clearTimeout(timer);
112112

113113
const mergeProc = ffmpeg()
@@ -123,15 +123,15 @@ function beforeOffer(peerConnection) {
123123

124124
streams.forEach(({ recordPath })=>{
125125
mergeProc.addInput(recordPath)
126-
})
126+
});
127127

128128
mergeProc
129129
.output(VIDEO_OUTPUT_FILE)
130130
.run();
131131
}
132132
}
133-
})
134-
},1000)
133+
});
134+
}, 1000)
135135

136136
return close.apply(this, arguments);
137137
}

‎html/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ <h1>node-webrtc examples</h1>
4747
<li><a href="/sine-wave/index.html">sine-wave
4848
<li><a href="/sine-wave-stereo/index.html">sine-wave-stereo
4949
<li><a href="/video-compositing/index.html">video-compositing
50-
<li><a href="/stream-record/index.html">record-audio-video-stream
50+
<li><a href="/record-audio-video-stream/index.html">record-audio-video-stream
5151
<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)