SipVideoController.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package com.usky.cdi.controller;
  2. import com.usky.cdi.service.sip.config.SipServerProperties;
  3. import com.usky.cdi.service.sip.model.VideoStreamSession;
  4. import com.usky.cdi.service.sip.service.VideoStreamManager;
  5. import com.usky.cdi.service.sip.util.NetworkUtils;
  6. import lombok.RequiredArgsConstructor;
  7. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  8. import org.springframework.web.bind.annotation.*;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.stream.Collectors;
  13. @RestController
  14. @RequestMapping("/api/sip")
  15. @RequiredArgsConstructor
  16. @ConditionalOnProperty(prefix = "sip.server", name = "enabled", havingValue = "true")
  17. public class SipVideoController {
  18. private final SipServerProperties properties;
  19. private final VideoStreamManager streamManager;
  20. @GetMapping("/status")
  21. public Map<String, Object> status() {
  22. Map<String, Object> result = new HashMap<>();
  23. result.put("enabled", properties.isEnabled());
  24. result.put("host", properties.getHost());
  25. result.put("port", properties.getPort());
  26. result.put("localIp", NetworkUtils.resolveLocalIp(properties.getLocalIp()));
  27. result.put("platformId", properties.getPlatformId());
  28. result.put("activeSessions", streamManager.getAllSessions().size());
  29. return result;
  30. }
  31. @GetMapping("/sessions")
  32. public List<Map<String, Object>> sessions() {
  33. return streamManager.getAllSessions().stream()
  34. .map(this::toSessionMap)
  35. .collect(Collectors.toList());
  36. }
  37. @GetMapping("/sessions/{callId}")
  38. public Map<String, Object> session(@PathVariable String callId) {
  39. VideoStreamSession session = streamManager.getSession(callId);
  40. if (session == null) {
  41. Map<String, Object> notFound = new HashMap<>();
  42. notFound.put("error", "会话不存在");
  43. return notFound;
  44. }
  45. return toSessionMap(session);
  46. }
  47. @DeleteMapping("/sessions/{callId}")
  48. public Map<String, Object> stopSession(@PathVariable String callId) {
  49. streamManager.stopSession(callId);
  50. Map<String, Object> result = new HashMap<>();
  51. result.put("callId", callId);
  52. result.put("status", "stopped");
  53. return result;
  54. }
  55. private Map<String, Object> toSessionMap(VideoStreamSession session) {
  56. Map<String, Object> map = new HashMap<>();
  57. map.put("sessionId", session.getSessionId());
  58. map.put("callId", session.getCallId());
  59. map.put("remoteAddress", session.getRemoteAddress());
  60. map.put("remoteRtpPort", session.getRemoteRtpPort());
  61. map.put("localRtpPort", session.getLocalRtpPort());
  62. map.put("codec", session.getCodec());
  63. map.put("payloadType", session.getPayloadType());
  64. map.put("ssrc", session.getSsrc());
  65. map.put("state", session.getState().name());
  66. map.put("startTime", session.getStartTime());
  67. map.put("receivedPackets", session.getReceivedPackets().get());
  68. map.put("receivedFrames", session.getReceivedFrames().get());
  69. map.put("receivedBytes", session.getReceivedBytes().get());
  70. return map;
  71. }
  72. }