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