video_session.cpp 87 KB

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