panlinlin
2021-04-11 89244932c6185cd39e9a9f8aa8bf3acf99329335
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.genersoft.iot.vmp.media.zlm;
 
import com.alibaba.fastjson.JSON;
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;
import com.genersoft.iot.vmp.storager.dao.GbStreamMapper;
import com.genersoft.iot.vmp.storager.dao.PlatformGbStreamMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.*;
 
@Component
public class ZLMMediaListManager {
 
    private Logger logger = LoggerFactory.getLogger("ZLMMediaListManager");
 
    @Autowired
    private ZLMRESTfulUtils zlmresTfulUtils;
 
    @Autowired
    private IRedisCatchStorage redisCatchStorage;
 
    @Autowired
    private IVideoManagerStorager storager;
 
    @Autowired
    private GbStreamMapper gbStreamMapper;
 
    @Autowired
    private PlatformGbStreamMapper platformGbStreamMapper;
 
    @Autowired
    private IStreamPushService streamPushService;
 
    @Autowired
    private ZLMHttpHookSubscribe subscribe;
 
 
    public void updateMediaList() {
        storager.clearMediaList();
 
        // 使用异步的当时更新媒体流列表
        zlmresTfulUtils.getMediaList((mediaList ->{
            if (mediaList == null) return;
            String dataStr = mediaList.getString("data");
 
            Integer code = mediaList.getInteger("code");
            Map<String, StreamPushItem> result = new HashMap<>();
            List<StreamPushItem> streamPushItems = null;
            // 获取所有的国标关联
            List<GbStream> gbStreams = gbStreamMapper.selectAll();
            if (code == 0 ) {
                if (dataStr != null) {
                    streamPushItems = streamPushService.handleJSON(dataStr);
                }
            }else {
                logger.warn("更新视频流失败,错误code: " + code);
            }
 
            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)->{
                        System.out.println(1222211111);
                        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->{
 
            if (json == null) return;
            String dataStr = json.getString("data");
 
            Integer code = json.getInteger("code");
            Map<String, StreamPushItem> result = new HashMap<>();
            List<StreamPushItem> streamPushItems = null;
            if (code == 0 ) {
                if (dataStr != null) {
                    streamPushItems = streamPushService.handleJSON(dataStr);
                }
            }else {
                logger.warn("更新视频流失败,错误code: " + code);
            }
 
            if (streamPushItems != null && streamPushItems.size() == 1) {
                storager.updateMedia(streamPushItems.get(0));
            }
        });
    }
 
 
    public void removeMedia(String app, String streamId) {
        // 查找是否关联了国标, 关联了不删除, 置为离线
        StreamProxyItem streamProxyItem = gbStreamMapper.selectOne(app, streamId);
        if (streamProxyItem == null) {
            storager.removeMedia(app, streamId);
        }else {
            storager.mediaOutline(app, streamId);
        }
    }
}