blob: 6580f1868ae7d356da9fea249ab3e3f34e097aef [file] [log] [blame]
Benny Prijono77998ce2007-06-20 10:03:46 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono77998ce2007-06-20 10:03:46 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/**
22 * invtester.c
23 *
24 * Send INVITE/re-INVITE without SDP.
25 */
26
27
28/* Include all headers. */
29#include <pjsip.h>
30#include <pjlib-util.h>
31#include <pjlib.h>
32
33#define THIS_FILE "invtester.c"
34
35#define PORT 50060
36#define PORT_STR ":50060"
37#define SAME_BRANCH 0
38#define ACK_HAS_SDP 1
39
40static pjsip_endpoint *sip_endpt;
41static pj_bool_t quit_flag;
42static pjsip_dialog *dlg;
43
44
45/* Callback to handle incoming requests. */
46static void on_tsx_state(pjsip_transaction *tsx, pjsip_event *event);
47
48static pjsip_module mod_app =
49{
50 NULL, NULL, /* prev, next. */
51 { "mod-app", 7 }, /* Name. */
52 -1, /* Id */
53 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
54 NULL, /* load() */
55 NULL, /* start() */
56 NULL, /* stop() */
57 NULL, /* unload() */
58 NULL, /* on_rx_request() */
59 NULL, /* on_rx_response() */
60 NULL, /* on_tx_request. */
61 NULL, /* on_tx_response() */
62 &on_tsx_state /* on_tsx_state() */
63};
64
65
66/* Worker thread */
67static int worker_thread(void *arg)
68{
69 PJ_UNUSED_ARG(arg);
70
71 while (!quit_flag) {
72 pj_time_val timeout = {0, 500};
73 pjsip_endpt_handle_events(sip_endpt, &timeout);
74 }
75
76 return 0;
77}
78
79/* Send request */
80static void send_request(const pjsip_method *method,
81 int cseq,
82 const pj_str_t *branch,
83 pj_bool_t with_offer)
84{
85 pjsip_tx_data *tdata;
86 pj_str_t dummy_sdp_str =
87 {
88 "v=0\r\n"
89 "o=- 3360842071 3360842071 IN IP4 192.168.0.68\r\n"
90 "s=pjmedia\r\n"
91 "c=IN IP4 192.168.0.68\r\n"
92 "t=0 0\r\n"
93 "m=audio 4000 RTP/AVP 0 101\r\n"
94 "a=rtcp:4001 IN IP4 192.168.0.68\r\n"
95 "a=rtpmap:0 PCMU/8000\r\n"
96 "a=sendrecv\r\n"
97 "a=rtpmap:101 telephone-event/8000\r\n"
98 "a=fmtp:101 0-15\r\n",
99 0
100 };
101 pj_status_t status;
102
103 status = pjsip_dlg_create_request(dlg, method, cseq, &tdata);
104 pj_assert(status == PJ_SUCCESS);
105
106 if (branch) {
107 pjsip_via_hdr *via;
108
109 via = (pjsip_via_hdr*) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
110 pj_strdup(tdata->pool, &via->branch_param, branch);
111 }
112
113 if (with_offer) {
114 pjsip_msg_body *body;
115 pj_str_t mime_application = { "application", 11};
116 pj_str_t mime_sdp = {"sdp", 3};
117
118
119 dummy_sdp_str.slen = pj_ansi_strlen(dummy_sdp_str.ptr);
120 body = pjsip_msg_body_create(tdata->pool,
121 &mime_application, &mime_sdp,
122 &dummy_sdp_str);
123 tdata->msg->body = body;
124 }
125
126 status = pjsip_dlg_send_request(dlg, tdata, -1, NULL);
127 pj_assert(status == PJ_SUCCESS);
128}
129
130/* Callback to handle incoming requests. */
131static void on_tsx_state(pjsip_transaction *tsx, pjsip_event *event)
132{
133 if (tsx->role == PJSIP_ROLE_UAC) {
134 if (tsx->method.id == PJSIP_INVITE_METHOD && tsx->state == PJSIP_TSX_STATE_TERMINATED) {
135#if SAME_BRANCH
136 send_request(&pjsip_ack_method, tsx->cseq, &tsx->branch, ACK_HAS_SDP);
137#else
138 send_request(&pjsip_ack_method, tsx->cseq, NULL, ACK_HAS_SDP);
139#endif
140 }
141
142 } else {
143 if (event->type == PJSIP_EVENT_RX_MSG && tsx->state == PJSIP_TSX_STATE_TRYING) {
144 pjsip_tx_data *tdata;
145
146 pjsip_dlg_create_response(dlg, event->body.tsx_state.src.rdata,
147 200, NULL, &tdata);
148 pjsip_dlg_send_response(dlg, tsx, tdata);
149 }
150 }
151}
152
153/* make call */
154void make_call(char *uri, pj_bool_t with_offer)
155{
156 pj_str_t local = pj_str("sip:localhost" PORT_STR);
157 pj_str_t remote = pj_str(uri);
158 pj_status_t status;
159
160 status = pjsip_dlg_create_uac(pjsip_ua_instance(),
161 &local, &local, &remote, &remote, &dlg);
162 pj_assert(status == PJ_SUCCESS);
163
164 pjsip_dlg_inc_lock(dlg);
165
166 status = pjsip_dlg_add_usage(dlg, &mod_app, NULL);
167 pj_assert(status == PJ_SUCCESS);
168
169 pjsip_dlg_inc_session(dlg, &mod_app);
170
171 send_request(&pjsip_invite_method, -1, NULL, with_offer);
172
173 pjsip_dlg_dec_lock(dlg);
174}
175
176/* reinvite */
177void reinvite(pj_bool_t with_offer)
178{
179 send_request(&pjsip_invite_method, -1, NULL, with_offer);
180}
181
182/* hangup call */
183void hangup(void)
184{
185 send_request(&pjsip_bye_method, -1, NULL, PJ_FALSE);
186 pjsip_dlg_dec_session(dlg, &mod_app);
187}
188
189/*
190 * main()
191 *
192 */
193int main(int argc, char *argv[])
194{
195 pj_caching_pool cp;
196 pj_thread_t *thread;
197 pj_pool_t *pool;
198 pj_status_t status;
199
200 if (argc != 2) {
201 puts("Error: destination URL needed");
202 return 0;
203 }
204
205 /* Must init PJLIB first: */
206 status = pj_init();
207 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
208
209
210 /* Then init PJLIB-UTIL: */
211 status = pjlib_util_init();
212 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
213
214 /* Must create a pool factory before we can allocate any memory. */
215 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
216
217
218 /* Create the endpoint: */
219 status = pjsip_endpt_create(&cp.factory, "sipstateless",
220 &sip_endpt);
221 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
222
223 /*
224 * Add UDP transport, with hard-coded port
225 */
226 {
227 pj_sockaddr_in addr;
228
Benny Prijono8ab968f2007-07-20 08:08:30 +0000229 addr.sin_family = pj_AF_INET();
Benny Prijono77998ce2007-06-20 10:03:46 +0000230 addr.sin_addr.s_addr = 0;
231 addr.sin_port = pj_htons(PORT);
232
233 status = pjsip_udp_transport_start( sip_endpt, &addr, NULL, 1, NULL);
234 if (status != PJ_SUCCESS) {
235 PJ_LOG(3,(THIS_FILE,
236 "Error starting UDP transport (port in use?)"));
237 return 1;
238 }
239 }
240
241 status = pjsip_tsx_layer_init_module(sip_endpt);
242 pj_assert(status == PJ_SUCCESS);
243
244 status = pjsip_ua_init_module(sip_endpt, NULL);
245 pj_assert(status == PJ_SUCCESS);
246
247 /*
248 * Register our module to receive incoming requests.
249 */
250 status = pjsip_endpt_register_module( sip_endpt, &mod_app);
251 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
252
253 pool = pjsip_endpt_create_pool(sip_endpt, "", 1000, 1000);
254
255 status = pj_thread_create(pool, "", &worker_thread, NULL, 0, 0, &thread);
256 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
257
258 printf("Destination URL: %s\n", argv[1]);
259
260 for (;;) {
261 char line[10];
262
263 fgets(line, sizeof(line), stdin);
264
265 switch (line[0]) {
266 case 'm':
267 make_call(argv[1], PJ_FALSE);
268 break;
269 case 'M':
270 make_call(argv[1], PJ_TRUE);
271 break;
272 case 'r':
273 reinvite(PJ_FALSE);
274 break;
275 case 'R':
276 reinvite(PJ_TRUE);
277 break;
278 case 'h':
279 hangup();
280 break;
281 case 'q':
282 goto on_quit;
283 }
284 }
285
286on_quit:
287 quit_flag = 1;
288 pj_thread_join(thread);
289
290 pjsip_endpt_destroy(sip_endpt);
291 pj_caching_pool_destroy(&cp);
292 pj_shutdown();
293 return 0;
294}
295