pjsua_app.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  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. #include "pjsua_app.h"
  20. #define THIS_FILE "pjsua_app.c"
  21. //#define STEREO_DEMO
  22. //#define TRANSPORT_ADAPTER_SAMPLE
  23. //#define HAVE_MULTIPART_TEST
  24. /* Ringtones US UK */
  25. #define RINGBACK_FREQ1 440 /* 400 */
  26. #define RINGBACK_FREQ2 480 /* 450 */
  27. #define RINGBACK_ON 2000 /* 400 */
  28. #define RINGBACK_OFF 4000 /* 200 */
  29. #define RINGBACK_CNT 1 /* 2 */
  30. #define RINGBACK_INTERVAL 4000 /* 2000 */
  31. #define RING_FREQ1 800
  32. #define RING_FREQ2 640
  33. #define RING_ON 200
  34. #define RING_OFF 100
  35. #define RING_CNT 3
  36. #define RING_INTERVAL 3000
  37. #define current_acc pjsua_acc_get_default()
  38. #ifdef STEREO_DEMO
  39. static void stereo_demo();
  40. #endif
  41. #ifdef USE_GUI
  42. pj_bool_t showNotification(pjsua_call_id call_id);
  43. pj_bool_t reportCallState(pjsua_call_id call_id);
  44. #endif
  45. static void ringback_start(pjsua_call_id call_id);
  46. static void ring_start(pjsua_call_id call_id);
  47. static void ring_stop(pjsua_call_id call_id);
  48. static pj_status_t app_init(void);
  49. static pj_status_t app_destroy(void);
  50. static pjsua_app_cfg_t app_cfg;
  51. pj_str_t uri_arg;
  52. pj_bool_t app_running = PJ_FALSE;
  53. /*****************************************************************************
  54. * Configuration manipulation
  55. */
  56. /*****************************************************************************
  57. * Callback
  58. */
  59. static void ringback_start(pjsua_call_id call_id)
  60. {
  61. if (app_config.no_tones)
  62. return;
  63. if (app_config.call_data[call_id].ringback_on)
  64. return;
  65. app_config.call_data[call_id].ringback_on = PJ_TRUE;
  66. if (++app_config.ringback_cnt==1 &&
  67. app_config.ringback_slot!=PJSUA_INVALID_ID)
  68. {
  69. pjsua_conf_connect(app_config.ringback_slot, 0);
  70. }
  71. }
  72. static void ring_stop(pjsua_call_id call_id)
  73. {
  74. if (app_config.no_tones)
  75. return;
  76. if (app_config.call_data[call_id].ringback_on) {
  77. app_config.call_data[call_id].ringback_on = PJ_FALSE;
  78. pj_assert(app_config.ringback_cnt>0);
  79. if (--app_config.ringback_cnt == 0 &&
  80. app_config.ringback_slot!=PJSUA_INVALID_ID)
  81. {
  82. pjsua_conf_disconnect(app_config.ringback_slot, 0);
  83. pjmedia_tonegen_rewind(app_config.ringback_port);
  84. }
  85. }
  86. if (app_config.call_data[call_id].ring_on) {
  87. app_config.call_data[call_id].ring_on = PJ_FALSE;
  88. pj_assert(app_config.ring_cnt>0);
  89. if (--app_config.ring_cnt == 0 &&
  90. app_config.ring_slot!=PJSUA_INVALID_ID)
  91. {
  92. pjsua_conf_disconnect(app_config.ring_slot, 0);
  93. pjmedia_tonegen_rewind(app_config.ring_port);
  94. }
  95. }
  96. }
  97. static void ring_start(pjsua_call_id call_id)
  98. {
  99. if (app_config.no_tones)
  100. return;
  101. if (app_config.call_data[call_id].ring_on)
  102. return;
  103. app_config.call_data[call_id].ring_on = PJ_TRUE;
  104. if (++app_config.ring_cnt==1 &&
  105. app_config.ring_slot!=PJSUA_INVALID_ID)
  106. {
  107. pjsua_conf_connect(app_config.ring_slot, 0);
  108. }
  109. }
  110. /* Callback from timer when the maximum call duration has been
  111. * exceeded.
  112. */
  113. static void call_timeout_callback(pj_timer_heap_t *timer_heap,
  114. struct pj_timer_entry *entry)
  115. {
  116. pjsua_call_id call_id = entry->id;
  117. pjsua_msg_data msg_data_;
  118. pjsip_generic_string_hdr warn;
  119. pj_str_t hname = pj_str("Warning");
  120. pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
  121. PJ_UNUSED_ARG(timer_heap);
  122. if (call_id == PJSUA_INVALID_ID) {
  123. PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
  124. return;
  125. }
  126. /* Add warning header */
  127. pjsua_msg_data_init(&msg_data_);
  128. pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
  129. pj_list_push_back(&msg_data_.hdr_list, &warn);
  130. /* Call duration has been exceeded; disconnect the call */
  131. PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
  132. "for call %d, disconnecting the call",
  133. app_config.duration, call_id));
  134. entry->id = PJSUA_INVALID_ID;
  135. pjsua_call_hangup(call_id, 200, NULL, &msg_data_);
  136. }
  137. /*
  138. * Handler when invite state has changed.
  139. */
  140. static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
  141. {
  142. pjsua_call_info call_info;
  143. PJ_UNUSED_ARG(e);
  144. #ifdef USE_GUI
  145. reportCallState(call_id);
  146. #endif
  147. pjsua_call_get_info(call_id, &call_info);
  148. if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
  149. /* Stop all ringback for this call */
  150. ring_stop(call_id);
  151. /* Cancel duration timer, if any */
  152. if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
  153. app_call_data *cd = &app_config.call_data[call_id];
  154. pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
  155. cd->timer.id = PJSUA_INVALID_ID;
  156. pjsip_endpt_cancel_timer(endpt, &cd->timer);
  157. }
  158. /* Rewind play file when hangup automatically,
  159. * since file is not looped
  160. */
  161. if (app_config.auto_play_hangup)
  162. pjsua_player_set_pos(app_config.wav_id, 0);
  163. PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%.*s)]",
  164. call_id,
  165. call_info.last_status,
  166. (int)call_info.last_status_text.slen,
  167. call_info.last_status_text.ptr));
  168. if (call_id == current_call) {
  169. find_next_call();
  170. }
  171. /* Dump media state upon disconnected.
  172. * Now pjsua_media_channel_deinit() automatically log the call dump.
  173. */
  174. if (0) {
  175. PJ_LOG(5,(THIS_FILE,
  176. "Call %d disconnected, dumping media stats..",
  177. call_id));
  178. log_call_dump(call_id);
  179. }
  180. } else {
  181. if (app_config.duration != PJSUA_APP_NO_LIMIT_DURATION &&
  182. call_info.state == PJSIP_INV_STATE_CONFIRMED)
  183. {
  184. /* Schedule timer to hangup call after the specified duration */
  185. app_call_data *cd = &app_config.call_data[call_id];
  186. pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
  187. pj_time_val delay;
  188. cd->timer.id = call_id;
  189. delay.sec = app_config.duration;
  190. delay.msec = 0;
  191. pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
  192. }
  193. if (call_info.state == PJSIP_INV_STATE_EARLY) {
  194. int code;
  195. pj_str_t reason;
  196. pjsip_msg *msg;
  197. /* This can only occur because of TX or RX message */
  198. pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
  199. if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
  200. msg = e->body.tsx_state.src.rdata->msg_info.msg;
  201. } else {
  202. msg = e->body.tsx_state.src.tdata->msg;
  203. }
  204. code = msg->line.status.code;
  205. reason = msg->line.status.reason;
  206. /* Start ringback for 180 for UAC unless there's SDP in 180 */
  207. if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
  208. msg->body == NULL &&
  209. call_info.media_status==PJSUA_CALL_MEDIA_NONE)
  210. {
  211. ringback_start(call_id);
  212. }
  213. PJ_LOG(3,(THIS_FILE, "Call %d state changed to %.*s (%d %.*s)",
  214. call_id, (int)call_info.state_text.slen,
  215. call_info.state_text.ptr, code,
  216. (int)reason.slen, reason.ptr));
  217. } else {
  218. PJ_LOG(3,(THIS_FILE, "Call %d state changed to %.*s",
  219. call_id,
  220. (int)call_info.state_text.slen,
  221. call_info.state_text.ptr));
  222. }
  223. if (current_call==PJSUA_INVALID_ID)
  224. current_call = call_id;
  225. }
  226. }
  227. /*
  228. * Handler when audio stream is destroyed.
  229. */
  230. static void on_stream_destroyed(pjsua_call_id call_id,
  231. pjmedia_stream *strm,
  232. unsigned stream_idx)
  233. {
  234. PJ_UNUSED_ARG(strm);
  235. /* Now pjsua_media_channel_deinit() automatically log the call dump. */
  236. if (0) {
  237. PJ_LOG(5,(THIS_FILE,
  238. "Call %d stream %d destroyed, dumping media stats..",
  239. call_id, stream_idx));
  240. log_call_dump(call_id);
  241. }
  242. }
  243. /**
  244. * Handler when there is incoming call.
  245. */
  246. static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
  247. pjsip_rx_data *rdata)
  248. {
  249. pjsua_call_info call_info;
  250. PJ_UNUSED_ARG(acc_id);
  251. PJ_UNUSED_ARG(rdata);
  252. pjsua_call_get_info(call_id, &call_info);
  253. if (current_call==PJSUA_INVALID_ID)
  254. current_call = call_id;
  255. #ifdef USE_GUI
  256. showNotification(call_id);
  257. #endif
  258. /* Start ringback */
  259. if (call_info.rem_aud_cnt)
  260. ring_start(call_id);
  261. if (app_config.auto_answer > 0) {
  262. pjsua_call_setting opt;
  263. pjsua_call_setting_default(&opt);
  264. opt.aud_cnt = app_config.aud_cnt;
  265. opt.vid_cnt = app_config.vid.vid_cnt;
  266. pjsua_call_answer2(call_id, &opt, app_config.auto_answer, NULL,
  267. NULL);
  268. }
  269. if (app_config.auto_answer < 200) {
  270. char notif_st[80] = {0};
  271. #if PJSUA_HAS_VIDEO
  272. if (call_info.rem_offerer && call_info.rem_vid_cnt) {
  273. snprintf(notif_st, sizeof(notif_st),
  274. "To %s the video, type \"vid %s\" first, "
  275. "before answering the call!\n",
  276. (app_config.vid.vid_cnt? "reject":"accept"),
  277. (app_config.vid.vid_cnt? "disable":"enable"));
  278. }
  279. #endif
  280. PJ_LOG(3,(THIS_FILE,
  281. "Incoming call for account %d!\n"
  282. "Media count: %d audio & %d video\n"
  283. "%s"
  284. "From: %.*s\n"
  285. "To: %.*s\n"
  286. "Press %s to answer or %s to reject call",
  287. acc_id,
  288. call_info.rem_aud_cnt,
  289. call_info.rem_vid_cnt,
  290. notif_st,
  291. (int)call_info.remote_info.slen,
  292. call_info.remote_info.ptr,
  293. (int)call_info.local_info.slen,
  294. call_info.local_info.ptr,
  295. (app_config.use_cli?"ca a":"a"),
  296. (app_config.use_cli?"g":"h")));
  297. }
  298. }
  299. /* General processing for media state. "mi" is the media index */
  300. static void on_call_generic_media_state(pjsua_call_info *ci, unsigned mi,
  301. pj_bool_t *has_error)
  302. {
  303. const char *status_name[] = {
  304. "None",
  305. "Active",
  306. "Local hold",
  307. "Remote hold",
  308. "Error"
  309. };
  310. PJ_UNUSED_ARG(has_error);
  311. pj_assert(ci->media[mi].status <= PJ_ARRAY_SIZE(status_name));
  312. pj_assert(PJSUA_CALL_MEDIA_ERROR == 4);
  313. PJ_LOG(4,(THIS_FILE, "Call %d media %d [type=%s], status is %s",
  314. ci->id, mi, pjmedia_type_name(ci->media[mi].type),
  315. status_name[ci->media[mi].status]));
  316. }
  317. /* Process audio media state. "mi" is the media index. */
  318. static void on_call_audio_state(pjsua_call_info *ci, unsigned mi,
  319. pj_bool_t *has_error)
  320. {
  321. PJ_UNUSED_ARG(has_error);
  322. /* Stop ringback */
  323. ring_stop(ci->id);
  324. /* Connect ports appropriately when media status is ACTIVE or REMOTE HOLD,
  325. * otherwise we should NOT connect the ports.
  326. */
  327. if (ci->media[mi].status == PJSUA_CALL_MEDIA_ACTIVE ||
  328. ci->media[mi].status == PJSUA_CALL_MEDIA_REMOTE_HOLD)
  329. {
  330. pj_bool_t connect_sound = PJ_TRUE;
  331. pj_bool_t disconnect_mic = PJ_FALSE;
  332. pjsua_conf_port_id call_conf_slot;
  333. call_conf_slot = ci->media[mi].stream.aud.conf_slot;
  334. /* Make sure conf slot is valid (e.g: media dir is not "inactive") */
  335. if (call_conf_slot == PJSUA_INVALID_ID)
  336. return;
  337. /* Loopback sound, if desired */
  338. if (app_config.auto_loop) {
  339. pjsua_conf_connect(call_conf_slot, call_conf_slot);
  340. connect_sound = PJ_FALSE;
  341. }
  342. /* Automatically record conversation, if desired */
  343. if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
  344. pjsua_conf_connect(call_conf_slot, app_config.rec_port);
  345. }
  346. /* Stream a file, if desired */
  347. if ((app_config.auto_play || app_config.auto_play_hangup) &&
  348. app_config.wav_port != PJSUA_INVALID_ID)
  349. {
  350. pjsua_conf_connect(app_config.wav_port, call_conf_slot);
  351. connect_sound = PJ_FALSE;
  352. }
  353. /* Stream AVI, if desired */
  354. if (app_config.avi_auto_play &&
  355. app_config.avi_def_idx != PJSUA_INVALID_ID &&
  356. app_config.avi[app_config.avi_def_idx].slot != PJSUA_INVALID_ID)
  357. {
  358. pjsua_conf_connect(app_config.avi[app_config.avi_def_idx].slot,
  359. call_conf_slot);
  360. disconnect_mic = PJ_TRUE;
  361. }
  362. /* Put call in conference with other calls, if desired */
  363. if (app_config.auto_conf) {
  364. pjsua_call_id call_ids[PJSUA_MAX_CALLS];
  365. unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
  366. unsigned i;
  367. /* Get all calls, and establish media connection between
  368. * this call and other calls.
  369. */
  370. pjsua_enum_calls(call_ids, &call_cnt);
  371. for (i=0; i<call_cnt; ++i) {
  372. if (call_ids[i] == ci->id)
  373. continue;
  374. if (!pjsua_call_has_media(call_ids[i]))
  375. continue;
  376. pjsua_conf_connect(call_conf_slot,
  377. pjsua_call_get_conf_port(call_ids[i]));
  378. pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
  379. call_conf_slot);
  380. /* Automatically record conversation, if desired */
  381. if (app_config.auto_rec && app_config.rec_port !=
  382. PJSUA_INVALID_ID)
  383. {
  384. pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
  385. app_config.rec_port);
  386. }
  387. }
  388. /* Also connect call to local sound device */
  389. connect_sound = PJ_TRUE;
  390. }
  391. /* Otherwise connect to sound device */
  392. if (connect_sound) {
  393. pjsua_conf_connect(call_conf_slot, 0);
  394. if (!disconnect_mic)
  395. pjsua_conf_connect(0, call_conf_slot);
  396. /* Automatically record conversation, if desired */
  397. if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID)
  398. {
  399. pjsua_conf_connect(call_conf_slot, app_config.rec_port);
  400. pjsua_conf_connect(0, app_config.rec_port);
  401. }
  402. }
  403. }
  404. }
  405. /* Process video media state. "mi" is the media index. */
  406. static void on_call_video_state(pjsua_call_info *ci, unsigned mi,
  407. pj_bool_t *has_error)
  408. {
  409. if (ci->media_status != PJSUA_CALL_MEDIA_ACTIVE)
  410. return;
  411. arrange_window(ci->media[mi].stream.vid.win_in);
  412. PJ_UNUSED_ARG(has_error);
  413. }
  414. /*
  415. * Callback on media state changed event.
  416. * The action may connect the call to sound device, to file, or
  417. * to loop the call.
  418. */
  419. static void on_call_media_state(pjsua_call_id call_id)
  420. {
  421. pjsua_call_info call_info;
  422. unsigned mi;
  423. pj_bool_t has_error = PJ_FALSE;
  424. pjsua_call_get_info(call_id, &call_info);
  425. for (mi=0; mi<call_info.media_cnt; ++mi) {
  426. on_call_generic_media_state(&call_info, mi, &has_error);
  427. switch (call_info.media[mi].type) {
  428. case PJMEDIA_TYPE_AUDIO:
  429. on_call_audio_state(&call_info, mi, &has_error);
  430. break;
  431. case PJMEDIA_TYPE_VIDEO:
  432. on_call_video_state(&call_info, mi, &has_error);
  433. break;
  434. default:
  435. /* Make gcc happy about enum not handled by switch/case */
  436. break;
  437. }
  438. }
  439. if (has_error) {
  440. pj_str_t reason = pj_str("Media failed");
  441. pjsua_call_hangup(call_id, 500, &reason, NULL);
  442. }
  443. #if PJSUA_HAS_VIDEO
  444. /* Check if remote has just tried to enable video */
  445. if (call_info.rem_offerer && call_info.rem_vid_cnt)
  446. {
  447. int vid_idx;
  448. /* Check if there is active video */
  449. vid_idx = pjsua_call_get_vid_stream_idx(call_id);
  450. if (vid_idx == -1 || call_info.media[vid_idx].dir == PJMEDIA_DIR_NONE) {
  451. PJ_LOG(3,(THIS_FILE,
  452. "Just rejected incoming video offer on call %d, "
  453. "use \"vid call enable %d\" or \"vid call add\" to "
  454. "enable video!", call_id, vid_idx));
  455. }
  456. }
  457. #endif
  458. }
  459. /*
  460. * DTMF callback.
  461. */
  462. /*
  463. static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
  464. {
  465. PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
  466. }
  467. */
  468. static void call_on_dtmf_callback2(pjsua_call_id call_id,
  469. const pjsua_dtmf_info *info)
  470. {
  471. char duration[16];
  472. char method[16];
  473. duration[0] = '\0';
  474. switch (info->method) {
  475. case PJSUA_DTMF_METHOD_RFC2833:
  476. pj_ansi_snprintf(method, sizeof(method), "RFC2833");
  477. break;
  478. case PJSUA_DTMF_METHOD_SIP_INFO:
  479. pj_ansi_snprintf(method, sizeof(method), "SIP INFO");
  480. pj_ansi_snprintf(duration, sizeof(duration), ":duration(%d)",
  481. info->duration);
  482. break;
  483. };
  484. PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c%s, using %s method",
  485. call_id, info->digit, duration, method));
  486. }
  487. /*
  488. * Redirection handler.
  489. */
  490. static pjsip_redirect_op call_on_redirected(pjsua_call_id call_id,
  491. const pjsip_uri *target,
  492. const pjsip_event *e)
  493. {
  494. PJ_UNUSED_ARG(e);
  495. if (app_config.redir_op == PJSIP_REDIRECT_PENDING) {
  496. char uristr[PJSIP_MAX_URL_SIZE];
  497. int len;
  498. len = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, target, uristr,
  499. sizeof(uristr));
  500. if (len < 1) {
  501. pj_ansi_strxcpy(uristr, "--URI too long--", sizeof(uristr));
  502. }
  503. PJ_LOG(3,(THIS_FILE, "Call %d is being redirected to %.*s. "
  504. "Press 'Ra' to accept+replace To header, 'RA' to accept, "
  505. "'Rr' to reject, or 'Rd' to disconnect.",
  506. call_id, len, uristr));
  507. }
  508. return app_config.redir_op;
  509. }
  510. /*
  511. * Handler registration status has changed.
  512. */
  513. static void on_reg_state(pjsua_acc_id acc_id)
  514. {
  515. PJ_UNUSED_ARG(acc_id);
  516. // Log already written.
  517. }
  518. /*
  519. * Handler for incoming presence subscription request
  520. */
  521. static void on_incoming_subscribe(pjsua_acc_id acc_id,
  522. pjsua_srv_pres *srv_pres,
  523. pjsua_buddy_id buddy_id,
  524. const pj_str_t *from,
  525. pjsip_rx_data *rdata,
  526. pjsip_status_code *code,
  527. pj_str_t *reason,
  528. pjsua_msg_data *msg_data_)
  529. {
  530. /* Just accept the request (the default behavior) */
  531. PJ_UNUSED_ARG(acc_id);
  532. PJ_UNUSED_ARG(srv_pres);
  533. PJ_UNUSED_ARG(buddy_id);
  534. PJ_UNUSED_ARG(from);
  535. PJ_UNUSED_ARG(rdata);
  536. PJ_UNUSED_ARG(code);
  537. PJ_UNUSED_ARG(reason);
  538. PJ_UNUSED_ARG(msg_data_);
  539. }
  540. /*
  541. * Handler on buddy state changed.
  542. */
  543. static void on_buddy_state(pjsua_buddy_id buddy_id)
  544. {
  545. pjsua_buddy_info info;
  546. pjsua_buddy_get_info(buddy_id, &info);
  547. PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s, subscription state is %s "
  548. "(last termination reason code=%d %.*s)",
  549. (int)info.uri.slen,
  550. info.uri.ptr,
  551. (int)info.status_text.slen,
  552. info.status_text.ptr,
  553. info.sub_state_name,
  554. info.sub_term_code,
  555. (int)info.sub_term_reason.slen,
  556. info.sub_term_reason.ptr));
  557. }
  558. /*
  559. * Handler on buddy dialog event state changed.
  560. */
  561. static void on_buddy_dlg_event_state(pjsua_buddy_id buddy_id)
  562. {
  563. pjsua_buddy_dlg_event_info info;
  564. pjsua_buddy_get_dlg_event_info(buddy_id, &info);
  565. PJ_LOG(3,(THIS_FILE, "%.*s dialog-info-state: %.*s, "
  566. "dialog-info-entity: %.*s, dialog-id: %.*s, "
  567. "dialog-call-id: %.*s, dialog-direction: %.*s, "
  568. "dialog-state: %.*s, dialog-duration: %.*s, "
  569. "local-identity: %.*s, local-target-uri: %.*s, "
  570. "remote-identity: %.*s, remote-target-uri: %.*s, "
  571. "dialog-local-tag: %.*s, dialog-remote-tag: %.*s, "
  572. "subscription state: %s (last termination reason code=%d %.*s)",
  573. (int)info.uri.slen, info.uri.ptr,
  574. (int)info.dialog_info_state.slen, info.dialog_info_state.ptr,
  575. (int)info.dialog_info_entity.slen, info.dialog_info_entity.ptr,
  576. (int)info.dialog_id.slen, info.dialog_id.ptr,
  577. (int)info.dialog_call_id.slen, info.dialog_call_id.ptr,
  578. (int)info.dialog_direction.slen, info.dialog_direction.ptr,
  579. (int)info.dialog_state.slen, info.dialog_state.ptr,
  580. (int)info.dialog_duration.slen, info.dialog_duration.ptr,
  581. (int)info.local_identity.slen, info.local_identity.ptr,
  582. (int)info.local_target_uri.slen, info.local_target_uri.ptr,
  583. (int)info.remote_identity.slen, info.remote_identity.ptr,
  584. (int)info.remote_target_uri.slen, info.remote_target_uri.ptr,
  585. (int)info.dialog_local_tag.slen, info.dialog_local_tag.ptr,
  586. (int)info.dialog_remote_tag.slen, info.dialog_remote_tag.ptr,
  587. info.sub_state_name, info.sub_term_code,
  588. (int)info.sub_term_reason.slen, info.sub_term_reason.ptr));
  589. }
  590. /*
  591. * Subscription state has changed.
  592. */
  593. static void on_buddy_evsub_state(pjsua_buddy_id buddy_id,
  594. pjsip_evsub *sub,
  595. pjsip_event *event)
  596. {
  597. char event_info[80];
  598. PJ_UNUSED_ARG(sub);
  599. event_info[0] = '\0';
  600. if (event->type == PJSIP_EVENT_TSX_STATE &&
  601. event->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
  602. {
  603. pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
  604. snprintf(event_info, sizeof(event_info),
  605. " (RX %s)",
  606. pjsip_rx_data_get_info(rdata));
  607. }
  608. PJ_LOG(4,(THIS_FILE,
  609. "Buddy %d: subscription state: %s (event: %s%s)",
  610. buddy_id, pjsip_evsub_get_state_name(sub),
  611. pjsip_event_str(event->type),
  612. event_info));
  613. }
  614. static void on_buddy_evsub_dlg_event_state(pjsua_buddy_id buddy_id,
  615. pjsip_evsub *sub,
  616. pjsip_event *event)
  617. {
  618. char event_info[80];
  619. PJ_UNUSED_ARG(sub);
  620. event_info[0] = '\0';
  621. if (event->type == PJSIP_EVENT_TSX_STATE &&
  622. event->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
  623. {
  624. pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
  625. snprintf(event_info, sizeof(event_info),
  626. " (RX %s)",
  627. pjsip_rx_data_get_info(rdata));
  628. }
  629. PJ_LOG(4,(THIS_FILE,
  630. "Buddy %d: dialog event subscription state: %s (event: %s%s)",
  631. buddy_id, pjsip_evsub_get_state_name(sub),
  632. pjsip_event_str(event->type), event_info));
  633. }
  634. /**
  635. * Incoming IM message (i.e. MESSAGE request)!
  636. */
  637. static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
  638. const pj_str_t *to, const pj_str_t *contact,
  639. const pj_str_t *mime_type, const pj_str_t *text)
  640. {
  641. /* Note: call index may be -1 */
  642. PJ_UNUSED_ARG(call_id);
  643. PJ_UNUSED_ARG(to);
  644. PJ_UNUSED_ARG(contact);
  645. PJ_UNUSED_ARG(mime_type);
  646. PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
  647. (int)from->slen, from->ptr,
  648. (int)text->slen, text->ptr,
  649. (int)mime_type->slen, mime_type->ptr));
  650. }
  651. /**
  652. * Received typing indication
  653. */
  654. static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
  655. const pj_str_t *to, const pj_str_t *contact,
  656. pj_bool_t is_typing)
  657. {
  658. PJ_UNUSED_ARG(call_id);
  659. PJ_UNUSED_ARG(to);
  660. PJ_UNUSED_ARG(contact);
  661. PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
  662. (int)from->slen, from->ptr,
  663. (is_typing?"is typing..":"has stopped typing")));
  664. }
  665. /**
  666. * Call transfer request status.
  667. */
  668. static void on_call_transfer_status(pjsua_call_id call_id,
  669. int status_code,
  670. const pj_str_t *status_text,
  671. pj_bool_t final,
  672. pj_bool_t *p_cont)
  673. {
  674. PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
  675. call_id, status_code,
  676. (int)status_text->slen, status_text->ptr,
  677. (final ? "[final]" : "")));
  678. if (status_code/100 == 2) {
  679. PJ_LOG(3,(THIS_FILE,
  680. "Call %d: call transferred successfully, disconnecting call",
  681. call_id));
  682. pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
  683. *p_cont = PJ_FALSE;
  684. }
  685. }
  686. /*
  687. * Notification that call is being replaced.
  688. */
  689. static void on_call_replaced(pjsua_call_id old_call_id,
  690. pjsua_call_id new_call_id)
  691. {
  692. pjsua_call_info old_ci, new_ci;
  693. pjsua_call_get_info(old_call_id, &old_ci);
  694. pjsua_call_get_info(new_call_id, &new_ci);
  695. PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
  696. "call %d with %.*s",
  697. old_call_id,
  698. (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
  699. new_call_id,
  700. (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
  701. }
  702. /*
  703. * NAT type detection callback.
  704. */
  705. static void on_nat_detect(const pj_stun_nat_detect_result *res)
  706. {
  707. if (res->status != PJ_SUCCESS) {
  708. pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
  709. } else {
  710. PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
  711. }
  712. }
  713. /*
  714. * MWI indication
  715. */
  716. static void on_mwi_info(pjsua_acc_id acc_id, pjsua_mwi_info *mwi_info)
  717. {
  718. pj_str_t body;
  719. PJ_LOG(3,(THIS_FILE, "Received MWI for acc %d:", acc_id));
  720. if (mwi_info->rdata->msg_info.ctype) {
  721. const pjsip_ctype_hdr *ctype = mwi_info->rdata->msg_info.ctype;
  722. PJ_LOG(3,(THIS_FILE, " Content-Type: %.*s/%.*s",
  723. (int)ctype->media.type.slen,
  724. ctype->media.type.ptr,
  725. (int)ctype->media.subtype.slen,
  726. ctype->media.subtype.ptr));
  727. }
  728. if (!mwi_info->rdata->msg_info.msg->body) {
  729. PJ_LOG(3,(THIS_FILE, " no message body"));
  730. return;
  731. }
  732. body.ptr = (char *)mwi_info->rdata->msg_info.msg->body->data;
  733. body.slen = mwi_info->rdata->msg_info.msg->body->len;
  734. PJ_LOG(3,(THIS_FILE, " Body:\n%.*s", (int)body.slen, body.ptr));
  735. }
  736. /*
  737. * Transport status notification
  738. */
  739. static void on_transport_state(pjsip_transport *tp,
  740. pjsip_transport_state state,
  741. const pjsip_transport_state_info *info)
  742. {
  743. char host_port[128];
  744. pj_addr_str_print(&tp->remote_name.host,
  745. tp->remote_name.port, host_port, sizeof(host_port), 1);
  746. switch (state) {
  747. case PJSIP_TP_STATE_CONNECTED:
  748. {
  749. PJ_LOG(3,(THIS_FILE, "SIP %s transport is connected to %s",
  750. tp->type_name, host_port));
  751. }
  752. break;
  753. case PJSIP_TP_STATE_DISCONNECTED:
  754. {
  755. char buf[100];
  756. int len;
  757. len = pj_ansi_snprintf(buf, sizeof(buf), "SIP %s transport is "
  758. "disconnected from %s", tp->type_name, host_port);
  759. PJ_CHECK_TRUNC_STR(len, buf, sizeof(buf));
  760. pjsua_perror(THIS_FILE, buf, info->status);
  761. }
  762. break;
  763. default:
  764. break;
  765. }
  766. #if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
  767. if (!pj_ansi_stricmp(tp->type_name, "tls") && info->ext_info &&
  768. (state == PJSIP_TP_STATE_CONNECTED ||
  769. ((pjsip_tls_state_info*)info->ext_info)->
  770. ssl_sock_info->verify_status != PJ_SUCCESS))
  771. {
  772. pjsip_tls_state_info *tls_info = (pjsip_tls_state_info*)info->ext_info;
  773. pj_ssl_sock_info *ssl_sock_info = tls_info->ssl_sock_info;
  774. char buf[2048];
  775. const char *verif_msgs[32];
  776. unsigned verif_msg_cnt;
  777. /* Dump server TLS cipher */
  778. PJ_LOG(4,(THIS_FILE, "TLS cipher used: 0x%06X/%s",
  779. ssl_sock_info->cipher,
  780. pj_ssl_cipher_name(ssl_sock_info->cipher) ));
  781. /* Dump server TLS certificate */
  782. pj_ssl_cert_info_dump(ssl_sock_info->remote_cert_info, " ",
  783. buf, sizeof(buf));
  784. PJ_LOG(4,(THIS_FILE, "TLS cert info of %s:\n%s", host_port, buf));
  785. /* Dump server TLS certificate verification result */
  786. verif_msg_cnt = PJ_ARRAY_SIZE(verif_msgs);
  787. pj_ssl_cert_get_verify_status_strings(ssl_sock_info->verify_status,
  788. verif_msgs, &verif_msg_cnt);
  789. PJ_LOG(3,(THIS_FILE, "TLS cert verification result of %s : %s",
  790. host_port,
  791. (verif_msg_cnt == 1? verif_msgs[0]:"")));
  792. if (verif_msg_cnt > 1) {
  793. unsigned i;
  794. for (i = 0; i < verif_msg_cnt; ++i)
  795. PJ_LOG(3,(THIS_FILE, "- %s", verif_msgs[i]));
  796. }
  797. if (ssl_sock_info->verify_status &&
  798. !app_config.udp_cfg.tls_setting.verify_server)
  799. {
  800. PJ_LOG(3,(THIS_FILE, "PJSUA is configured to ignore TLS cert "
  801. "verification errors"));
  802. }
  803. }
  804. #endif
  805. }
  806. /*
  807. * Notification on ICE error.
  808. */
  809. static void on_ice_transport_error(int index, pj_ice_strans_op op,
  810. pj_status_t status, void *param)
  811. {
  812. PJ_UNUSED_ARG(op);
  813. PJ_UNUSED_ARG(param);
  814. PJ_PERROR(1,(THIS_FILE, status,
  815. "ICE keep alive failure for transport %d", index));
  816. }
  817. /*
  818. * Notification on sound device operation.
  819. */
  820. static pj_status_t on_snd_dev_operation(int operation)
  821. {
  822. int cap_dev, play_dev;
  823. pjsua_get_snd_dev(&cap_dev, &play_dev);
  824. PJ_LOG(3,(THIS_FILE, "Turning sound device %d %d %s", cap_dev, play_dev,
  825. (operation? "ON":"OFF")));
  826. return PJ_SUCCESS;
  827. }
  828. static char *get_media_dir(pjmedia_dir dir) {
  829. switch (dir) {
  830. case PJMEDIA_DIR_ENCODING:
  831. return "TX";
  832. case PJMEDIA_DIR_DECODING:
  833. return "RX";
  834. case PJMEDIA_DIR_ENCODING+PJMEDIA_DIR_DECODING:
  835. return "TX+RX";
  836. default:
  837. return "unknown dir";
  838. }
  839. }
  840. /* Callback on media events */
  841. static void on_call_media_event(pjsua_call_id call_id,
  842. unsigned med_idx,
  843. pjmedia_event *event)
  844. {
  845. char event_name[5];
  846. PJ_LOG(5,(THIS_FILE, "Event %s",
  847. pjmedia_fourcc_name(event->type, event_name)));
  848. if (event->type == PJMEDIA_EVENT_MEDIA_TP_ERR) {
  849. pjmedia_event_media_tp_err_data *err_data;
  850. err_data = &event->data.med_tp_err;
  851. PJ_PERROR(3, (THIS_FILE, err_data->status,
  852. "Media transport error event (%s %s %s)",
  853. (err_data->type==PJMEDIA_TYPE_AUDIO)?"Audio":"Video",
  854. (err_data->is_rtp)?"RTP":"RTCP",
  855. get_media_dir(err_data->dir)));
  856. }
  857. #if PJSUA_HAS_VIDEO
  858. else if (event->type == PJMEDIA_EVENT_FMT_CHANGED) {
  859. /* Adjust renderer window size to original video size */
  860. pjsua_call_info ci;
  861. pjsua_call_get_info(call_id, &ci);
  862. if ((ci.media[med_idx].type == PJMEDIA_TYPE_VIDEO) &&
  863. (ci.media[med_idx].dir & PJMEDIA_DIR_DECODING))
  864. {
  865. pjsua_vid_win_id wid;
  866. pjmedia_rect_size size;
  867. pjsua_vid_win_info win_info;
  868. wid = ci.media[med_idx].stream.vid.win_in;
  869. pjsua_vid_win_get_info(wid, &win_info);
  870. size = event->data.fmt_changed.new_fmt.det.vid.size;
  871. if (size.w != win_info.size.w || size.h != win_info.size.h) {
  872. pjsua_vid_win_set_size(wid, &size);
  873. /* Re-arrange video windows */
  874. arrange_window(PJSUA_INVALID_ID);
  875. }
  876. }
  877. }
  878. #else
  879. PJ_UNUSED_ARG(call_id);
  880. PJ_UNUSED_ARG(med_idx);
  881. #endif
  882. }
  883. #ifdef TRANSPORT_ADAPTER_SAMPLE
  884. /*
  885. * This callback is called when media transport needs to be created.
  886. */
  887. static pjmedia_transport* on_create_media_transport(pjsua_call_id call_id,
  888. unsigned media_idx,
  889. pjmedia_transport *base_tp,
  890. unsigned flags)
  891. {
  892. pjmedia_transport *adapter;
  893. pj_status_t status;
  894. /* Create the adapter */
  895. status = pjmedia_tp_adapter_create(pjsua_get_pjmedia_endpt(),
  896. NULL, base_tp,
  897. (flags & PJSUA_MED_TP_CLOSE_MEMBER),
  898. &adapter);
  899. if (status != PJ_SUCCESS) {
  900. PJ_PERROR(1,(THIS_FILE, status, "Error creating adapter"));
  901. return NULL;
  902. }
  903. PJ_LOG(3,(THIS_FILE, "Media transport is created for call %d media %d",
  904. call_id, media_idx));
  905. return adapter;
  906. }
  907. #endif
  908. /* Playfile done notification, set timer to hangup calls */
  909. void on_playfile_done(pjmedia_port *port, void *usr_data)
  910. {
  911. pj_time_val delay;
  912. PJ_UNUSED_ARG(port);
  913. PJ_UNUSED_ARG(usr_data);
  914. /* Just rewind WAV when it is played outside of call */
  915. if (pjsua_call_get_count() == 0) {
  916. pjsua_player_set_pos(app_config.wav_id, 0);
  917. }
  918. /* Timer is already active */
  919. if (app_config.auto_hangup_timer.id == 1)
  920. return;
  921. app_config.auto_hangup_timer.id = 1;
  922. delay.sec = 0;
  923. delay.msec = 200; /* Give 200 ms before hangup */
  924. pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
  925. &app_config.auto_hangup_timer,
  926. &delay);
  927. }
  928. /* IP change progress callback. */
  929. void on_ip_change_progress(pjsua_ip_change_op op,
  930. pj_status_t status,
  931. const pjsua_ip_change_op_info *info)
  932. {
  933. char info_str[128];
  934. pjsua_acc_info acc_info;
  935. pjsua_transport_info tp_info;
  936. if (status == PJ_SUCCESS) {
  937. switch (op) {
  938. case PJSUA_IP_CHANGE_OP_SHUTDOWN_TP:
  939. pj_ansi_snprintf(info_str, sizeof(info_str),
  940. "TCP/TLS transports shutdown");
  941. break;
  942. case PJSUA_IP_CHANGE_OP_RESTART_LIS:
  943. pjsua_transport_get_info(info->lis_restart.transport_id, &tp_info);
  944. pj_ansi_snprintf(info_str, sizeof(info_str),
  945. "restart transport %.*s",
  946. (int)tp_info.info.slen, tp_info.info.ptr);
  947. break;
  948. case PJSUA_IP_CHANGE_OP_ACC_SHUTDOWN_TP:
  949. pjsua_acc_get_info(info->acc_shutdown_tp.acc_id, &acc_info);
  950. pj_ansi_snprintf(info_str, sizeof(info_str),
  951. "transport shutdown for account %.*s",
  952. (int)acc_info.acc_uri.slen,
  953. acc_info.acc_uri.ptr);
  954. break;
  955. case PJSUA_IP_CHANGE_OP_ACC_UPDATE_CONTACT:
  956. pjsua_acc_get_info(info->acc_shutdown_tp.acc_id, &acc_info);
  957. if (info->acc_update_contact.code) {
  958. pj_ansi_snprintf(info_str, sizeof(info_str),
  959. "update contact for account %.*s, code[%d]",
  960. (int)acc_info.acc_uri.slen,
  961. acc_info.acc_uri.ptr,
  962. info->acc_update_contact.code);
  963. } else {
  964. pj_ansi_snprintf(info_str, sizeof(info_str),
  965. "update contact for account %.*s",
  966. (int)acc_info.acc_uri.slen,
  967. acc_info.acc_uri.ptr);
  968. }
  969. break;
  970. case PJSUA_IP_CHANGE_OP_ACC_HANGUP_CALLS:
  971. pjsua_acc_get_info(info->acc_shutdown_tp.acc_id, &acc_info);
  972. pj_ansi_snprintf(info_str, sizeof(info_str),
  973. "hangup call for account %.*s, call_id[%d]",
  974. (int)acc_info.acc_uri.slen, acc_info.acc_uri.ptr,
  975. info->acc_hangup_calls.call_id);
  976. break;
  977. case PJSUA_IP_CHANGE_OP_ACC_REINVITE_CALLS:
  978. pjsua_acc_get_info(info->acc_shutdown_tp.acc_id, &acc_info);
  979. pj_ansi_snprintf(info_str, sizeof(info_str),
  980. "reinvite call for account %.*s, call_id[%d]",
  981. (int)acc_info.acc_uri.slen, acc_info.acc_uri.ptr,
  982. info->acc_reinvite_calls.call_id);
  983. break;
  984. case PJSUA_IP_CHANGE_OP_COMPLETED:
  985. pj_ansi_snprintf(info_str, sizeof(info_str),
  986. "done");
  987. break;
  988. default:
  989. pj_ansi_snprintf(info_str, sizeof(info_str),
  990. "unknown-op");
  991. break;
  992. }
  993. PJ_LOG(3,(THIS_FILE, "IP change progress report : %s", info_str));
  994. } else {
  995. PJ_PERROR(3,(THIS_FILE, status, "IP change progress fail"));
  996. }
  997. }
  998. /* Auto hangup timer callback */
  999. static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
  1000. struct pj_timer_entry *entry)
  1001. {
  1002. PJ_UNUSED_ARG(timer_heap);
  1003. PJ_UNUSED_ARG(entry);
  1004. app_config.auto_hangup_timer.id = 0;
  1005. pjsua_call_hangup_all();
  1006. }
  1007. /*
  1008. * A simple registrar, invoked by default_mod_on_rx_request()
  1009. */
  1010. static void simple_registrar(pjsip_rx_data *rdata)
  1011. {
  1012. pjsip_tx_data *tdata;
  1013. const pjsip_expires_hdr *exp;
  1014. const pjsip_hdr *h;
  1015. pjsip_generic_string_hdr *srv;
  1016. pj_status_t status;
  1017. status = pjsip_endpt_create_response(pjsua_get_pjsip_endpt(),
  1018. rdata, 200, NULL, &tdata);
  1019. if (status != PJ_SUCCESS)
  1020. return;
  1021. exp = (pjsip_expires_hdr *)pjsip_msg_find_hdr(rdata->msg_info.msg,
  1022. PJSIP_H_EXPIRES, NULL);
  1023. h = rdata->msg_info.msg->hdr.next;
  1024. while (h != &rdata->msg_info.msg->hdr) {
  1025. if (h->type == PJSIP_H_CONTACT) {
  1026. const pjsip_contact_hdr *c = (const pjsip_contact_hdr*)h;
  1027. unsigned e = c->expires;
  1028. if (e != PJSIP_EXPIRES_NOT_SPECIFIED) {
  1029. if (exp)
  1030. e = exp->ivalue;
  1031. else
  1032. e = 3600;
  1033. }
  1034. if (e > 0) {
  1035. pjsip_contact_hdr *nc = (pjsip_contact_hdr *)pjsip_hdr_clone(
  1036. tdata->pool, h);
  1037. nc->expires = e;
  1038. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)nc);
  1039. }
  1040. }
  1041. h = h->next;
  1042. }
  1043. srv = pjsip_generic_string_hdr_create(tdata->pool, NULL, NULL);
  1044. srv->name = pj_str("Server");
  1045. srv->hvalue = pj_str("pjsua simple registrar");
  1046. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)srv);
  1047. status = pjsip_endpt_send_response2(pjsua_get_pjsip_endpt(),
  1048. rdata, tdata, NULL, NULL);
  1049. if (status != PJ_SUCCESS) {
  1050. pjsip_tx_data_dec_ref(tdata);
  1051. }
  1052. }
  1053. /*****************************************************************************
  1054. * A simple module to handle otherwise unhandled request. We will register
  1055. * this with the lowest priority.
  1056. */
  1057. /* Notification on incoming request */
  1058. static pj_bool_t default_mod_on_rx_request(pjsip_rx_data *rdata)
  1059. {
  1060. pjsip_tx_data *tdata;
  1061. pjsip_status_code status_code;
  1062. pj_status_t status;
  1063. /* Don't respond to ACK! */
  1064. if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
  1065. &pjsip_ack_method) == 0)
  1066. return PJ_TRUE;
  1067. /* Simple registrar */
  1068. if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
  1069. &pjsip_register_method) == 0)
  1070. {
  1071. simple_registrar(rdata);
  1072. return PJ_TRUE;
  1073. }
  1074. /* Create basic response. */
  1075. if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
  1076. &pjsip_notify_method) == 0)
  1077. {
  1078. /* Unsolicited NOTIFY's, send with Bad Request */
  1079. status_code = PJSIP_SC_BAD_REQUEST;
  1080. } else {
  1081. /* Probably unknown method */
  1082. status_code = PJSIP_SC_METHOD_NOT_ALLOWED;
  1083. }
  1084. status = pjsip_endpt_create_response(pjsua_get_pjsip_endpt(),
  1085. rdata, status_code,
  1086. NULL, &tdata);
  1087. if (status != PJ_SUCCESS) {
  1088. pjsua_perror(THIS_FILE, "Unable to create response", status);
  1089. return PJ_TRUE;
  1090. }
  1091. /* Add Allow if we're responding with 405 */
  1092. if (status_code == PJSIP_SC_METHOD_NOT_ALLOWED) {
  1093. const pjsip_hdr *cap_hdr;
  1094. cap_hdr = pjsip_endpt_get_capability(pjsua_get_pjsip_endpt(),
  1095. PJSIP_H_ALLOW, NULL);
  1096. if (cap_hdr) {
  1097. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)pjsip_hdr_clone(
  1098. tdata->pool, cap_hdr));
  1099. }
  1100. }
  1101. /* Add User-Agent header */
  1102. {
  1103. pj_str_t user_agent;
  1104. char tmp[80];
  1105. const pj_str_t USER_AGENT = { "User-Agent", 10};
  1106. pjsip_hdr *h;
  1107. pj_ansi_snprintf(tmp, sizeof(tmp), "PJSUA v%s/%s",
  1108. pj_get_version(), PJ_OS_NAME);
  1109. pj_strdup2_with_null(tdata->pool, &user_agent, tmp);
  1110. h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
  1111. &USER_AGENT,
  1112. &user_agent);
  1113. pjsip_msg_add_hdr(tdata->msg, h);
  1114. }
  1115. status = pjsip_endpt_send_response2(pjsua_get_pjsip_endpt(), rdata, tdata,
  1116. NULL, NULL);
  1117. if (status != PJ_SUCCESS) pjsip_tx_data_dec_ref(tdata);
  1118. return PJ_TRUE;
  1119. }
  1120. /* The module instance. */
  1121. static pjsip_module mod_default_handler =
  1122. {
  1123. NULL, NULL, /* prev, next. */
  1124. { "mod-default-handler", 19 }, /* Name. */
  1125. -1, /* Id */
  1126. PJSIP_MOD_PRIORITY_APPLICATION+99, /* Priority */
  1127. NULL, /* load() */
  1128. NULL, /* start() */
  1129. NULL, /* stop() */
  1130. NULL, /* unload() */
  1131. &default_mod_on_rx_request, /* on_rx_request() */
  1132. NULL, /* on_rx_response() */
  1133. NULL, /* on_tx_request. */
  1134. NULL, /* on_tx_response() */
  1135. NULL, /* on_tsx_state() */
  1136. };
  1137. /** CLI callback **/
  1138. /* Called on CLI (re)started, e.g: initial start, after iOS bg */
  1139. void cli_on_started(pj_status_t status)
  1140. {
  1141. /* Notify app */
  1142. if (app_cfg.on_started) {
  1143. if (status == PJ_SUCCESS) {
  1144. char info[128];
  1145. cli_get_info(info, sizeof(info));
  1146. if (app_cfg.on_started) {
  1147. (*app_cfg.on_started)(status, info);
  1148. }
  1149. } else {
  1150. if (app_cfg.on_started) {
  1151. (*app_cfg.on_started)(status, NULL);
  1152. }
  1153. }
  1154. }
  1155. }
  1156. /* Called on CLI quit */
  1157. void cli_on_stopped(pj_bool_t restart, int argc, char* argv[])
  1158. {
  1159. /* Notify app */
  1160. if (app_cfg.on_stopped)
  1161. (*app_cfg.on_stopped)(restart, argc, argv);
  1162. }
  1163. /* Called on pjsua legacy quit */
  1164. void legacy_on_stopped(pj_bool_t restart)
  1165. {
  1166. /* Notify app */
  1167. if (app_cfg.on_stopped)
  1168. (*app_cfg.on_stopped)(restart, 1, NULL);
  1169. }
  1170. /*****************************************************************************
  1171. * Public API
  1172. */
  1173. int stdout_refresh_proc(void *arg)
  1174. {
  1175. extern char *stdout_refresh_text;
  1176. PJ_UNUSED_ARG(arg);
  1177. /* Set thread to lowest priority so that it doesn't clobber
  1178. * stdout output
  1179. */
  1180. pj_thread_set_prio(pj_thread_this(),
  1181. pj_thread_get_prio_min(pj_thread_this()));
  1182. while (!stdout_refresh_quit) {
  1183. pj_thread_sleep(stdout_refresh * 1000);
  1184. puts(stdout_refresh_text);
  1185. fflush(stdout);
  1186. }
  1187. return 0;
  1188. }
  1189. static pj_status_t app_init(void)
  1190. {
  1191. pjsua_transport_id transport_id = -1;
  1192. pjsua_transport_config tcp_cfg;
  1193. unsigned i;
  1194. pj_pool_t *tmp_pool;
  1195. pj_status_t status;
  1196. /** Create pjsua **/
  1197. status = pjsua_create();
  1198. if (status != PJ_SUCCESS)
  1199. return status;
  1200. /* Create pool for application */
  1201. app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000);
  1202. tmp_pool = pjsua_pool_create("tmp-pjsua", 1000, 1000);;
  1203. /* Init CLI & its FE settings */
  1204. if (!app_running) {
  1205. pj_cli_cfg_default(&app_config.cli_cfg.cfg);
  1206. pj_cli_telnet_cfg_default(&app_config.cli_cfg.telnet_cfg);
  1207. pj_cli_console_cfg_default(&app_config.cli_cfg.console_cfg);
  1208. app_config.cli_cfg.telnet_cfg.on_started = cli_on_started;
  1209. }
  1210. /** Parse args **/
  1211. status = load_config(app_cfg.argc, app_cfg.argv, &uri_arg);
  1212. if (status != PJ_SUCCESS) {
  1213. pj_pool_release(tmp_pool);
  1214. return status;
  1215. }
  1216. /* Initialize application callbacks */
  1217. app_config.cfg.cb.on_call_state = &on_call_state;
  1218. app_config.cfg.cb.on_stream_destroyed = &on_stream_destroyed;
  1219. app_config.cfg.cb.on_call_media_state = &on_call_media_state;
  1220. app_config.cfg.cb.on_incoming_call = &on_incoming_call;
  1221. app_config.cfg.cb.on_dtmf_digit2 = &call_on_dtmf_callback2;
  1222. app_config.cfg.cb.on_call_redirected = &call_on_redirected;
  1223. app_config.cfg.cb.on_reg_state = &on_reg_state;
  1224. app_config.cfg.cb.on_incoming_subscribe = &on_incoming_subscribe;
  1225. app_config.cfg.cb.on_buddy_state = &on_buddy_state;
  1226. app_config.cfg.cb.on_buddy_dlg_event_state = &on_buddy_dlg_event_state;
  1227. app_config.cfg.cb.on_buddy_evsub_state = &on_buddy_evsub_state;
  1228. app_config.cfg.cb.on_buddy_evsub_dlg_event_state =
  1229. &on_buddy_evsub_dlg_event_state;
  1230. app_config.cfg.cb.on_pager = &on_pager;
  1231. app_config.cfg.cb.on_typing = &on_typing;
  1232. app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
  1233. app_config.cfg.cb.on_call_replaced = &on_call_replaced;
  1234. app_config.cfg.cb.on_nat_detect = &on_nat_detect;
  1235. app_config.cfg.cb.on_mwi_info = &on_mwi_info;
  1236. app_config.cfg.cb.on_transport_state = &on_transport_state;
  1237. app_config.cfg.cb.on_ice_transport_error = &on_ice_transport_error;
  1238. app_config.cfg.cb.on_snd_dev_operation = &on_snd_dev_operation;
  1239. app_config.cfg.cb.on_call_media_event = &on_call_media_event;
  1240. app_config.cfg.cb.on_ip_change_progress = &on_ip_change_progress;
  1241. #ifdef TRANSPORT_ADAPTER_SAMPLE
  1242. app_config.cfg.cb.on_create_media_transport = &on_create_media_transport;
  1243. #endif
  1244. /* Set sound device latency */
  1245. if (app_config.capture_lat > 0)
  1246. app_config.media_cfg.snd_rec_latency = app_config.capture_lat;
  1247. if (app_config.playback_lat)
  1248. app_config.media_cfg.snd_play_latency = app_config.playback_lat;
  1249. if (app_cfg.on_config_init)
  1250. (*app_cfg.on_config_init)(&app_config);
  1251. /* Initialize pjsua */
  1252. status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
  1253. &app_config.media_cfg);
  1254. if (status != PJ_SUCCESS) {
  1255. pj_pool_release(tmp_pool);
  1256. return status;
  1257. }
  1258. /* Initialize our module to handle otherwise unhandled request */
  1259. status = pjsip_endpt_register_module(pjsua_get_pjsip_endpt(),
  1260. &mod_default_handler);
  1261. if (status != PJ_SUCCESS)
  1262. return status;
  1263. #ifdef STEREO_DEMO
  1264. stereo_demo();
  1265. #endif
  1266. /* Initialize calls data */
  1267. for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
  1268. app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
  1269. app_config.call_data[i].timer.cb = &call_timeout_callback;
  1270. }
  1271. /* Optionally registers WAV file */
  1272. for (i=0; i<app_config.wav_count; ++i) {
  1273. pjsua_player_id wav_id;
  1274. unsigned play_options = 0;
  1275. if (app_config.auto_play_hangup)
  1276. play_options |= PJMEDIA_FILE_NO_LOOP;
  1277. status = pjsua_player_create(&app_config.wav_files[i], play_options,
  1278. &wav_id);
  1279. if (status != PJ_SUCCESS)
  1280. goto on_error;
  1281. if (app_config.wav_id == PJSUA_INVALID_ID) {
  1282. app_config.wav_id = wav_id;
  1283. app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
  1284. if (app_config.auto_play_hangup) {
  1285. pjmedia_port *port;
  1286. pjsua_player_get_port(app_config.wav_id, &port);
  1287. status = pjmedia_wav_player_set_eof_cb2(port, NULL,
  1288. &on_playfile_done);
  1289. if (status != PJ_SUCCESS)
  1290. goto on_error;
  1291. pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
  1292. &hangup_timeout_callback);
  1293. }
  1294. }
  1295. }
  1296. /* Optionally registers tone players */
  1297. for (i=0; i<app_config.tone_count; ++i) {
  1298. pjmedia_port *tport;
  1299. char name[80];
  1300. pj_str_t label;
  1301. pj_status_t status2;
  1302. pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
  1303. app_config.tones[i].freq1,
  1304. app_config.tones[i].freq2);
  1305. label = pj_str(name);
  1306. status2 = pjmedia_tonegen_create2(app_config.pool, &label,
  1307. 8000, 1, 160, 16,
  1308. PJMEDIA_TONEGEN_LOOP, &tport);
  1309. if (status2 != PJ_SUCCESS) {
  1310. pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
  1311. goto on_error;
  1312. }
  1313. status2 = pjsua_conf_add_port(app_config.pool, tport,
  1314. &app_config.tone_slots[i]);
  1315. pj_assert(status2 == PJ_SUCCESS);
  1316. status2 = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
  1317. pj_assert(status2 == PJ_SUCCESS);
  1318. }
  1319. /* Optionally create recorder file, if any. */
  1320. if (app_config.rec_file.slen) {
  1321. status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
  1322. &app_config.rec_id);
  1323. if (status != PJ_SUCCESS)
  1324. goto on_error;
  1325. app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
  1326. }
  1327. pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
  1328. /* Create ringback tones */
  1329. if (app_config.no_tones == PJ_FALSE) {
  1330. unsigned samples_per_frame;
  1331. pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
  1332. pj_str_t name;
  1333. samples_per_frame = app_config.media_cfg.audio_frame_ptime *
  1334. app_config.media_cfg.clock_rate *
  1335. app_config.media_cfg.channel_count / 1000;
  1336. /* Ringback tone (call is ringing) */
  1337. name = pj_str("ringback");
  1338. status = pjmedia_tonegen_create2(app_config.pool, &name,
  1339. app_config.media_cfg.clock_rate,
  1340. app_config.media_cfg.channel_count,
  1341. samples_per_frame,
  1342. 16, PJMEDIA_TONEGEN_LOOP,
  1343. &app_config.ringback_port);
  1344. if (status != PJ_SUCCESS)
  1345. goto on_error;
  1346. pj_bzero(&tone, sizeof(tone));
  1347. for (i=0; i<RINGBACK_CNT; ++i) {
  1348. tone[i].freq1 = RINGBACK_FREQ1;
  1349. tone[i].freq2 = RINGBACK_FREQ2;
  1350. tone[i].on_msec = RINGBACK_ON;
  1351. tone[i].off_msec = RINGBACK_OFF;
  1352. }
  1353. tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
  1354. pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
  1355. PJMEDIA_TONEGEN_LOOP);
  1356. status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
  1357. &app_config.ringback_slot);
  1358. if (status != PJ_SUCCESS)
  1359. goto on_error;
  1360. /* Ring (to alert incoming call) */
  1361. name = pj_str("ring");
  1362. status = pjmedia_tonegen_create2(app_config.pool, &name,
  1363. app_config.media_cfg.clock_rate,
  1364. app_config.media_cfg.channel_count,
  1365. samples_per_frame,
  1366. 16, PJMEDIA_TONEGEN_LOOP,
  1367. &app_config.ring_port);
  1368. if (status != PJ_SUCCESS)
  1369. goto on_error;
  1370. for (i=0; i<RING_CNT; ++i) {
  1371. tone[i].freq1 = RING_FREQ1;
  1372. tone[i].freq2 = RING_FREQ2;
  1373. tone[i].on_msec = RING_ON;
  1374. tone[i].off_msec = RING_OFF;
  1375. }
  1376. tone[RING_CNT-1].off_msec = RING_INTERVAL;
  1377. pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
  1378. tone, PJMEDIA_TONEGEN_LOOP);
  1379. status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
  1380. &app_config.ring_slot);
  1381. if (status != PJ_SUCCESS)
  1382. goto on_error;
  1383. }
  1384. /* Create AVI player virtual devices */
  1385. if (app_config.avi_cnt) {
  1386. #if PJMEDIA_HAS_VIDEO && PJMEDIA_VIDEO_DEV_HAS_AVI
  1387. pjmedia_vid_dev_factory *avi_factory;
  1388. status = pjmedia_avi_dev_create_factory(pjsua_get_pool_factory(),
  1389. app_config.avi_cnt,
  1390. &avi_factory);
  1391. if (status != PJ_SUCCESS) {
  1392. PJ_PERROR(1,(THIS_FILE, status, "Error creating AVI factory"));
  1393. goto on_error;
  1394. }
  1395. for (i=0; i<app_config.avi_cnt; ++i) {
  1396. pjmedia_avi_dev_param avdp;
  1397. pjmedia_vid_dev_index avid;
  1398. unsigned strm_idx, strm_cnt;
  1399. app_config.avi[i].dev_id = PJMEDIA_VID_INVALID_DEV;
  1400. app_config.avi[i].slot = PJSUA_INVALID_ID;
  1401. pjmedia_avi_dev_param_default(&avdp);
  1402. avdp.path = app_config.avi[i].path;
  1403. status = pjmedia_avi_dev_alloc(avi_factory, &avdp, &avid);
  1404. if (status != PJ_SUCCESS) {
  1405. PJ_PERROR(1,(THIS_FILE, status,
  1406. "Error creating AVI player for %.*s",
  1407. (int)avdp.path.slen, avdp.path.ptr));
  1408. goto on_error;
  1409. }
  1410. PJ_LOG(4,(THIS_FILE, "AVI player %.*s created, dev_id=%d",
  1411. (int)avdp.title.slen, avdp.title.ptr, avid));
  1412. app_config.avi[i].dev_id = avid;
  1413. if (app_config.avi_def_idx == PJSUA_INVALID_ID)
  1414. app_config.avi_def_idx = i;
  1415. strm_cnt = pjmedia_avi_streams_get_num_streams(avdp.avi_streams);
  1416. for (strm_idx=0; strm_idx<strm_cnt; ++strm_idx) {
  1417. pjmedia_port *aud;
  1418. pjmedia_format *fmt;
  1419. pjsua_conf_port_id slot;
  1420. char fmt_name[5];
  1421. aud = pjmedia_avi_streams_get_stream(avdp.avi_streams,
  1422. strm_idx);
  1423. fmt = &aud->info.fmt;
  1424. pjmedia_fourcc_name(fmt->id, fmt_name);
  1425. if (fmt->id == PJMEDIA_FORMAT_PCM) {
  1426. status = pjsua_conf_add_port(app_config.pool, aud,
  1427. &slot);
  1428. if (status == PJ_SUCCESS) {
  1429. PJ_LOG(4,(THIS_FILE,
  1430. "AVI %.*s: audio added to slot %d",
  1431. (int)avdp.title.slen, avdp.title.ptr,
  1432. slot));
  1433. app_config.avi[i].slot = slot;
  1434. }
  1435. } else {
  1436. PJ_LOG(4,(THIS_FILE,
  1437. "AVI %.*s: audio ignored, format=%s",
  1438. (int)avdp.title.slen, avdp.title.ptr,
  1439. fmt_name));
  1440. }
  1441. }
  1442. }
  1443. #else
  1444. for (i=0; i<app_config.avi_cnt; ++i) {
  1445. app_config.avi[i].dev_id = PJMEDIA_VID_INVALID_DEV;
  1446. app_config.avi[i].slot = PJSUA_INVALID_ID;
  1447. }
  1448. PJ_LOG(2,(THIS_FILE,
  1449. "Warning: --play-avi is ignored because AVI is disabled"));
  1450. #endif /* PJMEDIA_VIDEO_DEV_HAS_AVI */
  1451. }
  1452. /* Add UDP transport unless it's disabled. */
  1453. if (!app_config.no_udp) {
  1454. pjsua_acc_id aid;
  1455. pjsip_transport_type_e type = PJSIP_TRANSPORT_UDP;
  1456. status = pjsua_transport_create(type,
  1457. &app_config.udp_cfg,
  1458. &transport_id);
  1459. if (status != PJ_SUCCESS)
  1460. goto on_error;
  1461. /* Add local account */
  1462. pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
  1463. /* Adjust local account config based on pjsua app config */
  1464. {
  1465. pjsua_acc_config acc_cfg;
  1466. pjsua_acc_get_config(aid, tmp_pool, &acc_cfg);
  1467. app_config_init_video(&acc_cfg);
  1468. acc_cfg.rtp_cfg = app_config.rtp_cfg;
  1469. pjsua_acc_modify(aid, &acc_cfg);
  1470. }
  1471. //pjsua_acc_set_transport(aid, transport_id);
  1472. pjsua_acc_set_online_status(current_acc, PJ_TRUE);
  1473. if (app_config.udp_cfg.port == 0) {
  1474. pjsua_transport_info ti;
  1475. pj_sockaddr_in *a;
  1476. pjsua_transport_get_info(transport_id, &ti);
  1477. a = (pj_sockaddr_in*)&ti.local_addr;
  1478. tcp_cfg.port = pj_ntohs(a->sin_port);
  1479. }
  1480. }
  1481. /* Add UDP IPv6 transport unless it's disabled. */
  1482. if (!app_config.no_udp && app_config.ipv6) {
  1483. pjsua_acc_id aid;
  1484. pjsip_transport_type_e type = PJSIP_TRANSPORT_UDP6;
  1485. pjsua_transport_config udp_cfg;
  1486. udp_cfg = app_config.udp_cfg;
  1487. if (udp_cfg.port == 0)
  1488. udp_cfg.port = 5060;
  1489. else
  1490. udp_cfg.port += 10;
  1491. status = pjsua_transport_create(type,
  1492. &udp_cfg,
  1493. &transport_id);
  1494. if (status != PJ_SUCCESS)
  1495. goto on_error;
  1496. /* Add local account */
  1497. pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
  1498. /* Adjust local account config based on pjsua app config */
  1499. {
  1500. pjsua_acc_config acc_cfg;
  1501. pjsua_acc_get_config(aid, tmp_pool, &acc_cfg);
  1502. app_config_init_video(&acc_cfg);
  1503. acc_cfg.rtp_cfg = app_config.rtp_cfg;
  1504. // acc_cfg.ipv6_media_use = PJSUA_IPV6_ENABLED;
  1505. pjsua_acc_modify(aid, &acc_cfg);
  1506. }
  1507. //pjsua_acc_set_transport(aid, transport_id);
  1508. pjsua_acc_set_online_status(current_acc, PJ_TRUE);
  1509. if (app_config.udp_cfg.port == 0) {
  1510. pjsua_transport_info ti;
  1511. pjsua_transport_get_info(transport_id, &ti);
  1512. tcp_cfg.port = pj_sockaddr_get_port(&ti.local_addr);
  1513. }
  1514. }
  1515. /* Add TCP transport unless it's disabled */
  1516. if (!app_config.no_tcp) {
  1517. pjsua_acc_id aid;
  1518. status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
  1519. &tcp_cfg,
  1520. &transport_id);
  1521. if (status != PJ_SUCCESS)
  1522. goto on_error;
  1523. /* Add local account */
  1524. pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
  1525. /* Adjust local account config based on pjsua app config */
  1526. {
  1527. pjsua_acc_config acc_cfg;
  1528. pjsua_acc_get_config(aid, tmp_pool, &acc_cfg);
  1529. app_config_init_video(&acc_cfg);
  1530. acc_cfg.rtp_cfg = app_config.rtp_cfg;
  1531. pjsua_acc_modify(aid, &acc_cfg);
  1532. }
  1533. pjsua_acc_set_online_status(current_acc, PJ_TRUE);
  1534. }
  1535. /* Add TCP IPv6 transport unless it's disabled. */
  1536. if (!app_config.no_tcp && app_config.ipv6) {
  1537. pjsua_acc_id aid;
  1538. pjsip_transport_type_e type = PJSIP_TRANSPORT_TCP6;
  1539. tcp_cfg.port += 10;
  1540. status = pjsua_transport_create(type,
  1541. &tcp_cfg,
  1542. &transport_id);
  1543. if (status != PJ_SUCCESS)
  1544. goto on_error;
  1545. /* Add local account */
  1546. pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
  1547. /* Adjust local account config based on pjsua app config */
  1548. {
  1549. pjsua_acc_config acc_cfg;
  1550. pjsua_acc_get_config(aid, tmp_pool, &acc_cfg);
  1551. app_config_init_video(&acc_cfg);
  1552. acc_cfg.rtp_cfg = app_config.rtp_cfg;
  1553. // acc_cfg.ipv6_media_use = PJSUA_IPV6_ENABLED;
  1554. pjsua_acc_modify(aid, &acc_cfg);
  1555. }
  1556. //pjsua_acc_set_transport(aid, transport_id);
  1557. pjsua_acc_set_online_status(current_acc, PJ_TRUE);
  1558. }
  1559. #if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
  1560. /* Add TLS transport when application wants one */
  1561. if (app_config.use_tls) {
  1562. pjsua_acc_id acc_id;
  1563. /* Copy the QoS settings */
  1564. tcp_cfg.tls_setting.qos_type = tcp_cfg.qos_type;
  1565. pj_memcpy(&tcp_cfg.tls_setting.qos_params, &tcp_cfg.qos_params,
  1566. sizeof(tcp_cfg.qos_params));
  1567. /* Set TLS port as TCP port+1 */
  1568. tcp_cfg.port++;
  1569. status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
  1570. &tcp_cfg,
  1571. &transport_id);
  1572. tcp_cfg.port--;
  1573. if (status != PJ_SUCCESS)
  1574. goto on_error;
  1575. /* Add local account */
  1576. pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
  1577. /* Adjust local account config based on pjsua app config */
  1578. {
  1579. pjsua_acc_config acc_cfg;
  1580. pjsua_acc_get_config(acc_id, tmp_pool, &acc_cfg);
  1581. app_config_init_video(&acc_cfg);
  1582. acc_cfg.rtp_cfg = app_config.rtp_cfg;
  1583. pjsua_acc_modify(acc_id, &acc_cfg);
  1584. }
  1585. pjsua_acc_set_online_status(acc_id, PJ_TRUE);
  1586. }
  1587. /* Add TLS IPv6 transport unless it's disabled. */
  1588. if (app_config.use_tls && app_config.ipv6) {
  1589. pjsua_acc_id aid;
  1590. pjsip_transport_type_e type = PJSIP_TRANSPORT_TLS6;
  1591. tcp_cfg.port += 10;
  1592. status = pjsua_transport_create(type,
  1593. &tcp_cfg,
  1594. &transport_id);
  1595. if (status != PJ_SUCCESS)
  1596. goto on_error;
  1597. /* Add local account */
  1598. pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
  1599. /* Adjust local account config based on pjsua app config */
  1600. {
  1601. pjsua_acc_config acc_cfg;
  1602. pjsua_acc_get_config(aid, tmp_pool, &acc_cfg);
  1603. app_config_init_video(&acc_cfg);
  1604. acc_cfg.rtp_cfg = app_config.rtp_cfg;
  1605. // acc_cfg.ipv6_media_use = PJSUA_IPV6_ENABLED;
  1606. pjsua_acc_modify(aid, &acc_cfg);
  1607. }
  1608. //pjsua_acc_set_transport(aid, transport_id);
  1609. pjsua_acc_set_online_status(current_acc, PJ_TRUE);
  1610. }
  1611. #endif
  1612. if (transport_id == -1) {
  1613. PJ_LOG(1,(THIS_FILE, "Error: no transport is configured"));
  1614. status = -1;
  1615. goto on_error;
  1616. }
  1617. /* Add accounts */
  1618. for (i=0; i<app_config.acc_cnt; ++i) {
  1619. app_config.acc_cfg[i].rtp_cfg = app_config.rtp_cfg;
  1620. app_config.acc_cfg[i].reg_retry_interval = 300;
  1621. app_config.acc_cfg[i].reg_first_retry_interval = 60;
  1622. app_config_init_video(&app_config.acc_cfg[i]);
  1623. status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
  1624. if (status != PJ_SUCCESS)
  1625. goto on_error;
  1626. pjsua_acc_set_online_status(current_acc, PJ_TRUE);
  1627. }
  1628. /* Add buddies */
  1629. for (i=0; i<app_config.buddy_cnt; ++i) {
  1630. status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
  1631. if (status != PJ_SUCCESS) {
  1632. PJ_PERROR(1,(THIS_FILE, status, "Error adding buddy"));
  1633. goto on_error;
  1634. }
  1635. }
  1636. /* Optionally disable some codec */
  1637. for (i=0; i<app_config.codec_dis_cnt; ++i) {
  1638. pjsua_codec_set_priority(&app_config.codec_dis[i],
  1639. PJMEDIA_CODEC_PRIO_DISABLED);
  1640. #if PJSUA_HAS_VIDEO
  1641. pjsua_vid_codec_set_priority(&app_config.codec_dis[i],
  1642. PJMEDIA_CODEC_PRIO_DISABLED);
  1643. #endif
  1644. }
  1645. /* Optionally set codec orders */
  1646. for (i=0; i<app_config.codec_cnt; ++i) {
  1647. pjsua_codec_set_priority(&app_config.codec_arg[i],
  1648. (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
  1649. #if PJSUA_HAS_VIDEO
  1650. pjsua_vid_codec_set_priority(&app_config.codec_arg[i],
  1651. (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
  1652. #endif
  1653. }
  1654. /* Use null sound device? */
  1655. #ifndef STEREO_DEMO
  1656. if (app_config.null_audio) {
  1657. status = pjsua_set_null_snd_dev();
  1658. if (status != PJ_SUCCESS)
  1659. goto on_error;
  1660. }
  1661. #endif
  1662. if (app_config.capture_dev != PJSUA_INVALID_ID ||
  1663. app_config.playback_dev != PJSUA_INVALID_ID)
  1664. {
  1665. status = pjsua_set_snd_dev(app_config.capture_dev,
  1666. app_config.playback_dev);
  1667. if (status != PJ_SUCCESS)
  1668. goto on_error;
  1669. }
  1670. /* Init call setting */
  1671. pjsua_call_setting_default(&call_opt);
  1672. call_opt.aud_cnt = app_config.aud_cnt;
  1673. call_opt.vid_cnt = app_config.vid.vid_cnt;
  1674. if (app_config.enable_loam) {
  1675. call_opt.flag |= PJSUA_CALL_NO_SDP_OFFER;
  1676. }
  1677. #if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
  1678. /* Wipe out TLS key settings in transport configs */
  1679. pjsip_tls_setting_wipe_keys(&app_config.udp_cfg.tls_setting);
  1680. #endif
  1681. pj_pool_release(tmp_pool);
  1682. return PJ_SUCCESS;
  1683. on_error:
  1684. #if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
  1685. /* Wipe out TLS key settings in transport configs */
  1686. pjsip_tls_setting_wipe_keys(&app_config.udp_cfg.tls_setting);
  1687. #endif
  1688. pj_pool_release(tmp_pool);
  1689. app_destroy();
  1690. return status;
  1691. }
  1692. pj_status_t pjsua_app_init(const pjsua_app_cfg_t *cfg)
  1693. {
  1694. pj_status_t status;
  1695. pj_memcpy(&app_cfg, cfg, sizeof(app_cfg));
  1696. status = app_init();
  1697. if (status != PJ_SUCCESS)
  1698. return status;
  1699. /* Init CLI if configured */
  1700. if (app_config.use_cli) {
  1701. status = cli_init();
  1702. }
  1703. return status;
  1704. }
  1705. pj_status_t pjsua_app_run(pj_bool_t wait_telnet_cli)
  1706. {
  1707. pj_thread_t *stdout_refresh_thread = NULL;
  1708. pj_status_t status;
  1709. /* Start console refresh thread */
  1710. if (stdout_refresh > 0) {
  1711. status = pj_thread_create(app_config.pool, "stdout",
  1712. &stdout_refresh_proc,
  1713. NULL, 0, 0, &stdout_refresh_thread);
  1714. PJ_ASSERT_RETURN(status==PJ_SUCCESS, status);
  1715. }
  1716. status = pjsua_start();
  1717. if (status != PJ_SUCCESS)
  1718. goto on_return;
  1719. if (app_config.use_cli && (app_config.cli_cfg.cli_fe & CLI_FE_TELNET)) {
  1720. char info[128];
  1721. cli_get_info(info, sizeof(info));
  1722. if (app_cfg.on_started) {
  1723. (*app_cfg.on_started)(status, info);
  1724. }
  1725. } else {
  1726. if (app_cfg.on_started) {
  1727. (*app_cfg.on_started)(status, "Ready");
  1728. }
  1729. }
  1730. /* If user specifies URI to call, then call the URI */
  1731. if (uri_arg.slen) {
  1732. pjsua_call_setting_default(&call_opt);
  1733. call_opt.aud_cnt = app_config.aud_cnt;
  1734. call_opt.vid_cnt = app_config.vid.vid_cnt;
  1735. pjsua_call_make_call(current_acc, &uri_arg, &call_opt, NULL,
  1736. NULL, NULL);
  1737. }
  1738. app_running = PJ_TRUE;
  1739. if (app_config.use_cli)
  1740. cli_main(wait_telnet_cli);
  1741. else
  1742. legacy_main();
  1743. status = PJ_SUCCESS;
  1744. on_return:
  1745. if (stdout_refresh_thread) {
  1746. stdout_refresh_quit = PJ_TRUE;
  1747. pj_thread_join(stdout_refresh_thread);
  1748. pj_thread_destroy(stdout_refresh_thread);
  1749. stdout_refresh_quit = PJ_FALSE;
  1750. }
  1751. return status;
  1752. }
  1753. static pj_status_t app_destroy(void)
  1754. {
  1755. pj_status_t status = PJ_SUCCESS;
  1756. unsigned i;
  1757. pj_bool_t use_cli = PJ_FALSE;
  1758. int cli_fe = 0;
  1759. pj_uint16_t cli_telnet_port = 0;
  1760. #ifdef STEREO_DEMO
  1761. if (app_config.snd) {
  1762. pjmedia_snd_port_destroy(app_config.snd);
  1763. app_config.snd = NULL;
  1764. }
  1765. if (app_config.sc_ch1) {
  1766. pjsua_conf_remove_port(app_config.sc_ch1_slot);
  1767. app_config.sc_ch1_slot = PJSUA_INVALID_ID;
  1768. pjmedia_port_destroy(app_config.sc_ch1);
  1769. app_config.sc_ch1 = NULL;
  1770. }
  1771. if (app_config.sc) {
  1772. pjmedia_port_destroy(app_config.sc);
  1773. app_config.sc = NULL;
  1774. }
  1775. #endif
  1776. /* Close avi devs and ports */
  1777. for (i=0; i<app_config.avi_cnt; ++i) {
  1778. if (app_config.avi[i].slot != PJSUA_INVALID_ID) {
  1779. pjsua_conf_remove_port(app_config.avi[i].slot);
  1780. app_config.avi[i].slot = PJSUA_INVALID_ID;
  1781. }
  1782. #if PJMEDIA_HAS_VIDEO && PJMEDIA_VIDEO_DEV_HAS_AVI
  1783. if (app_config.avi[i].dev_id != PJMEDIA_VID_INVALID_DEV) {
  1784. pjmedia_avi_dev_free(app_config.avi[i].dev_id);
  1785. app_config.avi[i].dev_id = PJMEDIA_VID_INVALID_DEV;
  1786. }
  1787. #endif
  1788. }
  1789. /* Close ringback port */
  1790. if (app_config.ringback_port &&
  1791. app_config.ringback_slot != PJSUA_INVALID_ID)
  1792. {
  1793. pjsua_conf_remove_port(app_config.ringback_slot);
  1794. app_config.ringback_slot = PJSUA_INVALID_ID;
  1795. pjmedia_port_destroy(app_config.ringback_port);
  1796. app_config.ringback_port = NULL;
  1797. }
  1798. /* Close ring port */
  1799. if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
  1800. pjsua_conf_remove_port(app_config.ring_slot);
  1801. app_config.ring_slot = PJSUA_INVALID_ID;
  1802. pjmedia_port_destroy(app_config.ring_port);
  1803. app_config.ring_port = NULL;
  1804. }
  1805. /* Close wav player */
  1806. if (app_config.wav_id != PJSUA_INVALID_ID) {
  1807. pjsua_player_destroy(app_config.wav_id);
  1808. app_config.wav_id = PJSUA_INVALID_ID;
  1809. app_config.wav_port = PJSUA_INVALID_ID;
  1810. }
  1811. /* Close wav recorder */
  1812. if (app_config.rec_id != PJSUA_INVALID_ID) {
  1813. pjsua_recorder_destroy(app_config.rec_id);
  1814. app_config.rec_id = PJSUA_INVALID_ID;
  1815. app_config.rec_port = PJSUA_INVALID_ID;
  1816. }
  1817. /* Close tone generators */
  1818. for (i=0; i<app_config.tone_count; ++i) {
  1819. if (app_config.tone_slots[i] != PJSUA_INVALID_ID) {
  1820. pjsua_conf_remove_port(app_config.tone_slots[i]);
  1821. app_config.tone_slots[i] = PJSUA_INVALID_ID;
  1822. }
  1823. }
  1824. #if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
  1825. /* Wipe out TLS key settings in transport configs */
  1826. pjsip_tls_setting_wipe_keys(&app_config.udp_cfg.tls_setting);
  1827. #endif
  1828. pj_pool_safe_release(&app_config.pool);
  1829. status = pjsua_destroy();
  1830. if (app_config.use_cli) {
  1831. use_cli = app_config.use_cli;
  1832. cli_fe = app_config.cli_cfg.cli_fe;
  1833. cli_telnet_port = app_config.cli_cfg.telnet_cfg.port;
  1834. }
  1835. /* Reset config */
  1836. pj_bzero(&app_config, sizeof(app_config));
  1837. app_config.wav_id = PJSUA_INVALID_ID;
  1838. app_config.rec_id = PJSUA_INVALID_ID;
  1839. if (use_cli) {
  1840. app_config.use_cli = use_cli;
  1841. app_config.cli_cfg.cli_fe = cli_fe;
  1842. app_config.cli_cfg.telnet_cfg.port = cli_telnet_port;
  1843. }
  1844. return status;
  1845. }
  1846. pj_status_t pjsua_app_destroy(void)
  1847. {
  1848. pj_status_t status;
  1849. status = app_destroy();
  1850. if (app_config.use_cli) {
  1851. cli_destroy();
  1852. }
  1853. return status;
  1854. }
  1855. /** ======================= **/
  1856. #ifdef STEREO_DEMO
  1857. /*
  1858. * In this stereo demo, we open the sound device in stereo mode and
  1859. * arrange the attachment to the PJSUA-LIB conference bridge as such
  1860. * so that channel0/left channel of the sound device corresponds to
  1861. * slot 0 in the bridge, and channel1/right channel of the sound
  1862. * device corresponds to slot 1 in the bridge. Then user can independently
  1863. * feed different media to/from the speakers/microphones channels, by
  1864. * connecting them to slot 0 or 1 respectively.
  1865. *
  1866. * Here's how the connection looks like:
  1867. *
  1868. +-----------+ stereo +-----------------+ 2x mono +-----------+
  1869. | AUDIO DEV |<------>| SPLITCOMB left|<------->|#0 BRIDGE |
  1870. +-----------+ | right|<------->|#1 |
  1871. +-----------------+ +-----------+
  1872. */
  1873. static void stereo_demo()
  1874. {
  1875. pjmedia_port *conf;
  1876. pj_status_t status;
  1877. /* Disable existing sound device */
  1878. conf = pjsua_set_no_snd_dev();
  1879. /* Create stereo-mono splitter/combiner */
  1880. status = pjmedia_splitcomb_create(app_config.pool,
  1881. PJMEDIA_PIA_SRATE(&conf->info) /* clock rate */,
  1882. 2 /* stereo */,
  1883. 2 * PJMEDIA_PIA_SPF(&conf->info),
  1884. PJMEDIA_PIA_BITS(&conf->info),
  1885. 0 /* options */,
  1886. &app_config.sc);
  1887. pj_assert(status == PJ_SUCCESS);
  1888. /* Connect channel0 (left channel?) to conference port slot0 */
  1889. status = pjmedia_splitcomb_set_channel(app_config.sc, 0 /* ch0 */,
  1890. 0 /*options*/,
  1891. conf);
  1892. pj_assert(status == PJ_SUCCESS);
  1893. /* Create reverse channel for channel1 (right channel?)... */
  1894. status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
  1895. app_config.sc,
  1896. 1 /* ch1 */,
  1897. 0 /* options */,
  1898. &app_config.sc_ch1);
  1899. pj_assert(status == PJ_SUCCESS);
  1900. /* .. and register it to conference bridge (it would be slot1
  1901. * if there's no other devices connected to the bridge)
  1902. */
  1903. status = pjsua_conf_add_port(app_config.pool, app_config.sc_ch1,
  1904. &app_config.sc_ch1_slot);
  1905. pj_assert(status == PJ_SUCCESS);
  1906. /* Create sound device */
  1907. status = pjmedia_snd_port_create(app_config.pool, -1, -1,
  1908. PJMEDIA_PIA_SRATE(&conf->info),
  1909. 2 /* stereo */,
  1910. 2 * PJMEDIA_PIA_SPF(&conf->info),
  1911. PJMEDIA_PIA_BITS(&conf->info),
  1912. 0, &app_config.snd);
  1913. pj_assert(status == PJ_SUCCESS);
  1914. /* Connect the splitter to the sound device */
  1915. status = pjmedia_snd_port_connect(app_config.snd, app_config.sc);
  1916. pj_assert(status == PJ_SUCCESS);
  1917. }
  1918. #endif