C# 流媒体服务器客户端封装

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

namespace HIK
{

    abstract class 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();

    };


    public delegate int pDataRec(int sid, int iusrdata, int idatatype, string pdata, int ilen);

    public delegate int pMsgBack(int sid, int opt, int param1, int param2);

 

    class hkClient
    {

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

        [DllImport("client.dll")]
        public static extern int InitStreamClientLib();
        [DllImport("client.dll")]
        public static extern int FiniStreamClientLib();
        [DllImport("client.dll")]
        public static extern int HIKS_OpenURL(int hSession, string pszURL, int iusrdata);
        [DllImport("client.dll")]
        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;
namespace HIK
{

    class clientControl : IHikClientAdviseSink
    {
        //private inheritAdviseSink adviseSink;
        ///


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

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

        private int m_opened;

        ///


        /// 播放句柄
        ///

        private int playSession = -1;

        Log.loginfo log = new Log.loginfo();

        public clientControl()
        {
            int ok;
             ok= hkClient.InitStreamClientLib();
             if (ok == -1)
             {
                 MessageBox.Show("初始化失败!");
             }

        }
        ///


        /// 关闭是调用
        ///

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

        ///


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

        public int CreatePlayer(IntPtr mHand)
        {
            pMsgBack mb = new pMsgBack(MsgBack);

            playSession = hkClient.HIKS_CreatePlayer(null, mHand, null, mb, 1);

            if (playSession == -1)
            {
                MessageBox.Show("创建play句柄失败!");
            }

            return playSession;

        }
        ///


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

        /// 。hSession 是Player 的标示,应该设置为HIKS_CreatePlayer 成功返回的新建Player 的标示
        /// pszURL 即为服务器的地址
        /// iusrdata 为用户数据。
        /// 返回值:成功返回 1,失败返回-1。
        public int HIKS_OpenURL(string pszURL, int iusrdata)
        {
            int ok;

 

            ok = hkClient.HIKS_OpenURL(playSession, pszURL, iusrdata);

            if (ok == -1)
            {
                MessageBox.Show("连接服务器失败!");

                log.Writelog("连接服务器失败!");

            }
            else
            {

                log.Writelog("连接服务器成功!");
            }

            return ok;


        }

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

            int len = -1;
            switch (opt)
            {
                case 1:
                    m_totaltime = param1;
                    break;
                case 2:
                    m_opened = param1;
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                case 7:
                    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("播放失败!");

                log.Writelog("播放失败!");

            }
            else
            {

                log.Writelog("播放成功!");
            }

            return ok;

        }
        ///


        /// 停止播放
        ///

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

        }

        public int HIKS_Resume()
        {
            int ok = hkClient.HIKS_Resume(playSession);


            if (ok == -1)
            {
                MessageBox.Show("恢复播放失败!");

                log.Writelog("恢复播放失败!");

            }
            else
            {

                log.Writelog("恢复播放成功!");
            }

            return ok;

        }
        public int HIKS_Stop()
        {

            int ok = hkClient.HIKS_Stop(playSession);


            if (ok == -1)
            {
                MessageBox.Show("停止播放失败!");

                log.Writelog("停止播放失败!");

            }
            else
            {

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

            return ok;

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


            if (ok == -1)
            {
                MessageBox.Show("获取播放时间失败!");

                log.Writelog("获取播放时间失败!");

            }
            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)
            {
                MessageBox.Show("销毁失败!");

                log.Writelog("销毁失败!");

            }
            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 override int OnPosLength(int nLength)
        {
            return 0;
        }

        public override int OnPresentationOpened(int success)
        {
            return 0;
        }

        public override int OnPresentationClosed()
        {
            return 0;
        }

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

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

        public override int OnStop()
        {
            return 0;
        }

        public override int OnPause(uint uTime)
        {
            return 0;
        }

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

        public override int OnRandomBegin(uint uTime)
        {
            return 0;
        }

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

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

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

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

        public override int OnDisconnect()
        {
            return 0;
        }


    }
}

你可能感兴趣的:(海康sdk开发)