SipRunner.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package com.genersoft.iot.vmp.gb28181.task;
  2. import com.genersoft.iot.vmp.conf.UserSetting;
  3. import com.genersoft.iot.vmp.gb28181.bean.Device;
  4. import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
  5. import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
  6. import com.genersoft.iot.vmp.gb28181.session.SSRCFactory;
  7. import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform;
  8. import com.genersoft.iot.vmp.media.bean.MediaServer;
  9. import com.genersoft.iot.vmp.service.IDeviceService;
  10. import com.genersoft.iot.vmp.media.service.IMediaServerService;
  11. import com.genersoft.iot.vmp.service.IPlatformService;
  12. import com.genersoft.iot.vmp.service.impl.PlatformServiceImpl;
  13. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  14. import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.boot.CommandLineRunner;
  19. import org.springframework.core.annotation.Order;
  20. import org.springframework.stereotype.Component;
  21. import javax.sip.InvalidArgumentException;
  22. import javax.sip.SipException;
  23. import java.text.ParseException;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. /**
  28. * 系统启动时控制设备
  29. * @author lin
  30. */
  31. @Component
  32. @Order(value=14)
  33. public class SipRunner implements CommandLineRunner {
  34. @Autowired
  35. private IVideoManagerStorage storager;
  36. @Autowired
  37. private IRedisCatchStorage redisCatchStorage;
  38. @Autowired
  39. private SSRCFactory ssrcFactory;
  40. @Autowired
  41. private UserSetting userSetting;
  42. @Autowired
  43. private IDeviceService deviceService;
  44. @Autowired
  45. private IMediaServerService mediaServerService;
  46. @Autowired
  47. private IPlatformService platformService;
  48. @Autowired
  49. private ISIPCommanderForPlatform commanderForPlatform;
  50. private final static Logger logger = LoggerFactory.getLogger(PlatformServiceImpl.class);
  51. @Override
  52. public void run(String... args) throws Exception {
  53. List<Device> deviceList = deviceService.getAllOnlineDevice();
  54. for (Device device : deviceList) {
  55. if (deviceService.expire(device)){
  56. deviceService.offline(device.getDeviceId(), "注册已过期");
  57. }else {
  58. deviceService.online(device, null);
  59. }
  60. }
  61. // 重置cseq计数
  62. redisCatchStorage.resetAllCSEQ();
  63. // 清理redis
  64. // 清理数据库不存在但是redis中存在的数据
  65. List<Device> devicesInDb = deviceService.getAll();
  66. if (devicesInDb.size() == 0) {
  67. redisCatchStorage.removeAllDevice();
  68. }else {
  69. List<Device> devicesInRedis = redisCatchStorage.getAllDevices();
  70. if (devicesInRedis.size() > 0) {
  71. Map<String, Device> deviceMapInDb = new HashMap<>();
  72. devicesInDb.parallelStream().forEach(device -> {
  73. deviceMapInDb.put(device.getDeviceId(), device);
  74. });
  75. devicesInRedis.parallelStream().forEach(device -> {
  76. if (deviceMapInDb.get(device.getDeviceId()) == null) {
  77. redisCatchStorage.removeDevice(device.getDeviceId());
  78. }
  79. });
  80. }
  81. }
  82. // 查找国标推流
  83. List<SendRtpItem> sendRtpItems = redisCatchStorage.queryAllSendRTPServer();
  84. if (sendRtpItems.size() > 0) {
  85. for (SendRtpItem sendRtpItem : sendRtpItems) {
  86. MediaServer mediaServerItem = mediaServerService.getOne(sendRtpItem.getMediaServerId());
  87. redisCatchStorage.deleteSendRTPServer(sendRtpItem.getPlatformId(),sendRtpItem.getChannelId(), sendRtpItem.getCallId(),sendRtpItem.getStream());
  88. if (mediaServerItem != null) {
  89. ssrcFactory.releaseSsrc(sendRtpItem.getMediaServerId(), sendRtpItem.getSsrc());
  90. boolean stopResult = mediaServerService.stopSendRtp(mediaServerItem, sendRtpItem.getApp(), sendRtpItem.getStream(), sendRtpItem.getSsrc());
  91. if (stopResult) {
  92. ParentPlatform platform = platformService.queryPlatformByServerGBId(sendRtpItem.getPlatformId());
  93. if (platform != null) {
  94. try {
  95. commanderForPlatform.streamByeCmd(platform, sendRtpItem.getCallId());
  96. } catch (InvalidArgumentException | ParseException | SipException e) {
  97. logger.error("[命令发送失败] 国标级联 发送BYE: {}", e.getMessage());
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }