video_session.cpp 76 KB

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