| | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; |
| | | import okhttp3.*; |
| | | import okhttp3.logging.HttpLoggingInterceptor; |
| | | import org.jetbrains.annotations.NotNull; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | |
| | | void run(JSONObject response); |
| | | } |
| | | |
| | | private OkHttpClient getClient(){ |
| | | OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); |
| | | if (logger.isDebugEnabled()) { |
| | | HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> { |
| | | logger.debug("http请求参数:" + message); |
| | | }); |
| | | logging.setLevel(HttpLoggingInterceptor.Level.BASIC); |
| | | // OkHttp進行添加攔截器loggingInterceptor |
| | | httpClientBuilder.addInterceptor(logging); |
| | | } |
| | | return httpClientBuilder.build(); |
| | | } |
| | | |
| | | |
| | | public JSONObject sendPost(MediaServerItem mediaServerItem, String api, Map<String, Object> param, RequestCallback callback) { |
| | | OkHttpClient client = new OkHttpClient(); |
| | | OkHttpClient client = getClient(); |
| | | |
| | | if (mediaServerItem == null) { |
| | | return null; |
| | | } |
| | | String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api); |
| | | JSONObject responseJSON = null; |
| | | logger.debug(url); |
| | | |
| | | FormBody.Builder builder = new FormBody.Builder(); |
| | | builder.add("secret",mediaServerItem.getSecret()); |
| | |
| | | try { |
| | | Response response = client.newCall(request).execute(); |
| | | if (response.isSuccessful()) { |
| | | String responseStr = response.body().string(); |
| | | if (responseStr != null) { |
| | | ResponseBody responseBody = response.body(); |
| | | if (responseBody != null) { |
| | | String responseStr = responseBody.string(); |
| | | responseJSON = JSON.parseObject(responseStr); |
| | | } |
| | | }else { |
| | | response.close(); |
| | | Objects.requireNonNull(response.body()).close(); |
| | | } |
| | | } catch (ConnectException e) { |
| | | logger.error(String.format("连接ZLM失败: %s, %s", e.getCause().getMessage(), e.getMessage())); |
| | |
| | | } catch (IOException e) { |
| | | logger.error(String.format("[ %s ]请求失败: %s", url, e.getMessage())); |
| | | } |
| | | |
| | | }else { |
| | | response.close(); |
| | | Objects.requireNonNull(response.body()).close(); |
| | | } |
| | | } |
| | | |
| | |
| | | public void sendGetForImg(MediaServerItem mediaServerItem, String api, Map<String, Object> params, String targetPath, String fileName) { |
| | | String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api); |
| | | logger.debug(url); |
| | | HttpUrl.Builder httpBuilder = HttpUrl.parse(url).newBuilder(); |
| | | HttpUrl parseUrl = HttpUrl.parse(url); |
| | | if (parseUrl == null) { |
| | | return; |
| | | } |
| | | HttpUrl.Builder httpBuilder = parseUrl.newBuilder(); |
| | | |
| | | httpBuilder.addQueryParameter("secret", mediaServerItem.getSecret()); |
| | | if (params != null) { |
| | |
| | | if (targetPath != null) { |
| | | File snapFolder = new File(targetPath); |
| | | if (!snapFolder.exists()) { |
| | | snapFolder.mkdirs(); |
| | | if (!snapFolder.mkdirs()) { |
| | | logger.warn("{}路径创建失败", snapFolder.getAbsolutePath()); |
| | | } |
| | | |
| | | } |
| | | File snapFile = new File(targetPath + "/" + fileName); |
| | | FileOutputStream outStream = new FileOutputStream(snapFile); |
| | | outStream.write(response.body().bytes()); |
| | | |
| | | outStream.write(Objects.requireNonNull(response.body()).bytes()); |
| | | outStream.close(); |
| | | } else { |
| | | logger.error(String.format("[ %s ]请求失败: %s %s", url, response.code(), response.message())); |
| | | } |
| | | response.body().close(); |
| | | Objects.requireNonNull(response.body()).close(); |
| | | } else { |
| | | logger.error(String.format("[ %s ]请求失败: %s %s", url, response.code(), response.message())); |
| | | } |
| | |
| | | return sendPost(mediaServerItem, "stopSendRtp",param, null); |
| | | } |
| | | |
| | | public JSONObject restartServer(MediaServerItem mediaServerItem) { |
| | | return sendPost(mediaServerItem, "restartServer",null, null); |
| | | } |
| | | |
| | | public JSONObject addStreamProxy(MediaServerItem mediaServerItem, String app, String stream, String url, boolean enable_hls, boolean enable_mp4, String rtp_type) { |
| | | Map<String, Object> param = new HashMap<>(); |
| | | param.put("vhost", "__defaultVhost__"); |
| | |
| | | param.put("url", url); |
| | | param.put("enable_hls", enable_hls?1:0); |
| | | param.put("enable_mp4", enable_mp4?1:0); |
| | | param.put("enable_rtmp", 1); |
| | | param.put("enable_fmp4", 1); |
| | | param.put("enable_audio", 1); |
| | | param.put("enable_rtsp", 1); |
| | | param.put("add_mute_audio", 1); |
| | | param.put("rtp_type", rtp_type); |
| | | return sendPost(mediaServerItem, "addStreamProxy",param, null); |
| | | } |