My favorites
▼
|
Sign in
rxwen-blog-stuff
my blog stuff
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
protocol
/
basic_osip_sample
/
basic_osip_sample.cpp
‹r31
r314
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
// basic_osip_sample.cpp : Defines the entry point for the console application.
// http://www.gnu.org/software/osip/doc/html/group__howto0__initialize.html
#include "stdafx.h"
#define ENABLE_TRACE
#include "winsock2.h"
#include "osip2/osip.h"
#include <osip2/osip_mt.h>
#include <iostream>
#include <sstream>
#include <assert.h>
#define UNUSED(x) (void(x))
using namespace std;
static SOCKET g_sock;
static const size_t BUFFSIZE = 1600; // MTU usually doesn't exceed 1600
int cb_SendMsg(osip_transaction_t *tr, osip_message_t *sip, char *, int port, int out_socket)
{
UNUSED(out_socket);
UNUSED(tr);
UNUSED(port);
char *msgP;
size_t msgLen;
osip_message_to_str(sip, &msgP, &msgLen);
stringstream ss;
ss << tr->topvia->host << ':' << tr->topvia->port;
struct sockaddr addr;
int addrSize = sizeof(struct sockaddr);
WSAStringToAddressA(const_cast<LPSTR>(ss.str().c_str()), AF_INET, NULL, &addr, &addrSize);
sendto(out_socket, msgP, msgLen, 0, &addr, addrSize);
return 0;
}
int BuildResponse(const osip_message_t *request, osip_message_t **response)
{
osip_message_t *msg = NULL;
osip_message_init(&msg);
osip_from_clone(request->from, &msg->from);
osip_to_clone(request->to, &msg->to);
osip_cseq_clone(request->cseq, &msg->cseq);
osip_call_id_clone(request->call_id, &msg->call_id);
int pos = 0;//copy vias from request to response
while (!osip_list_eol (&request->vias, pos))
{
osip_via_t *via;
osip_via_t *via2;
via = (osip_via_t *) osip_list_get (&request->vias, pos);
int i = osip_via_clone (via, &via2);
if (i != 0)
{
osip_message_free (msg);
return i;
}
osip_list_add (&(msg->vias), via2, -1);
pos++;
}
osip_to_set_tag(msg->to, osip_strdup("4893693")); // set to tag in response
osip_message_set_version(msg, osip_strdup("SIP/2.0"));
osip_message_set_contact(msg, osip_strdup("sip:raymond@127.0.0.1"));
osip_message_set_user_agent(msg, osip_strdup("Linphone/3.2.1 (eXosip2/3.3.0)"));
*response = msg;
return 0;
}
void cb_RcvICTRes(int, osip_transaction_t *, osip_message_t *)
{
OutputDebugString(TEXT("cb_RcvICTRes fired"));
}
void cb_RcvNICTRes(int, osip_transaction_t *, osip_message_t *)
{
OutputDebugString(TEXT("cb_RcvNICTRes fired"));
}
void cb_ISTTranKill(int, osip_transaction_t *)
{
OutputDebugString(TEXT("cb_ISTTranKill fired"));
}
void* Notify(void* arg)
{
osip_transaction_t *tran = static_cast<osip_transaction_t*>(arg);
osip_message_t *response = NULL;
osip_event_t *evt = NULL;
BuildResponse(tran->orig_request, &response);
cout << "incoming call from " << tran->from->url << endl << " [a]nswer or [d]ecline?" << endl;
char act = 'd';
cin >> act;
if('a' == act)
{//accept call
osip_message_set_status_code(response, SIP_OK);
osip_message_set_reason_phrase(response, osip_strdup("OK"));
const char* mime = "application/sdp";
osip_message_set_body_mime(response, mime, strlen(mime));
osip_message_set_content_type(response, mime);
const char* sdp = "v=0\r\n\
o=raymond 323456 654323 IN IP4 127.0.0.1\r\n\
s=A conversation\r\n\
c=IN IP4 127.0.0.1\r\n\
t=0 0\r\n\
m=audio 7078 RTP/AVP 111 110 0 8 101\r\n\
a=rtpmap:111 speex/16000/1\r\n\
a=rtpmap:110 speex/8000/1\r\n\
a=rtpmap:0 PCMU/8000/1\r\n\
a=rtpmap:8 PCMA/8000/1\r\n\
a=rtpmap:101 telephone-event/8000\r\n\
a=fmtp:101 0-11\r\n";
osip_message_set_body(response, osip_strdup(sdp), strlen(sdp));
}
else
{//decline call
osip_message_set_status_code(response, SIP_DECLINE);
osip_message_set_reason_phrase(response, osip_strdup("Decline"));
osip_message_set_contact(response, NULL);
}
evt = osip_new_outgoing_sipmessage(response);
evt->transactionid = tran->transactionid;
osip_transaction_add_event(tran, evt);
return NULL;
}
void cb_RcvISTReq(int, osip_transaction_t *tran, osip_message_t *msg)
{
osip_message_t *response = NULL;
osip_event_t *evt = NULL;
BuildResponse(msg, &response);//trying
osip_message_set_status_code(response, SIP_TRYING);
evt = osip_new_outgoing_sipmessage(response);
osip_message_set_reason_phrase(response, osip_strdup("Trying"));
osip_transaction_add_event(tran, evt);
BuildResponse(msg, &response);//dialog establishement
osip_message_set_status_code(response, 101);
evt = osip_new_outgoing_sipmessage(response);
osip_message_set_reason_phrase(response, osip_strdup("Dialog Establishement"));
osip_transaction_add_event(tran, evt);
BuildResponse(msg, &response);//ringing
osip_message_set_status_code(response, SIP_RINGING);
evt = osip_new_outgoing_sipmessage(response);
osip_message_set_reason_phrase(response, osip_strdup("Ringing"));
osip_transaction_add_event(tran, evt);
osip_thread_create(0, Notify, tran);// start another thread to notify user the incoming call
}
void SetCallbacks(osip_t *osip)
{
osip_set_cb_send_message(osip, cb_SendMsg);
osip_set_message_callback(osip, OSIP_ICT_STATUS_1XX_RECEIVED, cb_RcvICTRes);
osip_set_message_callback(osip, OSIP_NICT_STATUS_1XX_RECEIVED, cb_RcvNICTRes);
osip_set_message_callback(osip, OSIP_IST_INVITE_RECEIVED, cb_RcvISTReq);
osip_set_kill_transaction_callback(osip, OSIP_IST_KILL_TRANSACTION, cb_ISTTranKill);
}
SOCKET InitNet()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
TCHAR ip[] = TEXT("0.0.0.0:5060");
struct sockaddr addr;
int addrSize = sizeof(struct sockaddr);
WSAStringToAddress(ip, AF_INET, NULL, &addr, &addrSize);
bind(sock, &addr, addrSize);
return sock;
}
void ProcessNewReq(osip_t* osip, osip_event_t *evt)
{
osip_transaction_t *tran;
osip_transaction_init(&tran, IST, osip, evt->sip);
//osip_transaction_set_in_socket (tran, socket);
osip_transaction_set_out_socket (tran, g_sock);
osip_transaction_set_your_instance(tran, osip);// store osip in transaction for later usage
osip_transaction_add_event(tran, evt);
}
void* TransportFun(void* arg)
{
int rc;
osip_t* osip = static_cast<osip_t*>(arg);
OutputDebugString(TEXT("initialize network"));
g_sock = InitNet();
assert(0 < g_sock);
char buf[BUFFSIZE];
while(true)
{
struct sockaddr from;
int addrSize = sizeof(struct sockaddr);
int len = recvfrom(g_sock, buf, BUFFSIZE, 0, &from, &addrSize);
if(len < 1)
continue;
buf[len] = 0;
WCHAR addrBuf[1024];
DWORD addrBufLen;
WSAAddressToString(&from, addrSize, NULL, addrBuf, &addrBufLen);
osip_event_t *evt = osip_parse(buf, len);
rc = osip_find_transaction_and_add_event(osip, evt);
if(0 != rc)
{
OutputDebugString(TEXT("this event has no transaction, create a new one."));
ProcessNewReq(osip, evt);
}
}
return NULL;
}
int main(int argc, _TCHAR* argv[])
{
UNUSED(argc); UNUSED(argv);
osip_t* osip = NULL;
osip_init(&osip);
SetCallbacks(osip);
osip_thread_create(0, TransportFun, osip);
while(true)
{
osip_ict_execute(osip);
osip_ist_execute(osip);
osip_nict_execute(osip);
osip_nist_execute(osip);
osip_timers_ict_execute(osip);
osip_timers_ist_execute(osip);
osip_timers_nict_execute(osip);
osip_timers_nist_execute(osip);
}
return 0;
}
Show details
Hide details
Change log
r32
by rx.wen218 on Mar 22, 2010
Diff
update ospi sample structure
Go to:
...col/basic_osip_sample/ReadMe.txt
...ic_osip_sample/basic_osip_sample
...sip_sample/basic_osip_sample.cpp
..._sample/basic_osip_sample.vcproj
...tocol/basic_osip_sample/osip.vsd
...col/basic_osip_sample/stdafx.cpp
...tocol/basic_osip_sample/stdafx.h
...ol/basic_osip_sample/targetver.h
Project members,
sign in
to write a code review
Older revisions
r31
by rx.wen218 on Mar 22, 2010
Diff
add basic osip sample
All revisions of this file
File info
Size: 7032 bytes, 243 lines
View raw file
Powered by
Google Project Hosting