Commit 72c1b36d6d2ece497e032c8434641d6576590f9d

Authored by 648540858
1 parent b6c97528

优化对讲逻辑

src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/ByeRequestProcessor.java
... ... @@ -93,7 +93,7 @@ public class ByeRequestProcessor extends SIPRequestProcessorParent implements In
93 93 String channelId = ((SipURI) ((HeaderAddress) evt.getRequest().getHeader(ToHeader.NAME)).getAddress().getURI()).getUser();
94 94 SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(platformGbId, null, null, callIdHeader.getCallId());
95 95 logger.info("收到bye, [{}/{}]", platformGbId, channelId);
96   - if (sendRtpItem != null){
  96 + if (sendRtpItem != null ){
97 97 String streamId = sendRtpItem.getStreamId();
98 98 Map<String, Object> param = new HashMap<>();
99 99 param.put("vhost","__defaultVhost__");
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/InviteRequestProcessor.java
... ... @@ -834,83 +834,87 @@ public class InviteRequestProcessor extends SIPRequestProcessorParent implements
834 834 subscribeKey.put("mediaServerId", mediaServerItem.getId());
835 835 String finalSsrc = ssrc;
836 836 // 流已经存在时直接推流
837   - JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo(mediaServerItem, app, "rtsp", stream);
838   - JSONArray tracks = mediaInfo.getJSONArray("tracks");
839   - Integer codecId = null;
840   - if (tracks != null && tracks.size() > 0) {
841   - for (int i = 0; i < tracks.size(); i++) {
842   - MediaItem.MediaTrack track = JSON.toJavaObject((JSON)tracks.get(i),MediaItem.MediaTrack.class);
843   - if (track.getCodecType() == 1) {
844   - codecId = track.getCodecId();
845   - break;
846   - }
847   - }
848   - }
849   - if ((mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online"))) {
850   - logger.info("发现已经在推流");
851   - sendRtpItem.setStatus(2);
852   - redisCatchStorage.updateSendRTPSever(sendRtpItem);
853   - StringBuffer content = new StringBuffer(200);
854   - content.append("v=0\r\n");
855   - content.append("o="+ config.getId() +" "+ sdp.getOrigin().getSessionId() +" " + sdp.getOrigin().getSessionVersion() + " IN IP4 "+mediaServerItem.getSdpIp()+"\r\n");
856   - content.append("s=Play\r\n");
857   - content.append("c=IN IP4 "+mediaServerItem.getSdpIp()+"\r\n");
858   - content.append("t=0 0\r\n");
859   - if (codecId == null) {
860   - if (mediaTransmissionTCP) {
861   - content.append("m=audio "+ sendRtpItem.getLocalPort()+" TCP/RTP/AVP 8\r\n");
862   - }else {
863   - content.append("m=audio "+ sendRtpItem.getLocalPort()+" RTP/AVP 8\r\n");
864   - }
865   -
866   - content.append("a=rtpmap:8 PCMA/8000\r\n");
867   - }else {
868   - if (codecId == 4) {
869   - if (mediaTransmissionTCP) {
870   - content.append("m=audio "+ sendRtpItem.getLocalPort()+" TCP/RTP/AVP 0\r\n");
871   - }else {
872   - content.append("m=audio "+ sendRtpItem.getLocalPort()+" RTP/AVP 0\r\n");
873   - }
874   - content.append("a=rtpmap:0 PCMU/8000\r\n");
875   - }else {
876   - if (mediaTransmissionTCP) {
877   - content.append("m=audio "+ sendRtpItem.getLocalPort()+" TCP/RTP/AVP 8\r\n");
878   - }else {
879   - content.append("m=audio "+ sendRtpItem.getLocalPort()+" RTP/AVP 8\r\n");
880   - }
881   - content.append("a=rtpmap:8 PCMA/8000\r\n");
882   - }
883   - }
884   - if (sendRtpItem.isTcp()) {
885   - content.append("a=connection:new\r\n");
886   - if (!sendRtpItem.isTcpActive()) {
887   - content.append("a=setup:active\r\n");
888   - }else {
889   - content.append("a=setup:passive\r\n");
890   - }
891   - }
892   - content.append("a=sendonly\r\n");
893   - content.append("y="+ finalSsrc + "\r\n");
894   - content.append("f=v/////a/1/8/1\r\n");
895   -
896   - ParentPlatform parentPlatform = new ParentPlatform();
897   - parentPlatform.setServerIP(device.getIp());
898   - parentPlatform.setServerPort(device.getPort());
899   - parentPlatform.setServerGBId(device.getDeviceId());
900   - try {
901   - responseSdpAck(evt, content.toString(), parentPlatform);
902   - Dialog dialog = evt.getDialog();
903   - audioBroadcastCatch.setDialog((SIPDialog) dialog);
904   - audioBroadcastCatch.setRequest((SIPRequest) request);
905   - audioBroadcastManager.update(audioBroadcastCatch);
906   - } catch (SipException e) {
907   - throw new RuntimeException(e);
908   - } catch (InvalidArgumentException e) {
909   - throw new RuntimeException(e);
910   - } catch (ParseException e) {
911   - throw new RuntimeException(e);
912   - }
913   - }else {
  837 +// JSONObject mediaInfo = zlmresTfulUtils.getMediaList(mediaServerItem, app, stream);
  838 +// System.out.println(mediaInfo != null);
  839 +// System.out.println(mediaInfo);
  840 +// if (mediaInfo != null &&
  841 +// (mediaInfo.getInteger("code") != null && mediaInfo.getInteger("code") == 0
  842 +// && mediaInfo.getJSONArray("data") != null && mediaInfo.getJSONArray("data").size() > 0)) {
  843 +// logger.info("发现已经在推流");
  844 +// JSONArray tracks = mediaInfo.getJSONArray("data").getJSONObject(0).getJSONArray("tracks");
  845 +// Integer codecId = null;
  846 +// if (tracks != null && tracks.size() > 0) {
  847 +// for (int i = 0; i < tracks.size(); i++) {
  848 +// MediaItem.MediaTrack track = JSON.toJavaObject((JSON)tracks.get(i),MediaItem.MediaTrack.class);
  849 +// if (track.getCodecType() == 1) {
  850 +// codecId = track.getCodecId();
  851 +// break;
  852 +// }
  853 +// }
  854 +// }
  855 +// sendRtpItem.setStatus(2);
  856 +// redisCatchStorage.updateSendRTPSever(sendRtpItem);
  857 +// StringBuffer content = new StringBuffer(200);
  858 +// content.append("v=0\r\n");
  859 +// content.append("o="+ config.getId() +" "+ sdp.getOrigin().getSessionId() +" " + sdp.getOrigin().getSessionVersion() + " IN IP4 "+mediaServerItem.getSdpIp()+"\r\n");
  860 +// content.append("s=Play\r\n");
  861 +// content.append("c=IN IP4 "+mediaServerItem.getSdpIp()+"\r\n");
  862 +// content.append("t=0 0\r\n");
  863 +// if (codecId == null) {
  864 +// if (mediaTransmissionTCP) {
  865 +// content.append("m=audio "+ sendRtpItem.getLocalPort()+" TCP/RTP/AVP 8\r\n");
  866 +// }else {
  867 +// content.append("m=audio "+ sendRtpItem.getLocalPort()+" RTP/AVP 8\r\n");
  868 +// }
  869 +//
  870 +// content.append("a=rtpmap:8 PCMA/8000\r\n");
  871 +// }else {
  872 +// if (codecId == 4) {
  873 +// if (mediaTransmissionTCP) {
  874 +// content.append("m=audio "+ sendRtpItem.getLocalPort()+" TCP/RTP/AVP 0\r\n");
  875 +// }else {
  876 +// content.append("m=audio "+ sendRtpItem.getLocalPort()+" RTP/AVP 0\r\n");
  877 +// }
  878 +// content.append("a=rtpmap:0 PCMU/8000\r\n");
  879 +// }else {
  880 +// if (mediaTransmissionTCP) {
  881 +// content.append("m=audio "+ sendRtpItem.getLocalPort()+" TCP/RTP/AVP 8\r\n");
  882 +// }else {
  883 +// content.append("m=audio "+ sendRtpItem.getLocalPort()+" RTP/AVP 8\r\n");
  884 +// }
  885 +// content.append("a=rtpmap:8 PCMA/8000\r\n");
  886 +// }
  887 +// }
  888 +// if (sendRtpItem.isTcp()) {
  889 +// content.append("a=connection:new\r\n");
  890 +// if (!sendRtpItem.isTcpActive()) {
  891 +// content.append("a=setup:active\r\n");
  892 +// }else {
  893 +// content.append("a=setup:passive\r\n");
  894 +// }
  895 +// }
  896 +// content.append("a=sendonly\r\n");
  897 +// content.append("y="+ finalSsrc + "\r\n");
  898 +// content.append("f=v/////a/1/8/1\r\n");
  899 +//
  900 +// ParentPlatform parentPlatform = new ParentPlatform();
  901 +// parentPlatform.setServerIP(device.getIp());
  902 +// parentPlatform.setServerPort(device.getPort());
  903 +// parentPlatform.setServerGBId(device.getDeviceId());
  904 +// try {
  905 +// responseSdpAck(evt, content.toString(), parentPlatform);
  906 +// Dialog dialog = evt.getDialog();
  907 +// audioBroadcastCatch.setDialog((SIPDialog) dialog);
  908 +// audioBroadcastCatch.setRequest((SIPRequest) request);
  909 +// audioBroadcastManager.update(audioBroadcastCatch);
  910 +// } catch (SipException e) {
  911 +// throw new RuntimeException(e);
  912 +// } catch (InvalidArgumentException e) {
  913 +// throw new RuntimeException(e);
  914 +// } catch (ParseException e) {
  915 +// throw new RuntimeException(e);
  916 +// }
  917 +// }else {
914 918 // 流不存在时监听流上线
915 919 // 设置等待推流的超时; 默认20s
916 920 String waiteStreamTimeoutTaskKey = "waite-stream-" + device.getDeviceId() + audioBroadcastCatch.getChannelId();
... ... @@ -1012,7 +1016,7 @@ public class InviteRequestProcessor extends SIPRequestProcessorParent implements
1012 1016 throw new RuntimeException(e);
1013 1017 }
1014 1018 });
1015   - }
  1019 +// }
