From 9361943e47a09ea46f76adf06fa0d24a07ac711d Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期一, 26 十月 2020 11:40:46 +0800
Subject: [PATCH] 修复ui录象播放 优化录象查询接口

---
 src/main/java/com/genersoft/iot/vmp/vmanager/play/PlayController.java |   61 +++++++++++++++++-------------
 1 files changed, 35 insertions(+), 26 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/play/PlayController.java b/src/main/java/com/genersoft/iot/vmp/vmanager/play/PlayController.java
index 996039e..ea2a062 100644
--- a/src/main/java/com/genersoft/iot/vmp/vmanager/play/PlayController.java
+++ b/src/main/java/com/genersoft/iot/vmp/vmanager/play/PlayController.java
@@ -7,6 +7,7 @@
 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.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.CrossOrigin;
@@ -41,41 +42,49 @@
 	public ResponseEntity<String> play(@PathVariable String deviceId,@PathVariable String channelId){
 		
 		Device device = storager.queryVideoDevice(deviceId);
-		StreamInfo streamInfo = cmder.playStreamCmd(device, channelId);
-		// 绛夊緟鎺ㄦ祦, TODO 榛樿瓒呮椂15s
+		StreamInfo streamInfo = storager.queryPlayByDevice(deviceId, channelId);
+
+		if (streamInfo == null) {
+			streamInfo = cmder.playStreamCmd(device, channelId);
+		}else {
+			String streamId = String.format("%08x", Integer.parseInt(streamInfo.getSsrc())).toUpperCase();
+			JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(streamId);
+			if (rtpInfo.getBoolean("exist")) {
+				return new ResponseEntity<String>(JSON.toJSONString(streamInfo),HttpStatus.OK);
+			}else {
+				storager.stopPlay(streamInfo);
+				streamInfo = cmder.playStreamCmd(device, channelId);
+			}
+
+		}
+		String streamId = String.format("%08x", Integer.parseInt(streamInfo.getSsrc())).toUpperCase();
+		// 绛夊緟鎺ㄦ祦, TODO 榛樿瓒呮椂30s
 		boolean lockFlag = true;
 		long startTime = System.currentTimeMillis();
-		String streamId = String.format("%08x", Integer.parseInt(streamInfo.getSsrc())).toUpperCase();
 
-		// 鍒ゆ柇鎺ㄦ祦鏄惁瀛樺湪
 		while (lockFlag) {
 			try {
-				if (System.currentTimeMillis() - startTime > 15 * 1000) {
+				if (System.currentTimeMillis() - startTime > 30 * 1000) {
+					storager.stopPlay(streamInfo);
+					return new ResponseEntity<String>("timeout",HttpStatus.OK);
+				}else {
+					streamInfo = storager.queryPlayByDevice(deviceId, channelId);
 					JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(streamId);
-					if (rtpInfo == null){
-						continue;
-					}else {
-						lockFlag = false;
-						streamInfo = storager.queryPlay(streamInfo);
-						// 鑾峰彇濯掍綋淇℃伅
-						JSONObject mediaList = zlmresTfulUtils.getMediaList("rtp", "rtmp");
-						if (mediaList.getInteger("code") == 0) {
-							JSONArray data = mediaList.getJSONArray("data");
-							if (data!= null) {
-								for (Object datum : data) {
-									JSONObject media = (JSONObject)datum;
-									if (streamId.equals(media.getString("stream"))) {
-										streamInfo.setTracks(media.getJSONArray("tracks"));
-										storager.startPlay(streamInfo);
-									}
-								}
-							}
+					if (rtpInfo != null && rtpInfo.getBoolean("exist") && streamInfo != null && streamInfo.getFlv() != null){
+						JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo("rtp", "rtmp", streamId);
+						if (mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online")) {
+							lockFlag = false;
+							JSONArray tracks = mediaInfo.getJSONArray("tracks");
+							streamInfo.setTracks(tracks);
+							storager.startPlay(streamInfo);
+						}else {
+
 						}
+					}else {
+						Thread.sleep(2000);
+						continue;
 					};
-
 				}
-
-				Thread.sleep(200);
 			} catch (InterruptedException e) {
 				e.printStackTrace();
 			}

--
Gitblit v1.8.0