AudioBroadcastManager.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.genersoft.iot.vmp.gb28181.session;
  2. import com.genersoft.iot.vmp.conf.SipConfig;
  3. import com.genersoft.iot.vmp.gb28181.bean.AudioBroadcastCatch;
  4. import com.genersoft.iot.vmp.gb28181.utils.SipUtils;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Component;
  7. import java.util.*;
  8. import java.util.concurrent.ConcurrentHashMap;
  9. import java.util.stream.Collectors;
  10. import java.util.stream.Stream;
  11. /**
  12. * 语音广播消息管理类
  13. * @author lin
  14. */
  15. @Component
  16. public class AudioBroadcastManager {
  17. @Autowired
  18. private SipConfig config;
  19. public static Map<String, AudioBroadcastCatch> data = new ConcurrentHashMap<>();
  20. public void add(AudioBroadcastCatch audioBroadcastCatch) {
  21. this.update(audioBroadcastCatch);
  22. }
  23. public void update(AudioBroadcastCatch audioBroadcastCatch) {
  24. if (SipUtils.isFrontEnd(audioBroadcastCatch.getDeviceId())) {
  25. data.put(audioBroadcastCatch.getDeviceId(), audioBroadcastCatch);
  26. }else {
  27. data.put(audioBroadcastCatch.getDeviceId() + audioBroadcastCatch.getChannelId(), audioBroadcastCatch);
  28. }
  29. }
  30. public void del(String deviceId, String channelId) {
  31. if (SipUtils.isFrontEnd(deviceId)) {
  32. data.remove(deviceId);
  33. }else {
  34. data.remove(deviceId + channelId);
  35. }
  36. }
  37. public void delByDeviceId(String deviceId) {
  38. for (String key : data.keySet()) {
  39. if (key.startsWith(deviceId)) {
  40. data.remove(key);
  41. }
  42. }
  43. }
  44. public List<AudioBroadcastCatch> getAll(){
  45. Collection<AudioBroadcastCatch> values = data.values();
  46. return new ArrayList<>(values);
  47. }
  48. public boolean exit(String deviceId, String channelId) {
  49. for (String key : data.keySet()) {
  50. if (SipUtils.isFrontEnd(deviceId)) {
  51. return key.equals(deviceId);
  52. }else {
  53. return key.equals(deviceId + channelId);
  54. }
  55. }
  56. return false;
  57. }
  58. public AudioBroadcastCatch get(String deviceId, String channelId) {
  59. AudioBroadcastCatch audioBroadcastCatch;
  60. if (SipUtils.isFrontEnd(deviceId)) {
  61. audioBroadcastCatch = data.get(deviceId);
  62. }else {
  63. audioBroadcastCatch = data.get(deviceId + channelId);
  64. }
  65. if (audioBroadcastCatch == null) {
  66. Stream<AudioBroadcastCatch> allAudioBroadcastCatchStreamForDevice = data.values().stream().filter(
  67. audioBroadcastCatchItem -> Objects.equals(audioBroadcastCatchItem.getDeviceId(), deviceId));
  68. List<AudioBroadcastCatch> audioBroadcastCatchList = allAudioBroadcastCatchStreamForDevice.collect(Collectors.toList());
  69. if (audioBroadcastCatchList.size() == 1 && Objects.equals(config.getId(), channelId)) {
  70. audioBroadcastCatch = audioBroadcastCatchList.get(0);
  71. }
  72. }
  73. return audioBroadcastCatch;
  74. }
  75. }