SipRunner.java 4.7 KB

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