| | |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.uuid.IdUtils; |
| | | import com.ruoyi.media.domain.*; |
| | | import com.ruoyi.media.mapper.VtduMapper; |
| | | import com.ruoyi.media.service.IMediaService; |
| | | import com.ruoyi.utils.forest.MediaClient; |
| | | import com.ruoyi.utils.tools.ArdTool; |
| | |
| | | @Service |
| | | @Slf4j(topic = "cmd") |
| | | public class MediaService implements IMediaService { |
| | | |
| | | @Resource |
| | | VtduMapper vtduMapper; |
| | | @Resource |
| | | MediaClient mediaClient; |
| | | @Value("${mediamtx.host}") |
| | | String mediamtxHost; |
| | | @Value("${mediamtx.enabled}") |
| | | Boolean mediamtxEnabled; |
| | | @Value("${mediamtx.software_decoding}") |
| | | Boolean softwareDecoding; |
| | | |
| | | String processName = "mediamtx.exe"; |
| | | |
| | |
| | | // CmdUtils.commandStart(command); |
| | | } |
| | | } |
| | | List<StreamInfo> paths = paths(); |
| | | for(StreamInfo path:paths) |
| | | { |
| | | mediaClient.removePath(path.getName()); |
| | | } |
| | | List<Vtdu> vtduList = vtduMapper.selectVtduList(new Vtdu()); |
| | | for (Vtdu v : vtduList) { |
| | | addPath(v.getName(), v.getRtspUrl(), v.getCodeType(), v.getIsCode()); |
| | | } |
| | | } |
| | | |
| | | @PreDestroy |
| | |
| | | public String addPath(String name, String rtspPath, String mode, String isCode) { |
| | | String rtspUrl = "rtsp://" + mediamtxHost + ":7554/" + name; |
| | | Conf mediaInfo = new Conf(); |
| | | String rootPath = System.getProperty("user.dir").replaceAll("\\\\", "/"); |
| | | //-vcodec libx264 //指定视频编码器为 libx264,使用 H.264 编码格式进行视频压缩 |
| | | //-preset ultrafast //--preset的参数主要调节编码速度和质量的平衡,有ultrafast(转码速度最快,视频往往也最模糊)、superfast、veryfast、faster、fast、medium、slow、slower、veryslow、placebo这10个选项,从快到慢 |
| | | //-r 25 //设置输出视频的帧率为 25 帧/秒 |
| | | //-rtsp_transport tcp //这个选项告诉 FFmpeg 使用 TCP 作为 RTSP 的传输协议 |
| | | //-threads 4: 指定要使用的线程数为 4。//这允许 FFmpeg 在多核处理器上使用多个线程来进行视频编码,以加快速度。 |
| | | // -i //用于指定输入媒体文件或输入流的地址 |
| | | // -bf 0 禁用B帧,因为webrtc在网页调用时控制台一直输出 WebRTC doesn’t support H264 streams with B-frames |
| | | //-f rtsp //这个选项告诉 FFmpeg 输出为 RTSP 格式。 |
| | | //CPU软解码编码 |
| | | 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"; |
| | | //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 = "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"; |
| | | //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 + "/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"; |
| | | if (!softwareDecoding) { |
| | | 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 (mode.equals("1")) { |
| | | mediaInfo.setRunondemand(cmd); |
| | | mediaInfo.setRunondemandrestart(true); |
| | |
| | | info.setMode("1"); |
| | | } else { |
| | | runoninit = item.getConf().getRunoninit(); |
| | | info.setMode("2"); |
| | | info.setMode("0"); |
| | | } |
| | | //RTSP源地址 |
| | | String regex = "rtsp://[^\\s\"]+"; |
| | |
| | | return pathInfoList; |
| | | } |
| | | |
| | | @Override |
| | | public List<Items> rtspconns() { |
| | | String list = mediaClient.rtspconns(); |
| | | JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class); |
| | | return jsonsRoot.getItems(); |
| | | } |
| | | |
| | | @Override |
| | | public List<Items> rtspsessions() { |
| | | String list = mediaClient.rtspsessions(); |
| | | JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class); |
| | | return jsonsRoot.getItems(); |
| | | } |
| | | |
| | | @Override |
| | | public RtspSession getRtspSessionById(String sessionId) { |
| | |
| | | return rtmpSession; |
| | | } |
| | | |
| | | @Override |
| | | public List<RtspSession> getPushStreams() { |
| | | List<RtspSession> rtspSessions = new ArrayList<>(); |
| | | |
| | | String list = mediaClient.paths(); |
| | | JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class); |
| | | List<Items> items = jsonsRoot.getItems(); |
| | | for (Items item : items) { |
| | | Source source = item.getSource(); |
| | | RtspSession rtspSession = getRtspSessionById(source.getId()); |
| | | rtspSession.setName(item.getName()); |
| | | rtspSessions.add(rtspSession); |
| | | } |
| | | return rtspSessions; |
| | | } |
| | | |
| | | @Override |
| | | public List<RtspSession> getPullStreams() { |
| | | List<RtspSession> rtspSessions = new ArrayList<>(); |
| | | String list = mediaClient.paths(); |
| | | JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class); |
| | | List<Items> items = jsonsRoot.getItems(); |
| | | for (Items item : items) { |
| | | List<Readers> readers = item.getReaders(); |
| | | for (Readers reader : readers) { |
| | | RtspSession rtspSession = getRtspSessionById(reader.getId()); |
| | | rtspSession.setName(item.getName()); |
| | | rtspSessions.add(rtspSession); |
| | | } |
| | | } |
| | | return rtspSessions; |
| | | } |
| | | |
| | | /** |
| | | * 获取推流列表 |
| | | * 刘苏义 |
| | | * 2023/8/29 9:37:05 |
| | | */ |
| | | @Override |
| | | public List<StreamInfo> getPushStreamList() { |
| | | List<StreamInfo> PushStreamInfoList = new ArrayList<>(); |
| | |
| | | return PushStreamInfoList; |
| | | } |
| | | |
| | | /** |
| | | * 获取拉流列表 |
| | | * 刘苏义 |
| | | * 2023/8/29 9:37:05 |
| | | */ |
| | | @Override |
| | | public List<StreamInfo> getPullStreamList() { |
| | | List<StreamInfo> PullStreamInfoList = new ArrayList<>(); |
| | |
| | | //开始拉流时间 |
| | | info.setBeginTime(webrtcSession.getCreated()); |
| | | //上行流量 |
| | | bytesReceived = webrtcSession.getBytesReceived(); |
| | | formatReceivedSize = ArdTool.formatFileSize(bytesReceived); |
| | | bytesReceived = webrtcSession.getBytesReceived(); |
| | | formatReceivedSize = ArdTool.formatFileSize(bytesReceived); |
| | | info.setUpTraffic(formatReceivedSize); |
| | | //下行流量 |
| | | bytesSent = webrtcSession.getBytesSent(); |
| | | formatSentSize = ArdTool.formatFileSize(bytesSent); |
| | | bytesSent = webrtcSession.getBytesSent(); |
| | | formatSentSize = ArdTool.formatFileSize(bytesSent); |
| | | info.setDownTraffic(formatSentSize); |
| | | //拉流服务器 |
| | | info.setRemoteAddr(webrtcSession.getRemoteAddr()); |
| | |
| | | return PullStreamInfoList; |
| | | } |
| | | |
| | | /** |
| | | * 踢出rtsp会话 |
| | | * 刘苏义 |
| | | * 2023/8/29 9:37:05 |
| | | */ |
| | | @Override |
| | | public Boolean kickRtspSession(String sessionId) { |
| | | try { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 踢出rtmp会话 |
| | | * 刘苏义 |
| | | * 2023/8/29 9:37:05 |
| | | */ |
| | | @Override |
| | | public Boolean kickRtmpSession(String sessionId) { |
| | | try { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 踢出webrtc会话 |
| | | * 刘苏义 |
| | | * 2023/8/29 9:37:05 |
| | | */ |
| | | @Override |
| | | public Boolean kickWebrtcSession(String sessionId) { |
| | | try { |