endpoint.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. #include "SpBase.h"
  2. #ifdef RVC_OS_WIN
  3. #include "stdafx.h"
  4. #include <ObjBase.h>
  5. #else
  6. #endif
  7. #include "endpoint.h"
  8. #include <sofia-sip/su.h>
  9. #include <sofia-sip/su_tag_class.h>
  10. #include <sofia-sip/nua.h>
  11. #include <sofia-sip/sip_status.h>
  12. #include <sofia-sip/sdp.h>
  13. #include <sofia-sip/sip_header.h>
  14. #include <sofia-sip/su_log.h>
  15. #include <sofia-sip/su_tagarg.h>
  16. #include <sofia-sip/su_tag_io.h>
  17. #include <sofia-sip/soa_tag.h>
  18. #include <sofia-sip/su_wait.h>
  19. #include <sofia-sip/nua_tag.h>
  20. #include "refcnt.h"
  21. #include "list.h"
  22. #include "sockutil.h"
  23. #include "hash.h"
  24. #include "video_session.h"
  25. #include "audio_session.h"
  26. #include "rvc_media_common.h"
  27. #include "Event.h"
  28. bool g_IsExternalTerminalted = false;
  29. // default: ptime = 20ms
  30. #define CONDITION_PARAMS \
  31. int status, \
  32. char const *phrase, \
  33. nua_t *nua, \
  34. struct endpoint_t *ep, \
  35. nua_handle_t *nh, \
  36. struct endpoint_call_t *call, \
  37. sip_t const *sip, \
  38. tagi_t tags[]
  39. // the same with rtp payload type, the same as huawei open-eye
  40. enum e_audio_rtp_pt
  41. {
  42. PCMU = 0, // 64kbps, 4.17
  43. PCMA = 8, // 64kbps, 4.11
  44. G729 = 18, // 8kbps, 3.9
  45. };
  46. enum e_video_rtp_pt
  47. {
  48. H263 = 34, // h263
  49. };
  50. enum e_media_dir
  51. {
  52. DIR_NONE = 0,
  53. DIR_TX = 1,
  54. DIR_RX = 2,
  55. DIR_BOTH = 3,
  56. };
  57. typedef struct sdpvideo_desc_t
  58. {
  59. unsigned long local_rtp_ip;
  60. int local_rtp_port;
  61. unsigned long remote_rtp_ip;
  62. int remote_rtp_port;
  63. int local_pt;
  64. int remote_pt;
  65. };
  66. typedef struct media_desc_t
  67. {
  68. unsigned long remote_ip;
  69. int remote_port;
  70. unsigned long local_ip;
  71. int local_port;
  72. int media_dir;
  73. int local_pt;
  74. int local_ptime;
  75. int remote_pt;
  76. int remote_ptime;
  77. int local_telephone_event_pt;
  78. int remote_telephone_event_pt;
  79. int param[16];
  80. }media_desc_t;
  81. // 1. how to exit
  82. struct endpoint_call_t {
  83. struct list_head entry;
  84. endpoint_call_callback_t cb;
  85. endpoint_t *ep;
  86. audio_session_t *audio;
  87. video_session_t *video;
  88. int id;
  89. su_home_t *home;
  90. nua_handle_t *nh;
  91. char *to_uri;
  92. char *from_uri;
  93. char *call_id;
  94. sdp_session_t *last_sdp;
  95. int connected;
  96. int local_media_port;
  97. int local_video_port;
  98. unsigned int last_media_desc_hash;
  99. char local_ip[RVC_MAX_IP_LEN];
  100. DeviceTypeEnum eDeviceType;
  101. CallingTypeEnum nCallType;
  102. sdpvideo_desc_t sdpvieo_desc;
  103. DECLARE_REF_COUNT_MEMBER(ref_cnt);
  104. };
  105. DECLARE_REF_COUNT_STATIC(endpoint_call, endpoint_call_t)
  106. struct endpoint_t {
  107. su_home_t home[1];
  108. su_root_t *root;
  109. nua_t *nua;
  110. endpoint_call_t *active_call;
  111. #ifdef RVC_OS_WIN
  112. HANDLE event_thread;
  113. #else
  114. pthread_t ievent_threadid;
  115. #endif
  116. int call_seq;
  117. int media_port_seq;
  118. CEntityBase *entity;
  119. struct list_head call_list;
  120. endpoint_conf_t conf;
  121. int curr_audio_dev_type; // handfree or pickup
  122. };
  123. static const char *state_desc[] = {
  124. "STATE::INIT",
  125. "STATE::CALLING",
  126. "STATE::PROCEEDING",
  127. "STATE::COMPLETING",
  128. "STATE::READY",
  129. "STATE::TERMINATING",
  130. "STATE::TERMINATED",
  131. };
  132. static const char* call_type_table[] = {
  133. "NORMAL_CALLTYPE",
  134. "PADRINGUP_CALLTYPE",
  135. "PADTOPAD_CALLTYPE",
  136. "MOBILETOPAD_CALLTYPE",
  137. "DOUBLERECORD_CALLTYPE"
  138. };
  139. #ifndef VIDEOPLAYER_FLAG_DOUBLESIZE
  140. #define VIDEOPLAYER_FLAG_DOUBLESIZE 0x01
  141. #endif
  142. #ifndef VIDEOPLAYER_FLAG_NOTIMER
  143. #define VIDEOPLAYER_FLAG_NOTIMER 0x02
  144. #endif
  145. #ifndef VIDEOPLAYER_FLAG_PUSH
  146. #define VIDEOPLAYER_FLAG_PUSH 0x04
  147. #endif
  148. #ifndef VIDEOPLAYER_FLAG_PULL
  149. #define VIDEOPLAYER_FLAG_PULL 0x08
  150. #endif
  151. #ifndef VIDEOPLAYER_FLAG_CHECKTOP
  152. #define VIDEOPLAYER_FLAG_CHECKTOP 0x10
  153. #endif
  154. #ifndef VIDEOPLAYER_FLAG_ZOOMOUTSIZE
  155. #define VIDEOPLAYER_FLAG_ZOOMOUTSIZE 0x100
  156. #endif
  157. static void endpoint_media_change_audio_dev(endpoint_call_t *call, e_dev_type current_dev_type)
  158. {
  159. if (call->audio) {
  160. LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_AUDIO_DEVICE_SWITCH, CSimpleStringA::Format("change audio device to %s mode.", DEV_PICKUP == current_dev_type ? "pickup" : "hand free").GetData());
  161. audio_session_change_dev(call->audio, current_dev_type);
  162. }
  163. }
  164. static __inline struct in_addr __lton(unsigned long ip)
  165. {
  166. struct in_addr addr;
  167. addr.s_addr = ip;
  168. return addr;
  169. }
  170. #ifdef RVC_OS_WIN
  171. static void endpoint_media_update_video(endpoint_call_t *call, media_desc_t *video_desc, video_session_callback_t* cb)
  172. {
  173. if (video_desc->media_dir == DIR_NONE) {
  174. if (call->video) {
  175. video_session_stop(call->video);
  176. video_session_destroy(call->video);
  177. call->video = NULL;
  178. }
  179. else{
  180. if (DOUBLERECORD_CALLTYPE == call->nCallType){
  181. double_record_broadcast_video_session_stop();
  182. }
  183. }
  184. }
  185. else
  186. {
  187. int rc;
  188. endpoint_conf_t *ep_conf = &call->ep->conf;
  189. if (!call->audio){
  190. audio_session_conf_t conf = {0};
  191. strcpy(&conf.in_dev[DEV_PICKUP][0], ep_conf->audio_pickup_in_dev);
  192. strcpy(&conf.in_dev[DEV_HANDFREE][0], ep_conf->audio_handfree_in_dev);
  193. strcpy(&conf.out_dev[DEV_PICKUP][0], ep_conf->audio_pickup_out_dev);
  194. strcpy(&conf.out_dev[DEV_HANDFREE][0], ep_conf->audio_handfree_out_dev);
  195. conf.agc_in[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_in_agc;
  196. conf.agc_in[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_in_agc;
  197. conf.agc_out[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_out_agc;
  198. conf.agc_out[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_out_agc;
  199. conf.ns_in[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_in_ns;
  200. conf.ns_in[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_in_ns;
  201. conf.ns_out[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_out_ns;
  202. conf.ns_out[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_out_ns;
  203. conf.aec[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_aec;
  204. conf.aec[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_aec;
  205. rc = audio_session_create(&conf, &call->audio);
  206. if (rc != 0) {
  207. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create audio session failed! rc = %d", rc);
  208. return;
  209. }
  210. }
  211. if (call->video) {
  212. video_session_destroy(call->video);
  213. call->video = NULL;
  214. }
  215. int i = 0;
  216. int local_view_x = video_desc->param[i++];
  217. int local_view_y = video_desc->param[i++];
  218. int local_view_cx = video_desc->param[i++];
  219. int local_view_cy = video_desc->param[i++];
  220. int remote_view_x = video_desc->param[i++];
  221. int remote_view_y = video_desc->param[i++];
  222. int remote_view_cx = video_desc->param[i++];
  223. int remote_view_cy = video_desc->param[i++];
  224. int remote_width = video_desc->param[i++];
  225. int remote_height = video_desc->param[i++];
  226. int local_hwd_move = video_desc->param[i++];
  227. int remote_hwd_move = video_desc->param[i++];
  228. video_session_conf_t video_conf = {0};
  229. video_conf.bit_rate = 256 * 1024;
  230. video_conf.local_rtp_ip = video_desc->local_ip;
  231. video_conf.local_rtp_port = video_desc->local_port;
  232. video_conf.local_video_view_x = local_view_x;
  233. video_conf.local_video_view_y = local_view_y;
  234. video_conf.local_video_view_cx = local_view_cx;
  235. video_conf.local_video_view_cy = local_view_cy;
  236. video_conf.mtu = ep_conf->mtu;
  237. video_conf.video_quant = ep_conf->quant;
  238. video_conf.remote_rtp_ip = video_desc->remote_ip;
  239. video_conf.remote_rtp_port = video_desc->remote_port;
  240. video_conf.remote_video_view_x = remote_view_x;
  241. video_conf.remote_video_view_y = remote_view_y;
  242. video_conf.remote_video_view_cx = remote_view_cx;
  243. video_conf.remote_video_view_cy = remote_view_cy;
  244. video_conf.remote_video_width = remote_width;
  245. video_conf.remote_video_height = remote_height;
  246. video_conf.ref_active_camera = call->ep->conf.ref_active_camera;
  247. video_conf.ref_camera_switch = call->ep->conf.ref_camera_switch;
  248. video_conf.ref_camera_state = call->ep->conf.ref_camera_state;
  249. video_conf.ref_window_state = call->ep->conf.ref_window_state;
  250. video_conf.ref_Is_showPersonArea = call->ep->conf.ref_Is_showPersonArea;
  251. video_conf.ref_active_img = NULL;
  252. video_conf.ref_Is_ActiveInspect = NULL;
  253. video_conf.camera_count = call->ep->conf.camera_count;
  254. video_conf.screen_count = call->ep->conf.screen_count;
  255. video_conf.ref_Up_Fps = call->ep->conf.ref_Up_Fps;
  256. video_conf.eDeviceType = call->eDeviceType;
  257. video_conf.nCallType = call->nCallType;
  258. video_conf.local_move = local_hwd_move;
  259. video_conf.remote_move = remote_hwd_move;
  260. video_conf.video_echo_cb = cb;
  261. video_conf.ilocal_wind_flags = VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP;
  262. video_conf.iremote_wind_flags = VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP;
  263. video_conf.eType = (eVideoRenderType)call->ep->conf.irendertype;
  264. video_conf.local_pt = REC_COMMON_VIDEO_H264_PT;
  265. video_conf.remote_pt = REC_COMMON_VIDEO_H264_PT;
  266. rc = video_session_create(&video_conf, &call->video);
  267. if (rc != 0) {
  268. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create video session failed! rc = %d", rc);
  269. return;
  270. }
  271. rc = video_session_start(call->video);
  272. if (rc != 0) {
  273. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start video session failed! rc = %d", rc);
  274. video_session_destroy(call->video);
  275. call->video = NULL;
  276. return;
  277. }
  278. }
  279. }
  280. #else
  281. static void endpoint_media_update_video(endpoint_call_t *call, media_desc_t *video_desc, rvc_video_render_callback_t* cb)
  282. {
  283. if (video_desc->media_dir == DIR_NONE) {
  284. if (call->video) {
  285. video_session_stop(call->video);
  286. video_session_destroy(call->video);
  287. call->video = NULL;
  288. }
  289. }
  290. else
  291. {
  292. int rc;
  293. endpoint_conf_t *ep_conf = &call->ep->conf;
  294. if (!call->audio){
  295. audio_session_conf_t conf = {0};
  296. strcpy(&conf.in_dev[DEV_PICKUP][0], ep_conf->audio_pickup_in_dev);
  297. strcpy(&conf.in_dev[DEV_HANDFREE][0], ep_conf->audio_handfree_in_dev);
  298. strcpy(&conf.out_dev[DEV_PICKUP][0], ep_conf->audio_pickup_out_dev);
  299. strcpy(&conf.out_dev[DEV_HANDFREE][0], ep_conf->audio_handfree_out_dev);
  300. conf.agc_in[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_in_agc;
  301. conf.agc_in[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_in_agc;
  302. conf.agc_out[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_out_agc;
  303. conf.agc_out[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_out_agc;
  304. conf.ns_in[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_in_ns;
  305. conf.ns_in[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_in_ns;
  306. conf.ns_out[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_out_ns;
  307. conf.ns_out[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_out_ns;
  308. conf.aec[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_aec;
  309. conf.aec[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_aec;
  310. rc = audio_session_create(&conf, &call->audio);
  311. if (rc != 0) {
  312. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create audio session failed! rc = %d", rc);
  313. return;
  314. }
  315. }
  316. if (call->video) {
  317. video_session_destroy(call->video);
  318. call->video = NULL;
  319. }
  320. int i = 0;
  321. int remote_width = video_desc->param[i++];
  322. int remote_height = video_desc->param[i++];
  323. video_session_conf_t video_conf = {0};
  324. video_conf.bit_rate = 256 * 1024;
  325. video_conf.local_rtp_ip = video_desc->local_ip;
  326. video_conf.local_rtp_port = video_desc->local_port;
  327. video_conf.mtu = ep_conf->mtu;
  328. video_conf.video_quant = ep_conf->quant;
  329. video_conf.remote_rtp_ip = video_desc->remote_ip;
  330. video_conf.remote_rtp_port = video_desc->remote_port;
  331. video_conf.remote_video_width = remote_width;
  332. video_conf.remote_video_height = remote_height;
  333. video_conf.ref_active_camera = call->ep->conf.ref_active_camera;
  334. video_conf.ref_camera_switch = call->ep->conf.ref_camera_switch;
  335. video_conf.ref_camera_state = call->ep->conf.ref_camera_state;
  336. video_conf.ref_window_state = call->ep->conf.ref_window_state;
  337. video_conf.ref_Is_showPersonArea = call->ep->conf.ref_Is_showPersonArea;
  338. video_conf.ref_active_img = NULL;
  339. video_conf.ref_Is_ActiveInspect = NULL;
  340. video_conf.camera_count = call->ep->conf.camera_count;
  341. video_conf.screen_count = call->ep->conf.screen_count;
  342. video_conf.ref_Up_Fps = call->ep->conf.ref_Up_Fps;
  343. video_conf.eDeviceType = call->eDeviceType;
  344. video_conf.nCallType = call->nCallType;
  345. memcpy(&video_conf.video_render_cb, cb, sizeof(rvc_video_render_callback_t));
  346. video_conf.ilocal_wind_flags = VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP;
  347. video_conf.iremote_wind_flags = VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP;
  348. video_conf.local_pt = REC_COMMON_VIDEO_H264_PT;
  349. video_conf.remote_pt = REC_COMMON_VIDEO_H264_PT;
  350. rc = video_session_create(&video_conf, &call->video);
  351. if (rc != 0) {
  352. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create video session failed! rc = %d", rc);
  353. return;
  354. }
  355. rc = video_session_start(call->video);
  356. if (rc != 0) {
  357. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start video session failed! rc = %d", rc);
  358. video_session_destroy(call->video);
  359. call->video = NULL;
  360. return;
  361. }
  362. }
  363. }
  364. #endif
  365. static void endpoint_media_update_audio(endpoint_call_t *call, media_desc_t *audio_desc, int dev_type)
  366. {
  367. if (audio_desc->media_dir == DIR_NONE) {
  368. if (call->audio) {
  369. audio_session_stop(call->audio);
  370. audio_session_destroy(call->audio);
  371. call->audio = NULL;
  372. }
  373. else{
  374. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("call->audio is null.");
  375. }
  376. if (call->video) { // close video also
  377. video_session_stop(call->video);
  378. video_session_destroy(call->video);
  379. call->video = NULL;
  380. }
  381. else{
  382. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("call->video is null.");
  383. #ifdef RVC_OS_WIN
  384. if (DOUBLERECORD_CALLTYPE == call->nCallType){
  385. double_record_broadcast_video_session_stop();
  386. }
  387. #endif
  388. }
  389. } else {
  390. int rc;
  391. if (call->audio) {
  392. audio_session_stop(call->audio);
  393. audio_session_destroy(call->audio);
  394. call->audio = NULL;
  395. }
  396. if (!call->audio) {
  397. endpoint_conf_t *ep_conf = &call->ep->conf;
  398. audio_session_conf_t conf = {0};
  399. strcpy(&conf.in_dev[DEV_PICKUP][0], ep_conf->audio_pickup_in_dev);
  400. strcpy(&conf.in_dev[DEV_HANDFREE][0], ep_conf->audio_handfree_in_dev);
  401. strcpy(&conf.out_dev[DEV_PICKUP][0], ep_conf->audio_pickup_out_dev);
  402. strcpy(&conf.out_dev[DEV_HANDFREE][0], ep_conf->audio_handfree_out_dev);
  403. conf.agc_in[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_in_agc;
  404. conf.agc_in[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_in_agc;
  405. conf.agc_out[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_out_agc;
  406. conf.agc_out[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_out_agc;
  407. conf.ns_in[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_in_ns;
  408. conf.ns_in[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_in_ns;
  409. conf.ns_out[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_out_ns;
  410. conf.ns_out[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_out_ns;
  411. conf.aec[DEV_PICKUP] = !!ep_conf->audio_dsp.audio_pickup_aec;
  412. conf.aec[DEV_HANDFREE] = !!ep_conf->audio_dsp.audio_handfree_aec;
  413. rc = audio_session_create(&conf, &call->audio);
  414. if (rc != 0) {
  415. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("create audio session failed! rc = %d", rc);
  416. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setResultCode("RTA3109")("视频连线创建音频通道失败");
  417. return;
  418. }
  419. }
  420. audio_session_phonemedia_conf_t phone_conf = {0};
  421. phone_conf.dir = audio_desc->media_dir;
  422. phone_conf.dev_type = (e_dev_type)dev_type;
  423. phone_conf.local_dtmf_pt = audio_desc->local_telephone_event_pt;
  424. phone_conf.local_pt = audio_desc->local_pt;
  425. phone_conf.local_ptime = audio_desc->local_ptime;
  426. phone_conf.local_rtp_ip = audio_desc->local_ip;
  427. phone_conf.local_rtp_port = audio_desc->local_port;
  428. phone_conf.remote_dtmf_pt = audio_desc->remote_telephone_event_pt;
  429. phone_conf.remote_pt = audio_desc->remote_pt;
  430. phone_conf.remote_ptime = audio_desc->remote_ptime;
  431. phone_conf.remote_rtp_ip = audio_desc->remote_ip;
  432. phone_conf.remote_rtp_port = audio_desc->remote_port;
  433. if (NULL != call){
  434. phone_conf.eCalltype = call->nCallType;
  435. phone_conf.eDeviceType = call->eDeviceType;
  436. }
  437. rc = audio_session_start_phonemedia(call->audio, &phone_conf);
  438. if (rc != 0)
  439. {
  440. LogWarn(Severity_Middle, Error_Debug, ERROR_MOD_SIP_AUDIO_INITFAIL, CSimpleStringA::Format("start audio session failed! rc = %d.", rc).GetData());
  441. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setResultCode("RTA310A")("视频连线启动音频通道失败");
  442. audio_session_destroy(call->audio);
  443. call->audio = NULL;
  444. return;
  445. }
  446. }
  447. }
  448. static unsigned int __hash_media_desc(media_desc_t *media_desc, unsigned int hash_code)
  449. {
  450. if (media_desc) {
  451. hash_code += (unsigned int)media_desc->local_ip * 33;
  452. hash_code += (unsigned int)media_desc->remote_ip * 33;
  453. hash_code += (unsigned int)media_desc->local_port * 33;
  454. hash_code += (unsigned int)media_desc->remote_port * 33;
  455. hash_code += (unsigned int)media_desc->media_dir * 33;
  456. hash_code += (unsigned int)media_desc->local_pt * 33;
  457. hash_code += (unsigned int)media_desc->local_ptime * 33;
  458. hash_code += (unsigned int)media_desc->remote_pt * 33;
  459. hash_code += (unsigned int)media_desc->remote_ptime * 33;
  460. hash_code += (unsigned int)media_desc->local_telephone_event_pt * 33;
  461. hash_code += (unsigned int)media_desc->remote_telephone_event_pt * 33;
  462. }
  463. return hash_code;
  464. }
  465. static unsigned int hash_media_desc(media_desc_t *audio)
  466. {
  467. unsigned int hash_code = 0;
  468. hash_code = __hash_media_desc(audio, hash_code);
  469. return hash_code;
  470. }
  471. #define USE_ALAW 1
  472. static const char *call_make_offer(endpoint_call_t *call, char *buf, int size)
  473. {
  474. int local_audio_rtp_port = call->local_media_port;
  475. int local_video_rtp_port = call->local_video_port;
  476. int local_video_pt = REC_COMMON_VIDEO_PT;
  477. int need;
  478. const char *fmt;
  479. {
  480. fmt =
  481. "c=IN IP4 %s\r\n"
  482. #ifdef USE_ALAW
  483. "m=audio %d RTP/AVP 8\r\n"
  484. "a=rtpmap:8 PCMA/8000\r\n"
  485. "a=ptime:30\r\n"
  486. #elif defined(USE_G729)
  487. "m=audio %d RTP/AVP 18\r\n"
  488. "a=rtpmap:18 G729/8000\r\n"
  489. "a=fmtp:18 annexb=no\r\n"
  490. "a=ptime:30\r\n"
  491. #endif
  492. //"a=rtpmap:97 telephone-event/8000\r\n"
  493. //"a=fmtp:97 0-15\r\n"
  494. #ifdef USE_H263_VIDEO
  495. "m=video %d RTP/AVP 34\r\n"
  496. "a=rtpmap:34 H263/90000\r\n"
  497. "a=fmtp:34 QCIF=2 MaxBR=5040\r\n"
  498. #endif
  499. //"a=inactive\r\n" //for debug only
  500. ;
  501. }
  502. need = _scprintf(fmt, call->local_ip, local_audio_rtp_port);
  503. if (size < 0 || size > need) {
  504. sprintf(buf, fmt, call->local_ip, local_audio_rtp_port);
  505. return buf;
  506. } else {
  507. return NULL;
  508. }
  509. }
  510. static int new_call_id(endpoint_t *ep)
  511. {
  512. return ++ep->call_seq;
  513. }
  514. int new_media_port(endpoint_t *ep)
  515. {
  516. ep->media_port_seq += 2;
  517. if (ep->media_port_seq >= ep->conf.media_stop_port) {
  518. ep->media_port_seq = ep->conf.media_start_port;
  519. }
  520. return ep->media_port_seq;
  521. }
  522. static void handle_invite(CONDITION_PARAMS)
  523. {
  524. // we are client only, not accept in-comming call
  525. nua_respond(nh, SIP_403_FORBIDDEN, TAG_END());
  526. }
  527. static void handle_reinvite(CONDITION_PARAMS)
  528. {
  529. char sdp[512] = {0};
  530. sip_content_type_t const *ct = NULL;
  531. tl_gets(tags,
  532. SIPTAG_CONTENT_TYPE_REF(ct),
  533. TAG_END());
  534. if (ct) {
  535. call_make_offer(call, sdp, sizeof(sdp));
  536. }
  537. nua_respond(nh,
  538. SIP_200_OK,
  539. TAG_IF(ct, SOATAG_USER_SDP_STR(sdp)),
  540. TAG_IF(ct, SOATAG_AUDIO_AUX("telephone-event")),
  541. TAG_END());
  542. }
  543. static void handle_r_invite(CONDITION_PARAMS)
  544. {
  545. // nothing to do
  546. }
  547. static int dir_from_attr(const sdp_media_t *m)
  548. {
  549. int dir = DIR_NONE;
  550. if (m) {
  551. if (m->m_attributes) {
  552. static const char *ds[] = {
  553. "inactive", "sendonly", "recvonly", "sendrecv"
  554. };
  555. int i;
  556. int n = array_size(ds);
  557. for (i = 0; i < n; ++i) {
  558. sdp_attribute_t *attr = sdp_attribute_find(m->m_attributes, ds[i]);
  559. if (attr) {
  560. dir = i;
  561. break;
  562. }
  563. }
  564. if (i >= n)
  565. dir = DIR_BOTH;
  566. } else {
  567. dir = DIR_BOTH;
  568. }
  569. }
  570. return dir;
  571. }
  572. static int ptime_from_attr(const sdp_media_t *m)
  573. {
  574. int ptime = 0; // zero for default
  575. if (m) {
  576. if (m->m_attributes) {
  577. sdp_attribute_t *attr = sdp_attribute_find(m->m_attributes, "ptime");
  578. if (attr && attr->a_value) {
  579. ptime = atoi(attr->a_value);
  580. if (ptime < 0)
  581. ptime = 0;
  582. }
  583. }
  584. }
  585. return ptime;
  586. }
  587. static const sdp_rtpmap_t *find_codec(const sdp_rtpmap_t *list, const char *codec)
  588. {
  589. if (!codec)
  590. return NULL;
  591. for (;list; list=list->rm_next) {
  592. if (!_stricmp(list->rm_encoding, codec)) {
  593. break;
  594. }
  595. }
  596. return list;
  597. }
  598. static int get_telephone_event_pt(const sdp_rtpmap_t *list)
  599. {
  600. int pt = -1;
  601. const sdp_rtpmap_t *rm = find_codec(list, "telephone-event");
  602. if (rm) {
  603. pt = rm->rm_pt;
  604. }
  605. return pt;
  606. }
  607. static int get_media_direction(const sdp_media_t *lm, const sdp_media_t *rm)
  608. {
  609. int media_dir = DIR_NONE;
  610. if (rm->m_port == 0 || lm->m_port == 0) {
  611. media_dir = DIR_NONE;
  612. } else {
  613. int ldir = dir_from_attr(lm);
  614. int rdir = dir_from_attr(rm);
  615. if (ldir == DIR_NONE || rdir == DIR_NONE) {
  616. media_dir = DIR_NONE;
  617. } else if (ldir == DIR_TX || rdir == DIR_RX) {
  618. media_dir = DIR_TX;
  619. } else if (ldir == DIR_RX || rdir == DIR_TX) {
  620. media_dir = DIR_RX;
  621. } else {
  622. media_dir = DIR_BOTH;
  623. }
  624. }
  625. return media_dir;
  626. }
  627. static int negotiate_audio(endpoint_call_t *call, media_desc_t *audio_desc, const sdp_media_t *lm, const sdp_media_t *rm)
  628. {
  629. memset(audio_desc, 0, sizeof(media_desc_t));
  630. const char *local_ip;
  631. const char *remote_ip;
  632. local_ip = lm->m_connections ? lm->m_connections->c_address : lm->m_session->sdp_connection->c_address;
  633. remote_ip = rm->m_connections ? rm->m_connections->c_address : rm->m_session->sdp_connection->c_address;
  634. audio_desc->local_ip = strlen(call->local_ip) ? inet_addr(call->local_ip) : 0;
  635. audio_desc->local_port = call->local_media_port;
  636. audio_desc->remote_ip = remote_ip ? inet_addr(remote_ip) : 0;
  637. audio_desc->remote_port = rm->m_port;
  638. audio_desc->local_telephone_event_pt = 97;
  639. audio_desc->remote_telephone_event_pt = get_telephone_event_pt(rm->m_rtpmaps);
  640. audio_desc->local_ptime = ptime_from_attr(lm);
  641. audio_desc->remote_ptime = ptime_from_attr(rm);
  642. int pt = -1;
  643. for (sdp_rtpmap_t *t = lm->m_rtpmaps; t; t = t->rm_next) {
  644. sdp_rtpmap_t *tt = sdp_rtpmap_find_matching(rm->m_rtpmaps, t);
  645. if (tt && tt->rm_pt < 96) {
  646. pt = tt->rm_pt;
  647. break;
  648. }
  649. }
  650. audio_desc->local_pt = audio_desc->remote_pt = pt;
  651. audio_desc->media_dir = get_media_direction(lm, rm);
  652. return 0;
  653. }
  654. static int negotiate_video(endpoint_call_t *call, media_desc_t *video_desc, const sdp_media_t *lm, const sdp_media_t *rm)
  655. {
  656. memset(video_desc, 0, sizeof(media_desc_t));
  657. const char *local_ip;
  658. const char *remote_ip;
  659. local_ip = lm->m_connections ? lm->m_connections->c_address : lm->m_session->sdp_connection->c_address;
  660. remote_ip = rm->m_connections ? rm->m_connections->c_address : rm->m_session->sdp_connection->c_address;
  661. video_desc->local_ip = strlen(call->local_ip) ? inet_addr(call->local_ip) : 0;
  662. video_desc->local_port = call->local_video_port;
  663. video_desc->remote_ip = remote_ip ? inet_addr(remote_ip) : 0;
  664. video_desc->remote_port = rm->m_port;
  665. video_desc->local_telephone_event_pt = 97;
  666. video_desc->remote_telephone_event_pt = get_telephone_event_pt(rm->m_rtpmaps);
  667. video_desc->local_ptime = ptime_from_attr(lm);
  668. video_desc->remote_ptime = ptime_from_attr(rm);
  669. int pt = -1;
  670. for (sdp_rtpmap_t *t = lm->m_rtpmaps; t; t = t->rm_next) {
  671. sdp_rtpmap_t *tt = sdp_rtpmap_find_matching(rm->m_rtpmaps, t);
  672. if (tt) {
  673. pt = tt->rm_pt;
  674. break;
  675. }
  676. }
  677. video_desc->local_pt = video_desc->remote_pt = pt;
  678. video_desc->media_dir = get_media_direction(lm, rm);
  679. return 0;
  680. }
  681. static void negotiate_sdp(endpoint_call_t *call, const sdp_session_t *local_sdp, const sdp_session_t* remote_sdp)
  682. {
  683. const sdp_media_t *lm = local_sdp->sdp_media;
  684. const sdp_media_t *rm = remote_sdp->sdp_media;
  685. media_desc_t audio_desc = {0}, video_desc = {0};
  686. int rc = -1, rv = -1;
  687. for(; lm; lm = lm->m_next)
  688. {
  689. if (0 == lm->m_port)
  690. {
  691. continue;
  692. }
  693. const sdp_media_t *rm = remote_sdp->sdp_media;
  694. for (; rm; rm = rm->m_next)
  695. {
  696. if (rm->m_type == lm->m_type)
  697. {
  698. if (sdp_media_audio == rm->m_type)
  699. {
  700. rc = negotiate_audio(call, &audio_desc, lm, rm);
  701. }
  702. if (call->nCallType != NORMAL_CALLTYPE && call->nCallType != DOUBLERECORD_CALLTYPE)
  703. {
  704. if (sdp_media_video == rm->m_type)
  705. {
  706. rv = negotiate_video(call, &video_desc, lm, rm);
  707. }
  708. }
  709. }
  710. }
  711. }
  712. if (rc == 0)
  713. {
  714. unsigned int hash_code = hash_media_desc(&audio_desc);
  715. if (hash_code != call->last_media_desc_hash)
  716. {
  717. endpoint_media_update_audio(call, &audio_desc, call->ep->curr_audio_dev_type);
  718. call->last_media_desc_hash = hash_code;
  719. //char strmsg[MAX_PATH] = {0};
  720. //snprintf(strmsg, MAX_PATH, "negotiate audio success!local audio ip=%s port=%d pt=%d,remote audio ip=%s port=%d,pt=%d",str_local,audio_desc.local_port,audio_desc.local_pt,str_remote,audio_desc.remote_port,audio_desc.remote_pt);
  721. //LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_CALL_AUDIO_NEGOTIATE, strmsg);
  722. }
  723. }
  724. if (rv == 0)
  725. {
  726. unsigned int hash_code = hash_media_desc(&video_desc);
  727. if (hash_code != call->last_media_desc_hash)
  728. {
  729. call->sdpvieo_desc.local_pt = video_desc.local_pt;
  730. call->sdpvieo_desc.local_rtp_ip = video_desc.local_ip;
  731. call->sdpvieo_desc.local_rtp_port = video_desc.local_port;
  732. call->sdpvieo_desc.remote_pt = video_desc.remote_pt;
  733. call->sdpvieo_desc.remote_rtp_ip = video_desc.remote_ip;
  734. call->sdpvieo_desc.remote_rtp_port = video_desc.remote_port;
  735. call->last_media_desc_hash = hash_code;
  736. //char strmsg[MAX_PATH] = {0};
  737. //_snprintf(strmsg, MAX_PATH, "negotiate video success!local video ip=%s port=%d pt=%d,remote video ip=%s port=%d pt=%d",str_local,video_desc.local_port,video_desc.local_pt,str_remote,video_desc.remote_port,video_desc.remote_pt);
  738. //LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_CALL_VIDEO_NEGOTIATE, strmsg);
  739. }
  740. }
  741. else
  742. {
  743. if (call->nCallType != NORMAL_CALLTYPE && call->nCallType != DOUBLERECORD_CALLTYPE)
  744. {
  745. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("negotiate video Fail!!!");
  746. }
  747. }
  748. }
  749. static void on_state(CONDITION_PARAMS)
  750. {
  751. int offer_recv = 0, answer_recv = 0, offer_sent = 0, answer_sent = 0;
  752. int state = nua_callstate_init;
  753. const char *replaces_str = NULL;
  754. const sdp_session_t *remote_sdp = NULL;
  755. const char *remote_sdp_str = NULL;
  756. const sdp_session_t *local_sdp = NULL;
  757. const char *local_sdp_str = NULL;
  758. int st;
  759. if (!call) {
  760. nua_handle_destroy(nh);
  761. return;
  762. }
  763. if (g_IsExternalTerminalted)
  764. {
  765. return;
  766. }
  767. tl_gets(tags,
  768. NUTAG_CALLSTATE_REF(state),
  769. NUTAG_OFFER_RECV_REF(offer_recv),
  770. NUTAG_ANSWER_RECV_REF(answer_recv),
  771. NUTAG_OFFER_SENT_REF(offer_sent),
  772. NUTAG_ANSWER_SENT_REF(answer_sent),
  773. SIPTAG_REPLACES_STR_REF(replaces_str),
  774. SOATAG_REMOTE_SDP_STR_REF(remote_sdp_str),
  775. SOATAG_LOCAL_SDP_STR_REF(local_sdp_str),
  776. TAG_END());
  777. switch (state)
  778. {
  779. case nua_callstate_calling:
  780. {
  781. if (local_sdp_str) {
  782. sdp_parser_t* parser = sdp_parse(call->home, local_sdp_str, strlen(local_sdp_str), -1);
  783. assert(call->last_sdp == NULL);
  784. call->last_sdp = sdp_session_dup(call->home, sdp_session(parser));
  785. sdp_parser_free(parser);
  786. }
  787. st = CALLING;
  788. call->cb.on_call_state(st, state_desc[st], phrase, call->cb.user_data);
  789. }
  790. break;
  791. case nua_callstate_proceeding:
  792. {
  793. st = PROCEEDING;
  794. call->cb.on_call_state(st, state_desc[st], phrase, call->cb.user_data);
  795. if (remote_sdp_str) {
  796. sdp_parser_t* parser = sdp_parse(call->home, remote_sdp_str, strlen(remote_sdp_str), -1);
  797. remote_sdp = sdp_session(parser);
  798. if (call->last_sdp) {
  799. negotiate_sdp(call, call->last_sdp, remote_sdp);
  800. su_free(call->home, call->last_sdp);
  801. call->last_sdp = NULL;
  802. }
  803. sdp_parser_free(parser);
  804. }
  805. }
  806. break;
  807. case nua_callstate_completing:
  808. {
  809. st = COMPLETING;
  810. call->cb.on_call_state(st, state_desc[st], phrase, call->cb.user_data);
  811. if (remote_sdp_str) {
  812. sdp_parser_t* parser = sdp_parse(call->home, remote_sdp_str, strlen(remote_sdp_str), -1);
  813. remote_sdp = sdp_session(parser);
  814. if (call->last_sdp) {
  815. negotiate_sdp(call, call->last_sdp, remote_sdp);
  816. su_free(call->home, call->last_sdp);
  817. call->last_sdp = NULL;
  818. }
  819. sdp_parser_free(parser);
  820. }
  821. }
  822. break;
  823. case nua_callstate_completed:
  824. {
  825. if (remote_sdp_str) {
  826. if (!call->last_sdp) {
  827. sdp_parser_t* parser = sdp_parse(call->home, remote_sdp_str, strlen(remote_sdp_str), -1);
  828. call->last_sdp = sdp_session_dup(call->home, sdp_session(parser));
  829. sdp_parser_free(parser);
  830. }
  831. }
  832. if (local_sdp_str) {
  833. if (call->last_sdp) {
  834. sdp_parser_t* parser = sdp_parse(call->home, local_sdp_str, strlen(local_sdp_str), -1);
  835. local_sdp = sdp_session(parser);
  836. negotiate_sdp(call, local_sdp, call->last_sdp);
  837. su_free(call->home, call->last_sdp);
  838. call->last_sdp = NULL;
  839. sdp_parser_free(parser);
  840. }
  841. }
  842. }
  843. break;
  844. case nua_callstate_ready:
  845. {
  846. call->connected = true;
  847. st = READY;
  848. call->cb.on_call_state(st, state_desc[st], phrase, call->cb.user_data);
  849. if (remote_sdp_str) {
  850. sdp_parser_t* parser = sdp_parse(call->home, remote_sdp_str, strlen(remote_sdp_str), -1);
  851. remote_sdp = sdp_session(parser);
  852. if (call->last_sdp) {
  853. negotiate_sdp(call, call->last_sdp, remote_sdp);
  854. su_free(call->home, call->last_sdp);
  855. call->last_sdp = NULL;
  856. }
  857. sdp_parser_free(parser);
  858. }
  859. #if 0
  860. if (remote_sdp_str) {
  861. sdp_parser_t* parser = sdp_parse(call->home, remote_sdp_str, strlen(remote_sdp_str), -1);
  862. remote_sdp = sdp_session(parser);
  863. negotiate_sdp(call, call->last_sdp, remote_sdp);
  864. sdp_parser_free(parser);
  865. }
  866. else if (local_sdp_str) {
  867. sdp_parser_t* parser = sdp_parse(call->home, local_sdp_str, strlen(local_sdp_str), -1);
  868. local_sdp = sdp_session(parser);
  869. negotiate_sdp(call, local_sdp, call->last_sdp);
  870. sdp_parser_free(parser);
  871. }
  872. #endif
  873. }
  874. break;
  875. case nua_callstate_terminated:
  876. {
  877. st = TERMINATED;
  878. {
  879. media_desc_t audio_desc = { 0 };
  880. audio_desc.media_dir = DIR_NONE;
  881. endpoint_media_update_audio(call, &audio_desc, call->ep->curr_audio_dev_type);
  882. }
  883. call->cb.on_call_state(st, state_desc[st], phrase, call->cb.user_data);
  884. nua_handle_bind(nh, NULL);
  885. nua_handle_destroy(nh);
  886. call->nh = NULL;
  887. }
  888. break;
  889. default:
  890. break;
  891. }
  892. }
  893. void terminatedcall(endpoint_call_t *call)
  894. {
  895. int st = TERMINATED;
  896. g_IsExternalTerminalted = true;
  897. {
  898. media_desc_t audio_desc = {0};
  899. audio_desc.media_dir = DIR_NONE;
  900. endpoint_media_update_audio(call, &audio_desc, call->ep->curr_audio_dev_type);
  901. }
  902. }
  903. static void endpoint_callback(nua_event_t event,
  904. int status,
  905. char const *phrase,
  906. nua_t *nua,
  907. nua_magic_t *magic,
  908. nua_handle_t *nh,
  909. nua_hmagic_t *hmagic,
  910. sip_t const *sip,
  911. tagi_t tags[])
  912. {
  913. switch (event)
  914. {
  915. if (!g_IsExternalTerminalted)
  916. {
  917. case nua_i_invite:
  918. if (hmagic == NULL) {
  919. handle_invite(status, phrase, nua, (endpoint_t*)magic, nh, (endpoint_call_t*)hmagic, sip, tags);
  920. } else {
  921. handle_reinvite(status, phrase, nua, (endpoint_t*)magic, nh, (endpoint_call_t*)hmagic, sip, tags);
  922. }
  923. break;
  924. case nua_r_invite:
  925. handle_r_invite(status, phrase, nua, (endpoint_t*)magic, nh, (endpoint_call_t*)hmagic, sip, tags);
  926. break;
  927. case nua_i_bye:
  928. //.....
  929. break;
  930. case nua_i_info:
  931. break;
  932. case nua_i_state:
  933. on_state(status, phrase, nua, (endpoint_t*)magic, nh, (endpoint_call_t*)hmagic, sip, tags);
  934. break;
  935. case nua_i_active:
  936. break;
  937. case nua_i_options:
  938. //nua_respond(nh, SIP_200_OK, NUTAG_WITH_THIS(nua), SOATAG_USER_SDP_STR(NULL), TAG_END());
  939. nua_respond(nh, SIP_200_OK, TAG_END());
  940. break;
  941. case nua_r_cancel:
  942. if (status > 300) {
  943. nua_bye(nh, TAG_END());
  944. }
  945. break;
  946. case nua_i_prack:
  947. case nua_r_prack:
  948. break;
  949. case nua_r_bye:
  950. //LOG_TRACE("nua_r_bye");
  951. if (status > 300) {
  952. nua_bye(nh, TAG_END());
  953. }
  954. break;
  955. /* and so on ... */
  956. default:
  957. if (status > 100) { /* unknown event -> print out error message */
  958. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unknown event %d: %03d %s.",
  959. event,
  960. status,
  961. phrase);
  962. } else {
  963. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unknown event %d.", event);
  964. }
  965. break;
  966. }
  967. }
  968. if (!hmagic)
  969. {
  970. nua_handle_destroy(nh);
  971. return;
  972. }
  973. }
  974. #ifdef RVC_OS_WIN
  975. static unsigned int __stdcall __event_thread(void* arg)
  976. #else
  977. void* __event_thread(void* arg)
  978. #endif
  979. {
  980. endpoint_t *ep = (endpoint_t *)arg;
  981. ep->entity->GetFunction()->InitLogCurrentThread();
  982. #ifdef RVC_OS_WIN
  983. CoInitialize(NULL);
  984. #else
  985. #endif // RVC_OS_WIN
  986. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("enter event_thread.");
  987. ep->root = su_root_create(ep);
  988. ep->nua = nua_create(ep->root,
  989. &endpoint_callback,
  990. ep,
  991. NUTAG_URL(ep->conf.uri),
  992. TAG_END());
  993. nua_set_params(ep->nua,
  994. SIPTAG_ALLOW_STR("INVITE,CANCEL,BYE,ACK,INFO,OPTIONS"),
  995. NUTAG_AUTOALERT(1),
  996. NUTAG_ALLOW("PRACK"),
  997. NUTAG_EARLY_MEDIA(1),
  998. NUTAG_SESSION_TIMER(1),
  999. NUTAG_AUTOANSWER(0),
  1000. NUTAG_AUTOACK(1),
  1001. NTATAG_SIP_T1X64(8000),
  1002. TAG_NULL());
  1003. su_root_run(ep->root);
  1004. nua_shutdown(ep->nua);
  1005. ep->nua = NULL;
  1006. su_root_destroy(ep->root);
  1007. ep->root = NULL;
  1008. #ifdef RVC_OS_WIN
  1009. CoUninitialize();
  1010. #else
  1011. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("leave event_thread.");
  1012. #endif // RVC_OS_WIN
  1013. return 0;
  1014. }
  1015. static int __endpoint_break(void *arg)
  1016. {
  1017. endpoint_t *ep = (endpoint_t *)arg;
  1018. //.....nua_shutdown
  1019. su_root_break(ep->root);
  1020. return 0;
  1021. }
  1022. static endpoint_t *__endpoint_create(CEntityBase *pEntity, const endpoint_conf_t *conf,int nDev)
  1023. {
  1024. endpoint_t *ep;
  1025. ep = ZALLOC_T(endpoint_t);
  1026. su_home_init(ep->home);
  1027. memcpy(&ep->conf, conf, sizeof(endpoint_conf_t));
  1028. ep->media_port_seq = (conf->media_start_port + (SP::Module::Comm::RVCGetTickCount() % (conf->media_stop_port - conf->media_start_port))) & 0xfffc;
  1029. ep->entity = pEntity;
  1030. ep->call_seq = SP::Module::Comm::RVCGetTickCount();
  1031. ep->curr_audio_dev_type = nDev;
  1032. INIT_LIST_HEAD(&ep->call_list);
  1033. return ep;
  1034. }
  1035. static void __endpoint_destroy(endpoint_t *ep)
  1036. {
  1037. su_root_destroy(ep->root);
  1038. su_home_deinit(ep->home);
  1039. free(ep);
  1040. }
  1041. static int __endpoint_start(endpoint_t *ep)
  1042. {
  1043. #ifdef RVC_OS_WIN
  1044. ep->event_thread = (HANDLE)_beginthreadex(NULL, 0, &__event_thread, ep, 0, NULL);
  1045. if (ep->event_thread)
  1046. {
  1047. while (!ep->root)
  1048. {
  1049. DWORD dwRet = WaitForSingleObject(ep->event_thread, 1);
  1050. if (dwRet == WAIT_OBJECT_0)
  1051. {
  1052. CloseHandle(ep->event_thread);
  1053. ep->event_thread = NULL;
  1054. break;
  1055. }
  1056. }
  1057. }
  1058. return ep->event_thread ? 0 : -1;
  1059. #else
  1060. //pthread_attr_t attr;
  1061. //pthread_attr_init(&attr);
  1062. //pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  1063. int err = pthread_create(&ep->ievent_threadid, NULL, __event_thread, ep);
  1064. //pthread_attr_destroy(&attr);
  1065. if (0 == err) {
  1066. while (!ep->root) {
  1067. struct timespec ts;
  1068. clock_gettime(CLOCK_REALTIME, &ts);
  1069. long unsec = ts.tv_nsec + (1000 * 1000);
  1070. ts.tv_sec += (unsec / 1000000000);
  1071. ts.tv_nsec = (unsec % 1000000000);
  1072. if (0 == pthread_timedjoin_np(ep->ievent_threadid, NULL, &ts)) {
  1073. ep->ievent_threadid = 0;
  1074. break;
  1075. }
  1076. }
  1077. }
  1078. else {
  1079. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create event thread failed.");
  1080. }
  1081. return ep->ievent_threadid > 0 ? 0 : -1;
  1082. #endif
  1083. }
  1084. static void __endpoint_stop(endpoint_t *ep)
  1085. {
  1086. #ifdef RVC_OS_WIN
  1087. if (ep->event_thread)
  1088. {
  1089. int rc;
  1090. su_task_execute(su_root_task(ep->root), &__endpoint_break, ep, &rc);
  1091. WaitForSingleObject(ep->event_thread, INFINITE);
  1092. CloseHandle(ep->event_thread);
  1093. ep->event_thread = NULL;
  1094. }
  1095. #else
  1096. if (ep->ievent_threadid > 0)
  1097. {
  1098. int rc;
  1099. su_task_execute(su_root_task(ep->root), &__endpoint_break, ep, &rc);
  1100. if (0 == pthread_join(ep->ievent_threadid, NULL)) {
  1101. ep->ievent_threadid = 0;
  1102. }
  1103. else {
  1104. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("pthread join error for %s", strerror(errno));
  1105. }
  1106. }
  1107. #endif
  1108. }
  1109. static void rvc_sofia_logger(void* stream, char const* fmt, va_list ap) {
  1110. if (!fmt)
  1111. return;
  1112. char line[255];
  1113. vsnprintf(line, sizeof(line), fmt, ap);
  1114. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("rvc_sofia_logger : %s", line);
  1115. }
  1116. int endpoint_init_lib()
  1117. {
  1118. int result = su_init();
  1119. //su_log_set_level(NULL, 9);
  1120. //su_log_redirect(NULL, rvc_sofia_logger, NULL);
  1121. return result;
  1122. }
  1123. void endpoint_deinit_lib()
  1124. {
  1125. su_deinit();
  1126. }
  1127. endpoint_t *endpoint_create(CEntityBase *pEntity, const endpoint_conf_t *conf,int nDev)
  1128. {
  1129. endpoint_t *endpt;
  1130. endpt = __endpoint_create(pEntity, conf,nDev);
  1131. if (endpt) {
  1132. int rc = __endpoint_start(endpt);
  1133. if (rc != 0)
  1134. {
  1135. __endpoint_destroy(endpt);
  1136. endpt = NULL;
  1137. }
  1138. }
  1139. return endpt;
  1140. }
  1141. void endpoint_destroy(endpoint_t *ep)
  1142. {
  1143. if (ep)
  1144. {
  1145. __endpoint_stop(ep);
  1146. __endpoint_destroy(ep);
  1147. }
  1148. }
  1149. int endpoint_invoke(endpoint_t *ep, int (*func)(void*), void *user_data, int *result)
  1150. {
  1151. if (ep){
  1152. return su_task_execute(su_root_task(ep->root), func, user_data, result);
  1153. }
  1154. else
  1155. {
  1156. *result = Error_Param;
  1157. return -1;
  1158. }
  1159. }
  1160. void endpoint_change_audio_dev(endpoint_t *ep, int dev_type)
  1161. {
  1162. if (ep){
  1163. if (ep->curr_audio_dev_type != dev_type) {
  1164. ep->curr_audio_dev_type = dev_type;
  1165. if (ep->active_call) {
  1166. endpoint_media_change_audio_dev(ep->active_call, (e_dev_type)dev_type);
  1167. } else {
  1168. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("active call is null!");
  1169. }
  1170. }
  1171. }
  1172. }
  1173. endpoint_call_t *endpoint_call_create(endpoint_t *ep, const endpoint_call_params_t* pcallparam, const endpoint_call_callback_t *cb)
  1174. {
  1175. endpoint_call_t *call = NULL;
  1176. su_home_t *home = NULL;
  1177. if (!pcallparam || !pcallparam->to_uri){
  1178. return NULL;
  1179. }
  1180. home = su_home_create();
  1181. if (!home){
  1182. goto on_error;
  1183. }
  1184. call = (endpoint_call_t*)su_home_new(sizeof(endpoint_call_t));
  1185. if (!call){
  1186. goto on_error;
  1187. }
  1188. memset(call, 0, sizeof(endpoint_call_t));
  1189. call->connected = 0;
  1190. call->ep = ep;
  1191. call->home = home;
  1192. call->last_media_desc_hash = 0;
  1193. call->to_uri = su_strdup(home, pcallparam->to_uri);
  1194. if (pcallparam->from_uri != NULL){
  1195. call->from_uri = su_strdup(home, pcallparam->from_uri);
  1196. }
  1197. if (pcallparam->call_id != NULL){
  1198. call->call_id = su_strdup(home, pcallparam->call_id);
  1199. }
  1200. if (NULL != pcallparam->local_ip){
  1201. _snprintf(call->local_ip, RVC_MAX_IP_LEN, "%s", pcallparam->local_ip);
  1202. }
  1203. call->eDeviceType = pcallparam->nDeviceType;
  1204. call->nCallType = pcallparam->nCallType;
  1205. memcpy(&call->cb, cb, sizeof(endpoint_call_callback_t));
  1206. call->id = new_call_id(ep);
  1207. call->local_media_port = pcallparam->ilocal_audio_port;
  1208. call->local_video_port = pcallparam->ilocal_video_port;
  1209. REF_COUNT_INIT(&call->ref_cnt);
  1210. list_add_tail(&call->entry, &ep->call_list);
  1211. ep->active_call = call;
  1212. return call;
  1213. on_error:
  1214. if (home)
  1215. su_home_unref(home);
  1216. return call;
  1217. }
  1218. void endpoint_call_destroy(endpoint_call_t *call)
  1219. {
  1220. endpoint_call_dec_ref(call);
  1221. }
  1222. int endpoint_call_start(endpoint_call_t *call)
  1223. {
  1224. char sdp[512] = {0};
  1225. if (call->nh)
  1226. return -1;
  1227. if (call->from_uri != NULL && call->call_id != NULL){
  1228. call->nh = nua_handle(call->ep->nua,
  1229. call,
  1230. SIPTAG_CALL_ID_STR(call->call_id),
  1231. SIPTAG_FROM_STR(call->from_uri),
  1232. SIPTAG_TO_STR(call->to_uri),
  1233. TAG_END());
  1234. } else if (call->from_uri != NULL){
  1235. call->nh = nua_handle(call->ep->nua,
  1236. call,
  1237. SIPTAG_FROM_STR(call->from_uri),
  1238. SIPTAG_TO_STR(call->to_uri),
  1239. TAG_END());
  1240. }else if (call->call_id != NULL){
  1241. call->nh = nua_handle(call->ep->nua,
  1242. call,
  1243. SIPTAG_FROM_STR(call->ep->conf.uri),
  1244. SIPTAG_CALL_ID_STR(call->call_id),
  1245. SIPTAG_TO_STR(call->to_uri),
  1246. TAG_END());
  1247. }else{
  1248. call->nh = nua_handle(call->ep->nua,
  1249. call,
  1250. SIPTAG_FROM_STR(call->ep->conf.uri),
  1251. SIPTAG_TO_STR(call->to_uri),
  1252. TAG_END());
  1253. }
  1254. call_make_offer(call, sdp, -1);
  1255. if (0 == strlen(sdp)){
  1256. LogWarn(Severity_Middle, Error_Debug, EVENT_MOD_SIP_INVALID_SDP, "invalid sdp.");
  1257. }
  1258. nua_invite(call->nh,
  1259. //NUTAG_URL(call->remote_uri),
  1260. SOATAG_USER_SDP_STR(sdp),
  1261. NUTAG_SESSION_TIMER(20000),
  1262. //SOATAG_AUDIO_AUX("telephone-event"),
  1263. TAG_END());
  1264. return 0;
  1265. }
  1266. int endpoint_call_hangup(endpoint_call_t *call)
  1267. {
  1268. if (call->nh) {
  1269. if (call->connected) {
  1270. nua_bye(call->nh, NUTAG_SESSION_TIMER(8000),TAG_END());
  1271. } else {
  1272. nua_cancel(call->nh, NUTAG_SESSION_TIMER(8000),TAG_END());
  1273. }
  1274. }
  1275. return 0;
  1276. }
  1277. #ifdef RVC_OS_WIN
  1278. int endpoint_call_start_video(endpoint_call_t *call, unsigned long remote_ip, int remote_video_rtp,
  1279. unsigned long local_ip, int local_video_rtp,
  1280. int remote_width, int remote_height,
  1281. int local_view_x, int local_view_y, int local_view_cx, int local_view_cy,
  1282. int remote_view_x, int remote_view_y, int remote_view_cx, int remote_view_cy,
  1283. int local_move, int remote_move, video_session_callback_t* cb)
  1284. {
  1285. if (call)
  1286. {
  1287. int i = 0;
  1288. media_desc_t video_desc = {0};
  1289. video_desc.media_dir = DIR_BOTH;
  1290. if (call->nCallType == NORMAL_CALLTYPE || call->nCallType == DOUBLERECORD_CALLTYPE)//如果是可视柜台模式,使用协助通道的协商结果
  1291. {
  1292. video_desc.local_ip = local_ip;
  1293. video_desc.local_port = local_video_rtp;
  1294. video_desc.remote_port = remote_video_rtp;
  1295. video_desc.remote_ip = remote_ip;
  1296. }
  1297. else //如果是非可视柜台模式,使用SDP协商的参数
  1298. {
  1299. video_desc.local_ip = call->sdpvieo_desc.local_rtp_ip;
  1300. video_desc.local_port = call->sdpvieo_desc.local_rtp_port;
  1301. video_desc.local_pt = call->sdpvieo_desc.local_pt;
  1302. video_desc.remote_ip = call->sdpvieo_desc.remote_rtp_ip;
  1303. video_desc.remote_port = call->sdpvieo_desc.remote_rtp_port;
  1304. video_desc.remote_pt = call->sdpvieo_desc.remote_pt;
  1305. }
  1306. video_desc.param[i++] = local_view_x;
  1307. video_desc.param[i++] = local_view_y;
  1308. video_desc.param[i++] = local_view_cx;
  1309. video_desc.param[i++] = local_view_cy;
  1310. video_desc.param[i++] = remote_view_x;
  1311. video_desc.param[i++] = remote_view_y;
  1312. video_desc.param[i++] = remote_view_cx;
  1313. video_desc.param[i++] = remote_view_cy;
  1314. video_desc.param[i++] = remote_width;
  1315. video_desc.param[i++] = remote_height;
  1316. video_desc.param[i++] = local_move;
  1317. video_desc.param[i++] = remote_move;
  1318. endpoint_media_update_video(call, &video_desc, cb);
  1319. return 0;
  1320. }
  1321. else
  1322. {
  1323. return Error_Param;
  1324. }
  1325. }
  1326. #else
  1327. int endpoint_call_start_video(endpoint_call_t* call, endpoint_call_param_t* pcallparam, rvc_video_render_callback_t* render_cb)
  1328. {
  1329. if (call)
  1330. {
  1331. int i = 0;
  1332. media_desc_t video_desc = {0};
  1333. video_desc.media_dir = DIR_BOTH;
  1334. if (call->nCallType == NORMAL_CALLTYPE || call->nCallType == DOUBLERECORD_CALLTYPE)//如果是可视柜台模式,使用协助通道的协商结果
  1335. {
  1336. video_desc.local_ip = pcallparam->local_ip;
  1337. video_desc.local_port = pcallparam->local_port;
  1338. video_desc.remote_port = pcallparam->remote_port;
  1339. video_desc.remote_ip = pcallparam->remote_ip;
  1340. }
  1341. else //如果是非可视柜台模式,使用SDP协商的参数
  1342. {
  1343. video_desc.local_ip = call->sdpvieo_desc.local_rtp_ip;
  1344. video_desc.local_port = call->sdpvieo_desc.local_rtp_port;
  1345. video_desc.local_pt = call->sdpvieo_desc.local_pt;
  1346. video_desc.remote_ip = call->sdpvieo_desc.remote_rtp_ip;
  1347. video_desc.remote_port = call->sdpvieo_desc.remote_rtp_port;
  1348. video_desc.remote_pt = call->sdpvieo_desc.remote_pt;
  1349. }
  1350. video_desc.param[i++] = pcallparam->remote_width;
  1351. video_desc.param[i++] = pcallparam->remote_height;
  1352. endpoint_media_update_video(call, &video_desc, render_cb);
  1353. return 0;
  1354. }
  1355. else
  1356. {
  1357. return Error_Param;
  1358. }
  1359. }
  1360. #endif
  1361. int endpoint_call_stop_video(endpoint_call_t *call)
  1362. {
  1363. if (call) {
  1364. media_desc_t video_desc = {0};
  1365. video_desc.media_dir = DIR_NONE;
  1366. endpoint_media_update_video(call, &video_desc,NULL);
  1367. return 0;
  1368. } else {
  1369. return Error_Param;
  1370. }
  1371. }
  1372. #ifdef RVC_OS_WIN
  1373. int endpoint_call_stop_double_record_broadcast_video()
  1374. {
  1375. double_record_broadcast_video_session_stop();
  1376. return Error_Succeed;
  1377. }
  1378. int local_remote_show_video(endpoint_call_t *call,int local_view_x, int local_view_y, int local_view_cx, int local_view_cy,int remote_view_x, int remote_view_y, int remote_view_cx, int remote_view_cy, int local_move, int remote_move, video_session_callback_t* cb)
  1379. {
  1380. int rc;
  1381. endpoint_conf_t *ep_conf = &call->ep->conf;
  1382. if (call->video)
  1383. {
  1384. video_session_destroy(call->video);
  1385. call->video = NULL;
  1386. }
  1387. int remote_video_width = REC_COMMON_VIDEO_SSM_AGENT_WIDTH;
  1388. int remote_video_height = REC_COMMON_VIDEO_SSM_AGENT_HEIGHT;
  1389. if (eStand2sType == call->eDeviceType){
  1390. remote_video_width = REC_COMMON_VIDEO_DSM_AGENT_WIDTH;
  1391. remote_video_height = REC_COMMON_VIDEO_DSM_AGENT_HEIGHT;
  1392. }
  1393. video_session_conf_t video_conf = {0};
  1394. video_conf.bit_rate = 256 * 1024;
  1395. video_conf.local_rtp_ip = 0;
  1396. video_conf.local_rtp_port = 0;
  1397. video_conf.local_video_view_x = local_view_x;
  1398. video_conf.local_video_view_y = local_view_y;
  1399. video_conf.local_video_view_cx = local_view_cx;
  1400. video_conf.local_video_view_cy = local_view_cy;
  1401. video_conf.local_move = local_move;
  1402. video_conf.mtu = ep_conf->mtu;
  1403. video_conf.video_quant = ep_conf->quant;
  1404. video_conf.remote_rtp_ip = 0;
  1405. video_conf.remote_rtp_port = 0;
  1406. video_conf.remote_video_view_x = remote_view_x;
  1407. video_conf.remote_video_view_y = remote_view_y;
  1408. video_conf.remote_video_view_cx = remote_view_cx;
  1409. video_conf.remote_video_view_cy = remote_view_cy;
  1410. video_conf.remote_move = remote_move;
  1411. video_conf.remote_video_width = remote_video_width;
  1412. video_conf.remote_video_height = remote_video_height;
  1413. video_conf.ref_active_camera = call->ep->conf.ref_active_camera;
  1414. video_conf.ref_camera_switch = call->ep->conf.ref_camera_switch;
  1415. video_conf.ref_camera_state = call->ep->conf.ref_camera_state;
  1416. video_conf.ref_window_state = call->ep->conf.ref_window_state;
  1417. video_conf.ref_active_img = call->ep->conf.ref_active_img;
  1418. video_conf.ref_Is_ActiveInspect = call->ep->conf.ref_Is_ActiveInspect;
  1419. video_conf.ref_Is_showPersonArea = call->ep->conf.ref_Is_showPersonArea;
  1420. video_conf.camera_count = call->ep->conf.camera_count;
  1421. video_conf.screen_count = call->ep->conf.screen_count;
  1422. video_conf.eDeviceType = call->eDeviceType;
  1423. video_conf.video_echo_cb = cb;
  1424. video_conf.ilocal_wind_flags = VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP;
  1425. video_conf.iremote_wind_flags = VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP;
  1426. video_conf.eType = (eVideoRenderType)call->ep->conf.irendertype;
  1427. rc = local_video_session_create(&video_conf, &call->video, true);
  1428. if (rc != 0){
  1429. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create video session failed! rc = %d", rc);
  1430. return -1;
  1431. }
  1432. rc = video_session_start(call->video);
  1433. if (rc != 0){
  1434. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start video session failed! rc = %d", rc);
  1435. video_session_destroy(call->video);
  1436. call->video = NULL;
  1437. return -1;
  1438. }
  1439. return 0;
  1440. }
  1441. int local_play_stop_video(endpoint_call_t *call)
  1442. {
  1443. if (call) {
  1444. media_desc_t video_desc = {0};
  1445. video_desc.media_dir = DIR_NONE;
  1446. endpoint_media_update_video(call, &video_desc,NULL);
  1447. return 0;
  1448. } else
  1449. {
  1450. return Error_Param;
  1451. }
  1452. }
  1453. #endif
  1454. static void __endpoint_call_destroy(endpoint_call_t *call)
  1455. {
  1456. list_del(&call->entry);
  1457. if (call->ep && call->ep->active_call == call) {
  1458. call->ep->active_call = NULL;
  1459. }
  1460. su_home_unref(call->home);
  1461. }
  1462. IMPLEMENT_REF_COUNT_MT_STATIC(endpoint_call, endpoint_call_t, ref_cnt, __endpoint_call_destroy)