@Service
@Slf4j
public class HikVisionServiceImp {
private String secret;
@Resource
ServiceBin serviceBin;
@Value("${media.zlmediakit_domain}")
String domain;
/***
* /ISAPI/Streaming/Channels/101
* @param ip
* @param port
* @param account
* @param password
* @param deviceType
* @param channel
* @param isMasterStream 是否是主码流-高清(反之为模糊画质,但不是所有设备都支持模糊画质)
* @return
*/
public LiveCreateResponse createLive(String ip, String port, String account, String password, String deviceType, Integer channel, boolean isMasterStream) {
String rtsp = "rtsp://";
// app 和 stream 都是任意指定的名字,用于区分不同摄像头的直播
String app = ip.replaceAll("\\.", "");
String stream = port;
if (!StringUtils.isEmpty(account) && !StringUtils.isEmpty(password)) {
rtsp += account + ":" + password + "@";
}
if (StringUtils.isEmpty(ip)) {
return LiveCreateResponse.wrong("无效 ip");
} else {
rtsp += ip;
}
if (!StringUtils.isEmpty(port)) {
rtsp += ":" + port;
}
if (deviceType.equals(MediaSourceType.NVR)) {
stream += channel;
rtsp += "/ISAPI/Streaming/Channels/" + channel
+ (isMasterStream ? "01" : "02");
}
log.info("rtsp=======" + rtsp );
String result1 = serviceBin.getZlMediaKitFeign().addStreamProxy(app, stream, rtsp, "1");
log.info(result1);
LiveCreateResponse result = JSONObject.parseObject(result1, LiveCreateResponse.class);
if (result.getCode() == 0) {
String uri = domain + "/" + app + "/" + stream;
result.setHls("http://" + uri + "/hls.m3u8");
result.setHttpFlv("http://" + uri + ".flv");
result.setWsFlv("ws://" + uri + ".flv");
}
return result;
}
上面是通过fegin远程调用流媒体服务器进行创建直播流 rtsp地址格式
@FeignClient(name = "zlmediakit", url = "${media.zlmediakit_domain}", configuration = {ZLMediaKitFeignClientInterceptor.class}, contextId = "mediaKitFeign")
public interface ZLMediaKitFeign {
@GetMapping("/index/api/addStreamProxy")
String addStreamProxy(@RequestParam(value = "app") String app,
@RequestParam(value = "stream") String stream,
@RequestParam(value = "url") String url,
@RequestParam(value = "enable_hls") String enable_hls);
}
media:
##要带端口号
zlmediakit_domain: 自己的流媒体服务器地址
zlmediakit_secret: 自己的密钥
defaultVhost: __defaultVhost__