1
zhanghua
2024-09-26 c775c6953d9759e70f08acbfa8f6d7490aaae3d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package com.netsdk.demo.customize.JordanPSD.module;
 
import com.netsdk.lib.NativeString;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.netsdk.lib.enumeration.ENUMERROR;
import com.netsdk.lib.structure.NET_IN_ADAPTIVE_DOWNLOAD_BY_TIME;
import com.netsdk.lib.structure.NET_IN_DOWNLOAD_BYFILE_SELFADAPT;
import com.netsdk.lib.structure.NET_OUT_ADAPTIVE_DOWNLOAD_BY_TIME;
import com.netsdk.lib.structure.NET_OUT_DOWNLOAD_BYFILE_SELFADAPT;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.ptr.IntByReference;
 
import static com.netsdk.demo.util.StructFieldChooser.GetSelectedSingleFieldValue;
 
/**
 * @author 47040
 * @since Created at 2021/5/27 11:44
 */
public class RecordModule {
 
    static NetSDKLib NetSdk = NetSDKLib.NETSDK_INSTANCE;
 
    /**
     * 开始查询文件信息
     *
     * @param m_hLoginHandle 登录句柄
     * @param type           查询类型
     * @param condition      查询条件
     * @param waitTime       超时时间(毫秒)
     * @return 查询句柄
     */
    public static NetSDKLib.LLong FindFileEx(NetSDKLib.LLong m_hLoginHandle, int type, Structure condition, int waitTime) {
 
        condition.write();
        NetSDKLib.LLong m_hFindFileHandle = NetSdk.CLIENT_FindFileEx(m_hLoginHandle, type, condition.getPointer(), null, waitTime);
        condition.read();
        if (m_hFindFileHandle.longValue() == 0) {
            System.err.println("FindFileEx Failed: " + ENUMERROR.getErrorCode());
        }
        return m_hFindFileHandle;
    }
 
    /**
     * 获取文件数量
     *
     * @param m_hFindFileHandle 查询句柄
     * @param waitTime          超时时间(毫秒)
     * @return 失败返回  -1 成功返回 实际数量
     */
    public static int GetTotalFileCount(NetSDKLib.LLong m_hFindFileHandle, int waitTime) {
        IntByReference nCount = new IntByReference(0);
        boolean ret = NetSdk.CLIENT_GetTotalFileCount(m_hFindFileHandle, nCount, null, waitTime);
        if (!ret) {
            System.err.println("GetTotalFileCount Failed:" + ENUMERROR.getErrorCode());
            return -1;
        }
        return nCount.getValue();
    }
 
    /**
     * 获取文件查询信息
     *
     * @param m_hFindFileHandle 查询句柄
     * @param structures        查询信息数组
     * @param memoryHolder      与信息数组大小相同的 Native 内存
     * @param waitTime          超时时间
     * @return 查询到的数量
     */
    public static int FindNextFileEx(NetSDKLib.LLong m_hFindFileHandle, Structure[] structures, Pointer memoryHolder, int waitTime) {
 
        ToolKits.SetStructArrToPointerData(structures, memoryHolder);
        int memorySize = structures[0].size() * structures.length;
        int nRet = NetSdk.CLIENT_FindNextFileEx(m_hFindFileHandle, structures.length, memoryHolder, memorySize, null, waitTime);
        if (nRet < 0) {
            System.err.println("FindNextFileEx Failed:" + ENUMERROR.getErrorCode());
        } else if (nRet > 0) {
            ToolKits.GetPointerDataToStructArr(memoryHolder, structures);
        }
        return nRet;
    }
 
    // FindNextFileEx 查询录像专用
    public static int FindNextMediaFileEx(NetSDKLib.LLong m_hFindFileHandle, NetSDKLib.NET_OUT_MEDIA_QUERY_FILE[] mediaInfos, Pointer memoryHolder, int waitTime) {
        if (mediaInfos == null || mediaInfos.length == 0) return -1;
        long offset = 0;
        int sizeOfStruct = mediaInfos[0].dwSize;
        for (NetSDKLib.NET_OUT_MEDIA_QUERY_FILE mediaInfo : mediaInfos) {
            mediaInfo.writeField("dwSize");
            memoryHolder.write(offset, mediaInfo.getPointer().getByteArray(0, sizeOfStruct), 0, sizeOfStruct);
            offset += sizeOfStruct;
        }
 
        int memorySize = sizeOfStruct * mediaInfos.length;
        int nRet = NetSdk.CLIENT_FindNextFileEx(m_hFindFileHandle, mediaInfos.length, memoryHolder, memorySize, null, waitTime);
        if (nRet < 0) {
            System.err.println("FindNextFileEx Failed:" + ENUMERROR.getErrorCode());
        } else if (nRet > 0) {
            GetMediaInfosFromPointer(mediaInfos, memoryHolder);
        }
        return nRet;
    }
 
