| | |
| | | package com.ruoyi.media.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.ruoyi.media.domain.Conf; |
| | | import com.ruoyi.media.domain.Items; |
| | | import com.ruoyi.media.domain.JsonsRoot; |
| | | import com.ruoyi.media.domain.*; |
| | | import com.ruoyi.media.service.IMediaService; |
| | | import com.ruoyi.utils.forest.MediaClient; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | // -i //用于指定输入媒体文件或输入流的地址 |
| | | //-f rtsp //这个选项告诉 FFmpeg 输出为 RTSP 格式。 |
| | | //CPU软解码编码 |
| | | //String cmd = "ffmpeg -rtsp_transport udp -i \"" + rtspPath + "\" -vcodec libx264 -preset:v veryfast -r 25 -threads 4 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH"; |
| | | //GPU硬解码编码 |
| | | String cmd = "ffmpeg -hwaccel cuvid -c:v h264_cuvid -rtsp_transport udp -i \"" + rtspPath + "\" -c:v h264_nvenc -r 25 -threads 4 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH"; |
| | | 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 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH"; |
| | | mediaInfo.setRunoninit(cmd); |
| | | mediaInfo.setRunoninitrestart(true); |
| | | mediaClient.add(apiUrl, name, mediaInfo); |
| | | mediaClient.addPath(apiUrl, name, mediaInfo); |
| | | return rtspUrl + name; |
| | | } |
| | | |
| | | @Override |
| | | public void removePath(String name) { |
| | | String apiUrl="http://"+mediamtxHost+":9997/v2"; |
| | | mediaClient.remove(apiUrl, name); |
| | | mediaClient.removePath(apiUrl, name); |
| | | } |
| | | |
| | | @Override |
| | | public List<Items> list() { |
| | | public List<Items> paths() { |
| | | String apiUrl="http://"+mediamtxHost+":9997/v2"; |
| | | String list = mediaClient.list(apiUrl); |
| | | String list = mediaClient.paths(apiUrl); |
| | | JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class); |
| | | return jsonsRoot.getItems(); |
| | | } |
| | | |
| | | @Override |
| | | public List<Items> rtspconns() { |
| | | String apiUrl="http://"+mediamtxHost+":9997/v2"; |
| | | String list = mediaClient.rtspconns(apiUrl); |
| | | 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); |
| | | 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); |
| | | RtspSession rtspSession = JSONObject.parseObject(list, RtspSession.class); |
| | | return rtspSession; |
| | | } |
| | | |
| | | @Override |
| | | public List<RtspSession> getPushStreams() { |
| | | List<RtspSession> rtspSessions=new ArrayList<>(); |
| | | String apiUrl="http://"+mediamtxHost+":9997/v2"; |
| | | String list = mediaClient.paths(apiUrl); |
| | | 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.setCameraId(item.getName()); |
| | | rtspSessions.add(rtspSession); |
| | | } |
| | | return rtspSessions; |
| | | } |
| | | |
| | | @Override |
| | | public List<RtspSession> getPullStreams() { |
| | | List<RtspSession> rtspSessions=new ArrayList<>(); |
| | | String apiUrl="http://"+mediamtxHost+":9997/v2"; |
| | | String list = mediaClient.paths(apiUrl); |
| | | 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.setCameraId(item.getName()); |
| | | rtspSessions.add(rtspSession); |
| | | } |
| | | } |
| | | return rtspSessions; |
| | | } |
| | | } |