panlinlin
2021-01-24 c1b3f2beb4b76e89d4938d2f7d5b31ff0f10220d
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
package com.genersoft.iot.vmp.gb28181.transmit.request.impl;
 
import javax.sdp.*;
import javax.sip.InvalidArgumentException;
import javax.sip.RequestEvent;
import javax.sip.SipException;
import javax.sip.address.SipURI;
import javax.sip.header.ContentTypeHeader;
import javax.sip.header.FromHeader;
import javax.sip.header.SubjectHeader;
import javax.sip.message.Request;
import javax.sip.message.Response;
 
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.conf.MediaServerConfig;
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommanderFroPlatform;
import com.genersoft.iot.vmp.gb28181.transmit.request.SIPRequestAbstractProcessor;
import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
import com.genersoft.iot.vmp.vmanager.play.bean.PlayResult;
import com.genersoft.iot.vmp.vmanager.service.IPlayService;
import gov.nist.javax.sdp.fields.SDPFormat;
import gov.nist.javax.sip.address.AddressImpl;
import gov.nist.javax.sip.address.SipUri;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
 
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.UUID;
import java.util.Vector;
 
/**    
 * @Description:处理INVITE请求
 * @author: panll
 * @date:   2021年1月14日
 */
public class InviteRequestProcessor extends SIPRequestAbstractProcessor {
 
    private final static Logger logger = LoggerFactory.getLogger(MessageRequestProcessor.class);
 
    private SIPCommanderFroPlatform cmderFroPlatform;
 
    private IVideoManagerStorager storager;
 
    private IRedisCatchStorage  redisCatchStorage;
 
    private SIPCommander cmder;
 
    private IPlayService playService;
 
    private ZLMRTPServerFactory zlmrtpServerFactory;
 
    public ZLMRTPServerFactory getZlmrtpServerFactory() {
        return zlmrtpServerFactory;
    }
 
    public void setZlmrtpServerFactory(ZLMRTPServerFactory zlmrtpServerFactory) {
        this.zlmrtpServerFactory = zlmrtpServerFactory;
    }
 
    /**
     * 处理invite请求
     * 
     * @param evt
     *            请求消息
     */ 
    @Override
    public void process(RequestEvent evt) {
        //  Invite Request消息实现,此消息一般为级联消息,上级给下级发送请求视频指令
        try {
            Request request = evt.getRequest();
            SipURI sipURI = (SipURI) request.getRequestURI();
            String channelId = sipURI.getUser();
            String platformId = null;
//            SubjectHeader subjectHeader = (SubjectHeader)request.getHeader(SubjectHeader.NAME);
//            // 查询通道是否存在 不存在回复404
//            if (subjectHeader != null) { // 存在则从subjectHeader 获取平台信息
//                String subject = subjectHeader.getSubject();
//                if (subject != null) {
//                    String[] info1 = subject.split(",");
//                    if (info1 != null && info1 .length == 2) {
//                        String[] info2 = info1[1].split(":");
//                        if (info2 != null && info2.length == 2) {
//                            platformId = info2[0];
//                        }
//                    }
//                }
//            }
 
            FromHeader fromHeader = (FromHeader)request.getHeader(FromHeader.NAME);
            AddressImpl address = (AddressImpl) fromHeader.getAddress();
            SipUri uri = (SipUri) address.getURI();
            platformId = uri.getUser();
 
            if (platformId == null || channelId == null) {
                response400Ack(evt); // 参数不全, 发400,请求错误
                return;
            }
            // 查询平台下是否有该通道
            DeviceChannel channel = storager.queryChannelInParentPlatform(platformId, channelId);
            if (channel == null) {
                response404Ack(evt); // 通道不存在,发404,资源不存在
                return;
            }else {
                response100Ack(evt); // 通道存在,发100,trying
            }
            // 解析sdp消息, 使用jainsip 自带的sdp解析方式
            String contentString = new String(request.getRawContent());
            SessionDescription sdp = SdpFactory.getInstance().createSessionDescription(contentString);
 
            // TODO 区分TCP发流还是udp, 当前默认udp
            //  获取支持的格式
            Vector mediaDescriptions = sdp.getMediaDescriptions(true);
            // 查看是否支持PS 负载96
            String ip = null;
            int port = -1;
            for (int i = 0; i < mediaDescriptions.size(); i++) {
                MediaDescription mediaDescription = (MediaDescription)mediaDescriptions.get(i);
                Media media = mediaDescription.getMedia();
                port = media.getMediaPort();
            }
//            for (MediaDescription mediaDescription : mediaDescriptions) {
//
//                List<Codec> codecs = mediaDescription.getCodecs();
//                for (Codec codec : codecs) {
//                    if("96".equals(codec.getPayloadType()) || "PS".equals(codec.getName()) || "ps".equals(codec.getName())) {
//                        // TODO 这里很慢
//                        ip = mediaDescription.getIpAddress().getHostName();
//                        port = mediaDescription.getPort();
//                        break;
//                    }
//                }
//            }
//            if (ip == null || port == -1) { // TODO 没有合适的视频流格式, 可配置是否使用第一个media信息
//                if (mediaDescriptions.size() > 0) {
//                    ip = mediaDescriptions.get(0).getIpAddress().getHostName();
//                    port = mediaDescriptions.get(0).getPort();
//                }
//            }
//
//            if (ip == null || port == -1) {
//                response488Ack(evt);
//                return;
//            }
//
//
//            String ssrc = sdp.getSsrc();
//
//            Device device = storager.queryVideoDeviceByPlatformIdAndChannelId(platformId, channelId);
//            if (device == null) {
//                logger.warn("点播平台{}的通道{}时未找到设备信息", platformId, channel);
//                response500Ack(evt);
//                return;
//            }
//
//            // 通知下级推流,
//            PlayResult playResult = playService.play(device.getDeviceId(), channelId, (responseJSON)->{
//                // 收到推流, 回复200OK
//                UUID uuid = UUID.randomUUID();
//                int rtpServer = zlmrtpServerFactory.createRTPServer(uuid.toString());
//                if (rtpServer == -1) {
//                    logger.error("为获取到可用端口");
//                    return;
//                }else {
//                    zlmrtpServerFactory.closeRTPServer(uuid.toString());
//                }
//                // TODO 添加对tcp的支持
//                MediaServerConfig mediaInfo = redisCatchStorage.getMediaInfo();
//                StringBuffer content = new StringBuffer(200);
//                content.append("v=0\r\n");
//                content.append("o="+"00000"+" 0 0 IN IP4 "+mediaInfo.getWanIp()+"\r\n");
//                content.append("s=Play\r\n");
//                content.append("c=IN IP4 "+mediaInfo.getWanIp()+"\r\n");
//                content.append("t=0 0\r\n");
//                content.append("m=video "+ rtpServer+" RTP/AVP 96\r\n");
//                content.append("a=sendonly\r\n");
//                content.append("a=rtpmap:96 PS/90000\r\n");
//                content.append("y="+ ssrc + "\r\n");
//                content.append("f=\r\n");
//
//                try {
//                    responseAck(evt, content.toString());
//                } catch (SipException e) {
//                    e.printStackTrace();
//                } catch (InvalidArgumentException e) {
//                    e.printStackTrace();
//                } catch (ParseException e) {
//                    e.printStackTrace();
//                }
//
//                // 写入redis, 超时时回复
////                redisCatchStorage.waiteAck()
//            },(event -> {
//                // 未知错误。直接转发设备点播的错误
//                Response response = null;
//                try {
//                    response = getMessageFactory().createResponse(event.getResponse().getStatusCode(), evt.getRequest());
//                    getServerTransaction(evt).sendResponse(response);
//
//                } catch (ParseException | SipException | InvalidArgumentException e) {
//                    e.printStackTrace();
//                }
//            }));
//            playResult.getResult();
            // 查找合适的端口推流,
            // 收到ack后调用推流接口
 
 
 
 
        } catch (SipException | InvalidArgumentException | ParseException e) {
            e.printStackTrace();
            logger.warn("sdp解析错误");
            e.printStackTrace();
        } catch (SdpParseException e) {
            e.printStackTrace();
        } catch (SdpException e) {
            e.printStackTrace();
        }
 
    }
 
