Commit 37e97a782c43f658a5bfee7ac1df31bb86d9b955
1 parent
eecffb51
改正并优化查询RTP流信息的流程
Showing
2 changed files
with
60 additions
and
41 deletions
src/main/java/com/genersoft/iot/vmp/vmanager/play/PlayController.java
| @@ -26,32 +26,32 @@ import com.genersoft.iot.vmp.storager.IVideoManagerStorager; | @@ -26,32 +26,32 @@ import com.genersoft.iot.vmp.storager.IVideoManagerStorager; | ||
| 26 | @RestController | 26 | @RestController |
| 27 | @RequestMapping("/api") | 27 | @RequestMapping("/api") |
| 28 | public class PlayController { | 28 | public class PlayController { |
| 29 | - | 29 | + |
| 30 | private final static Logger logger = LoggerFactory.getLogger(PlayController.class); | 30 | private final static Logger logger = LoggerFactory.getLogger(PlayController.class); |
| 31 | - | 31 | + |
| 32 | @Autowired | 32 | @Autowired |
| 33 | private SIPCommander cmder; | 33 | private SIPCommander cmder; |
| 34 | - | 34 | + |
| 35 | @Autowired | 35 | @Autowired |
| 36 | private IVideoManagerStorager storager; | 36 | private IVideoManagerStorager storager; |
| 37 | 37 | ||
| 38 | @Autowired | 38 | @Autowired |
| 39 | private ZLMRESTfulUtils zlmresTfulUtils; | 39 | private ZLMRESTfulUtils zlmresTfulUtils; |
| 40 | - | 40 | + |
| 41 | @GetMapping("/play/{deviceId}/{channelId}") | 41 | @GetMapping("/play/{deviceId}/{channelId}") |
| 42 | - public ResponseEntity<String> play(@PathVariable String deviceId,@PathVariable String channelId){ | ||
| 43 | - | 42 | + public ResponseEntity<String> play(@PathVariable String deviceId, @PathVariable String channelId) { |
| 43 | + | ||
| 44 | Device device = storager.queryVideoDevice(deviceId); | 44 | Device device = storager.queryVideoDevice(deviceId); |
| 45 | StreamInfo streamInfo = storager.queryPlayByDevice(deviceId, channelId); | 45 | StreamInfo streamInfo = storager.queryPlayByDevice(deviceId, channelId); |
| 46 | 46 | ||
| 47 | if (streamInfo == null) { | 47 | if (streamInfo == null) { |
| 48 | streamInfo = cmder.playStreamCmd(device, channelId); | 48 | streamInfo = cmder.playStreamCmd(device, channelId); |
| 49 | - }else { | 49 | + } else { |
| 50 | String streamId = String.format("%08x", Integer.parseInt(streamInfo.getSsrc())).toUpperCase(); | 50 | String streamId = String.format("%08x", Integer.parseInt(streamInfo.getSsrc())).toUpperCase(); |
| 51 | JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(streamId); | 51 | JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(streamId); |
| 52 | if (rtpInfo.getBoolean("exist")) { | 52 | if (rtpInfo.getBoolean("exist")) { |
| 53 | - return new ResponseEntity<String>(JSON.toJSONString(streamInfo),HttpStatus.OK); | ||
| 54 | - }else { | 53 | + return new ResponseEntity<String>(JSON.toJSONString(streamInfo), HttpStatus.OK); |
| 54 | + } else { | ||
| 55 | storager.stopPlay(streamInfo); | 55 | storager.stopPlay(streamInfo); |
| 56 | streamInfo = cmder.playStreamCmd(device, channelId); | 56 | streamInfo = cmder.playStreamCmd(device, channelId); |
| 57 | } | 57 | } |
| @@ -60,34 +60,40 @@ public class PlayController { | @@ -60,34 +60,40 @@ public class PlayController { | ||
| 60 | String streamId = String.format("%08x", Integer.parseInt(streamInfo.getSsrc())).toUpperCase(); | 60 | String streamId = String.format("%08x", Integer.parseInt(streamInfo.getSsrc())).toUpperCase(); |
| 61 | // 等待推流, TODO 默认超时30s | 61 | // 等待推流, TODO 默认超时30s |
| 62 | boolean lockFlag = true; | 62 | boolean lockFlag = true; |
| 63 | + boolean rtpPushed = false; | ||
| 63 | long startTime = System.currentTimeMillis(); | 64 | long startTime = System.currentTimeMillis(); |
| 65 | + JSONObject rtpInfo = null; | ||
| 64 | 66 | ||
| 65 | while (lockFlag) { | 67 | while (lockFlag) { |
| 66 | try { | 68 | try { |
| 67 | - if (System.currentTimeMillis() - startTime > 30 * 1000) { | 69 | + if (System.currentTimeMillis() - startTime > 60 * 1000) { |
| 68 | storager.stopPlay(streamInfo); | 70 | storager.stopPlay(streamInfo); |
| 69 | logger.info("播放等待超时"); | 71 | logger.info("播放等待超时"); |
| 70 | - return new ResponseEntity<String>("timeout",HttpStatus.OK); | ||
| 71 | - }else { | 72 | + return new ResponseEntity<String>("timeout", HttpStatus.OK); |
| 73 | + } else { | ||
| 72 | streamInfo = storager.queryPlayByDevice(deviceId, channelId); | 74 | streamInfo = storager.queryPlayByDevice(deviceId, channelId); |
| 73 | - JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(streamId); | ||
| 74 | - if (rtpInfo != null && rtpInfo.getBoolean("exist") && streamInfo != null && streamInfo.getFlv() != null){ | ||
| 75 | - logger.info("RTP已推流,查询编码信息:"+streamInfo.getFlv()); | 75 | + if (!rtpPushed) { |
| 76 | + logger.info("查询RTP推流信息..."); | ||
| 77 | + rtpInfo = zlmresTfulUtils.getRtpInfo(streamId); | ||
| 78 | + } | ||
| 79 | + if (rtpInfo != null && rtpInfo.getBoolean("exist") && streamInfo != null && streamInfo.getFlv() != null) { | ||
| 80 | + logger.info("查询流编码信息:" + streamInfo.getFlv()); | ||
| 81 | + rtpPushed = true; | ||
| 76 | Thread.sleep(2000); | 82 | Thread.sleep(2000); |
| 77 | JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo("rtp", "rtmp", streamId); | 83 | JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo("rtp", "rtmp", streamId); |
| 78 | if (mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online")) { | 84 | if (mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online")) { |
| 79 | lockFlag = false; | 85 | lockFlag = false; |
| 80 | - logger.info("媒体编码信息已获取"); | 86 | + logger.info("流编码信息已获取"); |
| 81 | JSONArray tracks = mediaInfo.getJSONArray("tracks"); | 87 | JSONArray tracks = mediaInfo.getJSONArray("tracks"); |
| 82 | streamInfo.setTracks(tracks); | 88 | streamInfo.setTracks(tracks); |
| 83 | storager.startPlay(streamInfo); | 89 | storager.startPlay(streamInfo); |
| 84 | - }else { | ||
| 85 | - logger.info("媒体编码信息未获取,2秒后重试..."); | 90 | + } else { |
| 91 | + logger.info("流编码信息未获取,2秒后重试..."); | ||
| 86 | } | 92 | } |
| 87 | - }else { | 93 | + } else { |
| 88 | Thread.sleep(2000); | 94 | Thread.sleep(2000); |
| 89 | continue; | 95 | continue; |
| 90 | - }; | 96 | + } |
| 91 | } | 97 | } |
| 92 | } catch (InterruptedException e) { | 98 | } catch (InterruptedException e) { |
| 93 | e.printStackTrace(); | 99 | e.printStackTrace(); |
| @@ -95,33 +101,35 @@ public class PlayController { | @@ -95,33 +101,35 @@ public class PlayController { | ||
| 95 | } | 101 | } |
| 96 | 102 | ||
| 97 | if (logger.isDebugEnabled()) { | 103 | if (logger.isDebugEnabled()) { |
| 98 | - logger.debug(String.format("设备预览 API调用,deviceId:%s ,channelId:%s",deviceId, channelId)); | ||
| 99 | - logger.debug("设备预览 API调用,ssrc:"+streamInfo.getSsrc()+",ZLMedia streamId:"+Integer.toHexString(Integer.parseInt(streamInfo.getSsrc()))); | 104 | + logger.debug(String.format("设备预览 API调用,deviceId:%s ,channelId:%s", deviceId, channelId)); |
| 105 | + logger.debug("设备预览 API调用,ssrc:" + streamInfo.getSsrc() + ",ZLMedia streamId:" | ||
| 106 | + + Integer.toHexString(Integer.parseInt(streamInfo.getSsrc()))); | ||
| 100 | } | 107 | } |
| 101 | - | ||
| 102 | - if(streamInfo!=null) { | ||
| 103 | - return new ResponseEntity<String>(JSON.toJSONString(streamInfo),HttpStatus.OK); | 108 | + |
| 109 | + if (streamInfo != null) { | ||
| 110 | + return new ResponseEntity<String>(JSON.toJSONString(streamInfo), HttpStatus.OK); | ||
| 104 | } else { | 111 | } else { |
| 105 | logger.warn("设备预览API调用失败!"); | 112 | logger.warn("设备预览API调用失败!"); |
| 106 | return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR); | 113 | return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 107 | } | 114 | } |
| 108 | } | 115 | } |
| 109 | - | 116 | + |
| 110 | @PostMapping("/play/{ssrc}/stop") | 117 | @PostMapping("/play/{ssrc}/stop") |
| 111 | - public ResponseEntity<String> playStop(@PathVariable String ssrc){ | ||
| 112 | - | 118 | + public ResponseEntity<String> playStop(@PathVariable String ssrc) { |
| 119 | + | ||
| 113 | cmder.streamByeCmd(ssrc); | 120 | cmder.streamByeCmd(ssrc); |
| 114 | StreamInfo streamInfo = storager.queryPlayBySSRC(ssrc); | 121 | StreamInfo streamInfo = storager.queryPlayBySSRC(ssrc); |
| 115 | - if (streamInfo == null) return new ResponseEntity<String>(HttpStatus.PAYMENT_REQUIRED); | 122 | + if (streamInfo == null) |
| 123 | + return new ResponseEntity<String>(HttpStatus.PAYMENT_REQUIRED); | ||
| 116 | storager.stopPlay(streamInfo); | 124 | storager.stopPlay(streamInfo); |
| 117 | if (logger.isDebugEnabled()) { | 125 | if (logger.isDebugEnabled()) { |
| 118 | logger.debug(String.format("设备预览停止API调用,ssrc:%s", ssrc)); | 126 | logger.debug(String.format("设备预览停止API调用,ssrc:%s", ssrc)); |
| 119 | } | 127 | } |
| 120 | - | ||
| 121 | - if(ssrc!=null) { | 128 | + |
| 129 | + if (ssrc != null) { | ||
| 122 | JSONObject json = new JSONObject(); | 130 | JSONObject json = new JSONObject(); |
| 123 | json.put("ssrc", ssrc); | 131 | json.put("ssrc", ssrc); |
| 124 | - return new ResponseEntity<String>(json.toString(),HttpStatus.OK); | 132 | + return new ResponseEntity<String>(json.toString(), HttpStatus.OK); |
| 125 | } else { | 133 | } else { |
| 126 | logger.warn("设备预览停止API调用失败!"); | 134 | logger.warn("设备预览停止API调用失败!"); |
| 127 | return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR); | 135 | return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR); |
src/main/java/com/genersoft/iot/vmp/vmanager/playback/PlaybackController.java
| @@ -52,7 +52,7 @@ public class PlaybackController { | @@ -52,7 +52,7 @@ public class PlaybackController { | ||
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | Device device = storager.queryVideoDevice(deviceId); | 54 | Device device = storager.queryVideoDevice(deviceId); |
| 55 | - StreamInfo streamInfo = storager.queryPlayBlackByDevice(deviceId, channelId); | 55 | + StreamInfo streamInfo = storager.queryPlaybackByDevice(deviceId, channelId); |
| 56 | 56 | ||
| 57 | if (streamInfo != null) { | 57 | if (streamInfo != null) { |
| 58 | cmder.streamByeCmd(streamInfo.getSsrc()); | 58 | cmder.streamByeCmd(streamInfo.getSsrc()); |
| @@ -64,7 +64,7 @@ public class PlaybackController { | @@ -64,7 +64,7 @@ public class PlaybackController { | ||
| 64 | // if (rtpInfo.getBoolean("exist")) { | 64 | // if (rtpInfo.getBoolean("exist")) { |
| 65 | // return new ResponseEntity<String>(JSON.toJSONString(streamInfo),HttpStatus.OK); | 65 | // return new ResponseEntity<String>(JSON.toJSONString(streamInfo),HttpStatus.OK); |
| 66 | // }else { | 66 | // }else { |
| 67 | -// storager.stopPlayBlack(streamInfo); | 67 | +// storager.stopPlayback(streamInfo); |
| 68 | // streamInfo = cmder.playbackStreamCmd(device, channelId, startTime, endTime); | 68 | // streamInfo = cmder.playbackStreamCmd(device, channelId, startTime, endTime); |
| 69 | // } | 69 | // } |
| 70 | // } | 70 | // } |
| @@ -77,29 +77,40 @@ public class PlaybackController { | @@ -77,29 +77,40 @@ public class PlaybackController { | ||
| 77 | } | 77 | } |
| 78 | // 等待推流, TODO 默认超时15s | 78 | // 等待推流, TODO 默认超时15s |
| 79 | boolean lockFlag = true; | 79 | boolean lockFlag = true; |
| 80 | + boolean rtpPushed = false; | ||
| 80 | long lockStartTime = System.currentTimeMillis(); | 81 | long lockStartTime = System.currentTimeMillis(); |
| 82 | + JSONObject rtpInfo = null; | ||
| 83 | + | ||
| 81 | while (lockFlag) { | 84 | while (lockFlag) { |
| 82 | try { | 85 | try { |
| 83 | if (System.currentTimeMillis() - lockStartTime > 75 * 1000) { | 86 | if (System.currentTimeMillis() - lockStartTime > 75 * 1000) { |
| 84 | - storager.stopPlayBlack(streamInfo); | 87 | + storager.stopPlayback(streamInfo); |
| 88 | + logger.info("播放等待超时"); | ||
| 85 | return new ResponseEntity<String>("timeout",HttpStatus.OK); | 89 | return new ResponseEntity<String>("timeout",HttpStatus.OK); |
| 86 | }else { | 90 | }else { |
| 87 | - streamInfo = storager.queryPlayBlackByDevice(deviceId, channelId); | ||
| 88 | - JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(streamId); | ||
| 89 | - if (rtpInfo != null && rtpInfo.getBoolean("exist") && streamInfo.getFlv() != null){ | 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); | ||
| 90 | JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo("rtp", "rtmp", streamId); | 100 | JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo("rtp", "rtmp", streamId); |
| 91 | if (mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online")) { | 101 | if (mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online")) { |
| 92 | lockFlag = false; | 102 | lockFlag = false; |
| 103 | + logger.info("流编码信息已获取"); | ||
| 93 | JSONArray tracks = mediaInfo.getJSONArray("tracks"); | 104 | JSONArray tracks = mediaInfo.getJSONArray("tracks"); |
| 94 | streamInfo.setTracks(tracks); | 105 | streamInfo.setTracks(tracks); |
| 95 | - storager.startPlayBlack(streamInfo); | 106 | + storager.startPlayback(streamInfo); |
| 96 | }else { | 107 | }else { |
| 97 | - | 108 | + logger.info("流编码信息未获取,2秒后重试..."); |
| 98 | } | 109 | } |
| 99 | }else { | 110 | }else { |
| 100 | Thread.sleep(2000); | 111 | Thread.sleep(2000); |
| 101 | continue; | 112 | continue; |
| 102 | - }; | 113 | + } |
| 103 | } | 114 | } |
| 104 | } catch (InterruptedException e) { | 115 | } catch (InterruptedException e) { |
| 105 | e.printStackTrace(); | 116 | e.printStackTrace(); |