whycxzp
2025-04-14 8e85ad8e022d2885c410d0654bce08e2a392b08f
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.whyc.service;
 
import com.google.gson.internal.LinkedTreeMap;
import com.whyc.dto.Mp4FileDTO;
import com.whyc.dto.Response;
import com.whyc.dto.Response4Http;
import com.whyc.util.HttpUtil;
import com.whyc.util.JsonUtil;
import org.springframework.stereotype.Service;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
import static com.whyc.util.HttpUtil.urlEncode;
 
/**
 * 视频相关服务封装
 */
@Service
public class VideoService {
 
    /**
     * 获取视频流id
     * @return
     */
    public Response4Http getVideoStreamIds() {
        Response4Http responseResult = new Response4Http<>();
 
        Map<String, Object> params = new HashMap<>();//组合参数
        params.put("secret", "TWSYFgYJOQWB4ftgeYut8DW4wbs7pQnj");
        String queryParams = urlEncode(params);
        String httpUrl ="http://127.0.0.1:9092/index/api/getMediaList";
        Response response = HttpUtil.doGet(httpUrl, queryParams);
        Integer httpResponseCode = response.getCode();
        String responseJson = (String) response.getData();
        if(httpResponseCode == 1) { //请求成功
            Response4Http responseHttp = JsonUtil.getGson().fromJson(responseJson, Response4Http.class);
            if(responseHttp.getCode() == 0 ) { //接口返回状态码为成功
                if(responseHttp.getData()!=null) {
                    List<LinkedTreeMap<String, Object>> list = (List) responseHttp.getData();
                    Stream<LinkedTreeMap<String, Object>> linkedTreeMapStream = list.stream().filter(item -> ((String) item.get("schema")).equals("rtsp"));
                    return responseResult.setII(1,true,linkedTreeMapStream.map(item -> (String) item.get("stream")).collect(Collectors.toList()),null);
                }{
                    return responseResult.set(1,true,"未查询到已开启的流");
                }
            }else{ //接口返回状态码为失败,获取msg
                return responseResult.set(1,false,responseHttp.getMsg());
            }
        }else{ //请求失败
            return responseResult.set(0,response.getData(),response.getMsg());
        }
    }
 
    /**
     * 1个小时录制为视频录制单个时长 :
     *  max_second = 1*60*60
     *
     * @param streamId
     * @return
     */
    public Response4Http startRecord(String streamId){
        Map<String, Object> params = new HashMap<>();//组合参数
        params.put("secret", "TWSYFgYJOQWB4ftgeYut8DW4wbs7pQnj");
        params.put("type", 1);
        params.put("vhost", "__defaultVhost__");
        params.put("app", "rtp");
        params.put("stream", streamId);
        params.put("max_second",3600);
        String httpUrl ="http://127.0.0.1:9092/index/api/startRecord";
        //Response response = HttpUtil.doGet(httpUrl, queryParams);
        //Integer code = response.getCode();
        //String responseJson = (String) response.getData();
        return HttpUtil.doGet(httpUrl,params);
 
    }
 
    public Response4Http stopRecord(String streamId){
        Map<String, Object> params = new HashMap<>();//组合参数
        params.put("secret", "TWSYFgYJOQWB4ftgeYut8DW4wbs7pQnj");
        params.put("type", 1);
        params.put("vhost", "__defaultVhost__");
        params.put("app", "rtp");
        params.put("stream", streamId);
        String httpUrl ="http://127.0.0.1:9092/index/api/stopRecord";
        return HttpUtil.doGet(httpUrl,params);
        /*String queryParams = urlEncode(params);
        String httpUrl ="http://127.0.0.1:9092/index/api/stopRecord";
        Response response = HttpUtil.doGet(httpUrl, queryParams);
        Integer code = response.getCode();
        String responseJson = (String) response.getData();
        if(code == 1) { //请求成功
            Response4Http responseHttp = HttpUtil.getGson().fromJson(responseJson, Response4Http.class);
            if(responseHttp.getCode() == 0 && responseHttp.getMsg().equals("success")) { //请求成功
            }else{
                System.out.println(responseHttp.getMsg());
            }
        }*/
 
    }
 
    /**
     * getMp4RecordFile
     */
    public Response4Http<String> getMp4RecordFile(String streamId, String period){
        Map<String, Object> params = new HashMap<>();//组合参数
        params.put("secret", "TWSYFgYJOQWB4ftgeYut8DW4wbs7pQnj");
        params.put("type", 1);
        params.put("vhost", "__defaultVhost__");
        params.put("app", "rtp");
        params.put("stream", streamId);
        params.put("period",period);
        String httpUrl ="http://127.0.0.1:9092/index/api/getMp4RecordFile";
        Response4Http response4Http = HttpUtil.doGet(httpUrl, params);
        //data格式为:"data" : {
        //      "paths" : [ "2020-01-25", "2020-01-24" ],
        //      "rootPath" : "/www/record/live/ss/"
        //   }
        String dataStr = (String) response4Http.getData2();
        Mp4FileDTO mp4FileDTO = JsonUtil.getGson().fromJson(dataStr, Mp4FileDTO.class);
        response4Http.setData2(mp4FileDTO);
        return response4Http;
 
        /*if(code == 1) { //请求成功
            Response responseHttp = HttpUtil.getGson().fromJson(responseJson, Response.class);
            if(responseHttp.getCode() == 0 && responseHttp.getMsg().equals("success")) { //请求成功
                System.out.println(responseHttp.getData());
                return responseJson;
            }else{
                System.out.println(responseHttp.getMsg());
            }
        }*/
 
    }
 
    public Response4Http<String> openRtpServer(String streamId) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("port", 0);
        paramMap.put("tcp_mode", 1);
        paramMap.put("stream_id", streamId);
        paramMap.put("secret", "TWSYFgYJOQWB4ftgeYut8DW4wbs7pQnj");
        return HttpUtil.doGet("http://127.0.0.1:9092/index/api/openRtpServer", paramMap);
    }
 
    public Response4Http<String> addStreamProxy(String streamId) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("vhost", "__defaultVhost__");
        paramMap.put("app", "rtp");
        paramMap.put("stream", streamId);
        paramMap.put("url", "rtsp://127.0.0.1:554/rtp/"+streamId);
        paramMap.put("secret", "TWSYFgYJOQWB4ftgeYut8DW4wbs7pQnj");
        return HttpUtil.doGet("http://127.0.0.1:9092/index/api/addStreamProxy", paramMap);
    }
 
    public Response4Http isRecording(String streamId){
        Map<String, Object> params = new HashMap<>();//组合参数
        params.put("secret", "TWSYFgYJOQWB4ftgeYut8DW4wbs7pQnj");
        params.put("type", 1);
        params.put("vhost", "__defaultVhost__");
        params.put("app", "rtp");
        params.put("stream", streamId);
        String httpUrl ="http://127.0.0.1:9092/index/api/isRecording";
        return HttpUtil.doGet(httpUrl, params);
    }
 
}