添加查询流接口

This commit is contained in:
sh
2026-06-25 13:05:24 +08:00
parent 130a3cae81
commit b6959ce22b
2 changed files with 109 additions and 3 deletions

View File

@@ -1,25 +1,37 @@
package com.ruoyi.cms.controller.cms;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.cms.domain.liveSteam.StreamResp;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/cms/liveSteam")
@Api(tags = "后台:直播")
public class CmsLiveSteamingController {
private final RestTemplate restTemplate = new RestTemplate();
private static final String SERVER_IP = "10.98.80.207";
private static final int SRS_API_PORT = 8081;
private static final int RTC_PORT = 8000;
private static final int HTTP_PORT = 8081;
private static final int RTMP_PORT = 1935;
@ApiOperation("下载推流工具")
@PreAuthorize("@ss.hasPermi('cms:liveSteam:downloadTools')")
@GetMapping("/downloadTools")
@@ -72,4 +84,77 @@ public class CmsLiveSteamingController {
System.out.println("文件读取异常:" + e.getMessage());
}
}
/**
* 查询所有正在直播的流
*/
@ApiOperation("查询所有正在直播的流")
@PreAuthorize("@ss.hasPermi('cms:liveSteam:streams')")
@GetMapping("/streams")
public AjaxResult listAllStream() {
String srsUrl = String.format("http://%s:%d/api/v1/streams", SERVER_IP, SRS_API_PORT);
Map<String, Object> resp = restTemplate.getForObject(srsUrl, Map.class);
List<Map<String, Object>> allStreams = (List<Map<String, Object>>) resp.get("streams");
List<Map<String, Object>> liveStreams = allStreams.stream()
.filter(item -> {
Map<String, Object> pub = (Map<String, Object>) item.get("publish");
return Boolean.TRUE.equals(pub.get("active"));
})
.collect(Collectors.toList());
resp.put("streams", liveStreams);
return AjaxResult.success(resp);
}
/**
* 根据流ID查询是否开播
*/
@ApiOperation("查询单条流")
@PreAuthorize("@ss.hasPermi('cms:liveSteam:streams:query')")
@GetMapping("/streams/{streamId}")
public AjaxResult getInfo(@PathVariable String streamId) {
String srsUrl = String.format("http://%s:%d/api/v1/streams", SERVER_IP, SRS_API_PORT);
Map<String, Object> srsResp;
try {
srsResp = restTemplate.getForObject(srsUrl, Map.class);
} catch (Exception e) {
return AjaxResult.error("服务异常,获取直播状态失败");
}
List<Map<String, Object>> streamList = Collections.emptyList();
if (srsResp != null && srsResp.get("streams") instanceof List) {
streamList = (List<Map<String, Object>>) srsResp.get("streams");
}
boolean isLive = false;
for (Map<String, Object> item : streamList) {
if (item == null) continue;
String name = (String) item.get("name");
if (streamId.equals(name)) {
Map<String, Object> publish = (Map<String, Object>) item.get("publish");
if (publish != null && Boolean.TRUE.equals(publish.get("active"))) {
isLive = true;
}
break;
}
}
if (!isLive) {
return AjaxResult.error("当前流未开播");
}
String rtmpUrl = String.format("rtmp://%s:%d/live/%s", SERVER_IP, RTMP_PORT, streamId);
String hlsUrl = String.format("http://%s:%d/live/%s.m3u8", SERVER_IP, HTTP_PORT, streamId);
String flvUrl = String.format("http://%s:%d/live/%s.flv", SERVER_IP, HTTP_PORT, streamId);
String webrtcUrl = String.format("webrtc://%s:%d/live/%s", SERVER_IP, RTC_PORT, streamId);
StreamResp resp = new StreamResp();
resp.setStream_id(streamId);
resp.setPublishing_url(rtmpUrl);
StreamResp.PlayUrls urls = new StreamResp.PlayUrls();
urls.setHls(hlsUrl);
urls.setFlv(flvUrl);
urls.setWebrtc(webrtcUrl);
resp.setPlay_urls(urls);
return AjaxResult.success(resp);
}
}

View File

@@ -0,0 +1,21 @@
package com.ruoyi.cms.domain.liveSteam;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class StreamResp {
private String stream_id;
private String publishing_url;
private PlayUrls play_urls;
@Data
public static class PlayUrls {
private String hls;
private String flv;
private String webrtc;
}
}