‘liusuyi’
2023-09-09 71dbac60afa54d2642c3471c080686da56579ca0
流媒体优化
已修改9个文件
241 ■■■■■ 文件已修改
ard-work/src/main/java/com/ruoyi/media/controller/MediaController.java 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/media/domain/Conf.java 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/media/domain/Vtdu.java 15 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/media/service/IMediaService.java 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/media/service/impl/MediaServiceImpl.java 83 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/media/service/impl/VtduServiceImpl.java 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/resources/mapper/vtdu/VtduMapper.xml 83 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/resources/templates/preview.html 20 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/mediamtx/mediamtx.yml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/media/controller/MediaController.java
@@ -18,6 +18,7 @@
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
@@ -51,14 +52,18 @@
        if (StringUtils.isEmpty(streamInfo.getRtspSource())) {
            return AjaxResult.error("rtsp地址不能为空");
        }
        String rtsp = mediaService.addPath(streamInfo.getName(), streamInfo.getRtspSource(), streamInfo.getMode(), streamInfo.getIsCode());
        Map<String, String> map = mediaService.addPath(streamInfo.getName(), streamInfo.getRtspSource(), streamInfo.getMode(), streamInfo.getIsCode());
        map.get("rtspUrl");
        Vtdu vtdu = new Vtdu();
        vtdu.setName(streamInfo.getName());
        vtdu.setRtspUrl(streamInfo.getRtspSource());
        vtdu.setSourceUrl(streamInfo.getRtspSource());
        vtdu.setIsCode(streamInfo.getIsCode());
        vtdu.setCodeType(streamInfo.getMode());
        vtdu.setRtspUrl(map.get("rtspUrl"));
        vtdu.setRtmpUrl(map.get("rtmpUrl"));
        vtdu.setWebrtcUrl(map.get("webrtcUrl"));
        vtduService.insertVtdu(vtdu);
        return AjaxResult.success(rtsp);
        return AjaxResult.success(map);
    }
