‘liusuyi’
2023-07-17 1e79a0e52ad5c666d38f900ae10ee92e26804da4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package com.ruoyi.media.service.impl;
 
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.media.domain.*;
import com.ruoyi.media.service.IMediaService;
import com.ruoyi.utils.forest.MediaClient;
import com.ruoyi.utils.tools.ArdTool;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * @Description:
 * @ClassName: MediaService
 * @Author: 刘苏义
 * @Date: 2023年07月13日9:28
 * @Version: 1.0
 **/
@Service
public class MediaService implements IMediaService {
 
    @Resource
    MediaClient mediaClient;
 
    @Value("${mediamtx.host}")
    String mediamtxHost;
 
    @Override
    public String addPath(String name, String rtspPath) {
        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
        String rtspUrl = "rtsp://" + mediamtxHost + ":8554/";
        Conf mediaInfo = new Conf();
        //-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 //用于指定输入媒体文件或输入流的地址
        //-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";
        //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);
        return rtspUrl + name;
    }
 
    @Override
    public void removePath(String name) {
        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
        mediaClient.removePath(apiUrl, name);
    }
 
    @Override
    public List<StreamInfo> paths() {
        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
        String list = mediaClient.paths(apiUrl);
        JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
        List<Items> items = jsonsRoot.getItems();
        List<StreamInfo> pathInfoList = new ArrayList<>();
        for (Items item : items) {
            StreamInfo info = new StreamInfo();
            //ID
            String name = item.getName();
            info.setName(name);
            String runoninit="";
            String runondemand = item.getConf().getRunondemand();
            if (StringUtils.isNotEmpty(runondemand))
            {
                runoninit= item.getConf().getRunondemand();
            }
            else
            {
                runoninit = item.getConf().getRunoninit();
            }
            //RTSP源地址
            runoninit = item.getConf().getRunondemand();
            String regex = "rtsp://[^\\s\"]+";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(runoninit);
            if (matcher.find()) {
                info.setRtspSource(matcher.group());
            }
            //传输协议
            regex = "-rtsp_transport\\s+(\\w+)";
            pattern = Pattern.compile(regex);
            matcher = pattern.matcher(runoninit);
            if (matcher.find()) {
                info.setProtocol(matcher.group(1));
            }
 
            pathInfoList.add(info);
        }
        return pathInfoList;
    }
 
    @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.setName(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.setName(item.getName());
                rtspSessions.add(rtspSession);
            }
        }
        return rtspSessions;
    }
 
    @Override
    public List<StreamInfo> getPushStreamList() {
        List<StreamInfo> PushStreamInfoList = 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) {
            StreamInfo info = new StreamInfo();
            //ID
            String name = item.getName();
            info.setName(name);
            //RTSP播放地址
            String rtspUrl = "rtsp://" + mediamtxHost + ":8554/" + name;
            info.setRtspUrl(rtspUrl);
            Source source = item.getSource();
            if(source==null)
            {
                continue;
            }
            RtspSession rtspSession = getRtspSessionById(source.getId());
            //会话ID
            info.setId(rtspSession.getId());
            //开始推流时间
            info.setBeginTime(rtspSession.getCreated());
            //上行流量
            long bytesReceived = rtspSession.getBytesReceived();
            String formatReceivedSize = ArdTool.formatFileSize(bytesReceived);
            info.setUpTraffic(formatReceivedSize);
            //下行流量
            long bytesSent = rtspSession.getBytesSent();
            String formatSentSize = ArdTool.formatFileSize(bytesSent);
            info.setDownTraffic(formatSentSize);
            //RTSP源地址
            //String runoninit = item.getConf().getRunoninit();
            String runoninit = item.getConf().getRunondemand();
            String regex = "rtsp://[^\\s\"]+";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(runoninit);
            if (matcher.find()) {
                info.setRtspSource(matcher.group());
            }
            //传输协议
            regex = "-rtsp_transport\\s+(\\w+)";
            pattern = Pattern.compile(regex);
            matcher = pattern.matcher(runoninit);
            if (matcher.find()) {
                info.setProtocol(matcher.group(1));
            }
            //拉流数量
            List<Readers> readers = item.getReaders();
            info.setNum(readers.size());
            //推流服务器
            info.setRemoteAddr(rtspSession.getRemoteAddr());
 
            PushStreamInfoList.add(info);
        }
        return PushStreamInfoList;
    }
 
    @Override
    public List<StreamInfo> getPullStreamList() {
        List<StreamInfo> PullStreamInfoList = 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) {
            StreamInfo info = new StreamInfo();
            //ID
            String name = item.getName();
            info.setName(name);
            //RTSP播放地址
            String rtspUrl = "rtsp://" + mediamtxHost + ":8554/" + name;
            info.setRtspUrl(rtspUrl);
            List<Readers> readers = item.getReaders();
            for (Readers reader : readers) {
                RtspSession rtspSession = getRtspSessionById(reader.getId());
                //会话ID
                info.setId(rtspSession.getId());
                //开始拉流时间
                info.setBeginTime(rtspSession.getCreated());
                //上行流量
                long bytesReceived = rtspSession.getBytesReceived();
                String formatReceivedSize = ArdTool.formatFileSize(bytesReceived);
                info.setUpTraffic(formatReceivedSize);
                //下行流量
                long bytesSent = rtspSession.getBytesSent();
                String formatSentSize = ArdTool.formatFileSize(bytesSent);
                info.setDownTraffic(formatSentSize);
 
                //传输协议
                String runoninit = item.getConf().getRunondemand();
                String regex = "-rtsp_transport\\s+(\\w+)";
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(runoninit);
                if (matcher.find()) {
                    info.setProtocol(matcher.group(1));
                }
                //拉流服务器
                info.setRemoteAddr(rtspSession.getRemoteAddr());
                PullStreamInfoList.add(info);
            }
        }
        return PullStreamInfoList;
    }
 
    @Override
    public Boolean kickRtspSession(String sessionId) {
        try {
            String apiUrl = "http://" + mediamtxHost + ":9997/v2";
            mediaClient.kick(apiUrl, sessionId);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
}