乐乐随笔

绿叶

BlogJava 首页 新随笔 联系 聚合 管理
  14 Posts :: 0 Stories :: 2 Comments :: 0 Trackbacks

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;


//本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/doubaijun/archive/2009/04/07/4053868.aspx

namespace HIKLib
{
    interface  IHikClientAdviseSink
    {
        /******************************************************************
        在Setup时被调用,获取总的播放长度.nLength为总的播放长度,以1/64秒为单位
        */
        //public abstract int OnPosLength(int nLength);

        /******************************************************************
         在Setup后被调用,表示URL已经被成功打开,sucess为1表示成功,0表示失败
        */
        //public abstract int OnPresentationOpened(int success);

        /************************************************************************
        在Player被停止销毁后调用
        */
        //public abstract int OnPresentationClosed();

        /************************************************************************
        未使用
        */
        //public abstract int OnPreSeek(uint uOldTime, uint uNewTime);

        /************************************************************************
        未使用
        */
        //public abstract int OnPostSeek(uint uOldTime, uint uNewTime);

        /************************************************************************
        未使用
        */
        //public abstract int OnStop();

        /************************************************************************
        在Pause时被调用,uTime目前都是0
        */
       // public abstract int OnPause(uint uTime);

        /************************************************************************
         在开始播放时调用,uTime目前都是0
         */
        //public abstract int OnBegin(uint uTime);

        /************************************************************************
        在随机播放时调用,uTime目前都是0
        */
        //public abstract int OnRandomBegin(uint uTime);

        /************************************************************************
         在Setup前调用,pszHost表示正在连接的服务器
         */
        //public abstract int OnContacting(string pszHost);

        /************************************************************************
        在服务器端返回出错信息是调用, pError中为出错信息内容
        */
       // public abstract int OnPutErrorMsg(string pError);

        /************************************************************************
        未使用
         */
       // public abstract int OnBuffering(uint uFlag, uint uPercentComplete);

        //public abstract int OnChangeRate(int flag);

        //public abstract int OnDisconnect();

      
    /******************************************************************
 在Setup时被调用,获取总的播放长度.nLength为总的播放长度,以1/64秒为单位
 */
         int OnPosLength(ulong nLength);

 /******************************************************************
     在Setup后被调用,表示URL已经被成功打开,sucess为1表示成功,0表示失败
 */
        int OnPresentationOpened(int success);

  /************************************************************************
     在Player被停止销毁后调用
     */
       int OnPresentationClosed();

  /************************************************************************
     未使用
     */
        int OnPreSeek(ulong uOldTime, ulong uNewTime);

  /************************************************************************
     未使用
     */
       int OnPostSeek(ulong uOldTime, ulong uNewTime);

  /************************************************************************
     未使用
  */
        int OnStop();

  /************************************************************************
     在Pause时被调用,uTime目前都是0
     */
         int OnPause(ulong uTime);

 /************************************************************************
     在开始播放时调用,uTime目前都是0
     */
         int OnBegin(ulong uTime);

     /************************************************************************
     在随机播放时调用,uTime目前都是0
     */
         int OnRandomBegin(ulong uTime);

 /************************************************************************
     在Setup前调用,pszHost表示正在连接的服务器
     */
         int OnContacting(string pszHost);
   
 /************************************************************************
 在服务器端返回出错信息是调用, pError中为出错信息内容
 */
       int OnPutErrorMsg(string pError);
 
 /************************************************************************
    未使用
     */
        int OnBuffering(uint uFlag, ushort uPercentComplete);

        int OnChangeRate(int flag);

         int OnDisconnect();
    };

 

    public delegate int pDataRec(int sid, int iusrdata, int idatatype, StringBuilder pdata, int ilen);
    public delegate int pMsgBack(int sid, int opt, int param1, int param2);

 


    class hkClient
    {

        [DllImport("client.dll", SetLastError = true)]
        public static extern int HIKS_CreatePlayer(IHikClientAdviseSink pSink, IntPtr pWndSiteHandle, pDataRec pRecFunc, pMsgBack pMsgFunc, int TransMethod);

