Alexandre Lision | 8af73cb | 2013-12-10 14:11:20 -0500 | [diff] [blame] | 1 | /* |
| 2 | ======================================================================== |
| 3 | Name : pjsuaAppUi.cpp |
| 4 | Author : nanang |
| 5 | Copyright : Copyright (C) 2013 Teluu Inc. (http://www.teluu.com) |
| 6 | Description : |
| 7 | ======================================================================== |
| 8 | */ |
| 9 | // [[[ begin generated region: do not modify [Generated System Includes] |
| 10 | #include <eikmenub.h> |
| 11 | #include <akncontext.h> |
| 12 | #include <akntitle.h> |
| 13 | #include <pjsua.rsg> |
| 14 | // ]]] end generated region [Generated System Includes] |
| 15 | |
| 16 | // [[[ begin generated region: do not modify [Generated User Includes] |
| 17 | #include "pjsuaAppUi.h" |
| 18 | #include "pjsua.hrh" |
| 19 | #include "pjsuaContainerView.h" |
| 20 | // ]]] end generated region [Generated User Includes] |
| 21 | |
| 22 | // [[[ begin generated region: do not modify [Generated Constants] |
| 23 | // ]]] end generated region [Generated Constants] |
| 24 | |
| 25 | #include "../../pjsua_app.h" |
| 26 | #include "../../pjsua_app_config.h" |
| 27 | |
| 28 | /* Global vars */ |
| 29 | static CpjsuaAppUi *appui = NULL; |
| 30 | static pj_ioqueue_t *app_ioqueue = NULL; |
| 31 | static int start_argc = 0; |
| 32 | static char **start_argv = NULL; |
| 33 | |
| 34 | static pj_status_t InitSymbSocket(); |
| 35 | static void DestroySymbSocket(); |
| 36 | |
| 37 | /* Helper funtions to init/restart/destroy the pjsua */ |
| 38 | static void PjsuaInitL(); |
| 39 | static void PjsuaDestroyL(); |
| 40 | |
| 41 | /* pjsua app callbacks */ |
| 42 | static void PjsuaOnStarted(pj_status_t status, const char* title); |
| 43 | static void PjsuaOnStopped(pj_bool_t restart, int argc, char** argv); |
| 44 | static void PjsuaOnConfig(pjsua_app_config *cfg); |
| 45 | |
| 46 | /* Helper class to schedule function execution */ |
| 47 | class MyTimer : public CActive |
| 48 | { |
| 49 | public: |
| 50 | typedef void (*timer_func)(); |
| 51 | |
| 52 | public: |
| 53 | static MyTimer* NewL(int ms, timer_func f) { |
| 54 | MyTimer *self = new MyTimer(f); |
| 55 | CleanupStack::PushL(self); |
| 56 | self->ConstructL(ms); |
| 57 | CleanupStack::Pop(self); |
| 58 | return self; |
| 59 | } |
| 60 | |
| 61 | MyTimer(timer_func f) : CActive(EPriorityStandard), func(f) {} |
| 62 | ~MyTimer() { |
| 63 | Cancel(); |
| 64 | rtimer.Close(); |
| 65 | } |
| 66 | |
| 67 | virtual void RunL() { (*func)(); delete this; } |
| 68 | virtual void DoCancel() { rtimer.Cancel(); } |
| 69 | |
| 70 | private: |
| 71 | RTimer rtimer; |
| 72 | timer_func func; |
| 73 | |
| 74 | void ConstructL(int ms) { |
| 75 | rtimer.CreateLocal(); |
| 76 | CActiveScheduler::Add(this); |
| 77 | rtimer.After(iStatus, ms * 1000); |
| 78 | SetActive(); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * Construct the CpjsuaAppUi instance |
| 84 | */ |
| 85 | CpjsuaAppUi::CpjsuaAppUi() |
| 86 | { |
| 87 | // [[[ begin generated region: do not modify [Generated Contents] |
| 88 | // ]]] end generated region [Generated Contents] |
| 89 | |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * The appui's destructor removes the container from the control |
| 94 | * stack and destroys it. |
| 95 | */ |
| 96 | CpjsuaAppUi::~CpjsuaAppUi() |
| 97 | { |
| 98 | // [[[ begin generated region: do not modify [Generated Contents] |
| 99 | // ]]] end generated region [Generated Contents] |
| 100 | |
| 101 | } |
| 102 | |
| 103 | // [[[ begin generated function: do not modify |
| 104 | void CpjsuaAppUi::InitializeContainersL() |
| 105 | { |
| 106 | iPjsuaContainerView = CpjsuaContainerView::NewL(); |
| 107 | AddViewL( iPjsuaContainerView ); |
| 108 | SetDefaultViewL( *iPjsuaContainerView ); |
| 109 | } |
| 110 | // ]]] end generated function |
| 111 | |
| 112 | /** |
| 113 | * Handle a command for this appui (override) |
| 114 | * @param aCommand command id to be handled |
| 115 | */ |
| 116 | void CpjsuaAppUi::HandleCommandL( TInt aCommand ) |
| 117 | { |
| 118 | // [[[ begin generated region: do not modify [Generated Code] |
| 119 | TBool commandHandled = EFalse; |
| 120 | switch ( aCommand ) |
| 121 | { // code to dispatch to the AppUi's menu and CBA commands is generated here |
| 122 | default: |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | if ( !commandHandled ) |
| 128 | { |
| 129 | if ( aCommand == EAknSoftkeyExit || aCommand == EEikCmdExit ) |
| 130 | { |
| 131 | Exit(); |
| 132 | } |
| 133 | } |
| 134 | // ]]] end generated region [Generated Code] |
| 135 | |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Override of the HandleResourceChangeL virtual function |
| 140 | */ |
| 141 | void CpjsuaAppUi::HandleResourceChangeL( TInt aType ) |
| 142 | { |
| 143 | CAknViewAppUi::HandleResourceChangeL( aType ); |
| 144 | // [[[ begin generated region: do not modify [Generated Code] |
| 145 | // ]]] end generated region [Generated Code] |
| 146 | |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Override of the HandleKeyEventL virtual function |
| 151 | * @return EKeyWasConsumed if event was handled, EKeyWasNotConsumed if not |
| 152 | * @param aKeyEvent |
| 153 | * @param aType |
| 154 | */ |
| 155 | TKeyResponse CpjsuaAppUi::HandleKeyEventL( |
| 156 | const TKeyEvent& aKeyEvent, |
| 157 | TEventCode aType ) |
| 158 | { |
| 159 | // The inherited HandleKeyEventL is private and cannot be called |
| 160 | // [[[ begin generated region: do not modify [Generated Contents] |
| 161 | // ]]] end generated region [Generated Contents] |
| 162 | |
| 163 | // Left or right softkey pressed |
| 164 | if (aType==EEventKeyDown && |
| 165 | (aKeyEvent.iScanCode == EStdKeyDevice0 || |
| 166 | aKeyEvent.iScanCode == EStdKeyDevice1)) |
| 167 | { |
| 168 | Cba()->MakeVisible(ETrue); |
| 169 | } else { |
| 170 | Cba()->MakeVisible(EFalse); |
| 171 | } |
| 172 | |
| 173 | return EKeyWasNotConsumed; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Override of the HandleViewDeactivation virtual function |
| 178 | * |
| 179 | * @param aViewIdToBeDeactivated |
| 180 | * @param aNewlyActivatedViewId |
| 181 | */ |
| 182 | void CpjsuaAppUi::HandleViewDeactivation( |
| 183 | const TVwsViewId& aViewIdToBeDeactivated, |
| 184 | const TVwsViewId& aNewlyActivatedViewId ) |
| 185 | { |
| 186 | CAknViewAppUi::HandleViewDeactivation( |
| 187 | aViewIdToBeDeactivated, |
| 188 | aNewlyActivatedViewId ); |
| 189 | // [[[ begin generated region: do not modify [Generated Contents] |
| 190 | // ]]] end generated region [Generated Contents] |
| 191 | |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @brief Completes the second phase of Symbian object construction. |
| 196 | * Put initialization code that could leave here. |
| 197 | */ |
| 198 | void CpjsuaAppUi::ConstructL() |
| 199 | { |
| 200 | // [[[ begin generated region: do not modify [Generated Contents] |
| 201 | |
| 202 | BaseConstructL( EAknEnableSkin | |
| 203 | EAknEnableMSK ); |
| 204 | InitializeContainersL(); |
| 205 | // ]]] end generated region [Generated Contents] |
| 206 | |
| 207 | // Save pointer to this AppUi |
| 208 | appui = this; |
| 209 | |
| 210 | // Full screen |
| 211 | StatusPane()->MakeVisible(EFalse); |
| 212 | Cba()->MakeVisible(EFalse); |
| 213 | |
| 214 | if (InitSymbSocket() != PJ_SUCCESS) { |
| 215 | PutMsg("Failed to initialize Symbian network param."); |
| 216 | } else { |
| 217 | start_argc = pjsua_app_def_argc; |
| 218 | start_argv = (char**)pjsua_app_def_argv; |
| 219 | |
| 220 | // Schedule Lib Init |
| 221 | MyTimer::NewL(100, &PjsuaInitL); |
| 222 | } |
| 223 | |
| 224 | } |
| 225 | |
| 226 | /* Called by Symbian GUI framework when app is about to exit */ |
| 227 | void CpjsuaAppUi::PrepareToExit() |
| 228 | { |
| 229 | TRAPD(result, PjsuaDestroyL()); |
| 230 | DestroySymbSocket(); |
| 231 | CloseSTDLIB(); |
| 232 | CAknViewAppUi::PrepareToExit(); |
| 233 | } |
| 234 | |
| 235 | /* Print message on screen */ |
| 236 | void CpjsuaAppUi::PutMsg(const char *msg) |
| 237 | { |
| 238 | iPjsuaContainerView->PutMessage(msg); |
| 239 | } |
| 240 | |
| 241 | #include <es_sock.h> |
| 242 | |
| 243 | static RSocketServ aSocketServer; |
| 244 | static RConnection aConn; |
| 245 | |
| 246 | /* Called when pjsua is started */ |
| 247 | void PjsuaOnStarted(pj_status_t status, const char* title) |
| 248 | { |
| 249 | char err_msg[128]; |
| 250 | |
| 251 | if (status != PJ_SUCCESS || title == NULL) { |
| 252 | char err_str[PJ_ERR_MSG_SIZE]; |
| 253 | pj_strerror(status, err_str, sizeof(err_str)); |
| 254 | pj_ansi_snprintf(err_msg, sizeof(err_msg), "%s: %s", |
| 255 | (title?title:"App start error"), err_str); |
| 256 | title = err_msg; |
| 257 | } |
| 258 | |
| 259 | appui->PutMsg(title); |
| 260 | } |
| 261 | |
| 262 | /* Called when pjsua is stopped */ |
| 263 | void PjsuaOnStopped(pj_bool_t restart, int argc, char** argv) |
| 264 | { |
| 265 | if (restart) { |
| 266 | start_argc = argc; |
| 267 | start_argv = argv; |
| 268 | |
| 269 | // Schedule Lib Init |
| 270 | MyTimer::NewL(100, &PjsuaInitL); |
| 271 | } else { |
| 272 | /* Destroy & quit GUI, e.g: clean up window, resources */ |
| 273 | appui->Exit(); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* Called before pjsua initializing config. |
| 278 | * We need to override some settings here. |
| 279 | */ |
| 280 | void PjsuaOnConfig(pjsua_app_config *cfg) |
| 281 | { |
| 282 | /* Disable threading */ |
| 283 | cfg->cfg.thread_cnt = 0; |
| 284 | cfg->cfg.thread_cnt = 0; |
| 285 | cfg->media_cfg.thread_cnt = 0; |
| 286 | cfg->media_cfg.has_ioqueue = PJ_FALSE; |
| 287 | |
| 288 | /* Create ioqueue for telnet CLI */ |
| 289 | if (app_ioqueue == NULL) { |
| 290 | pj_ioqueue_create(cfg->pool, 0, &app_ioqueue); |
| 291 | } |
| 292 | cfg->cli_cfg.telnet_cfg.ioqueue = app_ioqueue; |
| 293 | } |
| 294 | |
| 295 | // Set Symbian OS parameters in pjlib. |
| 296 | // This must be done before pj_init() is called. |
| 297 | pj_status_t InitSymbSocket() |
| 298 | { |
| 299 | pj_symbianos_params sym_params; |
| 300 | TInt err; |
| 301 | |
| 302 | // Initialize RSocketServ |
| 303 | if ((err=aSocketServer.Connect(32)) != KErrNone) { |
| 304 | return PJ_STATUS_FROM_OS(err); |
| 305 | } |
| 306 | |
| 307 | // Open up a connection |
| 308 | if ((err=aConn.Open(aSocketServer)) != KErrNone) { |
| 309 | aSocketServer.Close(); |
| 310 | return PJ_STATUS_FROM_OS(err); |
| 311 | } |
| 312 | if ((err=aConn.Start()) != KErrNone) { |
| 313 | aConn.Close(); |
| 314 | aSocketServer.Close(); |
| 315 | return PJ_STATUS_FROM_OS(err); |
| 316 | } |
| 317 | |
| 318 | pj_bzero(&sym_params, sizeof(sym_params)); |
| 319 | sym_params.rsocketserv = &aSocketServer; |
| 320 | sym_params.rconnection = &aConn; |
| 321 | pj_symbianos_set_params(&sym_params); |
| 322 | |
| 323 | return PJ_SUCCESS; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | void DestroySymbSocket() |
| 328 | { |
| 329 | aConn.Close(); |
| 330 | aSocketServer.Close(); |
| 331 | } |
| 332 | |
| 333 | |
| 334 | void PjsuaInitL() |
| 335 | { |
| 336 | pjsua_app_cfg_t app_cfg; |
| 337 | pj_status_t status; |
| 338 | |
| 339 | PjsuaDestroyL(); |
| 340 | |
| 341 | pj_bzero(&app_cfg, sizeof(app_cfg)); |
| 342 | app_cfg.argc = start_argc; |
| 343 | app_cfg.argv = start_argv; |
| 344 | app_cfg.on_started = &PjsuaOnStarted; |
| 345 | app_cfg.on_stopped = &PjsuaOnStopped; |
| 346 | app_cfg.on_config_init = &PjsuaOnConfig; |
| 347 | |
| 348 | appui->PutMsg("Initializing.."); |
| 349 | status = pjsua_app_init(&app_cfg); |
| 350 | if (status != PJ_SUCCESS) |
| 351 | goto on_return; |
| 352 | |
| 353 | appui->PutMsg("Starting.."); |
| 354 | status = pjsua_app_run(PJ_FALSE); |
| 355 | if (status != PJ_SUCCESS) |
| 356 | goto on_return; |
| 357 | |
| 358 | on_return: |
| 359 | if (status != PJ_SUCCESS) |
| 360 | appui->PutMsg("Initialization failed"); |
| 361 | } |
| 362 | |
| 363 | void PjsuaDestroyL() |
| 364 | { |
| 365 | if (app_ioqueue) { |
| 366 | pj_ioqueue_destroy(app_ioqueue); |
| 367 | app_ioqueue = NULL; |
| 368 | } |
| 369 | pjsua_app_destroy(); |
| 370 | } |