panlinlin
2021-05-08 e48fa711a3664bece9b3e58840a75fe7c05bc47c
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMMediaListManager.java
@@ -1,12 +1,10 @@
package com.genersoft.iot.vmp.media.zlm;
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.media.zlm.dto.StreamProxyItem;
import com.genersoft.iot.vmp.media.zlm.dto.StreamPushItem;
import com.genersoft.iot.vmp.gb28181.bean.GbStream;
import com.genersoft.iot.vmp.media.zlm.dto.MediaItem;
import com.genersoft.iot.vmp.service.IStreamPushService;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
@@ -42,6 +40,9 @@
    @Autowired
    private IStreamPushService streamPushService;
    @Autowired
    private ZLMHttpHookSubscribe subscribe;
    public void updateMediaList() {
        storager.clearMediaList();
@@ -66,12 +67,26 @@
            if (streamPushItems != null) {
                storager.updateMediaList(streamPushItems);
                for (StreamPushItem streamPushItem : streamPushItems) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("app", streamPushItem.getApp());
                    jsonObject.put("stream", streamPushItem.getStream());
                    subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_play,jsonObject,(response)->{
                        updateMedia(response.getString("app"), response.getString("stream"));
                    });
                }
            }
        }));
    }
    public void addMedia(String app, String streamId) {
        //使用异步更新推流
        updateMedia(app, streamId);
    }
    public void updateMedia(String app, String streamId) {
        //使用异步更新推流
        zlmresTfulUtils.getMediaList(app, streamId, "rtmp", json->{
@@ -105,4 +120,33 @@
            storager.mediaOutline(app, streamId);
        }
    }
    public void clearAllSessions() {
        logger.info("清空所有国标相关的session");
        JSONObject allSessionJSON = zlmresTfulUtils.getAllSession();
        ZLMServerConfig mediaInfo = redisCatchStorage.getMediaInfo();
        HashSet<String> allLocalPorts = new HashSet();
        if (allSessionJSON.getInteger("code") == 0) {
            JSONArray data = allSessionJSON.getJSONArray("data");
            if (data.size() > 0) {
                for (int i = 0; i < data.size(); i++) {
                    JSONObject sessionJOSN = data.getJSONObject(i);
                    Integer local_port = sessionJOSN.getInteger("local_port");
                    if (!local_port.equals(Integer.valueOf(mediaInfo.getHttpPort())) &&
                        !local_port.equals(Integer.valueOf(mediaInfo.getHttpSSLport())) &&
                        !local_port.equals(Integer.valueOf(mediaInfo.getRtmpPort())) &&
                        !local_port.equals(Integer.valueOf(mediaInfo.getRtspPort())) &&
                        !local_port.equals(Integer.valueOf(mediaInfo.getRtspSSlport())) &&
                        !local_port.equals(Integer.valueOf(mediaInfo.getHookOnFlowReport()))){
                        allLocalPorts.add(sessionJOSN.getInteger("local_port") + "");
                     }
                }
            }
        }
        if (allLocalPorts.size() > 0) {
            List<String> result = new ArrayList<>(allLocalPorts);
            String localPortSStr = String.join(",", result);
            zlmresTfulUtils.kickSessions(localPortSStr);
        }
    }
}