| | |
| | | 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.bytedeco.javacv.OpenCVFrameConverter; |
| | | import org.bytedeco.javacv.*; |
| | | import org.bytedeco.opencv.global.opencv_imgcodecs; |
| | | import org.bytedeco.opencv.opencv_core.Mat; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.context.request.async.DeferredResult; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.BufferedReader; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.net.*; |
| | |
| | | public class PlayController { |
| | | |
| | | private final static Logger logger = LoggerFactory.getLogger(PlayController.class); |
| | | |
| | | @Value("${platform.upload}") |
| | | public String upload; |
| | | @Autowired |
| | | private SIPCommander cmder; |
| | | @Autowired |
| | |
| | | public DeferredResult<WVPResult<String>> playReturnImg(HttpServletRequest request, @PathVariable String deviceId, |
| | | @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); |
| | | |
| | | RequestMessage requestMessage = new RequestMessage(); |
| | | String key = DeferredResultHolder.CALLBACK_CMD_PLAY + deviceId + channelId; |
| | | requestMessage.setKey(key); |
| | | String uuid = UUID.randomUUID().toString(); |
| | | requestMessage.setId(uuid); |
| | | //20秒存活 |
| | | |
| | | //存活时间 |
| | | DeferredResult<WVPResult<String>> result = new DeferredResult<>(120 * 1000L); |
| | | |
| | | //120秒存活 |
| | | result.onTimeout(() -> { |
| | | logger.info("[点播等待超时] deviceId:{}, channelId:{}, ", deviceId, channelId); |
| | | // 释放rtpserver |
| | |
| | | // 此处必须释放所有请求 |
| | | resultHolder.invokeAllResult(requestMessage); |
| | | }); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | |
| | | grabber = new FFmpegFrameGrabber(rtspUrl); |
| | | // grabber.setOption("rtsp_transport", "tcp"); // 使用tcp的方式,不然会丢包很严重 |
| | | // grabber.setVideoOption("probesize", "10000"); // 设置捕获分析的最大字节 |
| | | grabber.start(); |
| | | // grabber.setFrameRate(25); |
| | | grabber.startUnsafe(); |
| | | Frame frame = grabber.grabImage(); // 直接捕获一帧 |
| | | // //转换图像 |
| | | // Java2DFrameConverter converter = new Java2DFrameConverter(); |
| | | // BufferedImage srcImage = converter.getBufferedImage(frame); |
| | | // if (srcImage!=null) { |
| | | // //创建文件 |
| | | // File file = new File(picPath); |
| | | // //输出文件 |
| | | // ImageIO.write(srcImage, "png", file); |
| | | // } |
| | | |
| | | if (frame != null) { |
| | | System.out.println("成功捕获一帧"); |
| | | // 将Frame转换为Mat |
| | |
| | | |
| | | imgUrl = format.format(new Date()) + "_" + IdUtils.randomUUID() + ".png"; |
| | | // 生成图片路径 |
| | | String imgPath = "/home/zgyw/uploadPath/" + imgUrl; |
| | | String imgPath = upload + imgUrl; |
| | | System.out.println("图片保存地址:" + imgPath); |
| | | imgUrl = "/profile/" + imgUrl; |
| | | // 保存图片 |