@@ -72,14 +77,16 @@
    public AjaxResult edit(@RequestBody StreamInfo streamInfo) {
        mediaService.removePath(new String[]{streamInfo.getName()});
        vtduService.deleteVtduByName(streamInfo.getName());
        String rtsp = mediaService.addPath(streamInfo.getName(), streamInfo.getRtspSource(), streamInfo.getMode(), streamInfo.getIsCode());
        Map<String, String> map = mediaService.addPath(streamInfo.getName(), streamInfo.getRtspSource(), streamInfo.getMode(), streamInfo.getIsCode());
        Vtdu vtdu = new Vtdu();
        vtdu.setName(streamInfo.getName());
        vtdu.setRtspUrl(streamInfo.getRtspSource());
        vtdu.setSourceUrl(streamInfo.getRtspSource());
        vtdu.setIsCode(streamInfo.getIsCode());
        vtdu.setCodeType(streamInfo.getMode());
        vtduService.insertVtdu(vtdu);
        return AjaxResult.success(rtsp);
        vtdu.setRtspUrl(map.get("rtspUrl"));
        vtdu.setRtmpUrl(map.get("rtmpUrl"));
        vtdu.setWebrtcUrl(map.get("webrtcUrl"));
        return AjaxResult.success(map);
    }
    /**
ard-work/src/main/java/com/ruoyi/media/domain/Conf.java
@@ -130,4 +130,7 @@
    private String runonread;
    @JsonProperty("runOnReadRestart")
    private boolean runonreadrestart;
    @JsonProperty("maxReaders")
    private Integer maxReaders;
}
ard-work/src/main/java/com/ruoyi/media/domain/Vtdu.java
@@ -27,7 +27,7 @@
     * rtsp源地址
     */
    @Excel(name = "rtsp源地址")
    private String rtspUrl;
    private String sourceUrl;
    /**
     * 是否转码
@@ -41,5 +41,16 @@
    @Excel(name = "转码模式")
    private String codeType;
    /**
     * rtsp播放地址
     */
    String rtspUrl;
    /**
     * rtmp播放地址
     */
    String rtmpUrl;
    /**
     * webrtc播放地址
     */
    String webrtcUrl;
}
ard-work/src/main/java/com/ruoyi/media/service/IMediaService.java
@@ -2,7 +2,9 @@
import com.ruoyi.media.domain.*;
import java.lang.reflect.MalformedParameterizedTypeException;
import java.util.List;
import java.util.Map;
public interface IMediaService {
    /**
@@ -14,7 +16,7 @@
     * 刘苏义
     * 2023/8/12 13:56:52
     */
    String addPath(String name, String rtspPath, String mode, String isCode);
    Map<String,String> addPath(String name, String rtspPath, String mode, String isCode);
    StreamInfo getPathInfo(String name);
ard-work/src/main/java/com/ruoyi/media/service/impl/MediaServiceImpl.java
@@ -1,6 +1,8 @@
package com.ruoyi.media.service.impl;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.media.domain.*;
import com.ruoyi.media.mapper.VtduMapper;
@@ -20,10 +22,7 @@
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -61,8 +60,13 @@
                mediaClient.removePath(path.getName());
            }
            List<Vtdu> vtduList = vtduMapper.selectVtduList(new Vtdu());
            for (Vtdu v : vtduList) {
                addPath(v.getName(), v.getRtspUrl(), v.getCodeType(), v.getIsCode());
            for (Vtdu vtdu : vtduList) {
                Map<String, String> map = addPath(vtdu.getName(), vtdu.getSourceUrl(), vtdu.getCodeType(), vtdu.getIsCode());
                vtdu.setRtspUrl(map.get("rtspUrl"));
                vtdu.setRtmpUrl(map.get("rtmpUrl"));
                vtdu.setWebrtcUrl(map.get("webrtcUrl"));
                vtdu.setUpdateTime(DateUtils.getNowDate());
                vtduMapper.updateVtdu(vtdu);
            }
        } catch (Exception ex) {
            log.error("加载流媒体列表异常:" + ex.getMessage());
@@ -105,8 +109,11 @@
    }
    @Override
    public String addPath(String name, String rtspPath, String mode, String isCode) {
    public Map<String,String> addPath(String name, String sourceUrl, String mode, String isCode) {
        String rtspUrl = "rtsp://" + mediamtxHost + ":7554/" + name;
        String rtmpUrl = "rtmp://" + mediamtxHost + ":1935/" + name;
        String webrtcUrl = "http://" + mediamtxHost + ":8889/" + name;
        Conf mediaInfo = new Conf();
        String rootPath = System.getProperty("user.dir").replaceAll("\\\\", "/") + "/lib/mediamtx/";
        //-vcodec libx264 //指定视频编码器为 libx264,使用 H.264 编码格式进行视频压缩
@@ -124,9 +131,9 @@
        //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 = rootPath + "ffmpeg -rtsp_transport tcp -i " + rtspPath + " -vcodec libx264 -preset:v ultrafast -r 25 -keyint_min 20 -g 20 -sc_threshold 0 -threads 6  -b:v 2048k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
            String cmd = rootPath + "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 4096k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
            if (!softwareDecoding) {
                cmd = rootPath + "ffmpeg -hwaccel cuvid -c:v h264_cuvid  -rtsp_transport tcp  -i " + rtspPath + " -c:v h264_nvenc  -r 25 -g 60 -threads 6  -b:v 2048k -bf 0 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
                cmd = rootPath + "ffmpeg -hwaccel cuvid -c:v h264_cuvid  -rtsp_transport tcp  -i " + sourceUrl + " -c:v h264_nvenc  -r 25 -g 60 -threads 6  -b:v 4096k -bf 0 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
            }
            if (mode.equals("0")) {
                mediaInfo.setRunondemand(cmd);
@@ -134,13 +141,20 @@
            } else {
                mediaInfo.setRunoninit(cmd);
                mediaInfo.setRunoninitrestart(true);
                //mediaInfo.setRunonready(cmd);
                //mediaInfo.setRunonreadyrestart(true);
            }
        } else {
            mediaInfo.setSource(rtspPath);
            mediaInfo.setSource(sourceUrl);
        }
        mediaInfo.setMaxReaders(100);
        mediaInfo.setSourceprotocol("tcp");
        mediaClient.addPath(name, mediaInfo);
        return rtspUrl;
        Map<String,String> map=new HashMap<>();
        map.put("rtspUrl",rtspUrl);
        map.put("rtmpUrl",rtmpUrl);
        map.put("webrtcUrl",webrtcUrl);
        return map;
    }
    @Override
