| | |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.genersoft.iot.vmp.common.InviteInfo; |
| | | import com.genersoft.iot.vmp.common.InviteSessionType; |
| | | import com.genersoft.iot.vmp.common.StreamInfo; |
| | | import com.genersoft.iot.vmp.conf.UserSetting; |
| | |
| | | import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander; |
| | | import com.genersoft.iot.vmp.media.bean.MediaServer; |
| | | import com.genersoft.iot.vmp.media.service.IMediaServerService; |
| | | import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils; |
| | | import com.genersoft.iot.vmp.service.IInviteStreamService; |
| | | import com.genersoft.iot.vmp.service.IPlayService; |
| | | import com.genersoft.iot.vmp.service.bean.InviteErrorCode; |
| | |
| | | import io.swagger.v3.oas.annotations.Parameter; |
| | | import io.swagger.v3.oas.annotations.security.SecurityRequirement; |
| | | import io.swagger.v3.oas.annotations.tags.Tag; |
| | | import okhttp3.HttpUrl; |
| | | import okhttp3.OkHttpClient; |
| | | import okhttp3.Request; |
| | | import okhttp3.Response; |
| | | import org.bytedeco.javacv.FFmpegFrameGrabber; |
| | | import org.bytedeco.javacv.Frame; |
| | | import org.bytedeco.javacv.FrameGrabber; |
| | |
| | | import org.springframework.web.context.request.async.DeferredResult; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.net.MalformedURLException; |
| | | import java.net.URL; |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.net.*; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.UUID; |
| | | |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private SIPCommander cmder; |
| | | |
| | | @Autowired |
| | | private ZLMRESTfulUtils zlmresTfulUtils; |
| | | @Autowired |
| | | private VideoStreamSessionManager streamSession; |
| | | |
| | |
| | | @Parameter(name = "channelId", description = "通道国标编号", required = true) |
| | | @GetMapping("/start/img/{deviceId}/{channelId}") |
| | | public DeferredResult<WVPResult<String>> playReturnImg(HttpServletRequest request, @PathVariable String deviceId, |
| | | @PathVariable String channelId) { |
| | | @PathVariable String channelId) throws IOException { |
| | | logger.info("[开始点播] deviceId:{}, channelId:{}, ", deviceId, channelId); |
| | | DeferredResult<WVPResult<String>> result = new DeferredResult<>(20 * 1000L); |
| | | //已经存在流 |
| | | InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channelId); |
| | | if (inviteInfo != null) { |
| | | logger.info("已存在流"); |
| | | if (inviteInfo.getStreamInfo() != null) { |
| | | // 已存在线直接截图 |
| | | MediaServer mediaServerItemInuse = mediaServerService.getOne(inviteInfo.getStreamInfo().getMediaServerId()); |
| | | String streamUrl; |
| | | if (mediaServerItemInuse.getRtspPort() != 0) { |
| | | streamUrl = String.format("rtsp://127.0.0.1:%s/%s/%s", mediaServerItemInuse.getRtspPort(), "rtp", inviteInfo.getStreamInfo().getStream()); |
| | | } else { |
| | | streamUrl = String.format("http://127.0.0.1:%s/%s/%s.live.mp4", mediaServerItemInuse.getHttpPort(), "rtp", inviteInfo.getStreamInfo().getStream()); |
| | | } |
| | | WVPResult<String> wvpResult = new WVPResult<>(); |
| | | String img = getImg(streamUrl); |
| | | wvpResult.setData(img); |
| | | wvpResult.setCode(ErrorCode.SUCCESS.getCode()); |
| | | wvpResult.setMsg(ErrorCode.SUCCESS.getMsg()); |
| | | result.setResult(wvpResult); |
| | | } |
| | | } else { |
| | | logger.info("未存在流"); |
| | | // 获取可用的zlm |
| | | Device device = storager.queryVideoDevice(deviceId); |
| | | MediaServer newMediaServerItem = playService.getNewMediaServerItem(device); |
| | | |
| | | DeferredResult<WVPResult<StreamContent>> play = this.play(request, deviceId, channelId); |
| | | Object resultStr = play.getResult(); |
| | | System.out.println("获取结果:" + play); |
| | | System.out.println("获取结果:" + resultStr); |
| | | WVPResult wvpResult = (WVPResult) resultStr; |
| | | WVPResult<String> result = new WVPResult<>(); |
| | | result.setData(this.getImg(wvpResult)); |
| | | result.setCode(wvpResult.getCode()); |
| | | result.setMsg(wvpResult.getMsg()); |
| | | DeferredResult<WVPResult<String>> r = new DeferredResult<>(userSetting.getPlayTimeout().longValue()); |
| | | r.setResult(result); |
| | | return r; |
| | | RequestMessage requestMessage = new RequestMessage(); |
| | | String key = DeferredResultHolder.CALLBACK_CMD_PLAY + deviceId + channelId; |
| | | requestMessage.setKey(key); |
| | | String uuid = UUID.randomUUID().toString(); |
| | | requestMessage.setId(uuid); |
| | | //20秒存活 |
| | | result.onTimeout(() -> { |
| | | logger.info("[点播等待超时] deviceId:{}, channelId:{}, ", deviceId, channelId); |
| | | // 释放rtpserver |
| | | WVPResult<StreamInfo> wvpResult = new WVPResult<>(); |
| | | wvpResult.setCode(ErrorCode.ERROR100.getCode()); |
| | | wvpResult.setMsg("点播超时"); |
| | | requestMessage.setData(wvpResult); |
| | | resultHolder.invokeAllResult(requestMessage); |
| | | inviteStreamService.removeInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, deviceId, channelId); |
| | | storager.stopPlay(deviceId, channelId); |
| | | }); |
| | | |
| | | // 录像查询以channelId作为deviceId查询 |
| | | resultHolder.put(key, uuid, result); |
| | | |
| | | playService.play(newMediaServerItem, deviceId, channelId, null, (code, msg, data) -> { |
| | | WVPResult<String> wvpResult = new WVPResult<>(); |
| | | if (code == InviteErrorCode.SUCCESS.getCode()) { |
| | | wvpResult.setCode(ErrorCode.SUCCESS.getCode()); |
| | | wvpResult.setMsg(ErrorCode.SUCCESS.getMsg()); |
| | | |
| | | if (data != null) { |
| | | StreamInfo streamInfo = (StreamInfo) data; |
| | | if (userSetting.getUseSourceIpAsStreamIp()) { |
| | | streamInfo = streamInfo.clone();//深拷贝 |
| | | String host; |
| | | try { |
| | | URL url = new URL(request.getRequestURL().toString()); |
| | | host = url.getHost(); |
| | | } catch (MalformedURLException e) { |
| | | host = request.getLocalAddr(); |
| | | } |
| | | streamInfo.channgeStreamIp(host); |
| | | } |
| | | if (!ObjectUtils.isEmpty(newMediaServerItem.getTranscodeSuffix()) && !"null".equalsIgnoreCase(newMediaServerItem.getTranscodeSuffix())) { |
| | | streamInfo.setStream(streamInfo.getStream() + "_" + newMediaServerItem.getTranscodeSuffix()); |
| | | } |
| | | StreamContent streamContent = new StreamContent(streamInfo); |
| | | String rtspUrl = streamContent.getFmp4(); // 取mp4地址 |
| | | String img = getImg(rtspUrl); |
| | | wvpResult.setData(img); |
| | | } else { |
| | | wvpResult.setCode(code); |
| | | wvpResult.setMsg(msg); |
| | | } |
| | | } else { |
| | | wvpResult.setCode(code); |
| | | wvpResult.setMsg(msg); |
| | | } |
| | | requestMessage.setData(wvpResult); |
| | | // 此处必须释放所有请求 |
| | | resultHolder.invokeAllResult(requestMessage); |
| | | }); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private String getImg(WVPResult<StreamContent> wvpResult) { |
| | | private String getImg(String rtspUrl) { |
| | | String imgUrl = null; |
| | | if (wvpResult.getCode() == 0) { |
| | | String rtspUrl = wvpResult.getData().getFmp4(); // 取mp4地址 |
| | | if (StringUtils.hasText(rtspUrl)) { |
| | | System.out.println("目标地址:" + rtspUrl); |
| | | FFmpegFrameGrabber grabber = null; |
| | |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | System.out.println("请求失败,错误码:" + wvpResult.getCode() + "--" + wvpResult.getMsg()); |
| | | } |
| | | System.out.println("图片URL:" + imgUrl); |
| | | return imgUrl; |
| | | } |
| | | |
| | |
| | | json.put("channelId", channelId); |
| | | return json; |
| | | } |
| | | |
| | | /** |
| | | * 结束转码 |
| | | */ |