PageRenderTime 66ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/pjsip-apps/src/pjsua/symbian/src/pjsuaAppUi.cpp

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