blob: 6268b55e0305a6d3faaaf4b035410069fef6da3f [file] [log] [blame]
Alexandre Lision8af73cb2013-12-10 14:11:20 -05001/* $Id: pjsua_app_callback.cpp $ */
2/*
3 * Copyright (C) 2012-2012 Teluu Inc. (http://www.teluu.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include "pjsua_app_callback.h"
21#include "../../pjsua_app.h"
22#include "../../pjsua_app_config.h"
23
24#if defined(PJ_ANDROID) && PJ_ANDROID != 0
25
26static PjsuaAppCallback* registeredCallbackObject = NULL;
27static pjsua_app_cfg_t android_app_config;
28static int restart_argc;
29static char **restart_argv;
30
31extern const char *pjsua_app_def_argv[];
32
33#define THIS_FILE "pjsua_app_callback.cpp"
34
35/** Callback wrapper **/
36void on_cli_started(pj_status_t status, const char *msg)
37{
38 char errmsg[PJ_ERR_MSG_SIZE];
39 if (registeredCallbackObject) {
40 if ((status != PJ_SUCCESS) && (!msg || !*msg)) {
41 pj_strerror(status, errmsg, sizeof(errmsg));
42 msg = errmsg;
43 }
44 registeredCallbackObject->onStarted(msg);
45 }
46}
47
48void on_cli_stopped(pj_bool_t restart, int argc, char **argv)
49{
50 if (restart) {
51 restart_argc = argc;
52 restart_argv = argv;
53 }
54
55 if (registeredCallbackObject) {
56 registeredCallbackObject->onStopped(restart);
57 }
58}
59
60static int initMain(int argc, char **argv)
61{
62 pj_status_t status;
63 android_app_config.argc = argc;
64 android_app_config.argv = argv;
65
66 status = pjsua_app_init(&android_app_config);
67 if (status == PJ_SUCCESS) {
68 status = pjsua_app_run(PJ_FALSE);
69 } else {
70 pjsua_app_destroy();
71 }
72
73 return status;
74}
75
76int pjsuaStart()
77{
78 pj_status_t status;
79
80 const char **argv = pjsua_app_def_argv;
81 int argc = pjsua_app_def_argc;
82
83 pj_bzero(&android_app_config, sizeof(android_app_config));
84
85 android_app_config.on_started = &on_cli_started;
86 android_app_config.on_stopped = &on_cli_stopped;
87
88 return initMain(argc, (char**)argv);
89}
90
91void pjsuaDestroy()
92{
93 pjsua_app_destroy();
94
95 /** This is on purpose **/
96 pjsua_app_destroy();
97}
98
99int pjsuaRestart()
100{
101 pj_status_t status;
102
103 pjsuaDestroy();
104
105 return initMain(restart_argc, restart_argv);
106}
107
108void setCallbackObject(PjsuaAppCallback* callback)
109{
110 registeredCallbackObject = callback;
111}
112
113#endif