feat(camera): 添加摄像头流媒体拉流功能

- 在 CameraConfig 中添加 apiSecret 配置项- 实现摄像头 FLV 流地址生成和自动拉流功能- 添加 fastjson 依赖用于 JSON 解析
- 优化摄像头流媒体管理接口
This commit is contained in:
JR
2025-08-08 14:35:11 +08:00
parent 55dc112d40
commit 6357368459
4 changed files with 41 additions and 10 deletions

View File

@@ -1,12 +1,15 @@
package com.klp.controller;
import java.net.URLEncoder;
import java.util.List;
import java.util.Arrays;
import com.alibaba.fastjson.JSON;
import com.klp.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.klp.common.annotation.RepeatSubmit;
@@ -23,6 +26,7 @@ import com.klp.domain.bo.WmsCameraManagementBo;
import com.klp.service.IWmsCameraManagementService;
import com.klp.common.core.page.TableDataInfo;
import com.klp.common.config.CameraConfig;
import org.springframework.web.client.RestTemplate;
/**
* 摄像头管理
@@ -69,20 +73,39 @@ public class WmsCameraManagementController extends BaseController {
}
/**
* 获取摄像头FLV地址前端可用flv.js播放
* 获取 FLV地址并自动拉流
*/
@GetMapping("/stream/{cameraId}")
public R<WmsCameraManagementVo> getCameraStream(@PathVariable Long cameraId) {
// 1. 查询摄像头RTSP地址
WmsCameraManagementVo camera = iWmsCameraManagementService.queryById(cameraId);
if (camera == null) {
return R.fail("摄像头不存在");
if (camera == null || StringUtils.isBlank(camera.getRtspUrl())) {
return R.fail("摄像头配置错误");
}
// 2. 生成FLV播放地址
String flvUrl = String.format("%s/live/%s.flv",
cameraConfig.getMediaServerHost(),
camera.getCameraCode());
// 3. 调用ZLMediaKit拉流
String apiUrl = String.format("%s/index/api/addStreamProxy?&vhost=__defaultVhost__&app=live&stream=%s&url=%s&secret=%s",
cameraConfig.getMediaServerHost(),
camera.getCameraCode(),
camera.getRtspUrl(),
cameraConfig.getApiSecret());
System.out.println("拉流API地址: " + apiUrl);
try {
String result = new RestTemplate().getForObject(apiUrl, String.class);
if (!JSON.parseObject(result).getInteger("code").equals(0)) {
return R.fail("拉流失败: " + JSON.parseObject(result).getString("msg"));
}
camera.setHttpFmp4(flvUrl);
return R.ok(camera);
} catch (Exception e) {
return R.fail("API调用失败: " + e.getMessage());
}
// 组装ZLMediaKit的FLV播放地址
String app = "live";
String stream = camera.getCameraCode();
String flvUrl = cameraConfig.getMediaServerHost() + "/" + app + "/" + stream + ".flv";
camera.setHttpFmp4(flvUrl); // 复用已有字段存flvUrl前端用httpFmp4字段接收
return R.ok(camera);
}
/**