Skip to content

Commit be5bbd3

Browse files
committed
Fix FFmpeg tests
1 parent 3f94a75 commit be5bbd3

File tree

1 file changed

+132
-74
lines changed

1 file changed

+132
-74
lines changed

internal/ffmpeg/ffmpeg_test.go

Lines changed: 132 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestParseArgsFile(t *testing.T) {
3131
{
3232
name: "[FILE] video will be transcoded to H265 and rotate 270º, audio will be skipped",
3333
source: "/media/bbb.mp4#video=h265#rotate=-90",
34-
expect: `ffmpeg -hide_banner -re -i /media/bbb.mp4 -c:v libx265 -g 50 -profile:v main -level:v 5.1 -preset:v superfast -tune:v zerolatency -an -vf "transpose=2" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
34+
expect: `ffmpeg -hide_banner -re -i /media/bbb.mp4 -c:v libx265 -g 50 -profile:v main -level:v 5.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -vf "transpose=2" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
3535
},
3636
{
3737
name: "[FILE] video will be output for MJPEG to pipe, audio will be skipped",
@@ -53,85 +53,143 @@ func TestParseArgsFile(t *testing.T) {
5353
}
5454

5555
func TestParseArgsDevice(t *testing.T) {
56-
// [DEVICE] video will be output for MJPEG to pipe, with size 1920x1080
57-
args := parseArgs("device?video=0&video_size=1920x1080")
58-
require.Equal(t, `ffmpeg -hide_banner -f dshow -video_size 1920x1080 -i "video=0" -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
59-
60-
// [DEVICE] video will be transcoded to H265 with framerate 20, audio will be skipped
61-
//args = parseArgs("device?video=0&video_size=1280x720&framerate=20#video=h265#audio=pcma")
62-
args = parseArgs("device?video=0&framerate=20#video=h265")
63-
require.Equal(t, `ffmpeg -hide_banner -f dshow -framerate 20 -i "video=0" -c:v libx265 -g 50 -profile:v main -level:v 5.1 -preset:v superfast -tune:v zerolatency -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
64-
65-
args = parseArgs("device?video=FaceTime HD Camera&audio=Microphone (High Definition Audio Device)")
66-
require.Equal(t, `ffmpeg -hide_banner -f dshow -i "video=FaceTime HD Camera:audio=Microphone (High Definition Audio Device)" -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
56+
tests := []struct {
57+
name string
58+
source string
59+
expect string
60+
}{
61+
{
62+
name: "[DEVICE] video will be output for MJPEG to pipe, with size 1920x1080",
63+
source: "device?video=0&video_size=1920x1080",
64+
expect: `ffmpeg -hide_banner -f dshow -video_size 1920x1080 -i "video=0" -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
65+
},
66+
{
67+
name: "[DEVICE] video will be transcoded to H265 with framerate 20, audio will be skipped",
68+
source: "device?video=0&framerate=20#video=h265",
69+
expect: `ffmpeg -hide_banner -f dshow -framerate 20 -i "video=0" -c:v libx265 -g 50 -profile:v main -level:v 5.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
70+
},
71+
{
72+
name: "[DEVICE] video/audio",
73+
source: "device?video=FaceTime HD Camera&audio=Microphone (High Definition Audio Device)",
74+
expect: `ffmpeg -hide_banner -f dshow -i "video=FaceTime HD Camera:audio=Microphone (High Definition Audio Device)" -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
75+
},
76+
}
77+
for _, test := range tests {
78+
t.Run(test.name, func(t *testing.T) {
79+
args := parseArgs(test.source)
80+
require.Equal(t, test.expect, args.String())
81+
})
82+
}
6783
}
6884

6985
func TestParseArgsIpCam(t *testing.T) {
70-
// [HTTP] video will be copied
71-
args := parseArgs("http://example.com")
72-
require.Equal(t, `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
73-
74-
// [HTTP-MJPEG] video will be transcoded to H264
75-
args = parseArgs("http://example.com#video=h264")
76-
require.Equal(t, `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http://example.com -c:v libx264 -g 50 -profile:v high -level:v 4.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
77-
78-
// [HLS] video will be copied, audio will be skipped
79-
args = parseArgs("https://example.com#video=copy")
80-
require.Equal(t, `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i https://example.com -c:v copy -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
81-
82-
// [RTSP] video will be copied without transcoding codecs
83-
args = parseArgs("rtsp://example.com")
84-
require.Equal(t, `ffmpeg -hide_banner -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
85-
86-
// [RTSP] video with resize to 1280x720, should be transcoded, so select H265
87-
args = parseArgs("rtsp://example.com#video=h265#width=1280#height=720")
88-
require.Equal(t, `ffmpeg -hide_banner -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v libx265 -g 50 -profile:v main -level:v 5.1 -preset:v superfast -tune:v zerolatency -an -vf "scale=1280:720" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
89-
90-
// [RTSP] video will be copied, changing RTSP transport from TCP to UDP+TCP
91-
args = parseArgs("rtsp://example.com#input=rtsp/udp")
92-
require.Equal(t, `ffmpeg -hide_banner -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -i rtsp://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
93-
94-
// [RTMP] video will be copied, changing RTSP transport from TCP to UDP+TCP
95-
args = parseArgs("rtmp://example.com#input=rtsp/udp")
96-
require.Equal(t, `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -i rtmp://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
86+
tests := []struct {
87+
name string
88+
source string
89+
expect string
90+
}{
91+
{
92+
name: "[HTTP] video will be copied",
93+
source: "http://example.com",
94+
expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
95+
},
96+
{
97+
name: "[HTTP-MJPEG] video will be transcoded to H264",
98+
source: "http://example.com#video=h264",
99+
expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http://example.com -c:v libx264 -g 50 -profile:v high -level:v 4.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
100+
},
101+
{
102+
name: "[HLS] video will be copied, audio will be skipped",
103+
source: "https://example.com#video=copy",
104+
expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i https://example.com -c:v copy -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
105+
},
106+
{
107+
name: "[RTSP] video will be copied without transcoding codecs",
108+
source: "rtsp://example.com",
109+
expect: `ffmpeg -hide_banner -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
110+
},
111+
{
112+
name: "[RTSP] video with resize to 1280x720, should be transcoded, so select H265",
113+
source: "rtsp://example.com#video=h265#width=1280#height=720",
114+
expect: `ffmpeg -hide_banner -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v libx265 -g 50 -profile:v main -level:v 5.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -vf "scale=1280:720" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
115+
},
116+
{
117+
name: "[RTSP] video will be copied, changing RTSP transport from TCP to UDP+TCP",
118+
source: "rtsp://example.com#input=rtsp/udp",
119+
expect: `ffmpeg -hide_banner -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -i rtsp://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
120+
},
121+
{
122+
name: "[RTMP] video will be copied, changing RTSP transport from TCP to UDP+TCP",
123+
source: "rtmp://example.com#input=rtsp/udp",
124+
expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -i rtmp://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
125+
},
126+
}
127+
for _, test := range tests {
128+
t.Run(test.name, func(t *testing.T) {
129+
args := parseArgs(test.source)
130+
require.Equal(t, test.expect, args.String())
131+
})
132+
}
97133
}
98134

99135
func TestParseArgsAudio(t *testing.T) {
100-
// [AUDIO] audio will be transcoded to AAC, video will be skipped
101-
args := parseArgs("rtsp:///example.com#audio=aac")
102-
require.Equal(t, `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp:///example.com -c:a aac -vn -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
103-
104-
// [AUDIO] audio will be transcoded to AAC/16000, video will be skipped
105-
args = parseArgs("rtsp:///example.com#audio=aac/16000")
106-
require.Equal(t, `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp:///example.com -c:a aac -ar:a 16000 -ac:a 1 -vn -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
107-
108-
// [AUDIO] audio will be transcoded to OPUS, video will be skipped
109-
args = parseArgs("rtsp:///example.com#audio=opus")
110-
require.Equal(t, `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp:///example.com -c:a libopus -application:a lowdelay -min_comp 0 -vn -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
111-
112-
// [AUDIO] audio will be transcoded to PCMU, video will be skipped
113-
args = parseArgs("rtsp:///example.com#audio=pcmu")
114-
require.Equal(t, `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp:///example.com -c:a pcm_mulaw -ar:a 8000 -ac:a 1 -vn -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
115-
116-
// [AUDIO] audio will be transcoded to PCMU/16000, video will be skipped
117-
args = parseArgs("rtsp:///example.com#audio=pcmu/16000")
118-
require.Equal(t, `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp:///example.com -c:a pcm_mulaw -ar:a 16000 -ac:a 1 -vn -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
119-
120-
// [AUDIO] audio will be transcoded to PCMU/48000, video will be skipped
121-
args = parseArgs("rtsp:///example.com#audio=pcmu/48000")
122-
require.Equal(t, `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp:///example.com -c:a pcm_mulaw -ar:a 48000 -ac:a 1 -vn -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
123-
124-
// [AUDIO] audio will be transcoded to PCMA, video will be skipped
125-
args = parseArgs("rtsp:///example.com#audio=pcma")
126-
require.Equal(t, `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp:///example.com -c:a pcm_alaw -ar:a 8000 -ac:a 1 -vn -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
127-
128-
// [AUDIO] audio will be transcoded to PCMA/16000, video will be skipped
129-
args = parseArgs("rtsp:///example.com#audio=pcma/16000")
130-
require.Equal(t, `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp:///example.com -c:a pcm_alaw -ar:a 16000 -ac:a 1 -vn -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
131-
132-
// [AUDIO] audio will be transcoded to PCMA/48000, video will be skipped
133-
args = parseArgs("rtsp:///example.com#audio=pcma/48000")
134-
require.Equal(t, `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp:///example.com -c:a pcm_alaw -ar:a 48000 -ac:a 1 -vn -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
136+
tests := []struct {
137+
name string
138+
source string
139+
expect string
140+
}{
141+
{
142+
name: "[AUDIO] audio will be transcoded to AAC, video will be skipped",
143+
source: "rtsp://example.com#audio=aac",
144+
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a aac -vn -f adts -`,
145+
},
146+
{
147+
name: "[AUDIO] audio will be transcoded to AAC/16000, video will be skipped",
148+
source: "rtsp://example.com#audio=aac/16000",
149+
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a aac -ar:a 16000 -ac:a 1 -vn -f adts -`,
150+
},
151+
{
152+
name: "[AUDIO] audio will be transcoded to OPUS, video will be skipped",
153+
source: "rtsp://example.com#audio=opus",
154+
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a libopus -application:a lowdelay -min_comp 0 -vn -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
155+
},
156+
{
157+
name: "[AUDIO] audio will be transcoded to PCMU, video will be skipped",
158+
source: "rtsp://example.com#audio=pcmu",
159+
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_mulaw -ar:a 8000 -ac:a 1 -vn -f wav -`,
160+
},
161+
{
162+
name: "[AUDIO] audio will be transcoded to PCMU/16000, video will be skipped",
163+
source: "rtsp://example.com#audio=pcmu/16000",
164+
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_mulaw -ar:a 16000 -ac:a 1 -vn -f wav -`,
165+
},
166+
{
167+
name: "[AUDIO] audio will be transcoded to PCMU/48000, video will be skipped",
168+
source: "rtsp://example.com#audio=pcmu/48000",
169+
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_mulaw -ar:a 48000 -ac:a 1 -vn -f wav -`,
170+
},
171+
{
172+
name: "[AUDIO] audio will be transcoded to PCMA, video will be skipped",
173+
source: "rtsp://example.com#audio=pcma",
174+
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_alaw -ar:a 8000 -ac:a 1 -vn -f wav -`,
175+
},
176+
{
177+
name: "[AUDIO] audio will be transcoded to PCMA/16000, video will be skipped",
178+
source: "rtsp://example.com#audio=pcma/16000",
179+
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_alaw -ar:a 16000 -ac:a 1 -vn -f wav -`,
180+
},
181+
{
182+
name: "[AUDIO] audio will be transcoded to PCMA/48000, video will be skipped",
183+
source: "rtsp://example.com#audio=pcma/48000",
184+
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_alaw -ar:a 48000 -ac:a 1 -vn -f wav -`,
185+
},
186+
}
187+
for _, test := range tests {
188+
t.Run(test.name, func(t *testing.T) {
189+
args := parseArgs(test.source)
190+
require.Equal(t, test.expect, args.String())
191+
})
192+
}
135193
}
136194

137195
func TestParseArgsHwVaapi(t *testing.T) {

0 commit comments

Comments
 (0)