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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package com.ruoyi.media.service.impl;
 
import com.alibaba.fastjson2.JSONObject;
import com.dtflys.forest.exceptions.ForestNetworkException;
import com.dtflys.forest.exceptions.ForestRuntimeException;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
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.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * @Description: 流媒体业务
 * @ClassName: MediaService
 * @Author: 刘苏义
 * @Date: 2023年07月13日9:28
 * @Version: 1.0
 **/
@Service
@Slf4j(topic = "vtdu")
@Order(2)
public class MediaServiceImpl implements IMediaService {
 
    @Resource
    MediaClient mediaClient;
 
    @Value("${mediamtx.host}")
    String mediamtxHost;
    @Value("${mediamtx.ffmpegPath}")
    String ffmpegPath;
 
    /**
     * 添加流媒体
     * name 相机ID
     * sourceUrl rtsp地址
     * isCode 0-不转码 1-转码
     * mode 0-gpu硬解码 1-cpu软解码
     * <p>
     * 刘苏义
     * 2023/10/12 9:03:41
     */
    @Override
    public Map<String, String> addPath(String name, String sourceUrl, String mode, String isCode) {
        Map<String, String> map = new HashMap<>();
        try {
            String rtspUrl = "rtsp://" + mediamtxHost + ":8554/" + name;
            String rtmpUrl = "rtmp://" + mediamtxHost + ":1935/" + name;
            String webrtcUrl = "http://" + mediamtxHost + ":8889/" + name;
 
            Conf conf = new Conf();
            String rootPath = System.getProperty("user.dir").replaceAll("\\\\", "/") + "/server/mediamtx/";
            if (isCode.equals("1")) {
                conf.setSource("publisher");
                //默认软解码
                String cmd = ffmpegPath + "/ffmpeg -rtsp_transport tcp -i " + sourceUrl + " -vcodec libx264 -preset:v ultrafast -r 25 -keyint_min 25 -g 60 -sc_threshold 0 -threads 6  -b:v 2048k -acodec opus  -strict -2 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
                if (mode.equals("0")) {//硬解码
                    cmd = ffmpegPath + "/ffmpeg -hwaccel cuvid -c:v hevc_cuvid  -rtsp_transport tcp  -i " + sourceUrl + " -c:v h264_nvenc  -r 25 -g 60 -sc_threshold 0 -threads 6  -b:v 2048k -bf 0 -acodec opus  -strict -2 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
                }
                conf.setRunOnDemand(cmd);
                conf.setRunOnDemandRestart(true);
                conf.setRunOnDemandCloseAfter("5s");
            } else {
                conf.setSource(sourceUrl);
                conf.setSourceOnDemand(true);
            }
            conf.setMaxReaders(100);
            conf.setSourceProtocol("tcp");
 
            if (!checkNameExist(name)) {
                mediaClient.addPath(name, conf);
            } else {
                mediaClient.editPath(name, conf);
            }
 
            map.put("rtspUrl", rtspUrl);
            map.put("rtmpUrl", rtmpUrl);
            map.put("webrtcUrl", webrtcUrl);
        } catch (ForestNetworkException ex) {
            log.error("添加流媒体异常:" + ex.getMessage());
        } catch (ForestRuntimeException ex) {
            log.error("添加流媒体异常:" + ex.getMessage());
        }
        return map;
    }
 
    @Override
    public Map<String, String> editPath(String name, String sourceUrl, String mode, String isCode) {
        Map<String, String> map = new HashMap<>();
        try {
            String rtspUrl = "rtsp://" + mediamtxHost + ":8554/" + name;
            String rtmpUrl = "rtmp://" + mediamtxHost + ":1935/" + name;
            String webrtcUrl = "http://" + mediamtxHost + ":8889/" + name;
 
            Conf conf = new Conf();
            String rootPath = System.getProperty("user.dir").replaceAll("\\\\", "/") + "/server/mediamtx/";
            if (isCode.equals("1")) {
                conf.setSource("publisher");
                //默认软解码
                String cmd = ffmpegPath + "/ffmpeg -rtsp_transport tcp -i " + sourceUrl + " -vcodec libx264 -preset:v ultrafast -r 25 -keyint_min 25 -g 60 -sc_threshold 0 -threads 6  -b:v 2048k -acodec opus  -strict -2 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
                if (mode.equals("0")) {//硬解码
                    cmd = ffmpegPath + "/ffmpeg -hwaccel cuvid -c:v hevc_cuvid  -rtsp_transport tcp  -i " + sourceUrl + " -c:v h264_nvenc  -r 25 -g 60 -sc_threshold 0 -threads 6  -b:v 2048k -bf 0 -acodec opus  -strict -2 -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
                }
                conf.setRunOnDemand(cmd);
                conf.setRunOnDemandRestart(true);
                conf.setRunOnDemandCloseAfter("5s");
            } else {
                conf.setSource(sourceUrl);
                conf.setSourceOnDemand(true);
                conf.setRunOnDemand("");
                conf.setRunOnDemandRestart(false);
                conf.setRunOnDemandCloseAfter("5s");
            }
            conf.setMaxReaders(100);
            conf.setSourceProtocol("tcp");
 
            if (checkNameExist(name)) {
                mediaClient.editPath(name, conf);
            }
 
            map.put("rtspUrl", rtspUrl);
            map.put("rtmpUrl", rtmpUrl);
            map.put("webrtcUrl", webrtcUrl);
        } catch (ForestRuntimeException ex) {
            log.error("修改流媒体异常:" + ex.getMessage());
        }
        return map;
    }
 
    @Override
    public StreamInfo getPathInfo(String name) {
        Conf conf = mediaClient.getPathInfo(name);
        StreamInfo info = new StreamInfo();
        //ID
        info.setName(name);
        String runOn = "";
        if (StringUtils.isNotEmpty(conf.getRunOnDemand())) {
            runOn = conf.getRunOnDemand();
            info.setMode("0");
        } else {
            info.setMode("1");
        }
        //RTSP源地址
        Matcher matcher = Pattern.compile("rtsp://[^\\s\"]+").matcher(runOn);
        if (matcher.find()) {
            info.setRtspSource(matcher.group());
            info.setIsCode("1");
        } else {
            info.setRtspSource(conf.getSource());
            info.setIsCode("0");
        }
        return info;
    }
 
    @Override
    public void removePath(String[] names) {
        try {
            for (String name : names) {
                if (checkNameExist(name)) {
                    mediaClient.removePath(name);
                }
            }
        } catch (Exception ex) {
            log.error("批量移除流媒体异常:" + ex.getMessage());
        }
    }
 
    @Override
    public void removePath(String name) {
        try {
            if (checkNameExist(name)) {
                mediaClient.removePath(name);
            }
        } catch (ForestRuntimeException ex) {
            log.error("移除流媒体异常:" + ex.getMessage());
        }
    }
 
    @Override
    public List<StreamInfo> paths(Integer pageNum, Integer pageSize) {
        Paths paths = mediaClient.paths(pageNum - 1, pageSize);
        List<Items> items = paths.getItems();
        List<StreamInfo> pathInfoList = new ArrayList<>();
        for (Items item : items) {
            StreamInfo info = new StreamInfo();
            //ID
            String name = item.getName();
            info.setName(name);
 
            Conf conf = mediaClient.getPathInfo(name);
            String runOn = "";
            if (StringUtils.isNotEmpty(conf.getRunOnDemand())) {
                runOn = conf.getRunOnDemand();
            }
            //RTSP源地址
            Matcher matcher = Pattern.compile("rtsp://[^\\s\"]+").matcher(runOn);
            if (matcher.find()) {
                info.setRtspSource(matcher.group());
                info.setIsCode("1");
            } else {
                info.setRtspSource(conf.getSource());
                info.setIsCode("0");
            }
            //传输协议
            matcher = Pattern.compile("-rtsp_transport\\s+(\\w+)").matcher(runOn);
            if (matcher.find()) {
                info.setProtocol(matcher.group(1));
            }
 
            pathInfoList.add(info);
        }
        return pathInfoList;
    }
 
    @Override
    public RtspSession getRtspSessionById(String sessionId) {
        return mediaClient.getRtspsessionById(sessionId);
    }
 
    @Override
    public WebrtcSession getWebrtcSessionById(String sessionId) {
        return mediaClient.getWebrtcsessionById(sessionId);
    }
 
    @Override
    public RtmpSession getRtmpSessionById(String sessionId) {
        return mediaClient.getRtmpsessionById(sessionId);
    }
 
    /**
     * 获取推流列表
     * 刘苏义
     * 2023/8/29 9:37:05
     */
    @Override
    public TableDataInfo getPushStreamList(Integer pageNum, Integer pageSize) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        List<StreamInfo> PushStreamInfoList = new ArrayList<>();
        Paths paths = mediaClient.paths(pageNum - 1, pageSize);
        int itemCount = paths.getItemCount();
        tableDataInfo.setTotal(itemCount);
        List<Items> items = paths.getItems();
        for (Items item : items) {
            StreamInfo info = new StreamInfo();
            //ID
            String name = item.getName();
            info.setName(name);
            Conf conf = mediaClient.getPathInfo(name);
            //RTMP播放地址
            String rtmpUrl = "rtmp://" + mediamtxHost + ":1935/" + name;
            info.setRtmpUrl(rtmpUrl);
            //RTSP播放地址
            String rtspUrl = "rtsp://" + mediamtxHost + ":8554/" + name;
            info.setRtspUrl(rtspUrl);
            //WEBRTC播放地址
            String webrtcUrl = "http://" + mediamtxHost + ":8889/" + name;
            info.setWebrtcUrl(webrtcUrl);
            Source source = item.getSource();
            if (source == null || source.getId().equals("")) {
                //会话ID
                info.setId("0");
                //下行流量
                long bytesReceived = item.getBytesReceived();
                String formatReceivedSize = ArdTool.formatFileSize(bytesReceived);
                info.setDownTraffic(formatReceivedSize);
                //上行流量
                long bytesSent = item.getBytesSent();
                String formatSentdSize = ArdTool.formatFileSize(bytesSent);
                info.setUpTraffic(formatSentdSize);
                info.setBeginTime(item.getReadyTime());
            } else {
                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);
                //推流服务器
                info.setRemoteAddr(rtspSession.getRemoteAddr());
            }
            //RTSP源地址
            String runOn = "";
            if (StringUtils.isNotEmpty(conf.getRunOnDemand())) {
                runOn = conf.getRunOnDemand();
            }
            Matcher matcher = Pattern.compile("rtsp://[^\\s\"]+").matcher(runOn);
            if (matcher.find()) {
                info.setRtspSource(matcher.group());
            } else {
                info.setRtspSource(conf.getSource());
            }
            //传输协议
            info.setProtocol(conf.getSourceProtocol());
            //拉流数量
            List<Readers> readers = item.getReaders();
            info.setNum(readers.size());
 
