Commit f68cfd8de2cf6e516e220d1a706cecabb2b185aa

Authored by Lawrence
1 parent 366d57b2

优化播放程序,缩短播放加载时间

src/main/java/com/genersoft/iot/vmp/vmanager/play/PlayController.java
... ... @@ -39,7 +39,8 @@ public class PlayController {
39 39 private ZLMRESTfulUtils zlmresTfulUtils;
40 40  
41 41 @GetMapping("/play/{deviceId}/{channelId}")
42   - public ResponseEntity<String> play(@PathVariable String deviceId, @PathVariable String channelId) {
  42 + public ResponseEntity<String> play(@PathVariable String deviceId, @PathVariable String channelId,
  43 + Integer getEncoding) {
43 44  
44 45 Device device = storager.queryVideoDevice(deviceId);
45 46 StreamInfo streamInfo = storager.queryPlayByDevice(deviceId, channelId);
... ... @@ -64,40 +65,49 @@ public class PlayController {
64 65 long startTime = System.currentTimeMillis();
65 66 JSONObject rtpInfo = null;
66 67  
67   - while (lockFlag) {
68   - try {
69   - if (System.currentTimeMillis() - startTime > 60 * 1000) {
70   - storager.stopPlay(streamInfo);
71   - logger.info("播放等待超时");
72   - return new ResponseEntity<String>("timeout", HttpStatus.OK);
73   - } else {
74   - streamInfo = storager.queryPlayByDevice(deviceId, channelId);
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;
82   - Thread.sleep(2000);
83   - JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo("rtp", "rtmp", streamId);
84   - if (mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online")) {
85   - lockFlag = false;
86   - logger.info("流编码信息已获取");
87   - JSONArray tracks = mediaInfo.getJSONArray("tracks");
88   - streamInfo.setTracks(tracks);
89   - storager.startPlay(streamInfo);
  68 + if (getEncoding == 1) {
  69 + while (lockFlag) {
  70 + try {
  71 + if (System.currentTimeMillis() - startTime > 60 * 1000) {
  72 + storager.stopPlay(streamInfo);
  73 + logger.info("播放等待超时");
  74 + return new ResponseEntity<String>("timeout", HttpStatus.OK);
  75 + } else {
  76 + streamInfo = storager.queryPlayByDevice(deviceId, channelId);
  77 + if (!rtpPushed) {
  78 + logger.info("查询RTP推流信息...");
  79 + rtpInfo = zlmresTfulUtils.getRtpInfo(streamId);
  80 + }
  81 + if (rtpInfo != null && rtpInfo.getBoolean("exist") && streamInfo != null
  82 + && streamInfo.getFlv() != null) {
  83 + logger.info("查询流编码信息:" + streamInfo.getFlv());
  84 + rtpPushed = true;
  85 + Thread.sleep(2000);
  86 + JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo("rtp", "rtmp", streamId);
  87 + if (mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online")) {
  88 + lockFlag = false;
  89 + logger.info("流编码信息已获取");
  90 + JSONArray tracks = mediaInfo.getJSONArray("tracks");
  91 + streamInfo.setTracks(tracks);
  92 + storager.startPlay(streamInfo);
  93 + } else {
  94 + logger.info("流编码信息未获取,2秒后重试...");
  95 + }
90 96 } else {
91   - logger.info("流编码信息未获取,2秒后重试...");
  97 + Thread.sleep(2000);
  98 + continue;
92 99 }
93   - } else {
94   - Thread.sleep(2000);
95   - continue;
96 100 }
  101 + } catch (InterruptedException e) {
  102 + e.printStackTrace();
97 103 }
98   - } catch (InterruptedException e) {
99   - e.printStackTrace();
100 104 }
  105 + } else {
  106 + String flv = storager.getMediaInfo().getLocalIP() + ":" + storager.getMediaInfo().getHttpPort() + "/rtp/"
  107 + + streamId + ".flv";
  108 + streamInfo.setFlv("http://" + flv);
  109 + streamInfo.setWs_flv("ws://" + flv);
  110 + storager.startPlay(streamInfo);
101 111 }
102 112  
103 113 if (logger.isDebugEnabled()) {
... ...
web_src/src/components/channelList.vue
... ... @@ -58,8 +58,8 @@
58 58 <el-button size="mini" icon="el-icon-video-play" @click="sendDevicePush(scope.row)">播放</el-button>
59 59 <el-button size="mini" icon="el-icon-switch-button" type="danger" v-if="scope.row.play" @click="stopDevicePush(scope.row)">停止</el-button>
60 60 <el-button size="mini" icon="el-icon-s-open" type="primary" v-if="scope.row.parental == 1" @click="changeSubchannel(scope.row)">查看</el-button>
61   - <el-button size="mini" icon="el-icon-video-camera" type="primary" @click="queryRecords(scope.row)">设备录象</el-button>
62   -<!-- <el-button size="mini" @click="sendDevicePush(scope.row)">录像查询</el-button> -->
  61 + <el-button size="mini" icon="el-icon-video-camera" type="primary" @click="queryRecords(scope.row)">设备录象</el-button>
  62 + <!-- <el-button size="mini" @click="sendDevicePush(scope.row)">录像查询</el-button> -->
63 63 </el-button-group>
64 64 </template>
65 65 </el-table-column>
... ... @@ -197,20 +197,21 @@ export default {
197 197 let deviceId = this.deviceId;
198 198 this.isLoging = true;
199 199 let channelId = itemData.channelId;
200   - console.log("通知设备推流1:" + deviceId + " : " + channelId);
  200 + let getEncoding = itemData.hasAudio ? '1' : '0'
  201 + console.log("通知设备推流1:" + deviceId + " : " + channelId + ":" + getEncoding);
201 202 let that = this;
202 203 this.$axios({
203 204 method: 'get',
204   - url: '/api/play/' + deviceId + '/' + channelId
  205 + url: '/api/play/' + deviceId + '/' + channelId + '?getEncoding=' + getEncoding
205 206 }).then(function (res) {
206 207 console.log(res.data)
207 208 let ssrc = res.data.ssrc;
208 209 that.isLoging = false;
209 210 if (!!ssrc) {
210 211 // that.$refs.devicePlayer.play(res.data, deviceId, channelId, itemData.hasAudio);
211   - that.$refs.devicePlayer.openDialog("media", deviceId, channelId,{
212   - streamInfo: res.data,
213   - hasAudio: itemData.hasAudio
  212 + that.$refs.devicePlayer.openDialog("media", deviceId, channelId, {
  213 + streamInfo: res.data,
  214 + hasAudio: itemData.hasAudio
214 215 });
215 216 that.initData();
216 217 } else {
... ... @@ -219,10 +220,10 @@ export default {
219 220 }).catch(function (e) {});
220 221 },
221 222 queryRecords: function (itemData) {
222   - var format = moment().format("YYYY-M-D");
223   - let deviceId = this.deviceId;
224   - let channelId = itemData.channelId;
225   - this.$refs.devicePlayer.openDialog("record", deviceId, channelId, {date:format})
  223 + var format = moment().format("YYYY-M-D");
  224 + let deviceId = this.deviceId;
  225 + let channelId = itemData.channelId;
  226 + this.$refs.devicePlayer.openDialog("record", deviceId, channelId, {date: format})
226 227 },
227 228 stopDevicePush: function (itemData) {
228 229 console.log(itemData)
... ...
web_src/src/components/gb28181/devicePlayer.vue
... ... @@ -223,7 +223,7 @@ export default {
223 223 play: function (streamInfo, hasAudio) {
224 224 this.hasaudio = hasAudio;
225 225 // 根据媒体流信息二次判断
226   - if (!!streamInfo.tracks && streamInfo.tracks.length > 0) {
  226 + if (!!streamInfo.tracks && streamInfo.tracks.length > 0 && hasAudio) {
227 227 var realHasAudio = false;
228 228 for (let i = 0; i < streamInfo.tracks.length; i++) {
229 229 if (streamInfo.tracks[i].codec_type == 1 && streamInfo.tracks[i].codec_id_name == "CodecAAC") { // 判断为AAC音频
... ...