    // 选择性读取
    private static void GetMediaInfosFromPointer(NetSDKLib.NET_OUT_MEDIA_QUERY_FILE[] mediaInfos, Pointer bufferData) {
        if (mediaInfos == null || mediaInfos.length == 0) return;
        long offset = 0;
        int sizeOfStruct = mediaInfos[0].size();
        Pointer tmpBuffer = new Memory(sizeOfStruct);
        for (NetSDKLib.NET_OUT_MEDIA_QUERY_FILE info : mediaInfos) {
            tmpBuffer.clear(sizeOfStruct);
            tmpBuffer.write(0, bufferData.getByteArray(offset, sizeOfStruct), 0, sizeOfStruct);
            offset += sizeOfStruct;
 
            NetSDKLib.NET_OUT_MEDIA_QUERY_FILE mediaInfo = info;
            if (mediaInfo == null) mediaInfo = new NetSDKLib.NET_OUT_MEDIA_QUERY_FILE();
 
            // todo 参考下面的方式自由添加字段
            mediaInfo.nChannelID = (int) GetSelectedSingleFieldValue("nChannelID", mediaInfo, tmpBuffer);
            mediaInfo.stuStartTime = (NetSDKLib.NET_TIME) GetSelectedSingleFieldValue("stuStartTime", mediaInfo, tmpBuffer);
            mediaInfo.stuEndTime = (NetSDKLib.NET_TIME) GetSelectedSingleFieldValue("stuEndTime", mediaInfo, tmpBuffer);
            mediaInfo.byFileType = (byte) GetSelectedSingleFieldValue("byFileType", mediaInfo, tmpBuffer);
            mediaInfo.byPartition = (byte) GetSelectedSingleFieldValue("byPartition", mediaInfo, tmpBuffer);
            mediaInfo.byVideoStream = (byte) GetSelectedSingleFieldValue("byVideoStream", mediaInfo, tmpBuffer);
            mediaInfo.nCluster = (int) GetSelectedSingleFieldValue("nCluster", mediaInfo, tmpBuffer);
            mediaInfo.szFilePath = (byte[]) GetSelectedSingleFieldValue("szFilePath", mediaInfo, tmpBuffer);
            mediaInfo.nDriveNo = (int) GetSelectedSingleFieldValue("nDriveNo", mediaInfo, tmpBuffer);
            mediaInfo.nFileSizeEx = (long) GetSelectedSingleFieldValue("nFileSizeEx", mediaInfo, tmpBuffer);
            mediaInfo.nTotalFrame = (int) GetSelectedSingleFieldValue("nTotalFrame", mediaInfo, tmpBuffer);
            mediaInfo.emFileState = (int) GetSelectedSingleFieldValue("emFileState", mediaInfo, tmpBuffer);
        }
    }
 
    /**
     * 结束查询
     *
     * @param m_hFindFileHandle 查询句柄
     */
    public static boolean FindCloseEx(NetSDKLib.LLong m_hFindFileHandle) {
        if (m_hFindFileHandle.longValue() == 0) {
            System.err.println("FindFileHandle invalid");
            return false;
        }
 
        if (!NetSdk.CLIENT_FindCloseEx(m_hFindFileHandle)) {
            System.err.println("FindCloseEx Failed:" + ENUMERROR.getErrorCode());
            return false;
        } else {
            m_hFindFileHandle.setValue(0);
            return true;
        }
    }
 
