@ -3,11 +3,15 @@ package com.genersoft.iot.vmp.service.impl;
import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.JSONObject ;
import com.alibaba.fastjson.JSONObject ;
import com.alibaba.fastjson.TypeReference ;
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.GbStream ;
import com.genersoft.iot.vmp.gb28181.bean.GbStream ;
import com.genersoft.iot.vmp.media.zlm.ZLMHttpHookSubscribe ;
import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils ;
import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils ;
import com.genersoft.iot.vmp.media.zlm.ZLMServerConfig ;
import com.genersoft.iot.vmp.media.zlm.ZLMServerConfig ;
import com.genersoft.iot.vmp.media.zlm.dto.MediaItem ;
import com.genersoft.iot.vmp.media.zlm.dto.MediaItem ;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem ;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem ;
import com.genersoft.iot.vmp.media.zlm.dto.OriginType ;
import com.genersoft.iot.vmp.media.zlm.dto.StreamPushItem ;
import com.genersoft.iot.vmp.media.zlm.dto.StreamPushItem ;
import com.genersoft.iot.vmp.service.IMediaServerService ;
import com.genersoft.iot.vmp.service.IMediaServerService ;
import com.genersoft.iot.vmp.service.IStreamPushService ;
import com.genersoft.iot.vmp.service.IStreamPushService ;
@ -20,10 +24,7 @@ import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import org.springframework.stereotype.Service ;
import java.util.ArrayList ;
import java.util.* ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
@Service
@Service
public class StreamPushServiceImpl implements IStreamPushService {
public class StreamPushServiceImpl implements IStreamPushService {
@ -43,6 +44,9 @@ public class StreamPushServiceImpl implements IStreamPushService {
@Autowired
@Autowired
private IRedisCatchStorage redisCatchStorage ;
private IRedisCatchStorage redisCatchStorage ;
@Autowired
private UserSetup userSetup ;
@Autowired
@Autowired
private IMediaServerService mediaServerService ;
private IMediaServerService mediaServerService ;
@ -56,7 +60,9 @@ public class StreamPushServiceImpl implements IStreamPushService {
for ( MediaItem item : mediaItems ) {
for ( MediaItem item : mediaItems ) {
// 不保存国标推理以及拉流代理的流
// 不保存国标推理以及拉流代理的流
if ( item . getOriginType ( ) = = 1 | | item . getOriginType ( ) = = 2 | | item . getOriginType ( ) = = 8 ) {
if ( item . getOriginType ( ) = = OriginType . RTSP_PUSH . ordinal ( )
| | item . getOriginType ( ) = = OriginType . RTMP_PUSH . ordinal ( )
| | item . getOriginType ( ) = = OriginType . RTC_PUSH . ordinal ( ) ) {
String key = item . getApp ( ) + "_" + item . getStream ( ) ;
String key = item . getApp ( ) + "_" + item . getStream ( ) ;
StreamPushItem streamPushItem = result . get ( key ) ;
StreamPushItem streamPushItem = result . get ( key ) ;
if ( streamPushItem = = null ) {
if ( streamPushItem = = null ) {
@ -97,6 +103,11 @@ public class StreamPushServiceImpl implements IStreamPushService {
return new PageInfo < > ( all ) ;
return new PageInfo < > ( all ) ;
}
}
@Override
public List < StreamPushItem > getPushList ( String mediaServerId ) {
return streamPushMapper . selectAllByMediaServerId ( mediaServerId ) ;
}
@Override
@Override
public boolean saveToGB ( GbStream stream ) {
public boolean saveToGB ( GbStream stream ) {
stream . setStreamType ( "push" ) ;
stream . setStreamType ( "push" ) ;
@ -137,17 +148,84 @@ public class StreamPushServiceImpl implements IStreamPushService {
}
}
@Override
@Override
public void zlmServerOnline ( ZLMServerConfig zlmServerConfig ) {
public void zlmServerOnline ( String mediaServerId ) {
// 似乎没啥需要做的
// 同步zlm推流信息
MediaServerItem mediaServerItem = mediaServerService . getOne ( mediaServerId ) ;
if ( mediaServerItem = = null ) {
return ;
}
List < StreamPushItem > pushList = getPushList ( mediaServerId ) ;
if ( pushList . size ( ) > 0 ) {
Map < String , StreamPushItem > pushItemMap = new HashMap < > ( ) ;
for ( StreamPushItem streamPushItem : pushList ) {
pushItemMap . put ( streamPushItem . getApp ( ) + streamPushItem . getStream ( ) , streamPushItem ) ;
}
zlmresTfulUtils . getMediaList ( mediaServerItem , ( mediaList - > {
if ( mediaList = = null ) return ;
String dataStr = mediaList . getString ( "data" ) ;
Integer code = mediaList . getInteger ( "code" ) ;
List < StreamPushItem > streamPushItems = null ;
if ( code = = 0 ) {
if ( dataStr ! = null ) {
streamPushItems = handleJSON ( dataStr , mediaServerItem ) ;
}
}
if ( streamPushItems ! = null ) {
for ( StreamPushItem streamPushItem : streamPushItems ) {
pushItemMap . remove ( streamPushItem . getApp ( ) + streamPushItem . getStream ( ) ) ;
}
}
Collection < StreamPushItem > offlinePushItems = pushItemMap . values ( ) ;
if ( offlinePushItems . size ( ) > 0 ) {
String type = "PUSH" ;
streamPushMapper . delAll ( new ArrayList < > ( offlinePushItems ) ) ;
for ( StreamPushItem offlinePushItem : offlinePushItems ) {
JSONObject jsonObject = new JSONObject ( ) ;
jsonObject . put ( "serverId" , userSetup . getServerId ( ) ) ;
jsonObject . put ( "app" , offlinePushItem . getApp ( ) ) ;
jsonObject . put ( "stream" , offlinePushItem . getStream ( ) ) ;
jsonObject . put ( "register" , false ) ;
jsonObject . put ( "mediaServerId" , mediaServerId ) ;
redisCatchStorage . sendStreamChangeMsg ( type , jsonObject ) ;
// 移除redis内流的信息
redisCatchStorage . removeStream ( mediaServerItem . getId ( ) , "PUSH" , offlinePushItem . getApp ( ) , offlinePushItem . getStream ( ) ) ;
}
}
} ) ) ;
}
}
}
@Override
@Override
public void zlmServerOffline ( String mediaServerId ) {
public void zlmServerOffline ( String mediaServerId ) {
// 移除没有serverId的推流
List < StreamPushItem > streamPushItems = streamPushMapper . selectAllByMediaServerId ( mediaServerId ) ;
// 移除没有GBId的推流
streamPushMapper . deleteWithoutGBId ( mediaServerId ) ;
streamPushMapper . deleteWithoutGBId ( mediaServerId ) ;
gbStreamMapper . deleteWithoutGBId ( "push" , mediaServerId ) ;
// 其他的流设置未启用
// 其他的流设置未启用
gbStreamMapper . updateStatusByMediaServerId ( mediaServerId , false ) ;
gbStreamMapper . updateStatusByMediaServerId ( mediaServerId , false ) ;
// 发送流停止消息
String type = "PUSH" ;
// 发送redis消息
List < StreamInfo > streamInfoList = redisCatchStorage . getStreams ( mediaServerId , type ) ;
if ( streamInfoList . size ( ) > 0 ) {
for ( StreamInfo streamInfo : streamInfoList ) {
JSONObject jsonObject = new JSONObject ( ) ;
jsonObject . put ( "serverId" , userSetup . getServerId ( ) ) ;
jsonObject . put ( "app" , streamInfo . getApp ( ) ) ;
jsonObject . put ( "stream" , streamInfo . getStreamId ( ) ) ;
jsonObject . put ( "register" , false ) ;
jsonObject . put ( "mediaServerId" , mediaServerId ) ;
redisCatchStorage . sendStreamChangeMsg ( type , jsonObject ) ;
// 移除redis内流的信息
// 移除redis内流的信息
redisCatchStorage . removeStream ( mediaServerId , "PUSH" ) ;
redisCatchStorage . removeStream ( mediaServerId , type , streamInfo . getApp ( ) , streamInfo . getStreamId ( ) ) ;
}
}
}
@Override
public void clean ( ) {
}
}
}
}