-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathSipClientApent.cpp
309 lines (257 loc) · 9.22 KB
/
SipClientApent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "SipClientApent.h"
CSipClientApent::CSipClientApent(void)
:agent_type_(0)
,register_state_(REGISTER_STATUE::SIP_REGISTERED)
,keeplive_time_(0)
,cid_(0)
,did_(0)
,tid_(0)
{
}
CSipClientApent::~CSipClientApent(void)
{
}
CSipClientApent::CSipClientApent(char *ip,long port,char *id,char *name,time_t kt,int agenttype,int registerstate)
{
if(ip)
this->ip_ = ip;
if(id)
this->id_ = id;
if(name)
this->name_ = name;
this->port_ = port;
this->keeplive_time_ = kt;
this->agent_type_ = agenttype;
this->register_state_ = registerstate;
}
void CSipClientApent::update_callinfo(int cid,CSipCallInfo *sci)
{
ACE_Guard<ACE_Recursive_Thread_Mutex> locker(sipcall_lock_);
map<int,CSipCallInfo*>::iterator iter = sip_call_map_.find(cid);
if(iter!=sip_call_map_.end())
{
if(iter->second)
{
delete iter->second;
iter->second = NULL;
sip_call_map_.erase(iter);
}
}
sip_call_map_.insert(pair<int,CSipCallInfo*>(cid,sci));
}
void CSipClientApent::remove_callinfo(int cid)
{
ACE_Guard<ACE_Recursive_Thread_Mutex> locker(sipcall_lock_);
map<int,CSipCallInfo*>::iterator iter = sip_call_map_.find(cid);
if(iter!=sip_call_map_.end())
{
if(iter->second)
{
delete iter->second;
iter->second = NULL;
sip_call_map_.erase(iter);
}
}
}
CSipCallInfo * CSipClientApent::get_callinfo(int cid)
{
ACE_Guard<ACE_Recursive_Thread_Mutex> locker(sipcall_lock_);
map<int,CSipCallInfo*>::iterator iter = sip_call_map_.find(cid);
if(iter!=sip_call_map_.end())
{
return iter->second;
}
return NULL;
}
void CSipClientApent::get_callinfo(int srccid,vector<CSipCallInfo*> &vt)
{
ACE_Guard<ACE_Recursive_Thread_Mutex> locker(sipcall_lock_);
map<int,CSipCallInfo*>::iterator iter = sip_call_map_.begin();
for (;iter!=sip_call_map_.end();++iter)
{
if(iter->second&&iter->second->srccid_ == srccid)
{
vt.push_back(iter->second);
}
}
}
int CSipClientApent::check_callinfo(int cid)
{
ACE_Guard<ACE_Recursive_Thread_Mutex> locker(sipcall_lock_);
map<int,CSipCallInfo*>::iterator iter = sip_call_map_.find(cid);
if(iter!=sip_call_map_.end())
{
return 1;
}
return 0;
}
int CSipClientApent::send_invite(eXosip_t *context_eXosip,CSipClientApent *sipproxy,CSipCallInfo *srccall,string fromid,string toid)
{
if(sipproxy == NULL || srccall == NULL)
return -1;
int ret = 0;
osip_message_t *invitesip = NULL;
char from[100];/*sip:主叫用户名@被叫IP地址*/
char to[100];/*sip:被叫IP地址:被叫IP端口*/
memset(from, 0, 100);
memset(to, 0, 100);
sprintf(from, "sip:%s@%s:%d", sipproxy->id().c_str()/*fromid.c_str()*/, sipproxy->ip().c_str(),sipproxy->port());
sprintf(to, "sip:%s@%s:%d",this->id().c_str()/*toid.c_str()*/,this->ip().c_str(), this->port());
ret = eXosip_call_build_initial_invite(context_eXosip,&invitesip,to,from,NULL,"");
did_ = ret;
if(ret < 0 )
{
APTS_LOGMSG((LM_ERROR,"(%t)%D [GB28181ServerTask] send_invite eXosip_call_build_initial_invite fail,from:%s,to:%s\n",from,to));
return -1;
}
ret = eXosip_call_send_initial_invite(context_eXosip,invitesip);
if(ret < 0)
{
APTS_LOGMSG((LM_ERROR,"(%t)%D [GB28181ServerTask] send_invite eXosip_call_send_initial_invite fail,from:%s,to:%s\n",from,to));
return -1;
}
cid_ = ret;
CSipCallInfo *callinfo = new CSipCallInfo(ret,cid_,did_,srccall->did_,srccall->cid_,srccall->srcid_.c_str(),srccall->dstid_.c_str(),SIPREQUST_STATUS::STATUS_SEND_INVITE_NOSDP);
callinfo->set_ssrc(srccall->ssrc_);
update_callinfo(ret,callinfo);
APTS_LOGMSG((LM_INFO,"(%t)%D [GB28181ServerTask] send_invite from:%s,to:%s\n",from,to));
return ret;
}
int CSipClientApent::send_invite_sdp(eXosip_t *context_eXosip,eXosip_event_t *evt,CSipClientApent *sipproxy,CSipCallInfo *srccall,string fromid,string toid)
{
if(sipproxy == NULL || srccall == NULL)
return -1;
int ret = 0;
osip_message_t *invitesip = NULL;
char from[100];/*sip:主叫用户名@被叫IP地址*/
char to[100];/*sip:被叫IP地址:被叫IP端口*/
memset(from, 0, 100);
memset(to, 0, 100);
sprintf(from, "sip:%s@%s:%d", sipproxy->id().c_str()/*fromid.c_str()*/, sipproxy->ip().c_str(),sipproxy->port());
sprintf(to, "sip:%s@%s:%d",this->id().c_str()/*toid.c_str()*/,this->ip().c_str(), this->port());
ret = eXosip_call_build_initial_invite(context_eXosip,&invitesip,to,from,NULL,"");
if(ret < 0 )
{
APTS_LOGMSG((LM_ERROR,"(%t)%D [GB28181ServerTask] send_invite_sdp eXosip_call_build_initial_invite fail,from:%s,to:%s\n",from,to));
return -1;
}
did_ = ret;
sdp_message_t *remotesdp= NULL;
remotesdp = eXosip_get_remote_sdp(context_eXosip,evt->did);
string sdpcontent;
if(remotesdp)
{
char* csdp = NULL;
sdp_message_to_str(remotesdp, &csdp);
if(csdp)
{
sdpcontent = csdp;
osip_free(csdp);
}
if(sdpcontent.find("y=") == std::string::npos)
{
sdpcontent.append("y=");
sdpcontent.append(srccall->ssrc_.c_str());
sdpcontent.append("\r\n");
}
}
osip_message_set_body(invitesip,sdpcontent.c_str(),sdpcontent.length());
osip_message_set_content_type(invitesip,"application/sdp");
ret = eXosip_call_send_initial_invite(context_eXosip,invitesip);
if(ret < 0)
{
APTS_LOGMSG((LM_ERROR,"(%t)%D [GB28181ServerTask] send_invite_sdp eXosip_call_send_initial_invite fail,from:%s,to:%s\n",from,to));
return -1;
}
cid_ = ret;
CSipCallInfo *callinfo = new CSipCallInfo(ret,cid_,did_,srccall->did_,srccall->cid_,srccall->srcid_.c_str(),srccall->dstid_.c_str(),SIPREQUST_STATUS::STATUS_SEND_INVITE_SDP);
callinfo->set_ssrc(srccall->ssrc_);
callinfo->set_sdp(sdpcontent);
update_callinfo(ret,callinfo);
APTS_LOGMSG((LM_INFO,"(%t)%D [GB28181ServerTask] send_invite_sdp from:%s,to:%s\n",from,to));
return ret;
}
int CSipClientApent::send_invite_sdp(eXosip_t *context_eXosip,string sdpcontent,CSipClientApent *sipproxy,CSipCallInfo *srccall,string fromid,string toid)
{
if(sipproxy == NULL || srccall == NULL)
return -1;
int ret = 0;
osip_message_t *invitesip = NULL;
char from[100];/*sip:主叫用户名@被叫IP地址*/
char to[100];/*sip:被叫IP地址:被叫IP端口*/
memset(from, 0, 100);
memset(to, 0, 100);
sprintf(from, "sip:%s@%s:%d", sipproxy->id().c_str()/*fromid.c_str()*/, sipproxy->ip().c_str(),sipproxy->port());
sprintf(to, "sip:%s@%s:%d",this->id().c_str()/*toid.c_str()*/,this->ip().c_str(), this->port());
ret = eXosip_call_build_initial_invite(context_eXosip,&invitesip,to,from,NULL,"");
if(ret < 0 )
{
APTS_LOGMSG((LM_ERROR,"(%t)%D [GB28181ServerTask] send_invite_sdp 2 eXosip_call_build_initial_invite fail,from:%s,to:%s\n",from,to));
return -1;
}
osip_message_set_body(invitesip,sdpcontent.c_str(),sdpcontent.length());
osip_message_set_content_type(invitesip,"application/sdp");
ret = eXosip_call_send_initial_invite(context_eXosip,invitesip);
if(ret < 0)
{
APTS_LOGMSG((LM_ERROR,"(%t)%D [GB28181ServerTask] send_invite_sdp 2 eXosip_call_send_initial_invite fail,from:%s,to:%s\n",from,to));
return -1;
}
CSipCallInfo *callinfo = new CSipCallInfo(ret,ret,ret,srccall->did_,srccall->cid_,srccall->srcid_.c_str(),srccall->dstid_.c_str(),SIPREQUST_STATUS::STATUS_SEND_INVITE_SDP);
callinfo->set_ssrc(srccall->ssrc_);
callinfo->set_sdp(sdpcontent);
update_callinfo(ret,callinfo);
APTS_LOGMSG((LM_INFO,"(%t)%D [GB28181ServerTask] send_invite_sdp 2 from:%s,to:%s\n",from,to));
return ret;
}
int CSipClientApent::send_ack(eXosip_t *context_eXosip,string sdpcontent,int did,int ishavesdp)
{
osip_message_t *ack = NULL;
int ret = eXosip_call_build_ack(context_eXosip,did,&ack);
if(ishavesdp)
{
osip_message_set_body(ack,sdpcontent.c_str(),sdpcontent.length());
osip_message_set_content_type(ack,"application/sdp");
}
ret = eXosip_call_send_ack(context_eXosip,did,ack);
if(ret <0)
{
APTS_LOGMSG((LM_ERROR,"(%t)%D [GB28181ServerTask] 1 eXosip_call_send_ack fail did:%d\n",did));
return -1;
}
return 0;
}
int CSipClientApent::send_200OK(eXosip_t *context_eXosip,int tid,long code,string sdpcontent,CSipCallInfo *srccall)
{
osip_message_t *answer = NULL;
int ret = eXosip_call_build_answer (context_eXosip, tid, code, &answer);
if(ret < 0)
return -1;
osip_message_set_body (answer, sdpcontent.c_str(), sdpcontent.length());
osip_message_set_content_type (answer, "application/sdp");
ret = eXosip_call_send_answer(context_eXosip, tid, code, answer);
if(ret <0)
{
APTS_LOGMSG((LM_INFO,"(%t)%D [GB28181ServerTask] eXosip_call_send_answer error"));
return -1;
}
CSipCallInfo *callinfo = new CSipCallInfo(ret,ret,ret,srccall->did_,srccall->cid_,srccall->srcid_.c_str(),srccall->dstid_.c_str(),SIPREQUST_STATUS::STATUS_SEND_200OK);
callinfo->set_ssrc(srccall->ssrc_);
callinfo->set_sdp(sdpcontent);
update_callinfo(ret,callinfo);
return 0;
}
int CSipClientApent::send_200OK(eXosip_t *context_eXosip,int tid,long code)
{
osip_message_t *answer = NULL;
int ret = eXosip_call_build_answer (context_eXosip, tid, code, &answer);
if(ret < 0)
return -1;
ret = eXosip_call_send_answer(context_eXosip, tid, code, answer);
if(ret <0)
{
APTS_LOGMSG((LM_INFO,"(%t)%D [GB28181ServerTask] eXosip_call_send_answer error"));
return -1;
}
return 0;
}