            PushStreamInfoList.add(info);
        }
        tableDataInfo.setRows(PushStreamInfoList);
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }
 
    /**
     * 获取webrtc拉流列表
     * 刘苏义
     * 2023/8/29 9:37:05
     */
    @Override
    public TableDataInfo getPullWebrtcStreamList(Integer pageNum, Integer pageSize) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        WebrtcSessions WebrtcSessions = mediaClient.webrtcsessions(pageNum - 1, pageSize);
        List<WebrtcSession> webrtcsessions = WebrtcSessions.getItems();
        webrtcsessions.stream().forEach(
                webrtcSession -> {
                    webrtcSession.setUpStream(ArdTool.formatFileSize(webrtcSession.getBytesReceived()));
                    webrtcSession.setDownStream(ArdTool.formatFileSize(webrtcSession.getBytesSent()));
                    webrtcSession.setBeginTime(webrtcSession.getCreated());
                }
        );
        tableDataInfo.setTotal(WebrtcSessions.getItemCount());
        tableDataInfo.setRows(webrtcsessions);
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }
 
    /**
     * 获取rtmp拉流列表
     * 刘苏义
     * 2023/8/29 9:37:05
     */
    @Override
    public TableDataInfo getPullRtmpStreamList(Integer pageNum, Integer pageSize) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        RtmpSessions rtmpSessions = mediaClient.rtmpsessions(pageNum - 1, pageSize);
        List<RtmpSession> webrtcsessions = rtmpSessions.getItems();
        webrtcsessions.stream().forEach(
                webrtcSession -> {
                    webrtcSession.setUpStream(ArdTool.formatFileSize(webrtcSession.getBytesReceived()));
                    webrtcSession.setDownStream(ArdTool.formatFileSize(webrtcSession.getBytesSent()));
                    webrtcSession.setBeginTime(webrtcSession.getCreated());
                }
        );
        tableDataInfo.setTotal(rtmpSessions.getItemCount());
        tableDataInfo.setRows(webrtcsessions);
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }
 
    /**
     * 获取rtsp拉流列表
     * 刘苏义
     * 2023/8/29 9:37:05
     */
    @Override
    public TableDataInfo getPullRtspStreamList(Integer pageNum, Integer pageSize) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        RtspSessions rtspSessions = mediaClient.rtspsessions(pageNum - 1, pageSize);
        List<RtspSession> webrtcsessions = rtspSessions.getItems();
        webrtcsessions.stream().forEach(
                webrtcSession -> {
                    webrtcSession.setUpStream(ArdTool.formatFileSize(webrtcSession.getBytesReceived()));
                    webrtcSession.setDownStream(ArdTool.formatFileSize(webrtcSession.getBytesSent()));
                    webrtcSession.setBeginTime(webrtcSession.getCreated());
                }
        );
        tableDataInfo.setTotal(rtspSessions.getItemCount());
        tableDataInfo.setRows(webrtcsessions);
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }
 
    /**
     * 踢出rtsp会话
     * 刘苏义
     * 2023/8/29 9:37:05
     */
    @Override
    public Boolean kickRtspSession(String sessionId) {
        try {
            mediaClient.kickRtspSessions(sessionId);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
 
    /**
     * 踢出rtmp会话
     * 刘苏义
     * 2023/8/29 9:37:05
     */
    @Override
    public Boolean kickRtmpSession(String sessionId) {
        try {
            mediaClient.kickRtmpSessions(sessionId);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
 
    /**
     * 踢出webrtc会话
     * 刘苏义
     * 2023/8/29 9:37:05
     */
    @Override
    public Boolean kickWebrtcSession(String sessionId) {
        try {
            mediaClient.kickWebrtcSessions(sessionId);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
 
    /**
     * 获取流媒体name列表
     * 刘苏义
     * 2023/10/13 14:19:07
     */
    @Override
    public List<String> getNameList() {
        List<String> nameList = new ArrayList<>();
        try {
            Paths paths = mediaClient.paths(0, 1000);
            List<Items> items = paths.getItems();
            for (Items item : items) {
                nameList.add(item.getName());
            }
        } catch (Exception ex) {
            log.error("获取流媒体name列表异常:" + ex.getMessage());
        }
        return nameList;
    }
 
    /**
     * 检查name是否存在
     * 刘苏义
     * 2023/10/19 15:18:45
     */
    @Override
    public boolean checkNameExist(String name) {
        boolean result = false;
        List<String> nameList = getNameList();
        if (nameList.contains(name)) {
            result = true;
        }
        return result;
    }
 
 
}