endpoint.cpp 47 KB

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