下载指定时间段视频信息
-1 初始化
-2 加密密码
-3 登录
*4 启动播放器
*5 查询录像文件
6 获取录像文件的url
7 打开下载媒体流
8 设置下载媒体流厂商标志
9 开始下载媒体流
10 停止下载媒体流
11 停止播放器
-12 注销用户
-13 释放imosSDK资源
//4 启动播放器
ulRet = IMOSSDK.IMOS_StartPlayer(ref stLoginInfo.stUserLoginIDInfo,//用户标识
25,//播放窗格数
ptrPlayWndInfo);//播放窗格编码结构体指针
//5 分页检索录像
ulRet = IMOSSDK.IMOS_RecordRetrieval(ref stLoginInfo.stUserLoginIDInfo,//用户标识
ref stRecQueryInfo, //回放检索消息数据结构
ref stPageInfo,//请求分页信息
ptrRspPage,//响应分页信息
ptrRecFileList);//录像文件信息列表
/// <summary>
/// 下载指定时间段的视频
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBeginDownLoad_Click(object sender, EventArgs e)
{
try
{
//录像下载类
DownloadFileInfo downLoadFile = new DownloadFileInfo();
List<RECORD_FILE_INFO_S> recFileList = (List<RECORD_FILE_INFO_S>)dgvVodInfo.Tag;
dtBegin = dtpBeginDate.Value.Date + dtpBeginTime.Value.TimeOfDay;//时间起
dtEnd = dtpEndDate.Value.Date + dtpEndTime.Value.TimeOfDay;//时间止
string strBegin = dtBegin.ToString(m_strDateFormat);
string strEnd = dtEnd.ToString(m_strDateFormat);
//批量下载
if (null != recFileList)
{
if (dtBegin < dtEnd)//开始时间小于结束时间
{
//如果路径不存在,那么创建路径
if (false == Directory.Exists(LocalConfig.vodDownloadLoc))//回放下载地址
{
Directory.CreateDirectory(LocalConfig.vodDownloadLoc);//创建目录
}
//加放下载
downloadID = sdkManager.startDownLoad(recFileList[0],//录像信息
Encoding.UTF8.GetBytes(CameraCode),//摄像机编号
LocalConfig.vodProtocol,//播放器所使用协议
LocalConfig.vodDownloadLoc,//文件下载存储位置
LocalConfig.downloadSpd, //媒体下载速度
LocalConfig.downloadFormat, //录像的下载格式0:ts 1:flv
dtBegin,//开始时间
dtEnd);//结束时间
//存储此次下载的信息
downLoadFile.downloadID = downloadID;
downLoadFile.downloadPercet = 0;
downLoadFile.startTime = dtBegin;
downLoadFile.endTime = dtEnd;
}
else
{
MessageBox.Show("下载时间选择错误!请重新选择!");
}
if (null == downloadID)
{
MessageBox.Show("下载失败!");
}
else
{
lock(synRoot)
{
//将欲下载的文件加入下载面板
DataRow dr = dt.NewRow();
dr["开始时间"] = strBegin;
dr["结束时间"] = strEnd;
dr["下载进度"] = "0";
dt.Rows.Add(dr);
downloadIndex++;
dgvVodInfo.Rows[downloadIndex].Tag = downLoadFile;
}
}
}
else
{
MessageBox.Show("没有录像信息!");
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 回放下载
/// </summary>
/// <param name="fileInfo">录像信息</param>
/// <param name="camCode">摄像机编号</param>
/// <param name="vodProtocol">播放器所使用协议</param>
/// <param name="downloadLoc">用户下载存储位置</param>
/// <param name="speed">媒体数据下载速度</param>
/// <param name="downloadFormat">视频下载格式0:ts 1:flv</param>
/// <param name="beginTime">开始时间</param>
/// <param name="endTime">结束时间</param>
/// <returns></returns>
public byte[] startDownLoad(RECORD_FILE_INFO_S fileInfo, byte[] camCode, XP_PROTOCOL_E vodProtocol,String downloadLoc, XP_DOWN_MEDIA_SPEED_E speed, uint downloadFormat, DateTime beginTime, DateTime endTime)
{
UInt32 ulRet = 0;
IntPtr ptrSDKURLInfo = IntPtr.Zero;
//IntPtr pcChannelCode = IntPtr.Zero;
try
{
GET_URL_INFO_S getUrlInfo = new GET_URL_INFO_S();//录像文件详情
TIME_SLICE_S timeSlice = new TIME_SLICE_S();//时间段
URL_INFO_S urlInfo = new URL_INFO_S();//url详情
//开始时间
byte[] begin = new byte[IMOSSDK.IMOS_TIME_LEN];
String strBeginTime = beginTime.ToString("yyyy-MM-dd HH:mm:ss");
Encoding.UTF8.GetBytes(strBeginTime).CopyTo(begin, 0);
//结束时间
byte[] end = new byte[IMOSSDK.IMOS_TIME_LEN];
String strEndTime = endTime.ToString("yyyy-MM-dd HH:mm:ss");
Encoding.UTF8.GetBytes(strEndTime).CopyTo(end, 0);
timeSlice.szBeginTime = begin;
timeSlice.szEndTime = end;
getUrlInfo.szCamCode = camCode;
getUrlInfo.szFileName = fileInfo.szFileName;
getUrlInfo.stRecTimeSlice = timeSlice;
getUrlInfo.szClientIp = stLoginInfo.stUserLoginIDInfo.szUserIpAddress;
//ptrSDKURLInfo = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(URL_INFO_S)));
//这个下载通道号,是录像下载的唯一标志,以后查询录像都要用到这个通道号
byte[] byPcChannel = new byte[IMOSSDK.IMOS_RES_CODE_LEN];
//6 获取录像文件的url位置
ulRet = IMOSSDK.IMOS_GetRecordFileURL(ref stLoginInfo.stUserLoginIDInfo,//用户的id信息
ref getUrlInfo, //获取录像文件的url
ref urlInfo);//url信息
//URL_INFO_S URLInfo = (URL_INFO_S)Marshal.PtrToStructure(ptrSDKURLInfo, typeof(URL_INFO_S));
byte[] pcFileName = Encoding.UTF8.GetBytes(downloadLoc);
//7 打开媒体流下载,保存为指定格式
ulRet = IMOSSDK.IMOS_OpenDownload(ref stLoginInfo.stUserLoginIDInfo,//用户登录id信息
urlInfo.szURL,//下载录像文件的utl
urlInfo.stVodSeverIP.szServerIp,//流控服务器的ip
urlInfo.stVodSeverIP.usServerPort,//流控服务器的端口号
(uint)vodProtocol,//接收媒体流的协议类型
(uint)speed,//媒体数据下载速度
pcFileName,//下载后保存本地的文件路径
downloadFormat,//下载保存文件的格式
byPcChannel);//下载资源编码
//8 设置下载媒体流厂商标志
ulRet = IMOSSDK.IMOS_SetDecoderTag(ref stLoginInfo.stUserLoginIDInfo,//用户标识
byPcChannel,//播放通道编码
urlInfo.szDecoderTag);//通道解码器的厂商标识
//9 开始下载,首先要打开媒体流下载
ulRet = IMOSSDK.IMOS_StartDownload(ref stLoginInfo.stUserLoginIDInfo,//用户标识
byPcChannel);//下载资源编码
return byPcChannel;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
finally
{
Marshal.FreeHGlobal(ptrSDKURLInfo);
}
}
正常下载完成,会通过消息回调函数ERR_XP_RTSP_COMPLETE上报下载完成消息,不需要手动调用IMOS_StopDownload,
注:除了ERR_XP_DISK_CAPACITY_WARN之外,其他情况都会自动关闭
//设置回调函数
uint ulResult = IMOSSDK.IMOS_SetRunMsgCB(ptrCallbakc);
if (0 != ulResult)
{
//Log.Write.Error("IMOS_SetRunMsgCB fail errorCode:" + ulResult);
MessageBox.Show("IMOS_SetRunMsgCB fail errorCode:" + ulResult);
}
/// <summary>
/// XP回调处理函数
/// </summary>
/// <param name="stUserLoginIDInfo"></param>
/// <param name="ulRunInfoType"></param>
/// <param name="ptrInfo"></param>
public void XpInfoRush(ref USER_LOGIN_ID_INFO_S stUserLoginIDInfo, uint ulRunInfoType, IntPtr ptrInfo)
{
if (ulRunInfoType == (uint)XP_RUN_INFO_TYPE_E.XP_RUN_INFO_DOWN_RTSP_PROTOCOL)
{
//下载rtsp消息
try
{
XP_RUN_INFO_EX_S stDownLoadInfo = (XP_RUN_INFO_EX_S)Marshal.PtrToStructure(ptrInfo, typeof(XP_RUN_INFO_EX_S));
//string szDownLoadId = Encoding.UTF8.GetString(stDownLoadInfo.szDownID);
string szErrorInfo = string.Empty;
//因为除了ERR_XP_DISK_CAPACITY_WARN之外其他都会自动关闭下载
if (stDownLoadInfo.ulErrCode != IMOSSDK.ERR_XP_DISK_CAPACITY_WARN)
{
if (stDownLoadInfo.ulErrCode != IMOSSDK.ERR_XP_RTSP_COMPLETE)
{
MessageBox.Show("下载异常关闭!");
//Log.Write.Error("下载通道[" + szDownLoadId.TrimEnd('\0') + "]异常关闭,errcode:" + stDownLoadInfo.ulErrCode + " " + szErrorInfo);
}
//下载完成后将进度更新为100,再从datagridview中移除此列
lock (synRoot)
{
DataGridViewRowCollection viewRowCollection = dgvVodInfo.Rows;
foreach (DataGridViewRow dgvr in viewRowCollection)
{
DownloadFileInfo downLoadFileInfo = (DownloadFileInfo)dgvr.Tag;
if (Encoding.UTF8.GetString(downLoadFileInfo.downloadID) == Encoding.UTF8.GetString(stDownLoadInfo.szPortCode))
{
dgvr.Cells["下载进度"].Value = 100;
MessageBox.Show("录像下载完成!录像被下载到:" + LocalConfig.vodDownloadLoc);
removeIndex.Add(dgvr.Index);
continue;
}
}
}
//遍历删除下载成功的记录
lock (synRoot)
{
foreach (int index in removeIndex)
{
dgvVodInfo.Rows.RemoveAt(index);
downloadIndex--;
}
//刷新datagridview
dgvVodInfo.Invalidate();
removeIndex.Clear();
}
}
}
catch (Exception err)
{
//Log.Write.Error("DownLoadComplete Exception", err);
}
}
}
调用sdk中的方法
4
/// <summary>
/// 启动播放器
/// </summary>
/// <param name="stUserLoginInfo">用户登录标识</param>
/// <param name="ulPlayWndNum">播放窗格数量</param>
/// <param name="ptrPlayWndInfo">播放窗格编码结构体指针</param>
/// <returns></returns>
[DllImport("imos_sdk.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern UInt32 IMOS_StartPlayer(ref USER_LOGIN_ID_INFO_S stUserLoginInfo, UInt32 ulPlayWndNum, IntPtr ptrPlayWndInfo);
5
/// <summary>
/// 分页检索录像
/// </summary>
/// <param name="stUserLoginInfo">用户标识</param>
/// <param name="stSDKRecQueryInfo">加放检索消息数据结构</param>
/// <param name="stQueryPageInfo">请求分页信息</param>
/// <param name="ptrRspPage">响应分页信息</param>
/// <param name="ptrSDKRecordFileInfo">录像文件信息列表</param>
/// <returns></returns>
[DllImport("imos_sdk.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern UInt32 IMOS_RecordRetrieval(ref USER_LOGIN_ID_INFO_S stUserLoginInfo, ref REC_QUERY_INFO_S stSDKRecQueryInfo, ref QUERY_PAGE_INFO_S stQueryPageInfo, IntPtr ptrRspPage, IntPtr ptrSDKRecordFileInfo);
6
/// <summary>
/// 获取录像文件的url信息
/// </summary>
/// <param name="stUserLoginInfo"></param>
/// <param name="stSDKGetUrlInfo"></param>
/// <param name="stUrlInfo"></param>
/// <returns></returns>
[DllImport("imos_sdk.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern UInt32 IMOS_GetRecordFileURL(ref USER_LOGIN_ID_INFO_S stUserLoginInfo, ref GET_URL_INFO_S stSDKGetUrlInfo, ref URL_INFO_S stUrlInfo);
7
/// <summary>
/// 打开媒体流下载
/// </summary>
/// <param name="pstUserLoginIDInfo">用户登录标识</param>
/// <param name="pcDownUrl">要下载录像文件的url</param>
/// <param name="pcServerIP">媒体流控服务器的ip</param>
/// <param name="usServerPort">媒体流控服务器的端口号</param>
/// <param name="ulProtl">接收媒体流的协议类型 即播放器所用协议(cp_protoclo_e)</param>
/// <param name="ulDownMediaSpeed">媒体数据下载速度</param>
/// <param name="pcFileName">下载后保存本地的文件路径(不含文件名,路径后面不带斜杠)</param>
/// <param name="ulFileFormat">下载保存文件的格式</param>
/// <param name="pcChannelCode">下载资源编码</param>
/// <returns></returns>
[DllImport("xp_frame.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern UInt32 IMOS_OpenDownload(ref USER_LOGIN_ID_INFO_S pstUserLoginIDInfo, byte[] pcDownUrl, byte[] pcServerIP, ushort usServerPort, UInt32 ulProtl, UInt32 ulDownMediaSpeed, byte[] pcFileName, UInt32 ulFileFormat, byte[] pcChannelCode);
8
/// <summary>
/// 设置通道解码器的厂商标识
/// </summary>
/// <param name="pstUserLoginIDInfo">用户登录信息id</param>
/// <param name="pcChannelCode">播放通道编码</param>
/// <param name="pcDecorderTag">通道解码器的厂商标识</param>
/// <returns></returns>
[DllImport("xp_frame.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern UInt32 IMOS_SetDecoderTag(ref USER_LOGIN_ID_INFO_S pstUserLoginIDInfo, byte[] pcChannelCode, byte[] pcDecorderTag);
9
/// <summary>
/// 开始下载,首先要打开媒体流下载
/// </summary>
/// <param name="pstUserLoginIDInfo">用户信息</param>
/// <param name="pcChannelCode">下载资源编码</param>
/// <returns></returns>
[DllImport("xp_frame.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern UInt32 IMOS_StartDownload(ref USER_LOGIN_ID_INFO_S pstUserLoginIDInfo, byte[] pcChannelCode);
/// <summary>
/// 停止下载媒体流
/// </summary>
/// <param name="pstUserLoginIDInfo"></param>
/// <param name="pcDownloadID"></param>
/// <returns></returns>
[DllImport("xp_frame.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern UInt32 IMOS_StopDownload(ref USER_LOGIN_ID_INFO_S pstUserLoginIDInfo, byte[] pcDownloadID);
/// <summary>
/// 设置接收消息或者异常消息的回调函数
/// </summary>
/// <param name="ptrRunInfoFunc">视频显示模式</param>
/// <returns></returns>
[DllImport("xp_frame.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern UInt32 IMOS_SetRunMsgCB(IntPtr ptrRunInfoFunc);