Commit 37e97a782c43f658a5bfee7ac1df31bb86d9b955

Authored by Lawrence
1 parent eecffb51

改正并优化查询RTP流信息的流程

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