Commit 24cd9158d418bdacd88d080a2c80ac814be57948

Authored by Lawrence
1 parent e4e62098

录像回放时同样根据设置决定是否事先取编码信息

src/main/java/com/genersoft/iot/vmp/vmanager/playback/PlaybackController.java
@@ -7,6 +7,7 @@ import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils; @@ -7,6 +7,7 @@ import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
7 import org.slf4j.Logger; 7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory; 8 import org.slf4j.LoggerFactory;
9 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.beans.factory.annotation.Value;
10 import org.springframework.http.HttpStatus; 11 import org.springframework.http.HttpStatus;
11 import org.springframework.http.ResponseEntity; 12 import org.springframework.http.ResponseEntity;
12 import org.springframework.util.StringUtils; 13 import org.springframework.util.StringUtils;
@@ -26,29 +27,33 @@ import com.genersoft.iot.vmp.storager.IVideoManagerStorager; @@ -26,29 +27,33 @@ import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
26 @RestController 27 @RestController
27 @RequestMapping("/api") 28 @RequestMapping("/api")
28 public class PlaybackController { 29 public class PlaybackController {
29 - 30 +
30 private final static Logger logger = LoggerFactory.getLogger(PlaybackController.class); 31 private final static Logger logger = LoggerFactory.getLogger(PlaybackController.class);
31 - 32 +
32 @Autowired 33 @Autowired
33 private SIPCommander cmder; 34 private SIPCommander cmder;
34 - 35 +
35 @Autowired 36 @Autowired
36 private IVideoManagerStorager storager; 37 private IVideoManagerStorager storager;
37 38
38 @Autowired 39 @Autowired
39 private ZLMRESTfulUtils zlmresTfulUtils; 40 private ZLMRESTfulUtils zlmresTfulUtils;
40 41
  42 + @Value("${media.closeWaitRTPInfo}")
  43 + private boolean closeWaitRTPInfo;
  44 +
41 @GetMapping("/playback/{deviceId}/{channelId}") 45 @GetMapping("/playback/{deviceId}/{channelId}")
42 - public ResponseEntity<String> play(@PathVariable String deviceId,@PathVariable String channelId, String startTime, String endTime){  
43 - 46 + public ResponseEntity<String> play(@PathVariable String deviceId, @PathVariable String channelId, String startTime,
  47 + String endTime) {
  48 +
44 if (logger.isDebugEnabled()) { 49 if (logger.isDebugEnabled()) {
45 - logger.debug(String.format("设备回放 API调用,deviceId:%s ,channelId:%s",deviceId, channelId)); 50 + logger.debug(String.format("设备回放 API调用,deviceId:%s ,channelId:%s", deviceId, channelId));
46 } 51 }
47 - 52 +
48 if (StringUtils.isEmpty(deviceId) || StringUtils.isEmpty(channelId)) { 53 if (StringUtils.isEmpty(deviceId) || StringUtils.isEmpty(channelId)) {
49 - String log = String.format("设备回放 API调用失败,deviceId:%s ,channelId:%s",deviceId, channelId); 54 + String log = String.format("设备回放 API调用失败,deviceId:%s ,channelId:%s", deviceId, channelId);
50 logger.warn(log); 55 logger.warn(log);
51 - return new ResponseEntity<String>(log,HttpStatus.BAD_REQUEST); 56 + return new ResponseEntity<String>(log, HttpStatus.BAD_REQUEST);
52 } 57 }
53 58
54 Device device = storager.queryVideoDevice(deviceId); 59 Device device = storager.queryVideoDevice(deviceId);
@@ -58,20 +63,22 @@ public class PlaybackController { @@ -58,20 +63,22 @@ public class PlaybackController {
58 cmder.streamByeCmd(streamInfo.getSsrc()); 63 cmder.streamByeCmd(streamInfo.getSsrc());
59 } 64 }
60 65
61 -// }else {  
62 -// String streamId = String.format("%08x", Integer.parseInt(streamInfo.getSsrc())).toUpperCase();  
63 -// JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(streamId);  
64 -// if (rtpInfo.getBoolean("exist")) {  
65 -// return new ResponseEntity<String>(JSON.toJSONString(streamInfo),HttpStatus.OK);  
66 -// }else {  
67 -// storager.stopPlayback(streamInfo);  
68 -// streamInfo = cmder.playbackStreamCmd(device, channelId, startTime, endTime);  
69 -// }  
70 -// } 66 + // }else {
  67 + // String streamId = String.format("%08x",
  68 + // Integer.parseInt(streamInfo.getSsrc())).toUpperCase();
  69 + // JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(streamId);
  70 + // if (rtpInfo.getBoolean("exist")) {
  71 + // return new
  72 + // ResponseEntity<String>(JSON.toJSONString(streamInfo),HttpStatus.OK);
  73 + // }else {
  74 + // storager.stopPlayback(streamInfo);
  75 + // streamInfo = cmder.playbackStreamCmd(device, channelId, startTime, endTime);
  76 + // }
  77 + // }
71 streamInfo = cmder.playbackStreamCmd(device, channelId, startTime, endTime); 78 streamInfo = cmder.playbackStreamCmd(device, channelId, startTime, endTime);
72 79
73 String streamId = String.format("%08x", Integer.parseInt(streamInfo.getSsrc())).toUpperCase(); 80 String streamId = String.format("%08x", Integer.parseInt(streamInfo.getSsrc())).toUpperCase();
74 - 81 +
75 if (logger.isDebugEnabled()) { 82 if (logger.isDebugEnabled()) {
76 logger.debug("设备回放 API调用,ssrc:" + streamInfo.getSsrc() + ",ZLMedia streamId:" + streamId); 83 logger.debug("设备回放 API调用,ssrc:" + streamInfo.getSsrc() + ",ZLMedia streamId:" + streamId);
77 } 84 }
@@ -81,62 +88,71 @@ public class PlaybackController { @@ -81,62 +88,71 @@ public class PlaybackController {
81 long lockStartTime = System.currentTimeMillis(); 88 long lockStartTime = System.currentTimeMillis();
82 JSONObject rtpInfo = null; 89 JSONObject rtpInfo = null;
83 90
84 - while (lockFlag) {  
85 - try {  
86 - if (System.currentTimeMillis() - lockStartTime > 75 * 1000) {  
87 - storager.stopPlayback(streamInfo);  
88 - logger.info("播放等待超时");  
89 - return new ResponseEntity<String>("timeout",HttpStatus.OK);  
90 - }else {  
91 - streamInfo = storager.queryPlaybackByDevice(deviceId, channelId);  
92 - if (!rtpPushed) {  
93 - logger.info("查询RTP推流信息...");  
94 - rtpInfo = zlmresTfulUtils.getRtpInfo(streamId);  
95 - }  
96 - if (rtpInfo != null && rtpInfo.getBoolean("exist") && streamInfo != null && streamInfo.getFlv() != null){  
97 - logger.info("查询流编码信息:"+streamInfo.getFlv());  
98 - rtpPushed = true;  
99 - Thread.sleep(2000);  
100 - JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo("rtp", "rtmp", streamId);  
101 - if (mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online")) {  
102 - lockFlag = false;  
103 - logger.info("流编码信息已获取");  
104 - JSONArray tracks = mediaInfo.getJSONArray("tracks");  
105 - streamInfo.setTracks(tracks);  
106 - storager.startPlayback(streamInfo);  
107 - }else {  
108 - logger.info("流编码信息未获取,2秒后重试..."); 91 + if (closeWaitRTPInfo) {
  92 + String flv = storager.getMediaInfo().getWanIp() + ":" + storager.getMediaInfo().getHttpPort() + "/rtp/"
  93 + + streamId + ".flv";
  94 + streamInfo.setFlv("http://" + flv);
  95 + streamInfo.setWs_flv("ws://" + flv);
  96 + storager.startPlayback(streamInfo);
  97 + } else {
  98 + while (lockFlag) {
  99 + try {
  100 + if (System.currentTimeMillis() - lockStartTime > 75 * 1000) {
  101 + storager.stopPlayback(streamInfo);
  102 + logger.info("播放等待超时");
  103 + return new ResponseEntity<String>("timeout", HttpStatus.OK);
  104 + } else {
  105 + streamInfo = storager.queryPlaybackByDevice(deviceId, channelId);
  106 + if (!rtpPushed) {
  107 + logger.info("查询RTP推流信息...");
  108 + rtpInfo = zlmresTfulUtils.getRtpInfo(streamId);
  109 + }
  110 + if (rtpInfo != null && rtpInfo.getBoolean("exist") && streamInfo != null
  111 + && streamInfo.getFlv() != null) {
  112 + logger.info("查询流编码信息:" + streamInfo.getFlv());
  113 + rtpPushed = true;
  114 + Thread.sleep(2000);
  115 + JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo("rtp", "rtmp", streamId);
  116 + if (mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online")) {
  117 + lockFlag = false;
  118 + logger.info("流编码信息已获取");
  119 + JSONArray tracks = mediaInfo.getJSONArray("tracks");
  120 + streamInfo.setTracks(tracks);
  121 + storager.startPlayback(streamInfo);
  122 + } else {
  123 + logger.info("流编码信息未获取,2秒后重试...");
  124 + }
  125 + } else {
  126 + Thread.sleep(2000);
  127 + continue;
109 } 128 }
110 - }else {  
111 - Thread.sleep(2000);  
112 - continue;  
113 } 129 }
  130 + } catch (InterruptedException e) {
  131 + e.printStackTrace();
114 } 132 }
115 - } catch (InterruptedException e) {  
116 - e.printStackTrace();  
117 } 133 }
118 } 134 }
119 - if(streamInfo!=null) {  
120 - return new ResponseEntity<String>(JSON.toJSONString(streamInfo),HttpStatus.OK); 135 + if (streamInfo != null) {
  136 + return new ResponseEntity<String>(JSON.toJSONString(streamInfo), HttpStatus.OK);
121 } else { 137 } else {
122 logger.warn("设备回放API调用失败!"); 138 logger.warn("设备回放API调用失败!");
123 return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR); 139 return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
124 } 140 }
125 } 141 }
126 - 142 +
127 @RequestMapping("/playback/{ssrc}/stop") 143 @RequestMapping("/playback/{ssrc}/stop")
128 - public ResponseEntity<String> playStop(@PathVariable String ssrc){  
129 - 144 + public ResponseEntity<String> playStop(@PathVariable String ssrc) {
  145 +
130 cmder.streamByeCmd(ssrc); 146 cmder.streamByeCmd(ssrc);
131 - 147 +
132 if (logger.isDebugEnabled()) { 148 if (logger.isDebugEnabled()) {
133 logger.debug(String.format("设备录像回放停止 API调用,ssrc:%s", ssrc)); 149 logger.debug(String.format("设备录像回放停止 API调用,ssrc:%s", ssrc));
134 } 150 }
135 -  
136 - if(ssrc!=null) { 151 +
  152 + if (ssrc != null) {
137 JSONObject json = new JSONObject(); 153 JSONObject json = new JSONObject();
138 json.put("ssrc", ssrc); 154 json.put("ssrc", ssrc);
139 - return new ResponseEntity<String>(json.toString(),HttpStatus.OK); 155 + return new ResponseEntity<String>(json.toString(), HttpStatus.OK);
140 } else { 156 } else {
141 logger.warn("设备录像回放停止API调用失败!"); 157 logger.warn("设备录像回放停止API调用失败!");
142 return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR); 158 return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);