| | |
| | | package com.genersoft.iot.vmp.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alibaba.fastjson.TypeReference; |
| | | import com.genersoft.iot.vmp.common.StreamInfo; |
| | | import com.genersoft.iot.vmp.conf.UserSetup; |
| | | import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel; |
| | | import com.genersoft.iot.vmp.gb28181.bean.GbStream; |
| | | import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform; |
| | | import com.genersoft.iot.vmp.gb28181.bean.PlatformCatalog; |
| | | import com.genersoft.iot.vmp.conf.MediaConfig; |
| | | import com.genersoft.iot.vmp.conf.UserSetting; |
| | | import com.genersoft.iot.vmp.gb28181.bean.*; |
| | | import com.genersoft.iot.vmp.gb28181.event.EventPublisher; |
| | | import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent; |
| | | import com.genersoft.iot.vmp.media.zlm.ZLMHttpHookSubscribe; |
| | | import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils; |
| | | import com.genersoft.iot.vmp.media.zlm.ZLMServerConfig; |
| | | import com.genersoft.iot.vmp.media.zlm.dto.*; |
| | | import com.genersoft.iot.vmp.service.IGbStreamService; |
| | | import com.genersoft.iot.vmp.service.IMediaServerService; |
| | | import com.genersoft.iot.vmp.service.IStreamPushService; |
| | | import com.genersoft.iot.vmp.service.bean.StreamPushItemFromRedis; |
| | | import com.genersoft.iot.vmp.storager.IRedisCatchStorage; |
| | | import com.genersoft.iot.vmp.storager.dao.*; |
| | | import com.genersoft.iot.vmp.vmanager.bean.StreamPushExcelDto; |
| | | import com.genersoft.iot.vmp.utils.DateUtil; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.TransactionDefinition; |
| | | import org.springframework.transaction.TransactionStatus; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.util.*; |
| | |
| | | @Service |
| | | public class StreamPushServiceImpl implements IStreamPushService { |
| | | |
| | | private final static Logger logger = LoggerFactory.getLogger(StreamPushServiceImpl.class); |
| | | |
| | | @Autowired |
| | | private GbStreamMapper gbStreamMapper; |
| | | |
| | | @Autowired |
| | | private StreamPushMapper streamPushMapper; |
| | | |
| | | @Autowired |
| | | private StreamProxyMapper streamProxyMapper; |
| | | |
| | | @Autowired |
| | | private ParentPlatformMapper parentPlatformMapper; |
| | |
| | | private IRedisCatchStorage redisCatchStorage; |
| | | |
| | | @Autowired |
| | | private UserSetup userSetup; |
| | | private UserSetting userSetting; |
| | | |
| | | @Autowired |
| | | private IMediaServerService mediaServerService; |
| | | |
| | | @Autowired |
| | | DataSourceTransactionManager dataSourceTransactionManager; |
| | | |
| | | @Autowired |
| | | TransactionDefinition transactionDefinition; |
| | | |
| | | @Autowired |
| | | private MediaConfig mediaConfig; |
| | | |
| | | |
| | | @Override |
| | | public List<StreamPushItem> handleJSON(String jsonData, MediaServerItem mediaServerItem) { |
| | | if (jsonData == null) return null; |
| | | if (jsonData == null) { |
| | | return null; |
| | | } |
| | | |
| | | Map<String, StreamPushItem> result = new HashMap<>(); |
| | | |
| | |
| | | streamPushItem.setOriginType(item.getOriginType()); |
| | | streamPushItem.setOriginTypeStr(item.getOriginTypeStr()); |
| | | streamPushItem.setOriginUrl(item.getOriginUrl()); |
| | | streamPushItem.setCreateStamp(item.getCreateStamp() * 1000); |
| | | streamPushItem.setCreateTime(DateUtil.getNow()); |
| | | streamPushItem.setAliveSecond(item.getAliveSecond()); |
| | | streamPushItem.setStatus(true); |
| | | streamPushItem.setStreamType("push"); |
| | | streamPushItem.setVhost(item.getVhost()); |
| | | streamPushItem.setServerId(item.getSeverId()); |
| | | return streamPushItem; |
| | | } |
| | | |
| | |
| | | public boolean saveToGB(GbStream stream) { |
| | | stream.setStreamType("push"); |
| | | stream.setStatus(true); |
| | | stream.setCreateStamp(System.currentTimeMillis()); |
| | | stream.setCreateTime(DateUtil.getNow()); |
| | | stream.setStreamType("push"); |
| | | stream.setMediaServerId(mediaConfig.getId()); |
| | | int add = gbStreamMapper.add(stream); |
| | | |
| | | // 查找开启了全部直播流共享的上级平台 |
| | | List<ParentPlatform> parentPlatforms = parentPlatformMapper.selectAllAhareAllLiveStream(); |
| | | if (parentPlatforms.size() > 0) { |
| | | for (ParentPlatform parentPlatform : parentPlatforms) { |
| | | stream.setCatalogId(parentPlatform.getCatalogId()); |
| | | stream.setPlatformId(parentPlatform.getServerGBId()); |
| | | String streamId = stream.getStream(); |
| | | StreamProxyItem streamProxyItem = platformGbStreamMapper.selectOne(stream.getApp(), streamId, parentPlatform.getServerGBId()); |
| | | if (streamProxyItem == null) { |
| | | platformGbStreamMapper.add(stream); |
| | | eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.ADD); |
| | | }else { |
| | | if (!streamProxyItem.getGbId().equals(stream.getGbId())) { |
| | | // 此流使用另一个国标Id已经与该平台关联,移除此记录 |
| | | platformGbStreamMapper.delByAppAndStreamAndPlatform(stream.getApp(), streamId, parentPlatform.getServerGBId()); |
| | | platformGbStreamMapper.add(stream); |
| | | eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.ADD); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | return add > 0; |
| | | } |
| | | |
| | |
| | | public boolean removeFromGB(GbStream stream) { |
| | | // 判断是否需要发送事件 |
| | | gbStreamService.sendCatalogMsg(stream, CatalogEvent.DEL); |
| | | int del = gbStreamMapper.del(stream.getApp(), stream.getStream()); |
| | | platformGbStreamMapper.delByAppAndStream(stream.getApp(), stream.getStream()); |
| | | int del = gbStreamMapper.del(stream.getApp(), stream.getStream()); |
| | | MediaServerItem mediaInfo = mediaServerService.getOne(stream.getMediaServerId()); |
| | | JSONObject mediaList = zlmresTfulUtils.getMediaList(mediaInfo, stream.getApp(), stream.getStream()); |
| | | if (mediaList == null) { |
| | | streamPushMapper.del(stream.getApp(), stream.getStream()); |
| | | if (mediaList != null) { |
| | | if (mediaList.getInteger("code") == 0) { |
| | | JSONArray data = mediaList.getJSONArray("data"); |
| | | if (data == null) { |
| | | streamPushMapper.del(stream.getApp(), stream.getStream()); |
| | | } |
| | | } |
| | | } |
| | | return del > 0; |
| | | } |
| | |
| | | |
| | | @Override |
| | | public StreamPushItem getPush(String app, String streamId) { |
| | | |
| | | return streamPushMapper.selectOne(app, streamId); |
| | | } |
| | | |
| | |
| | | StreamPushItem streamPushItem = streamPushMapper.selectOne(app, streamId); |
| | | gbStreamService.sendCatalogMsg(streamPushItem, CatalogEvent.DEL); |
| | | |
| | | int delStream = streamPushMapper.del(app, streamId); |
| | | gbStreamMapper.del(app, streamId); |
| | | platformGbStreamMapper.delByAppAndStream(app, streamId); |
| | | gbStreamMapper.del(app, streamId); |
| | | int delStream = streamPushMapper.del(app, streamId); |
| | | if (delStream > 0) { |
| | | MediaServerItem mediaServerItem = mediaServerService.getOne(streamPushItem.getMediaServerId()); |
| | | zlmresTfulUtils.closeStreams(mediaServerItem,app, streamId); |
| | |
| | | Map<String, MediaItem> streamInfoPushItemMap = new HashMap<>(); |
| | | if (pushList.size() > 0) { |
| | | for (StreamPushItem streamPushItem : pushList) { |
| | | if (StringUtils.isEmpty(streamPushItem.getGbId())) { |
| | | if (ObjectUtils.isEmpty(streamPushItem.getGbId())) { |
| | | pushItemMap.put(streamPushItem.getApp() + streamPushItem.getStream(), streamPushItem); |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | zlmresTfulUtils.getMediaList(mediaServerItem, (mediaList ->{ |
| | | if (mediaList == null) return; |
| | | if (mediaList == null) { |
| | | return; |
| | | } |
| | | String dataStr = mediaList.getString("data"); |
| | | |
| | | Integer code = mediaList.getInteger("code"); |
| | |
| | | String type = "PUSH"; |
| | | for (MediaItem offlineMediaItem : offlineMediaItemList) { |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("serverId", userSetup.getServerId()); |
| | | jsonObject.put("serverId", userSetting.getServerId()); |
| | | jsonObject.put("app", offlineMediaItem.getApp()); |
| | | jsonObject.put("stream", offlineMediaItem.getStream()); |
| | | jsonObject.put("register", false); |
| | |
| | | streamPushMapper.deleteWithoutGBId(mediaServerId); |
| | | gbStreamMapper.deleteWithoutGBId("push", mediaServerId); |
| | | // 其他的流设置未启用 |
| | | gbStreamMapper.updateStatusByMediaServerId(mediaServerId, false); |
| | | streamPushMapper.updateStatusByMediaServerId(mediaServerId, false); |
| | | streamProxyMapper.updateStatusByMediaServerId(mediaServerId, false); |
| | | // 发送流停止消息 |
| | | String type = "PUSH"; |
| | | // 发送redis消息 |
| | |
| | | // 移除redis内流的信息 |
| | | redisCatchStorage.removeStream(mediaServerId, type, mediaItem.getApp(), mediaItem.getStream()); |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("serverId", userSetup.getServerId()); |
| | | jsonObject.put("serverId", userSetting.getServerId()); |
| | | jsonObject.put("app", mediaItem.getApp()); |
| | | jsonObject.put("stream", mediaItem.getStream()); |
| | | jsonObject.put("register", false); |
| | |
| | | streamPushItem.setStreamType("push"); |
| | | streamPushItem.setStatus(true); |
| | | streamPushItem.setGbId("34020000004111" + gbId); |
| | | streamPushItem.setCreateStamp(System.currentTimeMillis()); |
| | | streamPushItem.setCreateTime(DateUtil.getNow()); |
| | | gbId ++; |
| | | } |
| | | int limitCount = 30; |
| | |
| | | public void batchAdd(List<StreamPushItem> streamPushItems) { |
| | | streamPushMapper.addAll(streamPushItems); |
| | | gbStreamMapper.batchAdd(streamPushItems); |
| | | // 查找开启了全部直播流共享的上级平台 |
| | | List<ParentPlatform> parentPlatforms = parentPlatformMapper.selectAllAhareAllLiveStream(); |
| | | if (parentPlatforms.size() > 0) { |
| | | for (StreamPushItem stream : streamPushItems) { |
| | | for (ParentPlatform parentPlatform : parentPlatforms) { |
| | | stream.setCatalogId(parentPlatform.getCatalogId()); |
| | | stream.setPlatformId(parentPlatform.getServerGBId()); |
| | | String streamId = stream.getStream(); |
| | | StreamProxyItem streamProxyItem = platformGbStreamMapper.selectOne(stream.getApp(), streamId, parentPlatform.getServerGBId()); |
| | | if (streamProxyItem == null) { |
| | | platformGbStreamMapper.add(stream); |
| | | eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.ADD); |
| | | }else { |
| | | if (!streamProxyItem.getGbId().equals(stream.getGbId())) { |
| | | // 此流使用另一个国标Id已经与该平台关联,移除此记录 |
| | | platformGbStreamMapper.delByAppAndStreamAndPlatform(stream.getApp(), streamId, parentPlatform.getServerGBId()); |
| | | platformGbStreamMapper.add(stream); |
| | | eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.ADD); |
| | | stream.setGbId(streamProxyItem.getGbId()); |
| | | eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.DEL); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void batchAddForUpload(List<StreamPushItem> streamPushItems, Map<String, List<String[]>> streamPushItemsForAll ) { |
| | |
| | | .collect(Collectors.toList()); |
| | | |
| | | if (streamPushItemsForPlatform.size() > 0) { |
| | | // 获取所有平台,平台和目录信息一般不会特别大量。 |
| | | List<ParentPlatform> parentPlatformList = parentPlatformMapper.getParentPlatformList(); |
| | | Map<String, Map<String, PlatformCatalog>> platformInfoMap = new HashMap<>(); |
| | | if (parentPlatformList.size() == 0) { |
| | | return; |
| | | } |
| | | for (ParentPlatform platform : parentPlatformList) { |
| | | Map<String, PlatformCatalog> catalogMap = new HashMap<>(); |
| | | |
| | | // 创建根节点 |
| | | PlatformCatalog platformCatalog = new PlatformCatalog(); |
| | | platformCatalog.setId(platform.getServerGBId()); |
| | | catalogMap.put(platform.getServerGBId(), platformCatalog); |
| | | |
| | | // 查询所有节点信息 |
| | | List<PlatformCatalog> platformCatalogs = platformCatalogMapper.selectByPlatForm(platform.getServerGBId()); |
| | | if (platformCatalogs.size() > 0) { |
| | | for (PlatformCatalog catalog : platformCatalogs) { |
| | | catalogMap.put(catalog.getId(), catalog); |
| | | } |
| | | } |
| | | platformInfoMap.put(platform.getServerGBId(), catalogMap); |
| | | } |
| | | List<StreamPushItem> streamPushItemListFroPlatform = new ArrayList<>(); |
| | | Map<String, List<StreamPushItem>> platformForEvent = new HashMap<>(); |
| | | Map<String, List<GbStream>> platformForEvent = new HashMap<>(); |
| | | // 遍历存储结果,查找app+Stream->platformId+catalogId的对应关系,然后执行批量写入 |
| | | for (StreamPushItem streamPushItem : streamPushItemsForPlatform) { |
| | | List<String[]> platFormInfoList = streamPushItemsForAll.get(streamPushItem.getApp() + streamPushItem.getStream()); |
| | | if (platFormInfoList != null) { |
| | | if (platFormInfoList.size() > 0) { |
| | | for (String[] platFormInfoArray : platFormInfoList) { |
| | | StreamPushItem streamPushItemForPlatform = new StreamPushItem(); |
| | | streamPushItemForPlatform.setGbStreamId(streamPushItem.getGbStreamId()); |
| | | if (platFormInfoArray.length > 0) { |
| | | // 数组 platFormInfoArray 0 为平台ID。 1为目录ID |
| | | streamPushItemForPlatform.setPlatformId(platFormInfoArray[0]); |
| | | |
| | | List<StreamPushItem> streamPushItemsInPlatform = platformForEvent.get(streamPushItem.getPlatformId()); |
| | | if (streamPushItemsInPlatform == null) { |
| | | streamPushItemsInPlatform = new ArrayList<>(); |
| | | platformForEvent.put(platFormInfoArray[0], streamPushItemsInPlatform); |
| | | } |
| | | // 为发送通知整理数据 |
| | | streamPushItemForPlatform.setApp(streamPushItem.getApp()); |
| | | streamPushItemForPlatform.setStream(streamPushItem.getStream()); |
| | | streamPushItemForPlatform.setGbId(streamPushItem.getGbId()); |
| | | streamPushItemsInPlatform.add(streamPushItemForPlatform); |
| | | if (platFormInfoList != null && platFormInfoList.size() > 0) { |
| | | for (String[] platFormInfoArray : platFormInfoList) { |
| | | StreamPushItem streamPushItemForPlatform = new StreamPushItem(); |
| | | streamPushItemForPlatform.setGbStreamId(streamPushItem.getGbStreamId()); |
| | | if (platFormInfoArray.length > 0) { |
| | | // 数组 platFormInfoArray 0 为平台ID。 1为目录ID |
| | | // 不存在这个平台,则忽略导入此关联关系 |
| | | if (platformInfoMap.get(platFormInfoArray[0]) == null |
| | | || platformInfoMap.get(platFormInfoArray[0]).get(platFormInfoArray[1]) == null) { |
| | | logger.info("导入数据时不存在平台或目录{}/{},已导入未分配", platFormInfoArray[0], platFormInfoArray[1] ); |
| | | continue; |
| | | } |
| | | if (platFormInfoArray.length > 1) { |
| | | streamPushItemForPlatform.setCatalogId(platFormInfoArray[1]); |
| | | streamPushItemForPlatform.setPlatformId(platFormInfoArray[0]); |
| | | List<GbStream> gbStreamList = platformForEvent.get(platFormInfoArray[0]); |
| | | if (gbStreamList == null) { |
| | | gbStreamList = new ArrayList<>(); |
| | | platformForEvent.put(platFormInfoArray[0], gbStreamList); |
| | | } |
| | | streamPushItemListFroPlatform.add(streamPushItemForPlatform); |
| | | |
| | | |
| | | // 为发送通知整理数据 |
| | | streamPushItemForPlatform.setName(streamPushItem.getName()); |
| | | streamPushItemForPlatform.setApp(streamPushItem.getApp()); |
| | | streamPushItemForPlatform.setStream(streamPushItem.getStream()); |
| | | streamPushItemForPlatform.setGbId(streamPushItem.getGbId()); |
| | | gbStreamList.add(streamPushItemForPlatform); |
| | | } |
| | | if (platFormInfoArray.length > 1) { |
| | | streamPushItemForPlatform.setCatalogId(platFormInfoArray[1]); |
| | | } |
| | | streamPushItemListFroPlatform.add(streamPushItemForPlatform); |
| | | } |
| | | |
| | | } |
| | | } |
| | | platformGbStreamMapper.batchAdd(streamPushItemListFroPlatform); |
| | | // 发送通知 |
| | | for (String platformId : platformForEvent.keySet()) { |
| | | eventPublisher.catalogEventPublishForStream( |
| | | platformId, platformForEvent.get(platformId).toArray(new GbStream[0]), CatalogEvent.ADD); |
| | | if (streamPushItemListFroPlatform.size() > 0) { |
| | | platformGbStreamMapper.batchAdd(streamPushItemListFroPlatform); |
| | | // 发送通知 |
| | | for (String platformId : platformForEvent.keySet()) { |
| | | eventPublisher.catalogEventPublishForStream( |
| | | platformId, platformForEvent.get(platformId), CatalogEvent.ADD); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | gbStreamService.sendCatalogMsgs(gbStreams, CatalogEvent.DEL); |
| | | |
| | | int delStream = streamPushMapper.delAllForGbStream(gbStreams); |
| | | gbStreamMapper.batchDelForGbStream(gbStreams); |
| | | platformGbStreamMapper.delByGbStreams(gbStreams); |
| | | gbStreamMapper.batchDelForGbStream(gbStreams); |
| | | int delStream = streamPushMapper.delAllForGbStream(gbStreams); |
| | | if (delStream > 0) { |
| | | for (GbStream gbStream : gbStreams) { |
| | | MediaServerItem mediaServerItem = mediaServerService.getOne(gbStream.getMediaServerId()); |
| | |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public void allStreamOffline() { |
| | | List<GbStream> onlinePushers = streamPushMapper.getOnlinePusherForGb(); |
| | | if (onlinePushers.size() == 0) { |
| | | return; |
| | | } |
| | | streamPushMapper.setAllStreamOffline(); |
| | | |
| | | // 发送通知 |
| | | eventPublisher.catalogEventPublishForStream(null, onlinePushers, CatalogEvent.OFF); |
| | | } |
| | | |
| | | @Override |
| | | public void offline(List<StreamPushItemFromRedis> offlineStreams) { |
| | | // 更新部分设备离线 |
| | | List<GbStream> onlinePushers = streamPushMapper.getOnlinePusherForGbInList(offlineStreams); |
| | | streamPushMapper.offline(offlineStreams); |
| | | // 发送通知 |
| | | eventPublisher.catalogEventPublishForStream(null, onlinePushers, CatalogEvent.OFF); |
| | | } |
| | | |
| | | @Override |
| | | public void online(List<StreamPushItemFromRedis> onlineStreams) { |
| | | // 更新部分设备上线streamPushService |
| | | List<GbStream> onlinePushers = streamPushMapper.getOfflinePusherForGbInList(onlineStreams); |
| | | streamPushMapper.online(onlineStreams); |
| | | // 发送通知 |
| | | eventPublisher.catalogEventPublishForStream(null, onlinePushers, CatalogEvent.ON); |
| | | } |
| | | |
| | | @Override |
| | | public boolean add(StreamPushItem stream) { |
| | | stream.setUpdateTime(DateUtil.getNow()); |
| | | stream.setCreateTime(DateUtil.getNow()); |
| | | stream.setServerId(userSetting.getServerId()); |
| | | |
| | | // 放在事务内执行 |
| | | boolean result = false; |
| | | TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition); |
| | | try { |
| | | int addStreamResult = streamPushMapper.add(stream); |
| | | if (!ObjectUtils.isEmpty(stream.getGbId())) { |
| | | stream.setStreamType("push"); |
| | | gbStreamMapper.add(stream); |
| | | } |
| | | dataSourceTransactionManager.commit(transactionStatus); |
| | | result = true; |
| | | }catch (Exception e) { |
| | | logger.error("批量移除流与平台的关系时错误", e); |
| | | dataSourceTransactionManager.rollback(transactionStatus); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public List<String> getAllAppAndStream() { |
| | | return streamPushMapper.getAllAppAndStream(); |
| | | } |
| | | } |