|  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.common.StreamInfo; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.DynamicTask; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.SipConfig; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.UserSetting; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.bean.*; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.event.SipSubscribe; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage; | 
|---|
|  |  |  | 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.utils.DateUtil; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.utils.DateUtil; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.AssistRESTfulUtils; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.ZLMHttpHookSubscribe; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.springframework.web.context.request.async.DeferredResult; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.sip.ResponseEvent; | 
|---|
|  |  |  | import javax.sip.SipException; | 
|---|
|  |  |  | import java.io.FileNotFoundException; | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.text.ParseException; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  | import java.util.stream.Stream; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @SuppressWarnings(value = {"rawtypes", "unchecked"}) | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | 
|---|
|  |  |  | private UserSetting userSetting; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SipConfig sipConfig; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private DynamicTask dynamicTask; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ZLMHttpHookSubscribe subscribe; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | if (mediaServerItem.isRtpEnable()) { | 
|---|
|  |  |  | streamId = String.format("%s_%s", device.getDeviceId(), channelId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | SSRCInfo ssrcInfo = mediaServerService.openRTPServer(mediaServerItem, streamId, device.isSsrcCheck()); | 
|---|
|  |  |  | SSRCInfo ssrcInfo = mediaServerService.openRTPServer(mediaServerItem, streamId, device.isSsrcCheck(), false); | 
|---|
|  |  |  | play(mediaServerItem, ssrcInfo, device, channelId, (mediaServerItemInUse, response)->{ | 
|---|
|  |  |  | if (hookEvent != null) { | 
|---|
|  |  |  | hookEvent.response(mediaServerItem, response); | 
|---|
|  |  |  | 
|---|
|  |  |  | streamId = String.format("%s_%s", device.getDeviceId(), channelId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (ssrcInfo == null) { | 
|---|
|  |  |  | ssrcInfo = mediaServerService.openRTPServer(mediaServerItem, streamId, device.isSsrcCheck()); | 
|---|
|  |  |  | ssrcInfo = mediaServerService.openRTPServer(mediaServerItem, streamId, device.isSsrcCheck(), false); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 超时处理 | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }, userSetting.getPlayTimeout()*1000); | 
|---|
|  |  |  | final String ssrc = ssrcInfo.getSsrc(); | 
|---|
|  |  |  | final String stream = ssrcInfo.getStream(); | 
|---|
|  |  |  | cmder.playStreamCmd(mediaServerItem, ssrcInfo, device, channelId, (MediaServerItem mediaServerItemInuse, JSONObject response) -> { | 
|---|
|  |  |  | logger.info("收到订阅消息: " + response.toJSONString()); | 
|---|
|  |  |  | dynamicTask.stop(timeOutTaskKey); | 
|---|
|  |  |  | 
|---|
|  |  |  | if (ssrcIndex >= 0) { | 
|---|
|  |  |  | //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 TODO 后续对不规范的非10位ssrc兼容 | 
|---|
|  |  |  | String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12); | 
|---|
|  |  |  | if (!ssrc.equals(ssrcInResponse) && device.isSsrcCheck()) { // 查询到ssrc不一致且开启了ssrc校验则需要针对处理 | 
|---|
|  |  |  | // 查询 ssrcInResponse 是否可用 | 
|---|
|  |  |  | if (mediaServerItem.isRtpEnable() && !mediaServerItem.getSsrcConfig().checkSsrc(ssrcInResponse)) { | 
|---|
|  |  |  | // 查询到ssrc不一致且开启了ssrc校验则需要针对处理 | 
|---|
|  |  |  | if (ssrc.equals(ssrcInResponse)) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | logger.info("[SIP 消息] 收到invite 200, 发现下级自定义了ssrc 开启修正"); | 
|---|
|  |  |  | if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) { | 
|---|
|  |  |  | if (!mediaServerItem.getSsrcConfig().checkSsrc(ssrcInResponse)) { | 
|---|
|  |  |  | // ssrc 不可用 | 
|---|
|  |  |  | // 释放ssrc | 
|---|
|  |  |  | mediaServerService.releaseSsrc(mediaServerItem.getId(), finalSsrcInfo.getSsrc()); | 
|---|
|  |  |  | 
|---|
|  |  |  | errorEvent.response(event); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 单端口模式streamId也有变化,需要重新设置监听 | 
|---|
|  |  |  | if (!mediaServerItem.isRtpEnable()) { | 
|---|
|  |  |  | // 添加订阅 | 
|---|
|  |  |  | JSONObject subscribeKey = new JSONObject(); | 
|---|
|  |  |  | subscribeKey.put("app", "rtp"); | 
|---|
|  |  |  | subscribeKey.put("stream", stream); | 
|---|
|  |  |  | subscribeKey.put("regist", true); | 
|---|
|  |  |  | subscribeKey.put("schema", "rtmp"); | 
|---|
|  |  |  | subscribeKey.put("mediaServerId", mediaServerItem.getId()); | 
|---|
|  |  |  | subscribe.removeSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed,subscribeKey); | 
|---|
|  |  |  | subscribeKey.put("stream", String.format("%08x", Integer.parseInt(ssrcInResponse)).toUpperCase()); | 
|---|
|  |  |  | subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey, | 
|---|
|  |  |  | (MediaServerItem mediaServerItemInUse, JSONObject response)->{ | 
|---|
|  |  |  | logger.info("[ZLM HOOK] ssrc修正后收到订阅消息: " + response.toJSONString()); | 
|---|
|  |  |  | dynamicTask.stop(timeOutTaskKey); | 
|---|
|  |  |  | // hook响应 | 
|---|
|  |  |  | onPublishHandlerForPlay(mediaServerItemInUse, response, device.getDeviceId(), channelId, uuid); | 
|---|
|  |  |  | hookEvent.response(mediaServerItemInUse, response); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 关闭rtp server | 
|---|
|  |  |  | mediaServerService.closeRTPServer(device.getDeviceId(), channelId, finalSsrcInfo.getStream()); | 
|---|
|  |  |  | // 重新开启ssrc server | 
|---|
|  |  |  | mediaServerService.openRTPServer(mediaServerItem, finalSsrcInfo.getStream(), ssrcInResponse, device.isSsrcCheck(), false); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }, (event) -> { | 
|---|
|  |  |  | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | MediaServerItem newMediaServerItem = getNewMediaServerItem(device); | 
|---|
|  |  |  | SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, null, true); | 
|---|
|  |  |  | SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, null, true, true); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return playBack(newMediaServerItem, ssrcInfo, deviceId, channelId, startTime, endTime, inviteStreamCallback, callback); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | MediaServerItem newMediaServerItem = getNewMediaServerItem(device); | 
|---|
|  |  |  | SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, null, true); | 
|---|
|  |  |  | SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, null, true, true); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return download(newMediaServerItem, ssrcInfo, deviceId, channelId, startTime, endTime, downloadSpeed,infoCallBack, hookCallBack); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 查询通道使用状态 | 
|---|
|  |  |  | if (audioBroadcastManager.exit(device.getDeviceId(), channelId)) { | 
|---|
|  |  |  | logger.warn("语音广播已经开启: {}", channelId); | 
|---|
|  |  |  | event.call("语音广播已经开启"); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | SendRtpItem sendRtpItem =  redisCatchStorage.querySendRTPServer(device.getDeviceId(), channelId, null, null); | 
|---|
|  |  |  | if (sendRtpItem != null && sendRtpItem.isOnlyAudio()) { | 
|---|
|  |  |  | logger.warn("语音广播已经开启: {}", channelId); | 
|---|
|  |  |  | event.call("语音广播已经开启"); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String timeOutTaskKey = "audio-broadcast-" + device.getDeviceId() + channelId; | 
|---|
|  |  |  | dynamicTask.startDelay(timeOutTaskKey, ()->{ | 
|---|
|  |  |  | logger.error("语音广播发送超时: {}:{}", device.getDeviceId(), channelId); | 
|---|
|  |  |  | event.call("语音广播发送超时"); | 
|---|
|  |  |  | audioBroadcastManager.del(device.getDeviceId(), channelId); | 
|---|
|  |  |  | }, timeout * 1000); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 发送通知 | 
|---|
|  |  |  | cmder.audioBroadcastCmd(device, channelId, eventResultForOk -> { | 
|---|
|  |  |  | 
|---|
|  |  |  | AudioBroadcastCatch audioBroadcastCatch = new AudioBroadcastCatch(device.getDeviceId(), channelId, AudioBroadcastCatchStatus.Ready); | 
|---|
|  |  |  | audioBroadcastManager.add(audioBroadcastCatch); | 
|---|
|  |  |  | }, eventResultForError -> { | 
|---|
|  |  |  | dynamicTask.stop(timeOutTaskKey); | 
|---|
|  |  |  | // 发送失败 | 
|---|
|  |  |  | logger.error("语音广播发送失败: {}:{}", channelId, eventResultForError.msg); | 
|---|
|  |  |  | event.call("语音广播发送失败"); | 
|---|
|  |  |  | audioBroadcastManager.del(device.getDeviceId(), channelId); | 
|---|
|  |  |  | stopAudioBroadcast(device.getDeviceId(), channelId); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void stopAudioBroadcast(String deviceId, String channelId){ | 
|---|
|  |  |  | AudioBroadcastCatch audioBroadcastCatch = audioBroadcastManager.get(deviceId, channelId); | 
|---|
|  |  |  | if (audioBroadcastCatch != null) { | 
|---|
|  |  |  | audioBroadcastManager.del(deviceId, audioBroadcastCatch.getChannelId()); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | SendRtpItem sendRtpItem =  redisCatchStorage.querySendRTPServer(deviceId, audioBroadcastCatch.getChannelId(), null, null); | 
|---|
|  |  |  | if (sendRtpItem != null) { | 
|---|
|  |  |  | redisCatchStorage.deleteSendRTPServer(deviceId, sendRtpItem.getChannelId(), null, null); | 
|---|
|  |  |  | MediaServerItem mediaInfo = mediaServerService.getOne(sendRtpItem.getMediaServerId()); | 
|---|
|  |  |  | Map<String, Object> param = new HashMap<>(); | 
|---|
|  |  |  | param.put("vhost", "__defaultVhost__"); | 
|---|
|  |  |  | param.put("app", sendRtpItem.getApp()); | 
|---|
|  |  |  | param.put("stream", sendRtpItem.getStreamId()); | 
|---|
|  |  |  | zlmresTfulUtils.stopSendRtp(mediaInfo, param); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (audioBroadcastCatch.getStatus() == AudioBroadcastCatchStatus.Ok) { | 
|---|
|  |  |  | cmder.streamByeCmd(audioBroadcastCatch.getDialog(), audioBroadcastCatch.getRequest(), null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (SipException e) { | 
|---|
|  |  |  | throw new RuntimeException(e); | 
|---|
|  |  |  | } catch (ParseException e) { | 
|---|
|  |  |  | throw new RuntimeException(e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|