Ejemplo n.º 1
0
        /// <summary>
        /// 释放NETSOURCE通道资源
        /// </summary>
        private IVS_NETSOURCE_RESULT IVS_NETSOURCE_FreeChannel(UInt32 channel)
        {
            NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log);
            logEx.Trace("Enter: SipStackAdapter.IVS_NETSOURCE_FreeChannel.");
            IVS_NETSOURCE_RESULT iRet = IVS_NETSOURCE_RESULT.FAILURE;

            try
            {
                if (channel == 0)
                {
                    return(iRet);
                }
                //关闭网络流
                iRet = NetSourcedInterface.IVS_NETSOURCE_CloseNetStream(channel);
                if (iRet != CgwConst.IVS_SDK_SUCCESS_TAG)
                {
                    logEx.Error("SipStackAdapter.IVS_NETSOURCE_CloseNetStream ErrorCode = {0}.", iRet);
                }
                //释放指定通道
                iRet = NetSourcedInterface.IVS_NETSOURCE_FreeChannel(channel);
                if (iRet != CgwConst.IVS_SDK_SUCCESS_TAG)
                {
                    logEx.Error("SipStackAdapter.IVS_NETSOURCE_FreeChannel ErrorCode = {0}.", iRet);
                }
            }
            catch (System.Exception ex)
            {
                logEx.Error("SipStackAdapter.IVS_NETSOURCE_FreeChannel Error = {0}.", ex.ToString());
                iRet = IVS_NETSOURCE_RESULT.FAILURE;
            }
            return(iRet);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 停止接收rtp数据包
        /// </summary>
        /// <param name="cameraCode"></param>
        public EM_SIP_RESULT StopRecvStream(UInt32 channel)
        {
            NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log);
            logEx.Trace("Enter: SipStackAdapter.StopRecvStream channel = {0}.", channel);

            try
            {
                //停止实况
                int responseID = 0;
                foreach (KeyValuePair <UInt32, int> kvp in channelResponseIDDic)
                {
                    if (kvp.Key == channel)
                    {
                        responseID = kvp.Value;
                        channelResponseIDDic.Remove(kvp.Key);
                        break;
                    }
                }
                EM_SIP_RESULT iRet = SIP_SDK_BYE(responseID);
                if (iRet != EM_SIP_RESULT.RET_SUCCESS)
                {
                    logEx.Error("StopRecvStream failed channel={0}", channel);
                }
                //释放用户数据内存
                foreach (KeyValuePair <UInt32, IntPtr> kvp in channelInPtrDic)
                {
                    if (kvp.Key == channel)
                    {
                        Marshal.FreeHGlobal(kvp.Value);
                        channelInPtrDic.Remove(kvp.Key);
                        break;
                    }
                }

                //释放NETSOURCE通道资源
                IVS_NETSOURCE_RESULT iNet = IVS_NETSOURCE_FreeChannel(channel);
                if (iNet != IVS_NETSOURCE_RESULT.SUCCESS)
                {
                    logEx.Error("IVS_NETSOURCE_FreeChannel failed channel={0}", channel);
                    return(EM_SIP_RESULT.RET_FAILURE);
                }
                return(iRet);
            }
            catch (System.Exception ex)
            {
                logEx.Error("StopRecvStream failed.Exception message:{0}", ex.Message);
                return(EM_SIP_RESULT.RET_FAILURE);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 释放所有实况通道,释放NETSOURCE资源
        /// </summary>
        public IVS_NETSOURCE_RESULT IVS_NETSOURCE_UnInit()
        {
            NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log);
            logEx.Trace("Enter: SipStackAdapter.IVS_NETSOURCE_UnInit.");
            //IVS_NETSOURCE_RESULT iRet = IVS_NETSOURCE_RESULT.FAILURE;
            IVS_NETSOURCE_RESULT iRet = IVS_NETSOURCE_RESULT.SUCCESS;

            try
            {
                foreach (KeyValuePair <UInt32, int> kvp in channelResponseIDDic)
                {
                    UInt32 channel = kvp.Key;
                    iRet = NetSourcedInterface.IVS_NETSOURCE_CloseNetStream(channel);
                    if (iRet != IVS_NETSOURCE_RESULT.SUCCESS)
                    {
                        logEx.Error("SipStackAdapter.IVS_NETSOURCE_CloseNetStream channelResponseIDDic = {0}.", channel);
                        //注销return,避免返回,不实现IVS_NETSOURCE_UnInit
                        //return iRet;
                    }
                    iRet = NetSourcedInterface.IVS_NETSOURCE_FreeChannel(channel);
                    if (iRet != IVS_NETSOURCE_RESULT.SUCCESS)
                    {
                        logEx.Error("SipStackAdapter.IVS_NETSOURCE_FreeChannel channelResponseIDDic = {0}.", channel);
                        //注销return,避免返回,不实现IVS_NETSOURCE_UnInit
                        //return iRet;
                    }
                }
                //iRet = NetSourcedInterface.IVS_NETSOURCE_UnInit();
                //if (iRet != IVS_NETSOURCE_RESULT.SUCCESS)
                //{
                //    logEx.Error("SipStackAdapter.IVS_NETSOURCE_UnInit failed.");
                //}

                NetSourcedInterface.IVS_NETSOURCE_UnInit();
                logEx.Info("Leave: SipStackAdapter.IVS_NETSOURCE_UnInit.");
            }
            catch (System.Exception ex)
            {
                logEx.Error("SipStackAdapter.IVS_NETSOURCE_UnInit Error = {0}.", ex.ToString());
                iRet = IVS_NETSOURCE_RESULT.FAILURE;
            }
            return(iRet);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 初始化SIP协议栈
        /// </summary>
        public EM_SIP_RESULT SIP_SDK_Init(string pSipAccount, string pSipPasswd, string pLocalID, int iLocalPort, string pServerSipAccount, string pServerSipPasswd, string pServerID, string pServerIP, int iServerPort, EventHandler receivedAllDevice)
        {
            NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log);
            logEx.Trace("Enter: SipStackAdapter.SIP_SDK_Init().");
            EM_SIP_RESULT iRet = EM_SIP_RESULT.RET_FAILURE;

            try
            {
                //string pLocalIP = GetLocalIP(pServerIP, iServerPort, iLocalPort);
                if (string.IsNullOrEmpty(localIP))
                {
                    localIP = GetLocalIP(pServerIP, iServerPort, iLocalPort);
                }
                ReceivedAllDevice = receivedAllDevice;

                sip_CallBack = SIP_CallBackMethod;

                iRet = SipSDKInterface.SIP_SDK_Init(pSipAccount, pSipPasswd, pLocalID, localIP, iLocalPort, pServerSipAccount, pServerSipPasswd, pServerID, pServerIP, iServerPort, sip_CallBack);
                if (iRet != EM_SIP_RESULT.RET_SUCCESS)
                {
                    logEx.Error("SipStackAdapter.SIP_SDK_Init Failed ,pAccount:{0}", pSipAccount);
                }

                //设置NETSOURCE日志目录
                IVS_NETSOURCE_RESULT iNet = NetSourcedInterface.IVS_NETSOURCE_Init(AppDomain.CurrentDomain.BaseDirectory + "\\logs");
                if (iNet != IVS_NETSOURCE_RESULT.SUCCESS)
                {
                    logEx.Error("SipStackAdapter.IVS_NETSOURCE_Init Failed,pAccount:{0}", pSipAccount);
                    iRet = EM_SIP_RESULT.RET_FAILURE;
                }
            }
            catch (System.Exception ex)
            {
                logEx.Error("SipStackAdapter.SIP_SDK_Init ,Exception:{0}", ex.ToString());
                iRet = EM_SIP_RESULT.RET_FAILURE;
            }
            return(iRet);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 打开通道,开始接收实况RTP数据流
        /// </summary>
        /// <param name="cameraCode">摄像机编码</param>
        /// <returns></returns>
        public UInt32 StartRecvStream(string cameraCode, string domain, string iSipPort, string localPort)
        {
            NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log);
            logEx.Trace("Enter: SipStackAdapter.StartRecvStream  cameraCode :{0},domain = {1},iSipPort ={2}", cameraCode, domain, iSipPort);

            try
            {
                //接收视频码流RTP端口号
                uint uiLocalRecvStreamVideoRtpPort = 0;
                //接收音频码流RTP端口号
                uint uiLocalRecvStreamAudioRtpPort = 0;
                //通道号
                UInt32 uiChannel = 0;
                //string strLocal = GetLocalIP(domain, Convert.ToInt32(iSipPort), Convert.ToInt32(localPort));

                if (string.IsNullOrEmpty(localIP))
                {
                    localIP = GetLocalIP(domain, Convert.ToInt32(iSipPort), Convert.ToInt32(localPort));
                }
                string strLocal = localIP;

                //获取可用通道
                NetSourcedInterface.IVS_NETSOURCE_GetChannel(ref uiChannel);

                //用户参数,4字节整数
                IntPtr pUser = Marshal.AllocHGlobal(4);
                Marshal.Copy(new int[1] {
                    Convert.ToInt32(uiChannel)
                }, 0, pUser, 1);

                //设置媒体流回调函数
                NetSourcedInterface.IVS_NETSOURCE_SetDataCallBack(uiChannel, cbDataCallBack, pUser);
                //异常回调函数
                IVS_NETSOURCE_RESULT iRet = NetSourcedInterface.IVS_NETSOURCE_SetExceptionCallBack(cbNetExceptionCallBack, pUser);

                //协议类型 UDP
                iRet = NetSourcedInterface.IVS_NETSOURCE_SetProtocolType(uiChannel, 1);
                if (iRet != IVS_NETSOURCE_RESULT.SUCCESS)
                {
                    logEx.Error("SipStackAdapter.StartRecvStream.IVS_NETSOURCE_SetProtocolType failed. cameraCode :{0},domain = {1},iSipPort ={2}", cameraCode, domain, iSipPort);
                    return(CgwConst.T28181_ERROR_HANDLE);
                }

                iRet = NetSourcedInterface.IVS_NETSOURCE_SetLocalRecvStreamIP(uiChannel, localIP);
                if (iRet != IVS_NETSOURCE_RESULT.SUCCESS)
                {
                    logEx.Error("SipStackAdapter.StartRecvStream.IVS_NETSOURCE_SetLocalRecvStreamIP failed. cameraCode :{0},domain = {1},iSipPort ={2}", cameraCode, domain, iSipPort);
                    return(CgwConst.T28181_ERROR_HANDLE);
                }

                iRet = NetSourcedInterface.IVS_NETSOURCE_GetLocalRecvStreamPort(uiChannel, ref uiLocalRecvStreamVideoRtpPort, ref uiLocalRecvStreamAudioRtpPort);
                if (iRet != IVS_NETSOURCE_RESULT.SUCCESS)
                {
                    logEx.Error("SipStackAdapter.StartRecvStream.IVS_NETSOURCE_GetLocalRecvStreamPort failed. cameraCode :{0},domain = {1},iSipPort ={2}", cameraCode, domain, iSipPort);
                    return(CgwConst.T28181_ERROR_HANDLE);
                }

                //Invite消息体
                string strRealPlay = "v=0\r\n"
                                     + "o=" + cameraCode + @" 0 0 IN IP4 " + strLocal + "\r\n"
                                     + "s=Play\r\n"
                                     + "c=IN IP4 " + strLocal + "\r\n"
                                     + "t=0 0\r\n"
                                     + "m=video " + uiLocalRecvStreamVideoRtpPort + " RTP/AVP 96 98 97\r\n"
                                     + "a=recvonly\r\n"
                                     + "a=rtpmap:96 PS/90000\r\n"
                                     + "a=rtpmap:98 H264/90000\r\n"
                                     + "a=rtpmap:97 MPEG4/90000\r\n";

                //发送SIP Invite消息
                int iResponseID = 0;
                //制定字符串长度为1024
                StringBuilder strRemoteBody = new StringBuilder(1024);
                //发送SIP Invite消息
                EM_SIP_RESULT iSip = SIP_SDK_INVITE(cameraCode, strRealPlay, ref iResponseID, strRemoteBody);
                if (iSip != EM_SIP_RESULT.RET_SUCCESS)
                {
                    logEx.Error("SipStackAdapter.SIP_SDK_INVITE failed. cameraCode :{0},domain = {1},iSipPort ={2}", cameraCode, domain, iSipPort);
                    return(CgwConst.T28181_ERROR_HANDLE);
                }

                //源端发送码流IP;
                string pRemoteSendStreamIp = "";
                //源端发送视频码流RTP端口号
                uint uiRemoteSendStreamVideoPort = 0;
                GetInviteRemoteIPPort(strRemoteBody.ToString(), ref pRemoteSendStreamIp, ref uiRemoteSendStreamVideoPort);

                iRet = NetSourcedInterface.IVS_NETSOURCE_OpenNetStream(uiChannel, pRemoteSendStreamIp, uiRemoteSendStreamVideoPort, 0);//无音频,传入0
                if (iRet != IVS_NETSOURCE_RESULT.SUCCESS)
                {
                    logEx.Error("SipStackAdapter.StartRecvStream failed. cameraCode :{0},domain = {1},iSipPort ={2}", cameraCode, domain, iSipPort);
                    return(CgwConst.T28181_ERROR_HANDLE);
                }

                iRet = NetSourcedInterface.IVS_NETSOURCE_StartRecvStream(uiChannel);
                if (iRet == IVS_NETSOURCE_RESULT.SUCCESS)
                {
                    channelResponseIDDic.Add(uiChannel, iResponseID);
                    channelInPtrDic.Add(uiChannel, pUser);
                    logEx.Info("SipStackAdapter.StartRecvStream pChannel: {0}, cameraCode :{1},domain = {2},iSipPort ={3} ", uiChannel, cameraCode, domain, iSipPort);
                    return(uiChannel);
                }
                else
                {
                    logEx.Error("SipStackAdapter.StartRecvStream failed. cameraCode :{0},domain = {1},iSipPort ={2}", cameraCode, domain, iSipPort);
                    StopRecvStream(uiChannel);
                    Marshal.FreeHGlobal(pUser);
                    return(CgwConst.T28181_ERROR_HANDLE);
                }
            }
            catch (System.Exception ex)
            {
                logEx.Error("StartRecvStream failed.Exception message:{0},cameraCode :{0},domain = {1},iSipPort ={2}", ex.Message, cameraCode, domain, iSipPort);
                return(CgwConst.T28181_ERROR_HANDLE);
            }
        }