1
/* $Id: invtester.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
24
* Send INVITE/re-INVITE without SDP.
28
/* Include all headers. */
30
#include <pjlib-util.h>
33
#define THIS_FILE "invtester.c"
36
#define PORT_STR ":50060"
40
static pjsip_endpoint *sip_endpt;
41
static pj_bool_t quit_flag;
42
static pjsip_dialog *dlg;
45
/* Callback to handle incoming requests. */
46
static void on_tsx_state(pjsip_transaction *tsx, pjsip_event *event);
48
static pjsip_module mod_app =
50
NULL, NULL, /* prev, next. */
51
{ "mod-app", 7 }, /* Name. */
53
PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
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() */
67
static int worker_thread(void *arg)
72
pj_time_val timeout = {0, 500};
73
pjsip_endpt_handle_events(sip_endpt, &timeout);
80
static void send_request(const pjsip_method *method,
82
const pj_str_t *branch,
86
pj_str_t dummy_sdp_str =
89
"o=- 3360842071 3360842071 IN IP4 192.168.0.68\r\n"
91
"c=IN IP4 192.168.0.68\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"
97
"a=rtpmap:101 telephone-event/8000\r\n"
98
"a=fmtp:101 0-15\r\n",
103
status = pjsip_dlg_create_request(dlg, method, cseq, &tdata);
104
pj_assert(status == PJ_SUCCESS);
109
via = (pjsip_via_hdr*) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
110
pj_strdup(tdata->pool, &via->branch_param, branch);
114
pjsip_msg_body *body;
115
pj_str_t mime_application = { "application", 11};
116
pj_str_t mime_sdp = {"sdp", 3};
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,
123
tdata->msg->body = body;
126
status = pjsip_dlg_send_request(dlg, tdata, -1, NULL);
127
pj_assert(status == PJ_SUCCESS);
130
/* Callback to handle incoming requests. */
131
static void on_tsx_state(pjsip_transaction *tsx, pjsip_event *event)
133
if (tsx->role == PJSIP_ROLE_UAC) {
134
if (tsx->method.id == PJSIP_INVITE_METHOD && tsx->state == PJSIP_TSX_STATE_TERMINATED) {
136
send_request(&pjsip_ack_method, tsx->cseq, &tsx->branch, ACK_HAS_SDP);
138
send_request(&pjsip_ack_method, tsx->cseq, NULL, ACK_HAS_SDP);
143
if (event->type == PJSIP_EVENT_RX_MSG && tsx->state == PJSIP_TSX_STATE_TRYING) {
144
pjsip_tx_data *tdata;
146
pjsip_dlg_create_response(dlg, event->body.tsx_state.src.rdata,
148
pjsip_dlg_send_response(dlg, tsx, tdata);
154
void make_call(char *uri, pj_bool_t with_offer)
156
pj_str_t local = pj_str("sip:localhost" PORT_STR);
157
pj_str_t remote = pj_str(uri);
160
status = pjsip_dlg_create_uac(pjsip_ua_instance(),
161
&local, &local, &remote, &remote, &dlg);
162
pj_assert(status == PJ_SUCCESS);
164
pjsip_dlg_inc_lock(dlg);
166
status = pjsip_dlg_add_usage(dlg, &mod_app, NULL);
167
pj_assert(status == PJ_SUCCESS);
169
pjsip_dlg_inc_session(dlg, &mod_app);
171
send_request(&pjsip_invite_method, -1, NULL, with_offer);
173
pjsip_dlg_dec_lock(dlg);
177
void reinvite(pj_bool_t with_offer)
179
send_request(&pjsip_invite_method, -1, NULL, with_offer);
185
send_request(&pjsip_bye_method, -1, NULL, PJ_FALSE);
186
pjsip_dlg_dec_session(dlg, &mod_app);
193
int main(int argc, char *argv[])
201
puts("Error: destination URL needed");
205
/* Must init PJLIB first: */
207
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
210
/* Then init PJLIB-UTIL: */
211
status = pjlib_util_init();
212
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
214
/* Must create a pool factory before we can allocate any memory. */
215
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
218
/* Create the endpoint: */
219
status = pjsip_endpt_create(&cp.factory, "sipstateless",
221
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
224
* Add UDP transport, with hard-coded port
229
addr.sin_family = pj_AF_INET();
230
addr.sin_addr.s_addr = 0;
231
addr.sin_port = pj_htons(PORT);
233
status = pjsip_udp_transport_start( sip_endpt, &addr, NULL, 1, NULL);
234
if (status != PJ_SUCCESS) {
236
"Error starting UDP transport (port in use?)"));
241
status = pjsip_tsx_layer_init_module(sip_endpt);
242
pj_assert(status == PJ_SUCCESS);
244
status = pjsip_ua_init_module(sip_endpt, NULL);
245
pj_assert(status == PJ_SUCCESS);
248
* Register our module to receive incoming requests.
250
status = pjsip_endpt_register_module( sip_endpt, &mod_app);
251
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
253
pool = pjsip_endpt_create_pool(sip_endpt, "", 1000, 1000);
255
status = pj_thread_create(pool, "", &worker_thread, NULL, 0, 0, &thread);
256
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
258
printf("Destination URL: %s\n", argv[1]);
263
fgets(line, sizeof(line), stdin);
267
make_call(argv[1], PJ_FALSE);
270
make_call(argv[1], PJ_TRUE);
288
pj_thread_join(thread);
290
pjsip_endpt_destroy(sip_endpt);
291
pj_caching_pool_destroy(&cp);