whycxzp
2025-01-09 82258b502bb39bd7621617d79c59d2973fd4b378
src/main/java/com/whyc/service/VideoService.java
@@ -1,13 +1,17 @@
package com.whyc.service;
import com.google.gson.internal.LinkedTreeMap;
import com.whyc.dto.Mp4FileDTO;
import com.whyc.dto.Response;
import com.whyc.util.ActionUtil;
import com.whyc.dto.Response4Http;
import com.whyc.util.HttpUtil;
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;
@@ -21,70 +25,143 @@
     * 获取视频流id
     * @return
     */
    public List<String> getVideoStreamIds() {
    public Response4Http getVideoStreamIds() {
        Response4Http responseResult = new Response4Http<>();
        Map<String, Object> params = new HashMap<>();//组合参数
        params.put("pageIndex", 1);
        params.put("pageSize", 1);
        params.put("secret", "TWSYFgYJOQWB4ftgeYut8DW4wbs7pQnj");
        String queryParams = urlEncode(params);
        String httpUrl ="http://192.168.10.133:9092/index/api/getMediaList";
        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) { //请求成功
            Response responseHttp = HttpUtil.getGson().fromJson(responseJson, Response.class);
            if(responseHttp.getCode() == 1) {
                //TODO 需要实际调试,目前返回的数据结构未确定
                return HttpUtil.getGson().fromJson(responseHttp.getData().toString(), List.class);
            }else{
                return null;
            Response4Http responseHttp = HttpUtil.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 null;
        }else{ //请求失败
            return responseResult.set(0,response.getData(),response.getMsg());
        }
    }
    public void startRecord(String streamId){
    /**
     * 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);
        String queryParams = urlEncode(params);
        String httpUrl ="http://192.168.10.133:9092/index/api/startRecord";
        Response response = HttpUtil.doGet(httpUrl, queryParams);
        Integer code = response.getCode();
        String responseJson = (String) response.getData();
        if(code == 1) { //请求成功
            Response responseHttp = HttpUtil.getGson().fromJson(responseJson, Response.class);
            if(responseHttp.getCode() == 0 && responseHttp.getMsg().equals("success")) { //请求成功
            }else{
                System.out.println(responseHttp.getMsg());
            }
        }
        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 void stopRecord(String streamId){
    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 queryParams = urlEncode(params);
        String httpUrl ="http://192.168.10.133:9092/index/api/stopRecord";
        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) { //请求成功
            Response responseHttp = HttpUtil.getGson().fromJson(responseJson, Response.class);
            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 = HttpUtil.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);
    }
}