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