ZLMMediaNodeServerService.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. package com.genersoft.iot.vmp.media.zlm;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONArray;
  4. import com.alibaba.fastjson2.JSONObject;
  5. import com.genersoft.iot.vmp.common.CommonCallback;
  6. import com.genersoft.iot.vmp.common.StreamInfo;
  7. import com.genersoft.iot.vmp.conf.UserSetting;
  8. import com.genersoft.iot.vmp.conf.exception.ControllerException;
  9. import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
  10. import com.genersoft.iot.vmp.media.bean.MediaInfo;
  11. import com.genersoft.iot.vmp.media.bean.MediaServer;
  12. import com.genersoft.iot.vmp.media.service.IMediaNodeServerService;
  13. import com.genersoft.iot.vmp.media.zlm.dto.ZLMServerConfig;
  14. import com.genersoft.iot.vmp.streamProxy.bean.StreamProxy;
  15. import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
  16. import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.util.ObjectUtils;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. @Slf4j
  26. @Service("zlm")
  27. public class ZLMMediaNodeServerService implements IMediaNodeServerService {
  28. @Autowired
  29. private ZLMRESTfulUtils zlmresTfulUtils;
  30. @Autowired
  31. private ZLMServerFactory zlmServerFactory;
  32. @Autowired
  33. private UserSetting userSetting;
  34. @Override
  35. public int createRTPServer(MediaServer mediaServer, String streamId, long ssrc, Integer port, Boolean onlyAuto, Boolean disableAudio, Boolean reUsePort, Integer tcpMode) {
  36. return zlmServerFactory.createRTPServer(mediaServer, streamId, ssrc, port, onlyAuto, reUsePort, tcpMode);
  37. }
  38. @Override
  39. public void closeRtpServer(MediaServer mediaServer, String streamId) {
  40. zlmServerFactory.closeRtpServer(mediaServer, streamId);
  41. }
  42. @Override
  43. public void closeRtpServer(MediaServer mediaServer, String streamId, CommonCallback<Boolean> callback) {
  44. zlmServerFactory.closeRtpServer(mediaServer, streamId, callback);
  45. }
  46. @Override
  47. public void closeStreams(MediaServer mediaServer, String app, String stream) {
  48. zlmresTfulUtils.closeStreams(mediaServer, app, stream);
  49. }
  50. @Override
  51. public Boolean updateRtpServerSSRC(MediaServer mediaServer, String streamId, String ssrc) {
  52. return zlmServerFactory.updateRtpServerSSRC(mediaServer, streamId, ssrc);
  53. }
  54. @Override
  55. public boolean checkNodeId(MediaServer mediaServer) {
  56. if (mediaServer == null) {
  57. return false;
  58. }
  59. JSONObject responseJSON = zlmresTfulUtils.getMediaServerConfig(mediaServer);
  60. if (responseJSON != null) {
  61. JSONArray data = responseJSON.getJSONArray("data");
  62. if (data != null && !data.isEmpty()) {
  63. ZLMServerConfig zlmServerConfig= JSON.parseObject(JSON.toJSONString(data.get(0)), ZLMServerConfig.class);
  64. return zlmServerConfig.getGeneralMediaServerId().equals(mediaServer.getId());
  65. }else {
  66. return false;
  67. }
  68. }else {
  69. return false;
  70. }
  71. }
  72. @Override
  73. public void online(MediaServer mediaServer) {
  74. }
  75. @Override
  76. public MediaServer checkMediaServer(String ip, int port, String secret) {
  77. MediaServer mediaServer = new MediaServer();
  78. mediaServer.setIp(ip);
  79. mediaServer.setHttpPort(port);
  80. mediaServer.setFlvPort(port);
  81. mediaServer.setWsFlvPort(port);
  82. mediaServer.setSecret(secret);
  83. JSONObject responseJSON = zlmresTfulUtils.getMediaServerConfig(mediaServer);
  84. if (responseJSON == null) {
  85. throw new ControllerException(ErrorCode.ERROR100.getCode(), "连接失败");
  86. }
  87. JSONArray data = responseJSON.getJSONArray("data");
  88. if (data == null) {
  89. throw new ControllerException(ErrorCode.ERROR100.getCode(), "读取配置失败");
  90. }
  91. ZLMServerConfig zlmServerConfig = JSON.parseObject(JSON.toJSONString(data.get(0)), ZLMServerConfig.class);
  92. if (zlmServerConfig == null) {
  93. throw new ControllerException(ErrorCode.ERROR100.getCode(), "读取配置失败");
  94. }
  95. mediaServer.setId(zlmServerConfig.getGeneralMediaServerId());
  96. mediaServer.setHttpSSlPort(zlmServerConfig.getHttpPort());
  97. mediaServer.setFlvSSLPort(zlmServerConfig.getHttpPort());
  98. mediaServer.setWsFlvSSLPort(zlmServerConfig.getHttpPort());
  99. mediaServer.setRtmpPort(zlmServerConfig.getRtmpPort());
  100. mediaServer.setRtmpSSlPort(zlmServerConfig.getRtmpSslPort());
  101. mediaServer.setRtspPort(zlmServerConfig.getRtspPort());
  102. mediaServer.setRtspSSLPort(zlmServerConfig.getRtspSSlport());
  103. mediaServer.setRtpProxyPort(zlmServerConfig.getRtpProxyPort());
  104. mediaServer.setStreamIp(ip);
  105. mediaServer.setHookIp("127.0.0.1");
  106. mediaServer.setSdpIp(ip);
  107. mediaServer.setType("zlm");
  108. return mediaServer;
  109. }
  110. @Override
  111. public boolean stopSendRtp(MediaServer mediaInfo, String app, String stream, String ssrc) {
  112. Map<String, Object> param = new HashMap<>();
  113. param.put("vhost", "__defaultVhost__");
  114. param.put("app", app);
  115. param.put("stream", stream);
  116. if (!ObjectUtils.isEmpty(ssrc)) {
  117. param.put("ssrc", ssrc);
  118. }
  119. JSONObject jsonObject = zlmresTfulUtils.stopSendRtp(mediaInfo, param);
  120. if (jsonObject == null || jsonObject.getInteger("code") != 0 ) {
  121. log.error("停止发流失败: {}, 参数:{}", jsonObject.getString("msg"), JSON.toJSONString(param));
  122. throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg"));
  123. }
  124. return true;
  125. }
  126. @Override
  127. public boolean initStopSendRtp(MediaServer mediaInfo, String app, String stream, String ssrc) {
  128. Map<String, Object> param = new HashMap<>();
  129. param.put("vhost", "__defaultVhost__");
  130. param.put("app", app);
  131. param.put("stream", stream);
  132. if (!ObjectUtils.isEmpty(ssrc)) {
  133. param.put("ssrc", ssrc);
  134. }
  135. JSONObject jsonObject = zlmresTfulUtils.stopSendRtp(mediaInfo, param);
  136. if (jsonObject == null || jsonObject.getInteger("code") != 0 ) {
  137. log.error("停止发流失败: {}, 参数:{}", jsonObject.getString("msg"), JSON.toJSONString(param));
  138. return false;
  139. }
  140. return true;
  141. }
  142. @Override
  143. public boolean deleteRecordDirectory(MediaServer mediaServer, String app, String stream, String date, String fileName) {
  144. log.info("[zlm-deleteRecordDirectory] 删除磁盘文件, server: {} {}:{}->{}/{}", mediaServer.getId(), app, stream, date, fileName);
  145. JSONObject jsonObject = zlmresTfulUtils.deleteRecordDirectory(mediaServer, app,
  146. stream, date, fileName);
  147. if (jsonObject.getInteger("code") == 0) {
  148. return true;
  149. }else {
  150. log.info("[zlm-deleteRecordDirectory] 删除磁盘文件错误, server: {} {}:{}->{}/{}, 结果: {}", mediaServer.getId(), app, stream, date, fileName, jsonObject);
  151. return false;
  152. }
  153. }
  154. @Override
  155. public List<StreamInfo> getMediaList(MediaServer mediaServer, String app, String stream, String callId) {
  156. List<StreamInfo> streamInfoList = new ArrayList<>();
  157. JSONObject mediaList = zlmresTfulUtils.getMediaList(mediaServer, app, stream);
  158. if (mediaList != null) {
  159. if (mediaList.getInteger("code") == 0) {
  160. JSONArray data = mediaList.getJSONArray("data");
  161. if (data == null) {
  162. return null;
  163. }
  164. JSONObject mediaJSON = data.getJSONObject(0);
  165. MediaInfo mediaInfo = MediaInfo.getInstance(mediaJSON, mediaServer, userSetting.getServerId());
  166. StreamInfo streamInfo = getStreamInfoByAppAndStream(mediaServer, app, stream, mediaInfo, callId, true);
  167. if (streamInfo != null) {
  168. streamInfoList.add(streamInfo);
  169. }
  170. }
  171. }
  172. return streamInfoList;
  173. }
  174. public StreamInfo getStreamInfoByAppAndStream(MediaServer mediaServer, String app, String stream, MediaInfo mediaInfo, String callId, boolean isPlay) {
  175. StreamInfo streamInfoResult = new StreamInfo();
  176. streamInfoResult.setStream(stream);
  177. streamInfoResult.setApp(app);
  178. String addr = mediaServer.getStreamIp();
  179. streamInfoResult.setIp(addr);
  180. streamInfoResult.setMediaServerId(mediaServer.getId());
  181. String callIdParam = ObjectUtils.isEmpty(callId)?"":"?callId=" + callId;
  182. streamInfoResult.setRtmp(addr, mediaServer.getRtmpPort(),mediaServer.getRtmpSSlPort(), app, stream, callIdParam);
  183. streamInfoResult.setRtsp(addr, mediaServer.getRtspPort(),mediaServer.getRtspSSLPort(), app, stream, callIdParam);
  184. String flvFile = String.format("%s/%s.live.flv%s", app, stream, callIdParam);
  185. streamInfoResult.setFlv(addr, mediaServer.getHttpPort(),mediaServer.getHttpSSlPort(), flvFile);
  186. streamInfoResult.setWsFlv(addr, mediaServer.getHttpPort(),mediaServer.getHttpSSlPort(), flvFile);
  187. streamInfoResult.setFmp4(addr, mediaServer.getHttpPort(),mediaServer.getHttpSSlPort(), app, stream, callIdParam);
  188. streamInfoResult.setHls(addr, mediaServer.getHttpPort(),mediaServer.getHttpSSlPort(), app, stream, callIdParam);
  189. streamInfoResult.setTs(addr, mediaServer.getHttpPort(),mediaServer.getHttpSSlPort(), app, stream, callIdParam);
  190. streamInfoResult.setRtc(addr, mediaServer.getHttpPort(),mediaServer.getHttpSSlPort(), app, stream, callIdParam, isPlay);
  191. streamInfoResult.setMediaInfo(mediaInfo);
  192. if (mediaInfo != null) {
  193. streamInfoResult.setOriginType(mediaInfo.getOriginType());
  194. }
  195. return streamInfoResult;
  196. }
  197. @Override
  198. public Boolean connectRtpServer(MediaServer mediaServer, String address, int port, String stream) {
  199. JSONObject jsonObject = zlmresTfulUtils.connectRtpServer(mediaServer, address, port, stream);
  200. log.info("[TCP主动连接对方] 结果: {}", jsonObject);
  201. return jsonObject.getInteger("code") == 0;
  202. }
  203. @Override
  204. public void getSnap(MediaServer mediaServer, String streamUrl, int timeoutSec, int expireSec, String path, String fileName) {
  205. zlmresTfulUtils.getSnap(mediaServer, streamUrl, timeoutSec, expireSec, path, fileName);
  206. }
  207. @Override
  208. public MediaInfo getMediaInfo(MediaServer mediaServer, String app, String stream) {
  209. JSONObject jsonObject = zlmresTfulUtils.getMediaInfo(mediaServer, app, "rtsp", stream);
  210. if (jsonObject.getInteger("code") != 0) {
  211. return null;
  212. }
  213. return MediaInfo.getInstance(jsonObject, mediaServer, userSetting.getServerId());
  214. }
  215. @Override
  216. public Boolean pauseRtpCheck(MediaServer mediaServer, String streamKey) {
  217. JSONObject jsonObject = zlmresTfulUtils.pauseRtpCheck(mediaServer, streamKey);
  218. return jsonObject.getInteger("code") == 0;
  219. }
  220. @Override
  221. public Boolean resumeRtpCheck(MediaServer mediaServer, String streamKey) {
  222. JSONObject jsonObject = zlmresTfulUtils.resumeRtpCheck(mediaServer, streamKey);
  223. return jsonObject.getInteger("code") == 0;
  224. }
  225. @Override
  226. public String getFfmpegCmd(MediaServer mediaServer, String cmdKey) {
  227. JSONObject jsonObject = zlmresTfulUtils.getMediaServerConfig(mediaServer);
  228. if (jsonObject.getInteger("code") != 0) {
  229. log.warn("[getFfmpegCmd] 获取流媒体配置失败");
  230. throw new ControllerException(ErrorCode.ERROR100.getCode(), "获取流媒体配置失败");
  231. }
  232. JSONArray dataArray = jsonObject.getJSONArray("data");
  233. JSONObject mediaServerConfig = dataArray.getJSONObject(0);
  234. if (ObjectUtils.isEmpty(cmdKey)) {
  235. cmdKey = "ffmpeg.cmd";
  236. }
  237. return mediaServerConfig.getString(cmdKey);
  238. }
  239. @Override
  240. public WVPResult<String> addFFmpegSource(MediaServer mediaServer, String srcUrl, String dstUrl, int timeoutMs, boolean enableAudio, boolean enableMp4, String ffmpegCmdKey) {
  241. JSONObject jsonObject = zlmresTfulUtils.addFFmpegSource(mediaServer, srcUrl, dstUrl, timeoutMs, enableAudio, enableMp4, ffmpegCmdKey);
  242. if (jsonObject.getInteger("code") != 0) {
  243. log.warn("[getFfmpegCmd] 添加FFMPEG代理失败");
  244. return WVPResult.fail(ErrorCode.ERROR100.getCode(), "添加FFMPEG代理失败");
  245. }else {
  246. JSONObject data = jsonObject.getJSONObject("data");
  247. if (data == null) {
  248. return WVPResult.fail(ErrorCode.ERROR100.getCode(), "代理结果异常: " + jsonObject);
  249. }else {
  250. return WVPResult.success(data.getString("key"));
  251. }
  252. }
  253. }
  254. @Override
  255. public WVPResult<String> addStreamProxy(MediaServer mediaServer, String app, String stream, String url,
  256. boolean enableAudio, boolean enableMp4, String rtpType, Integer timeout) {
  257. JSONObject jsonObject = zlmresTfulUtils.addStreamProxy(mediaServer, app, stream, url, enableAudio, enableMp4, rtpType, timeout);
  258. if (jsonObject.getInteger("code") != 0) {
  259. return WVPResult.fail(ErrorCode.ERROR100.getCode(), "添加代理失败");
  260. }else {
  261. JSONObject data = jsonObject.getJSONObject("data");
  262. if (data == null) {
  263. return WVPResult.fail(ErrorCode.ERROR100.getCode(), "代理结果异常: " + jsonObject);
  264. }else {
  265. return WVPResult.success(data.getString("key"));
  266. }
  267. }
  268. }
  269. @Override
  270. public Boolean delFFmpegSource(MediaServer mediaServer, String streamKey) {
  271. JSONObject jsonObject = zlmresTfulUtils.delFFmpegSource(mediaServer, streamKey);
  272. return jsonObject.getInteger("code") == 0;
  273. }
  274. @Override
  275. public Boolean delStreamProxy(MediaServer mediaServer, String streamKey) {
  276. JSONObject jsonObject = zlmresTfulUtils.delStreamProxy(mediaServer, streamKey);
  277. return jsonObject.getInteger("code") == 0;
  278. }
  279. @Override
  280. public Map<String, String> getFFmpegCMDs(MediaServer mediaServer) {
  281. Map<String, String> result = new HashMap<>();
  282. JSONObject mediaServerConfigResuly = zlmresTfulUtils.getMediaServerConfig(mediaServer);
  283. if (mediaServerConfigResuly != null && mediaServerConfigResuly.getInteger("code") == 0
  284. && mediaServerConfigResuly.getJSONArray("data").size() > 0){
  285. JSONObject mediaServerConfig = mediaServerConfigResuly.getJSONArray("data").getJSONObject(0);
  286. for (String key : mediaServerConfig.keySet()) {
  287. if (key.startsWith("ffmpeg.cmd")){
  288. result.put(key, mediaServerConfig.getString(key));
  289. }
  290. }
  291. }
  292. return result;
  293. }
  294. @Override
  295. public void startSendRtpPassive(MediaServer mediaServer, SendRtpItem sendRtpItem, Integer timeout) {
  296. Map<String, Object> param = new HashMap<>(12);
  297. param.put("vhost","__defaultVhost__");
  298. param.put("app", sendRtpItem.getApp());
  299. param.put("stream", sendRtpItem.getStream());
  300. param.put("ssrc", sendRtpItem.getSsrc());
  301. param.put("src_port", sendRtpItem.getLocalPort());
  302. param.put("pt", sendRtpItem.getPt());
  303. param.put("use_ps", sendRtpItem.isUsePs() ? "1" : "0");
  304. param.put("only_audio", sendRtpItem.isOnlyAudio() ? "1" : "0");
  305. param.put("is_udp", sendRtpItem.isTcp() ? "0" : "1");
  306. param.put("recv_stream_id", sendRtpItem.getReceiveStream());
  307. if (timeout != null) {
  308. param.put("close_delay_ms", timeout);
  309. }
  310. if (!sendRtpItem.isTcp()) {
  311. // 开启rtcp保活
  312. param.put("udp_rtcp_timeout", sendRtpItem.isRtcp()? "1":"0");
  313. }
  314. if (!sendRtpItem.isTcpActive()) {
  315. param.put("dst_url",sendRtpItem.getIp());
  316. param.put("dst_port", sendRtpItem.getPort());
  317. }
  318. JSONObject jsonObject = zlmServerFactory.startSendRtpPassive(mediaServer, param, null);
  319. if (jsonObject == null || jsonObject.getInteger("code") != 0 ) {
  320. log.error("启动监听TCP被动推流失败: {}, 参数:{}", jsonObject.getString("msg"), JSON.toJSONString(param));
  321. throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg"));
  322. }
  323. log.info("调用ZLM-TCP被动推流接口, 结果: {}", jsonObject);
  324. log.info("启动监听TCP被动推流成功[ {}/{} ],{}->{}:{}, " , sendRtpItem.getApp(), sendRtpItem.getStream(),
  325. jsonObject.getString("local_port"), param.get("dst_url"), param.get("dst_port"));
  326. }
  327. @Override
  328. public void startSendRtpStream(MediaServer mediaServer, SendRtpItem sendRtpItem) {
  329. Map<String, Object> param = new HashMap<>(12);
  330. param.put("vhost", "__defaultVhost__");
  331. param.put("app", sendRtpItem.getApp());
  332. param.put("stream", sendRtpItem.getStream());
  333. param.put("ssrc", sendRtpItem.getSsrc());
  334. param.put("src_port", sendRtpItem.getLocalPort());
  335. param.put("pt", sendRtpItem.getPt());
  336. param.put("use_ps", sendRtpItem.isUsePs() ? "1" : "0");
  337. param.put("only_audio", sendRtpItem.isOnlyAudio() ? "1" : "0");
  338. param.put("is_udp", sendRtpItem.isTcp() ? "0" : "1");
  339. if (!sendRtpItem.isTcp()) {
  340. // udp模式下开启rtcp保活
  341. param.put("udp_rtcp_timeout", sendRtpItem.isRtcp() ? "1" : "0");
  342. }
  343. param.put("dst_url", sendRtpItem.getIp());
  344. param.put("dst_port", sendRtpItem.getPort());
  345. JSONObject jsonObject = zlmresTfulUtils.startSendRtp(mediaServer, param);
  346. if (jsonObject == null || jsonObject.getInteger("code") != 0 ) {
  347. throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg"));
  348. }
  349. }
  350. @Override
  351. public Long updateDownloadProcess(MediaServer mediaServer, String app, String stream) {
  352. MediaInfo mediaInfo = getMediaInfo(mediaServer, app, stream);
  353. if (mediaInfo == null) {
  354. log.warn("[获取下载进度] 查询进度失败, 节点Id: {}, {}/{}", mediaServer.getId(), app, stream);
  355. return null;
  356. }
  357. return mediaInfo.getDuration();
  358. }
  359. @Override
  360. public StreamInfo startProxy(MediaServer mediaServer, StreamProxy streamProxy) {
  361. String dstUrl;
  362. if ("ffmpeg".equalsIgnoreCase(streamProxy.getType())) {
  363. String ffmpegCmd = getFfmpegCmd(mediaServer, streamProxy.getFfmpegCmdKey());
  364. if (ffmpegCmd == null) {
  365. throw new ControllerException(ErrorCode.ERROR100.getCode(), "ffmpeg拉流代理无法获取ffmpeg cmd");
  366. }
  367. String schema = getSchemaFromFFmpegCmd(ffmpegCmd);
  368. if (schema == null) {
  369. throw new ControllerException(ErrorCode.ERROR100.getCode(), "ffmpeg拉流代理无法从ffmpeg cmd中获取到输出格式");
  370. }
  371. int port;
  372. String schemaForUri;
  373. if (schema.equalsIgnoreCase("rtsp")) {
  374. port = mediaServer.getRtspPort();
  375. schemaForUri = schema;
  376. }else if (schema.equalsIgnoreCase("flv")) {
  377. port = mediaServer.getRtmpPort();
  378. schemaForUri = schema;
  379. }else {
  380. port = mediaServer.getRtmpPort();
  381. schemaForUri = schema;
  382. }
  383. dstUrl = String.format("%s://%s:%s/%s/%s", schemaForUri, "127.0.0.1", port, streamProxy.getApp(),
  384. streamProxy.getStream());
  385. }else {
  386. dstUrl = String.format("rtsp://%s:%s/%s/%s", "127.0.0.1", mediaServer.getRtspPort(), streamProxy.getApp(),
  387. streamProxy.getStream());
  388. }
  389. MediaInfo mediaInfo = getMediaInfo(mediaServer, streamProxy.getApp(), streamProxy.getStream());
  390. if (mediaInfo != null) {
  391. if (mediaInfo.getOriginUrl().equals(streamProxy.getSrcUrl())) {
  392. log.info("[启动拉流代理] 已存在, 直接返回, app: {}, stream: {}", mediaInfo.getApp(), streamProxy.getStream());
  393. return getStreamInfoByAppAndStream(mediaServer, streamProxy.getApp(), streamProxy.getStream(), null, null, true);
  394. }
  395. closeStreams(mediaServer, streamProxy.getApp(), streamProxy.getStream());
  396. }
  397. JSONObject jsonObject = null;
  398. if ("ffmpeg".equalsIgnoreCase(streamProxy.getType())){
  399. if (streamProxy.getTimeout() == 0) {
  400. streamProxy.setTimeout(15);
  401. }
  402. jsonObject = zlmresTfulUtils.addFFmpegSource(mediaServer, streamProxy.getSrcUrl().trim(), dstUrl,
  403. streamProxy.getTimeout(), streamProxy.isEnableAudio(), streamProxy.isEnableMp4(),
  404. streamProxy.getFfmpegCmdKey());
  405. }else {
  406. jsonObject = zlmresTfulUtils.addStreamProxy(mediaServer, streamProxy.getApp(), streamProxy.getStream(), streamProxy.getSrcUrl().trim(),
  407. streamProxy.isEnableAudio(), streamProxy.isEnableMp4(), streamProxy.getRtspType(), streamProxy.getTimeout());
  408. }
  409. if (jsonObject == null) {
  410. throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败");
  411. }else if (jsonObject.getInteger("code") != 0) {
  412. throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg"));
  413. }else {
  414. JSONObject data = jsonObject.getJSONObject("data");
  415. if (data == null) {
  416. throw new ControllerException(jsonObject.getInteger("code"), "代理结果异常: " + jsonObject);
  417. }else {
  418. streamProxy.setStreamKey(jsonObject.getString("key"));
  419. return getStreamInfoByAppAndStream(mediaServer, streamProxy.getApp(), streamProxy.getStream(), null, null, true);
  420. }
  421. }
  422. }
  423. private String getSchemaFromFFmpegCmd(String ffmpegCmd) {
  424. ffmpegCmd = ffmpegCmd.replaceAll(" + ", " ");
  425. String[] paramArray = ffmpegCmd.split(" ");
  426. if (paramArray.length == 0) {
  427. return null;
  428. }
  429. for (int i = 0; i < paramArray.length; i++) {
  430. if (paramArray[i].equalsIgnoreCase("-f")) {
  431. if (i + 1 < paramArray.length - 1) {
  432. return paramArray[i+1];
  433. }else {
  434. return null;
  435. }
  436. }
  437. }
  438. return null;
  439. }
  440. @Override
  441. public void stopProxy(MediaServer mediaServer, String streamKey) {
  442. JSONObject jsonObject = zlmresTfulUtils.delStreamProxy(mediaServer, streamKey);
  443. if (jsonObject == null) {
  444. throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败");
  445. }else if (jsonObject.getInteger("code") != 0) {
  446. throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg"));
  447. }
  448. }
  449. }