‘liusuyi’
2023-07-19 191a9cb6a141fb39037c41d8c13e78bf0c1b1cde
优化流媒体转码增删改查
已修改5个文件
212 ■■■■■ 文件已修改
ard-work/src/main/java/com/ruoyi/media/controller/MediaController.java 61 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/media/service/IMediaService.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/media/service/impl/MediaService.java 103 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/utils/forest/MediaClient.java 40 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/media/controller/MediaController.java
@@ -1,9 +1,13 @@
package com.ruoyi.media.controller;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.device.camera.domain.ArdCameras;
import com.ruoyi.media.domain.StreamInfo;
import com.ruoyi.media.service.IMediaService;
import io.swagger.annotations.Api;
@@ -23,27 +27,48 @@
@RestController
@Api(tags = "流媒体接口")
@RequestMapping("/media/stream")
@Anonymous
public class MediaController extends BaseController {
    @Resource
    private IMediaService mediaService;
    @PostMapping("/add")
    @PostMapping()
    @ApiOperation("增加转码")
    @ApiOperationSupport(includeParameters = {"streamInfo.name","streamInfo.rtspSource"})
    @PreAuthorize("@ss.hasPermi('media:stream:add')")
    @ApiOperationSupport(includeParameters = {"streamInfo.name", "streamInfo.rtspSource", "streamInfo.mode"})
    public AjaxResult addPath(@RequestBody StreamInfo streamInfo) {
        String rtsp = mediaService.addPath(streamInfo.getName(), streamInfo.getRtspSource());
        String rtsp = mediaService.addPath(streamInfo.getName(), streamInfo.getRtspSource(), streamInfo.getMode());
        return AjaxResult.success(rtsp);
    }
    /**
     * 获取转码详细信息
     */
    @ApiOperation("获取转码详细信息")
    @GetMapping(value = "/{name}")
    public AjaxResult getInfo(@PathVariable("name") String name) {
        return success(mediaService.getPathInfo(name));
    }
    /**
     * 修改转码
     */
    @ApiOperation("修改转码")
    @PreAuthorize("@ss.hasPermi('media:stream:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody StreamInfo streamInfo) {
        mediaService.removePath(new String[]{streamInfo.getName()});
        String rtsp = mediaService.addPath(streamInfo.getName(), streamInfo.getRtspSource(), streamInfo.getMode());
        return AjaxResult.success(rtsp);
    }
    @PostMapping("/remove")
    @DeleteMapping("/path/{names}")
    @PreAuthorize("@ss.hasPermi('media:stream:remove')")
    @ApiOperation("移除转码")
    @ApiOperationSupport(includeParameters = {"streamInfo.name"})
    public AjaxResult removePath(@RequestBody StreamInfo streamInfo) {
        mediaService.removePath(streamInfo.getName());
    public AjaxResult removePath( @PathVariable String[] names) {
        mediaService.removePath(names);
        return AjaxResult.success();
    }
    @GetMapping("/getPaths")
    @GetMapping("/path/list")
    @ApiOperation("获取当前转码列表")
    public TableDataInfo getPaths() {
        startPage();
@@ -87,25 +112,7 @@
        startPage();
        return getDataTable(mediaService.getPushStreamList());
    }
    /**
     * 新增流媒体推流
     */
    @PreAuthorize("@ss.hasPermi('media:stream:add')")
    @PostMapping
    @ApiOperationSupport(includeParameters = {"streamInfo.name"})
    public AjaxResult add(@RequestBody StreamInfo streamInfo) throws InterruptedException {
        String rtsp = mediaService.addPath(streamInfo.getName(), streamInfo.getRtspSource());
        return AjaxResult.success(rtsp);
    }
    /**
     * 删除流媒体推流
     */
    @PreAuthorize("@ss.hasPermi('media:stream:remove')")
    @DeleteMapping("/path/{name}")
    public AjaxResult remove(@PathVariable String name) {
        mediaService.removePath(name);
        return AjaxResult.success();
    }
    /**
     * 删除流媒体拉流
     */
ard-work/src/main/java/com/ruoyi/media/service/IMediaService.java
@@ -7,8 +7,9 @@
public interface IMediaService {
    public String addPath(String name, String rtspPath);
    public void removePath(String name);
    public String addPath(String name, String rtspPath,String mode);
    StreamInfo getPathInfo(String name);
    public void removePath(String[] names);
    public List<StreamInfo>paths();
    public List<Items> rtspconns();
    public List<Items> rtspsessions();
ard-work/src/main/java/com/ruoyi/media/service/impl/MediaService.java
@@ -28,13 +28,10 @@
    @Resource
    MediaClient mediaClient;
    @Value("${mediamtx.host}")
    String mediamtxHost;
    @Override
    public String addPath(String name, String rtspPath) {
        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
    public String addPath(String name, String rtspPath,String mode) {
        String rtspUrl = "rtsp://" + mediamtxHost + ":8554/";
        Conf mediaInfo = new Conf();
        //-vcodec libx264 //指定视频编码器为 libx264,使用 H.264 编码格式进行视频压缩
@@ -48,24 +45,60 @@
        String cmd = "ffmpeg -rtsp_transport tcp -i \"" + rtspPath + "\" -vcodec libx264 -preset:v ultrafast -r 25 -threads 4  -b:v 4096k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
        //GPU硬解码编码 -hwaccel cuvid -c:v h264_cuvid  使用cuda解码   -c:v h264_nvenc 使用cuda编码
        //String cmd = "ffmpeg -hwaccel cuvid -c:v h264_cuvid  -rtsp_transport udp  -i \"" + rtspPath + "\" -c:v h264_nvenc  -r 25 -threads 4  -b:v 4096k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
//        mediaInfo.setRunoninit(cmd);
//        mediaInfo.setRunoninitrestart(true);
        mediaInfo.setRunondemand(cmd);
        mediaInfo.setRunondemandrestart(true);
        mediaClient.addPath(apiUrl, name, mediaInfo);
        if(mode.equals("1"))
        {
            mediaInfo.setRunondemand(cmd);
            mediaInfo.setRunondemandrestart(true);
        }
       else{
           mediaInfo.setRunoninit(cmd);
           mediaInfo.setRunoninitrestart(true);
        }
        mediaClient.addPath(name, mediaInfo);
        return rtspUrl + name;
    }
    @Override
    public void removePath(String name) {
        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
        mediaClient.removePath(apiUrl, name);
    public StreamInfo getPathInfo(String name) {
        Items item = mediaClient.getPathInfo(name);
        StreamInfo info = new StreamInfo();
        //ID
        info.setName(name);
        String runoninit;
        String runondemand = item.getConf().getRunondemand();
        if (StringUtils.isNotEmpty(runondemand))
        {
            runoninit= item.getConf().getRunondemand();
            info.setMode("1");
        }
        else
        {
            runoninit = item.getConf().getRunoninit();
            info.setMode("2");
        }
        //RTSP源地址
        String regex = "rtsp://[^\\s\"]+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(runoninit);
        if (matcher.find()) {
            info.setRtspSource(matcher.group());
        }
        return info;
    }
    @Override
    public void removePath(String[] names) {
        for(String name:names)
        {
            mediaClient.removePath(name);
        }
    }
    @Override
    public List<StreamInfo> paths() {
        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
        String list = mediaClient.paths(apiUrl);
        String list = mediaClient.paths();
        JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
        List<Items> items = jsonsRoot.getItems();
        List<StreamInfo> pathInfoList = new ArrayList<>();
@@ -79,15 +112,14 @@
            if (StringUtils.isNotEmpty(runondemand))
            {
                runoninit= item.getConf().getRunondemand();
                info.setMode("按需转码");
                info.setMode("1");
            }
            else
            {
                runoninit = item.getConf().getRunoninit();
                info.setMode("实时转码");
                info.setMode("2");
            }
            //RTSP源地址
            runoninit = item.getConf().getRunondemand();
            String regex = "rtsp://[^\\s\"]+";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(runoninit);
@@ -109,24 +141,21 @@
    @Override
    public List<Items> rtspconns() {
        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
        String list = mediaClient.rtspconns(apiUrl);
        String list = mediaClient.rtspconns();
        JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
        return jsonsRoot.getItems();
    }
    @Override
    public List<Items> rtspsessions() {
        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
        String list = mediaClient.rtspsessions(apiUrl);
        String list = mediaClient.rtspsessions();
        JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
        return jsonsRoot.getItems();
    }
    @Override
    public RtspSession getRtspSessionById(String sessionId) {
        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
        String list = mediaClient.getRtspsessionById(apiUrl, sessionId);
        String list = mediaClient.getRtspsessionById(sessionId);
        RtspSession rtspSession = JSONObject.parseObject(list, RtspSession.class);
        return rtspSession;
    }
@@ -134,8 +163,8 @@
    @Override
    public List<RtspSession> getPushStreams() {
        List<RtspSession> rtspSessions = new ArrayList<>();
        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
        String list = mediaClient.paths(apiUrl);
        String list = mediaClient.paths();
        JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
        List<Items> items = jsonsRoot.getItems();
        for (Items item : items) {
@@ -150,8 +179,7 @@
    @Override
    public List<RtspSession> getPullStreams() {
        List<RtspSession> rtspSessions = new ArrayList<>();
        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
        String list = mediaClient.paths(apiUrl);
        String list = mediaClient.paths();
        JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
        List<Items> items = jsonsRoot.getItems();
        for (Items item : items) {
@@ -168,8 +196,7 @@
    @Override
    public List<StreamInfo> getPushStreamList() {
        List<StreamInfo> PushStreamInfoList = new ArrayList<>();
        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
        String list = mediaClient.paths(apiUrl);
        String list = mediaClient.paths();
        JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
        List<Items> items = jsonsRoot.getItems();
        for (Items item : items) {
@@ -199,8 +226,16 @@
            String formatSentSize = ArdTool.formatFileSize(bytesSent);
            info.setDownTraffic(formatSentSize);
            //RTSP源地址
            //String runoninit = item.getConf().getRunoninit();
            String runoninit = item.getConf().getRunondemand();
            String runondemand = item.getConf().getRunondemand();
            String runoninit;
            if(StringUtils.isNotEmpty(runondemand))
            {
                 runoninit = item.getConf().getRunondemand();
            }
            else
            {
                 runoninit = item.getConf().getRunoninit();
            }
            String regex = "rtsp://[^\\s\"]+";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(runoninit);
@@ -228,8 +263,7 @@
    @Override
    public List<StreamInfo> getPullStreamList() {
        List<StreamInfo> PullStreamInfoList = new ArrayList<>();
        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
        String list = mediaClient.paths(apiUrl);
        String list = mediaClient.paths();
        JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
        List<Items> items = jsonsRoot.getItems();
        for (Items item : items) {
@@ -275,8 +309,7 @@
    @Override
    public Boolean kickRtspSession(String sessionId) {
        try {
            String apiUrl = "http://" + mediamtxHost + ":9997/v2";
            mediaClient.kick(apiUrl, sessionId);
            mediaClient.kick(sessionId);
            return true;
        } catch (Exception ex) {
            return false;
ard-work/src/main/java/com/ruoyi/utils/forest/MediaClient.java
@@ -1,10 +1,8 @@
package com.ruoyi.utils.forest;
import com.dtflys.forest.annotation.Get;
import com.dtflys.forest.annotation.JSONBody;
import com.dtflys.forest.annotation.Post;
import com.dtflys.forest.annotation.Var;
import com.dtflys.forest.annotation.*;
import com.ruoyi.media.domain.Conf;
import com.ruoyi.media.domain.Items;
/**
 * @Description: mediamtx流媒体客户端
@@ -13,47 +11,53 @@
 * @Date: 2023年07月06日9:51
 * @Version: 1.0
 **/
@BaseRequest(baseURL = "http://#{mediamtx.host}:9997/v2")
public interface MediaClient {
    /**
     * 增加路径
     */
    @Post("{apiUrl}/config/paths/add/{name}")
    String addPath(@Var("apiUrl") String apiUrl, @Var("name") String name, @JSONBody Conf body);
    @Post("/config/paths/add/{name}")
    String addPath( @Var("name") String name, @JSONBody Conf body);
    /**
     * 移除路径
     */
    @Post("{apiUrl}/config/paths/remove/{name}")
    String removePath(@Var("apiUrl") String apiUrl, @Var("name") String name);
    @Post("/config/paths/remove/{name}")
    String removePath(@Var("name") String name);
    /**
     * 获取路径详情
     */
    @Get("/paths/get/{name}")
    Items getPathInfo(@Var("name") String name);
    /**
     * 查询所有路径
     */
    @Get("{apiUrl}/paths/list")
    String paths(@Var("apiUrl") String apiUrl);
    @Get("/paths/list")
    String paths();
    /**
     * 查询所有rtsp会话
     */
    @Get("{apiUrl}/rtspsessions/list")
    String rtspsessions(@Var("apiUrl") String apiUrl);
    @Get("/rtspsessions/list")
    String rtspsessions();
    /**
     * 查询所有rtsp连接
     */
    @Get("{apiUrl}/rtspconns/list")
    String rtspconns(@Var("apiUrl") String apiUrl);
    @Get("/rtspconns/list")
    String rtspconns();
    /**
     * 按sessionId查询rtsp会话
     */
    @Get("{apiUrl}/rtspsessions/get/{sessionId}")
    String getRtspsessionById(@Var("apiUrl") String apiUrl, @Var("sessionId") String sessionId);
    @Get("/rtspsessions/get/{sessionId}")
    String getRtspsessionById(@Var("sessionId") String sessionId);
    /**
     * 按sessionId删除rtsp会话
     */
    @Post("{apiUrl}/rtspsessions/kick/{sessionId}")
    String kick(@Var("apiUrl") String apiUrl, @Var("sessionId") String sessionId);
    @Post("/rtspsessions/kick/{sessionId}")
    String kick(@Var("sessionId") String sessionId);
}
ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml
@@ -88,8 +88,7 @@
    <select id="selectUserList" parameterType="SysUser" resultMap="SysUserResult">
--         select u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.sex, u.status,
--         u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,u.camera_priority,u.rong_cloud_token, d.dept_name, d.leader from sys_user
--         u
--         u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,u.camera_priority,u.rong_cloud_token, d.dept_name, d.leader from sys_user u
--         left join sys_dept d on u.dept_id = d.dept_id
        <include refid="selectUserVo"/>
        where u.del_flag = '0'