SipRunner.java 4.3 KB

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