Monica视频监控处理程序
xc
2021-02-22 fa92d56ae9d27343df39c92b799f727c11f090b6
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
package com.hbbh.adapter.manager.impl;
 
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.hbbh.adapter.dto.StreamDto;
import com.hbbh.adapter.manager.MonibucaManager;
import com.hbbh.adapter.utils.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.Map;
 
 
@Service
public class MonibucaManagerImpl implements MonibucaManager {
 
    private static final Logger log = LoggerFactory.getLogger(Class.class);
 
 
    @Value("${monica.ip}")
    private String monicaIP;
    @Value("${monica.port}")
    private String monicaPort;
    @Value("${monica.rtsp.port}")
    private String monicaRTSPPort;
 
 
    //入参 rtsp视频流
    private String rtspDemo="rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov";
 
    //返回结果,通过配置文件即可反推得到 前提是调用 rtsp/pull 接口成功
    private String jessibucaDemo="ws://localhost:8080/live/test.flv";
 
    //StreamPath 是发布流的唯一标识
    private String streamPath="live/test";
 
 
    // =========== monica监控 ===========
 
    /**
     * 调用Gateway API
     * 成功返回 {"code":0}
     * 失败返回 {"code":1,"msg":"publish badname"}
     * @return jessibucaDemo
     */
    @Override
    public String parseVideo() {
        String url="http://localhost:8081/rtsp/pull";
        Map<String, String> params = Maps.newLinkedHashMap();
        params.put("target",rtspDemo);
        params.put("streamPath",streamPath);
 
        String result = HttpClientUtil.doGet(url, params);
 
 
        Map map = JSON.parseObject(result, Map.class);
        String code = map.get("code")+"";
        String msg = map.get("msg")+"";
        if (!StringUtils.equals("0",code)){
            return "-1";
        }
        return jessibucaDemo;
    }
 
    /**
     *
     * @param param StreamPath 是发布流的唯一标识
     * 示例: http://localhost:8081/api/stop?stream=live/test
     */
    @Override
    public void stopStream(String param) {
        String url="http://"+monicaIP+":"+monicaRTSPPort+"/api/stop";
        Map<String, String> params = Maps.newLinkedHashMap();
        params.put("stream",param);
        String result = HttpClientUtil.doGet(url, params);
        log.info("调用 Monibuca -stopStream- 执行结果 : {}",result);
    }
 
    /**
     * GET
     * @param param 推流入参
     */
    @Override
    public void pullStream(StreamDto param) {
        String url="http://"+monicaIP+":"+monicaRTSPPort+"/rtsp/pull";
        Map<String, String> params = Maps.newLinkedHashMap();
 
        String streamPath = param.getStreamPath();
        String target = param.getTarget();
 
        params.put("target",target);
        params.put("streamPath",streamPath);
        String result = HttpClientUtil.doGet(url, params);
        log.info("调用 Monibuca -pullStream- 执行结果 : {}",result);
    }
 
    /**
     * GET
     * @param params 推流入参
     */
    @Override
    public void pullStreamList(List<StreamDto> params) {
        String url="http://"+monicaIP+":"+monicaRTSPPort+"/rtsp/pull";
 
        for (StreamDto param : params) {
            Map<String, String> paramMap = Maps.newLinkedHashMap();
 
            String streamPath = param.getStreamPath();
            String target = param.getTarget();
 
            paramMap.put("target",target);
            paramMap.put("streamPath",streamPath);
            String result = HttpClientUtil.doGet(url, paramMap);
            log.info("调用 Monibuca -pullStreamList- streamPath:{}   执行结果 : {}",streamPath,result);
        }
    }
 
    /**
     * GET
     * @param param Monica启动实例名称
     */
    @Override
    public void instanceUpdate(String param) {
        String url="http://"+monicaIP+":"+monicaPort+"/api/instance/update";
        Map<String, String> params = Maps.newLinkedHashMap();
        params.put("name",param);
        String result = HttpClientUtil.doGet(url, params);
        log.info("调用 Monibuca -instanceUpdate- 执行结果 : {}",result);
    }
 
    /**
     * DELETE
     * @param param Monica启动实例名称
     */
    @Override
    public void instanceRemove(String param) {
        String url="http://"+monicaIP+":"+monicaPort+"/api/instance/remove";
        Map<String, String> params = Maps.newLinkedHashMap();
        params.put("name",param);
        String result = HttpClientUtil.doDelete(url, params);
        log.info("调用 Monibuca -instanceRemove- 执行结果 : {}",result);
    }
 
    /**
     * POST
     * @param param Monica启动实例名称
     */
    @Override
    public void instanceKill(String param) {
        String url="http://"+monicaIP+":"+monicaPort+"/api/instance/kill";
        Map<String, String> params = Maps.newLinkedHashMap();
        params.put("name",param);
        String result = HttpClientUtil.doPost(url, params);
        log.info("调用 Monibuca -instanceKill- 执行结果 : {}",result);
    }
 
