| | |
| | | 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.BufferedReader; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | 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"; |
| | | // 构建启动命令,使用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(); |
| | | List<String> cmd = new ArrayList<>(); |
| | | cmd.add(exePath); |
| | | cmd.add(ymlPath); |
| | | commandStart(cmd); |
| | | |
| | | } |
| | | } |
| | | public static void commandStart(List<String> command) { |
| | | command.forEach(v -> System.out.print(v + " ")); |
| | | System.out.println(); |
| | | System.out.println(); |
| | | ProcessBuilder builder = new ProcessBuilder(); |
| | | //正常信息和错误信息合并输出 |
| | | builder.redirectErrorStream(true); |
| | | builder.command(command); |
| | | //开始执行命令 |
| | | Process process = null; |
| | | try { |
| | | process = builder.start(); |
| | | //如果你想获取到执行完后的信息,那么下面的代码也是需要的 |
| | | // String line = ""; |
| | | // BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| | | // while ((line = br.readLine()) != null) { |
| | | // System.out.println(line); |
| | | // } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | @Override |
| | | public String addPath(String name, String rtspPath,String mode) { |
| | | String rtspUrl = "rtsp://" + mediamtxHost + ":8554/"; |
| | | public String addPath(String name, String rtspPath, String mode) { |
| | | String rtspUrl = "rtsp://" + mediamtxHost + ":7554/"; |
| | | Conf mediaInfo = new Conf(); |
| | | //-vcodec libx264 //指定视频编码器为 libx264,使用 H.264 编码格式进行视频压缩 |
| | | //-preset ultrafast //--preset的参数主要调节编码速度和质量的平衡,有ultrafast(转码速度最快,视频往往也最模糊)、superfast、veryfast、faster、fast、medium、slow、slower、veryslow、placebo这10个选项,从快到慢 |
| | |
| | | //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 name = item.getName(); |
| | | info.setName(name); |
| | | //RTSP播放地址 |
| | | String rtspUrl = "rtsp://" + mediamtxHost + ":8554/" + name; |
| | | String rtspUrl = "rtsp://" + mediamtxHost + ":7554/" + 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); |