Skip to content

Commit

Permalink
修改日志
Browse files Browse the repository at this point in the history
  • Loading branch information
邓林杰 committed Jan 18, 2024
1 parent a31e27f commit 8a4bec3
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 283 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,45 +32,19 @@
@Import(SpringContextHolder.class)
public class HikVisionAutoConfiguration {

private final static Logger log = LoggerFactory.getLogger(HikVisionAutoConfiguration.class);
private final static Logger log = LoggerFactory.getLogger(HikVisionAutoConfiguration.class);

private static final FIFOCache<String, CameraLogin> LOCAL_CACHE = CacheUtil.newFIFOCache(400);
private static final FIFOCache<String, CameraLogin> LOCAL_CACHE = CacheUtil.newFIFOCache(400);

private HCNetSDK hCNetSDK = null;
private HCNetSDK hCNetSDK = null;

private PlayCtrl playControl = null;
private final PlayCtrl playControl = null;

public HikVisionAutoConfiguration(HiKProperties properties){
if(properties.getSdk_path()==null){
throw new IllegalArgumentException("请指定sdk类库(注意区分系统版本)");
}

}

@Bean
public HCNetSDK initSdk(HiKProperties properties){
synchronized (HCNetSDK.class) {
String strDllPath = "";
try {
//win系统加载库路径
if (OsSelectUtil.isWindows()) {
strDllPath = properties.getSdk_path().startsWith("classpath") ?
ResourceUtils.getFile(properties.getSdk_path()+"/HCNetSDK.dll").getPath() : properties.getSdk_path()+"/HCNetSDK.dll";
}
//Linux系统加载库路径
else if (OsSelectUtil.isLinux()){
strDllPath = properties.getSdk_path().startsWith("classpath") ?
ResourceUtils.getFile(properties.getSdk_path()+"/libhcnetsdk.so").getPath() : properties.getSdk_path()+"/libhcnetsdk.so";
}
hCNetSDK = (HCNetSDK) Native.loadLibrary(strDllPath, HCNetSDK.class);
} catch (Exception ex) {
log.error("加载HikSdk失败");
ex.printStackTrace();
return null;
}
public HikVisionAutoConfiguration(HiKProperties properties) {
if (properties.getSdk_path() == null) {
throw new IllegalArgumentException("请指定sdk类库(注意区分系统版本)");
}
log.info("加载hksdk成功");
return hCNetSDK;

}

@Bean
Expand All @@ -83,12 +57,12 @@ private static PlayCtrl initPlay(HiKProperties properties) {
//win系统加载库路径
if (OsSelectUtil.isWindows())
strPlayPath = properties.getSdk_path().startsWith("classpath") ?
ResourceUtils.getFile(properties.getSdk_path()+"/PlayCtrl.dll").getPath() : properties.getSdk_path()+"/PlayCtrl.dll";
//Linux系统加载库路径
ResourceUtils.getFile(properties.getSdk_path() + "/PlayCtrl.dll").getPath() : properties.getSdk_path() + "/PlayCtrl.dll";
//Linux系统加载库路径
else if (OsSelectUtil.isLinux())
strPlayPath = properties.getSdk_path().startsWith("classpath") ?
ResourceUtils.getFile(properties.getSdk_path()+"/libPlayCtrl.so").getPath() : properties.getSdk_path()+"/libPlayCtrl.so";
playControl = (PlayCtrl) Native.loadLibrary(strPlayPath,PlayCtrl.class);
ResourceUtils.getFile(properties.getSdk_path() + "/libPlayCtrl.so").getPath() : properties.getSdk_path() + "/libPlayCtrl.so";
playControl = (PlayCtrl) Native.loadLibrary(strPlayPath, PlayCtrl.class);
} catch (Exception ex) {
log.error("加载playControl失败}");
ex.printStackTrace();
Expand All @@ -99,6 +73,32 @@ else if (OsSelectUtil.isLinux())
return playControl;
}

@Bean
public HCNetSDK initSdk(HiKProperties properties) {
synchronized (HCNetSDK.class) {
String strDllPath = "";
try {
//win系统加载库路径
if (OsSelectUtil.isWindows()) {
strDllPath = properties.getSdk_path().startsWith("classpath") ?
ResourceUtils.getFile(properties.getSdk_path() + "/HCNetSDK.dll").getPath() : properties.getSdk_path() + "/HCNetSDK.dll";
}
//Linux系统加载库路径
else if (OsSelectUtil.isLinux()) {
strDllPath = properties.getSdk_path().startsWith("classpath") ?
ResourceUtils.getFile(properties.getSdk_path() + "/libhcnetsdk.so").getPath() : properties.getSdk_path() + "/libhcnetsdk.so";
}
hCNetSDK = (HCNetSDK) Native.loadLibrary(strDllPath, HCNetSDK.class);
} catch (Exception ex) {
log.error("加载HikSdk失败");
ex.printStackTrace();
return null;
}
}
log.info("加载hksdk成功");
return hCNetSDK;
}

@Bean("converterPoolExecutor")
public ThreadPoolTaskExecutor asyncServiceExecutor(HiKProperties hiKProperties) {

Expand All @@ -123,7 +123,7 @@ public ThreadPoolTaskExecutor asyncServiceExecutor(HiKProperties hiKProperties)
}

@PreDestroy
public void destroy(){
public void destroy() {
HkUtils.stopListen();
if (null != hCNetSDK) {
log.info("释放hcNetSDK资源");
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/cn/nkk/hikvision/events/FExceptionCallBack.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ public class FExceptionCallBack implements HCNetSDK.FExceptionCallBack {

@Override
public void invoke(int dwType, int lUserID, int lHandle, Pointer pUser) {
log.info("设备异常,dwType:{},userId:{},handler:{}",dwType,lUserID,lHandle);
switch(dwType)
{
log.info("设备异常,dwType:{},userId:{},handler:{}", dwType, lUserID, lHandle);
switch (dwType) {
case EXCEPTION_EXCHANGE: //用户交互时异常
log.error("用户交互时异常:{}",lHandle);
log.error("用户交互时异常:{}", lHandle);
break;
case 0x8017:
log.error("用户交互恢复:{}",lHandle);
log.error("用户交互恢复:{}", lHandle);
break;
default:
System.out.println("异常");
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/cn/nkk/hikvision/factory/FlvConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
* @author dlj
* @date 2023/03/29
*/

/**
* flv转换器
*
* @author dlj
* @date 2023/03/29
*/
public class FlvConverter extends Thread implements Converter{
public class FlvConverter extends Thread implements Converter {

private static final Logger log = LoggerFactory.getLogger(FlvConverter.class);
private byte[] headers;
Expand Down Expand Up @@ -69,14 +70,14 @@ public void run() {
try {
avutil.av_log_set_level(avutil.AV_LOG_ERROR);
log.info("进入grabber------------------------");
grabber = Objects.nonNull(inputStream) ? new FFmpegFrameGrabber(inputStream,0) : new FFmpegFrameGrabber(rtspUrl);
grabber = Objects.nonNull(inputStream) ? new FFmpegFrameGrabber(inputStream, 0) : new FFmpegFrameGrabber(rtspUrl);
if (StrUtil.isNotEmpty(rtspUrl) && rtspUrl.startsWith("rtsp")) {
grabber.setOption("rtsp_transport", "tcp");
//首选TCP进行RTP传输
grabber.setOption("rtsp_flags", "prefer_tcp");
log.info("rtsp链接------------------------");
}
if(Objects.nonNull(inputStream)){
if (Objects.nonNull(inputStream)) {
//检测管道流中是否存在数据,如果2s后依然没有写入1024的数据,则认为管道流中无数据,避免grabber.start();发生阻塞
long stime = new Date().getTime();
while (true) {
Expand All @@ -93,7 +94,7 @@ public void run() {
grabber.setOption("buffer_size", "1024000");
grabber.startUnsafe();
int videoCodec = grabber.getVideoCodec();
log.info("启动grabber,编码{}",videoCodec);
log.info("启动grabber,编码{}", videoCodec);
grabber.setImageWidth(640);
grabber.setImageHeight(480);

Expand Down Expand Up @@ -141,20 +142,20 @@ public void run() {
TimeUnit.MILLISECONDS.sleep(5);
}
} catch (Exception e) {
log.error("异步出错,{}",e.getMessage());
log.error("异步出错,{}", e.getMessage());
} finally {
try {
if(this.outputStream!=null) this.outputStream.close();
if(this.inputStream != null) this.inputStream.close();
if(this.playHandler!= null) HkUtils.stopBackPlay(this.playHandler);
if(grabber!= null) grabber.close();
if (this.outputStream != null) this.outputStream.close();
if (this.inputStream != null) this.inputStream.close();
if (this.playHandler != null) HkUtils.stopBackPlay(this.playHandler);
if (grabber != null) grabber.close();
if (recorder != null) recorder.close();
if (stream != null) stream.close();
System.gc();
context.getResponse().flushBuffer();
context.complete();
} catch (Exception e) {
log.error("资源回收,{}",e.getMessage());
log.error("资源回收,{}", e.getMessage());
}
}

Expand All @@ -175,7 +176,6 @@ public void writeResponse(byte[] bytes) {
}



@Override
public String getKey() {
return null;
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/cn/nkk/hikvision/factory/M3u8Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@
* @author dlj
* @date 2023/03/29
*/
public class M3u8Converter extends Thread implements Converter{
public class M3u8Converter extends Thread implements Converter {

private final static Logger log = LoggerFactory.getLogger(M3u8Converter.class);

private final String rtspUrl;

public M3u8Converter(String rtspUrl){
public M3u8Converter(String rtspUrl) {
this.rtspUrl = rtspUrl;
}


@Override
public void run() {
HiKProperties hiKProperties = SpringContextHolder.getBean(HiKProperties.class);
if(StrUtil.isEmpty(hiKProperties.getStream().getM3u8_path())){
if (StrUtil.isEmpty(hiKProperties.getStream().getM3u8_path())) {
throw new IllegalArgumentException("请配置m3u8视频流存储路径");
}

String md5 = Md5Utils.encrypt16(rtspUrl);
File file = new File(hiKProperties.getStream().getM3u8_path(), md5);
if(!file.exists()){
if (!file.exists()) {
FileUtil.mkdir(file);
}
// 存放m3u8切片视频的目录
Expand Down Expand Up @@ -120,7 +120,7 @@ public void run() {
continue;
}
if (pkt.dts() == avutil.AV_NOPTS_VALUE && pkt.pts() == avutil.AV_NOPTS_VALUE || pkt.pts() < dts) {
log.debug("异常pkt 当前pts: " + pkt.pts() + " dts: " + pkt.dts() + " 上一包的pts: " + pts + " dts: "+ dts);
log.debug("异常pkt 当前pts: " + pkt.pts() + " dts: " + pkt.dts() + " 上一包的pts: " + pts + " dts: " + dts);
av_packet_unref(pkt);
continue;
}
Expand All @@ -133,8 +133,7 @@ public void run() {
//取出过滤器合并后的图像
// Frame filterFrame=filter.pullImage();
long spull_end = System.currentTimeMillis();
if ((spull_end - spull_start) > 500)
log.info("pull stream take up time : {}", spull_end - spull_start);
if ((spull_end - spull_start) > 500) log.info("pull stream take up time : {}", spull_end - spull_start);
recorder.recordPacket(pkt);
timebase = grabber.getFormatContext().streams(pkt.stream_index()).time_base().den();
pts += (timebase / (int) framerate);
Expand All @@ -147,12 +146,12 @@ public void run() {
//可以在这里写个错误图片流返回,这样播放器就能看到问题了
} finally {
try {
if(Objects.nonNull(grabber)) grabber.close();
if(Objects.nonNull(recorder)) recorder.close();
if (Objects.nonNull(grabber)) grabber.close();
if (Objects.nonNull(recorder)) recorder.close();
FileUtil.del(m3u8Path);
System.gc();
} catch (Exception e) {
log.error("资源回收:{}",e.getMessage());
log.error("资源回收:{}", e.getMessage());
}
}
}
Expand Down
82 changes: 38 additions & 44 deletions src/main/java/cn/nkk/hikvision/properties/HiKProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,53 @@
@ConfigurationProperties(prefix = "hik")
public class HiKProperties {

/**
* 海康sdk位置
*/
private String sdk_path;


/**
* 拉流配置
*/
public PullStream stream;

/**
* 海康sdk位置
*/
private String sdk_path;
/**
* 线程池配置
*/
private HikPool pool;

public String getSdk_path() {
return sdk_path;
}

public void setSdk_path(String sdk_path) {
this.sdk_path = sdk_path;
}

public PullStream getStream() {
return stream;
}

public void setStream(PullStream stream) {
this.stream = stream;
}

public HikPool getPool() {
return pool;
}

public void setPool(HikPool pool) {
this.pool = pool;
}

@Override
public String toString() {
return "HiKProperties{" +
"sdk_path='" + sdk_path + '\'' +
", stream=" + stream +
", pool=" + pool +
'}';
}

public static class HikPool{
public static class HikPool {
/**
* 核心线程数
*/
Expand Down Expand Up @@ -90,8 +119,7 @@ public String toString() {
}
}


public static class PullStream{
public static class PullStream {

/**
* m3u8磁盘路径
Expand All @@ -113,38 +141,4 @@ public String toString() {
'}';
}
}


public String getSdk_path() {
return sdk_path;
}

public void setSdk_path(String sdk_path) {
this.sdk_path = sdk_path;
}

public PullStream getStream() {
return stream;
}

public void setStream(PullStream stream) {
this.stream = stream;
}

public HikPool getPool() {
return pool;
}

public void setPool(HikPool pool) {
this.pool = pool;
}

@Override
public String toString() {
return "HiKProperties{" +
"sdk_path='" + sdk_path + '\'' +
", stream=" + stream +
", pool=" + pool +
'}';
}
}
Loading

0 comments on commit 8a4bec3

Please sign in to comment.