SipRunner.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.genersoft.iot.vmp.gb28181.task;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.genersoft.iot.vmp.conf.UserSetting;
  4. import com.genersoft.iot.vmp.gb28181.bean.Device;
  5. import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
  6. import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
  7. import com.genersoft.iot.vmp.gb28181.session.SSRCFactory;
  8. import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform;
  9. import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
  10. import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
  11. import com.genersoft.iot.vmp.service.IDeviceService;
  12. import com.genersoft.iot.vmp.service.IMediaServerService;
  13. import com.genersoft.iot.vmp.service.IPlatformService;
  14. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  15. import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
  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 java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. /**
  24. * 系统启动时控制设备
  25. * @author lin
  26. */
  27. @Component
  28. @Order(value=14)
  29. public class SipRunner implements CommandLineRunner {
  30. @Autowired
  31. private IVideoManagerStorage storager;
  32. @Autowired
  33. private IRedisCatchStorage redisCatchStorage;
  34. @Autowired
  35. private SSRCFactory ssrcFactory;
  36. @Autowired
  37. private UserSetting userSetting;
  38. @Autowired
  39. private IDeviceService deviceService;
  40. @Autowired
  41. private ZLMRESTfulUtils zlmresTfulUtils;
  42. @Autowired
  43. private IMediaServerService mediaServerService;
  44. @Autowired
  45. private IPlatformService platformService;
  46. @Autowired
  47. private ISIPCommanderForPlatform commanderForPlatform;
  48. @Override
  49. public void run(String... args) throws Exception {
  50. List<Device> deviceList = deviceService.getAllOnlineDevice();
  51. for (Device device : deviceList) {
  52. if (deviceService.expire(device)){
  53. deviceService.offline(device.getDeviceId(), "注册已过期");
  54. }else {
  55. deviceService.online(device, null);
  56. }
  57. }
  58. // 重置cseq计数
  59. redisCatchStorage.resetAllCSEQ();
  60. // 清理redis
  61. // 清理数据库不存在但是redis中存在的数据
  62. List<Device> devicesInDb = deviceService.getAll();
  63. if (devicesInDb.size() == 0) {
  64. redisCatchStorage.removeAllDevice();
  65. }else {
  66. List<Device> devicesInRedis = redisCatchStorage.getAllDevices();
  67. if (devicesInRedis.size() > 0) {
  68. Map<String, Device> deviceMapInDb = new HashMap<>();
  69. devicesInDb.parallelStream().forEach(device -> {
  70. deviceMapInDb.put(device.getDeviceId(), device);
  71. });
  72. devicesInRedis.parallelStream().forEach(device -> {
  73. if (deviceMapInDb.get(device.getDeviceId()) == null) {
  74. redisCatchStorage.removeDevice(device.getDeviceId());
  75. }
  76. });
  77. }
  78. }
  79. // 查找国标推流
  80. List<SendRtpItem> sendRtpItems = redisCatchStorage.queryAllSendRTPServer();
  81. if (sendRtpItems.size() > 0) {
  82. for (SendRtpItem sendRtpItem : sendRtpItems) {
  83. MediaServerItem mediaServerItem = mediaServerService.getOne(sendRtpItem.getMediaServerId());
  84. redisCatchStorage.deleteSendRTPServer(sendRtpItem.getPlatformId(),sendRtpItem.getChannelId(), sendRtpItem.getCallId(),sendRtpItem.getStream());
  85. if (mediaServerItem != null) {
  86. ssrcFactory.releaseSsrc(sendRtpItem.getMediaServerId(), sendRtpItem.getSsrc());
  87. Map<String, Object> param = new HashMap<>();
  88. param.put("vhost","__defaultVhost__");
  89. param.put("app",sendRtpItem.getApp());
  90. param.put("stream",sendRtpItem.getStream());
  91. param.put("ssrc",sendRtpItem.getSsrc());
  92. JSONObject jsonObject = zlmresTfulUtils.stopSendRtp(mediaServerItem, param);
  93. if (jsonObject != null && jsonObject.getInteger("code") == 0) {
  94. ParentPlatform platform = platformService.queryPlatformByServerGBId(sendRtpItem.getPlatformId());
  95. if (platform != null) {
  96. commanderForPlatform.streamByeCmd(platform, sendRtpItem.getCallId());
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }