video_session.cpp 90 KB

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