@@ -149,19 +163,17 @@
        StreamInfo info = new StreamInfo();
        //ID
        info.setName(name);
        String runoninit;
        String runondemand = item.getConf().getRunondemand();
        if (StringUtils.isNotEmpty(runondemand)) {
            runoninit = item.getConf().getRunondemand();
        String runOn;
        if (StringUtils.isNotEmpty(item.getConf().getRunondemand())) {
            runOn = item.getConf().getRunondemand();
            info.setMode("0");
        } else {
            runoninit = item.getConf().getRunoninit();
            //runOn = item.getConf().getRunonready();
            runOn = item.getConf().getRunoninit();
            info.setMode("1");
        }
        //RTSP源地址
        String regex = "rtsp://[^\\s\"]+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(runoninit);
        Matcher matcher = Pattern.compile("rtsp://[^\\s\"]+").matcher(runOn);
        if (matcher.find()) {
            info.setRtspSource(matcher.group());
            info.setIsCode("1");
@@ -190,19 +202,17 @@
            //ID
            String name = item.getName();
            info.setName(name);
            String runoninit;
            String runondemand = item.getConf().getRunondemand();
            if (StringUtils.isNotEmpty(runondemand)) {
                runoninit = item.getConf().getRunondemand();
            String runOn;
            if (StringUtils.isNotEmpty(item.getConf().getRunondemand())) {
                runOn = item.getConf().getRunondemand();
                info.setMode("0");
            } else {
                runoninit = item.getConf().getRunoninit();
                runOn = item.getConf().getRunoninit();
                //runOn = item.getConf().getRunonready();
                info.setMode("1");
            }
            //RTSP源地址
            String regex = "rtsp://[^\\s\"]+";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(runoninit);
            Matcher matcher = Pattern.compile("rtsp://[^\\s\"]+").matcher(runOn);
            if (matcher.find()) {
                info.setRtspSource(matcher.group());
                info.setIsCode("1");
@@ -211,9 +221,7 @@
                info.setIsCode("0");
            }
            //传输协议
            regex = "-rtsp_transport\\s+(\\w+)";
            pattern = Pattern.compile(regex);
            matcher = pattern.matcher(runoninit);
            matcher = Pattern.compile("-rtsp_transport\\s+(\\w+)").matcher(runOn);
            if (matcher.find()) {
                info.setProtocol(matcher.group(1));
            }
@@ -296,16 +304,14 @@
                info.setRemoteAddr(rtspSession.getRemoteAddr());
            }
            //RTSP源地址
            String runondemand = item.getConf().getRunondemand();
            String runoninit;
            if (StringUtils.isNotEmpty(runondemand)) {
                runoninit = item.getConf().getRunondemand();
            String runOn;
            if (StringUtils.isNotEmpty(item.getConf().getRunondemand())) {
                runOn = item.getConf().getRunondemand();
            } else {
                runoninit = item.getConf().getRunoninit();
                runOn = item.getConf().getRunoninit();
                //runOn = item.getConf().getRunonready();
            }
            String regex = "rtsp://[^\\s\"]+";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(runoninit);
            Matcher matcher = Pattern.compile("rtsp://[^\\s\"]+").matcher(runOn);
            if (matcher.find()) {
                info.setRtspSource(matcher.group());
            } else {
@@ -316,7 +322,6 @@
            //拉流数量
            List<Readers> readers = item.getReaders();
            info.setNum(readers.size());
            PushStreamInfoList.add(info);
        }
ard-work/src/main/java/com/ruoyi/media/service/impl/VtduServiceImpl.java
@@ -2,12 +2,16 @@
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.media.mapper.VtduMapper;
import com.ruoyi.media.domain.Vtdu;
import com.ruoyi.media.service.IVtduService;
import javax.annotation.Resource;
/**
 * 流媒体管理Service业务层处理
@@ -17,7 +21,7 @@
 */
@Service
public class VtduServiceImpl implements IVtduService {
    @Autowired
    @Resource
    private VtduMapper vtduMapper;
    /**
@@ -50,6 +54,8 @@
     */
    @Override
    public int insertVtdu(Vtdu vtdu) {
        vtdu.setCreateBy(SecurityUtils.getUserId());
        vtdu.setCreateTime(DateUtils.getNowDate());
        return vtduMapper.insertVtdu(vtdu);
    }
@@ -61,6 +67,8 @@
     */
    @Override
    public int updateVtdu(Vtdu vtdu) {
        vtdu.setUpdateBy(SecurityUtils.getUserId());
        vtdu.setUpdateTime(DateUtils.getNowDate());
        return vtduMapper.updateVtdu(vtdu);
    }
ard-work/src/main/resources/mapper/vtdu/VtduMapper.xml
@@ -1,63 +1,104 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.media.mapper.VtduMapper">
    <resultMap type="Vtdu" id="VtduResult">
        <result property="name"    column="name"    />
        <result property="rtspUrl"    column="rtsp_url"    />
        <result property="isCode"    column="is_code"    />
        <result property="codeType"    column="code_type"    />
        <result property="name" column="name"/>
        <result property="sourceUrl" column="source_url"/>
        <result property="isCode" column="is_code"/>
        <result property="codeType" column="code_type"/>
        <result property="rtspUrl" column="rtsp_url"/>
        <result property="rtmpUrl" column="rtmp_url"/>
        <result property="webrtcUrl" column="webrtc_url"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>
    <sql id="selectVtduVo">
        select name, rtsp_url, is_code, code_type from vtdu
        select name,
               source_url,
               is_code,
               code_type,
               rtsp_url,
               rtmp_url,
               webrtc_url,
               create_by,
               create_time,
               update_by,
               update_time
        from vtdu
    </sql>
    <select id="selectVtduList" parameterType="Vtdu" resultMap="VtduResult">
        <include refid="selectVtduVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like '%'||#{name}||'%'</if>
            <if test="rtspUrl != null  and rtspUrl != ''"> and rtsp_url = #{rtspUrl}</if>
            <if test="isCode != null  and isCode != ''"> and is_code = #{isCode}</if>
            <if test="codeType != null  and codeType != ''"> and code_type = #{codeType}</if>
        <where>
            <if test="name != null  and name != ''">and name like '%'||#{name}||'%'</if>
            <if test="sourceUrl != null  and sourceUrl != ''">and source_url = #{sourceUrl}</if>
            <if test="isCode != null  and isCode != ''">and is_code = #{isCode}</if>
            <if test="codeType != null  and codeType != ''">and code_type = #{codeType}</if>
        </where>
    </select>
    <select id="selectVtduByName" parameterType="String" resultMap="VtduResult">
        <include refid="selectVtduVo"/>
        where name = #{name}
    </select>
    <insert id="insertVtdu" parameterType="Vtdu">
        insert into vtdu
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="rtspUrl != null">rtsp_url,</if>
            <if test="sourceUrl != null">source_url,</if>
            <if test="isCode != null">is_code,</if>
            <if test="codeType != null">code_type,</if>
         </trim>
            <if test="rtspUrl != null">rtsp_url,</if>
            <if test="rtmpUrl != null">rtmp_url,</if>
            <if test="webrtcUrl != null">webrtc_url,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="rtspUrl != null">#{rtspUrl},</if>
            <if test="sourceUrl != null">#{sourceUrl},</if>
            <if test="isCode != null">#{isCode},</if>
            <if test="codeType != null">#{codeType},</if>
         </trim>
            <if test="rtspUrl != null">#{rtspUrl},</if>
            <if test="rtmpUrl != null">#{rtmpUrl},</if>
            <if test="webrtcUrl != null">#{webrtcUrl},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>
    <update id="updateVtdu" parameterType="Vtdu">
        update vtdu
        <trim prefix="SET" suffixOverrides=",">
            <if test="rtspUrl != null">rtsp_url = #{rtspUrl},</if>
            <if test="sourceUrl != null">source_url = #{sourceUrl},</if>
            <if test="isCode != null">is_code = #{isCode},</if>
            <if test="codeType != null">code_type = #{codeType},</if>
            <if test="rtspUrl != null">rtsp_url = #{rtspUrl},</if>
            <if test="rtmpUrl != null">rtmp_url = #{rtmpUrl},</if>
            <if test="webrtcUrl != null">webrtc_url = #{webrtcUrl},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where name = #{name}
    </update>
    <delete id="deleteVtduByName" parameterType="String">
        delete from vtdu where name = #{name}
        delete
        from vtdu
        where name = #{name}
    </delete>
    <delete id="deleteVtduByNames" parameterType="String">
ard-work/src/main/resources/templates/preview.html
@@ -153,14 +153,22 @@
    window.onload = function () {
        changeGrid(2, 2);
        chanMap.set("video1", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video2", "http://" + mediamtxHost + ":8889/165/");
        chanMap.set("video3", "http://" + mediamtxHost + ":8889/245/");
        chanMap.set("video2", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video3", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video4", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video5", "http://" + mediamtxHost + ":8889/165/");
        chanMap.set("video6", "http://" + mediamtxHost + ":8889/245/");
        chanMap.set("video5", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video6", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video7", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video8", "http://" + mediamtxHost + ":8889/165/");
        chanMap.set("video9", "http://" + mediamtxHost + ":8889/245/");
        chanMap.set("video8", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video9", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video10", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video11", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video12", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video13", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video14", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video15", "http://" + mediamtxHost + ":8889/164/");
        chanMap.set("video16", "http://" + mediamtxHost + ":8889/164/");
        console.log(chanMap);
    }
    const linkToIceServers = (links) => (
lib/mediamtx/mediamtx.yml
@@ -15,7 +15,7 @@
writeTimeout: 10s
# Size of the queue of outgoing packets.
# A higher value allows to increase throughput, a lower value allows to save RAM.
writeQueueSize: 512
writeQueueSize: 8192
# Maximum size of outgoing UDP packets.
# This can be decreased to avoid fragmentation on networks with a low UDP MTU.
udpMaxPayloadSize: 1472