video_session.cpp 78 KB

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