From 46dd3b1b2adfcf27d4930beb2ac52d7f29e5533b Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: Tue, 7 Dec 2021 17:43:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=80=9A=E9=81=93=E5=BD=95?= =?UTF-8?q?=E5=85=A5=E6=80=A7=E8=83=BD=EF=BC=8C=E4=BC=98=E5=8C=96=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E5=90=8E=E8=AF=BB=E5=8F=96=E9=85=8D=E7=BD=AE=E5=86=99?= =?UTF-8?q?=E5=85=A5=E6=95=B0=E6=8D=AE=E5=BA=93=E7=9A=84=E8=BF=87=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../genersoft/iot/vmp/conf/MediaConfig.java | 4 +- .../transmit/cmd/impl/SIPCommander.java | 2 +- .../cmd/CatalogResponseMessageHandler.java | 9 +- .../iot/vmp/media/zlm/ZLMRunner.java | 11 +- .../iot/vmp/media/zlm/ZLMServerConfig.java | 6 +- .../vmp/media/zlm/dto/MediaServerItem.java | 6 +- .../iot/vmp/service/IMediaServerService.java | 4 +- .../service/impl/MediaServerServiceImpl.java | 11 +- .../vmp/storager/IVideoManagerStorager.java | 8 ++ .../vmp/storager/dao/DeviceChannelMapper.java | 130 +++++++++++++----- .../impl/VideoManagerStoragerImpl.java | 49 ++++++- src/main/resources/all-application.yml | 3 + src/main/resources/application-dev.yml | 3 + src/main/resources/application-docker.yml | 3 + 14 files changed, 190 insertions(+), 59 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/conf/MediaConfig.java b/src/main/java/com/genersoft/iot/vmp/conf/MediaConfig.java index 20339a92..0c7ef887 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/MediaConfig.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/MediaConfig.java @@ -60,7 +60,7 @@ public class MediaConfig{ private String secret; @Value("${media.stream-none-reader-delay-ms:18000}") - private String streamNoneReaderDelayMS = "18000"; + private int streamNoneReaderDelayMS = 18000; @Value("${media.rtp.enable}") private boolean rtpEnable; @@ -141,7 +141,7 @@ public class MediaConfig{ return secret; } - public String getStreamNoneReaderDelayMS() { + public int getStreamNoneReaderDelayMS() { return streamNoneReaderDelayMS; } diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java index 2f90deed..138ccaf1 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java @@ -1163,7 +1163,7 @@ public class SIPCommander implements ISIPCommander { @Override public boolean catalogQuery(Device device, SipSubscribe.Event errorEvent) { // 清空通道 - storager.cleanChannelsForDevice(device.getDeviceId()); +// storager.cleanChannelsForDevice(device.getDeviceId()); try { StringBuffer catalogXml = new StringBuffer(200); catalogXml.append("\r\n"); diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java index 7349a7ac..5fcc3ada 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java @@ -27,7 +27,9 @@ import javax.sip.RequestEvent; import javax.sip.SipException; import javax.sip.message.Response; import java.text.ParseException; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import static com.genersoft.iot.vmp.gb28181.utils.XmlUtil.getText; @@ -69,7 +71,7 @@ public class CatalogResponseMessageHandler extends SIPRequestProcessorParent imp Element deviceListElement = rootElement.element("DeviceList"); Iterator deviceListIterator = deviceListElement.elementIterator(); if (deviceListIterator != null) { - + List channelList = new ArrayList<>(); // 遍历DeviceList while (deviceListIterator.hasNext()) { Element itemDevice = deviceListIterator.next(); @@ -151,9 +153,10 @@ public class CatalogResponseMessageHandler extends SIPRequestProcessorParent imp deviceChannel.setPTZType(Integer.parseInt(getText(itemDevice, "PTZType"))); } deviceChannel.setHasAudio(true); // 默认含有音频,播放时再检查是否有音频及是否AAC - storager.updateChannel(device.getDeviceId(), deviceChannel); + // TODO 修改为批量插入 + channelList.add(deviceChannel); } - + storager.updateChannels(device.getDeviceId(), channelList); RequestMessage msg = new RequestMessage(); msg.setKey(key); msg.setData(device); diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRunner.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRunner.java index f53c1677..71df2957 100644 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRunner.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRunner.java @@ -49,8 +49,13 @@ public class ZLMRunner implements CommandLineRunner { @Override public void run(String... strings) throws Exception { mediaServerService.clearMediaServerForOnline(); - if (mediaServerService.getDefaultMediaServer() == null) { + MediaServerItem defaultMediaServer = mediaServerService.getDefaultMediaServer(); + if (defaultMediaServer == null) { mediaServerService.addToDatabase(mediaConfig.getMediaSerItem()); + }else { + MediaServerItem mediaSerItem = mediaConfig.getMediaSerItem(); + mediaSerItem.setId(defaultMediaServer.getId()); + mediaServerService.updateToDatabase(mediaSerItem); } // 订阅 zlm启动事件, 新的zlm也会从这里进入系统 @@ -61,7 +66,7 @@ public class ZLMRunner implements CommandLineRunner { if (startGetMedia != null) { startGetMedia.remove(zlmServerConfig.getGeneralMediaServerId()); } - mediaServerService.handLeZLMServerConfig(zlmServerConfig); + mediaServerService.zlmServerOnline(zlmServerConfig); } }); @@ -107,7 +112,7 @@ public class ZLMRunner implements CommandLineRunner { zlmServerConfig.setIp(mediaServerItem.getIp()); zlmServerConfig.setHttpPort(mediaServerItem.getHttpPort()); startGetMedia.remove(mediaServerItem.getId()); - mediaServerService.handLeZLMServerConfig(zlmServerConfig); + mediaServerService.zlmServerOnline(zlmServerConfig); } } diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMServerConfig.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMServerConfig.java index c1a9bd20..841c14cf 100644 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMServerConfig.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMServerConfig.java @@ -32,7 +32,7 @@ public class ZLMServerConfig { private String generalMaxStreamWaitMS; @JSONField(name = "general.streamNoneReaderDelayMS") - private String generalStreamNoneReaderDelayMS; + private int generalStreamNoneReaderDelayMS; @JSONField(name = "ip") private String ip; @@ -296,11 +296,11 @@ public class ZLMServerConfig { this.generalMaxStreamWaitMS = generalMaxStreamWaitMS; } - public String getGeneralStreamNoneReaderDelayMS() { + public int getGeneralStreamNoneReaderDelayMS() { return generalStreamNoneReaderDelayMS; } - public void setGeneralStreamNoneReaderDelayMS(String generalStreamNoneReaderDelayMS) { + public void setGeneralStreamNoneReaderDelayMS(int generalStreamNoneReaderDelayMS) { this.generalStreamNoneReaderDelayMS = generalStreamNoneReaderDelayMS; } diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/dto/MediaServerItem.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/dto/MediaServerItem.java index 1d4bf318..b7b4e287 100644 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/dto/MediaServerItem.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/dto/MediaServerItem.java @@ -37,7 +37,7 @@ public class MediaServerItem{ private String secret; - private String streamNoneReaderDelayMS; + private int streamNoneReaderDelayMS; private boolean rtpEnable; @@ -206,11 +206,11 @@ public class MediaServerItem{ this.secret = secret; } - public String getStreamNoneReaderDelayMS() { + public int getStreamNoneReaderDelayMS() { return streamNoneReaderDelayMS; } - public void setStreamNoneReaderDelayMS(String streamNoneReaderDelayMS) { + public void setStreamNoneReaderDelayMS(int streamNoneReaderDelayMS) { this.streamNoneReaderDelayMS = streamNoneReaderDelayMS; } diff --git a/src/main/java/com/genersoft/iot/vmp/service/IMediaServerService.java b/src/main/java/com/genersoft/iot/vmp/service/IMediaServerService.java index 43d36d95..e539f233 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/IMediaServerService.java +++ b/src/main/java/com/genersoft/iot/vmp/service/IMediaServerService.java @@ -28,7 +28,7 @@ public interface IMediaServerService { * @param zlmServerConfig * @return */ - void handLeZLMServerConfig(ZLMServerConfig zlmServerConfig); + void zlmServerOnline(ZLMServerConfig zlmServerConfig); MediaServerItem getMediaServerForMinimumLoad(); @@ -56,6 +56,8 @@ public interface IMediaServerService { int addToDatabase(MediaServerItem mediaSerItem); + int updateToDatabase(MediaServerItem mediaSerItem); + void resetOnlineServerItem(MediaServerItem serverItem); WVPResult checkMediaServer(String ip, int port, String secret); diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java index 141c7cfe..b6bfc00c 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java @@ -285,7 +285,7 @@ public class MediaServerServiceImpl implements IMediaServerService, CommandLineR mediaServerItem.setId(zlmServerConfig.getGeneralMediaServerId()); zlmServerConfig.setIp(mediaServerItem.getIp()); mediaServerMapper.add(mediaServerItem); - handLeZLMServerConfig(zlmServerConfig); + zlmServerOnline(zlmServerConfig); result.setCode(0); result.setMsg("success"); }else { @@ -305,12 +305,19 @@ public class MediaServerServiceImpl implements IMediaServerService, CommandLineR return mediaServerMapper.add(mediaSerItem); } + @Override + public int updateToDatabase(MediaServerItem mediaSerItem) { + return mediaServerMapper.update(mediaSerItem); + } + + + /** * 处理zlm上线 * @param zlmServerConfig zlm上线携带的参数 */ @Override - public void handLeZLMServerConfig(ZLMServerConfig zlmServerConfig) { + public void zlmServerOnline(ZLMServerConfig zlmServerConfig) { logger.info("[ ZLM:{} ]-[ {}:{} ]已连接", zlmServerConfig.getGeneralMediaServerId(), zlmServerConfig.getIp(), zlmServerConfig.getHttpPort()); diff --git a/src/main/java/com/genersoft/iot/vmp/storager/IVideoManagerStorager.java b/src/main/java/com/genersoft/iot/vmp/storager/IVideoManagerStorager.java index 39dc5ad0..4c988d9a 100644 --- a/src/main/java/com/genersoft/iot/vmp/storager/IVideoManagerStorager.java +++ b/src/main/java/com/genersoft/iot/vmp/storager/IVideoManagerStorager.java @@ -49,6 +49,14 @@ public interface IVideoManagerStorager { */ public void updateChannel(String deviceId, DeviceChannel channel); + /** + * 批量添加设备通道 + * + * @param deviceId 设备id + * @param channels 多个通道 + */ + public void updateChannels(String deviceId, List channels); + /** * 开始播放 * @param deviceId 设备id diff --git a/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceChannelMapper.java b/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceChannelMapper.java index eef7a155..1d0b3659 100644 --- a/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceChannelMapper.java +++ b/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceChannelMapper.java @@ -25,30 +25,30 @@ public interface DeviceChannelMapper { @Update(value = {" "}) int update(DeviceChannel channel); @@ -57,16 +57,19 @@ public interface DeviceChannelMapper { "SELECT * FROM ( "+ " SELECT * , (SELECT count(0) FROM device_channel WHERE parentId=dc.channelId) as subCount FROM device_channel dc " + " WHERE dc.deviceId=#{deviceId} " + - " AND (dc.channelId LIKE '%${query}%' OR dc.name LIKE '%${query}%' OR dc.name LIKE '%${query}%') " + - " AND dc.parentId=#{parentChannelId} " + - " AND dc.status=1" + - " AND dc.status=0) dcr" + + " AND (dc.channelId LIKE '%${query}%' OR dc.name LIKE '%${query}%' OR dc.name LIKE '%${query}%') " + + " AND dc.parentId=#{parentChannelId} " + + " AND dc.status=1" + + " AND dc.status=0) dcr" + " WHERE 1=1 " + - " AND subCount >0" + - " AND subCount=0" + + " AND subCount >0" + + " AND subCount=0" + " ORDER BY channelId ASC" + " "}) - List queryChannelsByDeviceId(String deviceId, String parentChannelId, String query, Boolean hasSubChannel, Boolean online); + List queryChannels(String deviceId, String parentChannelId, String query, Boolean hasSubChannel, Boolean online); + + @Select("SELECT * FROM device_channel WHERE deviceId=#{deviceId}") + List queryChannelsByDeviceId(String deviceId); @Select("SELECT * FROM device_channel WHERE deviceId=#{deviceId} AND channelId=#{channelId}") DeviceChannel queryChannel(String deviceId, String channelId); @@ -92,15 +95,15 @@ public interface DeviceChannelMapper { "FROM device_channel dc " + "LEFT JOIN device de ON dc.deviceId = de.deviceId " + " WHERE 1=1 " + - " AND (dc.channelId LIKE '%${query}%' OR dc.name LIKE '%${query}%' OR dc.name LIKE '%${query}%') " + - " AND dc.status=1 " + - " AND dc.status=0 " + + " AND (dc.channelId LIKE '%${query}%' OR dc.name LIKE '%${query}%' OR dc.name LIKE '%${query}%') " + + " AND dc.status=1 " + + " AND dc.status=0 " + ") dcr" + " WHERE 1=1 " + - " AND subCount >0 " + - " AND subCount=0 " + - " AND platformId='${platformId}' " + - " AND (platformId != '${platformId}' OR platformId is NULL ) " + + " AND subCount >0 " + + " AND subCount=0 " + + " AND platformId='${platformId}' " + + " AND (platformId != '${platformId}' OR platformId is NULL ) " + " ORDER BY deviceId, channelId ASC" + " "}) @@ -114,4 +117,55 @@ public interface DeviceChannelMapper { @Update(value = {"UPDATE device_channel SET status=1 WHERE deviceId=#{deviceId} AND channelId=#{channelId}"}) void online(String deviceId, String channelId); + + @Insert("") + void batchAdd(List addChannels); + + @Update({""}) + void batchUpdate(List updateChannels); } diff --git a/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java b/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java index a5a4be7e..35dda131 100644 --- a/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java @@ -151,6 +151,49 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager { } } + @Override + public void updateChannels(String deviceId, List channels) { + List addChannels = new ArrayList<>(); + List updateChannels = new ArrayList<>(); + HashMap channelsInStore = new HashMap<>(); + if (channels != null && channels.size() > 0) { + List channelList = deviceChannelMapper.queryChannelsByDeviceId(deviceId); + if (channelList.size() == 0) { + for (DeviceChannel channel : channels) { + channel.setDeviceId(deviceId); + channel.setStreamId(streamSession.getStreamId(deviceId, channel.getChannelId())); + String now = this.format.format(System.currentTimeMillis()); + channel.setUpdateTime(now); + channel.setCreateTime(now); + addChannels.add(channel); + } + }else { + for (DeviceChannel deviceChannel : channelList) { + channelsInStore.put(deviceChannel.getChannelId(), deviceChannel); + } + for (DeviceChannel channel : channels) { + String channelId = channel.getChannelId(); + channel.setDeviceId(deviceId); + channel.setStreamId(streamSession.getStreamId(deviceId, channel.getChannelId())); + String now = this.format.format(System.currentTimeMillis()); + channel.setUpdateTime(now); + if (channelsInStore.get(channel.getChannelId()) != null) { + updateChannels.add(channel); + }else { + addChannels.add(channel); + channel.setCreateTime(now); + } + } + } + if (addChannels.size() > 0) { + deviceChannelMapper.batchAdd(addChannels); + } + if (updateChannels.size() > 0) { + deviceChannelMapper.batchUpdate(updateChannels); + } + } + } + @Override public void deviceChannelOnline(String deviceId, String channelId) { deviceChannelMapper.online(deviceId, channelId); @@ -186,19 +229,19 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager { public PageInfo queryChannelsByDeviceId(String deviceId, String query, Boolean hasSubChannel, Boolean online, int page, int count) { // 获取到所有正在播放的流 PageHelper.startPage(page, count); - List all = deviceChannelMapper.queryChannelsByDeviceId(deviceId, null, query, hasSubChannel, online); + List all = deviceChannelMapper.queryChannels(deviceId, null, query, hasSubChannel, online); return new PageInfo<>(all); } @Override public List queryChannelsByDeviceId(String deviceId) { - return deviceChannelMapper.queryChannelsByDeviceId(deviceId, null,null, null, null); + return deviceChannelMapper.queryChannels(deviceId, null,null, null, null); } @Override public PageInfo querySubChannels(String deviceId, String parentChannelId, String query, Boolean hasSubChannel, String online, int page, int count) { PageHelper.startPage(page, count); - List all = deviceChannelMapper.queryChannelsByDeviceId(deviceId, parentChannelId, null, null, null); + List all = deviceChannelMapper.queryChannels(deviceId, parentChannelId, null, null, null); return new PageInfo<>(all); } diff --git a/src/main/resources/all-application.yml b/src/main/resources/all-application.yml index c00ee2a1..20587424 100644 --- a/src/main/resources/all-application.yml +++ b/src/main/resources/all-application.yml @@ -38,6 +38,9 @@ spring: password: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.sqlite.JDBC + journal_mode: WAL + synchronous: NORMAL + transaction_mode: IMMEDIATE max-active: 1 min-idle: 1 diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 512152b9..066702a0 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -26,6 +26,9 @@ spring: password: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.sqlite.JDBC + journal_mode: WAL + synchronous: NORMAL + transaction_mode: IMMEDIATE max-active: 1 min-idle: 1 diff --git a/src/main/resources/application-docker.yml b/src/main/resources/application-docker.yml index 37c3294f..7eefe888 100644 --- a/src/main/resources/application-docker.yml +++ b/src/main/resources/application-docker.yml @@ -26,6 +26,9 @@ spring: password: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.sqlite.JDBC + journal_mode: WAL + synchronous: NORMAL + transaction_mode: IMMEDIATE max-active: 1 min-idle: 1