video_session.cpp 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385
  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. bool blocalrender;
  89. 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. Dbg("caution: shm_queue video insert shm video failed!");
  190. return Error_Unexpect;
  191. }
  192. else
  193. {
  194. //Dbg("shm_queue video insert shm video ok!");
  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. Dbg("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. Dbg("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) {
  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. Dbg("eStand2sType record call mod insert remote video to queue failed.");
  357. }
  358. //else {
  359. // Dbg("%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. Dbg("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. //Dbg("%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. //Dbg("%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. //Dbg("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. Dbg("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. //Dbg("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. Dbg("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) || (session->conf.eDeviceType == ePadtype) || (session->conf.eDeviceType == eDesk2SType) || (session->conf.eDeviceType == eDesk1SType) || (session->conf.eDeviceType == eDesk2SIntegratedType))
  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 (session->conf.eDeviceType == eMobilePadType)
  847. {
  848. config.fps_num = REC_COMMON_VIDEO_FPS_MOBILE_AGENT;
  849. }
  850. else if ((session->conf.eDeviceType == ePadtype) || (session->conf.eDeviceType == eDesk2SType) || (session->conf.eDeviceType == eDesk1SType) || (session->conf.eDeviceType == eDesk2SIntegratedType))
  851. {
  852. config.fps_num = REC_COMMON_VIDEO_FPS_MOBILE;
  853. }
  854. else if (session->conf.eDeviceType == eStand2sType)
  855. {
  856. config.fps_num = REC_COMMON_VIDEO_FPS_NUM;
  857. }
  858. config.capture_height = -1; // not use
  859. config.capture_width = -1; // not use
  860. config.dir = 3;
  861. config.tx_width = REC_COMMON_VIDEO_RTP_WIDTH;
  862. config.tx_height = REC_COMMON_VIDEO_RTP_HEIGHT;
  863. config.rx_width = session->conf.remote_video_width;
  864. config.rx_height = session->conf.remote_video_height;
  865. config.local_ip = session->conf.local_rtp_ip;
  866. config.local_pt = session->conf.local_pt;
  867. config.remote_pt = session->conf.local_pt;
  868. config.local_rtp_port = session->conf.local_rtp_port;
  869. config.mtu = session->conf.mtu;
  870. config.quant = session->conf.video_quant;
  871. config.remote_ip = session->conf.remote_rtp_ip;
  872. config.remote_rtp_port = session->conf.remote_rtp_port;
  873. config.user_data = session;
  874. config.bit_rate = session->conf.bit_rate;
  875. config.on_rx_frame = &on_rx_frame;
  876. config.on_rx_udp = &on_rx_udp;
  877. config.dbg = &__dbg;
  878. config.logevent = &__logevent;
  879. //config.fresh_time = session->conf.irvideo_ft;
  880. if (0 == videortp_create(&config, &session->rtp)) {
  881. Dbg("videortp create success!");
  882. }
  883. else {
  884. Dbg("videortp create failed!");
  885. }
  886. rc = videortp_start(session->rtp);
  887. if (rc != 0)
  888. {
  889. Dbg("start video rtp failed!");
  890. videortp_destroy(session->rtp);
  891. session->rtp = NULL;
  892. }
  893. else {
  894. Dbg("videortp_start suc.");
  895. }
  896. return rc;
  897. }
  898. static int start_local_video_clock(video_session_t* session)
  899. {
  900. int rc = -1;
  901. if (session->conf.eDeviceType == eMobilePadType){
  902. rc = videoclock_create(REC_COMMON_VIDEO_FPS_MOBILE, REC_COMMON_VIDEO_FPS_DEN,
  903. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_RGB24,
  904. &local_put_frame, session, &local_get_frame, session, &session->local_clock, session->conf.ref_Up_Fps, &__dbg);
  905. }
  906. else if ((session->conf.eDeviceType == ePadtype) || (session->conf.eDeviceType == eDesk2SType) || (session->conf.eDeviceType == eDesk1SType) || (session->conf.eDeviceType == eDesk2SIntegratedType))
  907. {
  908. rc = videoclock_create(REC_COMMON_VIDEO_FPS_MOBILE, REC_COMMON_VIDEO_FPS_DEN,
  909. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_RGB24,
  910. &local_put_frame, session, &local_get_frame, session, &session->local_clock, session->conf.ref_Up_Fps, &__dbg);
  911. }
  912. else if (session->conf.eDeviceType == eStand2sType)
  913. {
  914. rc = videoclock_create(REC_COMMON_VIDEO_FPS_NUM, REC_COMMON_VIDEO_FPS_DEN,
  915. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_RGB24,
  916. &local_put_frame, session, &local_get_frame, session, &session->local_clock, session->conf.ref_Up_Fps, &__dbg);
  917. }
  918. Dbg("create session->local_clock addr is 0x%08x", session->local_clock);
  919. if (0 == rc) {
  920. rc = videoclock_start(session->local_clock);
  921. if (rc != 0) {
  922. Dbg("start video clock failed!");
  923. videoclock_destroy(session->local_clock);
  924. session->local_clock = NULL;
  925. }
  926. else {
  927. Dbg("videoclock start success!");
  928. }
  929. }
  930. else {
  931. Dbg("create video clock failed!");
  932. }
  933. return rc;
  934. }
  935. static int start_video(video_session_t *session)
  936. {
  937. LOG_FUNCTION();
  938. int rc = -1;
  939. if (session->video_shm_q_env)
  940. {
  941. session->local_encode_sws_ctx_env = sws_getContext(
  942. REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, PIX_FMT_BGR24,
  943. REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, PIX_FMT_YUV420P,
  944. SWS_POINT, NULL, NULL, NULL);
  945. if (!session->local_encode_sws_ctx_env)
  946. goto on_error;
  947. else{
  948. Dbg("get session->local_encode_sws_ctx_env succ.");
  949. }
  950. }
  951. else{
  952. Dbg("session->video_shm_q_env is NULL.");
  953. }
  954. if (session->video_shm_q_opt)
  955. {
  956. session->local_encode_sws_ctx_opt = sws_getContext(
  957. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, PIX_FMT_BGR24,
  958. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, PIX_FMT_YUV420P,
  959. SWS_POINT, NULL, NULL, NULL);
  960. if (!session->local_encode_sws_ctx_opt)
  961. goto on_error;
  962. else{
  963. Dbg("get session->local_encode_sws_ctx_opt succ.");
  964. }
  965. }
  966. else{
  967. Dbg("session->video_shm_q_opt is NULL.");
  968. }
  969. //Dbg("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);
  970. #ifdef RVC_OS_WIN
  971. if (DOUBLERECORD_CALLTYPE != session->conf.nCallType){
  972. Dbg("create new remote video player");
  973. if (session->remote_hwnd)
  974. {
  975. if (session->conf.eDeviceType == eMobilePadType)
  976. {
  977. rc = videoplayer_create(session->remote_hwnd,
  978. 0,
  979. 0,
  980. session->conf.remote_video_view_cx,
  981. session->conf.remote_video_view_cy,
  982. REC_COMMON_VIDEO_FPS_MOBILE_AGENT,
  983. REC_COMMON_VIDEO_FPS_DEN,
  984. session->conf.remote_video_width,
  985. session->conf.remote_video_height,
  986. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP,
  987. session->conf.iremote_wind_flags,
  988. "remote",
  989. on_remoteWinstate,
  990. NULL,
  991. session,
  992. &session->remote_player);
  993. if (rc != 0)
  994. {
  995. Dbg("create remote video player failed!");
  996. goto on_error;
  997. }
  998. else{
  999. Dbg("create remote video player suc!");
  1000. }
  1001. }
  1002. else if((session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  1003. {
  1004. rc = videoplayer_create(session->remote_hwnd,
  1005. 0,
  1006. 0,
  1007. session->conf.remote_video_view_cx,
  1008. session->conf.remote_video_view_cy,
  1009. REC_COMMON_VIDEO_FPS_MOBILE,
  1010. REC_COMMON_VIDEO_FPS_DEN,
  1011. session->conf.remote_video_width,
  1012. session->conf.remote_video_height,
  1013. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP,
  1014. session->conf.iremote_wind_flags,
  1015. "remote",
  1016. on_remoteWinstate,
  1017. NULL,
  1018. session,
  1019. &session->remote_player);
  1020. if (rc != 0) {
  1021. Dbg("create remote video player failed!");
  1022. goto on_error;
  1023. }
  1024. else{
  1025. Dbg("create remote video player suc!");
  1026. }
  1027. }
  1028. else if(session->conf.eDeviceType == eStand2sType)
  1029. {
  1030. rc = videoplayer_create(session->remote_hwnd,
  1031. 0,
  1032. 0,
  1033. session->conf.remote_video_view_cx,
  1034. session->conf.remote_video_view_cy,
  1035. REC_COMMON_VIDEO_FPS_NUM,
  1036. REC_COMMON_VIDEO_FPS_DEN,
  1037. session->conf.remote_video_width,
  1038. session->conf.remote_video_height,
  1039. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP,
  1040. session->conf.iremote_wind_flags,
  1041. "remote",
  1042. on_remoteWinstate,
  1043. NULL,
  1044. session,
  1045. &session->remote_player);
  1046. if (rc != 0)
  1047. {
  1048. Dbg("create remote video player failed!");
  1049. goto on_error;
  1050. }
  1051. else{
  1052. Dbg("create remote video player suc remote_video_width = %d,remote_video_height = %d.", session->conf.remote_video_width, session->conf.remote_video_height);
  1053. }
  1054. }
  1055. }
  1056. else
  1057. {
  1058. Dbg("remote_hwnd == Null");
  1059. }
  1060. }
  1061. else{
  1062. if (NULL != pg_last_session){
  1063. session->remote_player = pg_last_session->remote_player;
  1064. Dbg("double record call remote_player reuse.");
  1065. }
  1066. else{
  1067. Dbg("pg_last_session is NULL, and call type is %d.", session->conf.nCallType);
  1068. }
  1069. }
  1070. BOOL bIsActiveInspect = FALSE;
  1071. if (session->conf.ref_Is_ActiveInspect != NULL)
  1072. {
  1073. if (*session->conf.ref_Is_ActiveInspect == 1)
  1074. {
  1075. bIsActiveInspect = TRUE;
  1076. }
  1077. else
  1078. {
  1079. bIsActiveInspect = FALSE;
  1080. }
  1081. }
  1082. else
  1083. {
  1084. bIsActiveInspect = FALSE;
  1085. }
  1086. if (DOUBLERECORD_CALLTYPE != session->conf.nCallType){
  1087. Dbg("create new local video player");
  1088. if (session->conf.eDeviceType == eMobilePadType)
  1089. {
  1090. rc = videoplayer_create(session->local_hwnd,
  1091. 0,
  1092. 0,
  1093. session->conf.local_video_view_cx,
  1094. session->conf.local_video_view_cy,
  1095. REC_COMMON_VIDEO_FPS_MOBILE,
  1096. REC_COMMON_VIDEO_FPS_DEN,
  1097. REC_COMMON_VIDEO_PREVIEW_WIDTH,
  1098. REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  1099. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP,
  1100. session->conf.ilocal_wind_flags,
  1101. "local",
  1102. on_pull, free_frame, session,
  1103. &session->local_player);
  1104. if (rc != 0)
  1105. {
  1106. Dbg("create local video player failed!");
  1107. goto on_error;
  1108. }
  1109. else{
  1110. Dbg("create local video player suc!");
  1111. }
  1112. }
  1113. else if((session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  1114. {
  1115. rc = videoplayer_create(session->local_hwnd,
  1116. 0,
  1117. 0,
  1118. session->conf.local_video_view_cx,
  1119. session->conf.local_video_view_cy,
  1120. REC_COMMON_VIDEO_FPS_MOBILE,
  1121. REC_COMMON_VIDEO_FPS_DEN,
  1122. REC_COMMON_VIDEO_PREVIEW_WIDTH,
  1123. REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  1124. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP,
  1125. session->conf.ilocal_wind_flags,
  1126. "local",
  1127. on_pull, free_frame, session,
  1128. &session->local_player);
  1129. if (rc != 0)
  1130. {
  1131. Dbg("create local video player failed!");
  1132. goto on_error;
  1133. }
  1134. else{
  1135. Dbg("create local video player suc!");
  1136. }
  1137. }
  1138. else if(session->conf.eDeviceType == eStand2sType)
  1139. {
  1140. rc = videoplayer_create(session->local_hwnd,
  1141. 0,
  1142. 0,
  1143. session->conf.local_video_view_cx,
  1144. session->conf.local_video_view_cy,
  1145. REC_COMMON_VIDEO_FPS_NUM,
  1146. REC_COMMON_VIDEO_FPS_DEN,
  1147. bIsActiveInspect?REC_COMMON_VIDEO_SNAPSHOT_WIDTH:REC_COMMON_VIDEO_PREVIEW_WIDTH,
  1148. bIsActiveInspect?REC_COMMON_VIDEO_SNAPSHOT_WIDTH:REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  1149. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP,
  1150. session->conf.ilocal_wind_flags,
  1151. "local",
  1152. on_pull, free_frame, session,
  1153. &session->local_player);
  1154. if (rc != 0)
  1155. {
  1156. Dbg("create local video player failed!");
  1157. goto on_error;
  1158. }
  1159. else{
  1160. Dbg("create local video player suc!");
  1161. }
  1162. }
  1163. }
  1164. else{
  1165. if (NULL != pg_last_session){
  1166. session->local_player = pg_last_session->local_player;
  1167. Dbg("double record call local player reuse.");
  1168. }
  1169. }
  1170. if (session->remote_hwnd)
  1171. {
  1172. if (0 != session->conf.remote_rtp_port)
  1173. {
  1174. //videortp_config_t config = {0};
  1175. //config.fps_den = REC_COMMON_VIDEO_FPS_DEN;
  1176. //if (session->conf.eDeviceType == eMobilePadType)
  1177. //{
  1178. // config.fps_num = REC_COMMON_VIDEO_FPS_MOBILE_AGENT;
  1179. //}
  1180. //else if((session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  1181. //{
  1182. // config.fps_num = REC_COMMON_VIDEO_FPS_MOBILE;
  1183. //}
  1184. //else if(session->conf.eDeviceType == eStand2sType)
  1185. //{
  1186. // config.fps_num = REC_COMMON_VIDEO_FPS_NUM;
  1187. //}
  1188. //config.capture_height = -1; // not use
  1189. //config.capture_width = -1; // not use
  1190. //config.dir = 3;
  1191. //config.tx_width = REC_COMMON_VIDEO_RTP_WIDTH;
  1192. //config.tx_height = REC_COMMON_VIDEO_RTP_HEIGHT;
  1193. //config.rx_width = session->conf.remote_video_width;
  1194. //config.rx_height = session->conf.remote_video_height;
  1195. //config.local_ip = session->conf.local_rtp_ip;
  1196. //config.local_pt = session->conf.local_pt;
  1197. //config.remote_pt = session->conf.local_pt;
  1198. //config.local_rtp_port = session->conf.local_rtp_port;
  1199. //config.mtu = session->conf.mtu;
  1200. //config.quant = session->conf.video_quant;
  1201. //config.remote_ip = session->conf.remote_rtp_ip;
  1202. //config.remote_rtp_port = session->conf.remote_rtp_port;
  1203. //config.user_data = session;
  1204. //config.bit_rate = session->conf.bit_rate;
  1205. //config.on_rx_frame = &on_rx_frame;
  1206. //config.dbg = &__dbg;
  1207. //videortp_create(&config, &session->rtp);
  1208. //rc = videortp_start(session->rtp);
  1209. //if (rc != 0)
  1210. //{
  1211. // Dbg("start video rtp failed!");
  1212. // videortp_destroy(session->rtp);
  1213. // session->rtp = NULL;
  1214. // goto on_error;
  1215. //}
  1216. //else{
  1217. // Dbg("videortp_start suc.");
  1218. //}
  1219. if (0 != start_video_rtpsession(session)) {
  1220. goto on_error;
  1221. }
  1222. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType){
  1223. if (NULL != pg_last_session){
  1224. pg_last_session->rtp = session->rtp;
  1225. }
  1226. }
  1227. //if (session->conf.eDeviceType == eMobilePadType)
  1228. //{
  1229. // rc = videoclock_create(REC_COMMON_VIDEO_FPS_MOBILE, REC_COMMON_VIDEO_FPS_DEN,
  1230. // REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_RGB24,
  1231. // &local_put_frame, session, &local_get_frame, session, &session->local_clock, session->conf.ref_Up_Fps);
  1232. //}
  1233. //else if((session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  1234. //{
  1235. // rc = videoclock_create(REC_COMMON_VIDEO_FPS_MOBILE, REC_COMMON_VIDEO_FPS_DEN,
  1236. // REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_RGB24,
  1237. // &local_put_frame, session, &local_get_frame, session, &session->local_clock, session->conf.ref_Up_Fps);
  1238. //}
  1239. //else if(session->conf.eDeviceType == eStand2sType)
  1240. //{
  1241. // rc = videoclock_create(REC_COMMON_VIDEO_FPS_NUM, REC_COMMON_VIDEO_FPS_DEN,
  1242. // REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_RGB24,
  1243. // &local_put_frame, session, &local_get_frame, session, &session->local_clock, session->conf.ref_Up_Fps);
  1244. //}
  1245. //Dbg("create session->local_clock addr is 0x%08x", session->local_clock);
  1246. //
  1247. //if (rc != 0) {
  1248. // Dbg("create video clock failed!");
  1249. // goto on_error;
  1250. //}
  1251. //rc = videoclock_start(session->local_clock);
  1252. //if (rc != 0) {
  1253. // Dbg("start video clock failed!");
  1254. // videoclock_destroy(session->local_clock);
  1255. // session->local_clock = NULL;
  1256. // goto on_error;
  1257. //}
  1258. if (0 != start_local_video_clock(session)) {
  1259. goto on_error;
  1260. }
  1261. }
  1262. else{
  1263. pg_last_session = session; //保存session信息
  1264. Dbg("begin show agent static picture.");
  1265. show_remote_agnet_picture(session);
  1266. }
  1267. }
  1268. #else
  1269. rc = start_video_rtpsession(session);
  1270. if (0 != rc) {
  1271. goto on_error;
  1272. }
  1273. rc = start_local_video_clock(session);
  1274. if (0 != rc) {
  1275. goto on_error;
  1276. }
  1277. #endif // RVC_OS_WIN
  1278. on_error:
  1279. return rc;
  1280. }
  1281. static void stop_video(video_session_t *session)
  1282. {
  1283. LOG_FUNCTION();
  1284. #ifdef RVC_OS_WIN
  1285. Dbg("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);
  1286. if (session->local_clock && session->remote_hwnd)
  1287. {
  1288. Dbg("videoclock_stop local_clock addr is 0x%08x", session->local_clock);
  1289. videoclock_stop(session->local_clock);
  1290. videoclock_destroy(session->local_clock);
  1291. session->local_clock = NULL;
  1292. pg_remote_hwnd = NULL;
  1293. pg_local_hwnd = NULL;
  1294. }
  1295. #else
  1296. if (session->local_clock)
  1297. {
  1298. Dbg("videoclock_stop local_clock addr is 0x%08x", session->local_clock);
  1299. videoclock_stop(session->local_clock);
  1300. videoclock_destroy(session->local_clock);
  1301. session->local_clock = NULL;
  1302. }
  1303. #endif // RVC_OS_WIN
  1304. if (session->rtp){
  1305. Dbg("begin stop video rtp.");
  1306. videortp_stop(session->rtp);
  1307. videortp_destroy(session->rtp);
  1308. session->rtp = NULL;
  1309. }
  1310. else {
  1311. Dbg("session->rtp == null");
  1312. }
  1313. #ifdef RVC_OS_WIN
  1314. if (session->local_player) {
  1315. videoplayer_destroy(session->local_player);
  1316. session->local_player = NULL;
  1317. Dbg("local video player destroy.");
  1318. }
  1319. if (session->remote_player && session->remote_hwnd)
  1320. {
  1321. videoplayer_destroy(session->remote_player);
  1322. session->remote_player = NULL;
  1323. Dbg("remote video player destroy.");
  1324. }
  1325. #else
  1326. #endif
  1327. if (session->local_encode_sws_ctx_env) {
  1328. sws_freeContext(session->local_encode_sws_ctx_env);
  1329. session->local_encode_sws_ctx_env = NULL;
  1330. }
  1331. if (session->local_encode_sws_ctx_opt) {
  1332. sws_freeContext(session->local_encode_sws_ctx_opt);
  1333. session->local_encode_sws_ctx_opt = NULL;
  1334. }
  1335. }
  1336. #ifdef RVC_OS_WIN
  1337. static bool ReMoveOutOfScreenVideoWindow(HWND hWnd, RECT rect)
  1338. {
  1339. bool bret = false;
  1340. int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  1341. int iScreenHight = GetSystemMetrics(SM_CYSCREEN);
  1342. Dbg("ScreenWidth = %d, ScreenHight = %d.", iScreenWidth, iScreenHight);
  1343. int iVideoWidth = rect.right - rect.left;
  1344. int iVideoHight = rect.bottom - rect.top;
  1345. if (rect.left < 0) {
  1346. if (rect.bottom <= iScreenHight) {
  1347. MoveWindow(hWnd, 0, rect.top, iVideoWidth, iVideoHight, TRUE);
  1348. }
  1349. else {
  1350. MoveWindow(hWnd, 0, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1351. }
  1352. bret = true;
  1353. }
  1354. if (rect.right > iScreenWidth) {
  1355. if (rect.bottom <= iScreenHight) {
  1356. MoveWindow(hWnd, iScreenWidth - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1357. }
  1358. else {
  1359. MoveWindow(hWnd, iScreenWidth - iVideoWidth, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1360. }
  1361. bret = true;
  1362. }
  1363. if (rect.bottom > iScreenHight) {
  1364. if (rect.left >= 0 && rect.right <= iScreenWidth) {
  1365. MoveWindow(hWnd, rect.left, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1366. bret = true;
  1367. }
  1368. }
  1369. return bret;
  1370. }
  1371. static bool ReMoveCenterOtherVideoWindow(HWND hWnd, RECT rect, RECT otherect)
  1372. {
  1373. bool bret = false;
  1374. int iVideoWidth = rect.right - rect.left;
  1375. int iVideoHight = rect.bottom - rect.top;
  1376. int iOtherVideoWidth = otherect.right - otherect.left;
  1377. int iOtherVideoHight = otherect.bottom - otherect.top;
  1378. if (iVideoWidth > iOtherVideoHight || iVideoHight > iOtherVideoHight) {
  1379. return bret;
  1380. }
  1381. if (rect.left > otherect.left + iVideoWidth && rect.right < otherect.right) {
  1382. if (rect.top > otherect.top && rect.bottom < otherect.bottom) {
  1383. if (otherect.right - rect.right <= rect.left - otherect.left) {
  1384. MoveWindow(hWnd, otherect.right - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1385. }
  1386. else {
  1387. MoveWindow(hWnd, otherect.left, rect.top, iVideoWidth, iVideoHight, TRUE);
  1388. }
  1389. bret = true;
  1390. }
  1391. }
  1392. if (rect.right < otherect.right - iVideoWidth && rect.left > otherect.left) {
  1393. if (rect.top > otherect.top && rect.bottom < otherect.bottom) {
  1394. if (otherect.right - rect.right <= rect.left - otherect.left) {
  1395. MoveWindow(hWnd, otherect.right - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1396. }
  1397. else {
  1398. MoveWindow(hWnd, otherect.left, rect.top, iVideoWidth, iVideoHight, TRUE);
  1399. }
  1400. bret = true;
  1401. }
  1402. }
  1403. return bret;
  1404. }
  1405. static bool ReMoveVideoWindow(HWND hWnd, RECT rect, RECT otherect)
  1406. {
  1407. LOG_FUNCTION();
  1408. bool bret = false;
  1409. bret = ReMoveOutOfScreenVideoWindow(hWnd, rect);
  1410. bret = ReMoveCenterOtherVideoWindow(hWnd, rect, otherect);
  1411. return bret;
  1412. }
  1413. static int HandleVideoMoveEvent(int iMessageType, HWND hWnd, video_session_t* pSession)
  1414. {
  1415. LOG_FUNCTION();
  1416. int iRet = -1;
  1417. if (NULL == pSession || NULL == hWnd) {
  1418. return iRet;
  1419. }
  1420. HWND hOtherWnd = NULL;
  1421. //本地和远端标识 1为本地,2为远端
  1422. int iVideoType = 2;
  1423. if (hWnd == pSession->local_hwnd) {
  1424. iVideoType = 1;
  1425. hOtherWnd = pSession->remote_hwnd;
  1426. }
  1427. else {
  1428. hOtherWnd = pSession->local_hwnd;
  1429. }
  1430. if (NULL == hOtherWnd) {
  1431. return iRet;
  1432. }
  1433. bool bMoved = false;
  1434. RECT rect;
  1435. GetWindowRect(hWnd, &rect);
  1436. Dbg("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);
  1437. RECT otherect;
  1438. GetWindowRect(hOtherWnd, &otherect);
  1439. Dbg("other video Window left = %d, right = %d, bottom = %d, top = %d", otherect.left, otherect.right, otherect.bottom, otherect.top);
  1440. bMoved = ReMoveVideoWindow(hWnd, rect, otherect);
  1441. if (bMoved) {
  1442. Dbg("has removed video window.");
  1443. GetWindowRect(hWnd, &rect);
  1444. }
  1445. if (NULL != pSession->conf.video_echo_cb && NULL != pSession->conf.video_echo_cb->on_video_box_move) {
  1446. pSession->conf.video_echo_cb->on_video_box_move(iMessageType, iVideoType, rect.left, rect.bottom, pSession->conf.video_echo_cb->user_data);
  1447. iRet = 0;
  1448. }
  1449. return iRet;
  1450. }
  1451. #else
  1452. #endif // RVC_OS_WIN
  1453. #ifdef RVC_OS_WIN
  1454. static LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  1455. {
  1456. video_session_t* session = (video_session_t*)GetWindowLongPtrA(hWnd, GWLP_USERDATA);
  1457. switch (msg) {
  1458. case WM_DESTROY:
  1459. Dbg("WndProc session addr is 0x%08x, and receive WM_DESTROY.", session);
  1460. if (session->local_hwnd == hWnd) {
  1461. Dbg("session->local_hwnd = 0");
  1462. session->local_hwnd = 0;
  1463. }
  1464. else if (session->remote_hwnd == hWnd) {
  1465. Dbg("session->remote_hwnd = 0");
  1466. session->remote_hwnd = 0;
  1467. }
  1468. if (session->local_hwnd == 0 && session->remote_hwnd == 0) {
  1469. Dbg("*****PostQuitMessage****");
  1470. PostQuitMessage(0);
  1471. }
  1472. return 0;
  1473. case WM_NCHITTEST:
  1474. if (((session->local_hwnd == hWnd) && (0 != session->conf.local_move)) || ((session->remote_hwnd == hWnd) && (0 != session->conf.remote_move))) {
  1475. return HTCAPTION;
  1476. }
  1477. else {
  1478. break;
  1479. }
  1480. case WM_ACTIVATE:
  1481. case WM_TOUCH:
  1482. case WM_GESTURE:
  1483. ReleaseCapture();
  1484. return 0;
  1485. #if 1
  1486. case WM_WINDOWPOSCHANGED:
  1487. {
  1488. LPWINDOWPOS pPos = (LPWINDOWPOS)lParam;
  1489. if (pPos->hwndInsertAfter != HWND_TOPMOST && pPos->hwndInsertAfter != HWND_TOP) {
  1490. //BringWindowToTop(hWnd);
  1491. SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  1492. }
  1493. else {
  1494. return DefWindowProc(hWnd, msg, wParam, lParam);
  1495. }
  1496. }
  1497. return 0;
  1498. //return DefWindowProc(hWnd, msg, wParam, lParam);
  1499. #endif
  1500. case WM_MBUTTONDOWN:
  1501. case WM_MBUTTONUP:
  1502. case WM_MBUTTONDBLCLK:
  1503. case WM_LBUTTONDBLCLK:
  1504. case WM_LBUTTONDOWN:
  1505. case WM_LBUTTONUP:
  1506. OutputDebugStringA("mouse clicked!");
  1507. if (session) {
  1508. if (session->local_hwnd) {
  1509. //ReleaseCapture();
  1510. }
  1511. }
  1512. return 0;
  1513. case WM_CLOSE:
  1514. Dbg("WndProc session addr is 0x%08x, and receive WM_CLOSE", session);
  1515. stop_video(session);
  1516. Dbg("WndProc SetEvent session addr is 0x%08x.", session);
  1517. //SetEvent(session->ui_thread);
  1518. DestroyWindow(session->local_hwnd);
  1519. DestroyWindow(session->remote_hwnd);
  1520. return 0;
  1521. case WM_MOVE:
  1522. break;
  1523. case WM_ENTERSIZEMOVE:
  1524. {
  1525. Dbg("***WM_ENTERSIZEMOVE***");
  1526. HandleVideoMoveEvent(0, hWnd, session);
  1527. }
  1528. break;
  1529. case WM_EXITSIZEMOVE:
  1530. {
  1531. Dbg("***WM_EXITSIZEMOVE***");
  1532. HandleVideoMoveEvent(1, hWnd, session);
  1533. }
  1534. break;
  1535. default:
  1536. return DefWindowProc(hWnd, msg, wParam, lParam);
  1537. }
  1538. }
  1539. #endif //
  1540. #ifdef RVC_OS_WIN
  1541. static unsigned int __stdcall ui_proc(void *arg)
  1542. {
  1543. video_session_t *session = (video_session_t*)arg;
  1544. Dbg("ui_proc session addr is 0x%08x", session);
  1545. WNDCLASSA wc = {0};
  1546. ATOM a = 0;
  1547. HWND hWnd = NULL;
  1548. MSG msg;
  1549. HINSTANCE hInst = ModuleBase::GetModuleBase()->GetInstance();
  1550. bool breuse_local_wnd = false;
  1551. bool breuse_remote_wnd = false;
  1552. DWORD dLocal_style = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_NOACTIVATE;
  1553. DWORD dRemote_style = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_NOACTIVATE;
  1554. //双录两阶段视频窗口复用
  1555. if (NULL != pg_local_hwnd){
  1556. session->local_hwnd = pg_local_hwnd;
  1557. //SetWindowLongPtrA(session->local_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1558. breuse_local_wnd = true;
  1559. Dbg("local video hwnd reuse.");
  1560. }
  1561. else{
  1562. Dbg("create local video hwnd and x = %d, y = %d, width = %d, height = %d.",
  1563. session->conf.local_video_view_x, session->conf.local_video_view_y, session->conf.local_video_view_cx, session->conf.local_video_view_cy);
  1564. Dbg("local video move flag is %d, remote video move flag is %d.", session->conf.local_move, session->conf.remote_move);
  1565. if (0 != session->conf.local_move){
  1566. dLocal_style -= WS_EX_NOACTIVATE;
  1567. }
  1568. if (0 != session->conf.remote_move){
  1569. dRemote_style -= WS_EX_NOACTIVATE;
  1570. }
  1571. CoInitialize(0);
  1572. wc.cbClsExtra = 0;
  1573. wc.cbWndExtra = 0;
  1574. wc.hInstance = hInst;
  1575. wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  1576. wc.hCursor = NULL;
  1577. wc.hIcon = NULL;
  1578. wc.lpfnWndProc = &WndProc;
  1579. wc.lpszClassName = WNDCLS_NAME;
  1580. wc.style = CS_HREDRAW | CS_OWNDC | CS_VREDRAW;
  1581. a = RegisterClassA(&wc);
  1582. if (a == 0)
  1583. return 0;
  1584. session->local_hwnd = CreateWindowExA(dLocal_style,
  1585. WNDCLS_NAME, NULL, WS_POPUP|WS_VISIBLE,
  1586. session->conf.local_video_view_x, session->conf.local_video_view_y, session->conf.local_video_view_cx, session->conf.local_video_view_cy,
  1587. NULL, NULL, hInst, NULL);
  1588. if (session->local_hwnd) {
  1589. SetWindowLongPtrA(session->local_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1590. if (0 == session->conf.local_move){
  1591. SetWindowPos(session->local_hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
  1592. }
  1593. pg_local_hwnd = session->local_hwnd;
  1594. Dbg("restore local video hwnd");
  1595. }
  1596. }
  1597. 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)
  1598. {
  1599. if (NULL != pg_remote_hwnd){
  1600. session->remote_hwnd = pg_remote_hwnd;
  1601. //SetWindowLongPtrA(session->remote_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1602. breuse_remote_wnd = true;
  1603. Dbg("remote video hwnd reuse.");
  1604. if (NULL != pg_last_session){
  1605. Dbg("SetEvent pg_last_session->ui_event and ui_event addr is %8x.", pg_last_session->ui_event);
  1606. record_agent_picture_show_session_destory(pg_last_session);
  1607. }
  1608. }
  1609. else{
  1610. Dbg("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);
  1611. session->remote_hwnd = CreateWindowExA(dRemote_style,
  1612. WNDCLS_NAME, NULL, WS_POPUP|WS_VISIBLE,
  1613. session->conf.remote_video_view_x, session->conf.remote_video_view_y, session->conf.remote_video_view_cx, session->conf.remote_video_view_cy,
  1614. NULL, NULL, hInst, NULL);
  1615. if (session->remote_hwnd) {
  1616. SetWindowLongPtrA(session->remote_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1617. if (0 == session->conf.remote_move){
  1618. SetWindowPos(session->remote_hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
  1619. }
  1620. pg_remote_hwnd = session->remote_hwnd;
  1621. Dbg("restore remote video hwnd.");
  1622. }
  1623. }
  1624. }
  1625. else
  1626. {
  1627. Dbg("remote_hwnd == Null");
  1628. session->remote_hwnd = NULL;
  1629. }
  1630. if (session->local_hwnd/* && session->remote_hwnd*/)
  1631. {
  1632. int rc = 0;
  1633. ShowCursor(FALSE);
  1634. Dbg("SetEvent1 session addr is 0x%08x.", session);
  1635. SetEvent(session->ui_event);
  1636. rc = start_video(session);
  1637. if (rc != 0)
  1638. {
  1639. Dbg("start video failed!");
  1640. }
  1641. else
  1642. {
  1643. Dbg("start video ok!");
  1644. }
  1645. if (!breuse_local_wnd && !breuse_remote_wnd)
  1646. {
  1647. while (GetMessageA(&msg, NULL, NULL, NULL))
  1648. {
  1649. if (msg.message == WM_CLOSE) {
  1650. Dbg("session addr is 0x%08x, session->local_hwnd is 0x%08x, rx WM_CLOSE", session, session->local_hwnd);
  1651. }
  1652. TranslateMessage(&msg);
  1653. DispatchMessageA(&msg);
  1654. }
  1655. Dbg("SetEvent2 session addr is 0x%08x.", session);
  1656. SetEvent(session->ui_event);
  1657. }
  1658. }
  1659. if (a)
  1660. UnregisterClassA(WNDCLS_NAME, hInst);
  1661. CoUninitialize();
  1662. return 0;
  1663. }
  1664. #else
  1665. static void __video_render_log(void* user_data, const char* fmt, va_list arg)
  1666. {
  1667. vDbg(fmt, arg);
  1668. }
  1669. //void* videorender_func(void* arg)
  1670. //{
  1671. // LOG_FUNCTION();
  1672. // video_session_t* session = (video_session_t*)arg;
  1673. // Dbg("%s:%d session addr is 0x%08x, session->plocal_render = 0x%08x", __FUNCTION__, __LINE__, session, session->plocal_render);
  1674. // char str_local_ip[MAX_PATH_SIZE] = { 0 };
  1675. // char str_remote_ip[MAX_PATH_SIZE] = { 0 };
  1676. // translate_ipaddr_from_int(str_local_ip, MAX_PATH_SIZE, session->conf.local_rtp_ip);
  1677. // translate_ipaddr_from_int(str_remote_ip, MAX_PATH_SIZE, session->conf.remote_rtp_ip);
  1678. // int ilocal_video_fresh_time = session->conf.ilvideo_ft;
  1679. // Dbg("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);
  1680. // Dbg("videorender_func remote_pt: %d remote_ip: %s remote_rtp_port: %d.", session->conf.remote_pt, str_remote_ip, session->conf.remote_rtp_port);
  1681. // Dbg("videorender_func rx_width: %d rx_height: %d.", session->conf.remote_video_width, session->conf.remote_video_height);
  1682. // Dbg("%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,
  1683. // session->conf.remote_video_view_x, session->conf.remote_video_view_y, session->conf.remote_video_view_cx, session->conf.remote_video_view_cy);
  1684. //
  1685. // videorender_callback_t t_callback = { 0 };
  1686. // t_callback.debug = &__video_render_log;
  1687. // session->plocal_render = CreateVideoRenderObj(&t_callback);
  1688. // if (NULL != session->plocal_render){
  1689. // videorender_param_t tparam = { 0 };
  1690. // tparam.icx = session->conf.local_video_view_x;
  1691. // tparam.icy = session->conf.local_video_view_y;
  1692. // tparam.uwidth = session->conf.local_video_view_cx;
  1693. // tparam.uheight = session->conf.local_video_view_cy;
  1694. // tparam.ivideoformat = VIDEO_FORMAT_RGB24;
  1695. // LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_LOCAL_VIDEO_RENDER_CREATE, "local video render create.");
  1696. // if (0 == session->plocal_render->VideoRenderSetParam(&tparam)){
  1697. // session->plocal_render->HideVideoWindow();
  1698. // }
  1699. // else {
  1700. // Dbg("%s:%d set video render param failed!", __FUNCTION__, __LINE__);
  1701. // return 0;
  1702. // }
  1703. // }
  1704. //
  1705. // 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){
  1706. // Dbg("%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);
  1707. // session->premote_render = CreateVideoRenderObj(&t_callback);
  1708. // if (session->premote_render){
  1709. // videorender_param_t tparam_remote = { 0 };
  1710. // tparam_remote.icx = session->conf.remote_video_view_x;
  1711. // tparam_remote.icy = session->conf.remote_video_view_y;
  1712. // tparam_remote.uwidth = session->conf.remote_video_view_cx;
  1713. // tparam_remote.uheight = session->conf.remote_video_view_cy;
  1714. // tparam_remote.ivideoformat = VIDEO_FORMAT_RGB24;
  1715. // if (0 == session->premote_render->VideoRenderSetParam(&tparam_remote)) {
  1716. // //session->premote_render->ShowVideoWindow();
  1717. // }
  1718. // LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_REMOTE_VIDEO_RENDER_CREATE, "remote video render create.");
  1719. // }
  1720. // else {
  1721. // Dbg("session remote video render is null.");
  1722. // }
  1723. // }
  1724. //
  1725. // if (session->plocal_render){
  1726. // start_video(session);
  1727. // bool bshow_local = false;
  1728. // bool bset = true;
  1729. //
  1730. // session->plocal_render->StartVideoRender();
  1731. // for (; ; ) {
  1732. // struct timespec ts;
  1733. // clock_gettime(CLOCK_REALTIME, &ts);
  1734. // long unsec = ts.tv_nsec + (1000 * 1000 * ilocal_video_fresh_time);
  1735. // ts.tv_sec += (unsec / 1000000000);
  1736. // ts.tv_nsec = (unsec % 1000000000);
  1737. // if (0 != sem_timedwait(&session->ui_stop_sem, &ts) && (ETIMEDOUT == errno))
  1738. // {
  1739. // video_frame* local_video_frame = NULL;
  1740. // Dbg("%s:%d", __FUNCTION__, __LINE__);
  1741. // int iwindowstate = get_local_video_frame(session, &local_video_frame);
  1742. // Dbg("%s:%d", __FUNCTION__, __LINE__);
  1743. // if (iwindowstate != session->ilast_windstae){
  1744. // set_video_windows(session, iwindowstate);
  1745. // session->ilast_windstae = iwindowstate;
  1746. // Dbg("%s:%d, window state is %d.", __FUNCTION__, __LINE__, iwindowstate);
  1747. // }
  1748. //
  1749. // if (NULL != local_video_frame) {
  1750. // Dbg("%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);
  1751. // video_frame* localframe = NULL;
  1752. // Dbg("%s:%d", __FUNCTION__, __LINE__);
  1753. // if (0 == translate_image_resolution(&localframe, session->conf.local_video_view_cx, session->conf.local_video_view_cy, local_video_frame)) {
  1754. // Dbg("%s:%d", __FUNCTION__, __LINE__);
  1755. // session->plocal_render->RenderVideoFrame(localframe, RVC_FLIP_VERTICAL);
  1756. // Dbg("%s:%d", __FUNCTION__, __LINE__);
  1757. // video_frame_delete(localframe);
  1758. // Dbg("%s:%d", __FUNCTION__, __LINE__);
  1759. // localframe = NULL;
  1760. // }
  1761. // else {
  1762. // Dbg("%s:%d", __FUNCTION__, __LINE__);
  1763. // session->plocal_render->RenderVideoFrame(local_video_frame, RVC_FLIP_VERTICAL);
  1764. // Dbg("%s:%d", __FUNCTION__, __LINE__);
  1765. // }
  1766. //
  1767. // if (false == session->blocalrender){
  1768. // LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_LOCAL_VIDEO_RENDER_STARTED, "start local video render.");
  1769. // session->blocalrender = true;
  1770. // }
  1771. //
  1772. // bshow_local = true;
  1773. // video_frame_delete(local_video_frame);
  1774. // local_video_frame = NULL;
  1775. // }
  1776. // else {
  1777. // Dbg("%s:%d,get video from shm preview queue failed!", __FUNCTION__, __LINE__);
  1778. // }
  1779. // }
  1780. // else {
  1781. // Dbg("%s:%d videorender_func exit!", __FUNCTION__, __LINE__);
  1782. // session->plocal_render->HideVideoWindow();
  1783. // break;
  1784. // }
  1785. //
  1786. // if (bset){
  1787. // if (bshow_local && session->bshow_remote) {
  1788. // session->plocal_render->ShowVideoWindow();
  1789. // session->premote_render->ShowVideoWindow();
  1790. // bset = false;
  1791. // }
  1792. // }
  1793. // }
  1794. // session->plocal_render->StopVideoRender();
  1795. // }
  1796. //
  1797. // return 0;
  1798. //}
  1799. #endif // RVC_OS_WIN
  1800. static int start_ui(video_session_t *session)
  1801. {
  1802. LOG_FUNCTION();
  1803. Dbg("start ui session addr is 0x%08x", session);
  1804. #ifdef RVC_OS_WIN
  1805. session->ui_event = CreateEventA(NULL, FALSE, FALSE, NULL);
  1806. if (!session->ui_event) {
  1807. Dbg("phonemedia_start create ui event failed!");
  1808. return Error_Resource;
  1809. }
  1810. else{
  1811. Dbg("session->ui_event = 0x%8x", session->ui_event);
  1812. }
  1813. session->ui_thread = (HANDLE)_beginthreadex(NULL, 0, &ui_proc, session, 0, NULL);
  1814. if (!session->ui_thread) {
  1815. Dbg("phonemedia_start create ui thread failed!");
  1816. CloseHandle(session->ui_event);
  1817. session->ui_event = NULL;
  1818. return Error_Resource;
  1819. }
  1820. else{
  1821. Dbg("session %8x create session->ui_thread = %8x", session, session->ui_thread);
  1822. }
  1823. Dbg("ui thread created ok!");
  1824. {
  1825. HANDLE hs[] = {session->ui_event, session->ui_thread};
  1826. DWORD dwRet = WaitForMultipleObjects(array_size(hs), hs, FALSE, INFINITE);
  1827. if (dwRet == WAIT_OBJECT_0) { //
  1828. Dbg("wait for ui ready event ok!");
  1829. } else if (dwRet == WAIT_OBJECT_0 + 1) { // thread exit
  1830. Dbg("ui thread aborted unexpected!");
  1831. Dbg("CloseHandle ui_thread 0x%08x", session->ui_thread);
  1832. CloseHandle(session->ui_thread);
  1833. session->ui_thread = NULL;
  1834. Dbg("CloseHandle ui_event 0x%08x", session->ui_event);
  1835. CloseHandle(session->ui_event);
  1836. session->ui_event = NULL;
  1837. return Error_Resource;
  1838. }
  1839. }
  1840. return 0;
  1841. #endif // RVC_OS_WIN
  1842. }
  1843. static void stop_ui(video_session_t *session)
  1844. {
  1845. LOG_FUNCTION();
  1846. Dbg("%s:%d session addr is 0x%08x.", __FUNCTION__, __LINE__, session);
  1847. #ifdef RVC_OS_WIN
  1848. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType) {
  1849. if (NULL != pg_last_session) {
  1850. pg_last_session->rtp = session->rtp;
  1851. pg_last_session->local_clock = session->local_clock;
  1852. CloseHandle(session->ui_thread);
  1853. session->ui_thread = NULL;
  1854. CloseHandle(session->ui_event);
  1855. session->ui_event = NULL;
  1856. session = pg_last_session;
  1857. }
  1858. }
  1859. if (NULL != session->pic_record) {
  1860. if (NULL != session->pic_record->work_thread) {
  1861. SetEvent(session->pic_record->evt);
  1862. Dbg("CloseHandle pic_record ui_thread 0x%08x", session->pic_record->work_thread);
  1863. CloseHandle(session->pic_record->work_thread);
  1864. session->pic_record->work_thread = NULL;
  1865. Dbg("CloseHandle pic_record ui_event 0x%08x", session->pic_record->evt);
  1866. CloseHandle(session->pic_record->evt);
  1867. session->pic_record->evt = NULL;
  1868. }
  1869. }
  1870. if (session->local_hwnd) {
  1871. Dbg("PostMessageA WM_CLOSE session addr is 0x%08x, session->local_hwnd = 0x%08x", session, session->local_hwnd);
  1872. BOOL bRet = PostMessageA(session->local_hwnd, WM_CLOSE, 0, 0);
  1873. Dbg("WaitForSingleObject session addr is %8x, and session->ui_thread is %8x.", session, session->ui_thread);
  1874. DWORD dCode = WaitForSingleObject(session->ui_thread, INFINITE);
  1875. Dbg("After WaitForSingleObject (session->ui_thread, INFINITE) session addr is %8x, and session->ui_thread = 0x%08x.", session, session->ui_thread);
  1876. Dbg("CloseHandle ui_thread 0x%08x", session->ui_thread);
  1877. CloseHandle(session->ui_thread);
  1878. session->ui_thread = NULL;
  1879. Dbg("CloseHandle ui_event 0x%08x", session->ui_event);
  1880. CloseHandle(session->ui_event);
  1881. session->ui_event = NULL;
  1882. }
  1883. pg_last_session = NULL;
  1884. pg_local_hwnd = NULL;
  1885. pg_remote_hwnd = NULL;
  1886. Dbg("set pg_last_session to null.");
  1887. #endif
  1888. }
  1889. static int load_record_area_png(video_session_t *session)
  1890. {
  1891. int iret = -1;
  1892. if (NULL == session){
  1893. return iret;
  1894. }
  1895. //加载双录人形背景框
  1896. char strPath[MAX_PATH_SIZE]={0};
  1897. char strRecordAreaPath[MAX_PATH_SIZE] = { 0 };
  1898. #ifdef RVC_OS_WIN
  1899. GetCurrentRunPath(strPath);
  1900. sprintf(strRecordAreaPath, "%s\\bin\\recordarea.png", strPath);
  1901. if (ExistsFile(strRecordAreaPath))
  1902. {
  1903. if (session->recordareaimage == NULL)
  1904. {
  1905. if((session->conf.eDeviceType == eMobilePadType)||(session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  1906. {
  1907. //pad版对人形区域放大处理
  1908. session->recordareaimage = cvLoadImage(strRecordAreaPath);
  1909. IplImage* tmp = cvCreateImage(cvSize(session->recordareaimage->width,session->recordareaimage->height),IPL_DEPTH_8U,3);
  1910. cvResize(session->recordareaimage ,tmp);
  1911. cvSetImageROI(tmp,cvRect(0,0,session->recordareaimage->width,session->recordareaimage->height));
  1912. //cvSetImageROI(tmp,cvRect(0,0,session->recordareaimage->width,session->recordareaimage->height));
  1913. cvCopy(tmp,session->recordareaimage);
  1914. cvReleaseImage(&tmp);
  1915. }
  1916. else
  1917. {
  1918. session->recordareaimage = cvLoadImage(strRecordAreaPath);
  1919. }
  1920. }
  1921. if (session->recordareamask == NULL )
  1922. {
  1923. if((session->conf.eDeviceType == eMobilePadType)||(session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  1924. {
  1925. //pad版对人形区域放大处理
  1926. session->recordareamask = cvLoadImage(strRecordAreaPath,0);
  1927. IplImage* tmp = cvCreateImage(cvSize(session->recordareamask->width,session->recordareamask->height),IPL_DEPTH_8U,1);
  1928. cvResize(session->recordareamask ,tmp);
  1929. cvSetImageROI(tmp,cvRect(0,0,session->recordareamask->width,session->recordareamask->height));
  1930. cvCopy(tmp,session->recordareamask);
  1931. cvReleaseImage(&tmp);
  1932. }
  1933. else
  1934. {
  1935. session->recordareamask = cvLoadImage(strRecordAreaPath,0);
  1936. }
  1937. }
  1938. Dbg("Load record area img Success");
  1939. iret = 0;
  1940. }
  1941. else
  1942. {
  1943. Dbg("Load record area img Fail");
  1944. session->recordareaimage = NULL;
  1945. session->recordareamask = NULL;
  1946. }
  1947. #endif // RVC_OS_WIN
  1948. return iret;
  1949. }
  1950. int video_session_create(const video_session_conf_t *conf, video_session_t **p_session)
  1951. {
  1952. LOG_FUNCTION();
  1953. video_session_t *session = ZALLOC_T(video_session_t);
  1954. if (session)
  1955. {
  1956. char str_local[MAX_PATH_SIZE] = {0};
  1957. char str_remote[MAX_PATH_SIZE] = {0};
  1958. translate_ipaddr_from_int(str_local, MAX_PATH_SIZE, conf->local_rtp_ip);
  1959. translate_ipaddr_from_int(str_remote, MAX_PATH_SIZE, conf->remote_rtp_ip);
  1960. Dbg("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.",
  1961. session, str_local, conf->local_rtp_port,conf->local_pt, str_remote, conf->remote_rtp_port, conf->remote_pt, conf->nCallType);
  1962. memcpy(&session->conf, conf, sizeof(video_session_conf_t));
  1963. //session->bshow_remote = false;
  1964. //session->ilast_windstae = 0;
  1965. session->video_shm_q_env = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE);
  1966. session->video_shm_q_remote = new Clibvideoqueue(REC_COMMON_VIDEO_REMOTE_SHM_RTP_QUEUE);
  1967. session->video_shm_q_preview = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_PREVIEW_QUEUE);
  1968. char strPath[MAX_PATH_SIZE]={0};
  1969. char strImgPath[MAX_PATH_SIZE]={0};
  1970. #ifdef RVC_OS_WIN
  1971. GetCurrentRunPath(strPath);
  1972. sprintf(strImgPath, "%s\\bin\\error.jpg", strPath);
  1973. #else
  1974. strcpy_s(strImgPath, MAX_PATH_SIZE, "./bin/error.jpg");
  1975. #endif // RVC_OS_WIN
  1976. if (ExistsFile(strImgPath))
  1977. {
  1978. IplImage*img = cvLoadImage(strImgPath,1);
  1979. if (img != NULL)
  1980. {
  1981. Dbg("load img success");
  1982. session->video_error = new videoq_frame;
  1983. session->video_error->format = VIDEOQ_FORMAT_RGB24;
  1984. session->video_error->framesize = 320*180*3;
  1985. session->video_error->height = 180;
  1986. session->video_error->width = 320;
  1987. session->video_error->data = new unsigned char[320*180*3];
  1988. memcpy(session->video_error->data,img->imageData,320*180*3);
  1989. cvReleaseImage(&img);
  1990. }
  1991. }
  1992. else
  1993. {
  1994. Dbg("Load error img Fail");
  1995. session->video_error = NULL;
  1996. }
  1997. //加载人形背景框
  1998. char strPersonPath[MAX_PATH_SIZE] = {0};
  1999. #ifdef RVC_OS_WIN
  2000. sprintf(strPersonPath, "%s\\bin\\rxk.jpg", strPath);
  2001. #else
  2002. snprintf(strPersonPath, MAX_PATH_SIZE, "./bin/rxk.jpg");
  2003. #endif // RVC_OS_WIN
  2004. if (ExistsFile(strPersonPath))
  2005. {
  2006. if (session->personimage == NULL)
  2007. {
  2008. if((session->conf.eDeviceType == eMobilePadType)||(session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  2009. {
  2010. //pad版对人形区域放大处理
  2011. session->personimage = cvLoadImage(strPersonPath);
  2012. IplImage*tmp = cvCreateImage(cvSize(session->personimage->width*3/2,session->personimage->height*3/2),IPL_DEPTH_8U,3);
  2013. cvResize(session->personimage ,tmp);
  2014. cvSetImageROI(tmp,cvRect(session->personimage->width/4,50,session->personimage->width,session->personimage->height));
  2015. cvCopy(tmp,session->personimage);
  2016. cvReleaseImage(&tmp);
  2017. }
  2018. else
  2019. {
  2020. session->personimage = cvLoadImage(strPersonPath);
  2021. }
  2022. }
  2023. if (session->personmask == NULL )
  2024. {
  2025. if((session->conf.eDeviceType == eMobilePadType)||(session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  2026. {
  2027. //pad版对人形区域放大处理
  2028. session->personmask = cvLoadImage(strPersonPath,0);
  2029. IplImage*tmp = cvCreateImage(cvSize(session->personmask->width*3/2,session->personmask->height*3/2),IPL_DEPTH_8U,1);
  2030. cvResize(session->personmask ,tmp);
  2031. cvSetImageROI(tmp,cvRect(session->personmask->width/4,50,session->personmask->width,session->personmask->height));
  2032. cvCopy(tmp,session->personmask);
  2033. cvReleaseImage(&tmp);
  2034. }
  2035. else
  2036. {
  2037. session->personmask = cvLoadImage(strPersonPath,0);
  2038. }
  2039. }
  2040. //IplImage*persongrey = cvLoadImage(strPersonPath,CV_LOAD_IMAGE_GRAYSCALE);
  2041. //二值化提取人形框掩码
  2042. //cvThreshold(persongrey,session->personmask,150, 255, CV_THRESH_BINARY);
  2043. Dbg("Load person img Success");
  2044. }
  2045. else
  2046. {
  2047. Dbg("Load person img Fail");
  2048. session->personimage = NULL;
  2049. session->personmask = NULL;
  2050. }
  2051. #if 0
  2052. video_frame frame;
  2053. video_frame_alloc(320, 180, VIDEO_FORMAT_RGB24, &frame);
  2054. video_frame_fill_black(&frame);
  2055. videoq_frame frm;
  2056. frm.data = frame.data[0];
  2057. session->video_shm_q_env->GetVideo(&frm, 0);
  2058. video_frame_save_bmpfile("d:\\a.bmp", &frame);
  2059. #endif
  2060. if (conf->camera_count == 2) {
  2061. session->video_shm_q_opt = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
  2062. }
  2063. session->bvideorecved = false;
  2064. session->blocalrender = false;
  2065. session->bremoterender = false;
  2066. *p_session = session;
  2067. }
  2068. return 0;
  2069. }
  2070. int video_session_start(video_session_t *session)
  2071. {
  2072. LOG_FUNCTION();
  2073. Dbg("video_session_start session addr is 0x%08x, session call type is %d.", session, session->conf.nCallType);
  2074. int rc = -1;
  2075. #ifdef RVC_OS_WIN
  2076. rc = start_ui(session);
  2077. #else
  2078. rc = start_video(session);
  2079. #endif // RVC_OS_WIN
  2080. return rc;
  2081. }
  2082. void video_session_stop(video_session_t *session)
  2083. {
  2084. LOG_FUNCTION();
  2085. #ifdef RVC_OS_WIN
  2086. Dbg("video session stop session addr is 0x%08x", session);
  2087. stop_ui(session);
  2088. #else
  2089. stop_video(session);
  2090. #endif
  2091. }
  2092. void video_session_destroy(video_session_t *session)
  2093. {
  2094. LOG_FUNCTION();
  2095. if (session->video_error){
  2096. delete session->video_error->data;
  2097. delete session->video_error;
  2098. }
  2099. if (session->video_shm_q_remote) {
  2100. delete session->video_shm_q_remote;
  2101. session->video_shm_q_remote = NULL;
  2102. }
  2103. if (session->personimage){
  2104. cvReleaseImage(&session->personimage);
  2105. }
  2106. if (session->personmask){
  2107. cvReleaseImage(&session->personmask);
  2108. }
  2109. if (session->recordareaimage){
  2110. cvReleaseImage(&session->recordareaimage);
  2111. }
  2112. if (session->recordareamask){
  2113. cvReleaseImage(&session->recordareamask);
  2114. }
  2115. free(session);
  2116. }
  2117. void av_log_cb(void*ptr, int level, const char*fmt, va_list list)
  2118. {
  2119. vDbg(fmt, list);
  2120. }
  2121. int video_lib_init()
  2122. {
  2123. #ifdef RVC_OS_WIN
  2124. CoInitialize(NULL);
  2125. {
  2126. int i, n;
  2127. n = videocap_get_device_count();
  2128. for (i = 0; i < n; ++i) {
  2129. WCHAR tmp[128];
  2130. char t[128];
  2131. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  2132. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  2133. Dbg("%d = %s", i, t);
  2134. }
  2135. }
  2136. videoframework_init();
  2137. av_log_set_callback(&av_log_cb);
  2138. //av_log_set_level(AV_LOG_DEBUG);
  2139. av_log_set_level(AV_LOG_QUIET);
  2140. #else
  2141. videoframework_init();
  2142. if (0 == VideoRender_Init()){
  2143. Dbg("sdl init success.");
  2144. }
  2145. else {
  2146. Dbg("sdl init failed.");
  2147. }
  2148. //av_log_set_callback(&av_log_cb);
  2149. //av_log_set_level(AV_LOG_TRACE);
  2150. //av_log_set_level(AV_LOG_QUIET);
  2151. #endif // RVC_OS_WIN
  2152. return 0;
  2153. }
  2154. void video_lib_deinit()
  2155. {
  2156. #ifdef RVC_OS_WIN
  2157. CoUninitialize();
  2158. #else
  2159. #endif // RVC_OS_WIN
  2160. videoframework_term();
  2161. VideoRender_Term();
  2162. }