    /**
     * 查询录像文件
     *
     * @return 查询到的文件数量 -1 表示失败
     */
    public static int QueryRecordFileByTimeLimit(NetSDKLib.LLong m_hLoginHandle, int maxFileCount, Structure[] fileInfos,
                                                 NetSDKLib.NET_TIME stTimeStart, NetSDKLib.NET_TIME stTimeEnd) {
        int bufferSize = maxFileCount * fileInfos[0].size();
        IntByReference retCountRef = new IntByReference(0);
        Pointer pFileInfo = new Memory(bufferSize);
        ToolKits.SetStructArrToPointerData(fileInfos, pFileInfo);
 
        // channel 查全部
        boolean ret = NetSdk.CLIENT_QueryRecordFile(m_hLoginHandle, -1, 0,
                stTimeStart, stTimeEnd, null, pFileInfo, bufferSize, retCountRef, 5000, false);
        if (!ret) {
            System.err.println("QueryRecordFile  Failed!" + ENUMERROR.getErrorCode());
            return -1;
        }
        ToolKits.GetPointerDataToStructArr(pFileInfo, fileInfos);
        return retCountRef.getValue();
    }
 
    /**
     * 按文件下载录像
     *
     * @return 下载句柄
     */
    public static NetSDKLib.LLong DownloadRecordFileByFileEx(NetSDKLib.LLong m_hLoginHandle,
                                                             NetSDKLib.NET_RECORDFILE_INFO fileInfo, String savePath,
                                                             NetSDKLib.fTimeDownLoadPosCallBack posCallBack, NetSDKLib.fDataCallBack dataCallBack) {
        Pointer pSavePath = new NativeString(savePath).getPointer();
 
        NetSDKLib.LLong m_hDownLoadHandle = NetSdk.CLIENT_DownloadByRecordFileEx(
                m_hLoginHandle, fileInfo, pSavePath, posCallBack, null, dataCallBack, null, null);
        if (m_hDownLoadHandle.longValue() == 0) {
            System.err.println("DownloadRecordFileByFileEx: " + ENUMERROR.getErrorCode());
        }
        return m_hDownLoadHandle;
    }
 
    /**
     * 按文件下载录像 sdk内部缓存控制自适应
     *
     * @return 下载句柄
     */
    public static NetSDKLib.LLong DownloadRecordFileWithSelfAdapt(NetSDKLib.LLong m_hLoginHandle,
                                                                  NET_IN_DOWNLOAD_BYFILE_SELFADAPT stuParamIn,
                                                                  NET_OUT_DOWNLOAD_BYFILE_SELFADAPT stuParamOut,
                                                                  int waitTime) {
        stuParamIn.write();
        stuParamOut.write();
        NetSDKLib.LLong m_hDownLoadHandle = NetSdk.CLIENT_DownloadByFileSelfAdapt(
                m_hLoginHandle, stuParamIn.getPointer(), stuParamOut.getPointer(), waitTime);
        if (m_hDownLoadHandle.longValue() == 0) {
            System.err.println("DownloadRecordFileWithSelfAdapt: " + ENUMERROR.getErrorCode());
        }
        return m_hDownLoadHandle;
    }
 
    /**
     * 按时间下载录像 sdk内部缓存控制自适应
     *
     * @return 下载句柄
     */
    public static NetSDKLib.LLong DownloadRecordAdaptiveDownloadByTime(NetSDKLib.LLong m_hLoginHandle,
                                                                       NET_IN_ADAPTIVE_DOWNLOAD_BY_TIME stuParamIn,
                                                                       NET_OUT_ADAPTIVE_DOWNLOAD_BY_TIME stuParamOut,
                                                                       int waitTime) {
        stuParamIn.write();
        stuParamOut.write();
        NetSDKLib.LLong m_hDownLoadHandle = NetSdk.CLIENT_AdaptiveDownloadByTime(
                m_hLoginHandle, stuParamIn.getPointer(), stuParamOut.getPointer(), waitTime);
        if (m_hDownLoadHandle.longValue() == 0) {
            System.err.println("DownloadRecordFileWithSelfAdapt: " + ENUMERROR.getErrorCode());
        }
        return m_hDownLoadHandle;
    }
 
    /**
     * 停止下载录像
     *
     * @param m_hDownLoadHandle 下载句柄
     */
    public static boolean StopDownLoadRecordFile(NetSDKLib.LLong m_hDownLoadHandle) {
 
        if (m_hDownLoadHandle.longValue() == 0) {
            System.err.println("DownLoadHandle invalid");
            return false;
        }
        if (!NetSdk.CLIENT_StopDownload(m_hDownLoadHandle)) {
            System.err.println("Stop DownLoad Failed:" + ENUMERROR.getErrorCode());
            return false;
        } else {
            System.out.println("Stop DownLoad Succeed.");
            m_hDownLoadHandle.setValue(0);
            return true;
        }
    }
}