Blame view

src/main/java/com/objecteye/service/impl/PreviewServiceImpl.java 2.99 KB
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  package com.objecteye.service.impl;
  
  import com.objecteye.entity.SyEquipment;
  import com.objecteye.service.EquipmentService;
  import com.objecteye.service.PreviewService;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import java.io.IOException;
  import java.net.InetAddress;
  import java.util.concurrent.ConcurrentHashMap;
  
  @Service
  public class PreviewServiceImpl implements PreviewService {
  
      @Autowired
      static String ffmpegTnstructions;
      private EquipmentService equipmentService;
  
      private static ConcurrentHashMap<String, Process> rtmpAndprocessMaps = new ConcurrentHashMap<>();
  
      private static ConcurrentHashMap<String, String> userAndrtmpMaps = new ConcurrentHashMap<>();
  
      @Override
      public String getRtmp(String userName, Integer deviceId) throws IOException {
  
          SyEquipment equipment = equipmentService.findOne(deviceId);
  
          String equipmentName = equipment.getEquipmentName();
          String rtspUrl = equipment.getRtspUrl();
          boolean rtmpStrIsVaild = false;
          String address = InetAddress.getLocalHost().toString().split("/")[1];
          String rtmpStr = "rtmp://" + address + ":1935/hls/" + equipmentName + deviceId;
  
          if (rtmpAndprocessMaps.containsKey(rtmpStr)) {
              if (userAndrtmpMaps.containsKey(userName)) {
                  userAndrtmpMaps.remove(userName);
              }
              userAndrtmpMaps.put(userName, rtmpStr);
          } else {
              Process process = null;
              if (rtspUrl.contains("rtsp")) {
                  ffmpegTnstructions = "ffmpeg  -re  -stream_loop -1 -rtsp_transport  tcp -i" + rtspUrl + "-vcodec copy -acodec copy -f flv -r 15 -s 1280x720 -an -y " + rtmpStr;
              } else {
                  ffmpegTnstructions = "ffmpeg  -re  -stream_loop -1 -rtsp_transport  tcp -i rtsp://" + equipmentName + ":" + equipment.getPassword() + "@" + equipment.getEquipmentIp() + ":" + equipment.getEquipmentPort() + "/h264/ch1/main/av_stream  -vcodec copy -acodec copy -f flv -r 15 -s 1280x720 -an -y rtmp://" + address + ":1935/hls/" + equipmentName + deviceId;
              }
              process = Runtime.getRuntime().exec(ffmpegTnstructions);
              rtmpAndprocessMaps.put(rtmpStr, process);
              userAndrtmpMaps.put(userName, rtmpStr);
              rtmpStrIsVaild = process.isAlive();
              /*if (rtmpStrIsVaild) {
                  System.out.println("转流进程正在运行");
              }*/
          }
          for (String rtmpString : rtmpAndprocessMaps.keySet()) {
              if (!userAndrtmpMaps.containsValue(rtmpString)) {
                  //System.out.println("当前转流进程未使用,停止rtsp转rtmp");
                  Process detroyPro = rtmpAndprocessMaps.get(rtmpString);
                  if (detroyPro.isAlive()) {
                      detroyPro.destroy();
                  }
                  rtmpAndprocessMaps.remove(rtmpString);
              }
          }
          if (rtmpStrIsVaild) {
              return rtmpStr;
          }
          return null;
      }
  }