提交 89909eb9 编写于 作者: E eguid

去除遗留代码

上级 b23caa95
package cc.eguid.FFmpegCommandManager.web;
/**
* web管理-待完善
* @author eguid
*
*/
public class ManagerController {
}
package cc.eguid.FFmpegCommandManager.config;
public class FFmpegConfig {
private String path;
private boolean debug;
private Integer size;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public boolean isDebug() {
return debug;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
@Override
public String toString() {
return "FFmpegConfig [path=" + path + ", debug=" + debug + ", size=" + size + "]";
}
}
package cc.eguid.FFmpegCommandManager.entity;
/**
* 用于存放任务id,任务主进程,任务输出线程
* @author eguid
* @since jdk1.7
* @version 2016年10月29日
*/
public class TaskEntity {
private final String id;//任务id
private final Process process;//任务主进程
private final Thread thread;//任务输出线程
public TaskEntity(String id,Process process,Thread thread) {
this.id=id;
this.process=process;
this.thread=thread;
}
public String getId() {
return id;
}
public Process getProcess() {
return process;
}
public Thread getThread() {
return thread;
}
@Override
public String toString() {
return "TaskEntity [id=" + id + ", process=" + process + ", thread=" + thread + "]";
}
}
package cc.eguid.FFmpegCommandManager.dao;
import java.util.Collection;
import cc.eguid.FFmpegCommandManager.entity.TaskEntity;
/**
* 任务信息持久层接口
* @author eguid
* @since jdk1.7
* @version 2016年10月29日
*/
public interface TaskDao {
/**
* 通过id查询任务信息
* @param id - 任务ID
* @return TaskEntity -任务实体
*/
public TaskEntity get(String id);
/**
* 查询全部任务信息
* @return Collection<TaskEntity>
*/
public Collection<TaskEntity> getAll();
/**
* 增加任务信息
* @param taskEntity -任务信息实体
* @return 增加数量:<1-增加失败,>=1-增加成功
*/
public int add(TaskEntity taskEntity);
/**
* 删除id对应的任务信息
* @param id
* @return 数量:<1-操作失败,>=1-操作成功
*/
public int remove(String id);
/**
* 删除全部任务信息
* @return 数量:<1-操作失败,>=1-操作成功
*/
public int removeAll();
/**
* 是否存在某个ID
* @param id - 任务ID
* @return true:存在,false:不存在
*/
public boolean isHave(String id);
}
package cc.eguid.FFmpegCommandManager.dao;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import cc.eguid.FFmpegCommandManager.entity.TaskEntity;
/**
* 任务信息持久层实现
*
* @author eguid
* @since jdk1.7
* @version 2016年10月29日
*/
public class TaskDaoImpl implements TaskDao {
// 存放任务信息
private ConcurrentMap<String, TaskEntity> map = null;
public TaskDaoImpl(int size) {
map = new ConcurrentHashMap<>(size);
}
@Override
public TaskEntity get(String id) {
return map.get(id);
}
@Override
public Collection<TaskEntity> getAll() {
return map.values();
}
@Override
public int add(TaskEntity taskEntity) {
String id = taskEntity.getId();
if (id != null && !map.containsKey(id)) {
map.put(taskEntity.getId(), taskEntity);
if(map.get(id)!=null)
{
return 1;
}
}
return 0;
}
@Override
public int remove(String id) {
if(map.remove(id) != null){
return 1;
};
return 0;
}
@Override
public int removeAll() {
int size = map.size();
try {
map.clear();
} catch (Exception e) {
return 0;
}
return size;
}
@Override
public boolean isHave(String id) {
return map.containsKey(id);
}
}
package cc.eguid.FFmpegCommandManager.service;
import java.util.Map;
/**
* 命令组装器接口
* @author eguid
* @since jdk1.7
* @version 2016年10月29日
*/
public interface CommandAssembly {
/**
* 将参数转为ffmpeg命令
* @param paramMap
* @return
*/
public String assembly(Map<String, String> paramMap);
}
package cc.eguid.FFmpegCommandManager.service;
import java.util.Map;
/**
* 命令组装器实现
* @author eguid
* @since jdk1.7
* @version 2016年10月29日
*/
public class CommandAssemblyImpl implements CommandAssembly{
/**
*
* @param map
* -要组装的map
* @param id
* -返回参数:id
* @param id
* -返回参数:组装好的命令
* @return
*/
public String assembly(Map<String, String> paramMap) {
try {
if (paramMap.containsKey("ffmpegPath")) {
String ffmpegPath = (String) paramMap.get("ffmpegPath");
// -i:输入流地址或者文件绝对地址
StringBuilder comm = new StringBuilder(ffmpegPath + " -i ");
// 是否有必输项:输入地址,输出地址,应用名,twoPart:0-推一个元码流;1-推一个自定义推流;2-推两个流(一个是自定义,一个是元码)
if (paramMap.containsKey("input") && paramMap.containsKey("output") && paramMap.containsKey("appName")
&& paramMap.containsKey("twoPart")) {
String input = (String) paramMap.get("input");
String output = (String) paramMap.get("output");
String appName = (String) paramMap.get("appName");
String twoPart = (String) paramMap.get("twoPart");
String codec = (String) paramMap.get("codec");
// 默认h264解码
codec = (codec == null ? "h264" : (String) paramMap.get("codec"));
// 输入地址
comm.append(input);
// 当twoPart为0时,只推一个元码流
if ("0".equals(twoPart)) {
comm.append(" -vcodec " + codec + " -f flv -an " + output + appName);
} else {
// -f :转换格式,默认flv
if (paramMap.containsKey("fmt")) {
String fmt = (String) paramMap.get("fmt");
comm.append(" -f " + fmt);
}
// -r :帧率,默认25;-g :帧间隔
if (paramMap.containsKey("fps")) {
String fps = (String) paramMap.get("fps");
comm.append(" -r " + fps);
comm.append(" -g " + fps);
}
// -s 分辨率 默认是原分辨率
if (paramMap.containsKey("rs")) {
String rs = (String) paramMap.get("rs");
comm.append(" -s " + rs);
}
// 输出地址+发布的应用名
comm.append(" -an " + output + appName);
// 当twoPart为2时推两个流,一个自定义流,一个元码流
if ("2".equals(twoPart)) {
// 一个视频源,可以有多个输出,第二个输出为拷贝源视频输出,不改变视频的各项参数并且命名为应用名+HD
comm.append(" -vcodec copy -f flv -an ").append(output + appName + "HD");
}
}
return comm.toString();
}
}
} catch (Exception e) {
return null;
}
return null;
}
}
package cc.eguid.FFmpegCommandManager.service;
/**
* 默认任务消息输出处理
* @author eguid
* @since jdk1.7
* @version 2017年10月13日
*/
public class DefaultOutHandlerMethod implements OutHandlerMethod{
@Override
public void parse(String type,String msg) {
// System.out.println(type+":完整消息:"+msg);
//过滤消息
if (msg.indexOf("[rtsp") != -1) {
System.err.println(type + "发生网络异常丢包,消息体:" + msg);
}else if(msg.indexOf("frame=")!=-1){
System.err.println(type + ":" + msg);
}
}
}
package cc.eguid.FFmpegCommandManager.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import cc.eguid.FFmpegCommandManager.FFmpegManager;
/**
* 任务消息输出处理器
* @author eguid
* @since jdk1.7
* @version 2017年10月13日
*/
public class OutHandler extends Thread {
/**
* 控制状态
*/
private volatile boolean desstatus = true;
/**
* 读取输出流
*/
private BufferedReader br = null;
/**
* 输出类型
*/
private String type = null;
/**
* 消息处理方法
*/
private OutHandlerMethod ohm;
public void setOhm(OutHandlerMethod ohm) {
this.ohm = ohm;
}
public void setDesStatus(boolean desStatus) {
this.desstatus = desStatus;
}
public OutHandler(InputStream is, String type,OutHandlerMethod ohm) {
br = new BufferedReader(new InputStreamReader(is));
this.type = type;
this.ohm=ohm;
}
/**
* 重写线程销毁方法,安全的关闭线程
*/
@Override
public void destroy() {
setDesStatus(false);
}
/**
* 执行输出线程
*/
@Override
public void run() {
String msg = null;
try {
if (FFmpegManager.config.isDebug()) {
System.out.println(type + "开始推流!");
while (desstatus && (msg = br.readLine()) != null) {
ohm.parse(type,msg);
}
} else {
Thread.yield();
}
} catch (IOException e) {
System.out.println("发生内部异常错误,自动关闭[" + this.getId() + "]线程");
destroy();
} finally {
if (this.isAlive()) {
destroy();
}
}
}
}
package cc.eguid.FFmpegCommandManager.service;
/**
* 输出消息处理
* @author eguid
* @since jdk1.7
* @version 2017年10月13日
*/
public interface OutHandlerMethod {
/**
* 解析消息
* @param msg
* @param msg
*/
public void parse(String type, String msg);
}
package cc.eguid.FFmpegCommandManager.service;
import cc.eguid.FFmpegCommandManager.entity.TaskEntity;
/**
* 任务执行接口
* @author eguid
* @since jdk1.7
* @version 2016年10月29日
*/
public interface TaskHandler {
/**
* 按照命令执行主进程和输出线程
*
* @param id
* @param command
* @return
*/
public TaskEntity process(String id, String command);
/**
* 停止主进程(停止主进程需要保证输出线程已经关闭,否则输出线程会出错)
*
* @param process
* @return
*/
public boolean stop(Process process);
/**
* 停止输出线程
*
* @param thread
* @return
*/
public boolean stop(Thread thread);
/**
* 正确的停止输出线程和主进程
*
* @param process
* @param thread
* @return
*/
public boolean stop(Process process, Thread thread);
}
package cc.eguid.FFmpegCommandManager.service;
import java.io.IOException;
import cc.eguid.FFmpegCommandManager.FFmpegManager;
import cc.eguid.FFmpegCommandManager.entity.TaskEntity;
/**
* 任务处理实现
* @author eguid
* @since jdk1.7
* @version 2016年10月29日
*/
public class TaskHandlerImpl implements TaskHandler {
private Runtime runtime = null;
private OutHandlerMethod ohm=null;
public TaskHandlerImpl(OutHandlerMethod ohm) {
this.ohm = ohm;
}
public void setOhm(OutHandlerMethod ohm) {
this.ohm = ohm;
}
@Override
public TaskEntity process(String id, String command) {
Process process = null;
OutHandler outHandler = null;
TaskEntity tasker = null;
try {
if (runtime == null) {
runtime = Runtime.getRuntime();
}
if(FFmpegManager.config.isDebug())
System.out.println("执行命令:"+command);
process = runtime.exec(command);// 执行本地命令获取任务主进程
outHandler = new OutHandler(process.getErrorStream(), id,this.ohm);
outHandler.start();
tasker = new TaskEntity(id, process, outHandler);
} catch (IOException e) {
if(FFmpegManager.config.isDebug())
System.err.println("执行命令失败!正在停止进程和输出线程...");
stop(outHandler);
stop(process);
// 出现异常说明开启失败,返回null
return null;
}
return tasker;
}
@Override
public boolean stop(Process process) {
if (process != null) {
process.destroy();
return true;
}
return false;
}
@SuppressWarnings("deprecation")
@Override
public boolean stop(Thread outHandler) {
if (outHandler != null && outHandler.isAlive()) {
outHandler.stop();
outHandler.destroy();
return true;
}
return false;
}
@Override
public boolean stop(Process process, Thread thread) {
boolean ret;
ret=stop(thread);
ret=stop(process);
return ret;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册