    /**
     * POST
     * @param param Monica启动实例名称
     */
    @Override
    public void instanceStart(String param) {
        String url="http://"+monicaIP+":"+monicaPort+"/api/instance/start";
        Map<String, String> params = Maps.newLinkedHashMap();
        params.put("name",param);
        String result = HttpClientUtil.doPost(url, params);
        log.info("调用 Monibuca -instanceStart- 执行结果 : {}",result);
    }
 
    @Override
    public void instanceCreate() {
        String url="http://"+monicaIP+":"+monicaPort+"/api/instance/create";
        Map<String, String> params = Maps.newLinkedHashMap();
        params.put("path","/Users/xucheng/Downloads/store/live");
        params.put("name","live");
        params.put("info",info);
        params.put("clear","true");
        String result = HttpClientUtil.doGet(url, params);
        log.info("调用 Monibuca -instanceCreate- 执行结果 : {}",result);
 
    }
 
 
 
    private String info="[Monibuca]\n" +
            "# 是否等待流,如果为true则订阅一个尚未发布的流会进入等待发布的状态,否则返回订阅失败\n" +
            "EnableWaitStream = true\n" +
            "EnableAudio = true\n" +
            "EnableVideo = true\n" +
            "# 缓冲环大小默认是2的10次方\n" +
            "RingSize = 10\n" +
            "# 发布流默认过期时间 1分钟\n" +
            "PublishTimeout = 60000000000\n" +
            "[RTMP]\n" +
            "ListenAddr = \":1935\"\n" +
            "[GateWay]\n" +
            "ListenAddr = \":8081\"\n" +
            "[Jessica]\n" +
            "ListenAddr = \":8080\"\n" +
            "[LogRotate]\n" +
            "# 日志存储目录相对或绝对\n" +
            "Path = \"logs\"\n" +
            "# 日志是否按大小分割,0表示不按大小分割,非零代表按该大小字节进行分割\n" +
            "Size = 0\n" +
            "Days = 1\n" +
            "[Cluster]\n" +
            "# 监听端口代表该服务器为源服务器\n" +
            "ListenAddr = \":2019\"\n" +
            "# 源服务器地址,用于向源服务器进行推或拉流\n" +
            "OriginServer = \"\"\n" +
            "# 推送模式,true表示如果此服务器有发布流,就会推送到源服务器,否则表示拉模式,即如果此服务器有订阅流则从源服务器拉流\n" +
            "Push = true\n" +
            "[HLS]\n" +
            "# 是否开启写磁盘,开启后侦测到发布流就会开始写TS文件\n" +
            "EnableWrite = false\n" +
            "# 是否打开内存模式,在内存中保留TS数据,方便直接读取\n" +
            "EnableMemory = false\n" +
            "# 分片大小 单位秒\n" +
            "Fragment = 10\n" +
            "# 窗口数里,代表一个m3u8文件里面有几个ts\n" +
            "Window = 2\n" +
            "# ts文件存放目录,m3u8会存放在上一级\n" +
            "Path = \"resource\"\n" +
            "[HDL]\n" +
            "ListenAddr = \":2020\"\n" +
            "[TS]\n" +
            "# 是否自动发布,开启后一旦有订阅流就会读取ts文件进行发布,方便测试\n" +
            "AutoPublish = false\n" +
            "# ts存放目录\n" +
            "Path  = \"resource\"\n" +
            "[Record]\n" +
            "Path = \"resource\"\n" +
            "# 是否自动发布,开启后一旦有订阅流就会读取flv文件进行发布,方便测试\n" +
            "AutoPublish = false\n" +
            "# 自动录制功能\n" +
            "AutoRecord  = false\n" +
            "[RTSP]\n" +
            "ListenAddr = \":554\"\n" +
            "AutoPull = true\n" +
            "Reconnect = true\n" +
            "RemoteAddr = \"rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov\"\n" +
            "StreamPath = \"live/rtsp\"\n" +
            "#[[RTSP.AutoPullList]]\n" +
            "#URL = \"rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov\"\n" +
            "#StreamPath = \"live/rtsp2\"\n" +
            "[WebRTC]\n" +
            "# 公网IP地址\n" +
            "PublicIP = [\"127.0.0.1\"]\n" +
            "# 端口范围不配置的话是自动分配\n" +
            "# PortMin = 30000\n" +
            "# PortMax = 40000\n" +
            "[GB28181]\n" +
            "Serial = \"34020000002000000001\"\n" +
            "Realm = \"3402000000\"\n" +
            "Expires = 3600\n" +
            "AutoInvite = false\n" +
            "ListenAddr = \"192.168.1.120:5060\"";
}