| | |
| | | import com.ruoyi.media.service.IMediaService; |
| | | import com.ruoyi.utils.forest.MediaClient; |
| | | import com.ruoyi.utils.tools.ArdTool; |
| | | import com.sun.jna.Platform; |
| | | import io.minio.messages.Item; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import javax.annotation.Resource; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * @Description: |
| | | * @Description: 流媒体业务 |
| | | * @ClassName: MediaService |
| | | * @Author: 刘苏义 |
| | | * @Date: 2023年07月13日9:28 |
| | | * @Version: 1.0 |
| | | **/ |
| | | @Service |
| | | @Slf4j |
| | | public class MediaService implements IMediaService { |
| | | |
| | | @Resource |
| | | MediaClient mediaClient; |
| | | @Value("${mediamtx.host}") |
| | | String mediamtxHost; |
| | | |
| | | @PostConstruct |
| | | public void initMediaMtx() { |
| | | if (Platform.isWindows()) { |
| | | String exePath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "mediamtx" + File.separator + "mediamtx.exe"; |
| | | String ymlPath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "mediamtx" + File.separator + "mediamtx.yml"; |
| | | try { |
| | | // 构建启动命令,使用cmd /c start命令来启动可执行程序并显示命令提示符窗口 |
| | | String[] cmd = {"cmd", "/c", "start", exePath,ymlPath}; |
| | | ProcessBuilder processBuilder = new ProcessBuilder(cmd); |
| | | processBuilder.redirectErrorStream(true); // 将错误输出重定向到标准输出 |
| | | Process process = processBuilder.start(); |
| | | // 如果你想等待程序完成 |
| | | int exitCode = process.waitFor(); |
| | | } catch (IOException | InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public String addPath(String name, String rtspPath,String mode) { |
| | | public String addPath(String name, String rtspPath, String mode) { |
| | | String rtspUrl = "rtsp://" + mediamtxHost + ":8554/"; |
| | | Conf mediaInfo = new Conf(); |
| | | //-vcodec libx264 //指定视频编码器为 libx264,使用 H.264 编码格式进行视频压缩 |
| | |
| | | //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"; |
| | | |
| | | if(mode.equals("1")) |
| | | { |
| | | if (mode.equals("1")) { |
| | | mediaInfo.setRunondemand(cmd); |
| | | mediaInfo.setRunondemandrestart(true); |
| | | } |
| | | else{ |
| | | mediaInfo.setRunoninit(cmd); |
| | | mediaInfo.setRunoninitrestart(true); |
| | | } else { |
| | | mediaInfo.setRunoninit(cmd); |
| | | mediaInfo.setRunoninitrestart(true); |
| | | } |
| | | mediaClient.addPath(name, mediaInfo); |
| | | return rtspUrl + name; |
| | |
| | | info.setName(name); |
| | | String runoninit; |
| | | String runondemand = item.getConf().getRunondemand(); |
| | | if (StringUtils.isNotEmpty(runondemand)) |
| | | { |
| | | runoninit= item.getConf().getRunondemand(); |
| | | if (StringUtils.isNotEmpty(runondemand)) { |
| | | runoninit = item.getConf().getRunondemand(); |
| | | info.setMode("1"); |
| | | } |
| | | else |
| | | { |
| | | } else { |
| | | runoninit = item.getConf().getRunoninit(); |
| | | info.setMode("2"); |
| | | } |
| | |
| | | |
| | | @Override |
| | | public void removePath(String[] names) { |
| | | for(String name:names) |
| | | { |
| | | for (String name : names) { |
| | | mediaClient.removePath(name); |
| | | } |
| | | } |
| | |
| | | info.setName(name); |
| | | String runoninit; |
| | | String runondemand = item.getConf().getRunondemand(); |
| | | if (StringUtils.isNotEmpty(runondemand)) |
| | | { |
| | | runoninit= item.getConf().getRunondemand(); |
| | | if (StringUtils.isNotEmpty(runondemand)) { |
| | | runoninit = item.getConf().getRunondemand(); |
| | | info.setMode("1"); |
| | | } |
| | | else |
| | | { |
| | | } else { |
| | | runoninit = item.getConf().getRunoninit(); |
| | | info.setMode("2"); |
| | | } |
| | |
| | | String rtspUrl = "rtsp://" + mediamtxHost + ":8554/" + name; |
| | | info.setRtspUrl(rtspUrl); |
| | | Source source = item.getSource(); |
| | | if(source==null) |
| | | { |
| | | if (source == null) { |
| | | continue; |
| | | } |
| | | RtspSession rtspSession = getRtspSessionById(source.getId()); |
| | |
| | | //RTSP源地址 |
| | | String runondemand = item.getConf().getRunondemand(); |
| | | String runoninit; |
| | | if(StringUtils.isNotEmpty(runondemand)) |
| | | { |
| | | runoninit = item.getConf().getRunondemand(); |
| | | } |
| | | else |
| | | { |
| | | runoninit = item.getConf().getRunoninit(); |
| | | if (StringUtils.isNotEmpty(runondemand)) { |
| | | runoninit = item.getConf().getRunondemand(); |
| | | } else { |
| | | runoninit = item.getConf().getRunoninit(); |
| | | } |
| | | String regex = "rtsp://[^\\s\"]+"; |
| | | Pattern pattern = Pattern.compile(regex); |