1016 1020 String key = DeferredResultHolder.CALLBACK_CMD_BROADCAST + device.getDeviceId();
1017 1021 WVPResult<AudioBroadcastResult> wvpResult = new WVPResult<>();
1018 1022 wvpResult.setCode(0);
... ...
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java
... ... @@ -294,11 +294,11 @@ public class ZLMRTPServerFactory {
294 294 return 0;
295 295 }
296 296 Integer code = mediaInfo.getInteger("code");
297   - if ( code < 0) {
  297 + if (code < 0) {
298 298 logger.warn("查询流({}/{})是否有其它观看者时得到: {}", app, streamId, mediaInfo.getString("msg"));
299 299 return -1;
300 300 }
301   - if ( code == 0 && ! mediaInfo.getBoolean("online")) {
  301 + if ( code == 0 && mediaInfo.getBoolean("online") != null && ! mediaInfo.getBoolean("online")) {
302 302 logger.warn("查询流({}/{})是否有其它观看者时得到: {}", app, streamId, mediaInfo.getString("msg"));
303 303 return -1;
304 304 }
... ...
src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
... ... @@ -15,6 +15,7 @@ import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
15 15 import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage;
16 16 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
17 17 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommanderFroPlatform;
  18 +import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
18 19 import com.genersoft.iot.vmp.utils.DateUtil;
19 20 import com.genersoft.iot.vmp.media.zlm.AssistRESTfulUtils;
20 21 import com.genersoft.iot.vmp.media.zlm.ZLMHttpHookSubscribe;
... ... @@ -76,6 +77,9 @@ public class PlayServiceImpl implements IPlayService {
76 77 private IRedisCatchStorage redisCatchStorage;
77 78  
78 79 @Autowired
  80 + private ZLMRTPServerFactory zlmrtpServerFactory;
  81 +
  82 + @Autowired
79 83 private DeferredResultHolder resultHolder;
80 84  
81 85 @Autowired
... ... @@ -678,9 +682,14 @@ public class PlayServiceImpl implements IPlayService {
678 682 if (audioBroadcastManager.exit(device.getDeviceId(), channelId)) {
679 683 SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(device.getDeviceId(), channelId, null, null);
680 684 if (sendRtpItem != null && sendRtpItem.isOnlyAudio()) {
681   - logger.warn("语音广播已经开启: {}", channelId);
682   - event.call("语音广播已经开启");
683   - return;
  685 + // 查询流是否存在,不存在则认为是异常状态
  686 + MediaServerItem mediaServerItem = mediaServerService.getOne(sendRtpItem.getMediaServerId());
  687 + Boolean streamReady = zlmrtpServerFactory.isStreamReady(mediaServerItem, sendRtpItem.getApp(), sendRtpItem.getStreamId());
  688 + if (streamReady) {
  689 + logger.warn("语音广播已经开启: {}", channelId);
  690 + event.call("语音广播已经开启");
  691 + return;
  692 + }
684 693 }
685 694 }
686 695  
... ... @@ -712,10 +721,13 @@ public class PlayServiceImpl implements IPlayService {
712 721 param.put("app", sendRtpItem.getApp());
713 722 param.put("stream", sendRtpItem.getStreamId());
714 723 zlmresTfulUtils.stopSendRtp(mediaInfo, param);
  724 + // 立刻结束设备的推流,等待自行结束太慢
  725 +// zlmresTfulUtils.closeStreams(mediaInfo, sendRtpItem.getApp(), sendRtpItem.getStreamId());
715 726 }
716 727 if (audioBroadcastCatch.getStatus() == AudioBroadcastCatchStatus.Ok) {
717 728 cmder.streamByeCmd(audioBroadcastCatch.getDialog(), audioBroadcastCatch.getRequest(), null);
718 729 }
  730 +
719 731 } catch (SipException e) {
720 732 throw new RuntimeException(e);
721 733 } catch (ParseException e) {
... ...
web_src/src/components/dialog/devicePlayer.vue
... ... @@ -537,6 +537,9 @@ export default {
537 537 this.stopPlayRecord();
538 538 }
539 539 this.recordPlay = ''
  540 + if (this.broadcastStatus === 1) {
  541 + this.stopBroadcast()
  542 + }
540 543 },
541 544  
542 545 copySharedInfo: function (data) {
... ... @@ -849,24 +852,7 @@ export default {
849 852 }
850 853 });
851 854 }else if (this.broadcastStatus === 1) {
852   - this.broadcastRtc.close()
853   - this.broadcastRtc = null;
854   - this.$axios({
855   - method: 'get',
856   - url: '/api/play/broadcast/stop/' + this.deviceId + '/' + this.channelId
857   - }).then( (res)=> {
858   - if (res.data.code == 0) {
859   - let streamInfo = res.data.data.streamInfo;
860   - this.startBroadcast(streamInfo.rtc)
861   - }else {
862   - this.$message({
863   - showClose: true,
864   - message: res.data.msg,
865   - type: "error",
866   - });
867   - }
868   - });
869   - this.broadcastStatus = -1;
  855 + this.stopBroadcast()
870 856 }
871 857 },
872 858 startBroadcast(url){
... ... @@ -920,12 +906,12 @@ export default {
920 906 });
921 907 this.broadcastRtc.on(ZLMRTCClient.Events.WEBRTC_ON_CONNECTION_STATE_CHANGE,(e)=>{// offer anwser 交换失败
922 908 console.log('状态改变',e)
923   - if (e === "failed") {
924   - this.broadcastStatus = -1;
925   - }else if (e === "connecting") {
  909 + if (e === "connecting") {
926 910 this.broadcastStatus = 0;
927   - }else{
  911 + }else if (e === "connected") {
928 912 this.broadcastStatus = 1;
  913 + }else {
  914 + this.broadcastStatus = -1;
929 915 }
930 916 });
931 917 this.broadcastRtc.on(ZLMRTCClient.Events.CAPTURE_STREAM_FAILED,(e)=>{// offer anwser 交换失败
... ... @@ -936,6 +922,26 @@ export default {
936 922 type: 'error'
937 923 });
938 924 });
  925 + },
  926 + stopBroadcast(){
  927 + this.broadcastRtc.close()
  928 + this.broadcastRtc = null;
  929 + this.$axios({
  930 + method: 'get',
  931 + url: '/api/play/broadcast/stop/' + this.deviceId + '/' + this.channelId
  932 + }).then( (res)=> {
  933 + if (res.data.code == 0) {
  934 + // this.broadcastStatus = -1;
  935 +
  936 + }else {
  937 + this.$message({
  938 + showClose: true,
  939 + message: res.data.msg,
  940 + type: "error",
  941 + });
  942 + }
  943 + });
  944 +
939 945 }
940 946 }
941 947 };
... ...