feat(camera): 添加摄像头流媒体拉流功能
- 在 CameraConfig 中添加 apiSecret 配置项- 实现摄像头 FLV 流地址生成和自动拉流功能- 添加 fastjson 依赖用于 JSON 解析 - 优化摄像头流媒体管理接口
This commit is contained in:
@@ -335,3 +335,4 @@ sales:
|
|||||||
|
|
||||||
camera:
|
camera:
|
||||||
media-server-host: http://47.117.71.33:15218 # ZLMediaKit 控制台地址
|
media-server-host: http://47.117.71.33:15218 # ZLMediaKit 控制台地址
|
||||||
|
api-secret: kuQV244gFXu7fBev9UBFkm0IrslUC2A9 # 必须与ZLMediaKit的config.ini一致
|
||||||
|
|||||||
@@ -9,4 +9,5 @@ import org.springframework.stereotype.Component;
|
|||||||
@Data
|
@Data
|
||||||
public class CameraConfig {
|
public class CameraConfig {
|
||||||
private String mediaServerHost;
|
private String mediaServerHost;
|
||||||
|
private String apiSecret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,5 +35,11 @@
|
|||||||
<version>2.0.29</version>
|
<version>2.0.29</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>1.2.83</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
package com.klp.controller;
|
package com.klp.controller;
|
||||||
|
|
||||||
|
import java.net.URLEncoder;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.klp.common.utils.StringUtils;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import com.klp.common.annotation.RepeatSubmit;
|
import com.klp.common.annotation.RepeatSubmit;
|
||||||
@@ -23,6 +26,7 @@ import com.klp.domain.bo.WmsCameraManagementBo;
|
|||||||
import com.klp.service.IWmsCameraManagementService;
|
import com.klp.service.IWmsCameraManagementService;
|
||||||
import com.klp.common.core.page.TableDataInfo;
|
import com.klp.common.core.page.TableDataInfo;
|
||||||
import com.klp.common.config.CameraConfig;
|
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}")
|
@GetMapping("/stream/{cameraId}")
|
||||||
public R<WmsCameraManagementVo> getCameraStream(@PathVariable Long cameraId) {
|
public R<WmsCameraManagementVo> getCameraStream(@PathVariable Long cameraId) {
|
||||||
|
// 1. 查询摄像头RTSP地址
|
||||||
WmsCameraManagementVo camera = iWmsCameraManagementService.queryById(cameraId);
|
WmsCameraManagementVo camera = iWmsCameraManagementService.queryById(cameraId);
|
||||||
if (camera == null) {
|
if (camera == null || StringUtils.isBlank(camera.getRtspUrl())) {
|
||||||
return R.fail("摄像头不存在");
|
return R.fail("摄像头配置错误");
|
||||||
}
|
}
|
||||||
// 组装ZLMediaKit的FLV播放地址
|
|
||||||
String app = "live";
|
// 2. 生成FLV播放地址
|
||||||
String stream = camera.getCameraCode();
|
String flvUrl = String.format("%s/live/%s.flv",
|
||||||
String flvUrl = cameraConfig.getMediaServerHost() + "/" + app + "/" + stream + ".flv";
|
cameraConfig.getMediaServerHost(),
|
||||||
camera.setHttpFmp4(flvUrl); // 复用已有字段存flvUrl,前端用httpFmp4字段接收
|
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);
|
return R.ok(camera);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return R.fail("API调用失败: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user