11import path from 'path' ;
2- import fs , { mkdirp , pathExists , writeFile } from 'fs-extra' ;
2+ import fs , { mkdirp , pathExists } from 'fs-extra' ;
33import type { ChildProcess } from 'child_process' ;
44import { spawn as nodeSpawn } from 'child_process' ;
55import { promisify } from 'util' ;
@@ -8,28 +8,6 @@ import fetch from 'node-fetch';
88
99const pipeline = promisify ( Stream . pipeline ) ;
1010
11- const TMP_SDP_DIR = '/tmp/smelter-examples' ;
12-
13- export async function ffplayStartRtpServerAsync (
14- ip : string ,
15- video_port : number ,
16- audio_port : number | undefined = undefined
17- ) : Promise < { spawn_promise : SpawnPromise } > {
18- await mkdirp ( TMP_SDP_DIR ) ;
19- let sdpFilePath ;
20- if ( audio_port === undefined ) {
21- sdpFilePath = path . join ( TMP_SDP_DIR , `video_input_${ video_port } .sdp` ) ;
22- await writeVideoSdpFile ( ip , video_port , sdpFilePath ) ;
23- } else {
24- sdpFilePath = path . join ( TMP_SDP_DIR , `video_audio_input_${ video_port } _${ audio_port } .sdp` ) ;
25- await writeVideoAudioSdpFile ( ip , video_port , audio_port , sdpFilePath ) ;
26- }
27-
28- const promise = spawn ( 'ffplay' , [ '-protocol_whitelist' , 'file,rtp,udp' , sdpFilePath ] ) ;
29- await sleep ( 2000 ) ;
30- return { spawn_promise : promise } ;
31- }
32-
3311export async function ffplayStartRtmpServerAsync (
3412 port : number
3513) : Promise < { spawn_promise : SpawnPromise } > {
@@ -41,37 +19,6 @@ export async function ffplayStartRtmpServerAsync(
4119 return { spawn_promise : promise } ;
4220}
4321
44- export async function gstReceiveTcpStream (
45- ip : string ,
46- port : number
47- ) : Promise < { spawn_promise : SpawnPromise } > {
48- const tcpReceiver = `tcpclientsrc host=${ ip } port=${ port } ! "application/x-rtp-stream" ! rtpstreamdepay ! queue ! demux.` ;
49- const videoPipe =
50- 'demux.src_96 ! "application/x-rtp,media=video,clock-rate=90000,encoding-name=H264" ! queue ! rtph264depay ! decodebin ! videoconvert ! autovideosink' ;
51- const audioPipe =
52- 'demux.src_97 ! "application/x-rtp,media=audio,clock-rate=48000,encoding-name=OPUS" ! queue ! rtpopusdepay ! decodebin ! audioconvert ! autoaudiosink ' ;
53- const gstCmd = `gst-launch-1.0 -v rtpptdemux name=demux ${ tcpReceiver } ${ videoPipe } ${ audioPipe } ` ;
54-
55- const promise = spawn ( 'bash' , [ '-c' , gstCmd ] ) ;
56- return { spawn_promise : promise } ;
57- }
58-
59- export function ffmpegSendVideoFromMp4 ( port : number , mp4Path : string ) : SpawnPromise {
60- return spawn ( 'ffmpeg' , [
61- '-stream_loop' ,
62- '-1' ,
63- '-re' ,
64- '-i' ,
65- mp4Path ,
66- '-an' ,
67- '-c:v' ,
68- 'libx264' ,
69- '-f' ,
70- 'rtp' ,
71- `rtp://127.0.0.1:${ port } ?rtcpport=${ port } ` ,
72- ] ) ;
73- }
74-
7522interface SpawnPromise extends Promise < void > {
7623 child : ChildProcess ;
7724}
@@ -96,46 +43,6 @@ function spawn(command: string, args: string[]): SpawnPromise {
9643 } ) as SpawnPromise ;
9744}
9845
99- async function writeVideoAudioSdpFile (
100- ip : string ,
101- video_port : number ,
102- audio_port : number ,
103- destination : string
104- ) : Promise < void > {
105- await writeFile (
106- destination ,
107- `
108- v=0
109- o=- 0 0 IN IP4 ${ ip }
110- s=No Name
111- c=IN IP4 ${ ip }
112- m=video ${ video_port } RTP/AVP 96
113- a=rtpmap:96 H264/90000
114- a=fmtp:96 packetization-mode=1
115- a=rtcp-mux
116- m=audio ${ audio_port } RTP/AVP 97
117- a=rtpmap:97 opus/48000/2
118- a=rtcp-mux
119- `
120- ) ;
121- }
122-
123- async function writeVideoSdpFile ( ip : string , port : number , destination : string ) : Promise < void > {
124- await writeFile (
125- destination ,
126- `
127- v=0
128- o=- 0 0 IN IP4 ${ ip }
129- s=No Name
130- c=IN IP4 ${ ip }
131- m=video ${ port } RTP/AVP 96
132- a=rtpmap:96 H264/90000
133- a=fmtp:96 packetization-mode=1
134- a=rtcp-mux
135- `
136- ) ;
137- }
138-
13946export async function sleep ( timeoutMs : number ) : Promise < void > {
14047 await new Promise < void > ( res => {
14148 setTimeout ( ( ) => {
0 commit comments