whycxzp
2021-04-19 3ce5138c7cb230c531c3e0ee43a6e9cb361719a1
取消打印
3个文件已修改
22 ■■■■■ 已修改文件
src/main/java/com/whyc/video/RtspController.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/video/VideoProcessTimer.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/video/WsHandler.java 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/video/RtspController.java
@@ -13,7 +13,6 @@
@Controller
@RequestMapping("/rtsp")
@Scope("prototype")
public class RtspController {
    @Autowired
@@ -24,7 +23,7 @@
    public String receive(HttpServletRequest request, Object response) {
        //String ipSession = request.getParameter("ipSession");
        String ip = request.getParameter("ip");
        System.out.println("method:" + request.getMethod());
        //System.out.println("method:" + request.getMethod());
        try {
            ServletInputStream inputStream = request.getInputStream();
            int len = -1;
src/main/java/com/whyc/video/VideoProcessTimer.java
@@ -19,8 +19,8 @@
    /**检查是否有不需要的ffmpeg进程,有则关掉,节约资源,10秒钟检查一次*/
    @Scheduled(cron = "0/10 * * * * *")
    public void checkProcess(){
        LocalDateTime now = LocalDateTime.now();
        System.out.println("当前时间:"+now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //LocalDateTime now = LocalDateTime.now();
        //System.out.println("当前时间:"+now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        Map<String, WebSocketSession> clients = WsHandler.clients;
        Map<String, Process> processMap = WsHandler.processMap;
src/main/java/com/whyc/video/WsHandler.java
@@ -32,7 +32,7 @@
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("新的加入");
        //System.out.println("新的加入");
        this.session = session;
        //获取ws请求的参数
@@ -46,7 +46,7 @@
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println(">>>>>>>>>>终止了连接<<<<<<<<<<");
        //System.out.println(">>>>>>>>>>终止了连接<<<<<<<<<<");
        Optional<String> first = clients.keySet().stream().filter(key -> key.contains(session.getId())).findFirst();
        clients.remove(first.get());
@@ -54,7 +54,7 @@
    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        System.out.println(">>>>>>>>>>发生错误,终止了连接<<<<<<<<<<");
        //System.out.println(">>>>>>>>>>发生错误,终止了连接<<<<<<<<<<");
        Optional<String> first = clients.keySet().stream().filter(key -> key.contains(session.getId())).findFirst();
        clients.remove(first.get());
    }
@@ -65,19 +65,14 @@
        String ffmpegPath = "F:\\download\\ffmpeg-2021-04-11-git-309e3cc15c-essentials_build\\bin\\ffmpeg.exe ";
        //String ffmpegPath = "";
        try {
            // 视频切换时,先销毁进程,全局变量Process process,方便进程销毁重启,即切换推流视频
            /*if(process != null){
                process.destroy();
                System.out.println(">>>>>>>>>>推流视频切换<<<<<<<<<<");
            }*/
            System.out.println(">>>>>>>>>>推流视频新增<<<<<<<<<<");
            //System.out.println(">>>>>>>>>>推流视频新增<<<<<<<<<<");
            String id = "rtsp://admin:a123456789@"+ip+":554/Streaming/Channels/101";
            // cmd命令拼接,注意命令中存在空格
            String command = ffmpegPath; // ffmpeg位置
            command += "  "; // ffmpeg开头,-re代表按照帧率发送,在推流时必须有
            command += " -i \"" + id + "\""; // 指定要推送的视频
            command += " -q 0 -f mpegts -codec:v mpeg1video -s 800x600 " + fileName; // 指定推送服务器,-f:指定格式
            System.out.println("ffmpeg推流命令:" + command);
            //System.out.println("ffmpeg推流命令:" + command);
            //如果不存在进程,则建立ip对应的视频流进程
            if(!processMap.containsKey(ip)){