我在工作中接管了一个存储库。这是一个工作演示,包含一个 Web 服务器,该服务器通过 RTSP 中继服务器从媒体服务器(从 rtsp-simple-server Docker 镜像构建)接收视频和摄像头源,并将源流传输到客户端,所有这些都通过 Docker Compose 部署。
我正在尝试改用 WebRTC。自 2 年前创建演示以来,rtsp-simple-server 已升级为MediaMTX。这是更新后的 Docker Compose 配置的相关部分:
media-server:
image: bluenviron/mediamtx:latest-ffmpeg
expose:
- 8889
init: true
ports:
- 8889:8889
restart: unless-stopped
volumes:
- type: bind
source: ./demo/vids
target: /vids
- type: bind
source: ./demo/mediamtx.yml
target: /mediamtx.yml
MediaMTX自定义配置的相关部分在mediamtx.yml
:
###############################################
# Path settings
# Settings in "paths" are applied to specific paths, and the map key
# is the name of the path.
# Any setting in "pathDefaults" can be overridden here.
# It's possible to use regular expressions by using a tilde as prefix,
# for example "~^(test1|test2)$" will match both "test1" and "test2",
# for example "~^prefix" will match all paths that start with "prefix".
paths:
# example:
# my_camera:
# source: rtsp://my_camera
~^demo\d+$:
runOnDemand: ffmpeg -re -stream_loop -1 -i /vids/$MTX_PATH.mp4 -c:v libvpx -b:v 0 -crf 18 -qmin 18 -qmax 18 -f webm http://localhost:8889/$MTX_PATH/whip
# Settings under path "all_others" are applied to all paths that
# do not match another entry.
all_others:
我对 WebRTC 完全没有经验。这是我第一次听说这个协议,更不用说使用它了。据我了解,我需要将演示 mp4 视频(在之前的实现中通过 RTSP 成功流式传输)转换为兼容的视频编解码器,因此我选择了 VP8。在尝试将视频流式传输到我的网络服务器之前,我直接在浏览器中测试了流(使用最新版本的 Chrome 和 Edge 进行了尝试)。我去了
http://localhost:8889/demo0
(它应该将 demo0.mp4 转换为 VP8,然后通过 WebRTC 进行流式传输)。视频播放器已在浏览器中加载,但未收到视频数据且未播放任何内容。几秒钟后,屏幕显示“错误:错误状态代码 400,几秒钟后重试”。在浏览器控制台中,显示:无法加载资源:服务器响应状态为 400(错误请求)
在 MediaMTX 容器的运行时日志中,显示的内容如下:
2024-04-02 14:53:08 ffmpeg version 6.1.1 Copyright (c) 2000-2023 the FFmpeg developers
2024-04-02 14:53:08 built with gcc 13.2.1 (Alpine 13.2.1_git20231014) 20231014
2024-04-02 14:53:08 configuration: --prefix=/usr --disable-librtmp --disable-lzma --disable-static --disable-stripping --enable-avfilter --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libdrm --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-libmp3lame --enable-libopenmpt --enable-libopus --enable-libplacebo --enable-libpulse --enable-librav1e --enable-librist --enable-libsoxr --enable-libsrt --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-lto=auto --enable-lv2 --enable-openssl --enable-pic --enable-postproc --enable-pthreads --enable-shared --enable-vaapi --enable-vdpau --enable-version3 --enable-vulkan --optflags=-O3 --enable-libjxl --enable-libsvtav1 --enable-libvpl
2024-04-02 14:53:08 libavutil 58. 29.100 / 58. 29.100
2024-04-02 14:53:08 libavcodec 60. 31.102 / 60. 31.102
2024-04-02 14:53:08 libavformat 60. 16.100 / 60. 16.100
2024-04-02 14:53:08 libavdevice 60. 3.100 / 60. 3.100
2024-04-02 14:53:08 libavfilter 9. 12.100 / 9. 12.100
2024-04-02 14:53:08 libswscale 7. 5.100 / 7. 5.100
2024-04-02 14:53:08 libswresample 5. 0.100 / 5. 0.100
2024-04-02 14:53:08 libpostproc 57. 3.100 / 57. 3.100
2024-04-02 14:53:08 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/vids/demo0.mp4':
2024-04-02 14:53:08 Metadata:
2024-04-02 14:53:08 major_brand : isom
2024-04-02 14:53:08 minor_version : 512
2024-04-02 14:53:08 compatible_brands: isomiso2mp41
2024-04-02 14:53:08 encoder : Lavf58.76.100
2024-04-02 14:53:08 Duration: 00:00:03.47, start: 0.000000, bitrate: 1675 kb/s
2024-04-02 14:53:08 Stream #0:0[0x1](und): Video: mpeg1video (mp4v / 0x7634706D), yuv420p(tv, progressive), 640x360 [SAR 1:1 DAR 16:9], 104857 kb/s, 30 fps, 30 tbr, 90k tbn (default)
2024-04-02 14:53:08 Metadata:
2024-04-02 14:53:08 handler_name : VideoHandler
2024-04-02 14:53:08 vendor_id : [0][0][0][0]
2024-04-02 14:53:08 Side data:
2024-04-02 14:53:08 cpb: bitrate max/min/avg: 0/0/0 buffer size: 49152 vbv_delay: N/A
2024-04-02 14:53:08 Stream mapping:
2024-04-02 14:53:08 Stream #0:0 -> #0:0 (mpeg1video (native) -> vp8 (libvpx))
2024-04-02 14:53:08 Press [q] to stop, [?] for help
2024-04-02 14:53:08 [libvpx @ 0x7faa8591b8c0] v1.13.1
2024-04-02 14:53:08 [libvpx @ 0x7faa8591b8c0] Bitrate not specified for constrained quality mode, using default of 256kbit/sec
2024-04-02 14:53:08 Output #0, webm, to 'http://localhost:8889/demo0/whip':
2024-04-02 14:53:08 Metadata:
2024-04-02 14:53:08 major_brand : isom
2024-04-02 14:53:08 minor_version : 512
2024-04-02 14:53:08 compatible_brands: isomiso2mp41
2024-04-02 14:53:08 encoder : Lavf60.16.100
2024-04-02 14:53:08 Stream #0:0(und): Video: vp8, yuv420p(tv, progressive), 640x360 [SAR 1:1 DAR 16:9], q=2-31, 256 kb/s, 30 fps, 1k tbn (default)
2024-04-02 14:53:08 Metadata:
2024-04-02 14:53:08 handler_name : VideoHandler
2024-04-02 14:53:08 vendor_id : [0][0][0][0]
2024-04-02 14:53:08 encoder : Lavc60.31.102 libvpx
2024-04-02 14:53:08 Side data:
2024-04-02 14:53:08 cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
2024-04-02 14:53:18 2024/04/02 06:53:18 INF [path demo0] runOnDemand command stopped: timed out
2024-04-02 14:53:18 2024/04/02 06:53:18 INF [WebRTC] [session 0f460c76] closed: source of path 'demo0' has timed out
[out#0/webm @ 0x7faa859487c0] video:272kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.042856%
2024-04-02 14:53:18 frame= 315 fps= 32 q=18.0 Lsize= 275kB time=00:00:10.46 bitrate= 215.1kbits/s speed=1.05x
2024-04-02 14:53:18 Exiting normally, received signal 2.
我不确定这是什么意思?为什么服务器无法传输这个 3 秒、709kb 的视频一次?浏览器已成功连接到服务器和 URL,但没有传输任何数据。为了以防万一,我决定使用 ffmpeg 手动将所有 mp4 文件转换为 webm,并使用 Window 的媒体播放器验证 webm 视频是否有效。然后,我修改了 MediaMTX 的配置以直接流式传输 webm 视频:
paths:
# example:
# my_camera:
# source: rtsp://my_camera
~^demo\d+$:
runOnDemand: ffmpeg -re -stream_loop -1 -i /vids/$MTX_PATH.webm -c copy -f webm http://localhost:8889/$MTX_PATH/whip
但是,错误仍然存在:
2024-04-02 15:03:58 ffmpeg version 6.1.1 Copyright (c) 2000-2023 the FFmpeg developers
2024-04-02 15:03:58 built with gcc 13.2.1 (Alpine 13.2.1_git20231014) 20231014
2024-04-02 15:03:58 configuration: --prefix=/usr --disable-librtmp --disable-lzma --disable-static --disable-stripping --enable-avfilter --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libdrm --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-libmp3lame --enable-libopenmpt --enable-libopus --enable-libplacebo --enable-libpulse --enable-librav1e --enable-librist --enable-libsoxr --enable-libsrt --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-lto=auto --enable-lv2 --enable-openssl --enable-pic --enable-postproc --enable-pthreads --enable-shared --enable-vaapi --enable-vdpau --enable-version3 --enable-vulkan --optflags=-O3 --enable-libjxl --enable-libsvtav1 --enable-libvpl
2024-04-02 15:03:58 libavutil 58. 29.100 / 58. 29.100
2024-04-02 15:03:58 libavcodec 60. 31.102 / 60. 31.102
2024-04-02 15:03:58 libavformat 60. 16.100 / 60. 16.100
2024-04-02 15:03:58 libavdevice 60. 3.100 / 60. 3.100
2024-04-02 15:03:58 libavfilter 9. 12.100 / 9. 12.100
2024-04-02 15:03:58 libswscale 7. 5.100 / 7. 5.100
2024-04-02 15:03:58 libswresample 5. 0.100 / 5. 0.100
2024-04-02 15:03:58 libpostproc 57. 3.100 / 57. 3.100
2024-04-02 15:03:58 Input #0, matroska,webm, from '/vids/demo0.webm':
2024-04-02 15:03:58 Metadata:
2024-04-02 15:03:58 COMPATIBLE_BRANDS: isomiso2mp41
2024-04-02 15:03:58 MAJOR_BRAND : isom
2024-04-02 15:03:58 MINOR_VERSION : 512
2024-04-02 15:03:58 ENCODER : Lavf60.16.100
2024-04-02 15:03:58 Duration: 00:00:03.47, start: 0.000000, bitrate: 217 kb/s
2024-04-02 15:03:58 Stream #0:0: Video: vp8, yuv420p(tv, progressive), 640x360, SAR 1:1 DAR 16:9, 30 fps, 30 tbr, 1k tbn (default)
2024-04-02 15:03:58 Metadata:
2024-04-02 15:03:58 HANDLER_NAME : VideoHandler
2024-04-02 15:03:58 VENDOR_ID : [0][0][0][0]
2024-04-02 15:03:58 ENCODER : Lavc60.31.102 libvpx
2024-04-02 15:03:58 DURATION : 00:00:03.466000000
2024-04-02 15:03:58 Output #0, webm, to 'http://localhost:8889/demo0/whip':
2024-04-02 15:03:58 Metadata:
2024-04-02 15:03:58 COMPATIBLE_BRANDS: isomiso2mp41
2024-04-02 15:03:58 MAJOR_BRAND : isom
2024-04-02 15:03:58 MINOR_VERSION : 512
2024-04-02 15:03:58 encoder : Lavf60.16.100
2024-04-02 15:03:58 Stream #0:0: Video: vp8, yuv420p(tv, progressive), 640x360 [SAR 1:1 DAR 16:9], q=2-31, 30 fps, 30 tbr, 1k tbn (default)
2024-04-02 15:03:58 Metadata:
2024-04-02 15:03:58 HANDLER_NAME : VideoHandler
2024-04-02 15:03:58 VENDOR_ID : [0][0][0][0]
2024-04-02 15:03:58 ENCODER : Lavc60.31.102 libvpx
2024-04-02 15:03:58 DURATION : 00:00:03.466000000
2024-04-02 15:03:58 Stream mapping:
2024-04-02 15:03:58 Stream #0:0 -> #0:0 (copy)
2024-04-02 15:03:58 Press [q] to stop, [?] for help
2024-04-02 15:04:08 2024/04/02 07:04:08 INF [path demo0] runOnDemand command stopped: timed out
2024-04-02 15:04:08 2024/04/02 07:04:08 INF [WebRTC] [session 829664cb] closed: source of path 'demo0' has timed out
[out#0/webm @ 0x7f04b00515c0] video:281kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.023511%
2024-04-02 15:04:08 size= 284kB time=00:00:10.49 bitrate= 221.3kbits/s speed=1.05x
2024-04-02 15:04:08 Exiting normally, received signal 2.
当我尝试流式传输其他视频(demo1.mp4、demo2.mp4 等)时,情况也是如此。我做错了什么?
network_mode: "host"
。 流媒体使用 UDP 端口,您只为主网页打开一个端口。这是我的工作示例:
services:
mediamtx:
container_name: mediamtx
image: bluenviron/mediamtx:latest-ffmpeg
restart: unless-stopped
network_mode: "host"
volumes:
- $PWD/conf:/conf:ro
command: /conf/mediamtx.yaml
devices:
- /dev/video0:/dev/video0
和 yaml 配置
paths:
cam:
runOnInit: ffmpeg -f v4l2 -i /dev/video0 -c:v h264 -pix_fmt yuv420p -preset ultrafast -b:v 600k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH
runOnInitRestart: yes