AudioBroadcastManager.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.genersoft.iot.vmp.gb28181.session;
  2. import com.genersoft.iot.vmp.conf.SipConfig;
  3. import com.genersoft.iot.vmp.conf.exception.SsrcTransactionNotFoundException;
  4. import com.genersoft.iot.vmp.gb28181.bean.AudioBroadcastCatch;
  5. import com.genersoft.iot.vmp.gb28181.bean.Device;
  6. import com.genersoft.iot.vmp.gb28181.bean.InviteStreamType;
  7. import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
  8. import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
  9. import com.genersoft.iot.vmp.gb28181.utils.SipUtils;
  10. import com.genersoft.iot.vmp.media.event.MediaDepartureEvent;
  11. import com.genersoft.iot.vmp.service.IDeviceService;
  12. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.context.event.EventListener;
  17. import org.springframework.scheduling.annotation.Async;
  18. import org.springframework.stereotype.Component;
  19. import javax.sip.InvalidArgumentException;
  20. import javax.sip.SipException;
  21. import java.text.ParseException;
  22. import java.util.*;
  23. import java.util.concurrent.ConcurrentHashMap;
  24. import java.util.stream.Collectors;
  25. import java.util.stream.Stream;
  26. /**
  27. * 语音广播消息管理类
  28. * @author lin
  29. */
  30. @Component
  31. public class AudioBroadcastManager {
  32. private final static Logger logger = LoggerFactory.getLogger(AudioBroadcastManager.class);
  33. @Autowired
  34. private SipConfig config;
  35. @Autowired
  36. private SIPCommander cmder;
  37. @Autowired
  38. private IRedisCatchStorage redisCatchStorage;
  39. @Autowired
  40. private IDeviceService deviceService;
  41. public static Map<String, AudioBroadcastCatch> data = new ConcurrentHashMap<>();
  42. /**
  43. * 流离开的处理
  44. */
  45. @Async("taskExecutor")
  46. @EventListener
  47. public void onApplicationEvent(MediaDepartureEvent event) {
  48. List<SendRtpItem> sendRtpItems = redisCatchStorage.querySendRTPServerByStream(event.getStream());
  49. if (!sendRtpItems.isEmpty()) {
  50. for (SendRtpItem sendRtpItem : sendRtpItems) {
  51. if (sendRtpItem != null && sendRtpItem.getApp().equals(event.getApp())) {
  52. String platformId = sendRtpItem.getPlatformId();
  53. Device device = deviceService.getDevice(platformId);
  54. try {
  55. if (device != null) {
  56. cmder.streamByeCmd(device, sendRtpItem.getChannelId(), event.getStream(), sendRtpItem.getCallId());
  57. if (sendRtpItem.getPlayType().equals(InviteStreamType.BROADCAST)
  58. || sendRtpItem.getPlayType().equals(InviteStreamType.TALK)) {
  59. AudioBroadcastCatch audioBroadcastCatch = get(sendRtpItem.getDeviceId(), sendRtpItem.getChannelId());
  60. if (audioBroadcastCatch != null) {
  61. // 来自上级平台的停止对讲
  62. logger.info("[停止对讲] 来自上级,平台:{}, 通道:{}", sendRtpItem.getDeviceId(), sendRtpItem.getChannelId());
  63. del(sendRtpItem.getDeviceId(), sendRtpItem.getChannelId());
  64. }
  65. }
  66. }
  67. } catch (SipException | InvalidArgumentException | ParseException |
  68. SsrcTransactionNotFoundException e) {
  69. logger.error("[命令发送失败] 发送BYE: {}", e.getMessage());
  70. }
  71. }
  72. }
  73. }
  74. }
  75. public void update(AudioBroadcastCatch audioBroadcastCatch) {
  76. if (SipUtils.isFrontEnd(audioBroadcastCatch.getDeviceId())) {
  77. audioBroadcastCatch.setChannelId(audioBroadcastCatch.getDeviceId());
  78. data.put(audioBroadcastCatch.getDeviceId(), audioBroadcastCatch);
  79. }else {
  80. data.put(audioBroadcastCatch.getDeviceId() + audioBroadcastCatch.getChannelId(), audioBroadcastCatch);
  81. }
  82. }
  83. public void del(String deviceId, String channelId) {
  84. if (SipUtils.isFrontEnd(deviceId)) {
  85. data.remove(deviceId);
  86. }else {
  87. data.remove(deviceId + channelId);
  88. }
  89. }
  90. public void delByDeviceId(String deviceId) {
  91. for (String key : data.keySet()) {
  92. if (key.startsWith(deviceId)) {
  93. data.remove(key);
  94. }
  95. }
  96. }
  97. public List<AudioBroadcastCatch> getAll(){
  98. Collection<AudioBroadcastCatch> values = data.values();
  99. return new ArrayList<>(values);
  100. }
  101. public boolean exit(String deviceId, String channelId) {
  102. for (String key : data.keySet()) {
  103. if (SipUtils.isFrontEnd(deviceId)) {
  104. return key.equals(deviceId);
  105. }else {
  106. return key.equals(deviceId + channelId);
  107. }
  108. }
  109. return false;
  110. }
  111. public AudioBroadcastCatch get(String deviceId, String channelId) {
  112. AudioBroadcastCatch audioBroadcastCatch;
  113. if (SipUtils.isFrontEnd(deviceId)) {
  114. audioBroadcastCatch = data.get(deviceId);
  115. }else {
  116. audioBroadcastCatch = data.get(deviceId + channelId);
  117. }
  118. if (audioBroadcastCatch == null) {
  119. Stream<AudioBroadcastCatch> allAudioBroadcastCatchStreamForDevice = data.values().stream().filter(
  120. audioBroadcastCatchItem -> Objects.equals(audioBroadcastCatchItem.getDeviceId(), deviceId));
  121. List<AudioBroadcastCatch> audioBroadcastCatchList = allAudioBroadcastCatchStreamForDevice.collect(Collectors.toList());
  122. if (audioBroadcastCatchList.size() == 1 && Objects.equals(config.getId(), channelId)) {
  123. audioBroadcastCatch = audioBroadcastCatchList.get(0);
  124. }
  125. }
  126. return audioBroadcastCatch;
  127. }
  128. public List<AudioBroadcastCatch> get(String deviceId) {
  129. List<AudioBroadcastCatch> audioBroadcastCatchList= new ArrayList<>();
  130. if (SipUtils.isFrontEnd(deviceId)) {
  131. if (data.get(deviceId) != null) {
  132. audioBroadcastCatchList.add(data.get(deviceId));
  133. }
  134. }else {
  135. for (String key : data.keySet()) {
  136. if (key.startsWith(deviceId)) {
  137. audioBroadcastCatchList.add(data.get(key));
  138. }
  139. }
  140. }
  141. return audioBroadcastCatchList;
  142. }
  143. }