|  |  |  | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.exception.SsrcTransactionNotFoundException; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.bean.*; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.event.SipSubscribe; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.session.SSRCFactory; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.IRedisCatchStorage; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.IVideoManagerStorage; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.utils.DateUtil; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.utils.redis.RedisUtil; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.vmanager.bean.WVPResult; | 
|---|
|  |  |  | import org.slf4j.Logger; | 
|---|
|  |  |  | import org.slf4j.LoggerFactory; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Qualifier; | 
|---|
|  |  |  | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | 
|---|
|  |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | import org.springframework.web.context.request.async.DeferredResult; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.sip.InvalidArgumentException; | 
|---|
|  |  |  | import javax.sip.ResponseEvent; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ZlmHttpHookSubscribe subscribe; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Qualifier("taskExecutor") | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ThreadPoolTaskExecutor taskExecutor; | 
|---|
|  |  |  | private SSRCFactory ssrcFactory; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RedisTemplate<Object, Object> redisTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void play(MediaServerItem mediaServerItem, String deviceId, String channelId, | 
|---|
|  |  |  | 
|---|
|  |  |  | onPublishHandlerForPlay(mediaServerItemInuse, response, device.getDeviceId(), channelId); | 
|---|
|  |  |  | hookEvent.response(mediaServerItemInuse, response); | 
|---|
|  |  |  | logger.info("[点播成功] deviceId: {}, channelId: {}", device.getDeviceId(), channelId); | 
|---|
|  |  |  | String streamUrl = String.format("http://127.0.0.1:%s/%s/%s.live.flv", mediaServerItemInuse.getHttpPort(), "rtp",  ssrcInfo.getStream()); | 
|---|
|  |  |  | String streamUrl; | 
|---|
|  |  |  | if (mediaServerItemInuse.getRtspPort() != 0) { | 
|---|
|  |  |  | streamUrl = String.format("rtsp://127.0.0.1:%s/%s/%s", mediaServerItemInuse.getRtspPort(), "rtp",  ssrcInfo.getStream()); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | streamUrl = String.format("http://127.0.0.1:%s/%s/%s.live.mp4", mediaServerItemInuse.getHttpPort(), "rtp",  ssrcInfo.getStream()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String path = "snap"; | 
|---|
|  |  |  | String fileName = device.getDeviceId() + "_" + channelId + ".jpg"; | 
|---|
|  |  |  | // 请求截图 | 
|---|
|  |  |  | 
|---|
|  |  |  | // 检查是否有y字段 | 
|---|
|  |  |  | if (ssrcIndex >= 0) { | 
|---|
|  |  |  | //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 TODO 后续对不规范的非10位ssrc兼容 | 
|---|
|  |  |  | String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12); | 
|---|
|  |  |  | String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12).trim(); | 
|---|
|  |  |  | // 查询到ssrc不一致且开启了ssrc校验则需要针对处理 | 
|---|
|  |  |  | if (ssrcInfo.getSsrc().equals(ssrcInResponse)) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | 
|---|
|  |  |  | if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) { | 
|---|
|  |  |  | logger.info("[点播消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!mediaServerItem.getSsrcConfig().checkSsrc(ssrcInResponse)) { | 
|---|
|  |  |  | if (!ssrcFactory.checkSsrc(mediaServerItem.getId(),ssrcInResponse)) { | 
|---|
|  |  |  | // ssrc 不可用 | 
|---|
|  |  |  | // 释放ssrc | 
|---|
|  |  |  | mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); | 
|---|
|  |  |  | ssrcFactory.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); | 
|---|
|  |  |  | streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream()); | 
|---|
|  |  |  | event.msg = "下级自定义了ssrc,但是此ssrc不可用"; | 
|---|
|  |  |  | event.statusCode = 400; | 
|---|
|  |  |  | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 关闭rtp server | 
|---|
|  |  |  | mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream()); | 
|---|
|  |  |  | // 重新开启ssrc server | 
|---|
|  |  |  | mediaServerService.openRTPServer(mediaServerItem, ssrcInfo.getStream(), ssrcInResponse, device.isSsrcCheck(), false, ssrcInfo.getPort()); | 
|---|
|  |  |  | mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream(), result->{ | 
|---|
|  |  |  | if (result) { | 
|---|
|  |  |  | // 重新开启ssrc server | 
|---|
|  |  |  | mediaServerService.openRTPServer(mediaServerItem, ssrcInfo.getStream(), ssrcInResponse, device.isSsrcCheck(), false, ssrcInfo.getPort()); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | logger.warn("[停止点播] {}/{}", device.getDeviceId(), channelId); | 
|---|
|  |  |  | cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null, null); | 
|---|
|  |  |  | } catch (InvalidArgumentException | SipException | ParseException | SsrcTransactionNotFoundException e) { | 
|---|
|  |  |  | logger.error("[命令发送失败] 停止点播, 发送BYE: {}", e.getMessage()); | 
|---|
|  |  |  | throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | dynamicTask.stop(timeOutTaskKey); | 
|---|
|  |  |  | // 释放ssrc | 
|---|
|  |  |  | mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream()); | 
|---|
|  |  |  | event.msg = "下级自定义了ssrc,重新设置收流信息失败"; | 
|---|
|  |  |  | event.statusCode = 500; | 
|---|
|  |  |  | errorEvent.response(event); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void onPublishHandlerForPlayback(MediaServerItem mediaServerItem, JSONObject response, String deviceId, String channelId, String uuid) { | 
|---|
|  |  |  | RequestMessage msg = new RequestMessage(); | 
|---|
|  |  |  | msg.setKey(DeferredResultHolder.CALLBACK_CMD_PLAY + deviceId + channelId); | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(uuid)) { | 
|---|
|  |  |  | msg.setId(uuid); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | StreamInfo streamInfo = onPublishHandler(mediaServerItem, response, deviceId, channelId); | 
|---|
|  |  |  | private void onPublishHandlerForPlayback(MediaServerItem mediaServerItem, JSONObject response, String deviceId, String channelId, PlayBackCallback playBackCallback) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | StreamInfo streamInfo = onPublishHandler(mediaServerItem, response, deviceId, channelId); | 
|---|
|  |  |  | PlayBackResult<StreamInfo> playBackResult = new PlayBackResult<>(); | 
|---|
|  |  |  | if (streamInfo != null) { | 
|---|
|  |  |  | DeviceChannel deviceChannel = storager.queryChannel(deviceId, channelId); | 
|---|
|  |  |  | if (deviceChannel != null) { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | redisCatchStorage.startPlay(streamInfo); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | WVPResult wvpResult = new WVPResult(); | 
|---|
|  |  |  | wvpResult.setCode(ErrorCode.SUCCESS.getCode()); | 
|---|
|  |  |  | wvpResult.setMsg(ErrorCode.SUCCESS.getMsg()); | 
|---|
|  |  |  | wvpResult.setData(streamInfo); | 
|---|
|  |  |  | msg.setData(wvpResult); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | resultHolder.invokeAllResult(msg); | 
|---|
|  |  |  | playBackResult.setCode(ErrorCode.SUCCESS.getCode()); | 
|---|
|  |  |  | playBackResult.setMsg(ErrorCode.SUCCESS.getMsg()); | 
|---|
|  |  |  | playBackResult.setData(streamInfo); | 
|---|
|  |  |  | playBackCallback.call(playBackResult); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | logger.warn("录像回放调用失败!"); | 
|---|
|  |  |  | msg.setData(WVPResult.fail(ErrorCode.ERROR100.getCode(), "录像回放调用失败!")); | 
|---|
|  |  |  | resultHolder.invokeAllResult(msg); | 
|---|
|  |  |  | playBackResult.setCode(ErrorCode.ERROR100.getCode()); | 
|---|
|  |  |  | playBackResult.setMsg("录像回放调用失败!"); | 
|---|
|  |  |  | playBackCallback.call(playBackResult); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | MediaServerItem mediaServerItem; | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(device.getMediaServerId()) || "auto".equals(device.getMediaServerId())) { | 
|---|
|  |  |  | mediaServerItem = mediaServerService.getMediaServerForMinimumLoad(); | 
|---|
|  |  |  | mediaServerItem = mediaServerService.getMediaServerForMinimumLoad(null); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | mediaServerItem = mediaServerService.getOne(device.getMediaServerId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public DeferredResult<WVPResult<StreamInfo>> playBack(String deviceId, String channelId, String startTime, | 
|---|
|  |  |  | public MediaServerItem getNewMediaServerItemHasAssist(Device device) { | 
|---|
|  |  |  | if (device == null) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | MediaServerItem mediaServerItem; | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(device.getMediaServerId()) || "auto".equals(device.getMediaServerId())) { | 
|---|
|  |  |  | mediaServerItem = mediaServerService.getMediaServerForMinimumLoad(true); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | mediaServerItem = mediaServerService.getOne(device.getMediaServerId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (mediaServerItem == null) { | 
|---|
|  |  |  | logger.warn("[获取可用的ZLM节点]未找到可使用的ZLM..."); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return mediaServerItem; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void playBack(String deviceId, String channelId, String startTime, | 
|---|
|  |  |  | String endTime, InviteStreamCallback inviteStreamCallback, | 
|---|
|  |  |  | PlayBackCallback callback) { | 
|---|
|  |  |  | Device device = storager.queryVideoDevice(deviceId); | 
|---|
|  |  |  | if (device == null) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | MediaServerItem newMediaServerItem = getNewMediaServerItem(device); | 
|---|
|  |  |  | SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, null, device.isSsrcCheck(), true); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return playBack(newMediaServerItem, ssrcInfo, deviceId, channelId, startTime, endTime, inviteStreamCallback, callback); | 
|---|
|  |  |  | playBack(newMediaServerItem, ssrcInfo, deviceId, channelId, startTime, endTime, inviteStreamCallback, callback); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public DeferredResult<WVPResult<StreamInfo>> playBack(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo, | 
|---|
|  |  |  | public void playBack(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo, | 
|---|
|  |  |  | String deviceId, String channelId, String startTime, | 
|---|
|  |  |  | String endTime, InviteStreamCallback infoCallBack, | 
|---|
|  |  |  | PlayBackCallback playBackCallback) { | 
|---|
|  |  |  | if (mediaServerItem == null || ssrcInfo == null) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String uuid = UUID.randomUUID().toString(); | 
|---|
|  |  |  | String key = DeferredResultHolder.CALLBACK_CMD_PLAYBACK + deviceId + channelId; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Device device = storager.queryVideoDevice(deviceId); | 
|---|
|  |  |  | if (device == null) { | 
|---|
|  |  |  | throw new ControllerException(ErrorCode.ERROR100.getCode(), "设备: " + deviceId + "不存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | DeferredResult<WVPResult<StreamInfo>> result = new DeferredResult<>(30000L); | 
|---|
|  |  |  | resultHolder.put(DeferredResultHolder.CALLBACK_CMD_PLAYBACK + deviceId + channelId, uuid, result); | 
|---|
|  |  |  | RequestMessage requestMessage = new RequestMessage(); | 
|---|
|  |  |  | requestMessage.setId(uuid); | 
|---|
|  |  |  | requestMessage.setKey(key); | 
|---|
|  |  |  | PlayBackResult<RequestMessage> playBackResult = new PlayBackResult<>(); | 
|---|
|  |  |  | logger.info("[回放消息] deviceId: {}, channelId: {},收流端口: {}, 收流模式:{}, SSRC: {}, SSRC校验:{}", device.getDeviceId(), channelId, ssrcInfo.getPort(), device.getStreamMode(), ssrcInfo.getSsrc(), device.isSsrcCheck()); | 
|---|
|  |  |  | PlayBackResult<StreamInfo> playBackResult = new PlayBackResult<>(); | 
|---|
|  |  |  | String playBackTimeOutTaskKey = UUID.randomUUID().toString(); | 
|---|
|  |  |  | dynamicTask.startDelay(playBackTimeOutTaskKey, () -> { | 
|---|
|  |  |  | logger.warn(String.format("设备回放超时,deviceId:%s ,channelId:%s", deviceId, channelId)); | 
|---|
|  |  |  | playBackResult.setCode(ErrorCode.ERROR100.getCode()); | 
|---|
|  |  |  | playBackResult.setMsg("回放超时"); | 
|---|
|  |  |  | playBackResult.setData(requestMessage); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null); | 
|---|
|  |  |  | 
|---|
|  |  |  | mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream()); | 
|---|
|  |  |  | streamSession.remove(deviceId, channelId, ssrcInfo.getStream()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 回复之前所有的点播请求 | 
|---|
|  |  |  | playBackCallback.call(playBackResult); | 
|---|
|  |  |  | result.setResult(WVPResult.fail(ErrorCode.ERROR100.getCode(), "回放超时")); | 
|---|
|  |  |  | resultHolder.exist(DeferredResultHolder.CALLBACK_CMD_PLAYBACK + deviceId + channelId, uuid); | 
|---|
|  |  |  | }, userSetting.getPlayTimeout()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | SipSubscribe.Event errorEvent = event -> { | 
|---|
|  |  |  | dynamicTask.stop(playBackTimeOutTaskKey); | 
|---|
|  |  |  | requestMessage.setData(WVPResult.fail(ErrorCode.ERROR100.getCode(), String.format("回放失败, 错误码: %s, %s", event.statusCode, event.msg))); | 
|---|
|  |  |  | playBackResult.setCode(ErrorCode.ERROR100.getCode()); | 
|---|
|  |  |  | playBackResult.setMsg(String.format("回放失败, 错误码: %s, %s", event.statusCode, event.msg)); | 
|---|
|  |  |  | playBackResult.setData(requestMessage); | 
|---|
|  |  |  | playBackResult.setEvent(event); | 
|---|
|  |  |  | playBackCallback.call(playBackResult); | 
|---|
|  |  |  | streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream()); | 
|---|
|  |  |  | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | redisCatchStorage.startPlayback(streamInfo, inviteStreamInfo.getCallId()); | 
|---|
|  |  |  | WVPResult<StreamInfo> success = WVPResult.success(streamInfo); | 
|---|
|  |  |  | requestMessage.setData(success); | 
|---|
|  |  |  | playBackResult.setCode(ErrorCode.SUCCESS.getCode()); | 
|---|
|  |  |  | playBackResult.setMsg(ErrorCode.SUCCESS.getMsg()); | 
|---|
|  |  |  | playBackResult.setData(requestMessage); | 
|---|
|  |  |  | playBackResult.setData(streamInfo); | 
|---|
|  |  |  | playBackResult.setMediaServerItem(inviteStreamInfo.getMediaServerItem()); | 
|---|
|  |  |  | playBackResult.setResponse(inviteStreamInfo.getResponse()); | 
|---|
|  |  |  | playBackCallback.call(playBackResult); | 
|---|
|  |  |  | 
|---|
|  |  |  | if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) { | 
|---|
|  |  |  | logger.info("[回放消息] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!mediaServerItem.getSsrcConfig().checkSsrc(ssrcInResponse)) { | 
|---|
|  |  |  | if (!ssrcFactory.checkSsrc(mediaServerItem.getId(),ssrcInResponse)) { | 
|---|
|  |  |  | // ssrc 不可用 | 
|---|
|  |  |  | // 释放ssrc | 
|---|
|  |  |  | dynamicTask.stop(playBackTimeOutTaskKey); | 
|---|
|  |  |  | mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); | 
|---|
|  |  |  | streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream()); | 
|---|
|  |  |  | eventResult.msg = "下级自定义了ssrc,但是此ssrc不可用"; | 
|---|
|  |  |  | 
|---|
|  |  |  | logger.info("[ZLM HOOK] ssrc修正后收到订阅消息: " + response.toJSONString()); | 
|---|
|  |  |  | dynamicTask.stop(playBackTimeOutTaskKey); | 
|---|
|  |  |  | // hook响应 | 
|---|
|  |  |  | onPublishHandlerForPlayback(mediaServerItemInUse, response, device.getDeviceId(), channelId, uuid); | 
|---|
|  |  |  | onPublishHandlerForPlayback(mediaServerItemInUse, response, device.getDeviceId(), channelId, playBackCallback); | 
|---|
|  |  |  | hookEvent.call(new InviteStreamInfo(mediaServerItem, null, eventResult.callId, "rtp", ssrcInfo.getStream())); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 关闭rtp server | 
|---|
|  |  |  | mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream()); | 
|---|
|  |  |  | // 重新开启ssrc server | 
|---|
|  |  |  | mediaServerService.openRTPServer(mediaServerItem, ssrcInfo.getStream(), ssrcInResponse, device.isSsrcCheck(), true, ssrcInfo.getPort()); | 
|---|
|  |  |  | mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream(), result->{ | 
|---|
|  |  |  | if (result) { | 
|---|
|  |  |  | // 重新开启ssrc server | 
|---|
|  |  |  | mediaServerService.openRTPServer(mediaServerItem, ssrcInfo.getStream(), ssrcInResponse, device.isSsrcCheck(), true, ssrcInfo.getPort()); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | logger.warn("[回放消息]停止 {}/{}", device.getDeviceId(), channelId); | 
|---|
|  |  |  | cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null, null); | 
|---|
|  |  |  | } catch (InvalidArgumentException | SipException | ParseException | SsrcTransactionNotFoundException e) { | 
|---|
|  |  |  | logger.error("[命令发送失败] 停止点播 停止, 发送BYE: {}", e.getMessage()); | 
|---|
|  |  |  | throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | dynamicTask.stop(playBackTimeOutTaskKey); | 
|---|
|  |  |  | // 释放ssrc | 
|---|
|  |  |  | mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); | 
|---|
|  |  |  | streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream()); | 
|---|
|  |  |  | errorEvent.response(eventResult); | 
|---|
|  |  |  | eventResult.msg = "下级自定义了ssrc,重新设置收流信息失败"; | 
|---|
|  |  |  | eventResult.statusCode = 500; | 
|---|
|  |  |  | errorEvent.response(eventResult); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | eventResult.msg = "命令发送失败"; | 
|---|
|  |  |  | errorEvent.response(eventResult); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public DeferredResult<WVPResult<StreamInfo>> download(String deviceId, String channelId, String startTime, String endTime, int downloadSpeed, InviteStreamCallback infoCallBack, PlayBackCallback hookCallBack) { | 
|---|
|  |  |  | public void download(String deviceId, String channelId, String startTime, String endTime, int downloadSpeed, InviteStreamCallback infoCallBack, PlayBackCallback playBackCallback) { | 
|---|
|  |  |  | Device device = storager.queryVideoDevice(deviceId); | 
|---|
|  |  |  | if (device == null) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | MediaServerItem newMediaServerItem = getNewMediaServerItem(device); | 
|---|
|  |  |  | MediaServerItem newMediaServerItem = getNewMediaServerItemHasAssist(device); | 
|---|
|  |  |  | if (newMediaServerItem == null) { | 
|---|
|  |  |  | PlayBackResult<StreamInfo> downloadResult = new PlayBackResult<>(); | 
|---|
|  |  |  | downloadResult.setCode(ErrorCode.ERROR100.getCode()); | 
|---|
|  |  |  | downloadResult.setMsg("未找到assist服务"); | 
|---|
|  |  |  | playBackCallback.call(downloadResult); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | SSRCInfo ssrcInfo = mediaServerService.openRTPServer(newMediaServerItem, null, device.isSsrcCheck(), true); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return download(newMediaServerItem, ssrcInfo, deviceId, channelId, startTime, endTime, downloadSpeed, infoCallBack, hookCallBack); | 
|---|
|  |  |  | download(newMediaServerItem, ssrcInfo, deviceId, channelId, startTime, endTime, downloadSpeed, infoCallBack, playBackCallback); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public DeferredResult<WVPResult<StreamInfo>> download(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo, String deviceId, String channelId, String startTime, String endTime, int downloadSpeed, InviteStreamCallback infoCallBack, PlayBackCallback hookCallBack) { | 
|---|
|  |  |  | public void download(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo, String deviceId, String channelId, String startTime, String endTime, int downloadSpeed, InviteStreamCallback infoCallBack, PlayBackCallback hookCallBack) { | 
|---|
|  |  |  | if (mediaServerItem == null || ssrcInfo == null) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String uuid = UUID.randomUUID().toString(); | 
|---|
|  |  |  | String key = DeferredResultHolder.CALLBACK_CMD_DOWNLOAD + deviceId + channelId; | 
|---|
|  |  |  | DeferredResult<WVPResult<StreamInfo>> result = new DeferredResult<>(30000L); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Device device = storager.queryVideoDevice(deviceId); | 
|---|
|  |  |  | if (device == null) { | 
|---|
|  |  |  | throw new ControllerException(ErrorCode.ERROR400.getCode(), "设备:" + deviceId + "不存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | resultHolder.put(key, uuid, result); | 
|---|
|  |  |  | RequestMessage requestMessage = new RequestMessage(); | 
|---|
|  |  |  | requestMessage.setId(uuid); | 
|---|
|  |  |  | requestMessage.setKey(key); | 
|---|
|  |  |  | WVPResult<StreamInfo> wvpResult = new WVPResult<>(); | 
|---|
|  |  |  | requestMessage.setData(wvpResult); | 
|---|
|  |  |  | PlayBackResult<RequestMessage> downloadResult = new PlayBackResult<>(); | 
|---|
|  |  |  | downloadResult.setData(requestMessage); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | PlayBackResult<StreamInfo> downloadResult = new PlayBackResult<>(); | 
|---|
|  |  |  | logger.info("[录像下载] deviceId: {}, channelId: {},收流端口: {}, 收流模式:{}, SSRC: {}, SSRC校验:{}", device.getDeviceId(), channelId, ssrcInfo.getPort(), device.getStreamMode(), ssrcInfo.getSsrc(), device.isSsrcCheck()); | 
|---|
|  |  |  | String downLoadTimeOutTaskKey = UUID.randomUUID().toString(); | 
|---|
|  |  |  | dynamicTask.startDelay(downLoadTimeOutTaskKey, () -> { | 
|---|
|  |  |  | logger.warn(String.format("录像下载请求超时,deviceId:%s ,channelId:%s", deviceId, channelId)); | 
|---|
|  |  |  | wvpResult.setCode(ErrorCode.ERROR100.getCode()); | 
|---|
|  |  |  | wvpResult.setMsg("录像下载请求超时"); | 
|---|
|  |  |  | downloadResult.setCode(ErrorCode.ERROR100.getCode()); | 
|---|
|  |  |  | downloadResult.setMsg("录像下载请求超时"); | 
|---|
|  |  |  | hookCallBack.call(downloadResult); | 
|---|
|  |  |  | 
|---|
|  |  |  | mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream()); | 
|---|
|  |  |  | streamSession.remove(deviceId, channelId, ssrcInfo.getStream()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 回复之前所有的点播请求 | 
|---|
|  |  |  | hookCallBack.call(downloadResult); | 
|---|
|  |  |  | }, userSetting.getPlayTimeout()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | SipSubscribe.Event errorEvent = event -> { | 
|---|
|  |  |  | dynamicTask.stop(downLoadTimeOutTaskKey); | 
|---|
|  |  |  | downloadResult.setCode(ErrorCode.ERROR100.getCode()); | 
|---|
|  |  |  | downloadResult.setMsg(String.format("录像下载失败, 错误码: %s, %s", event.statusCode, event.msg)); | 
|---|
|  |  |  | wvpResult.setCode(ErrorCode.ERROR100.getCode()); | 
|---|
|  |  |  | wvpResult.setMsg(String.format("录像下载失败, 错误码: %s, %s", event.statusCode, event.msg)); | 
|---|
|  |  |  | downloadResult.setEvent(event); | 
|---|
|  |  |  | hookCallBack.call(downloadResult); | 
|---|
|  |  |  | streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream()); | 
|---|
|  |  |  | }; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | InviteStreamCallback hookEvent = (InviteStreamInfo inviteStreamInfo) -> { | 
|---|
|  |  |  | logger.info("[录像下载]收到订阅消息: " + inviteStreamInfo.getCallId()); | 
|---|
|  |  |  | dynamicTask.stop(downLoadTimeOutTaskKey); | 
|---|
|  |  |  | StreamInfo streamInfo = onPublishHandler(inviteStreamInfo.getMediaServerItem(), inviteStreamInfo.getResponse(), deviceId, channelId); | 
|---|
|  |  |  | streamInfo.setStartTime(startTime); | 
|---|
|  |  |  | streamInfo.setEndTime(endTime); | 
|---|
|  |  |  | redisCatchStorage.startDownload(streamInfo, inviteStreamInfo.getCallId()); | 
|---|
|  |  |  | downloadResult.setCode(ErrorCode.SUCCESS.getCode()); | 
|---|
|  |  |  | downloadResult.setMsg(ErrorCode.SUCCESS.getMsg()); | 
|---|
|  |  |  | downloadResult.setData(streamInfo); | 
|---|
|  |  |  | downloadResult.setMediaServerItem(inviteStreamInfo.getMediaServerItem()); | 
|---|
|  |  |  | downloadResult.setResponse(inviteStreamInfo.getResponse()); | 
|---|
|  |  |  | hookCallBack.call(downloadResult); | 
|---|
|  |  |  | }; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | cmder.downloadStreamCmd(mediaServerItem, ssrcInfo, device, channelId, startTime, endTime, downloadSpeed, infoCallBack, | 
|---|
|  |  |  | inviteStreamInfo -> { | 
|---|
|  |  |  | logger.info("收到订阅消息: " + inviteStreamInfo.getResponse().toJSONString()); | 
|---|
|  |  |  | dynamicTask.stop(downLoadTimeOutTaskKey); | 
|---|
|  |  |  | StreamInfo streamInfo = onPublishHandler(inviteStreamInfo.getMediaServerItem(), inviteStreamInfo.getResponse(), deviceId, channelId); | 
|---|
|  |  |  | streamInfo.setStartTime(startTime); | 
|---|
|  |  |  | streamInfo.setEndTime(endTime); | 
|---|
|  |  |  | redisCatchStorage.startDownload(streamInfo, inviteStreamInfo.getCallId()); | 
|---|
|  |  |  | wvpResult.setCode(ErrorCode.SUCCESS.getCode()); | 
|---|
|  |  |  | wvpResult.setMsg(ErrorCode.SUCCESS.getMsg()); | 
|---|
|  |  |  | wvpResult.setData(streamInfo); | 
|---|
|  |  |  | downloadResult.setCode(ErrorCode.SUCCESS.getCode()); | 
|---|
|  |  |  | downloadResult.setMsg(ErrorCode.SUCCESS.getMsg()); | 
|---|
|  |  |  | downloadResult.setMediaServerItem(inviteStreamInfo.getMediaServerItem()); | 
|---|
|  |  |  | downloadResult.setResponse(inviteStreamInfo.getResponse()); | 
|---|
|  |  |  | hookCallBack.call(downloadResult); | 
|---|
|  |  |  | }, errorEvent); | 
|---|
|  |  |  | hookEvent, errorEvent, eventResult -> | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (eventResult.type == SipSubscribe.EventResultType.response) { | 
|---|
|  |  |  | ResponseEvent responseEvent = (ResponseEvent) eventResult.event; | 
|---|
|  |  |  | String contentString = new String(responseEvent.getResponse().getRawContent()); | 
|---|
|  |  |  | // 获取ssrc | 
|---|
|  |  |  | int ssrcIndex = contentString.indexOf("y="); | 
|---|
|  |  |  | // 检查是否有y字段 | 
|---|
|  |  |  | if (ssrcIndex >= 0) { | 
|---|
|  |  |  | //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段 TODO 后续对不规范的非10位ssrc兼容 | 
|---|
|  |  |  | String ssrcInResponse = contentString.substring(ssrcIndex + 2, ssrcIndex + 12); | 
|---|
|  |  |  | // 查询到ssrc不一致且开启了ssrc校验则需要针对处理 | 
|---|
|  |  |  | if (ssrcInfo.getSsrc().equals(ssrcInResponse)) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | logger.info("[录像下载] 收到invite 200, 发现下级自定义了ssrc: {}", ssrcInResponse); | 
|---|
|  |  |  | if (!mediaServerItem.isRtpEnable() || device.isSsrcCheck()) { | 
|---|
|  |  |  | logger.info("[录像下载] SSRC修正 {}->{}", ssrcInfo.getSsrc(), ssrcInResponse); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!ssrcFactory.checkSsrc(mediaServerItem.getId(),ssrcInResponse)) { | 
|---|
|  |  |  | // ssrc 不可用 | 
|---|
|  |  |  | // 释放ssrc | 
|---|
|  |  |  | mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); | 
|---|
|  |  |  | streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream()); | 
|---|
|  |  |  | eventResult.msg = "下级自定义了ssrc,但是此ssrc不可用"; | 
|---|
|  |  |  | eventResult.statusCode = 400; | 
|---|
|  |  |  | errorEvent.response(eventResult); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 单端口模式streamId也有变化,需要重新设置监听 | 
|---|
|  |  |  | if (!mediaServerItem.isRtpEnable()) { | 
|---|
|  |  |  | // 添加订阅 | 
|---|
|  |  |  | HookSubscribeForStreamChange hookSubscribe = HookSubscribeFactory.on_stream_changed("rtp", ssrcInfo.getStream(), true, "rtsp", mediaServerItem.getId()); | 
|---|
|  |  |  | subscribe.removeSubscribe(hookSubscribe); | 
|---|
|  |  |  | hookSubscribe.getContent().put("stream", String.format("%08x", Integer.parseInt(ssrcInResponse)).toUpperCase()); | 
|---|
|  |  |  | subscribe.addSubscribe(hookSubscribe, (MediaServerItem mediaServerItemInUse, JSONObject response) -> { | 
|---|
|  |  |  | logger.info("[ZLM HOOK] ssrc修正后收到订阅消息: " + response.toJSONString()); | 
|---|
|  |  |  | dynamicTask.stop(downLoadTimeOutTaskKey); | 
|---|
|  |  |  | // hook响应 | 
|---|
|  |  |  | onPublishHandlerForPlayback(mediaServerItemInUse, response, device.getDeviceId(), channelId, hookCallBack); | 
|---|
|  |  |  | hookEvent.call(new InviteStreamInfo(mediaServerItem, null, eventResult.callId, "rtp", ssrcInfo.getStream())); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 关闭rtp server | 
|---|
|  |  |  | mediaServerService.closeRTPServer(mediaServerItem, ssrcInfo.getStream(), result->{ | 
|---|
|  |  |  | if (result) { | 
|---|
|  |  |  | // 重新开启ssrc server | 
|---|
|  |  |  | mediaServerService.openRTPServer(mediaServerItem, ssrcInfo.getStream(), ssrcInResponse, device.isSsrcCheck(), true, ssrcInfo.getPort()); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | logger.warn("[录像下载] 停止{}/{}", device.getDeviceId(), channelId); | 
|---|
|  |  |  | cmder.streamByeCmd(device, channelId, ssrcInfo.getStream(), null, null); | 
|---|
|  |  |  | } catch (InvalidArgumentException | SipException | ParseException | SsrcTransactionNotFoundException e) { | 
|---|
|  |  |  | logger.error("[命令发送失败] 录像下载停止, 发送BYE: {}", e.getMessage()); | 
|---|
|  |  |  | throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | dynamicTask.stop(downLoadTimeOutTaskKey); | 
|---|
|  |  |  | // 释放ssrc | 
|---|
|  |  |  | mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream()); | 
|---|
|  |  |  | eventResult.msg = "下级自定义了ssrc,重新设置收流信息失败"; | 
|---|
|  |  |  | eventResult.statusCode = 500; | 
|---|
|  |  |  | errorEvent.response(eventResult); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } catch (InvalidArgumentException | SipException | ParseException e) { | 
|---|
|  |  |  | logger.error("[命令发送失败] 录像下载: {}", e.getMessage()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | eventResult.msg = "命令发送失败"; | 
|---|
|  |  |  | errorEvent.response(eventResult); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (mediaServerItem.getRecordAssistPort() > 0) { | 
|---|
|  |  |  | JSONObject jsonObject = assistRESTfulUtils.fileDuration(mediaServerItem, streamInfo.getApp(), streamInfo.getStream(), null); | 
|---|
|  |  |  | if (jsonObject != null && jsonObject.getInteger("code") == 0) { | 
|---|
|  |  |  | if (jsonObject == null) { | 
|---|
|  |  |  | throw new ControllerException(ErrorCode.ERROR100.getCode(), "连接Assist服务失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (jsonObject.getInteger("code") == 0) { | 
|---|
|  |  |  | long duration = jsonObject.getLong("data"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (duration == 0) { | 
|---|
|  |  |  | 
|---|
|  |  |  | throw new ServiceException("streamId不存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | streamInfo.setPause(true); | 
|---|
|  |  |  | RedisUtil.set(key, streamInfo); | 
|---|
|  |  |  | redisTemplate.opsForValue().set(key, streamInfo); | 
|---|
|  |  |  | MediaServerItem mediaServerItem = mediaServerService.getOne(streamInfo.getMediaServerId()); | 
|---|
|  |  |  | if (null == mediaServerItem) { | 
|---|
|  |  |  | logger.warn("mediaServer 不存在!"); | 
|---|
|  |  |  | 
|---|
|  |  |  | throw new ServiceException("streamId不存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | streamInfo.setPause(false); | 
|---|
|  |  |  | RedisUtil.set(key, streamInfo); | 
|---|
|  |  |  | redisTemplate.opsForValue().set(key, streamInfo); | 
|---|
|  |  |  | MediaServerItem mediaServerItem = mediaServerService.getOne(streamInfo.getMediaServerId()); | 
|---|
|  |  |  | if (null == mediaServerItem) { | 
|---|
|  |  |  | logger.warn("mediaServer 不存在!"); | 
|---|