| 1 |
/* $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 |
|
| 26 |
#include <android/log.h> |
| 27 |
|
| 28 |
static PjsuaAppCallback* registeredCallbackObject = NULL; |
| 29 |
static pjsua_app_cfg_t android_app_config; |
| 30 |
static int restart_argc; |
| 31 |
static char **restart_argv; |
| 32 |
static pjsua_callback pjsua_cb_orig; |
| 33 |
static pjsua_vid_win_id winId = PJSUA_INVALID_ID; |
| 34 |
|
| 35 |
void setVideoWindow(const WindowHandle& win) |
| 36 |
{ |
| 37 |
pjmedia_vid_dev_hwnd vhwnd; |
| 38 |
|
| 39 |
vhwnd.info.window = win.window; |
| 40 |
if (winId != PJSUA_INVALID_ID) |
| 41 |
pjsua_vid_win_set_win(winId, &vhwnd); |
| 42 |
} |
| 43 |
|
| 44 |
extern const char *pjsua_app_def_argv[]; |
| 45 |
|
| 46 |
#define THIS_FILE "pjsua_app_callback.cpp" |
| 47 |
|
| 48 |
extern "C" { |
| 49 |
static void log_writer(int level, const char *data, int len) |
| 50 |
{ |
| 51 |
__android_log_write(ANDROID_LOG_INFO, "pjsua", data); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
static void on_call_media_state(pjsua_call_id call_id) |
| 56 |
{ |
| 57 |
#if PJMEDIA_HAS_VIDEO |
| 58 |
pjsua_call_info call_info; |
| 59 |
unsigned mi; |
| 60 |
|
| 61 |
pjsua_call_get_info(call_id, &call_info); |
| 62 |
|
| 63 |
for (mi=0; mi<call_info.media_cnt; ++mi) { |
| 64 |
pjsua_call_media_info *med_info = &call_info.media[mi]; |
| 65 |
if (med_info->type == PJMEDIA_TYPE_VIDEO && |
| 66 |
med_info->status == PJSUA_CALL_MEDIA_ACTIVE && |
| 67 |
med_info->stream.vid.win_in != PJSUA_INVALID_ID) |
| 68 |
{ |
| 69 |
winId = med_info->stream.vid.win_in; |
| 70 |
registeredCallbackObject->onCallVideoStart(); |
| 71 |
break; |
| 72 |
} |
| 73 |
} |
| 74 |
#endif |
| 75 |
|
| 76 |
/* Forward to original callback */ |
| 77 |
if (pjsua_cb_orig.on_call_media_state) |
| 78 |
(*pjsua_cb_orig.on_call_media_state)(call_id); |
| 79 |
} |
| 80 |
|
| 81 |
/** Callback wrapper **/ |
| 82 |
static void on_cli_config(pjsua_app_config *cfg) |
| 83 |
{ |
| 84 |
pjsua_cb_orig = cfg->cfg.cb; |
| 85 |
cfg->log_cfg.cb = &log_writer; |
| 86 |
|
| 87 |
/* Override pjsua callback, e.g: to install renderer view */ |
| 88 |
cfg->cfg.cb.on_call_media_state = &on_call_media_state; |
| 89 |
} |
| 90 |
|
| 91 |
static void on_cli_started(pj_status_t status, const char *msg) |
| 92 |
{ |
| 93 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 94 |
if (registeredCallbackObject) { |
| 95 |
if ((status != PJ_SUCCESS) && (!msg || !*msg)) { |
| 96 |
pj_strerror(status, errmsg, sizeof(errmsg)); |
| 97 |
msg = errmsg; |
| 98 |
} |
| 99 |
registeredCallbackObject->onStarted(msg); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
static void on_cli_stopped(pj_bool_t restart, int argc, char **argv) |
| 104 |
{ |
| 105 |
if (restart) { |
| 106 |
restart_argc = argc; |
| 107 |
restart_argv = argv; |
| 108 |
} |
| 109 |
|
| 110 |
if (registeredCallbackObject) { |
| 111 |
registeredCallbackObject->onStopped(restart); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
static int initMain(int argc, char **argv) |
| 116 |
{ |
| 117 |
pj_status_t status; |
| 118 |
android_app_config.argc = argc; |
| 119 |
android_app_config.argv = argv; |
| 120 |
|
| 121 |
status = pjsua_app_init(&android_app_config); |
| 122 |
if (status == PJ_SUCCESS) { |
| 123 |
status = pjsua_app_run(PJ_FALSE); |
| 124 |
} else { |
| 125 |
pjsua_app_destroy(); |
| 126 |
} |
| 127 |
|
| 128 |
return status; |
| 129 |
} |
| 130 |
|
| 131 |
int pjsuaStart() |
| 132 |
{ |
| 133 |
pj_status_t status; |
| 134 |
|
| 135 |
const char **argv = pjsua_app_def_argv; |
| 136 |
int argc = pjsua_app_def_argc; |
| 137 |
|
| 138 |
pj_bzero(&android_app_config, sizeof(android_app_config)); |
| 139 |
|
| 140 |
android_app_config.on_config_init = &on_cli_config; |
| 141 |
android_app_config.on_started = &on_cli_started; |
| 142 |
android_app_config.on_stopped = &on_cli_stopped; |
| 143 |
|
| 144 |
return initMain(argc, (char**)argv); |
| 145 |
} |
| 146 |
|
| 147 |
void pjsuaDestroy() |
| 148 |
{ |
| 149 |
pjsua_app_destroy(); |
| 150 |
|
| 151 |
/** This is on purpose **/ |
| 152 |
pjsua_app_destroy(); |
| 153 |
} |
| 154 |
|
| 155 |
int pjsuaRestart() |
| 156 |
{ |
| 157 |
pj_status_t status; |
| 158 |
|
| 159 |
pjsuaDestroy(); |
| 160 |
|
| 161 |
return initMain(restart_argc, restart_argv); |
| 162 |
} |
| 163 |
|
| 164 |
void setCallbackObject(PjsuaAppCallback* callback) |
| 165 |
{ |
| 166 |
registeredCallbackObject = callback; |
| 167 |
} |
| 168 |
|
| 169 |
#endif |