‘liusuyi’
2023-09-28 33aed56cfbaad870dbf4780688ba6b08d0ee806c
ard-work/src/main/java/com/ruoyi/media/service/impl/MediaServiceImpl.java
@@ -71,7 +71,7 @@
    @Override
    public Map<String,String> addPath(String name, String sourceUrl, String mode, String isCode) {
        String rtspUrl = "rtsp://" + mediamtxHost + ":7554/" + name;
        String rtspUrl = "rtsp://" + mediamtxHost + ":8554/" + name;
        String rtmpUrl = "rtmp://" + mediamtxHost + ":1935/" + name;
        String webrtcUrl = "http://" + mediamtxHost + ":8889/" + name;
@@ -87,14 +87,17 @@
        // -i //用于指定输入媒体文件或输入流的地址
        // -bf 0 禁用B帧,因为webrtc在网页调用时控制台一直输出 WebRTC doesn’t support H264 streams with B-frames
        //-f rtsp //这个选项告诉 FFmpeg 输出为 RTSP 格式。
        //-acodec opus //音频转码opus
        //-acodec pcm_alaw //音频转码PCMA(G711A)
        //-strict 选项用于设置音频编解码的严格级别,而 -2 是其参数之一。在早期版本的 FFmpeg 中,-strict 选项用于设置音频编解码器的行为,其中 -2 表示使用非常严格的模式。
        //CPU软解码编码
        //String cmd = rootPath + "/lib/mediamtx/" +"ffmpeg -rtsp_transport tcp -i " + rtspPath + " -vcodec libx264 -preset:v ultrafast -r 25 -threads 4  -b:v 2048k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
        //GPU硬解码编码 -hwaccel cuvid -c:v h264_cuvid  使用cuda解码   -c:v h264_nvenc 使用cuda编码
        //String cmd = rootPath  + "/lib/mediamtx/" + "ffmpeg -hwaccel cuvid -c:v h264_cuvid  -rtsp_transport udp  -i " + rtspPath + " -c:v h264_nvenc  -r 25 -threads 4  -b:v 2048k -bf 0 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
        if (isCode.equals("1")) {
            String cmd =  "ffmpeg -rtsp_transport tcp -i " + sourceUrl + " -vcodec libx264 -preset:v ultrafast -r 25 -keyint_min 25 -g 25 -sc_threshold 0 -threads 6  -b:v 2048k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
            String cmd =  "ffmpeg -rtsp_transport tcp -i " + sourceUrl + " -vcodec libx264 -preset:v ultrafast -r 25 -keyint_min 25 -g 25 -sc_threshold 0 -threads 6  -b:v 2048k -acodec opus  -strict -2 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
            if (!softwareDecoding) {
                cmd =  "ffmpeg -hwaccel cuvid -c:v h264_cuvid  -rtsp_transport tcp  -i " + sourceUrl + " -c:v h264_nvenc  -r 25 -g 60 -threads 6  -b:v 2048k -bf 0 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
                cmd =  "ffmpeg -hwaccel cuvid -c:v h264_cuvid  -rtsp_transport tcp  -i " + sourceUrl + " -c:v h264_nvenc  -r 25 -g 60 -threads 6  -b:v 2048k -bf 0 -acodec opus  -strict -2 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
            }
            if (mode.equals("0")) {
                mediaInfo.setRunondemand(cmd);
@@ -106,6 +109,7 @@
            }
        } else {
            mediaInfo.setSource(sourceUrl);
            mediaInfo.setSourceondemand(true);
        }
        mediaInfo.setMaxReaders(100);
        mediaInfo.setSourceprotocol("tcp");
@@ -119,7 +123,7 @@
    @Override
    public Map<String, String> editPath(String name, String sourceUrl, String mode, String isCode) {
        String rtspUrl = "rtsp://" + mediamtxHost + ":7554/" + name;
        String rtspUrl = "rtsp://" + mediamtxHost + ":8554/" + name;
        String rtmpUrl = "rtmp://" + mediamtxHost + ":1935/" + name;
        String webrtcUrl = "http://" + mediamtxHost + ":8889/" + name;
@@ -135,15 +139,16 @@
        // -i //用于指定输入媒体文件或输入流的地址
        // -bf 0 禁用B帧,因为webrtc在网页调用时控制台一直输出 WebRTC doesn’t support H264 streams with B-frames
        //-f rtsp //这个选项告诉 FFmpeg 输出为 RTSP 格式。
        //-acodec opus //音频转码opus
        //CPU软解码编码
        //String cmd = rootPath + "/lib/mediamtx/" +"ffmpeg -rtsp_transport tcp -i " + rtspPath + " -vcodec libx264 -preset:v ultrafast -r 25 -threads 4  -b:v 2048k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
        //GPU硬解码编码 -hwaccel cuvid -c:v h264_cuvid  使用cuda解码   -c:v h264_nvenc 使用cuda编码
        //String cmd = rootPath  + "/lib/mediamtx/" + "ffmpeg -hwaccel cuvid -c:v h264_cuvid  -rtsp_transport udp  -i " + rtspPath + " -c:v h264_nvenc  -r 25 -threads 4  -b:v 2048k -bf 0 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
        if (isCode.equals("1")) {
            mediaInfo.setSource("publisher");
            String cmd =  "ffmpeg -rtsp_transport tcp -i " + sourceUrl + " -vcodec libx264 -preset:v ultrafast -r 25 -keyint_min 25 -g 25 -sc_threshold 0 -threads 6  -b:v 2048k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
            String cmd =  "ffmpeg -rtsp_transport tcp -i " + sourceUrl + "  -vcodec libx264 -preset:v ultrafast -r 25 -keyint_min 25 -g 25 -sc_threshold 0 -threads 6  -b:v 2048k  -acodec opus -f  rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
            if (!softwareDecoding) {
                cmd =  "ffmpeg -hwaccel cuvid -c:v h264_cuvid  -rtsp_transport tcp  -i " + sourceUrl + " -c:v h264_nvenc  -r 25 -g 60 -threads 6  -b:v 2048k -bf 0 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
                cmd =  "ffmpeg -hwaccel cuvid -c:v h264_cuvid  -rtsp_transport tcp  -i " + sourceUrl + "  -c:v h264_nvenc  -r 25 -g 60 -threads 6  -b:v 2048k -bf 0  -acodec opus  -f  rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
            }
            if (mode.equals("0")) {
                mediaInfo.setRunondemand(cmd);
@@ -159,6 +164,7 @@
            }
        } else {
            mediaInfo.setSource(sourceUrl);
            mediaInfo.setSourceondemand(true);
            mediaInfo.setRunondemand("");
            mediaInfo.setRunondemandrestart(false);
            mediaInfo.setRunoninit("");
@@ -290,7 +296,7 @@
            String rtmpUrl = "rtmp://" + mediamtxHost + ":1935/" + name;
            info.setRtmpUrl(rtmpUrl);
            //RTSP播放地址
            String rtspUrl = "rtsp://" + mediamtxHost + ":7554/" + name;
            String rtspUrl = "rtsp://" + mediamtxHost + ":8554/" + name;
            info.setRtspUrl(rtspUrl);
            //WEBRTC播放地址
            String webrtcUrl = "http://" + mediamtxHost + ":8889/" + name;