video_session.cpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  1. #include "stdafx.h"
  2. #include <io.h>
  3. #include "SpBase.h"
  4. #include "video_session.h"
  5. #include "videoframework.h"
  6. #include "memutil.h"
  7. #include <ObjBase.h>
  8. #include "videohorflip.h"
  9. #include "opencv2/opencv.hpp"
  10. #include "highgui.h"
  11. #include "y2k_time.h"
  12. #include "Event.h"
  13. #ifndef av_always_inline
  14. #define av_always_inline __inline
  15. #endif
  16. #ifndef inline
  17. #define inline __inline
  18. #endif
  19. #ifndef INT64_C
  20. #define INT64_C(c) (c##LL)
  21. #define UINT64_C(c) (c##UL)
  22. #endif
  23. extern "C"{
  24. #include <libavutil\avutil.h>
  25. #include <libavcodec\avcodec.h>
  26. #include <libswscale\swscale.h>
  27. }
  28. #include "video_common/ffmpeg_api_cpp_adapter.h"
  29. #include <DbgHelp.h>
  30. #pragma comment(lib, "dbghelp.lib")
  31. BOOL g_bIsSalesRecord = FALSE;
  32. HWND pg_local_hwnd = NULL; // preview window
  33. HWND pg_remote_hwnd = NULL; // remote window
  34. void* pg_local_render = NULL; // local render
  35. void* pg_remote_render = NULL; // remote render
  36. video_session_t* pg_last_session = NULL; // last used session
  37. #define WNDCLS_NAME "sipmedia_video"
  38. #ifndef MAX_PATH
  39. #define MAX_PATH 260
  40. #endif
  41. #define DEVICE_STANDARD_TYPE 0
  42. #define DEVICE_PAD_TYPE 1
  43. #define DEVICE_MOBILEPAD_TYPE 2
  44. static void __dbg(void *user_data, const char *fmt, va_list arg)
  45. {
  46. int n = _vscprintf(fmt, arg);
  47. if (n >= MAX_LOG_LEN) {
  48. char* buf = (char*)malloc((size_t)(n + 1));
  49. _vsnprintf(buf, n + 1, fmt, arg);
  50. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  51. free(buf);
  52. }
  53. else{
  54. char strlog[MAX_LOG_LEN] = {0};
  55. _vsnprintf(strlog, MAX_LOG_LEN, fmt, arg);
  56. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  57. }
  58. }
  59. static void __logevent(void* user_data, int itype, const char* strmessage)
  60. {
  61. if (0 == itype) {
  62. LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_VIDEO_RTP_CREATE, strmessage);
  63. }
  64. else {
  65. LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_VIDEO_RTP_DESTROY, strmessage);
  66. }
  67. }
  68. static bool __camera_error_event(bool bhaspost, int icameraid)
  69. {
  70. bool bret = false;
  71. if (false == bhaspost){
  72. if (0 == icameraid){
  73. LogWarn(Severity_Middle, Error_Debug, ERROR_MOD_SIP_GET_ENV_VIDEO_FAILED, "get video from env queue failed!");
  74. }
  75. else{
  76. LogWarn(Severity_Middle, Error_Debug, ERROR_MOD_SIP_GET_OPT_VIDEO_FAILED, "get video from opt queue failed!");
  77. }
  78. LogEvent(Severity_Middle, EVENT_MOD_SIP_GET_VIDEO_FAILED, "connected and get video failed!");
  79. bret = true;
  80. }
  81. return bret;
  82. }
  83. static int translate_image_resolution(video_frame** dstframe,const int iwidth, const int iheight, const video_frame* psrcframe)
  84. {
  85. int i = -1;
  86. if (iwidth == psrcframe->width && iheight == psrcframe->height){
  87. return i;
  88. }
  89. video_frame* pframe = video_frame_new(iwidth, iheight,VIDEO_FORMAT_RGB24);
  90. video_frame_fill_black(pframe);
  91. SwsContext*sws = sws_getCachedContext(NULL, psrcframe->width, psrcframe->height,
  92. PIX_FMT_BGR24,
  93. iwidth,
  94. iheight,
  95. PIX_FMT_BGR24,
  96. SWS_LANCZOS,
  97. NULL,
  98. NULL,
  99. NULL);
  100. sws_scale(sws, psrcframe->data, psrcframe->linesize, 0, psrcframe->height, pframe->data, pframe->linesize);
  101. sws_freeContext(sws);
  102. *dstframe = pframe;
  103. i = 0;
  104. return i;
  105. }
  106. static int calc_capture_mode(int width, int height, int *mode)
  107. {
  108. const struct {
  109. int mode;
  110. int width;
  111. int height;
  112. } modes [] = {
  113. {VIDEOCAP_FRAME_SQCIF, VIDEOCAP_SQCIF_WIDTH, VIDEOCAP_SQCIF_HEIGHT},
  114. {VIDEOCAP_FRAME_QQVGA, VIDEOCAP_QQVGA_WIDTH, VIDEOCAP_QQVGA_HEIGHT},
  115. {VIDEOCAP_FRAME_QCIF, VIDEOCAP_QCIF_WIDTH, VIDEOCAP_QCIF_HEIGHT},
  116. {VIDEOCAP_FRAME_QVGA, VIDEOCAP_QVGA_WIDTH, VIDEOCAP_QVGA_HEIGHT},
  117. {VIDEOCAP_FRAME_CIF, VIDEOCAP_CIF_WIDTH, VIDEOCAP_CIF_HEIGHT},
  118. {VIDEOCAP_FRAME_VGA, VIDEOCAP_VGA_WIDTH, VIDEOCAP_VGA_HEIGHT},
  119. {VIDEOCAP_FRAME_4CIF, VIDEOCAP_4CIF_WIDTH, VIDEOCAP_4CIF_HEIGHT},
  120. {VIDEOCAP_FRAME_SVGA, VIDEOCAP_SVGA_WIDTH, VIDEOCAP_SVGA_HEIGHT},
  121. {VIDEOCAP_FRAME_NHD, VIDEOCAP_NHD_WIDTH, VIDEOCAP_NHD_HEIGHT},
  122. {VIDEOCAP_FRAME_SXGA, VIDEOCAP_SXGA_WIDTH, VIDEOCAP_SXGA_HEIGHT},
  123. {VIDEOCAP_FRAME_720P, VIDEOCAP_720P_WIDTH, VIDEOCAP_720P_HEIGHT},
  124. {VIDEOCAP_FRAME_1080P, VIDEOCAP_1080P_WIDTH, VIDEOCAP_1080P_HEIGHT},
  125. };
  126. int i;
  127. for (i = 0; i < array_size(modes); ++i) {
  128. if (modes[i].width == width && modes[i].height == height) {
  129. *mode = modes[i].mode;
  130. return 0;
  131. }
  132. }
  133. return Error_NotExist;
  134. }
  135. static void __delete_frame(videoplayer_t *player, void *user_data, video_frame *frame)
  136. {
  137. video_frame_delete(frame);
  138. }
  139. static int video_shm_enqueue(Clibvideoqueue *shm_queue, video_frame *frame, int flags, int iframeid)
  140. {
  141. videoq_frame tmp_frm;
  142. tmp_frm.data = frame->data[0];
  143. tmp_frm.framesize = frame->width * frame->height * 3;
  144. tmp_frm.format = VIDEOQ_FORMAT_RGB24;
  145. tmp_frm.width = frame->width;
  146. tmp_frm.height = frame->height;
  147. tmp_frm.iframeid = iframeid;
  148. unsigned int nowtime = y2k_time_now();
  149. if (!shm_queue->InsertVideo(&tmp_frm, flags,nowtime))
  150. {
  151. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("caution: insert shm video failed!");
  152. return Error_Unexpect;
  153. }
  154. else
  155. {
  156. return Error_Succeed;
  157. }
  158. }
  159. static inline Clibvideoqueue *get_active_videoqueue(video_session_t *session)
  160. {
  161. int agent_option = *session->conf.ref_camera_switch;
  162. int facetracking_option = *session->conf.ref_active_camera;
  163. int camera_state = *session->conf.ref_camera_state;
  164. Clibvideoqueue *active = NULL;
  165. if (agent_option == CAMERA_TYPE_ENV)
  166. {
  167. if((camera_state!=CAMERA_TYPE_ENV)&&(camera_state!=CAMERA_TYPE_AUTO))
  168. {
  169. active = NULL;
  170. }
  171. else
  172. {
  173. active = session->video_shm_q_env;
  174. }
  175. }
  176. else if (agent_option == CAMERA_TYPE_OPT)
  177. {
  178. if((camera_state!=CAMERA_TYPE_OPT)&&(camera_state!=CAMERA_TYPE_AUTO))
  179. {
  180. active = NULL;
  181. }
  182. else
  183. {
  184. active = session->video_shm_q_opt;
  185. }
  186. }
  187. else if(agent_option == CAMERA_TYPE_ERROR)
  188. {
  189. active = NULL;
  190. }
  191. else
  192. { // auto
  193. if (camera_state == CAMERA_TYPE_AUTO)
  194. {
  195. if (facetracking_option == CAMERA_TYPE_ENV)
  196. {
  197. active = session->video_shm_q_env;
  198. }
  199. else
  200. { // opt
  201. active = session->video_shm_q_opt;
  202. }
  203. }
  204. else if (camera_state == CAMERA_TYPE_ENV)
  205. {
  206. active = session->video_shm_q_env;
  207. }
  208. else if (camera_state == CAMERA_TYPE_OPT)
  209. {
  210. active = session->video_shm_q_opt;
  211. }
  212. else if (camera_state == CAMERA_TYPE_ERROR)
  213. {
  214. active = NULL;
  215. }
  216. }
  217. return active;
  218. }
  219. static void local_get_frame(void *user_data, video_frame *frame)
  220. {
  221. video_session_t *session = (video_session_t*)user_data;
  222. Clibvideoqueue *q = get_active_videoqueue(session);
  223. if (q)
  224. {
  225. videoq_frame frm;
  226. frm.data = frame->data[0];
  227. if (q->GetVideo(&frm, 0))
  228. {
  229. //video_frame_fill_black(frame);
  230. frame->width = frm.width;
  231. frame->height = frm.height;
  232. frame->linesize[0] = frm.width * 3;
  233. }
  234. }
  235. else
  236. {
  237. //video_frame_fill_black(frame);
  238. //贴图
  239. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("get active cam is null, load error img.");
  240. if (session->video_error != NULL)
  241. {
  242. frame->width = session->video_error->width;
  243. frame->height = session->video_error->height;
  244. frame->linesize[0] = session->video_error->width * 3;
  245. memcpy(frame->data[0],session->video_error->data,frame->width*frame->height*3);
  246. }
  247. else
  248. {
  249. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("video error img is null");
  250. }
  251. }
  252. }
  253. static void local_put_frame(void *user_data, video_frame *frame)
  254. {
  255. video_session_t *session = (video_session_t*)user_data;
  256. video_frame *tmp_frame_encode = NULL;
  257. int rc;
  258. // send out
  259. {
  260. tmp_frame_encode = video_frame_new(REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_I420);
  261. video_frame_fill_black(tmp_frame_encode);
  262. if (frame->width == REC_COMMON_VIDEO_RTP_ENV_WIDTH && frame->height == REC_COMMON_VIDEO_RTP_ENV_HEIGHT) { // env
  263. int offset = (REC_COMMON_VIDEO_RTP_HEIGHT - REC_COMMON_VIDEO_RTP_ENV_HEIGHT) / 2;
  264. unsigned char *dst_data[4] = {tmp_frame_encode->data[0], tmp_frame_encode->data[1], tmp_frame_encode->data[2], NULL};
  265. dst_data[0] += offset * tmp_frame_encode->linesize[0];
  266. dst_data[1] += offset / 2 * tmp_frame_encode->linesize[1];
  267. dst_data[2] += offset / 2 * tmp_frame_encode->linesize[2];
  268. sws_scale(session->local_encode_sws_ctx_env, frame->data, frame->linesize, 0, frame->height, dst_data, tmp_frame_encode->linesize);
  269. }
  270. else if (frame->width == REC_COMMON_VIDEO_RTP_OPT_WIDTH && frame->height == REC_COMMON_VIDEO_RTP_OPT_HEIGHT) { // opt
  271. video_frame tt;
  272. video_frame_alloc(REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_RGB24, &tt);
  273. video_frame_fill_black(&tt);
  274. {
  275. int offset = (REC_COMMON_VIDEO_RTP_WIDTH - REC_COMMON_VIDEO_RTP_OPT_WIDTH) / 2;
  276. unsigned char *dst_data[4] = {tt.data[0], 0, 0, 0};
  277. dst_data[0] += offset * 3;
  278. IppiSize size;
  279. size.width = frame->width;
  280. size.height = frame->height;
  281. ippiCopy_8u_C3R(frame->data[0], frame->linesize[0], tt.data[0] + offset * 3, tt.linesize[0], size);
  282. }
  283. sws_scale(session->local_encode_sws_ctx_opt, tt.data, tt.linesize, 0, tt.height, tmp_frame_encode->data, tmp_frame_encode->linesize);
  284. video_frame_free(&tt);
  285. }
  286. if (session->conf.local_pt == REC_COMMON_VIDEO_PT)
  287. {
  288. videortp_send_frame(session->rtp, tmp_frame_encode);
  289. }
  290. else
  291. {
  292. videortp_send_yuvframe(session->rtp, tmp_frame_encode);
  293. }
  294. }
  295. on_error:
  296. if (tmp_frame_encode) {
  297. video_frame_delete(tmp_frame_encode);
  298. }
  299. }
  300. static int on_rx_frame(video_frame *frame, void *user_data)
  301. {
  302. video_session_t *session = (video_session_t *)user_data;
  303. int used = 0;
  304. session->irecv_frameid++;
  305. if (eGDI == session->conf.eType)
  306. {
  307. if (session->remote_player) {
  308. if(DOUBLERECORD_CALLTYPE == session->conf.nCallType){
  309. if (eStand2sType == session->conf.eDeviceType){
  310. //大机对远端视频进行缩放匹配
  311. video_frame* recordframe = NULL;
  312. if(0 == translate_image_resolution(&recordframe,/*REC_COMMON_VIDEO_SSM_AGENT_WIDTH*/REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH,/*REC_COMMON_VIDEO_SSM_AGENT_HEIGHT*/REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_HEIGHT,frame)){
  313. int rc = video_shm_enqueue(session->video_shm_q_remote, recordframe, VIDEOQUEUE_FLAG_VERTICAL_FLIP, session->irecv_frameid);
  314. if (rc != Error_Succeed) {
  315. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("eStand2sType record call mod insert remote video to queue failed!");
  316. }
  317. video_frame_delete(recordframe);
  318. }
  319. recordframe = NULL;
  320. }
  321. else{
  322. int rc = video_shm_enqueue(session->video_shm_q_remote, frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP, session->irecv_frameid);
  323. if (rc != Error_Succeed) {
  324. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("not eStand2sType record call mod insert remote video to queue failed!");
  325. }
  326. }
  327. }
  328. int rc = videoplayer_queue_frame(session->remote_player, frame, &__delete_frame, NULL);
  329. if (rc == 0) {
  330. used = TRUE;
  331. }
  332. }
  333. }
  334. else{
  335. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType) {
  336. if (eStand2sType == session->conf.eDeviceType) {
  337. video_frame* recordframe = NULL;
  338. if (0 == translate_image_resolution(&recordframe, REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_HEIGHT, frame)) {
  339. int rc = video_shm_enqueue(session->video_shm_q_remote, recordframe, VIDEOQUEUE_FLAG_VERTICAL_FLIP, session->irecv_frameid);
  340. if (rc != Error_Succeed) {
  341. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("eStand2sType record call mod insert remote video to queue failed.");
  342. }
  343. video_frame_delete(recordframe);
  344. recordframe = NULL;
  345. }
  346. }
  347. else {
  348. int rc = video_shm_enqueue(session->video_shm_q_remote, frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP, session->irecv_frameid);
  349. if (rc != Error_Succeed) {
  350. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("not eStand2sType record call mod insert remote video to queue failed.");
  351. }
  352. }
  353. }
  354. if (NULL != session->premote_render){
  355. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("session plocal_render RenderVideoFrame");
  356. video_frame* echoframe = NULL;
  357. if (0 == translate_image_resolution(&echoframe,session->conf.remote_video_view_cx,session->conf.remote_video_view_cy, frame)) {
  358. session->premote_render->RenderVideoFrame(echoframe, RVC_FLIP_VERTICAL);
  359. session->bshow_remote = true;
  360. video_frame_delete(echoframe);
  361. echoframe = NULL;
  362. }
  363. else {
  364. session->premote_render->RenderVideoFrame(frame, RVC_FLIP_VERTICAL);
  365. session->bshow_remote = true;
  366. }
  367. used = 1;
  368. }
  369. }
  370. return used;
  371. }
  372. int GetCurrentRunPath(char* pPath)
  373. {
  374. char* pBuf = new char[MAX_PATH];
  375. if (pBuf == NULL)
  376. return -1;
  377. ZeroMemory(pBuf, MAX_PATH);
  378. GetModuleFileName(NULL, pBuf, MAX_PATH);
  379. int len = strnlen_s(pBuf, MAX_PATH);
  380. if (len <= 0)
  381. {
  382. delete[]pBuf;
  383. return -2;
  384. }
  385. char* pch;
  386. pch = strstr(pBuf, "bin");
  387. if (pch == NULL)
  388. return -3;
  389. int lenDel = strnlen_s(pch, MAX_PATH);
  390. if (len <= 0)
  391. {
  392. delete[]pBuf;
  393. return -3;
  394. }
  395. strncpy_s(pPath, MAX_PATH, pBuf, len - lenDel);
  396. delete[]pBuf;
  397. return strnlen_s(pPath, MAX_PATH);
  398. }
  399. //远端视频窗口状态回调
  400. static int on_remoteWinstate(videoplayer_t *player, void *user_data, video_frame **frame)
  401. {
  402. video_session_t *session = (video_session_t*)user_data;
  403. return *session->conf.ref_window_state ;
  404. }
  405. //本地回显回调
  406. static int on_pull(videoplayer_t *player, void *user_data, video_frame **frame)
  407. {
  408. video_session_t *session = (video_session_t*)user_data;
  409. video_frame *tmp_frame_preview;
  410. int iflags = VIDEOQUEUE_FLAG_HORIZONTAL_FLIP;
  411. if (session->conf.ivideotype){
  412. iflags = 0;
  413. }
  414. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("ref_active_img %d,ref_Is_ActiveInspect %d,ref_camera_switch %d",*session->conf.ref_active_img,*session->conf.ref_Is_ActiveInspect,*session->conf.ref_camera_switch);
  415. if((session->conf.ref_active_img == NULL)&&(session->conf.ref_Is_ActiveInspect == NULL))
  416. {
  417. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24);
  418. videoq_frame frm;
  419. frm.data = tmp_frame_preview->data[0];
  420. BOOL result = session->video_shm_q_preview->GetVideo(&frm, iflags);
  421. //人形框融合处理
  422. if (result&&session->personimage!=NULL&&session->personmask!=NULL&&*session->conf.ref_Is_showPersonArea==1)
  423. {
  424. IplImage*img = cvCreateImageHeader(cvSize(frm.width,frm.height),IPL_DEPTH_8U,3);
  425. img->imageData = (char*)frm.data;
  426. if (frm.width!=session->personimage->width)
  427. {
  428. IplImage*tmp = cvCreateImage(cvSize(frm.width,frm.height),IPL_DEPTH_8U,3);
  429. IplImage*tmpmask = cvCreateImage(cvSize(frm.width,frm.height),IPL_DEPTH_8U,1);
  430. cvResize(session->personimage,tmp);
  431. cvResize(session->personmask,tmpmask);
  432. cvAdd(img,tmp,img,tmpmask);
  433. cvReleaseImage(&tmp);
  434. cvReleaseImage(&tmpmask);
  435. }
  436. else
  437. {
  438. cvAdd(img,session->personimage,img,session->personmask);
  439. }
  440. cvReleaseImageHeader(&img);
  441. }
  442. if (!result){
  443. if (__camera_error_event(session->bcamera_error_posted, 0)){
  444. session->bcamera_error_posted = true;
  445. }
  446. }
  447. }
  448. else
  449. {
  450. if (*session->conf.ref_active_img == 1)
  451. {
  452. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_WIDTH, VIDEO_FORMAT_RGB24);
  453. video_frame_fill_black(tmp_frame_preview);
  454. if (session->video_error == NULL)
  455. {
  456. char strPath[MAX_PATH] = {0};
  457. GetCurrentRunPath(strPath);
  458. sprintf(strPath, "%s\\bin\\looklowerscreen.jpg", strPath);
  459. if (_access(strPath,0)!=-1)
  460. {
  461. IplImage*img = cvLoadImage(strPath,1);
  462. if (img != NULL)
  463. {
  464. session->video_error = new videoq_frame;
  465. session->video_error->format = VIDEOQ_FORMAT_RGB24;
  466. session->video_error->framesize = img->imageSize;
  467. session->video_error->height = img->width;
  468. session->video_error->width = img->height;
  469. session->video_error->data = new unsigned char[img->imageSize];
  470. memcpy(session->video_error->data,img->imageData,img->imageSize);
  471. cvReleaseImage(&img);
  472. }
  473. }
  474. }
  475. if (session->video_error != NULL)
  476. {
  477. SwsContext*sws = sws_getContext(session->video_error->width, session->video_error->height,PIX_FMT_BGR24,
  478. REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  479. REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  480. PIX_FMT_BGR24,
  481. SWS_POINT, NULL, NULL, NULL);
  482. uint8_t *src_data[4] = {(unsigned char*)session->video_error->data+(session->video_error->height-1)*session->video_error->width*3,NULL,NULL,NULL};
  483. int src_linesize[4] = {-session->video_error->width*3,0,0,0};
  484. unsigned char *dst[4] = {tmp_frame_preview->data[0],NULL,NULL,NULL};
  485. int dst_linesize[4] = {tmp_frame_preview->linesize[0],0,0,0};
  486. sws_scale(sws, src_data, src_linesize, 0, session->video_error->height, dst, dst_linesize);
  487. sws_freeContext(sws);
  488. }
  489. }
  490. else if (*session->conf.ref_Is_ActiveInspect == 1)
  491. {
  492. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_WIDTH, VIDEO_FORMAT_RGB24);
  493. video_frame_fill_black(tmp_frame_preview);
  494. if ((*session->conf.ref_camera_switch == CAMERA_TYPE_ENV)||(*session->conf.ref_camera_switch == CAMERA_TYPE_AUTO))//显示上摄像头
  495. {
  496. if (session->video_shm_q_env)
  497. {
  498. videoq_frame frm;
  499. int offset = (REC_COMMON_VIDEO_SNAPSHOT_WIDTH - REC_COMMON_VIDEO_SNAPSHOT_HEIGHT) / 2;
  500. frm.data = tmp_frame_preview->data[0] + offset * tmp_frame_preview->linesize[0];
  501. if (FALSE == session->video_shm_q_env->GetVideo(&frm, VIDEOQUEUE_FLAG_VERTICAL_FLIP)){
  502. session->bcamera_error_posted = true;
  503. }
  504. }
  505. }
  506. else if (*session->conf.ref_camera_switch == CAMERA_TYPE_OPT)//显示下摄像头
  507. {
  508. if (session->video_shm_q_opt)
  509. {
  510. video_frame tt = {0};
  511. int offset = (REC_COMMON_VIDEO_SNAPSHOT_WIDTH-REC_COMMON_VIDEO_SNAPSHOT_HEIGHT) / 2;
  512. tt.data[0] = tmp_frame_preview->data[0] + offset *3;
  513. tt.format = tmp_frame_preview->format;
  514. tt.linesize[0] = tmp_frame_preview->linesize[0];
  515. tt.width = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
  516. tt.height = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
  517. if (FALSE == session->video_shm_q_opt->GetVideo2(&tt, VIDEOQUEUE_FLAG_VERTICAL_FLIP)){
  518. if (__camera_error_event(session->bcamera_error_posted, 1)){
  519. session->bcamera_error_posted = true;
  520. }
  521. }
  522. }
  523. }
  524. }
  525. else
  526. {
  527. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24);
  528. videoq_frame frm;
  529. frm.data = tmp_frame_preview->data[0];
  530. BOOL result = session->video_shm_q_preview->GetVideo(&frm, iflags);
  531. //人形框融合处理
  532. if (result&&session->personimage!=NULL&&session->personmask!=NULL&&*session->conf.ref_Is_showPersonArea==1)
  533. {
  534. IplImage*img = cvCreateImageHeader(cvSize(frm.width,frm.height),IPL_DEPTH_8U,3);
  535. img->imageData = (char*)frm.data;
  536. if (frm.width!=session->personimage->width)
  537. {
  538. IplImage*tmp = cvCreateImage(cvSize(frm.width,frm.height),IPL_DEPTH_8U,3);
  539. IplImage*tmpmask = cvCreateImage(cvSize(frm.width,frm.height),IPL_DEPTH_8U,1);
  540. cvResize(session->personimage,tmp);
  541. cvResize(session->personmask,tmpmask);
  542. cvAdd(img,tmp,img,tmpmask);
  543. cvReleaseImage(&tmp);
  544. cvReleaseImage(&tmpmask);
  545. }
  546. else
  547. {
  548. cvAdd(img,session->personimage,img,session->personmask);
  549. }
  550. cvReleaseImageHeader(&img);
  551. }
  552. if (!result){
  553. if (__camera_error_event(session->bcamera_error_posted, 0)){
  554. session->bcamera_error_posted = true;
  555. }
  556. }
  557. }
  558. }
  559. *frame = tmp_frame_preview;
  560. return *session->conf.ref_window_state;
  561. }
  562. static void free_frame(videoplayer_t *player, void *user_data, video_frame *frame)
  563. {
  564. video_frame_delete(frame);
  565. }
  566. //视频窗口显示状态,0:正常大小显示,1:放大一倍显示,2:隐藏全部窗口,3:从隐藏状态恢复为全部显示,4:显示远程窗口,隐藏本地窗口, 5:缩放显示
  567. int set_video_windows(video_session_t* psession, int iwindowstate)
  568. {
  569. int iret = -1;
  570. if (NULL == psession){
  571. return iret;
  572. }
  573. if (2 == iwindowstate){
  574. psession->plocal_render->HideVideoWindow();
  575. psession->premote_render->HideVideoWindow();
  576. iret = 0;
  577. }
  578. else if (3 == iwindowstate){
  579. psession->plocal_render->ShowVideoWindow();
  580. psession->premote_render->HideVideoWindow();
  581. psession->premote_render->ShowVideoWindow();
  582. iret = 0;
  583. }
  584. else if (4 == iwindowstate) {
  585. psession->plocal_render->HideVideoWindow();
  586. iret = 0;
  587. }
  588. return iret;
  589. }
  590. //本地回显回调
  591. static int get_local_video_frame(void* user_data, video_frame** frame)
  592. {
  593. video_session_t* session = (video_session_t*)user_data;
  594. video_frame* tmp_frame_preview = NULL;
  595. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("ref_active_img %d,ref_Is_ActiveInspect %d,ref_camera_switch %d",*session->conf.ref_active_img,*session->conf.ref_Is_ActiveInspect,*session->conf.ref_camera_switch);
  596. if ((session->conf.ref_active_img == NULL) && (session->conf.ref_Is_ActiveInspect == NULL))
  597. {
  598. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24);
  599. videoq_frame frm;
  600. frm.data = tmp_frame_preview->data[0];
  601. BOOL result = session->video_shm_q_preview->GetVideo(&frm, VIDEOQUEUE_FLAG_HORIZONTAL_FLIP);
  602. //人形框融合处理
  603. if (result){
  604. if (session->personimage != NULL && session->personmask != NULL && *session->conf.ref_Is_showPersonArea == 1)
  605. {
  606. IplImage* img = cvCreateImageHeader(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 3);
  607. img->imageData = (char*)frm.data;
  608. if (frm.width != session->personimage->width){
  609. IplImage* tmp = cvCreateImage(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 3);
  610. IplImage* tmpmask = cvCreateImage(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 1);
  611. cvResize(session->personimage, tmp);
  612. cvResize(session->personmask, tmpmask);
  613. cvAdd(img, tmp, img, tmpmask);
  614. cvReleaseImage(&tmp);
  615. cvReleaseImage(&tmpmask);
  616. }
  617. else{
  618. cvAdd(img, session->personimage, img, session->personmask);
  619. }
  620. cvReleaseImageHeader(&img);
  621. }
  622. }
  623. else {
  624. video_frame_fill_black(tmp_frame_preview);
  625. }
  626. }
  627. else
  628. {
  629. if (*session->conf.ref_active_img == 1)
  630. {
  631. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_WIDTH, VIDEO_FORMAT_RGB24);
  632. video_frame_fill_black(tmp_frame_preview);
  633. if (session->video_error == NULL)
  634. {
  635. char strPath[MAX_PATH] = { 0 };
  636. _snprintf(strPath, MAX_PATH, "%s", "./bin/looklowerscreen.jpg");
  637. if (-1 != _access(strPath,0))
  638. {
  639. IplImage* img = cvLoadImage(strPath, 1);
  640. if (img != NULL)
  641. {
  642. session->video_error = new videoq_frame;
  643. session->video_error->format = VIDEOQ_FORMAT_RGB24;
  644. session->video_error->framesize = img->imageSize;
  645. session->video_error->height = img->width;
  646. session->video_error->width = img->height;
  647. session->video_error->data = new unsigned char[img->imageSize];
  648. memcpy(session->video_error->data, img->imageData, img->imageSize);
  649. cvReleaseImage(&img);
  650. }
  651. }
  652. }
  653. if (session->video_error != NULL)
  654. {
  655. SwsContext* sws = sws_getContext(session->video_error->width, session->video_error->height, PIX_FMT_BGR24,
  656. REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  657. REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  658. PIX_FMT_BGR24,
  659. SWS_POINT, NULL, NULL, NULL);
  660. uint8_t* src_data[4] = { (unsigned char*)session->video_error->data + (session->video_error->height - 1) * session->video_error->width * 3,NULL,NULL,NULL };
  661. int src_linesize[4] = { -session->video_error->width * 3,0,0,0 };
  662. unsigned char* dst[4] = { tmp_frame_preview->data[0],NULL,NULL,NULL };
  663. int dst_linesize[4] = { tmp_frame_preview->linesize[0],0,0,0 };
  664. sws_scale(sws, src_data, src_linesize, 0, session->video_error->height, dst, dst_linesize);
  665. sws_freeContext(sws);
  666. }
  667. }
  668. else if (*session->conf.ref_Is_ActiveInspect == 1)
  669. {
  670. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_WIDTH, VIDEO_FORMAT_RGB24);
  671. video_frame_fill_black(tmp_frame_preview);
  672. if ((*session->conf.ref_camera_switch == CAMERA_TYPE_ENV) || (*session->conf.ref_camera_switch == CAMERA_TYPE_AUTO))//显示上摄像头
  673. {
  674. if (session->video_shm_q_env)
  675. {
  676. videoq_frame frm;
  677. int offset = (REC_COMMON_VIDEO_SNAPSHOT_WIDTH - REC_COMMON_VIDEO_SNAPSHOT_HEIGHT) / 2;
  678. frm.data = tmp_frame_preview->data[0] + offset * tmp_frame_preview->linesize[0];
  679. session->video_shm_q_env->GetVideo(&frm, VIDEOQUEUE_FLAG_VERTICAL_FLIP);
  680. }
  681. }
  682. else if (*session->conf.ref_camera_switch == CAMERA_TYPE_OPT)//显示下摄像头
  683. {
  684. if (session->video_shm_q_opt)
  685. {
  686. video_frame tt = { 0 };
  687. int offset = (REC_COMMON_VIDEO_SNAPSHOT_WIDTH - REC_COMMON_VIDEO_SNAPSHOT_HEIGHT) / 2;
  688. tt.data[0] = tmp_frame_preview->data[0] + offset * 3;
  689. tt.format = tmp_frame_preview->format;
  690. tt.linesize[0] = tmp_frame_preview->linesize[0];
  691. tt.width = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
  692. tt.height = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
  693. session->video_shm_q_opt->GetVideo2(&tt, VIDEOQUEUE_FLAG_VERTICAL_FLIP);
  694. }
  695. }
  696. }
  697. else
  698. {
  699. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24);
  700. videoq_frame frm;
  701. frm.data = tmp_frame_preview->data[0];
  702. BOOL result = session->video_shm_q_preview->GetVideo(&frm, VIDEOQUEUE_FLAG_HORIZONTAL_FLIP);
  703. if (result){
  704. //人形框融合处理
  705. if (session->personimage != NULL && session->personmask != NULL && *session->conf.ref_Is_showPersonArea == 1)
  706. {
  707. IplImage* img = cvCreateImageHeader(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 3);
  708. img->imageData = (char*)frm.data;
  709. if (frm.width != session->personimage->width)
  710. {
  711. IplImage* tmp = cvCreateImage(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 3);
  712. IplImage* tmpmask = cvCreateImage(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 1);
  713. cvResize(session->personimage, tmp);
  714. cvResize(session->personmask, tmpmask);
  715. cvAdd(img, tmp, img, tmpmask);
  716. cvReleaseImage(&tmp);
  717. cvReleaseImage(&tmpmask);
  718. }
  719. else
  720. {
  721. cvAdd(img, session->personimage, img, session->personmask);
  722. }
  723. cvReleaseImageHeader(&img);
  724. }
  725. }
  726. else {
  727. video_frame_fill_black(tmp_frame_preview);
  728. }
  729. }
  730. }
  731. *frame = tmp_frame_preview;
  732. return *session->conf.ref_window_state;
  733. }
  734. int show_agent_picture_proc(void *arg)
  735. {
  736. picture_record_t *t_record = (picture_record_t*)arg;
  737. if (NULL == t_record){
  738. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("show_agent_picture_proc param valid.");
  739. return -1;
  740. }
  741. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
  742. int iframeid = 1;
  743. for (;;) {
  744. DWORD dwRet = WaitForSingleObject(t_record->evt, 200);
  745. if (dwRet == WAIT_TIMEOUT){
  746. //struct timespec ts;
  747. //clock_gettime(CLOCK_REALTIME, &ts);
  748. //long unsec = ts.tv_nsec + (1000 * 1000 * 200);
  749. //ts.tv_sec += (unsec / 1000000000);
  750. //ts.tv_nsec = (unsec % 1000000000);
  751. //if (0 != sem_timedwait(&t_record->sem_evt, &ts)) {
  752. if (NULL != t_record->session)
  753. {
  754. if (NULL != t_record->session->video_shm_q_remote)
  755. {
  756. int rc = video_shm_enqueue(t_record->session->video_shm_q_remote, t_record->record_frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP, iframeid++);
  757. if (rc != Error_Succeed)
  758. {
  759. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("mobile mod insert remote video to queue failed.");
  760. }
  761. }
  762. video_frame* showframe = video_frame_new(t_record->show_frame->width, t_record->show_frame->height, VIDEO_FORMAT_RGB24);
  763. video_frame_fill_black(showframe);
  764. video_frame_copy(showframe, t_record->show_frame);
  765. int iused = on_rx_frame(showframe, t_record->session);
  766. if (0 == iused){
  767. video_frame_delete(showframe);
  768. }
  769. }
  770. }
  771. else {
  772. break;
  773. }
  774. }
  775. return 0;
  776. }
  777. static void showagentpicdump_exception(PEXCEPTION_POINTERS ExceptionInfo)
  778. {
  779. char tmp[MAX_PATH];
  780. HANDLE hDumpFile;
  781. sprintf(tmp, ".\\showagnetpic_%d.dmp", GetCurrentProcessId());
  782. hDumpFile = CreateFileA( tmp, GENERIC_READ | GENERIC_WRITE,
  783. 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
  784. if( ( hDumpFile != NULL ) && ( hDumpFile != INVALID_HANDLE_VALUE ) )
  785. {
  786. MINIDUMP_EXCEPTION_INFORMATION mdei;
  787. MINIDUMP_TYPE mdt;
  788. mdei.ThreadId = GetCurrentThreadId();
  789. mdei.ExceptionPointers = ExceptionInfo;
  790. mdei.ClientPointers = FALSE;
  791. mdt = MiniDumpWithFullMemory;
  792. MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(),
  793. hDumpFile, mdt, (ExceptionInfo != 0) ? &mdei : 0, 0, 0 );
  794. CloseHandle( hDumpFile );
  795. }
  796. }
  797. //坐席端图像显示线程
  798. static unsigned int __stdcall agent_picture_show_thread(void* arg)
  799. //void* agent_picture_show_thread(void* arg)
  800. {
  801. //__try {
  802. show_agent_picture_proc(arg);
  803. //}
  804. //__except(showagentpicdump_exception(GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER)
  805. //{
  806. // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("show_agent_picture_proc exception.");
  807. //}
  808. return 0;
  809. }
  810. static int agent_picture_video_start(picture_record_t* t_record)
  811. {
  812. t_record->evt = CreateEventA(NULL, FALSE, FALSE, NULL);
  813. t_record->work_thread = (HANDLE)_beginthreadex(NULL, 0, &agent_picture_show_thread, t_record, 0, NULL);
  814. //sem_init(&t_record->sem_evt, 0, 0);
  815. //pthread_attr_t attr;
  816. //pthread_attr_init(&attr);
  817. //struct sched_param param;
  818. //param.sched_priority = sched_get_priority_max(SCHED_RR);
  819. //pthread_attr_setschedpolicy(&attr, SCHED_RR);
  820. //pthread_attr_setschedparam(&attr, &param);
  821. //pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
  822. //int err = pthread_create(&t_record->work_threadid, NULL, agent_picture_show_thread, t_record);
  823. //if (0 == err) {
  824. // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create agent picture show thread success, %u.", t_record->work_threadid);
  825. //}
  826. //else {
  827. // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create agent picture show thread failed for %s.", strerror(errno));
  828. //}
  829. return 0;
  830. }
  831. static int agent_picture_video_stop(picture_record_t* t_record)
  832. {
  833. if (t_record->evt) {
  834. SetEvent(t_record->evt);
  835. if (t_record->work_thread) {
  836. WaitForSingleObject(t_record->work_thread, INFINITE);
  837. CloseHandle(t_record->work_thread);
  838. t_record->work_thread = NULL;
  839. }
  840. CloseHandle(t_record->evt);
  841. t_record->evt = NULL;
  842. }
  843. //sem_post(&t_record->sem_evt);
  844. //if (t_record->work_threadid > 0) {
  845. // pthread_join(t_record->work_threadid, NULL);
  846. // t_record->work_threadid = 0;
  847. //}
  848. //sem_destroy(&t_record->sem_evt);
  849. if (t_record->record_frame){
  850. video_frame_delete(t_record->record_frame);
  851. t_record->record_frame = NULL;
  852. }
  853. if (t_record->show_frame){
  854. video_frame_delete(t_record->show_frame);
  855. t_record->show_frame = NULL;
  856. }
  857. return 0;
  858. }
  859. static int record_agent_picture_show_session_destory(video_session_t* session)
  860. {
  861. if (session->pic_record){
  862. agent_picture_video_stop(session->pic_record);
  863. }
  864. if (session->video_shm_q_env) {
  865. delete session->video_shm_q_env;
  866. }
  867. if (session->video_shm_q_opt) {
  868. delete session->video_shm_q_opt;
  869. }
  870. if (session->video_error){
  871. delete session->video_error->data;
  872. delete session->video_error;
  873. }
  874. if (session->personimage){
  875. cvReleaseImage(&session->personimage);
  876. }
  877. if (session->personmask){
  878. cvReleaseImage(&session->personmask);
  879. }
  880. return 0;
  881. }
  882. static void show_remote_agnet_picture(video_session_t *t_session)
  883. {
  884. char strPath[MAX_PATH]={0};
  885. char strImgPath[MAX_PATH]={0};
  886. int irecord_video_frame_width = REC_COMMON_VIDEO_SSM_AGENT_WIDTH;
  887. int irecord_video_frame_heigt = REC_COMMON_VIDEO_SSM_AGENT_HEIGHT;
  888. if (t_session->conf.eDeviceType == eStand2sType){
  889. irecord_video_frame_width = REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH;
  890. irecord_video_frame_heigt = REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_HEIGHT;
  891. }
  892. GetCurrentRunPath(strPath);
  893. sprintf(strImgPath,"%s\\bin\\agent.jpg",strPath);
  894. video_frame* remote_frame = video_frame_new(irecord_video_frame_width, irecord_video_frame_heigt, VIDEO_FORMAT_RGB24);
  895. video_frame_fill_black(remote_frame);
  896. if (_access(strImgPath,0)!=-1)
  897. {
  898. IplImage*img = cvLoadImage(strImgPath,1);
  899. videoq_frame* vframe = new videoq_frame;
  900. if (img != NULL)
  901. {
  902. vframe->format = VIDEOQ_FORMAT_RGB24;
  903. vframe->framesize = img->imageSize;
  904. vframe->width = img->width;
  905. vframe->height = img->height;
  906. vframe->data = new unsigned char[img->imageSize];
  907. memcpy(vframe->data,img->imageData,img->imageSize);
  908. cvReleaseImage(&img);
  909. }
  910. SwsContext*sws = sws_getContext(vframe->width, vframe->height,PIX_FMT_BGR24,
  911. irecord_video_frame_width,
  912. irecord_video_frame_heigt,
  913. PIX_FMT_BGR24,
  914. SWS_POINT, NULL, NULL, NULL);
  915. uint8_t *src_data[4] = {(unsigned char*)vframe->data+(vframe->height-1)*vframe->width*3,NULL,NULL,NULL};
  916. int src_linesize[4] = {-vframe->width*3,0,0,0};
  917. unsigned char *dst[4] = {remote_frame->data[0],NULL,NULL,NULL};
  918. int dst_linesize[4] = {remote_frame->linesize[0],0,0,0};
  919. sws_scale(sws, src_data, src_linesize, 0, vframe->height, dst, dst_linesize);
  920. sws_freeContext(sws);
  921. if (vframe){
  922. delete vframe->data;
  923. delete vframe;
  924. }
  925. }
  926. if (NULL != t_session->video_shm_q_remote)
  927. {
  928. int rc = video_shm_enqueue(t_session->video_shm_q_remote, remote_frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP, 1);
  929. if (rc != Error_Succeed)
  930. {
  931. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("insert agent picture to remote video queue failed.");
  932. }
  933. }
  934. video_frame * showframe = NULL;
  935. if (-1 == translate_image_resolution(&showframe,t_session->conf.remote_video_width,t_session->conf.remote_video_height,remote_frame))
  936. {
  937. showframe = video_frame_new(t_session->conf.remote_video_width,t_session->conf.remote_video_height, VIDEO_FORMAT_RGB24);
  938. video_frame_fill_black(showframe);
  939. video_frame_copy(showframe, remote_frame);
  940. }
  941. t_session->pic_record = new picture_record_t();
  942. t_session->pic_record->record_frame = remote_frame;
  943. t_session->pic_record->show_frame = showframe;
  944. t_session->pic_record->session = t_session;
  945. agent_picture_video_start(t_session->pic_record);
  946. }
  947. static int start_video_rtpsession(video_session_t* session)
  948. {
  949. int rc = -1;
  950. videortp_config_t config = { 0 };
  951. config.fps_den = REC_COMMON_VIDEO_FPS_DEN;
  952. config.fps_num = REC_COMMON_VIDEO_FPS_NUM;
  953. config.capture_height = -1; // not use
  954. config.capture_width = -1; // not use
  955. config.dir = 3;
  956. config.tx_width = REC_COMMON_VIDEO_RTP_WIDTH;
  957. config.tx_height = REC_COMMON_VIDEO_RTP_HEIGHT;
  958. config.rx_width = session->conf.remote_video_width;
  959. config.rx_height = session->conf.remote_video_height;
  960. config.local_ip = session->conf.local_rtp_ip;
  961. config.local_pt = session->conf.local_pt;
  962. config.remote_pt = session->conf.local_pt;
  963. config.local_rtp_port = session->conf.local_rtp_port;
  964. config.mtu = session->conf.mtu;
  965. config.quant = session->conf.video_quant;
  966. config.remote_ip = session->conf.remote_rtp_ip;
  967. config.remote_rtp_port = session->conf.remote_rtp_port;
  968. config.user_data = session;
  969. config.bit_rate = session->conf.bit_rate;
  970. config.on_rx_frame = &on_rx_frame;
  971. config.dbg = &__dbg;
  972. config.logevent = &__logevent;
  973. videortp_create(&config, &session->rtp);
  974. if (eSDL == session->conf.eType){
  975. session->premote_render->StartVideoRender();
  976. }
  977. rc = videortp_start(session->rtp);
  978. if (rc != 0)
  979. {
  980. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start video rtp failed!");
  981. videortp_destroy(session->rtp);
  982. session->rtp = NULL;
  983. }
  984. return rc;
  985. }
  986. static int start_local_video_clock(video_session_t* session)
  987. {
  988. int rc = -1;
  989. rc = videoclock_create(REC_COMMON_VIDEO_FPS_NUM, REC_COMMON_VIDEO_FPS_DEN,
  990. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_RGB24,
  991. &local_put_frame, session, &local_get_frame, session, &session->local_clock, session->conf.ref_Up_Fps, &__dbg);
  992. if (0 == rc) {
  993. rc = videoclock_start(session->local_clock);
  994. if (rc != 0) {
  995. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start video clock failed!");
  996. videoclock_destroy(session->local_clock);
  997. session->local_clock = NULL;
  998. }
  999. }
  1000. else {
  1001. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create video clock failed!");
  1002. }
  1003. return rc;
  1004. }
  1005. static int start_video(video_session_t *session)
  1006. {
  1007. int rc = -1;
  1008. if (session->video_shm_q_env)
  1009. {
  1010. session->local_encode_sws_ctx_env = sws_getContext(
  1011. REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, PIX_FMT_BGR24,
  1012. REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, PIX_FMT_YUV420P,
  1013. SWS_POINT, NULL, NULL, NULL);
  1014. if (!session->local_encode_sws_ctx_env) {
  1015. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get session->local_encode_sws_ctx_env failed.");
  1016. goto on_error;
  1017. }
  1018. }
  1019. else{
  1020. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("session->video_shm_q_env is NULL.");
  1021. }
  1022. if (session->video_shm_q_opt)
  1023. {
  1024. session->local_encode_sws_ctx_opt = sws_getContext(
  1025. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, PIX_FMT_BGR24,
  1026. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, PIX_FMT_YUV420P,
  1027. SWS_POINT, NULL, NULL, NULL);
  1028. if (!session->local_encode_sws_ctx_opt) {
  1029. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get session->local_encode_sws_ctx_opt failed.");
  1030. goto on_error;
  1031. }
  1032. }
  1033. else{
  1034. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("session->video_shm_q_opt is NULL.");
  1035. }
  1036. if (eGDI == session->conf.eType){
  1037. if (DOUBLERECORD_CALLTYPE != session->conf.nCallType){
  1038. if (session->remote_hwnd)
  1039. {
  1040. rc = videoplayer_create(session->remote_hwnd,
  1041. 0,
  1042. 0,
  1043. session->conf.remote_video_view_cx,
  1044. session->conf.remote_video_view_cy,
  1045. REC_COMMON_VIDEO_FPS_NUM,
  1046. REC_COMMON_VIDEO_FPS_DEN,
  1047. session->conf.remote_video_width,
  1048. session->conf.remote_video_height,
  1049. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP,
  1050. session->conf.iremote_wind_flags,
  1051. "remote",
  1052. on_remoteWinstate,
  1053. NULL,
  1054. session,
  1055. &session->remote_player);
  1056. if (rc != 0) {
  1057. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create remote video player failed!");
  1058. goto on_error;
  1059. }
  1060. }
  1061. else
  1062. {
  1063. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("remote_hwnd == Null");
  1064. }
  1065. }
  1066. else{
  1067. if (NULL != pg_last_session){
  1068. session->remote_player = pg_last_session->remote_player;
  1069. rc = 0;
  1070. }
  1071. }
  1072. BOOL bIsActiveInspect = FALSE;
  1073. if (session->conf.ref_Is_ActiveInspect != NULL){
  1074. if (*session->conf.ref_Is_ActiveInspect == 1){
  1075. bIsActiveInspect = TRUE;
  1076. }
  1077. else{
  1078. bIsActiveInspect = FALSE;
  1079. }
  1080. }
  1081. else{
  1082. bIsActiveInspect = FALSE;
  1083. }
  1084. if (DOUBLERECORD_CALLTYPE != session->conf.nCallType){
  1085. rc = videoplayer_create(session->local_hwnd,
  1086. 0,
  1087. 0,
  1088. session->conf.local_video_view_cx,
  1089. session->conf.local_video_view_cy,
  1090. REC_COMMON_VIDEO_FPS_NUM,
  1091. REC_COMMON_VIDEO_FPS_DEN,
  1092. bIsActiveInspect?REC_COMMON_VIDEO_SNAPSHOT_WIDTH:REC_COMMON_VIDEO_PREVIEW_WIDTH,
  1093. bIsActiveInspect?REC_COMMON_VIDEO_SNAPSHOT_WIDTH:REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  1094. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP,
  1095. session->conf.ilocal_wind_flags,
  1096. "local",
  1097. on_pull, free_frame, session,
  1098. &session->local_player);
  1099. if (rc != 0) {
  1100. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create local video player failed!");
  1101. goto on_error;
  1102. }
  1103. }
  1104. else{
  1105. if (NULL != pg_last_session){
  1106. session->local_player = pg_last_session->local_player;
  1107. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("double record call local player reuse.");
  1108. }
  1109. }
  1110. if (session->remote_hwnd)
  1111. {
  1112. if (0 != session->conf.remote_rtp_port){
  1113. if (0 != start_video_rtpsession(session)) {
  1114. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start_video_rtpsession failed!");
  1115. goto on_error;
  1116. }
  1117. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType){
  1118. if (NULL != pg_last_session){
  1119. pg_last_session->rtp = session->rtp;
  1120. }
  1121. }
  1122. if (0 != start_local_video_clock(session)) {
  1123. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start_local_video_clock failed!");
  1124. goto on_error;
  1125. }
  1126. }
  1127. else{
  1128. pg_last_session = session; //保存session信息
  1129. show_remote_agnet_picture(session);
  1130. }
  1131. }
  1132. }
  1133. else{
  1134. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType) {
  1135. if (NULL != pg_last_session) {
  1136. session->premote_render = pg_last_session->premote_render;
  1137. session->plocal_render = pg_last_session->plocal_render;
  1138. }
  1139. }
  1140. if (session->premote_render)
  1141. {
  1142. if (0 != session->conf.remote_rtp_port) {
  1143. if (0 != start_video_rtpsession(session)) {
  1144. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start_video_rtpsession failed!");
  1145. goto on_error;
  1146. }
  1147. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType) {
  1148. if (NULL != pg_last_session) {
  1149. pg_last_session->rtp = session->rtp;
  1150. }
  1151. }
  1152. if (0 != start_local_video_clock(session)) {
  1153. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start_local_video_clock failed!");
  1154. goto on_error;
  1155. }
  1156. }
  1157. else {
  1158. show_remote_agnet_picture(session);
  1159. }
  1160. }
  1161. }
  1162. on_error:
  1163. return rc;
  1164. }
  1165. static void stop_video(video_session_t *session)
  1166. {
  1167. if (eGDI == session->conf.eType){
  1168. if (session->local_clock && session->remote_hwnd){
  1169. videoclock_stop(session->local_clock);
  1170. videoclock_destroy(session->local_clock);
  1171. session->local_clock = NULL;
  1172. pg_remote_hwnd = NULL;
  1173. pg_local_hwnd = NULL;
  1174. }
  1175. }
  1176. else{
  1177. if (session->local_clock){
  1178. videoclock_stop(session->local_clock);
  1179. videoclock_destroy(session->local_clock);
  1180. session->local_clock = NULL;
  1181. }
  1182. }
  1183. if (session->rtp){
  1184. videortp_stop(session->rtp);
  1185. videortp_destroy(session->rtp);
  1186. session->rtp = NULL;
  1187. }
  1188. else {
  1189. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("session->rtp == null");
  1190. }
  1191. if (eGDI == session->conf.eType){
  1192. if (session->local_player) {
  1193. videoplayer_destroy(session->local_player);
  1194. session->local_player = NULL;
  1195. }
  1196. if (session->remote_player && session->remote_hwnd){
  1197. videoplayer_destroy(session->remote_player);
  1198. session->remote_player = NULL;
  1199. }
  1200. }
  1201. else{
  1202. session->premote_render->StopVideoRender();
  1203. if (session->plocal_render){
  1204. delete session->plocal_render;
  1205. session->plocal_render = NULL;
  1206. }
  1207. if (session->premote_render){
  1208. delete session->premote_render;
  1209. session->premote_render = NULL;
  1210. }
  1211. }
  1212. if (session->local_encode_sws_ctx_env) {
  1213. sws_freeContext(session->local_encode_sws_ctx_env);
  1214. session->local_encode_sws_ctx_env = NULL;
  1215. }
  1216. if (session->local_encode_sws_ctx_opt) {
  1217. sws_freeContext(session->local_encode_sws_ctx_opt);
  1218. session->local_encode_sws_ctx_opt = NULL;
  1219. }
  1220. }
  1221. static bool ReMoveOutOfScreenVideoWindow(HWND hWnd, RECT rect)
  1222. {
  1223. bool bret = false;
  1224. int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  1225. int iScreenHight = GetSystemMetrics(SM_CYSCREEN);
  1226. int iVideoWidth = rect.right - rect.left;
  1227. int iVideoHight = rect.bottom - rect.top;
  1228. if (rect.left < 0) {
  1229. if (rect.bottom <= iScreenHight) {
  1230. MoveWindow(hWnd, 0, rect.top, iVideoWidth, iVideoHight, TRUE);
  1231. }
  1232. else {
  1233. MoveWindow(hWnd, 0, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1234. }
  1235. bret = true;
  1236. }
  1237. if (rect.right > iScreenWidth) {
  1238. if (rect.bottom <= iScreenHight) {
  1239. MoveWindow(hWnd, iScreenWidth - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1240. }
  1241. else {
  1242. MoveWindow(hWnd, iScreenWidth - iVideoWidth, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1243. }
  1244. bret = true;
  1245. }
  1246. if (rect.bottom > iScreenHight) {
  1247. if (rect.left >= 0 && rect.right <= iScreenWidth) {
  1248. MoveWindow(hWnd, rect.left, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1249. bret = true;
  1250. }
  1251. }
  1252. return bret;
  1253. }
  1254. static bool ReMoveCenterOtherVideoWindow(HWND hWnd, RECT rect, RECT otherect)
  1255. {
  1256. bool bret = false;
  1257. int iVideoWidth = rect.right - rect.left;
  1258. int iVideoHight = rect.bottom - rect.top;
  1259. int iOtherVideoWidth = otherect.right - otherect.left;
  1260. int iOtherVideoHight = otherect.bottom - otherect.top;
  1261. if (iVideoWidth > iOtherVideoHight || iVideoHight > iOtherVideoHight) {
  1262. return bret;
  1263. }
  1264. if (rect.left > otherect.left + iVideoWidth && rect.right < otherect.right) {
  1265. if (rect.top > otherect.top && rect.bottom < otherect.bottom) {
  1266. if (otherect.right - rect.right <= rect.left - otherect.left) {
  1267. MoveWindow(hWnd, otherect.right - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1268. }
  1269. else {
  1270. MoveWindow(hWnd, otherect.left, rect.top, iVideoWidth, iVideoHight, TRUE);
  1271. }
  1272. bret = true;
  1273. }
  1274. }
  1275. if (rect.right < otherect.right - iVideoWidth && rect.left > otherect.left) {
  1276. if (rect.top > otherect.top && rect.bottom < otherect.bottom) {
  1277. if (otherect.right - rect.right <= rect.left - otherect.left) {
  1278. MoveWindow(hWnd, otherect.right - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1279. }
  1280. else {
  1281. MoveWindow(hWnd, otherect.left, rect.top, iVideoWidth, iVideoHight, TRUE);
  1282. }
  1283. bret = true;
  1284. }
  1285. }
  1286. return bret;
  1287. }
  1288. static bool ReMoveVideoWindow(HWND hWnd, RECT rect, RECT otherect)
  1289. {
  1290. bool bret = false;
  1291. bret = ReMoveOutOfScreenVideoWindow(hWnd, rect);
  1292. bret = ReMoveCenterOtherVideoWindow(hWnd, rect, otherect);
  1293. return bret;
  1294. }
  1295. static int HandleVideoMoveEvent(int iMessageType, HWND hWnd, video_session_t* pSession)
  1296. {
  1297. int iRet = -1;
  1298. if (NULL == pSession || NULL == hWnd) {
  1299. return iRet;
  1300. }
  1301. HWND hOtherWnd = NULL;
  1302. //本地和远端标识 1为本地,2为远端
  1303. int iVideoType = 2;
  1304. if (hWnd == pSession->local_hwnd) {
  1305. iVideoType = 1;
  1306. hOtherWnd = pSession->remote_hwnd;
  1307. }
  1308. else {
  1309. hOtherWnd = pSession->local_hwnd;
  1310. }
  1311. if (NULL == hOtherWnd) {
  1312. return iRet;
  1313. }
  1314. bool bMoved = false;
  1315. RECT rect;
  1316. GetWindowRect(hWnd, &rect);
  1317. RECT otherect;
  1318. GetWindowRect(hOtherWnd, &otherect);
  1319. bMoved = ReMoveVideoWindow(hWnd, rect, otherect);
  1320. if (bMoved) {
  1321. GetWindowRect(hWnd, &rect);
  1322. }
  1323. if (NULL != pSession->conf.video_echo_cb && NULL != pSession->conf.video_echo_cb->on_video_box_move) {
  1324. pSession->conf.video_echo_cb->on_video_box_move(iMessageType, iVideoType, rect.left, rect.bottom, pSession->conf.video_echo_cb->user_data);
  1325. iRet = 0;
  1326. }
  1327. return iRet;
  1328. }
  1329. static LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  1330. {
  1331. video_session_t* session = (video_session_t*)GetWindowLongPtrA(hWnd, GWLP_USERDATA);
  1332. switch (msg) {
  1333. case WM_DESTROY:
  1334. if (session->local_hwnd == hWnd) {
  1335. session->local_hwnd = 0;
  1336. }
  1337. else if (session->remote_hwnd == hWnd) {
  1338. session->remote_hwnd = 0;
  1339. }
  1340. if (session->local_hwnd == 0 && session->remote_hwnd == 0) {
  1341. PostQuitMessage(0);
  1342. }
  1343. return 0;
  1344. case WM_NCHITTEST:
  1345. if (((session->local_hwnd == hWnd)&&(0 != session->conf.local_move)) || ((session->remote_hwnd == hWnd)&&(0 != session->conf.remote_move))){
  1346. return HTCAPTION;
  1347. }
  1348. else{
  1349. break;
  1350. }
  1351. case WM_ACTIVATE:
  1352. case WM_TOUCH:
  1353. case WM_GESTURE:
  1354. ReleaseCapture();
  1355. return 0;
  1356. #if 1
  1357. case WM_WINDOWPOSCHANGED:
  1358. {
  1359. LPWINDOWPOS pPos = (LPWINDOWPOS)lParam;
  1360. if (pPos->hwndInsertAfter != HWND_TOPMOST && pPos->hwndInsertAfter != HWND_TOP) {
  1361. //BringWindowToTop(hWnd);
  1362. SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  1363. }
  1364. else {
  1365. return DefWindowProc(hWnd, msg, wParam, lParam);
  1366. }
  1367. }
  1368. return 0;
  1369. //return DefWindowProc(hWnd, msg, wParam, lParam);
  1370. #endif
  1371. case WM_MBUTTONDOWN:
  1372. case WM_MBUTTONUP:
  1373. case WM_MBUTTONDBLCLK:
  1374. case WM_LBUTTONDBLCLK:
  1375. case WM_LBUTTONDOWN:
  1376. case WM_LBUTTONUP:
  1377. if (session) {
  1378. if (session->local_hwnd) {
  1379. //ReleaseCapture();
  1380. }
  1381. }
  1382. return 0;
  1383. case WM_CLOSE:
  1384. stop_video(session);
  1385. //SetEvent(session->ui_thread);
  1386. DestroyWindow(session->local_hwnd);
  1387. //LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_LOCAL_WINDOW_DESTROY_SUCCESS, "destroy local window.");
  1388. DestroyWindow(session->remote_hwnd);
  1389. //LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_REMOTE_WINDOW_DESTROY_SUCCESS, "destroy remote window.");
  1390. return 0;
  1391. case WM_MOVE:
  1392. break;
  1393. case WM_ENTERSIZEMOVE:
  1394. {
  1395. HandleVideoMoveEvent(0, hWnd, session);
  1396. }
  1397. break;
  1398. case WM_EXITSIZEMOVE:
  1399. {
  1400. HandleVideoMoveEvent(1, hWnd, session);
  1401. }
  1402. break;
  1403. default:
  1404. return DefWindowProc(hWnd, msg, wParam, lParam);
  1405. }
  1406. return 0;
  1407. }
  1408. static unsigned int __stdcall ui_proc(void *arg)
  1409. {
  1410. video_session_t *session = (video_session_t*)arg;
  1411. WNDCLASSA wc = {0};
  1412. ATOM a = 0;
  1413. HWND hWnd = NULL;
  1414. MSG msg;
  1415. HINSTANCE hInst = ModuleBase::GetModuleBase()->GetInstance();
  1416. bool breuse_local_wnd = false;
  1417. bool breuse_remote_wnd = false;
  1418. DWORD dLocal_style = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_NOACTIVATE;
  1419. DWORD dRemote_style = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_NOACTIVATE;
  1420. //双录两阶段视频窗口复用
  1421. if (NULL != pg_local_hwnd){
  1422. session->local_hwnd = pg_local_hwnd;
  1423. //SetWindowLongPtrA(session->local_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1424. breuse_local_wnd = true;
  1425. }
  1426. else{
  1427. if (0 != session->conf.local_move){
  1428. dLocal_style -= WS_EX_NOACTIVATE;
  1429. }
  1430. if (0 != session->conf.remote_move){
  1431. dRemote_style -= WS_EX_NOACTIVATE;
  1432. }
  1433. CoInitialize(0);
  1434. wc.cbClsExtra = 0;
  1435. wc.cbWndExtra = 0;
  1436. wc.hInstance = hInst;
  1437. wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  1438. wc.hCursor = NULL;
  1439. wc.hIcon = NULL;
  1440. wc.lpfnWndProc = &WndProc;
  1441. wc.lpszClassName = WNDCLS_NAME;
  1442. wc.style = CS_HREDRAW | CS_OWNDC | CS_VREDRAW;
  1443. a = RegisterClassA(&wc);
  1444. if (a == 0)
  1445. return 0;
  1446. session->local_hwnd = CreateWindowExA(dLocal_style,
  1447. WNDCLS_NAME, NULL, WS_POPUP|WS_VISIBLE,
  1448. session->conf.local_video_view_x, session->conf.local_video_view_y, session->conf.local_video_view_cx, session->conf.local_video_view_cy,
  1449. NULL, NULL, hInst, NULL);
  1450. if (session->local_hwnd) {
  1451. SetWindowLongPtrA(session->local_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1452. if (0 == session->conf.local_move){
  1453. SetWindowPos(session->local_hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
  1454. }
  1455. pg_local_hwnd = session->local_hwnd;
  1456. //LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_LOCAL_WINDOW_CREATE_SUCCESS, "create local window success and restore local video hwnd.");
  1457. }
  1458. else{
  1459. char strinfo[MAX_PATH] = {0};
  1460. _snprintf(strinfo, MAX_PATH, "%s%d", "create local window failed and last error is ", GetLastError());
  1461. LogWarn(Severity_Middle, Error_Debug, EVENT_MOD_SIP_LOCAL_WINDOW_CREATE_FAILED, strinfo);
  1462. //return -1;
  1463. }
  1464. }
  1465. if(session->conf.remote_video_view_x||session->conf.remote_video_view_y||session->conf.remote_video_view_cx||session->conf.remote_video_view_cy)
  1466. {
  1467. if (NULL != pg_remote_hwnd){
  1468. session->remote_hwnd = pg_remote_hwnd;
  1469. breuse_remote_wnd = true;
  1470. if (NULL != pg_last_session){
  1471. record_agent_picture_show_session_destory(pg_last_session);
  1472. }
  1473. }
  1474. else{
  1475. session->remote_hwnd = CreateWindowExA(dRemote_style,
  1476. WNDCLS_NAME, NULL, WS_POPUP|WS_VISIBLE,
  1477. session->conf.remote_video_view_x, session->conf.remote_video_view_y, session->conf.remote_video_view_cx, session->conf.remote_video_view_cy,
  1478. NULL, NULL, hInst, NULL);
  1479. if (session->remote_hwnd) {
  1480. SetWindowLongPtrA(session->remote_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1481. if (0 == session->conf.remote_move){
  1482. SetWindowPos(session->remote_hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
  1483. }
  1484. pg_remote_hwnd = session->remote_hwnd;
  1485. //LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_REMOTE_WINDOW_CREATE_SUCCESS, "create remote window success and restore remote video hwnd.");
  1486. }
  1487. else{
  1488. char strinfo[MAX_PATH] = {0};
  1489. _snprintf(strinfo, MAX_PATH, "%s%d", "create remote window failed and last error is ", GetLastError());
  1490. LogWarn(Severity_Middle, Error_Debug, EVENT_MOD_SIP_REMOTE_WINDOW_CREATE_FAILED, strinfo);
  1491. //return -1;
  1492. }
  1493. }
  1494. }
  1495. else
  1496. {
  1497. session->remote_hwnd = NULL;
  1498. }
  1499. if (session->local_hwnd/* && session->remote_hwnd*/)
  1500. {
  1501. int rc = 0;
  1502. ShowCursor(FALSE);
  1503. SetEvent(session->ui_event);
  1504. rc = start_video(session);
  1505. if (rc != 0) {
  1506. LogWarn(Severity_Middle, Error_Debug, ERROR_MOD_SIP_START_VIDEO_FAILED, "start video failed!");
  1507. }
  1508. if (!breuse_local_wnd && !breuse_remote_wnd)
  1509. {
  1510. while (GetMessageA(&msg, NULL, NULL, NULL))
  1511. {
  1512. if (msg.message == WM_CLOSE) {
  1513. }
  1514. TranslateMessage(&msg);
  1515. DispatchMessageA(&msg);
  1516. }
  1517. SetEvent(session->ui_event);
  1518. }
  1519. }
  1520. if (a)
  1521. UnregisterClassA(WNDCLS_NAME, hInst);
  1522. CoUninitialize();
  1523. return 0;
  1524. }
  1525. static void __video_render_log(void* user_data, const char* fmt, va_list arg)
  1526. {
  1527. int n = _vscprintf(fmt, arg);
  1528. if (n >= MAX_LOG_LEN) {
  1529. char* buf = (char*)malloc((size_t)(n + 1));
  1530. _vsnprintf(buf, n + 1, fmt, arg);
  1531. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s", buf);
  1532. free(buf);
  1533. }
  1534. else{
  1535. char strlog[MAX_LOG_LEN] = {0};
  1536. _vsnprintf(strlog, MAX_LOG_LEN, fmt, arg);
  1537. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s", strlog);
  1538. }
  1539. }
  1540. //void* videorender_func(void* arg)
  1541. static unsigned int __stdcall videorender_func(void* arg)
  1542. {
  1543. video_session_t* session = (video_session_t*)arg;
  1544. char str_local_ip[MAX_PATH] = { 0 };
  1545. char str_remote_ip[MAX_PATH] = { 0 };
  1546. translate_ipaddr_from_int(str_local_ip, MAX_PATH, session->conf.local_rtp_ip);
  1547. translate_ipaddr_from_int(str_remote_ip, MAX_PATH, session->conf.remote_rtp_ip);
  1548. videorender_callback_t t_callback = { 0 };
  1549. t_callback.debug = &__video_render_log;
  1550. //双录两阶段视频窗口复用
  1551. if (NULL != pg_local_render){
  1552. session->plocal_render = (IVideoRender*)pg_local_render;
  1553. }
  1554. else{
  1555. session->plocal_render = CreateVideoRenderObj(&t_callback);
  1556. if (NULL != session->plocal_render){
  1557. videorender_param_t tparam = { 0 };
  1558. tparam.icx = session->conf.local_video_view_x;
  1559. tparam.icy = session->conf.local_video_view_y;
  1560. tparam.uwidth = session->conf.local_video_view_cx;
  1561. tparam.uheight = session->conf.local_video_view_cy;
  1562. tparam.bmoveable = (bool)session->conf.local_move;
  1563. //tparam.uvideowidth = REC_COMMON_VIDEO_PREVIEW_WIDTH;
  1564. //tparam.uvideoheight = REC_COMMON_VIDEO_PREVIEW_HEIGHT;
  1565. tparam.ivideoformat = VIDEO_FORMAT_RGB24;
  1566. if (0 == session->plocal_render->VideoRenderSetParam(&tparam)){
  1567. session->plocal_render->HideVideoWindow();
  1568. }
  1569. else {
  1570. return 0;
  1571. }
  1572. pg_local_render = session->plocal_render;
  1573. }
  1574. }
  1575. if (session->conf.remote_video_view_x || session->conf.remote_video_view_y || session->conf.remote_video_view_cx || session->conf.remote_video_view_cy){
  1576. if (NULL != pg_local_render){
  1577. session->premote_render = (IVideoRender*)pg_remote_render;
  1578. }
  1579. else{
  1580. session->premote_render = CreateVideoRenderObj(&t_callback);
  1581. if (session->premote_render){
  1582. videorender_param_t tparam_remote = { 0 };
  1583. tparam_remote.icx = session->conf.remote_video_view_x;
  1584. tparam_remote.icy = session->conf.remote_video_view_y;
  1585. tparam_remote.uwidth = session->conf.remote_video_view_cx;
  1586. tparam_remote.uheight = session->conf.remote_video_view_cy;
  1587. tparam_remote.bmoveable = (bool)session->conf.remote_move;
  1588. //tparam_remote.uvideowidth = REC_COMMON_VIDEO_DSM_AGENT_WIDTH;
  1589. //tparam_remote.uvideoheight = REC_COMMON_VIDEO_DSM_AGENT_HEIGHT;
  1590. tparam_remote.ivideoformat = VIDEO_FORMAT_RGB24;
  1591. if (0 == session->premote_render->VideoRenderSetParam(&tparam_remote)) {
  1592. //session->premote_render->ShowVideoWindow();
  1593. }
  1594. }
  1595. else {
  1596. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("session remote video render is null.");
  1597. }
  1598. pg_remote_render = session->premote_render;
  1599. }
  1600. }
  1601. if (session->plocal_render){
  1602. start_video(session);
  1603. //session->plocal_render->ShowVideoWindow();
  1604. bool bshow_local = false;
  1605. bool bset = true;
  1606. //session->premote_render->ShowVideoWindow();
  1607. if (DOUBLERECORD_CALLTYPE != session->conf.nCallType){
  1608. session->plocal_render->StartVideoRender();
  1609. }
  1610. for (; ; ) {
  1611. //struct timespec ts;
  1612. //clock_gettime(CLOCK_REALTIME, &ts);
  1613. //long unsec = ts.tv_nsec + (1000 * 1000 * 500);
  1614. //ts.tv_sec += (unsec / 1000000000);
  1615. //ts.tv_nsec = (unsec % 1000000000);
  1616. //if (0 != sem_timedwait(&session->ui_stop_sem, &ts) && (ETIMEDOUT == errno)){
  1617. DWORD dwRet = WaitForSingleObject(session->ui_event, 500);
  1618. if (dwRet == WAIT_TIMEOUT){
  1619. video_frame* local_video_frame = NULL;
  1620. int iwindowstate = get_local_video_frame(session, &local_video_frame);
  1621. if (iwindowstate != session->ilast_windstae){
  1622. set_video_windows(session, iwindowstate);
  1623. session->ilast_windstae = iwindowstate;
  1624. }
  1625. if (NULL != local_video_frame) {
  1626. video_frame* localframe = NULL;
  1627. if (0 == translate_image_resolution(&localframe, session->conf.local_video_view_cx, session->conf.local_video_view_cy, local_video_frame)) {
  1628. session->plocal_render->RenderVideoFrame(localframe, RVC_FLIP_VERTICAL);
  1629. video_frame_delete(localframe);
  1630. localframe = NULL;
  1631. }
  1632. else {
  1633. session->plocal_render->RenderVideoFrame(local_video_frame, RVC_FLIP_VERTICAL);
  1634. }
  1635. bshow_local = true;
  1636. video_frame_delete(local_video_frame);
  1637. local_video_frame = NULL;
  1638. }
  1639. }
  1640. else {
  1641. session->plocal_render->HideVideoWindow();
  1642. break;
  1643. }
  1644. if (bset){
  1645. if (bshow_local && session->bshow_remote) {
  1646. session->plocal_render->ShowVideoWindow();
  1647. session->premote_render->ShowVideoWindow();
  1648. bset = false;
  1649. }
  1650. }
  1651. }
  1652. session->plocal_render->StopVideoRender();
  1653. }
  1654. return 0;
  1655. }
  1656. static int start_ui(video_session_t *session)
  1657. {
  1658. session->ui_event = CreateEventA(NULL, FALSE, FALSE, NULL);
  1659. if (!session->ui_event) {
  1660. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("phonemedia_start create ui event failed!");
  1661. return Error_Resource;
  1662. }
  1663. if (eGDI == session->conf.eType){
  1664. session->ui_thread = (HANDLE)_beginthreadex(NULL, 0, &ui_proc, session, 0, NULL);
  1665. }
  1666. else{
  1667. session->ui_thread = (HANDLE)_beginthreadex(NULL, 0, &videorender_func, session, 0, NULL);
  1668. }
  1669. if (!session->ui_thread) {
  1670. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("phonemedia_start create ui thread failed!");
  1671. CloseHandle(session->ui_event);
  1672. session->ui_event = NULL;
  1673. return Error_Resource;
  1674. }
  1675. {
  1676. HANDLE hs[] = {session->ui_event, session->ui_thread};
  1677. DWORD dwRet = WaitForMultipleObjects(array_size(hs), hs, FALSE, INFINITE);
  1678. if (dwRet == WAIT_OBJECT_0) { //
  1679. } else if (dwRet == WAIT_OBJECT_0 + 1) { // thread exit
  1680. CloseHandle(session->ui_thread);
  1681. session->ui_thread = NULL;
  1682. CloseHandle(session->ui_event);
  1683. session->ui_event = NULL;
  1684. return Error_Resource;
  1685. }
  1686. }
  1687. return 0;
  1688. }
  1689. static void stop_ui(video_session_t *session)
  1690. {
  1691. if (eGDI == session->conf.eType){
  1692. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType) {
  1693. if (NULL != pg_last_session) {
  1694. pg_last_session->rtp = session->rtp;
  1695. pg_last_session->local_clock = session->local_clock;
  1696. CloseHandle(session->ui_thread);
  1697. session->ui_thread = NULL;
  1698. CloseHandle(session->ui_event);
  1699. session->ui_event = NULL;
  1700. session = pg_last_session;
  1701. }
  1702. }
  1703. if (NULL != session->pic_record) {
  1704. if (NULL != session->pic_record->work_thread) {
  1705. SetEvent(session->pic_record->evt);
  1706. CloseHandle(session->pic_record->work_thread);
  1707. session->pic_record->work_thread = NULL;
  1708. CloseHandle(session->pic_record->evt);
  1709. session->pic_record->evt = NULL;
  1710. }
  1711. }
  1712. if (session->local_hwnd) {
  1713. BOOL bRet = PostMessageA(session->local_hwnd, WM_CLOSE, 0, 0);
  1714. DWORD dCode = WaitForSingleObject(session->ui_thread, INFINITE);
  1715. CloseHandle(session->ui_thread);
  1716. session->ui_thread = NULL;
  1717. CloseHandle(session->ui_event);
  1718. session->ui_event = NULL;
  1719. }
  1720. pg_last_session = NULL;
  1721. pg_local_hwnd = NULL;
  1722. pg_remote_hwnd = NULL;
  1723. }
  1724. else{
  1725. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType) {
  1726. if (NULL != pg_last_session) {
  1727. pg_last_session->rtp = session->rtp;
  1728. pg_last_session->local_clock = session->local_clock;
  1729. CloseHandle(session->ui_thread);
  1730. session->ui_thread = NULL;
  1731. CloseHandle(session->ui_event);
  1732. session->ui_event = NULL;
  1733. session = pg_last_session;
  1734. }
  1735. }
  1736. if (NULL != session->pic_record) {
  1737. if (NULL != session->pic_record->work_thread) {
  1738. SetEvent(session->pic_record->evt);
  1739. CloseHandle(session->pic_record->work_thread);
  1740. session->pic_record->work_thread = NULL;
  1741. CloseHandle(session->pic_record->evt);
  1742. session->pic_record->evt = NULL;
  1743. }
  1744. }
  1745. SetEvent(session->ui_event);
  1746. DWORD dCode = WaitForSingleObject(session->ui_thread, INFINITE);
  1747. CloseHandle(session->ui_thread);
  1748. session->ui_thread = NULL;
  1749. CloseHandle(session->ui_event);
  1750. session->ui_event = NULL;
  1751. pg_last_session = NULL;
  1752. stop_video(session);
  1753. }
  1754. }
  1755. int Local_video_session_create(const video_session_conf_t *conf, video_session_t **p_session, const bool bremote)
  1756. {
  1757. video_session_t *session = ZALLOC_T(video_session_t);
  1758. if (session)
  1759. {
  1760. memcpy(&session->conf, conf, sizeof(video_session_conf_t));
  1761. session->video_shm_q_preview = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_PREVIEW_QUEUE);
  1762. if (bremote){
  1763. if (eStand2sType == conf->eDeviceType){
  1764. session->video_shm_q_remote = new Clibvideoqueue(REC_COMMON_VIDEO_REMOTE_SHM_RTP_QUEUE);
  1765. }
  1766. else{
  1767. session->video_shm_q_remote = new Clibvideoqueue(REC_COMMON_VIDEO_REMOTE_SHM_RTP_QUEUE);
  1768. }
  1769. }
  1770. if (conf->ref_Is_ActiveInspect != NULL)
  1771. {
  1772. if (*conf->ref_Is_ActiveInspect == 1)
  1773. {
  1774. session->video_error = NULL;
  1775. session->video_shm_q_env = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
  1776. if (conf->camera_count == 2)
  1777. {
  1778. session->video_shm_q_opt = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_SNAPSHOT_QUEUE);
  1779. }
  1780. }
  1781. }
  1782. session->local_clock = NULL;
  1783. *p_session = session;
  1784. }
  1785. return 0;
  1786. }
  1787. int video_session_create(const video_session_conf_t *conf, video_session_t **p_session)
  1788. {
  1789. video_session_t *session = ZALLOC_T(video_session_t);
  1790. if (session)
  1791. {
  1792. char str_local[MAX_PATH] = {0};
  1793. char str_remote[MAX_PATH] = {0};
  1794. translate_ipaddr_from_int(str_local, MAX_PATH, conf->local_rtp_ip);
  1795. translate_ipaddr_from_int(str_remote, MAX_PATH, conf->remote_rtp_ip);
  1796. memcpy(&session->conf, conf, sizeof(video_session_conf_t));
  1797. session->video_shm_q_env = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE);
  1798. if (DOUBLERECORD_CALLTYPE != conf->nCallType){
  1799. session->video_shm_q_remote = new Clibvideoqueue(REC_COMMON_VIDEO_REMOTE_SHM_RTP_QUEUE);
  1800. session->video_shm_q_preview = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_PREVIEW_QUEUE);
  1801. }
  1802. else{
  1803. if (NULL != pg_last_session){
  1804. session->video_shm_q_remote = (pg_last_session)->video_shm_q_remote;
  1805. session->video_shm_q_preview = (pg_last_session)->video_shm_q_preview;
  1806. }
  1807. }
  1808. char strPath[MAX_PATH]={0};
  1809. char strImgPath[MAX_PATH]={0};
  1810. GetCurrentRunPath(strPath);
  1811. _snprintf(strImgPath, MAX_PATH,"%s\\bin\\error.jpg",strPath);
  1812. if (_access(strImgPath,0)!=-1)
  1813. {
  1814. IplImage*img = cvLoadImage(strImgPath,1);
  1815. if (img != NULL)
  1816. {
  1817. session->video_error = new videoq_frame;
  1818. session->video_error->format = VIDEOQ_FORMAT_RGB24;
  1819. session->video_error->framesize = 320*180*3;
  1820. session->video_error->height = 180;
  1821. session->video_error->width = 320;
  1822. session->video_error->data = new unsigned char[320*180*3];
  1823. memcpy(session->video_error->data,img->imageData,320*180*3);
  1824. cvReleaseImage(&img);
  1825. }
  1826. }
  1827. else
  1828. {
  1829. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Load error img Fail");
  1830. session->video_error = NULL;
  1831. }
  1832. //加载人形背景框
  1833. char strPersonPath[MAX_PATH] = {0};
  1834. _snprintf(strPersonPath, MAX_PATH,"%s\\bin\\rxk.jpg",strPath);
  1835. if (_access(strPersonPath,0)!=-1)
  1836. {
  1837. if (session->personimage == NULL)
  1838. {
  1839. session->personimage = cvLoadImage(strPersonPath);
  1840. }
  1841. if (session->personmask == NULL )
  1842. {
  1843. session->personmask = cvLoadImage(strPersonPath,0);
  1844. }
  1845. }
  1846. else
  1847. {
  1848. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Load person img Fail");
  1849. session->personimage = NULL;
  1850. session->personmask = NULL;
  1851. }
  1852. #if 0
  1853. video_frame frame;
  1854. video_frame_alloc(320, 180, VIDEO_FORMAT_RGB24, &frame);
  1855. video_frame_fill_black(&frame);
  1856. videoq_frame frm;
  1857. frm.data = frame.data[0];
  1858. session->video_shm_q_env->GetVideo(&frm, 0);
  1859. video_frame_save_bmpfile("d:\\a.bmp", &frame);
  1860. #endif
  1861. if (conf->camera_count == 2) {
  1862. session->video_shm_q_opt = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
  1863. }
  1864. *p_session = session;
  1865. }
  1866. return 0;
  1867. }
  1868. int video_session_start(video_session_t *session)
  1869. {
  1870. int rc = start_ui(session);
  1871. return rc;
  1872. }
  1873. void video_session_stop(video_session_t *session)
  1874. {
  1875. stop_ui(session);
  1876. }
  1877. void double_record_broadcast_video_session_stop()
  1878. {
  1879. if (NULL != pg_last_session)
  1880. {
  1881. if (NULL != pg_last_session->pic_record)
  1882. {
  1883. if (NULL != pg_last_session->pic_record->work_thread)
  1884. {
  1885. SetEvent(pg_last_session->pic_record->evt);
  1886. CloseHandle(pg_last_session->pic_record->work_thread);
  1887. pg_last_session->pic_record->work_thread = NULL;
  1888. CloseHandle(pg_last_session->pic_record->evt);
  1889. pg_last_session->pic_record->evt = NULL;
  1890. }
  1891. }
  1892. if (NULL != pg_last_session)
  1893. {
  1894. if (eGDI == pg_last_session->conf.eType) {
  1895. if (NULL != pg_last_session->local_hwnd){
  1896. BOOL bRet = PostMessageA(pg_last_session->local_hwnd, WM_CLOSE, 0, 0);
  1897. }
  1898. }
  1899. else{
  1900. if (NULL != pg_last_session->plocal_render){
  1901. pg_last_session->plocal_render->StopVideoRender();
  1902. }
  1903. }
  1904. DWORD dCode = WaitForSingleObject(pg_last_session->ui_thread, INFINITE);
  1905. CloseHandle(pg_last_session->ui_thread);
  1906. pg_last_session->ui_thread = NULL;
  1907. CloseHandle(pg_last_session->ui_event);
  1908. pg_last_session->ui_event = NULL;
  1909. if (eGDI == pg_last_session->conf.eType) {
  1910. pg_local_hwnd = NULL;
  1911. pg_remote_hwnd = NULL;
  1912. }
  1913. else{
  1914. pg_local_render = NULL;
  1915. pg_remote_render = NULL;
  1916. }
  1917. pg_last_session = NULL;
  1918. }
  1919. }
  1920. }
  1921. void video_session_destroy(video_session_t *session)
  1922. {
  1923. if (NULL != session) {
  1924. if (NULL != session->pic_record) {
  1925. if (NULL != session->pic_record->work_thread) {
  1926. SetEvent(session->pic_record->evt);
  1927. CloseHandle(session->pic_record->work_thread);
  1928. session->pic_record->work_thread = NULL;
  1929. CloseHandle(session->pic_record->evt);
  1930. session->pic_record->evt = NULL;
  1931. }
  1932. }
  1933. if (session->video_shm_q_env) {
  1934. delete session->video_shm_q_env;
  1935. session->video_shm_q_env = NULL;
  1936. }
  1937. if (session->video_shm_q_opt) {
  1938. delete session->video_shm_q_opt;
  1939. session->video_shm_q_opt = NULL;
  1940. }
  1941. if (session->video_shm_q_preview) {
  1942. delete session->video_shm_q_preview;
  1943. session->video_shm_q_preview = NULL;
  1944. }
  1945. }
  1946. if (session->video_error){
  1947. delete session->video_error->data;
  1948. delete session->video_error;
  1949. }
  1950. if (session->video_shm_q_remote) {
  1951. delete session->video_shm_q_remote;
  1952. session->video_shm_q_remote = NULL;
  1953. }
  1954. if (session->personimage){
  1955. cvReleaseImage(&session->personimage);
  1956. }
  1957. if (session->personmask){
  1958. cvReleaseImage(&session->personmask);
  1959. }
  1960. free(session);
  1961. }
  1962. void av_log_cb(void*ptr, int level, const char*fmt, va_list arg)
  1963. {
  1964. //int n = _vscprintf(fmt, arg);
  1965. //if (n > MAX_LOG_LEN) {
  1966. // char* buf = (char*)malloc((size_t)(n + 1));
  1967. // _vsnprintf(buf, n + 1, fmt, arg);
  1968. // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  1969. // free(buf);
  1970. //}
  1971. //else{
  1972. // char strlog[MAX_LOG_LEN] = {0};
  1973. // _vsnprintf(strlog, MAX_LOG_LEN, fmt, arg);
  1974. // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  1975. //}
  1976. }
  1977. int video_lib_init()
  1978. {
  1979. videoframework_init();
  1980. av_log_set_callback(&av_log_cb);
  1981. //av_log_set_level(AV_LOG_DEBUG);
  1982. av_log_set_level(AV_LOG_QUIET);
  1983. return 0;
  1984. }
  1985. void video_lib_deinit()
  1986. {
  1987. //CoUninitialize();
  1988. videoframework_term();
  1989. }