    /***
     * 回复100 trying
     * @param evt
     * @throws SipException
     * @throws InvalidArgumentException
     * @throws ParseException
     */
    private void response100Ack(RequestEvent evt) throws SipException, InvalidArgumentException, ParseException {
        Response response = getMessageFactory().createResponse(Response.TRYING, evt.getRequest());
        getServerTransaction(evt).sendResponse(response);
    }
 
    /***
     * 回复200 OK
     * @param evt
     * @throws SipException
     * @throws InvalidArgumentException
     * @throws ParseException
     */
    private void responseAck(RequestEvent evt, String sdp) throws SipException, InvalidArgumentException, ParseException {
        Response response = getMessageFactory().createResponse(Response.OK, evt.getRequest());
        ContentTypeHeader contentTypeHeader = getHeaderFactory().createContentTypeHeader("APPLICATION", "SDP");
        response.setContent(sdp, contentTypeHeader);
        getServerTransaction(evt).sendResponse(response);
    }
 
    /***
     * 回复400
     * @param evt
     * @throws SipException
     * @throws InvalidArgumentException
     * @throws ParseException
     */
    private void response400Ack(RequestEvent evt) throws SipException, InvalidArgumentException, ParseException {
        Response response = getMessageFactory().createResponse(Response.BAD_REQUEST, evt.getRequest());
        getServerTransaction(evt).sendResponse(response);
    }
 
    /***
     * 回复404
     * @param evt
     * @throws SipException
     * @throws InvalidArgumentException
     * @throws ParseException
     */
    private void response404Ack(RequestEvent evt) throws SipException, InvalidArgumentException, ParseException {
        Response response = getMessageFactory().createResponse(Response.NOT_FOUND, evt.getRequest());
        getServerTransaction(evt).sendResponse(response);
    }
 
    /***
     * 回复488
     * @param evt
     * @throws SipException
     * @throws InvalidArgumentException
     * @throws ParseException
     */
    private void response488Ack(RequestEvent evt) throws SipException, InvalidArgumentException, ParseException {
        Response response = getMessageFactory().createResponse(Response.NOT_ACCEPTABLE_HERE, evt.getRequest());
        getServerTransaction(evt).sendResponse(response);
    }
 
    /***
     * 回复500
     * @param evt
     * @throws SipException
     * @throws InvalidArgumentException
     * @throws ParseException
     */
    private void response500Ack(RequestEvent evt) throws SipException, InvalidArgumentException, ParseException {
        Response response = getMessageFactory().createResponse(Response.SERVER_INTERNAL_ERROR, evt.getRequest());
        getServerTransaction(evt).sendResponse(response);
    }
 
 
 
 
 
 
 
 
 
 
 
 
    public SIPCommanderFroPlatform getCmderFroPlatform() {
        return cmderFroPlatform;
    }
 
    public void setCmderFroPlatform(SIPCommanderFroPlatform cmderFroPlatform) {
        this.cmderFroPlatform = cmderFroPlatform;
    }
 
    public IVideoManagerStorager getStorager() {
        return storager;
    }
 
    public void setStorager(IVideoManagerStorager storager) {
        this.storager = storager;
    }
 
    public SIPCommander getCmder() {
        return cmder;
    }
 
    public void setCmder(SIPCommander cmder) {
        this.cmder = cmder;
    }
 
    public IPlayService getPlayService() {
        return playService;
    }
 
    public void setPlayService(IPlayService playService) {
        this.playService = playService;
    }
}