video_session.cpp 63 KB

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