        [DllImport("client.dll", SetLastError = true)]
        public static extern int InitStreamClientLib();

        [DllImport("client.dll")]
        public static extern int FiniStreamClientLib();

        [DllImport("client.dll", SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern int HIKS_OpenURL(int hSession, string pszURL, int iusrdata);

        [DllImport("client.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int HIKS_Play(int hSession);
        [DllImport("client.dll")]
        public static extern int HIKS_RandomPlay(int hSession, int timepos);
        [DllImport("client.dll")]
        public static extern int HIKS_Pause(int hSession);
        [DllImport("client.dll")]
        public static extern int HIKS_Resume(int hSession);
        [DllImport("client.dll")]
        public static extern int HIKS_Stop(int hSession);
        [DllImport("client.dll")]
        public static extern int HIKS_GetCurTime(int hSession, ref  ushort utime);
        [DllImport("client.dll")]
        public static extern int HIKS_ChangeRate(int hSession, int scale);
        [DllImport("client.dll")]
        public static extern int HIKS_Destroy(int hSession);
        [DllImport("client.dll")]
        public static extern int HIKS_GetVideoParams(int hSession, ref  int ibri, ref int icon, ref int isat, ref int ihue);
        [DllImport("client.dll")]
        public static extern int HIKS_SetVideoParams(int hSession, int ibri, int icon, int isat, int ihue);
        [DllImport("client.dll")]
        public static extern int HIKS_PTZControl(int hSession, ushort ucommand, int iparam1, int iparam2, int iparam3, int iparam4);
        [DllImport("client.dll")]
        public static extern int HIKS_SetVolume(int hSession, ushort volume);
        [DllImport("client.dll")]
        public static extern int HIKS_OpenSound(int hSession, bool bExclusive);
        [DllImport("client.dll")]
        public static extern int HIKS_CloseSound(int hSession);
        [DllImport("client.dll")]
        public static extern int HIKS_ThrowBFrameNum(int hSession, uint nNum);
        [DllImport("client.dll")]
        public static extern int HIKS_GrabPic(int hSession, string szPicFileName, ushort byPicType);


    }

}




using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using vvmc.util;
using System.Threading;

//本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/doubaijun/archive/2009/04/07/4053868.aspx
// C# 流媒体服务器客户端封装

namespace HIKLib
{
    class StreamClient : IHikClientAdviseSink
    {
        // private inheritAdviseSink adviseSink;
        /// <summary>
        /// 播放的总时间
        /// </summary>
        private int m_totaltime;
        /// <summary>
        /// url是否被打开
        /// </summary>
        private int m_opened=-1;
        private void setOpened(int openedvalue) {
            lock (this)
            {
                this.m_opened = openedvalue;
            }
        }
        private int m_Pause = -1;

        /// <summary>
        /// 播放句柄
        /// </summary>
        private int playSession = -1;
        public StreamClient()
        {
              int ok;
             ok= hkClient.InitStreamClientLib();
             if (ok == -1)
             {
                 //MessageBox.Show("初始化失败!");
                 LogManager.WriteLog(LogFile.Error, "初始化流媒体库失败!");
             }
             Console.WriteLine("----------->初始化流媒体库,返回值:{0}",ok);


        }
        /// <summary>
        /// 关闭是调用
        /// </summary>
        public void FiniStreamClientLib()
        {
            hkClient.FiniStreamClientLib();
        }

        /// <summary>
        /// 开始之前调用
        /// </summary>
        public int CreatePlayer(IntPtr mHand)
        {
            pMsgBack mb = new pMsgBack(MsgBack);
            //pDataRec dr = new pDataRec(DataRec);
            playSession = hkClient.HIKS_CreatePlayer(null, mHand, null, mb, 1);
            GC.KeepAlive(mb);
            //GC.KeepAlive(dr);
            if (playSession == -1)
            {
                LogManager.WriteLog(LogFile.Error, "创建play句柄失败!");
            }

            return playSession;

        }


        /// <summary>
        /// 根据URL,连接服务器
        /// </summary>
        /// <param name="hSession">。hSession 是Player 的标示,应该设置为HIKS_CreatePlayer 成功返回的新建Player 的标示</param>
        /// <param name="pszURL">pszURL 即为服务器的地址</param>
        /// <param name="iusrdata">iusrdata 为用户数据。</param>
        /// <returns>返回值:成功返回 1,失败返回-1。</returns>
        public int HIKS_OpenURL(string pszURL, int iusrdata)
        {
            int ok=-1;
            ///qq176122348
            ///
            try
            {
                int icount = 0;
                /**
                while (this.m_opened == 0&&icount<10) {
                    Console.WriteLine("等待直到上个连接打开成功!");
                    Thread.Sleep(100);
                    icount++;
                }
                 */
                lock (this)
                {
                    Console.WriteLine("等待结束时,opened={0},pause={1},threadId:{2}", this.m_opened, this.m_Pause, Thread.CurrentThread.ManagedThreadId);
                    this.setOpened(0);
                    this.m_Pause = 0;
                    ok = hkClient.HIKS_OpenURL(playSession, pszURL, iusrdata);
                    if (ok == -1)
                    {
                        LogManager.WriteLog(LogFile.Error, "连接服务器失败!");
                    }
                    else
                    {
                        LogManager.WriteLog(LogFile.Error, "连接服务器成功!");
                    }
                }
            }catch(Exception ex){
                LogManager.WriteLog(LogFile.Error, "HIKS_OpenURL,异常原因:"+ex.Message);
            }
            return ok;


        }

        public int DataRec(int sid, int iusrdata, int idatatype, StringBuilder pdata, int ilen) {
           
            LogManager.WriteLog(LogFile.Error, "接收到数据.........");
            return 0;
        }

        private int  MsgBack(int sid, int opt, int param1, int param2)
        {

            Console.WriteLine("msgBack:opt={0},param1={1},param2={2}",opt,param1,param2);
            int len = -1;
            switch (opt)
            {
                case 1:
                    m_totaltime = param1;
                    break;
                case 2:
                    //m_opened = param1;
                    this.setOpened(param1);
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                case 7://暂停
                    this.m_Pause = 1;

                    break;
                case 8:
                    break;
                case 9:
                    break;
                case 10:
                    break;
                case 11:
                    break;
                case 12:
                    break;
                case 13:
                    //m_speeded = true;
                    break;
                case 14:
                    //PostMessage(WM_DISCONNECT,0,0);
                    break;
                default:
                    break;
            }
            return 0;
        }

        /// <summary>
        /// 播放
        /// </summary>
        /// <returns></returns>
        public int HIKS_Play()
        {
            int ok = hkClient.HIKS_Play(playSession);
            if (ok == -1)
            {
                //MessageBox.Show("播放失败!");
                LogManager.WriteLog(LogFile.Error, "Play播放失败!");
             

            }
            else
            {
                LogManager.WriteLog(LogFile.Error, "Play播放成功!");
             
            }

            return ok;

        }
        /// <summary>
        /// 停止播放
        /// </summary>
        /// <returns></returns>
        public int HIKS_Pause()
        {
          
           int ok= hkClient.HIKS_Pause(playSession);
           if (ok == -1)
           {
               LogManager.WriteLog(LogFile.Error, "暂停播放失败!");

           }
           else
           {

               LogManager.WriteLog(LogFile.Error, "暂停播放成功!");
           }

           return ok;

        }

        public int HIKS_Resume()
        {
            int ok = hkClient.HIKS_Resume(playSession);
            if (ok == -1)
            {
                LogManager.WriteLog(LogFile.Error, "恢复播放失败!");

            }
            else
            {

                LogManager.WriteLog(LogFile.Error, "恢复播放成功!");
            }

            return ok;

        }
        public int HIKS_Stop()
        {
            int ok = hkClient.HIKS_Stop(playSession);
            if (ok == -1)
            {
   
                LogManager.WriteLog(LogFile.Error, "停止播放失败!");

            }
            else
            {
                //log.Writelog("停止播放成功!");
            }

            return ok;

        }
        public int HIKS_GetCurTime(ref  ushort utime)
        {
            int ok = hkClient.HIKS_GetCurTime(playSession, ref utime);


            if (ok == -1)
            {
              
                LogManager.WriteLog(LogFile.Error, "获取播放时间失败!");
             

            }
            else
            {

                //log.Writelog("获取播放时间成功!");
            }

            return ok;

        }
        /// <summary>
        /// 作用:改变播放速率,在快进或慢进的时候用。
        /// </summary>
        /// <param name="scale"></param>
        /// <returns>返回值:成功返回 0,失败返回-1。</returns>
        public int HIKS_ChangeRate(int scale)
        {
            int ok = hkClient.HIKS_ChangeRate(playSession,2);


            if (ok == -1)
            {
                MessageBox.Show("改变速度失败!");

               // log.Writelog("改变速度失败!");

            }
            else
            {

                //log.Writelog("改变速度成功!");
            }

            return ok;

        }
        /// <summary>
        /// 作用:销毁Player,只在HIKS_OpenURL 函数失败的请况下调用。
        /// </summary>
        /// <returns>返回值:成功返回 0,失败返回-1。</returns>
        public int HIKS_Destroy()
        {
            int ok = hkClient.HIKS_Destroy(playSession);


            if (ok == -1)
            {
             
                LogManager.WriteLog(LogFile.Error, "销毁失败!");

            }
            else
            {

               // log.Writelog("销毁成功!");
            }

            return ok;

        }

        /// <summary>
        /// 声音控制
        /// </summary>
        /// <param name="volume"></param>
        /// <returns></returns>
        public int HIKS_SetVolume(ushort volume)
        {

            return hkClient.HIKS_SetVolume(playSession, volume);
        }

        /// <summary>
        /// 作用:打开声音播放。hSession 是Player 的标示。bExclusive 表示打开声音
        //的方式,false 表示共享打开,true 表示独占打开。
        /// </summary>
        /// <param name="bExclusive"></param>
        /// <returns></returns>
        public int HIKS_OpenSound(bool bExclusive)
        {
            return hkClient.HIKS_OpenSound(playSession, bExclusive);

        }
        /// <summary>
        ///关闭声音
        /// </summary>
        /// <returns></returns>
        public int HIKS_CloseSound()
        {
            return hkClient.HIKS_CloseSound(playSession);

        }
        /// <summary>
        /// :本地抓图。hSession 表示Player 的标示。szPicFileName 表示文件名。
        //byPicType 表示图片的类型,0 表示bmp 图片,1 表示jpeg 图片。
        /// </summary>
        /// <param name="szPicFileName"></param>
        /// <param name="byPicType"></param>
        /// <returns></returns>
        public int HIKS_GrabPic(string szPicFileName, ushort byPicType)
        {
            Random ra=new Random();
            return hkClient.HIKS_GrabPic(playSession,System.DateTime.Now.Date.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + ra.Next().ToString(), 0);

        }

        /// <summary>
        /// 作用:获取视频参数。hSession 是Player 的标示
        /// </summary>
        /// <param name="ibri">是亮度</param>
        /// <param name="icon">是对比度</param>
        /// <param name="isat">是饱和度</param>
        /// <param name="ihue">是色调</param>
        /// <returns>成功返回 0,失败返回-1。</returns>
        public int HIKS_GetVideoParams(ref  int ibri, ref int icon, ref int isat, ref int ihue)
        {

            return hkClient.HIKS_GetVideoParams(playSession,ref  ibri, ref icon, ref isat, ref ihue);
        }
        /// <summary>
        /// 作用:设置视频参数。hSession 是Player 的标示, ibri 是亮度,icon 是对比度,
        ///值域范围:ibri 1-10, icon 1-10, isat 1-10, ihue 1-10。
        /// </summary>
         /// <param name="ibri">是亮度</param>
        /// <param name="icon">是对比度</param>
        /// <param name="isat">是饱和度</param>
        /// <param name="ihue">是色调</param>
        /// <returns>成功返回 0,失败返回-1。</returns>
        public int HIKS_SetVideoParams(int ibri, int icon, int isat, int ihue)
        {

            return hkClient.HIKS_SetVideoParams( playSession,  ibri,  icon,  isat,  ihue);
        }

        /// <summary>
        /// 云台控制
        /// </summary>
        /// <param name="ucommand"></param>
        /// <param name="iparam1"></param>
        /// <param name="iparam2"></param>
        /// <param name="iparam3"></param>
        /// <param name="iparam4"></param>
        /// <returns></returns>
        public int HIKS_PTZControl(ushort ucommand, int iparam1, int iparam2, int iparam3, int iparam4)
        {
            return hkClient.HIKS_PTZControl(playSession,ucommand, iparam1,  iparam2,  iparam3,  iparam4);
        }

        /// <summary>
        /// 随即播放
        /// </summary>
        /// <param name="timepos"></param>
        /// <returns></returns>
        public int HIKS_RandomPlay(int timepos)
        {
            return 0;

        }
        /**
        public  int OnPosLength(ulong nLength)
        {
            return 0;
        }

        public  int OnPresentationOpened(int success)
        {
            Console.WriteLine("Url Opened Successed");
            return 0;
        }

        public  int OnPresentationClosed()
        {
            return 0;
        }

        public  int OnPreSeek(ulong uOldTime, ulong uNewTime)
        {
            return 0;
        }

        public  int OnPostSeek(ulong uOldTime, ulong uNewTime)
        {
            return 0;
        }

        public  int OnStop()
        {
            return 0;
        }

        public  int OnPause(ulong uTime)
        {
            return 0;
        }

        public override int OnBegin(ulong uTime)
        {
            return 0;
        }

        public  int OnRandomBegin(ulong uTime)
        {
            return 0;
        }

        public  int OnContacting(string pszHost)
        {
            return 0;
        }

        public  int OnPutErrorMsg(string pError)
        {
            return 0;
        }

        public  int OnBuffering(uint uFlag, uint uPercentComplete)
        {
            return 0;
        }

        public  int OnChangeRate(int flag)
        {
            return 0;
        }

        public  int OnDisconnect()
        {
            return 0;
        }
        */

 

 

        #region IHikClientAdviseSink 成员

        int IHikClientAdviseSink.OnPosLength(ulong nLength)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        int IHikClientAdviseSink.OnPresentationOpened(int success)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        int IHikClientAdviseSink.OnPresentationClosed()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        int IHikClientAdviseSink.OnPreSeek(ulong uOldTime, ulong uNewTime)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        int IHikClientAdviseSink.OnPostSeek(ulong uOldTime, ulong uNewTime)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        int IHikClientAdviseSink.OnStop()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        int IHikClientAdviseSink.OnPause(ulong uTime)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        int IHikClientAdviseSink.OnBegin(ulong uTime)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        int IHikClientAdviseSink.OnRandomBegin(ulong uTime)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        int IHikClientAdviseSink.OnContacting(string pszHost)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        int IHikClientAdviseSink.OnPutErrorMsg(string pError)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        int IHikClientAdviseSink.OnBuffering(uint uFlag, ushort uPercentComplete)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        int IHikClientAdviseSink.OnChangeRate(int flag)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        int IHikClientAdviseSink.OnDisconnect()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}


posted on 2010-02-05 17:31 绿叶 阅读(2287) 评论(1)  编辑  收藏 所属分类: 海康sdk

Feedback

# re: 调用海康SDK 2011-03-01 20:00 往事如烟
我也正在研究海康SDK 有时间交流一下QQ:12348328  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: