‘liusuyi’
2023-09-26 516c3afd526c8e02db75e9115f4412b57296cc8a
流媒体增加音频转码
流媒体适配mediaMTX新版本1.1.1
已修改3个文件
26 ■■■■■ 文件已修改
ard-work/src/main/java/com/ruoyi/device/hiksdk/controller/SdkController.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/media/service/impl/MediaServiceImpl.java 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/resources/templates/preview.html 13 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/device/hiksdk/controller/SdkController.java
@@ -65,7 +65,7 @@
    }
    @RequestMapping("/media")
    private String media() {
        return "mediaMTX";
        return "preview2";
    }
    @GetMapping("/list")
    public @ResponseBody
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 pcm_alaw  -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);
@@ -120,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;
ard-work/src/main/resources/templates/preview.html
@@ -174,9 +174,9 @@
    }
    class WHEPClient {
        constructor(wurl, videoId) {
        constructor(whepUrl, videoId) {
            this.video = videoId;
            this.url = new URL('whep', wurl);
            this.wurl = new URL('whep', whepUrl);
            this.pc = null;
            this.restartTimeout = null;
            this.eTag = '';
@@ -186,7 +186,7 @@
        start() {
            console.log("requesting ICE servers");
            fetch(this.url, {
            fetch(this.wurl, {
                method: 'OPTIONS',
            })
                .then((res) => this.onIceServers(res))
@@ -223,7 +223,7 @@
            console.log("sending offer");
            fetch(this.url, {
            fetch(this.wurl, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/sdp',
@@ -234,7 +234,8 @@
                    if (res.status !== 201) {
                        throw new Error('bad status code');
                    }
                    this.eTag = res.headers.get('E-Tag');
                   // this.eTag = res.headers.get('ETag');
                    this.eTag = res.headers.get("ETag") || res.headers.get('E-Tag');
                    return res.text();
                })
                .then((sdp) => this.onRemoteAnswer(new RTCSessionDescription({
@@ -288,7 +289,7 @@
        }
        sendLocalCandidates(candidates) {
            fetch(this.url, {
            fetch(this.wurl, {
                method: 'PATCH',
                headers: {
                    'Content-Type': 'application/trickle-ice-sdpfrag',