From 330acd2d79198ccc6bed9401eea90c45d907f174 Mon Sep 17 00:00:00 2001 From: ovo Date: Sun, 8 Dec 2024 18:55:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=A7=86=E9=A2=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 视频初步搭建 --- .../com/guwan/backend/client/SrsClient.java | 83 +++++++++++++++++++ .../guwan/backend/service/LiveService.java | 34 +++++++- src/main/resources/application.yml | 7 +- 3 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/guwan/backend/client/SrsClient.java diff --git a/src/main/java/com/guwan/backend/client/SrsClient.java b/src/main/java/com/guwan/backend/client/SrsClient.java new file mode 100644 index 0000000..eeb59eb --- /dev/null +++ b/src/main/java/com/guwan/backend/client/SrsClient.java @@ -0,0 +1,83 @@ +package com.guwan.backend.client; + +import lombok.Data; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +@Slf4j +@Component +@RequiredArgsConstructor +public class SrsClient { + + private final RestTemplate restTemplate; + + @Value("${srs.server.url}") + private String srsServerUrl; + + /** + * 开始录制 + */ + public void startRecord(String streamKey, RecordConfig config) { + String url = String.format("%s/api/v1/streams/%s/recording/start", srsServerUrl, streamKey); + try { + ResponseEntity response = restTemplate.postForEntity(url, config, String.class); + if (!response.getStatusCode().is2xxSuccessful()) { + throw new RuntimeException("开始录制失败: " + response.getBody()); + } + } catch (Exception e) { + log.error("开始录制失败", e); + throw new RuntimeException("开始录制失败", e); + } + } + + /** + * 停止录制 + */ + public void stopRecord(String streamKey) { + String url = String.format("%s/api/v1/streams/%s/recording/stop", srsServerUrl, streamKey); + try { + ResponseEntity response = restTemplate.postForEntity(url, null, String.class); + if (!response.getStatusCode().is2xxSuccessful()) { + throw new RuntimeException("停止录制失败: " + response.getBody()); + } + } catch (Exception e) { + log.error("停止录制失败", e); + throw new RuntimeException("停止录制失败", e); + } + } + + /** + * 获取流信息 + */ + public StreamInfo getStreamInfo(String streamKey) { + String url = String.format("%s/api/v1/streams/%s", srsServerUrl, streamKey); + try { + ResponseEntity response = restTemplate.getForEntity(url, StreamInfo.class); + return response.getBody(); + } catch (Exception e) { + log.error("获取流信息失败", e); + throw new RuntimeException("获取流信息失败", e); + } + } + + @Data + public static class RecordConfig { + private String format = "flv"; // 录制格式 + private String filePath; // 文件路径 + } + + @Data + public static class StreamInfo { + private String streamId; // 流ID + private String clientId; // 客户端ID + private String ip; // 客户端IP + private Long startTime; // 开始时间 + private String status; // 状态 + private Long bytesIn; // 输入字节数 + private Long bytesOut; // 输出字节数 + } +} \ No newline at end of file diff --git a/src/main/java/com/guwan/backend/service/LiveService.java b/src/main/java/com/guwan/backend/service/LiveService.java index 07526fc..e27383a 100644 --- a/src/main/java/com/guwan/backend/service/LiveService.java +++ b/src/main/java/com/guwan/backend/service/LiveService.java @@ -66,9 +66,9 @@ public class LiveService { * 开始录制 */ private void startRecording(String streamKey) { - srsClient.startRecord(streamKey, new RecordConfig() - .setFormat("flv") - .setFilePath("/recordings/" + streamKey + ".flv")); + SrsClient.RecordConfig config = new SrsClient.RecordConfig(); + config.setFilePath("/recordings/" + streamKey + ".flv"); + srsClient.startRecord(streamKey, config); } /** @@ -77,4 +77,32 @@ public class LiveService { private void stopRecording(String streamKey) { srsClient.stopRecord(streamKey); } + + /** + * 生成推流密钥 + */ + private String generateStreamKey() { + return UUID.randomUUID().toString(); + } + + /** + * 生成推流地址 + */ + private String generateRtmpUrl(String streamKey) { + return "rtmp://localhost:1935/live/" + streamKey; + } + + /** + * 生成播放地址 + */ + private String generateHlsUrl(String streamKey) { + return "http://localhost:8088/live/" + streamKey + ".m3u8"; + } + + /** + * 生成回放地址 + */ + private String generateReplayUrl(String streamKey) { + return "http://localhost:8080/recordings/" + streamKey + ".flv"; + } } \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 40f3d27..c9f9643 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -124,4 +124,9 @@ config: detect-pool-size: 16 compare-pool-size: 16 rec-face-thd: 0.8 - rec-id-thd: 0.5 \ No newline at end of file + rec-id-thd: 0.5 + +# SRS配置 +srs: + server: + url: http://localhost:1985 # SRS HTTP API地址 \ No newline at end of file