海康流媒体客户端开发

海康流媒体客户端开发

流媒体SDK开发说明书
http://wenku.baidu.com/view/55281040be1e650e52ea992f.html

"iVMS-4200 客户端" 等程序中有流媒体服务器

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;
        ///


        /// 播放的总时间
        ///

        private int m_totaltime;
        ///
        /// url是否被打开
        ///

        private int m_opened=-1;
        private void setOpened(int openedvalue) {
            lock (this)
            {
                this.m_opened = openedvalue;
            }
        }
        private int m_Pause = -1;

        ///
        /// 播放句柄
        ///

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


        }
        ///
        /// 关闭是调用
        ///

        public void FiniStreamClientLib()
        {
            hkClient.FiniStreamClientLib();
        }

        ///
        /// 开始之前调用
        ///

        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;

        }


        ///
        /// 根据URL,连接服务器
        ///

        /// 。hSession 是Player 的标示,应该设置为HIKS_CreatePlayer 成功返回的新建Player 的标示
        /// pszURL 即为服务器的地址
        /// iusrdata 为用户数据。
        /// 返回值:成功返回 1,失败返回-1。
        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;
        }

        ///
        /// 播放
        ///

        ///
        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;

        }
        ///
        /// 停止播放
        ///

        ///
        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;

        }
        ///
        /// 作用:改变播放速率,在快进或慢进的时候用。
        ///

        ///
        /// 返回值:成功返回 0,失败返回-1。
        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;

        }
        ///
        /// 作用:销毁Player,只在HIKS_OpenURL 函数失败的请况下调用。
        ///

        /// 返回值:成功返回 0,失败返回-1。
        public int HIKS_Destroy()
        {
            int ok = hkClient.HIKS_Destroy(playSession);


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

            }
            else
            {

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

            return ok;

        }

        ///
        /// 声音控制
        ///

        ///
        ///
        public int HIKS_SetVolume(ushort volume)
        {

            return hkClient.HIKS_SetVolume(playSession, volume);
        }

        ///
        /// 作用:打开声音播放。hSession 是Player 的标示。bExclusive 表示打开声音
        //的方式,false 表示共享打开,true 表示独占打开。
        ///

        ///
        ///
        public int HIKS_OpenSound(bool bExclusive)
        {
            return hkClient.HIKS_OpenSound(playSession, bExclusive);

        }
        ///
        ///关闭声音
        ///

        ///
        public int HIKS_CloseSound()
        {
            return hkClient.HIKS_CloseSound(playSession);

        }
        ///
        /// :本地抓图。hSession 表示Player 的标示。szPicFileName 表示文件名。
        //byPicType 表示图片的类型,0 表示bmp 图片,1 表示jpeg 图片。
        ///

        ///
        ///
        ///
        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);

        }

        ///
        /// 作用:获取视频参数。hSession 是Player 的标示
        ///

        /// 是亮度
        /// 是对比度
        /// 是饱和度
        /// 是色调
        /// 成功返回 0,失败返回-1。
        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);
        }
        ///
        /// 作用:设置视频参数。hSession 是Player 的标示, ibri 是亮度,icon 是对比度,
        ///值域范围:ibri 1-10, icon 1-10, isat 1-10, ihue 1-10。
        ///

         /// 是亮度
        /// 是对比度
        /// 是饱和度
        /// 是色调
        /// 成功返回 0,失败返回-1。
        public int HIKS_SetVideoParams(int ibri, int icon, int isat, int ihue)
        {

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

        ///
        /// 云台控制
        ///

        ///
        ///
        ///
        ///
        ///
        ///
        public int HIKS_PTZControl(ushort ucommand, int iparam1, int iparam2, int iparam3, int iparam4)
        {
            return hkClient.HIKS_PTZControl(playSession,ucommand, iparam1,  iparam2,  iparam3,  iparam4);
        }

        ///
        /// 随即播放
        ///

        ///
        ///
        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
    }
}

--------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HIKLib;
using System.Threading;

namespace hik_test_cs3
{
    public partial class Form1 : Form
    {
        int m_hSession = -1;
        int m_opened = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int r = hkClient.InitStreamClientLib();//为 0 是正确的

            //int m_hSession = hkClient.HIKS_CreatePlayer(0, this.panel1.Handle, usb, usa, 0);
            //int m_hSession = hkClient.HIKS_CreatePlayer(null, this.panel1.Handle, null, pMsgFun, 0);//-1 好像不对
            m_hSession = hkClient.HIKS_CreatePlayer(null, this.panel1.Handle, null, MsgBack, 0);//返回 -1 好像不对,MsgBack 不能为空//参考 http://wenku.baidu.com/view/55281040be1e650e52ea992f.html

            //hkClient.HIKS_OpenURL(Label2.Text, "rtsp://111.1.111.111/111.111.111.111:5000:HIK-DS8000HC:6:0:admin:12345/av_stream", 0);
            //r = hkClient.HIKS_OpenURL(m_hSession, "rtsp://111.1.111.111/111.111.111.111:5000:HIK-DS8000HC:6:0:admin:12345/av_stream", 0);
            //r = hkClient.HIKS_OpenURL(m_hSession, "rtsp://127.0.0.1/192.168.10.113:8000:HIK-DS8000HC:0:0:admin:12345/av_stream", 0);
            //用微软媒体播放器打开是这个[svrt代替了rtsp]//svrt://192.0.0.43/192.0.0.111:8000:HIK-DS8000HC:0:0:admin:12345/av_stream
            //r = hkClient.HIKS_OpenURL(m_hSession, "rtsp://127.0.0.1/192.168.10.113:8000:HIK-DS8000HC:0:0:admin:12345/av_stream", 0);
            r = hkClient.HIKS_OpenURL(m_hSession, "rtsp://127.0.0.1/172.4.0.5:8000:HIK-DS8000HC:0:0:admin:12345/av_stream", 0);

            while (m_opened < 0) Thread.Sleep(100);

            r = hkClient.HIKS_Play(m_hSession);//奇怪,不能在回调函数里打开
        }//

        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);
                    //int r = hkClient.HIKS_OpenURL(m_hSession, "rtsp://127.0.0.1/172.x.x.x:8000:HIK-DS8000HC:0:0:admin:12345/av_stream", 0);
                    //int r = hkClient.HIKS_Play(m_hSession);//奇怪,不能在回调函数里打开
                    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;
        }//
    }//
}//








你可能感兴趣的:(海康流媒体客户端开发)