1
1
'use strict' ;
2
2
3
3
const { PassThrough } = require ( 'stream' )
4
+ const fs = require ( 'fs' )
4
5
5
6
const { RTCAudioSink, RTCVideoSink } = require ( 'wrtc' ) . nonstandard ;
6
7
7
8
const ffmpeg = require ( 'fluent-ffmpeg' )
8
9
const { StreamInput } = require ( 'fluent-ffmpeg-multistream' )
9
- const fs = require ( 'fs' )
10
10
11
11
const VIDEO_OUTPUT_SIZE = '320x240'
12
12
const VIDEO_OUTPUT_FILE = './recording.mp4'
@@ -22,9 +22,9 @@ function beforeOffer(peerConnection) {
22
22
23
23
const streams = [ ] ;
24
24
25
- videoSink . addEventListener ( 'frame' , function ( { frame : { width, height, data } } ) {
25
+ videoSink . addEventListener ( 'frame' , ( { frame : { width, height, data } } ) => {
26
26
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 ) ) {
28
28
UID ++ ;
29
29
30
30
const stream = {
@@ -34,24 +34,24 @@ function beforeOffer(peerConnection) {
34
34
audio : new PassThrough ( )
35
35
} ;
36
36
37
- const onAudioData = function ( { samples : { buffer } } ) {
38
- if ( ! stream . end ) {
37
+ const onAudioData = ( { samples : { buffer } } ) => {
38
+ if ( ! stream . end ) {
39
39
stream . audio . push ( Buffer . from ( buffer ) ) ;
40
40
}
41
41
} ;
42
42
43
- audioSink . addEventListener ( 'data' , onAudioData )
43
+ audioSink . addEventListener ( 'data' , onAudioData ) ;
44
44
45
- stream . audio . on ( 'end' , ( ) => {
46
- audioSink . removeEventListener ( 'data' , onAudioData )
47
- } )
45
+ stream . audio . on ( 'end' , ( ) => {
46
+ audioSink . removeEventListener ( 'data' , onAudioData ) ;
47
+ } ) ;
48
48
49
- streams . unshift ( stream )
49
+ streams . unshift ( stream ) ;
50
50
51
51
streams . forEach ( item => {
52
- if ( item !== stream && ! item . end ) {
52
+ if ( item !== stream && ! item . end ) {
53
53
item . end = true ;
54
- if ( item . audio ) {
54
+ if ( item . audio ) {
55
55
item . audio . end ( ) ;
56
56
}
57
57
item . video . end ( ) ;
@@ -80,34 +80,34 @@ function beforeOffer(peerConnection) {
80
80
console . log ( 'Stop recording >> ' , stream . recordPath )
81
81
} )
82
82
. size ( VIDEO_OUTPUT_SIZE )
83
- . output ( stream . recordPath )
83
+ . output ( stream . recordPath ) ;
84
84
85
- stream . proc . run ( )
85
+ stream . proc . run ( ) ;
86
86
}
87
87
88
88
streams [ 0 ] . video . push ( Buffer . from ( data ) ) ;
89
- } )
89
+ } ) ;
90
90
91
91
const { close } = peerConnection ;
92
92
peerConnection . close = function ( ) {
93
93
audioSink . stop ( ) ;
94
94
videoSink . stop ( ) ;
95
95
96
96
streams . forEach ( ( { audio, video, end, proc, recordPath } ) => {
97
- if ( ! end ) {
98
- if ( audio ) {
97
+ if ( ! end ) {
98
+ if ( audio ) {
99
99
audio . end ( ) ;
100
100
}
101
101
video . end ( ) ;
102
102
}
103
- } )
103
+ } ) ;
104
104
105
105
let totalEnd = 0 ;
106
106
const timer = setInterval ( ( ) => {
107
107
streams . forEach ( stream => {
108
- if ( stream . recordEnd ) {
108
+ if ( stream . recordEnd ) {
109
109
totalEnd ++ ;
110
- if ( totalEnd === streams . length ) {
110
+ if ( totalEnd === streams . length ) {
111
111
clearTimeout ( timer ) ;
112
112
113
113
const mergeProc = ffmpeg ( )
@@ -123,15 +123,15 @@ function beforeOffer(peerConnection) {
123
123
124
124
streams . forEach ( ( { recordPath } ) => {
125
125
mergeProc . addInput ( recordPath )
126
- } )
126
+ } ) ;
127
127
128
128
mergeProc
129
129
. output ( VIDEO_OUTPUT_FILE )
130
130
. run ( ) ;
131
131
}
132
132
}
133
- } )
134
- } , 1000 )
133
+ } ) ;
134
+ } , 1000 )
135
135
136
136
return close . apply ( this , arguments ) ;
137
137
}
0 commit comments