ZLMUtils.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package com.genersoft.iot.vmp.media.zlm;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.stereotype.Component;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. @Component
  9. public class ZLMUtils {
  10. @Value("${media.rtp.udpPortRange}")
  11. private String udpPortRange;
  12. @Autowired
  13. private ZLMRESTfulUtils zlmresTfulUtils;
  14. private int[] udpPortRangeArray = new int[2];
  15. private int currentPort = 0;
  16. public int getNewRTPPort(String ssrc) {
  17. String streamId = String.format("%08x", Integer.parseInt(ssrc)).toUpperCase();
  18. Map<String, Object> param = new HashMap<>();
  19. int newPort = getPortFromUdpPortRange();
  20. param.put("port", newPort);
  21. param.put("enable_tcp", 1);
  22. param.put("stream_id", streamId);
  23. JSONObject jsonObject = zlmresTfulUtils.openRtpServer(param);
  24. if (jsonObject != null && jsonObject.getInteger("code") == 0) {
  25. return newPort;
  26. } else {
  27. return getNewRTPPort(ssrc);
  28. }
  29. }
  30. private int getPortFromUdpPortRange() {
  31. if (currentPort == 0) {
  32. String[] udpPortRangeStrArray = udpPortRange.split(",");
  33. udpPortRangeArray[0] = Integer.parseInt(udpPortRangeStrArray[0]);
  34. udpPortRangeArray[1] = Integer.parseInt(udpPortRangeStrArray[1]);
  35. }
  36. if (currentPort == 0 || currentPort++ > udpPortRangeArray[1]) {
  37. currentPort = udpPortRangeArray[0];
  38. return udpPortRangeArray[0];
  39. } else {
  40. if (currentPort % 2 == 1) {
  41. currentPort++;
  42. }
  43. return currentPort++;
  44. }
  45. }
  46. }