|
|
@@ -25,59 +25,156 @@ public class RtmpLiveService { |
|
|
|
/** |
|
|
|
* 加载配置文件的推拉流地址 |
|
|
|
*/ |
|
|
|
public static final CmdParam cmdParam=load("/loadCmd.properties", CmdParam.class); |
|
|
|
public static final CmdParam cmdParam = load("/loadCmd.properties", CmdParam.class); |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private MqttProviderConfig mqttProviderConfig; |
|
|
|
|
|
|
|
public static final CommandManager manager = new CommandManagerImpl(); |
|
|
|
|
|
|
|
/** |
|
|
|
* h获取通道并推送 |
|
|
|
*/ |
|
|
|
public void pushServer(String code){ |
|
|
|
public void pushServer(String code) throws InterruptedException { |
|
|
|
//CommandManager manager = new CommandManagerImpl(); |
|
|
|
String reTopic = cmdParam.getMqttTopic().replace("live","result"); |
|
|
|
String reTopic = cmdParam.getMqttTopic().replace("live", "result"); |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
Collection<CommandTasker> infoList = manager.queryAll(); |
|
|
|
log.info(infoList.toString()); |
|
|
|
if (infoList.size()>0){ |
|
|
|
jsonObject.put("code", -1); |
|
|
|
if (infoList.size() > 0) { |
|
|
|
//manager.stopAll(); |
|
|
|
//manager.destory(); |
|
|
|
//Thread.sleep(3000); |
|
|
|
/*jsonObject.put("code", -1); |
|
|
|
jsonObject.put("msg", "推流服务已启动,请勿重复启动"); //推流失败 |
|
|
|
mqttProviderConfig.publish(2,false,reTopic,jsonObject.toJSONString()); |
|
|
|
mqttProviderConfig.publish(2,false,reTopic,jsonObject.toJSONString());*/ |
|
|
|
log.info("任务运行中"); |
|
|
|
return; |
|
|
|
} |
|
|
|
String pushUrl=""; |
|
|
|
if (cmdParam.getType()==2){ |
|
|
|
String pushUrl = ""; |
|
|
|
if (cmdParam.getType() == 2) { |
|
|
|
log.info("获取流媒体通道"); |
|
|
|
//TODO 获取通道服务推拉流地址 |
|
|
|
JSONObject object = getChenl(); |
|
|
|
if (!ObjectUtils.isEmpty(object)){ |
|
|
|
if (!ObjectUtils.isEmpty(object)) { |
|
|
|
code = object.get("code").toString(); |
|
|
|
object.put("code", 0); |
|
|
|
object.put("msg", "获取通道成功"); |
|
|
|
mqttProviderConfig.publish(2,false,reTopic,object.toJSONString()); |
|
|
|
}else{ |
|
|
|
mqttProviderConfig.publish(2, false, reTopic, object.toJSONString()); |
|
|
|
} else { |
|
|
|
log.info("获取通道失败"); |
|
|
|
object.put("code", -1); |
|
|
|
object.put("msg", "获取通道失败"); |
|
|
|
mqttProviderConfig.publish(2,false,reTopic,object.toJSONString()); |
|
|
|
mqttProviderConfig.publish(2, false, reTopic, object.toJSONString()); |
|
|
|
return; |
|
|
|
} |
|
|
|
runRtmp(pushUrl, cmdParam.getPlayUrl(), code); |
|
|
|
//TODO end |
|
|
|
}else{ |
|
|
|
} else if (cmdParam.getType() == 1) { |
|
|
|
log.info("使用固定流媒体通道"); |
|
|
|
pushUrl= cmdParam.getPushUrl(); |
|
|
|
pushUrl = cmdParam.getPushUrl(); |
|
|
|
runRtmp(pushUrl, cmdParam.getPlayUrl(), code); |
|
|
|
} else if (cmdParam.getType() == 3) { |
|
|
|
runRtmp2to2(cmdParam.getPlayUrl()); |
|
|
|
} else if (cmdParam.getType() == 4) { |
|
|
|
runRtmp1to2(cmdParam.getPlayUrl()); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private void runRtmp2to2(String playUrl) throws InterruptedException { |
|
|
|
try { |
|
|
|
String reTopic = cmdParam.getMqttTopic().replace("live", "result"); |
|
|
|
String taskId = manager.start("test1", CommandBuidlerFactory.createBuidler() |
|
|
|
.add("ffmpeg").add("-i", playUrl) |
|
|
|
.add("-rtsp_transport", "tcp") |
|
|
|
.add("-vcodec", "copy") |
|
|
|
.add("-acodec", "copy") |
|
|
|
.add("-f", "flv") |
|
|
|
.add("-b:v", "2M") |
|
|
|
.add("-maxrate", "2M") |
|
|
|
.add("-bufsize", "1M") |
|
|
|
.add("-y").add(cmdParam.getPushUrl())); |
|
|
|
Thread.sleep(3000); |
|
|
|
String taskId2 = manager.start("test2", CommandBuidlerFactory.createBuidler() |
|
|
|
.add("ffmpeg").add("-i", playUrl) |
|
|
|
.add("-rtsp_transport", "tcp") |
|
|
|
.add("-vcodec", "copy") |
|
|
|
.add("-acodec", "copy") |
|
|
|
.add("-f", "flv") |
|
|
|
.add("-b:v", "2M") |
|
|
|
.add("-maxrate", "2M") |
|
|
|
.add("-bufsize", "1M") |
|
|
|
.add("-y").add(cmdParam.getPushUrl2())); |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
if (StringUtils.isNullOrEmpty(taskId) && StringUtils.isNullOrEmpty(taskId2)) { |
|
|
|
jsonObject.put("code", -1); |
|
|
|
jsonObject.put("msg", "推流服务失败"); //推流失败 |
|
|
|
mqttProviderConfig.publish(2, false, reTopic, jsonObject.toJSONString()); |
|
|
|
// 停止全部任务 |
|
|
|
manager.stopAll(); |
|
|
|
return; |
|
|
|
} else { |
|
|
|
jsonObject.put("code", 0); |
|
|
|
jsonObject.put("msg", "推流服务启动成功"); |
|
|
|
mqttProviderConfig.publish(2, false, reTopic, jsonObject.toJSONString()); |
|
|
|
} |
|
|
|
Thread.sleep(cmdParam.getTime()); |
|
|
|
log.info(manager.queryAll().toString()); |
|
|
|
// 停止全部任务 |
|
|
|
manager.stopAll(); |
|
|
|
manager.destory(); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
runRtmp(pushUrl,cmdParam.getPlayUrl(),code); |
|
|
|
} |
|
|
|
|
|
|
|
private void runRtmp1to2(String playUrl) { |
|
|
|
try { |
|
|
|
//单进程 多路 |
|
|
|
String reTopic = cmdParam.getMqttTopic().replace("live", "result"); |
|
|
|
String taskId = manager.start("test4", CommandBuidlerFactory.createBuidler() |
|
|
|
.add("ffmpeg").add("-i", playUrl) |
|
|
|
.add("-rtsp_transport", "tcp") |
|
|
|
.add("-vcodec", "copy") |
|
|
|
.add("-acodec", "copy") |
|
|
|
.add("-b:v", "2M") |
|
|
|
.add("-maxrate", "2M") |
|
|
|
.add("-bufsize", "1M") |
|
|
|
//多路推流测试 |
|
|
|
.add("-map", "0") |
|
|
|
.add("-f").add(cmdParam.getPushUrlMap())); |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
if (StringUtils.isNullOrEmpty(taskId)) { |
|
|
|
jsonObject.put("code", -1); |
|
|
|
jsonObject.put("msg", "推流服务失败"); //推流失败 |
|
|
|
mqttProviderConfig.publish(2, false, reTopic, jsonObject.toJSONString()); |
|
|
|
// 停止全部任务 |
|
|
|
manager.stopAll(); |
|
|
|
return; |
|
|
|
} else { |
|
|
|
jsonObject.put("code", 0); |
|
|
|
jsonObject.put("msg", "推流服务启动成功"); |
|
|
|
mqttProviderConfig.publish(2, false, reTopic, jsonObject.toJSONString()); |
|
|
|
} |
|
|
|
Thread.sleep(cmdParam.getTime()); |
|
|
|
log.info(manager.queryAll().toString()); |
|
|
|
// 停止全部任务 |
|
|
|
manager.stopAll(); |
|
|
|
manager.destory(); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取阿里云通道 |
|
|
|
* |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private JSONObject getChenl() { |
|
|
|
String result = HttpURLConnectionUtil.doGet("https://streaming.t-aaron.com/livechannel/getLiveChannelList"); |
|
|
|
String result = HttpURLConnectionUtil.doGet("https://streaming.t-aaron.com/livechannel/getLiveChannelList"); |
|
|
|
if (!StringUtils.isNullOrEmpty(result)) { |
|
|
|
try { |
|
|
|
JSONObject object = JSONObject.parseObject(result); |
|
|
@@ -100,10 +197,10 @@ public class RtmpLiveService { |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
}catch (Exception e){ |
|
|
|
log.error("",e); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("", e); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
} else { |
|
|
|
return null; |
|
|
|
} |
|
|
|
return null; |
|
|
@@ -112,49 +209,54 @@ public class RtmpLiveService { |
|
|
|
|
|
|
|
/** |
|
|
|
* 根据推拉流地址推动 |
|
|
|
* |
|
|
|
* @param pushUrl |
|
|
|
* @param playUrl |
|
|
|
*/ |
|
|
|
public void runRtmp(String pushUrl,String playUrl,String code) { |
|
|
|
public void runRtmp(String pushUrl, String playUrl, String code) { |
|
|
|
try { |
|
|
|
log.info("获取播流地址:{}"); |
|
|
|
//CommandManager manager = new CommandManagerImpl(); |
|
|
|
String taskId = manager.start(code, CommandBuidlerFactory.createBuidler() |
|
|
|
.add("ffmpeg").add("-i",playUrl) |
|
|
|
.add("-rtsp_transport","tcp") |
|
|
|
.add("-vcodec","copy") |
|
|
|
.add("-acodec","copy") |
|
|
|
.add("-f","flv") |
|
|
|
String taskId = manager.start(code, CommandBuidlerFactory.createBuidler() |
|
|
|
.add("ffmpeg").add("-i", playUrl) |
|
|
|
.add("-rtsp_transport", "tcp") |
|
|
|
.add("-vcodec", "copy") |
|
|
|
.add("-acodec", "copy") |
|
|
|
.add("-f", "flv") |
|
|
|
.add("-b:v", "2M") |
|
|
|
.add("-maxrate", "2M") |
|
|
|
.add("-bufsize", "1M") |
|
|
|
.add("-v", "trace ") |
|
|
|
.add("-y").add(pushUrl)); |
|
|
|
cmdParam.getMqttTopic(); |
|
|
|
String reTopic = cmdParam.getMqttTopic().replace("live","result"); |
|
|
|
String reTopic = cmdParam.getMqttTopic().replace("live", "result"); |
|
|
|
|
|
|
|
log.info(manager.queryAll().toString()); |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
if (StringUtils.isNullOrEmpty(taskId)){ |
|
|
|
if (StringUtils.isNullOrEmpty(taskId)) { |
|
|
|
jsonObject.put("code", -1); |
|
|
|
jsonObject.put("msg", "推流服务失败"); //推流失败 |
|
|
|
mqttProviderConfig.publish(2,false,reTopic,jsonObject.toJSONString()); |
|
|
|
mqttProviderConfig.publish(2, false, reTopic, jsonObject.toJSONString()); |
|
|
|
// 停止全部任务 |
|
|
|
manager.stopAll(); |
|
|
|
return; |
|
|
|
}else { |
|
|
|
} else { |
|
|
|
jsonObject.put("code", 0); |
|
|
|
jsonObject.put("msg", "推流服务启动成功"); |
|
|
|
mqttProviderConfig.publish(2,false,reTopic,jsonObject.toJSONString()); |
|
|
|
mqttProviderConfig.publish(2, false, reTopic, jsonObject.toJSONString()); |
|
|
|
} |
|
|
|
Thread.sleep(36000); |
|
|
|
Thread.sleep(cmdParam.getTime()); |
|
|
|
log.info(manager.queryAll().toString()); |
|
|
|
// 停止全部任务 |
|
|
|
manager.stopAll(); |
|
|
|
manager.destory(); |
|
|
|
}catch (Exception e){ |
|
|
|
log.error("",e); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("", e); |
|
|
|
} |
|
|
|
//ds.close(); |
|
|
|
} |
|
|
|
|
|
|
|
public void stopRtmp() { |
|
|
|
public void stopRtmp() { |
|
|
|
try { |
|
|
|
log.info("停止推流"); |
|
|
|
//CommandManager manager = new CommandManagerImpl(); |
|
|
@@ -162,22 +264,22 @@ public class RtmpLiveService { |
|
|
|
int index = manager.stopAll(); |
|
|
|
manager.destory(); |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
String reTopic = cmdParam.getMqttTopic().replace("live","result"); |
|
|
|
if (index==0){ |
|
|
|
String reTopic = cmdParam.getMqttTopic().replace("live", "result"); |
|
|
|
if (index == 0) { |
|
|
|
jsonObject.put("code", -1); |
|
|
|
jsonObject.put("msg", "推流服务关闭失败"); //推流失败 |
|
|
|
mqttProviderConfig.publish(2,false,reTopic,jsonObject.toJSONString()); |
|
|
|
mqttProviderConfig.publish(2, false, reTopic, jsonObject.toJSONString()); |
|
|
|
// 停止全部任务 |
|
|
|
manager.stopAll(); |
|
|
|
return; |
|
|
|
}else if (index>0){ |
|
|
|
} else if (index > 0) { |
|
|
|
jsonObject.put("code", 0); |
|
|
|
jsonObject.put("msg", "推流服务关闭成功"); |
|
|
|
returnChenl(); |
|
|
|
mqttProviderConfig.publish(2,false,reTopic,jsonObject.toJSONString()); |
|
|
|
mqttProviderConfig.publish(2, false, reTopic, jsonObject.toJSONString()); |
|
|
|
} |
|
|
|
}catch (Exception e){ |
|
|
|
log.error("",e); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("", e); |
|
|
|
} |
|
|
|
//ds.close(); |
|
|
|
} |
|
|
@@ -185,6 +287,7 @@ public class RtmpLiveService { |
|
|
|
|
|
|
|
/** |
|
|
|
* 释放阿里云通道 |
|
|
|
* |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private JSONObject returnChenl() { |
|
|
@@ -215,8 +318,8 @@ public class RtmpLiveService { |
|
|
|
return null; |
|
|
|
} |
|
|
|
return null; |
|
|
|
}catch (Exception e){ |
|
|
|
log.error("",e); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("", e); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |