video_session.cpp 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924
  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 "videorenderobj.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. VideoRenderObj* plocal_render;
  104. VideoRenderObj* 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);
  426. session->bshow_remote = true;
  427. video_frame_delete(echoframe);
  428. echoframe = NULL;
  429. }
  430. else {
  431. session->premote_render->RenderVideoFrame(frame);
  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. videortp_create(&config, &session->rtp);
  1176. rc = videortp_start(session->rtp);
  1177. if (rc != 0)
  1178. {
  1179. Dbg("start video rtp failed!");
  1180. videortp_destroy(session->rtp);
  1181. session->rtp = NULL;
  1182. }
  1183. else {
  1184. Dbg("videortp_start suc.");
  1185. }
  1186. return rc;
  1187. }
  1188. static int start_local_video_clock(video_session_t* session)
  1189. {
  1190. int rc = -1;
  1191. if (session->conf.eDeviceType == eMobilePadType){
  1192. rc = videoclock_create(REC_COMMON_VIDEO_FPS_MOBILE, REC_COMMON_VIDEO_FPS_DEN,
  1193. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_RGB24,
  1194. &local_put_frame, session, &local_get_frame, session, &session->local_clock, session->conf.ref_Up_Fps, &__dbg);
  1195. }
  1196. else if ((session->conf.eDeviceType == ePadtype) || (session->conf.eDeviceType == eDesk2SType) || (session->conf.eDeviceType == eDesk1SType) || (session->conf.eDeviceType == eDesk2SIntegratedType))
  1197. {
  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 == eStand2sType)
  1203. {
  1204. rc = videoclock_create(REC_COMMON_VIDEO_FPS_NUM, 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. Dbg("create session->local_clock addr is 0x%08x", session->local_clock);
  1209. if (0 == rc) {
  1210. rc = videoclock_start(session->local_clock);
  1211. if (rc != 0) {
  1212. Dbg("start video clock failed!");
  1213. videoclock_destroy(session->local_clock);
  1214. session->local_clock = NULL;
  1215. }
  1216. else {
  1217. Dbg("videoclock start success!");
  1218. }
  1219. }
  1220. else {
  1221. Dbg("create video clock failed!");
  1222. }
  1223. return rc;
  1224. }
  1225. static int start_video(video_session_t *session)
  1226. {
  1227. LOG_FUNCTION();
  1228. int rc = -1;
  1229. if (session->video_shm_q_env)
  1230. {
  1231. session->local_encode_sws_ctx_env = sws_getContext(
  1232. REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, PIX_FMT_BGR24,
  1233. REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, PIX_FMT_YUV420P,
  1234. SWS_POINT, NULL, NULL, NULL);
  1235. if (!session->local_encode_sws_ctx_env)
  1236. goto on_error;
  1237. else{
  1238. Dbg("get session->local_encode_sws_ctx_env succ.");
  1239. }
  1240. }
  1241. else{
  1242. Dbg("session->video_shm_q_env is NULL.");
  1243. }
  1244. if (session->video_shm_q_opt)
  1245. {
  1246. session->local_encode_sws_ctx_opt = sws_getContext(
  1247. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, PIX_FMT_BGR24,
  1248. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, PIX_FMT_YUV420P,
  1249. SWS_POINT, NULL, NULL, NULL);
  1250. if (!session->local_encode_sws_ctx_opt)
  1251. goto on_error;
  1252. else{
  1253. Dbg("get session->local_encode_sws_ctx_opt succ.");
  1254. }
  1255. }
  1256. else{
  1257. Dbg("session->video_shm_q_opt is NULL.");
  1258. }
  1259. 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);
  1260. #ifdef RVC_OS_WIN
  1261. if (DOUBLERECORD_CALLTYPE != session->conf.nCallType){
  1262. Dbg("create new remote video player");
  1263. if (session->remote_hwnd)
  1264. {
  1265. if (session->conf.eDeviceType == eMobilePadType)
  1266. {
  1267. rc = videoplayer_create(session->remote_hwnd,
  1268. 0,
  1269. 0,
  1270. session->conf.remote_video_view_cx,
  1271. session->conf.remote_video_view_cy,
  1272. REC_COMMON_VIDEO_FPS_MOBILE_AGENT,
  1273. REC_COMMON_VIDEO_FPS_DEN,
  1274. session->conf.remote_video_width,
  1275. session->conf.remote_video_height,
  1276. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP,
  1277. session->conf.iremote_wind_flags,
  1278. "remote",
  1279. on_remoteWinstate,
  1280. NULL,
  1281. session,
  1282. &session->remote_player);
  1283. if (rc != 0)
  1284. {
  1285. Dbg("create remote video player failed!");
  1286. goto on_error;
  1287. }
  1288. else{
  1289. Dbg("create remote video player suc!");
  1290. }
  1291. }
  1292. else if((session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  1293. {
  1294. rc = videoplayer_create(session->remote_hwnd,
  1295. 0,
  1296. 0,
  1297. session->conf.remote_video_view_cx,
  1298. session->conf.remote_video_view_cy,
  1299. REC_COMMON_VIDEO_FPS_MOBILE,
  1300. REC_COMMON_VIDEO_FPS_DEN,
  1301. session->conf.remote_video_width,
  1302. session->conf.remote_video_height,
  1303. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP,
  1304. session->conf.iremote_wind_flags,
  1305. "remote",
  1306. on_remoteWinstate,
  1307. NULL,
  1308. session,
  1309. &session->remote_player);
  1310. if (rc != 0) {
  1311. Dbg("create remote video player failed!");
  1312. goto on_error;
  1313. }
  1314. else{
  1315. Dbg("create remote video player suc!");
  1316. }
  1317. }
  1318. else if(session->conf.eDeviceType == eStand2sType)
  1319. {
  1320. rc = videoplayer_create(session->remote_hwnd,
  1321. 0,
  1322. 0,
  1323. session->conf.remote_video_view_cx,
  1324. session->conf.remote_video_view_cy,
  1325. REC_COMMON_VIDEO_FPS_NUM,
  1326. REC_COMMON_VIDEO_FPS_DEN,
  1327. session->conf.remote_video_width,
  1328. session->conf.remote_video_height,
  1329. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP,
  1330. session->conf.iremote_wind_flags,
  1331. "remote",
  1332. on_remoteWinstate,
  1333. NULL,
  1334. session,
  1335. &session->remote_player);
  1336. if (rc != 0)
  1337. {
  1338. Dbg("create remote video player failed!");
  1339. goto on_error;
  1340. }
  1341. else{
  1342. Dbg("create remote video player suc remote_video_width = %d,remote_video_height = %d.", session->conf.remote_video_width, session->conf.remote_video_height);
  1343. }
  1344. }
  1345. }
  1346. else
  1347. {
  1348. Dbg("remote_hwnd == Null");
  1349. }
  1350. }
  1351. else{
  1352. if (NULL != pg_last_session){
  1353. session->remote_player = pg_last_session->remote_player;
  1354. Dbg("double record call remote_player reuse.");
  1355. }
  1356. else{
  1357. Dbg("pg_last_session is NULL, and call type is %d.", session->conf.nCallType);
  1358. }
  1359. }
  1360. BOOL bIsActiveInspect = FALSE;
  1361. if (session->conf.ref_Is_ActiveInspect != NULL)
  1362. {
  1363. if (*session->conf.ref_Is_ActiveInspect == 1)
  1364. {
  1365. bIsActiveInspect = TRUE;
  1366. }
  1367. else
  1368. {
  1369. bIsActiveInspect = FALSE;
  1370. }
  1371. }
  1372. else
  1373. {
  1374. bIsActiveInspect = FALSE;
  1375. }
  1376. if (DOUBLERECORD_CALLTYPE != session->conf.nCallType){
  1377. Dbg("create new local video player");
  1378. if (session->conf.eDeviceType == eMobilePadType)
  1379. {
  1380. rc = videoplayer_create(session->local_hwnd,
  1381. 0,
  1382. 0,
  1383. session->conf.local_video_view_cx,
  1384. session->conf.local_video_view_cy,
  1385. REC_COMMON_VIDEO_FPS_MOBILE,
  1386. REC_COMMON_VIDEO_FPS_DEN,
  1387. REC_COMMON_VIDEO_PREVIEW_WIDTH,
  1388. REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  1389. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP,
  1390. session->conf.ilocal_wind_flags,
  1391. "local",
  1392. on_pull, free_frame, session,
  1393. &session->local_player);
  1394. if (rc != 0)
  1395. {
  1396. Dbg("create local video player failed!");
  1397. goto on_error;
  1398. }
  1399. else{
  1400. Dbg("create local video player suc!");
  1401. }
  1402. }
  1403. else if((session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  1404. {
  1405. rc = videoplayer_create(session->local_hwnd,
  1406. 0,
  1407. 0,
  1408. session->conf.local_video_view_cx,
  1409. session->conf.local_video_view_cy,
  1410. REC_COMMON_VIDEO_FPS_MOBILE,
  1411. REC_COMMON_VIDEO_FPS_DEN,
  1412. REC_COMMON_VIDEO_PREVIEW_WIDTH,
  1413. REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  1414. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP,
  1415. session->conf.ilocal_wind_flags,
  1416. "local",
  1417. on_pull, free_frame, session,
  1418. &session->local_player);
  1419. if (rc != 0)
  1420. {
  1421. Dbg("create local video player failed!");
  1422. goto on_error;
  1423. }
  1424. else{
  1425. Dbg("create local video player suc!");
  1426. }
  1427. }
  1428. else if(session->conf.eDeviceType == eStand2sType)
  1429. {
  1430. rc = videoplayer_create(session->local_hwnd,
  1431. 0,
  1432. 0,
  1433. session->conf.local_video_view_cx,
  1434. session->conf.local_video_view_cy,
  1435. REC_COMMON_VIDEO_FPS_NUM,
  1436. REC_COMMON_VIDEO_FPS_DEN,
  1437. bIsActiveInspect?REC_COMMON_VIDEO_SNAPSHOT_WIDTH:REC_COMMON_VIDEO_PREVIEW_WIDTH,
  1438. bIsActiveInspect?REC_COMMON_VIDEO_SNAPSHOT_WIDTH:REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  1439. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP,
  1440. session->conf.ilocal_wind_flags,
  1441. "local",
  1442. on_pull, free_frame, session,
  1443. &session->local_player);
  1444. if (rc != 0)
  1445. {
  1446. Dbg("create local video player failed!");
  1447. goto on_error;
  1448. }
  1449. else{
  1450. Dbg("create local video player suc!");
  1451. }
  1452. }
  1453. }
  1454. else{
  1455. if (NULL != pg_last_session){
  1456. session->local_player = pg_last_session->local_player;
  1457. Dbg("double record call local player reuse.");
  1458. }
  1459. }
  1460. if (session->remote_hwnd)
  1461. {
  1462. if (0 != session->conf.remote_rtp_port)
  1463. {
  1464. //videortp_config_t config = {0};
  1465. //config.fps_den = REC_COMMON_VIDEO_FPS_DEN;
  1466. //if (session->conf.eDeviceType == eMobilePadType)
  1467. //{
  1468. // config.fps_num = REC_COMMON_VIDEO_FPS_MOBILE_AGENT;
  1469. //}
  1470. //else if((session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  1471. //{
  1472. // config.fps_num = REC_COMMON_VIDEO_FPS_MOBILE;
  1473. //}
  1474. //else if(session->conf.eDeviceType == eStand2sType)
  1475. //{
  1476. // config.fps_num = REC_COMMON_VIDEO_FPS_NUM;
  1477. //}
  1478. //config.capture_height = -1; // not use
  1479. //config.capture_width = -1; // not use
  1480. //config.dir = 3;
  1481. //config.tx_width = REC_COMMON_VIDEO_RTP_WIDTH;
  1482. //config.tx_height = REC_COMMON_VIDEO_RTP_HEIGHT;
  1483. //config.rx_width = session->conf.remote_video_width;
  1484. //config.rx_height = session->conf.remote_video_height;
  1485. //config.local_ip = session->conf.local_rtp_ip;
  1486. //config.local_pt = session->conf.local_pt;
  1487. //config.remote_pt = session->conf.local_pt;
  1488. //config.local_rtp_port = session->conf.local_rtp_port;
  1489. //config.mtu = session->conf.mtu;
  1490. //config.quant = session->conf.video_quant;
  1491. //config.remote_ip = session->conf.remote_rtp_ip;
  1492. //config.remote_rtp_port = session->conf.remote_rtp_port;
  1493. //config.user_data = session;
  1494. //config.bit_rate = session->conf.bit_rate;
  1495. //config.on_rx_frame = &on_rx_frame;
  1496. //config.dbg = &__dbg;
  1497. //videortp_create(&config, &session->rtp);
  1498. //rc = videortp_start(session->rtp);
  1499. //if (rc != 0)
  1500. //{
  1501. // Dbg("start video rtp failed!");
  1502. // videortp_destroy(session->rtp);
  1503. // session->rtp = NULL;
  1504. // goto on_error;
  1505. //}
  1506. //else{
  1507. // Dbg("videortp_start suc.");
  1508. //}
  1509. if (0 != start_video_rtpsession(session)) {
  1510. goto on_error;
  1511. }
  1512. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType){
  1513. if (NULL != pg_last_session){
  1514. pg_last_session->rtp = session->rtp;
  1515. }
  1516. }
  1517. //if (session->conf.eDeviceType == eMobilePadType)
  1518. //{
  1519. // rc = videoclock_create(REC_COMMON_VIDEO_FPS_MOBILE, REC_COMMON_VIDEO_FPS_DEN,
  1520. // REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_RGB24,
  1521. // &local_put_frame, session, &local_get_frame, session, &session->local_clock, session->conf.ref_Up_Fps);
  1522. //}
  1523. //else if((session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  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 == eStand2sType)
  1530. //{
  1531. // rc = videoclock_create(REC_COMMON_VIDEO_FPS_NUM, 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. //Dbg("create session->local_clock addr is 0x%08x", session->local_clock);
  1536. //
  1537. //if (rc != 0) {
  1538. // Dbg("create video clock failed!");
  1539. // goto on_error;
  1540. //}
  1541. //rc = videoclock_start(session->local_clock);
  1542. //if (rc != 0) {
  1543. // Dbg("start video clock failed!");
  1544. // videoclock_destroy(session->local_clock);
  1545. // session->local_clock = NULL;
  1546. // goto on_error;
  1547. //}
  1548. if (0 != start_local_video_clock(session)) {
  1549. goto on_error;
  1550. }
  1551. }
  1552. else{
  1553. pg_last_session = session; //保存session信息
  1554. Dbg("begin show agent static picture.");
  1555. show_remote_agnet_picture(session);
  1556. }
  1557. }
  1558. #else
  1559. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType) {
  1560. if (NULL != pg_last_session) {
  1561. session->premote_render = pg_last_session->premote_render;
  1562. session->plocal_render = pg_last_session->plocal_render;
  1563. Dbg("double record call local and remote render reuse.");
  1564. }
  1565. else {
  1566. Dbg("pg_last_session is NULL, and call type is %d.", session->conf.nCallType);
  1567. }
  1568. }
  1569. if (session->premote_render)
  1570. {
  1571. if (0 != session->conf.remote_rtp_port) {
  1572. if (0 != start_video_rtpsession(session)) {
  1573. goto on_error;
  1574. }
  1575. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType) {
  1576. if (NULL != pg_last_session) {
  1577. pg_last_session->rtp = session->rtp;
  1578. }
  1579. }
  1580. if (0 != start_local_video_clock(session)) {
  1581. goto on_error;
  1582. }
  1583. }
  1584. else {
  1585. Dbg("begin show agent static picture.");
  1586. show_remote_agnet_picture(session);
  1587. }
  1588. }
  1589. #endif // RVC_OS_WIN
  1590. on_error:
  1591. return rc;
  1592. }
  1593. static void stop_video(video_session_t *session)
  1594. {
  1595. LOG_FUNCTION();
  1596. #ifdef RVC_OS_WIN
  1597. 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);
  1598. if (session->local_clock && session->remote_hwnd)
  1599. {
  1600. Dbg("videoclock_stop local_clock addr is 0x%08x", session->local_clock);
  1601. videoclock_stop(session->local_clock);
  1602. videoclock_destroy(session->local_clock);
  1603. session->local_clock = NULL;
  1604. pg_remote_hwnd = NULL;
  1605. pg_local_hwnd = NULL;
  1606. }
  1607. #else
  1608. if (session->local_clock)
  1609. {
  1610. Dbg("videoclock_stop local_clock addr is 0x%08x", session->local_clock);
  1611. videoclock_stop(session->local_clock);
  1612. videoclock_destroy(session->local_clock);
  1613. session->local_clock = NULL;
  1614. }
  1615. #endif // RVC_OS_WIN
  1616. if (session->rtp)
  1617. {
  1618. Dbg("begin stop video rtp.");
  1619. videortp_stop(session->rtp);
  1620. videortp_destroy(session->rtp);
  1621. session->rtp = NULL;
  1622. }
  1623. else {
  1624. Dbg("session->rtp == null");
  1625. }
  1626. #ifdef RVC_OS_WIN
  1627. if (session->local_player) {
  1628. videoplayer_destroy(session->local_player);
  1629. session->local_player = NULL;
  1630. Dbg("local video player destroy.");
  1631. }
  1632. if (session->remote_player && session->remote_hwnd)
  1633. {
  1634. videoplayer_destroy(session->remote_player);
  1635. session->remote_player = NULL;
  1636. Dbg("remote video player destroy.");
  1637. }
  1638. #else
  1639. if (session->plocal_render){
  1640. delete session->plocal_render;
  1641. session->plocal_render = NULL;
  1642. }
  1643. if (session->premote_render){
  1644. delete session->premote_render;
  1645. session->premote_render = NULL;
  1646. }
  1647. #endif
  1648. if (session->local_encode_sws_ctx_env) {
  1649. sws_freeContext(session->local_encode_sws_ctx_env);
  1650. session->local_encode_sws_ctx_env = NULL;
  1651. }
  1652. if (session->local_encode_sws_ctx_opt) {
  1653. sws_freeContext(session->local_encode_sws_ctx_opt);
  1654. session->local_encode_sws_ctx_opt = NULL;
  1655. }
  1656. }
  1657. #ifdef RVC_OS_WIN
  1658. static bool ReMoveOutOfScreenVideoWindow(HWND hWnd, RECT rect)
  1659. {
  1660. bool bret = false;
  1661. int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  1662. int iScreenHight = GetSystemMetrics(SM_CYSCREEN);
  1663. Dbg("ScreenWidth = %d, ScreenHight = %d.", iScreenWidth, iScreenHight);
  1664. int iVideoWidth = rect.right - rect.left;
  1665. int iVideoHight = rect.bottom - rect.top;
  1666. if (rect.left < 0) {
  1667. if (rect.bottom <= iScreenHight) {
  1668. MoveWindow(hWnd, 0, rect.top, iVideoWidth, iVideoHight, TRUE);
  1669. }
  1670. else {
  1671. MoveWindow(hWnd, 0, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1672. }
  1673. bret = true;
  1674. }
  1675. if (rect.right > iScreenWidth) {
  1676. if (rect.bottom <= iScreenHight) {
  1677. MoveWindow(hWnd, iScreenWidth - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1678. }
  1679. else {
  1680. MoveWindow(hWnd, iScreenWidth - iVideoWidth, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1681. }
  1682. bret = true;
  1683. }
  1684. if (rect.bottom > iScreenHight) {
  1685. if (rect.left >= 0 && rect.right <= iScreenWidth) {
  1686. MoveWindow(hWnd, rect.left, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1687. bret = true;
  1688. }
  1689. }
  1690. return bret;
  1691. }
  1692. static bool ReMoveCenterOtherVideoWindow(HWND hWnd, RECT rect, RECT otherect)
  1693. {
  1694. bool bret = false;
  1695. int iVideoWidth = rect.right - rect.left;
  1696. int iVideoHight = rect.bottom - rect.top;
  1697. int iOtherVideoWidth = otherect.right - otherect.left;
  1698. int iOtherVideoHight = otherect.bottom - otherect.top;
  1699. if (iVideoWidth > iOtherVideoHight || iVideoHight > iOtherVideoHight) {
  1700. return bret;
  1701. }
  1702. if (rect.left > otherect.left + iVideoWidth && rect.right < otherect.right) {
  1703. if (rect.top > otherect.top && rect.bottom < otherect.bottom) {
  1704. if (otherect.right - rect.right <= rect.left - otherect.left) {
  1705. MoveWindow(hWnd, otherect.right - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1706. }
  1707. else {
  1708. MoveWindow(hWnd, otherect.left, rect.top, iVideoWidth, iVideoHight, TRUE);
  1709. }
  1710. bret = true;
  1711. }
  1712. }
  1713. if (rect.right < otherect.right - iVideoWidth && rect.left > otherect.left) {
  1714. if (rect.top > otherect.top && rect.bottom < otherect.bottom) {
  1715. if (otherect.right - rect.right <= rect.left - otherect.left) {
  1716. MoveWindow(hWnd, otherect.right - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1717. }
  1718. else {
  1719. MoveWindow(hWnd, otherect.left, rect.top, iVideoWidth, iVideoHight, TRUE);
  1720. }
  1721. bret = true;
  1722. }
  1723. }
  1724. return bret;
  1725. }
  1726. static bool ReMoveVideoWindow(HWND hWnd, RECT rect, RECT otherect)
  1727. {
  1728. LOG_FUNCTION();
  1729. bool bret = false;
  1730. bret = ReMoveOutOfScreenVideoWindow(hWnd, rect);
  1731. bret = ReMoveCenterOtherVideoWindow(hWnd, rect, otherect);
  1732. return bret;
  1733. }
  1734. static int HandleVideoMoveEvent(int iMessageType, HWND hWnd, video_session_t* pSession)
  1735. {
  1736. LOG_FUNCTION();
  1737. int iRet = -1;
  1738. if (NULL == pSession || NULL == hWnd) {
  1739. return iRet;
  1740. }
  1741. HWND hOtherWnd = NULL;
  1742. //本地和远端标识 1为本地,2为远端
  1743. int iVideoType = 2;
  1744. if (hWnd == pSession->local_hwnd) {
  1745. iVideoType = 1;
  1746. hOtherWnd = pSession->remote_hwnd;
  1747. }
  1748. else {
  1749. hOtherWnd = pSession->local_hwnd;
  1750. }
  1751. if (NULL == hOtherWnd) {
  1752. return iRet;
  1753. }
  1754. bool bMoved = false;
  1755. RECT rect;
  1756. GetWindowRect(hWnd, &rect);
  1757. 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);
  1758. RECT otherect;
  1759. GetWindowRect(hOtherWnd, &otherect);
  1760. Dbg("other video Window left = %d, right = %d, bottom = %d, top = %d", otherect.left, otherect.right, otherect.bottom, otherect.top);
  1761. bMoved = ReMoveVideoWindow(hWnd, rect, otherect);
  1762. if (bMoved) {
  1763. Dbg("has removed video window.");
  1764. GetWindowRect(hWnd, &rect);
  1765. }
  1766. if (NULL != pSession->conf.video_echo_cb && NULL != pSession->conf.video_echo_cb->on_video_box_move) {
  1767. pSession->conf.video_echo_cb->on_video_box_move(iMessageType, iVideoType, rect.left, rect.bottom, pSession->conf.video_echo_cb->user_data);
  1768. iRet = 0;
  1769. }
  1770. return iRet;
  1771. }
  1772. #else
  1773. #endif // RVC_OS_WIN
  1774. #ifdef RVC_OS_WIN
  1775. static LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  1776. {
  1777. video_session_t* session = (video_session_t*)GetWindowLongPtrA(hWnd, GWLP_USERDATA);
  1778. switch (msg) {
  1779. case WM_DESTROY:
  1780. Dbg("WndProc session addr is 0x%08x, and receive WM_DESTROY.", session);
  1781. if (session->local_hwnd == hWnd) {
  1782. Dbg("session->local_hwnd = 0");
  1783. session->local_hwnd = 0;
  1784. }
  1785. else if (session->remote_hwnd == hWnd) {
  1786. Dbg("session->remote_hwnd = 0");
  1787. session->remote_hwnd = 0;
  1788. }
  1789. if (session->local_hwnd == 0 && session->remote_hwnd == 0) {
  1790. Dbg("*****PostQuitMessage****");
  1791. PostQuitMessage(0);
  1792. }
  1793. return 0;
  1794. case WM_NCHITTEST:
  1795. if (((session->local_hwnd == hWnd) && (0 != session->conf.local_move)) || ((session->remote_hwnd == hWnd) && (0 != session->conf.remote_move))) {
  1796. return HTCAPTION;
  1797. }
  1798. else {
  1799. break;
  1800. }
  1801. case WM_ACTIVATE:
  1802. case WM_TOUCH:
  1803. case WM_GESTURE:
  1804. ReleaseCapture();
  1805. return 0;
  1806. #if 1
  1807. case WM_WINDOWPOSCHANGED:
  1808. {
  1809. LPWINDOWPOS pPos = (LPWINDOWPOS)lParam;
  1810. if (pPos->hwndInsertAfter != HWND_TOPMOST && pPos->hwndInsertAfter != HWND_TOP) {
  1811. //BringWindowToTop(hWnd);
  1812. SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  1813. }
  1814. else {
  1815. return DefWindowProc(hWnd, msg, wParam, lParam);
  1816. }
  1817. }
  1818. return 0;
  1819. //return DefWindowProc(hWnd, msg, wParam, lParam);
  1820. #endif
  1821. case WM_MBUTTONDOWN:
  1822. case WM_MBUTTONUP:
  1823. case WM_MBUTTONDBLCLK:
  1824. case WM_LBUTTONDBLCLK:
  1825. case WM_LBUTTONDOWN:
  1826. case WM_LBUTTONUP:
  1827. OutputDebugStringA("mouse clicked!");
  1828. if (session) {
  1829. if (session->local_hwnd) {
  1830. //ReleaseCapture();
  1831. }
  1832. }
  1833. return 0;
  1834. case WM_CLOSE:
  1835. Dbg("WndProc session addr is 0x%08x, and receive WM_CLOSE", session);
  1836. stop_video(session);
  1837. Dbg("WndProc SetEvent session addr is 0x%08x.", session);
  1838. //SetEvent(session->ui_thread);
  1839. DestroyWindow(session->local_hwnd);
  1840. DestroyWindow(session->remote_hwnd);
  1841. return 0;
  1842. case WM_MOVE:
  1843. break;
  1844. case WM_ENTERSIZEMOVE:
  1845. {
  1846. Dbg("***WM_ENTERSIZEMOVE***");
  1847. HandleVideoMoveEvent(0, hWnd, session);
  1848. }
  1849. break;
  1850. case WM_EXITSIZEMOVE:
  1851. {
  1852. Dbg("***WM_EXITSIZEMOVE***");
  1853. HandleVideoMoveEvent(1, hWnd, session);
  1854. }
  1855. break;
  1856. default:
  1857. return DefWindowProc(hWnd, msg, wParam, lParam);
  1858. }
  1859. }
  1860. #endif //
  1861. #ifdef RVC_OS_WIN
  1862. static unsigned int __stdcall ui_proc(void *arg)
  1863. {
  1864. video_session_t *session = (video_session_t*)arg;
  1865. Dbg("ui_proc session addr is 0x%08x", session);
  1866. WNDCLASSA wc = {0};
  1867. ATOM a = 0;
  1868. HWND hWnd = NULL;
  1869. MSG msg;
  1870. HINSTANCE hInst = ModuleBase::GetModuleBase()->GetInstance();
  1871. bool breuse_local_wnd = false;
  1872. bool breuse_remote_wnd = false;
  1873. DWORD dLocal_style = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_NOACTIVATE;
  1874. DWORD dRemote_style = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_NOACTIVATE;
  1875. //双录两阶段视频窗口复用
  1876. if (NULL != pg_local_hwnd){
  1877. session->local_hwnd = pg_local_hwnd;
  1878. //SetWindowLongPtrA(session->local_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1879. breuse_local_wnd = true;
  1880. Dbg("local video hwnd reuse.");
  1881. }
  1882. else{
  1883. Dbg("create local video hwnd and x = %d, y = %d, width = %d, height = %d.",
  1884. session->conf.local_video_view_x, session->conf.local_video_view_y, session->conf.local_video_view_cx, session->conf.local_video_view_cy);
  1885. Dbg("local video move flag is %d, remote video move flag is %d.", session->conf.local_move, session->conf.remote_move);
  1886. if (0 != session->conf.local_move){
  1887. dLocal_style -= WS_EX_NOACTIVATE;
  1888. }
  1889. if (0 != session->conf.remote_move){
  1890. dRemote_style -= WS_EX_NOACTIVATE;
  1891. }
  1892. CoInitialize(0);
  1893. wc.cbClsExtra = 0;
  1894. wc.cbWndExtra = 0;
  1895. wc.hInstance = hInst;
  1896. wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  1897. wc.hCursor = NULL;
  1898. wc.hIcon = NULL;
  1899. wc.lpfnWndProc = &WndProc;
  1900. wc.lpszClassName = WNDCLS_NAME;
  1901. wc.style = CS_HREDRAW | CS_OWNDC | CS_VREDRAW;
  1902. a = RegisterClassA(&wc);
  1903. if (a == 0)
  1904. return 0;
  1905. session->local_hwnd = CreateWindowExA(dLocal_style,
  1906. WNDCLS_NAME, NULL, WS_POPUP|WS_VISIBLE,
  1907. session->conf.local_video_view_x, session->conf.local_video_view_y, session->conf.local_video_view_cx, session->conf.local_video_view_cy,
  1908. NULL, NULL, hInst, NULL);
  1909. if (session->local_hwnd) {
  1910. SetWindowLongPtrA(session->local_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1911. if (0 == session->conf.local_move){
  1912. SetWindowPos(session->local_hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
  1913. }
  1914. pg_local_hwnd = session->local_hwnd;
  1915. Dbg("restore local video hwnd");
  1916. }
  1917. }
  1918. 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)
  1919. {
  1920. if (NULL != pg_remote_hwnd){
  1921. session->remote_hwnd = pg_remote_hwnd;
  1922. //SetWindowLongPtrA(session->remote_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1923. breuse_remote_wnd = true;
  1924. Dbg("remote video hwnd reuse.");
  1925. if (NULL != pg_last_session){
  1926. Dbg("SetEvent pg_last_session->ui_event and ui_event addr is %8x.", pg_last_session->ui_event);
  1927. record_agent_picture_show_session_destory(pg_last_session);
  1928. }
  1929. }
  1930. else{
  1931. 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);
  1932. session->remote_hwnd = CreateWindowExA(dRemote_style,
  1933. WNDCLS_NAME, NULL, WS_POPUP|WS_VISIBLE,
  1934. session->conf.remote_video_view_x, session->conf.remote_video_view_y, session->conf.remote_video_view_cx, session->conf.remote_video_view_cy,
  1935. NULL, NULL, hInst, NULL);
  1936. if (session->remote_hwnd) {
  1937. SetWindowLongPtrA(session->remote_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1938. if (0 == session->conf.remote_move){
  1939. SetWindowPos(session->remote_hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
  1940. }
  1941. pg_remote_hwnd = session->remote_hwnd;
  1942. Dbg("restore remote video hwnd.");
  1943. }
  1944. }
  1945. }
  1946. else
  1947. {
  1948. Dbg("remote_hwnd == Null");
  1949. session->remote_hwnd = NULL;
  1950. }
  1951. if (session->local_hwnd/* && session->remote_hwnd*/)
  1952. {
  1953. int rc = 0;
  1954. ShowCursor(FALSE);
  1955. Dbg("SetEvent1 session addr is 0x%08x.", session);
  1956. SetEvent(session->ui_event);
  1957. rc = start_video(session);
  1958. if (rc != 0)
  1959. {
  1960. Dbg("start video failed!");
  1961. }
  1962. else
  1963. {
  1964. Dbg("start video ok!");
  1965. }
  1966. if (!breuse_local_wnd && !breuse_remote_wnd)
  1967. {
  1968. while (GetMessageA(&msg, NULL, NULL, NULL))
  1969. {
  1970. if (msg.message == WM_CLOSE) {
  1971. Dbg("session addr is 0x%08x, session->local_hwnd is 0x%08x, rx WM_CLOSE", session, session->local_hwnd);
  1972. }
  1973. TranslateMessage(&msg);
  1974. DispatchMessageA(&msg);
  1975. }
  1976. Dbg("SetEvent2 session addr is 0x%08x.", session);
  1977. SetEvent(session->ui_event);
  1978. }
  1979. }
  1980. if (a)
  1981. UnregisterClassA(WNDCLS_NAME, hInst);
  1982. CoUninitialize();
  1983. return 0;
  1984. }
  1985. #else
  1986. void* videorender_func(void* arg)
  1987. {
  1988. LOG_FUNCTION();
  1989. video_session_t* session = (video_session_t*)arg;
  1990. Dbg("%s:%d session addr is 0x%08x, session->plocal_render = 0x%08x", __FUNCTION__, __LINE__, session, session->plocal_render);
  1991. char str_local_ip[MAX_PATH_SIZE] = { 0 };
  1992. char str_remote_ip[MAX_PATH_SIZE] = { 0 };
  1993. translate_ipaddr_from_int(str_local_ip, MAX_PATH_SIZE, session->conf.local_rtp_ip);
  1994. translate_ipaddr_from_int(str_remote_ip, MAX_PATH_SIZE, session->conf.remote_rtp_ip);
  1995. Dbg("videorender_func local_pt: %d local_ip: %s local_rtp_port: %d.", session->conf.local_pt, str_local_ip, session->conf.local_rtp_port);
  1996. 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);
  1997. Dbg("videorender_func rx_width: %d rx_height: %d.", session->conf.remote_video_width, session->conf.remote_video_height);
  1998. 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,
  1999. session->conf.remote_video_view_x, session->conf.remote_video_view_y, session->conf.remote_video_view_cx, session->conf.remote_video_view_cy);
  2000. session->plocal_render = new VideoRenderObj();
  2001. if (NULL != session->plocal_render){
  2002. videorender_param_t tparam = { 0 };
  2003. tparam.icx = session->conf.local_video_view_x;
  2004. tparam.icy = session->conf.local_video_view_y;
  2005. tparam.uwidth = session->conf.local_video_view_cx;
  2006. tparam.uheight = session->conf.local_video_view_cy;
  2007. //tparam.uvideowidth = REC_COMMON_VIDEO_PREVIEW_WIDTH;
  2008. //tparam.uvideoheight = REC_COMMON_VIDEO_PREVIEW_HEIGHT;
  2009. tparam.ivideoformat = VIDEO_FORMAT_RGB24;
  2010. if (0 == session->plocal_render->SetVideoRenderParam(&tparam)){
  2011. session->plocal_render->HideVideoWindow();
  2012. }
  2013. else {
  2014. Dbg("%s:%d set video render param failed!", __FUNCTION__, __LINE__);
  2015. return 0;
  2016. }
  2017. }
  2018. 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){
  2019. 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);
  2020. session->premote_render = new VideoRenderObj();
  2021. if (session->premote_render){
  2022. videorender_param_t tparam_remote = { 0 };
  2023. tparam_remote.icx = session->conf.remote_video_view_x;
  2024. tparam_remote.icy = session->conf.remote_video_view_y;
  2025. tparam_remote.uwidth = session->conf.remote_video_view_cx;
  2026. tparam_remote.uheight = session->conf.remote_video_view_cy;
  2027. //tparam_remote.uvideowidth = REC_COMMON_VIDEO_DSM_AGENT_WIDTH;
  2028. //tparam_remote.uvideoheight = REC_COMMON_VIDEO_DSM_AGENT_HEIGHT;
  2029. tparam_remote.ivideoformat = VIDEO_FORMAT_RGB24;
  2030. if (0 == session->premote_render->SetVideoRenderParam(&tparam_remote)) {
  2031. //session->premote_render->ShowVideoWindow();
  2032. }
  2033. }
  2034. else {
  2035. Dbg("session remote video render is null.");
  2036. }
  2037. }
  2038. if (session->plocal_render){
  2039. start_video(session);
  2040. //session->plocal_render->ShowVideoWindow();
  2041. bool bshow_local = false;
  2042. bool bset = true;
  2043. //session->premote_render->ShowVideoWindow();
  2044. for (; ; ) {
  2045. struct timespec ts;
  2046. clock_gettime(CLOCK_REALTIME, &ts);
  2047. long unsec = ts.tv_nsec + (1000 * 1000 * 500);
  2048. ts.tv_sec += (unsec / 1000000000);
  2049. ts.tv_nsec = (unsec % 1000000000);
  2050. if (0 != sem_timedwait(&session->ui_stop_sem, &ts) && (ETIMEDOUT == errno))
  2051. {
  2052. video_frame* local_video_frame = NULL;
  2053. int iwindowstate = get_local_video_frame(session, &local_video_frame);
  2054. if (iwindowstate != session->ilast_windstae){
  2055. set_video_windows(session, iwindowstate);
  2056. session->ilast_windstae = iwindowstate;
  2057. Dbg("%s:%d, window state is %d.", __FUNCTION__, __LINE__, iwindowstate);
  2058. }
  2059. if (NULL != local_video_frame) {
  2060. //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);
  2061. video_frame* localframe = NULL;
  2062. if (0 == translate_image_resolution(&localframe, session->conf.local_video_view_cx, session->conf.local_video_view_cy, local_video_frame)) {
  2063. session->plocal_render->RenderVideoFrame(localframe);
  2064. video_frame_delete(localframe);
  2065. localframe = NULL;
  2066. }
  2067. else {
  2068. session->plocal_render->RenderVideoFrame(local_video_frame);
  2069. }
  2070. if (false == session->blocalrender){
  2071. LogWarn(Severity_Middle, Error_Debug, EVENT_MOD_SIP_LOCAL_VIDEO_RENDER_STARTED, "start local video render.");
  2072. session->blocalrender = true;
  2073. }
  2074. bshow_local = true;
  2075. video_frame_delete(local_video_frame);
  2076. local_video_frame = NULL;
  2077. }
  2078. else {
  2079. //Dbg("%s:%d,get video from shm preview queue failed!", __FUNCTION__, __LINE__);
  2080. }
  2081. }
  2082. else {
  2083. Dbg("%s:%d videorender_func exit!", __FUNCTION__, __LINE__);
  2084. session->plocal_render->HideVideoWindow();
  2085. break;
  2086. }
  2087. if (bset){
  2088. if (bshow_local && session->bshow_remote) {
  2089. session->plocal_render->ShowVideoWindow();
  2090. session->premote_render->ShowVideoWindow();
  2091. bset = false;
  2092. }
  2093. }
  2094. }
  2095. }
  2096. return 0;
  2097. }
  2098. #endif // RVC_OS_WIN
  2099. static int start_ui(video_session_t *session)
  2100. {
  2101. LOG_FUNCTION();
  2102. Dbg("start ui session addr is 0x%08x", session);
  2103. #ifdef RVC_OS_WIN
  2104. session->ui_event = CreateEventA(NULL, FALSE, FALSE, NULL);
  2105. if (!session->ui_event) {
  2106. Dbg("phonemedia_start create ui event failed!");
  2107. return Error_Resource;
  2108. }
  2109. else{
  2110. Dbg("session->ui_event = 0x%8x", session->ui_event);
  2111. }
  2112. session->ui_thread = (HANDLE)_beginthreadex(NULL, 0, &ui_proc, session, 0, NULL);
  2113. if (!session->ui_thread) {
  2114. Dbg("phonemedia_start create ui thread failed!");
  2115. CloseHandle(session->ui_event);
  2116. session->ui_event = NULL;
  2117. return Error_Resource;
  2118. }
  2119. else{
  2120. Dbg("session %8x create session->ui_thread = %8x", session, session->ui_thread);
  2121. }
  2122. Dbg("ui thread created ok!");
  2123. {
  2124. HANDLE hs[] = {session->ui_event, session->ui_thread};
  2125. DWORD dwRet = WaitForMultipleObjects(array_size(hs), hs, FALSE, INFINITE);
  2126. if (dwRet == WAIT_OBJECT_0) { //
  2127. Dbg("wait for ui ready event ok!");
  2128. } else if (dwRet == WAIT_OBJECT_0 + 1) { // thread exit
  2129. Dbg("ui thread aborted unexpected!");
  2130. Dbg("CloseHandle ui_thread 0x%08x", session->ui_thread);
  2131. CloseHandle(session->ui_thread);
  2132. session->ui_thread = NULL;
  2133. Dbg("CloseHandle ui_event 0x%08x", session->ui_event);
  2134. CloseHandle(session->ui_event);
  2135. session->ui_event = NULL;
  2136. return Error_Resource;
  2137. }
  2138. }
  2139. return 0;
  2140. #else
  2141. if (0 != sem_init(&session->ui_stop_sem, 0, 0)) {
  2142. Dbg("%s:%d create ui stop event failed!", __FUNCTION__, __LINE__);
  2143. return Error_Resource;
  2144. }
  2145. else{
  2146. Dbg("%s:%d create ui stop event success!", __FUNCTION__, __LINE__);
  2147. }
  2148. int err = pthread_create(&session->ui_threadid, NULL, videorender_func, session);
  2149. if (0 == err) {
  2150. Dbg("create video render thread success, thread id is %u.", session->ui_threadid);
  2151. }
  2152. else {
  2153. Dbg("create video render thread failed.");
  2154. return Error_Resource;
  2155. }
  2156. return err;
  2157. #endif // RVC_OS_WIN
  2158. }
  2159. static void stop_ui(video_session_t *session)
  2160. {
  2161. LOG_FUNCTION();
  2162. Dbg("%s:%d session addr is 0x%08x.", __FUNCTION__, __LINE__, session);
  2163. #ifdef RVC_OS_WIN
  2164. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType) {
  2165. if (NULL != pg_last_session) {
  2166. pg_last_session->rtp = session->rtp;
  2167. pg_last_session->local_clock = session->local_clock;
  2168. CloseHandle(session->ui_thread);
  2169. session->ui_thread = NULL;
  2170. CloseHandle(session->ui_event);
  2171. session->ui_event = NULL;
  2172. session = pg_last_session;
  2173. }
  2174. }
  2175. if (NULL != session->pic_record) {
  2176. if (NULL != session->pic_record->work_thread) {
  2177. SetEvent(session->pic_record->evt);
  2178. Dbg("CloseHandle pic_record ui_thread 0x%08x", session->pic_record->work_thread);
  2179. CloseHandle(session->pic_record->work_thread);
  2180. session->pic_record->work_thread = NULL;
  2181. Dbg("CloseHandle pic_record ui_event 0x%08x", session->pic_record->evt);
  2182. CloseHandle(session->pic_record->evt);
  2183. session->pic_record->evt = NULL;
  2184. }
  2185. }
  2186. if (session->local_hwnd) {
  2187. Dbg("PostMessageA WM_CLOSE session addr is 0x%08x, session->local_hwnd = 0x%08x", session, session->local_hwnd);
  2188. BOOL bRet = PostMessageA(session->local_hwnd, WM_CLOSE, 0, 0);
  2189. Dbg("WaitForSingleObject session addr is %8x, and session->ui_thread is %8x.", session, session->ui_thread);
  2190. DWORD dCode = WaitForSingleObject(session->ui_thread, INFINITE);
  2191. Dbg("After WaitForSingleObject (session->ui_thread, INFINITE) session addr is %8x, and session->ui_thread = 0x%08x.", session, session->ui_thread);
  2192. Dbg("CloseHandle ui_thread 0x%08x", session->ui_thread);
  2193. CloseHandle(session->ui_thread);
  2194. session->ui_thread = NULL;
  2195. Dbg("CloseHandle ui_event 0x%08x", session->ui_event);
  2196. CloseHandle(session->ui_event);
  2197. session->ui_event = NULL;
  2198. }
  2199. pg_last_session = NULL;
  2200. pg_local_hwnd = NULL;
  2201. pg_remote_hwnd = NULL;
  2202. Dbg("set pg_last_session to null.");
  2203. #else
  2204. sem_post(&session->ui_stop_sem);
  2205. if (session->ui_threadid > 0) {
  2206. if (0 == pthread_join(session->ui_threadid, NULL)){
  2207. Dbg("video render thread %u pthread join success.", session->ui_threadid);
  2208. session->ui_threadid = 0;
  2209. }
  2210. else{
  2211. Dbg("video render thread pthread join error for %s", strerror(errno));
  2212. }
  2213. }
  2214. Dbg("ui thread exit!");
  2215. stop_video(session);
  2216. #endif // RVC_OS_WIN
  2217. }
  2218. static int load_record_area_png(video_session_t *session)
  2219. {
  2220. int iret = -1;
  2221. if (NULL == session){
  2222. return iret;
  2223. }
  2224. //加载双录人形背景框
  2225. char strPath[MAX_PATH_SIZE]={0};
  2226. char strRecordAreaPath[MAX_PATH_SIZE] = { 0 };
  2227. #ifdef RVC_OS_WIN
  2228. GetCurrentRunPath(strPath);
  2229. sprintf(strRecordAreaPath, "%s\\bin\\recordarea.png", strPath);
  2230. if (ExistsFile(strRecordAreaPath))
  2231. {
  2232. if (session->recordareaimage == NULL)
  2233. {
  2234. if((session->conf.eDeviceType == eMobilePadType)||(session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  2235. {
  2236. //pad版对人形区域放大处理
  2237. session->recordareaimage = cvLoadImage(strRecordAreaPath);
  2238. IplImage* tmp = cvCreateImage(cvSize(session->recordareaimage->width,session->recordareaimage->height),IPL_DEPTH_8U,3);
  2239. cvResize(session->recordareaimage ,tmp);
  2240. cvSetImageROI(tmp,cvRect(0,0,session->recordareaimage->width,session->recordareaimage->height));
  2241. //cvSetImageROI(tmp,cvRect(0,0,session->recordareaimage->width,session->recordareaimage->height));
  2242. cvCopy(tmp,session->recordareaimage);
  2243. cvReleaseImage(&tmp);
  2244. }
  2245. else
  2246. {
  2247. session->recordareaimage = cvLoadImage(strRecordAreaPath);
  2248. }
  2249. }
  2250. if (session->recordareamask == NULL )
  2251. {
  2252. if((session->conf.eDeviceType == eMobilePadType)||(session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  2253. {
  2254. //pad版对人形区域放大处理
  2255. session->recordareamask = cvLoadImage(strRecordAreaPath,0);
  2256. IplImage* tmp = cvCreateImage(cvSize(session->recordareamask->width,session->recordareamask->height),IPL_DEPTH_8U,1);
  2257. cvResize(session->recordareamask ,tmp);
  2258. cvSetImageROI(tmp,cvRect(0,0,session->recordareamask->width,session->recordareamask->height));
  2259. cvCopy(tmp,session->recordareamask);
  2260. cvReleaseImage(&tmp);
  2261. }
  2262. else
  2263. {
  2264. session->recordareamask = cvLoadImage(strRecordAreaPath,0);
  2265. }
  2266. }
  2267. Dbg("Load record area img Success");
  2268. iret = 0;
  2269. }
  2270. else
  2271. {
  2272. Dbg("Load record area img Fail");
  2273. session->recordareaimage = NULL;
  2274. session->recordareamask = NULL;
  2275. }
  2276. #endif // RVC_OS_WIN
  2277. return iret;
  2278. }
  2279. int Local_video_session_create(const video_session_conf_t *conf, video_session_t **p_session, const bool bremote)
  2280. {
  2281. LOG_FUNCTION();
  2282. video_session_t *session = ZALLOC_T(video_session_t);
  2283. if (session)
  2284. {
  2285. memcpy(&session->conf, conf, sizeof(video_session_conf_t));
  2286. session->bshow_remote = false;
  2287. session->ilast_windstae = 0;
  2288. if ((conf->eDeviceType == eDesk2SType||conf->eDeviceType == eDesk1SType||conf->eDeviceType == eDesk2SIntegratedType) && g_bIsSalesRecord)
  2289. {
  2290. //modify by clp 20191011 低柜区分是否远程连线模式双录
  2291. if (g_bEwsRecord)
  2292. {
  2293. session->video_shm_q_preview = new Clibvideoqueue(REC_COMMON_VIDEO_EWS_SHM_PREVIEW_QUEUE);
  2294. Dbg("[dbg] use ews preview share memory.");
  2295. }
  2296. else
  2297. {
  2298. session->video_shm_q_preview = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_PREVIEW_QUEUE);
  2299. Dbg("[dbg] use env preview share memory.");
  2300. }
  2301. }
  2302. else if ((conf->eDeviceType == ePadtype || conf->eDeviceType == eMobilePadType) && g_bEwsRecord)
  2303. {
  2304. session->video_shm_q_preview = new Clibvideoqueue(REC_COMMON_VIDEO_EWS_SHM_PREVIEW_QUEUE);
  2305. Dbg("[dbg] use ews preview share memory");
  2306. }
  2307. else
  2308. {
  2309. session->video_shm_q_preview = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_PREVIEW_QUEUE);
  2310. Dbg("[dbg] use env preview share memory");
  2311. }
  2312. if (bremote){
  2313. if (eStand2sType == conf->eDeviceType){
  2314. session->video_shm_q_remote = new Clibvideoqueue(REC_COMMON_VIDEO_REMOTE_SHM_RTP_QUEUE);
  2315. Dbg("[dbg] stand2s device, record remote video use 320*240, and video_shm_q_remote addr is 0x%08x.", session->video_shm_q_remote);
  2316. }
  2317. else{
  2318. session->video_shm_q_remote = new Clibvideoqueue(REC_COMMON_VIDEO_REMOTE_SHM_RTP_QUEUE);
  2319. 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);
  2320. }
  2321. }
  2322. if (conf->ref_Is_ActiveInspect != NULL)
  2323. {
  2324. if (*conf->ref_Is_ActiveInspect == 1)
  2325. {
  2326. session->video_error = NULL;
  2327. session->video_shm_q_env = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
  2328. if (conf->camera_count == 2)
  2329. {
  2330. session->video_shm_q_opt = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_SNAPSHOT_QUEUE);
  2331. }
  2332. }
  2333. }
  2334. session->local_clock = NULL;
  2335. if (false == bremote){
  2336. if (0 == load_record_area_png(session)){
  2337. Dbg("load record area picture success.");
  2338. }
  2339. else{
  2340. Dbg("load record area picture failed.");
  2341. }
  2342. }
  2343. *p_session = session;
  2344. }
  2345. return 0;
  2346. }
  2347. int video_session_create(const video_session_conf_t *conf, video_session_t **p_session)
  2348. {
  2349. LOG_FUNCTION();
  2350. video_session_t *session = ZALLOC_T(video_session_t);
  2351. if (session)
  2352. {
  2353. char str_local[MAX_PATH_SIZE] = {0};
  2354. char str_remote[MAX_PATH_SIZE] = {0};
  2355. translate_ipaddr_from_int(str_local, MAX_PATH_SIZE, conf->local_rtp_ip);
  2356. translate_ipaddr_from_int(str_remote, MAX_PATH_SIZE, conf->remote_rtp_ip);
  2357. 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.",
  2358. session, str_local, conf->local_rtp_port,conf->local_pt, str_remote, conf->remote_rtp_port, conf->remote_pt, conf->nCallType);
  2359. memcpy(&session->conf, conf, sizeof(video_session_conf_t));
  2360. session->bshow_remote = false;
  2361. session->ilast_windstae = 0;
  2362. session->video_shm_q_env = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE);
  2363. if (DOUBLERECORD_CALLTYPE != conf->nCallType){
  2364. session->video_shm_q_remote = new Clibvideoqueue(REC_COMMON_VIDEO_REMOTE_SHM_RTP_QUEUE);
  2365. session->video_shm_q_preview = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_PREVIEW_QUEUE);
  2366. }
  2367. else{
  2368. if (NULL != pg_last_session){
  2369. session->video_shm_q_remote = (pg_last_session)->video_shm_q_remote;
  2370. session->video_shm_q_preview = (pg_last_session)->video_shm_q_preview;
  2371. Dbg("double record the second period, reuse record video queue.");
  2372. }
  2373. }
  2374. char strPath[MAX_PATH_SIZE]={0};
  2375. char strImgPath[MAX_PATH_SIZE]={0};
  2376. #ifdef RVC_OS_WIN
  2377. GetCurrentRunPath(strPath);
  2378. sprintf(strImgPath, "%s\\bin\\error.jpg", strPath);
  2379. #else
  2380. strcpy_s(strImgPath, MAX_PATH_SIZE, "./bin/error.jpg");
  2381. #endif // RVC_OS_WIN
  2382. if (ExistsFile(strImgPath))
  2383. {
  2384. IplImage*img = cvLoadImage(strImgPath,1);
  2385. if (img != NULL)
  2386. {
  2387. Dbg("load img success");
  2388. session->video_error = new videoq_frame;
  2389. session->video_error->format = VIDEOQ_FORMAT_RGB24;
  2390. session->video_error->framesize = 320*180*3;
  2391. session->video_error->height = 180;
  2392. session->video_error->width = 320;
  2393. session->video_error->data = new unsigned char[320*180*3];
  2394. memcpy(session->video_error->data,img->imageData,320*180*3);
  2395. cvReleaseImage(&img);
  2396. }
  2397. }
  2398. else
  2399. {
  2400. Dbg("Load error img Fail");
  2401. session->video_error = NULL;
  2402. }
  2403. //加载人形背景框
  2404. char strPersonPath[MAX_PATH_SIZE] = {0};
  2405. #ifdef RVC_OS_WIN
  2406. sprintf(strPersonPath, "%s\\bin\\rxk.jpg", strPath);
  2407. #else
  2408. strcpy_s(strPersonPath, MAX_PATH_SIZE, "./bin/rxk.jpg");
  2409. #endif // RVC_OS_WIN
  2410. if (ExistsFile(strPersonPath))
  2411. {
  2412. if (session->personimage == NULL)
  2413. {
  2414. if((session->conf.eDeviceType == eMobilePadType)||(session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  2415. {
  2416. //pad版对人形区域放大处理
  2417. session->personimage = cvLoadImage(strPersonPath);
  2418. IplImage*tmp = cvCreateImage(cvSize(session->personimage->width*3/2,session->personimage->height*3/2),IPL_DEPTH_8U,3);
  2419. cvResize(session->personimage ,tmp);
  2420. cvSetImageROI(tmp,cvRect(session->personimage->width/4,50,session->personimage->width,session->personimage->height));
  2421. cvCopy(tmp,session->personimage);
  2422. cvReleaseImage(&tmp);
  2423. }
  2424. else
  2425. {
  2426. session->personimage = cvLoadImage(strPersonPath);
  2427. }
  2428. }
  2429. if (session->personmask == NULL )
  2430. {
  2431. if((session->conf.eDeviceType == eMobilePadType)||(session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  2432. {
  2433. //pad版对人形区域放大处理
  2434. session->personmask = cvLoadImage(strPersonPath,0);
  2435. IplImage*tmp = cvCreateImage(cvSize(session->personmask->width*3/2,session->personmask->height*3/2),IPL_DEPTH_8U,1);
  2436. cvResize(session->personmask ,tmp);
  2437. cvSetImageROI(tmp,cvRect(session->personmask->width/4,50,session->personmask->width,session->personmask->height));
  2438. cvCopy(tmp,session->personmask);
  2439. cvReleaseImage(&tmp);
  2440. }
  2441. else
  2442. {
  2443. session->personmask = cvLoadImage(strPersonPath,0);
  2444. }
  2445. }
  2446. //IplImage*persongrey = cvLoadImage(strPersonPath,CV_LOAD_IMAGE_GRAYSCALE);
  2447. //二值化提取人形框掩码
  2448. //cvThreshold(persongrey,session->personmask,150, 255, CV_THRESH_BINARY);
  2449. Dbg("Load person img Success");
  2450. }
  2451. else
  2452. {
  2453. Dbg("Load person img Fail");
  2454. session->personimage = NULL;
  2455. session->personmask = NULL;
  2456. }
  2457. #if 0
  2458. video_frame frame;
  2459. video_frame_alloc(320, 180, VIDEO_FORMAT_RGB24, &frame);
  2460. video_frame_fill_black(&frame);
  2461. videoq_frame frm;
  2462. frm.data = frame.data[0];
  2463. session->video_shm_q_env->GetVideo(&frm, 0);
  2464. video_frame_save_bmpfile("d:\\a.bmp", &frame);
  2465. #endif
  2466. if (conf->camera_count == 2) {
  2467. session->video_shm_q_opt = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
  2468. }
  2469. session->bvideorecved = false;
  2470. session->blocalrender = false;
  2471. session->bremoterender = false;
  2472. *p_session = session;
  2473. }
  2474. return 0;
  2475. }
  2476. int video_session_start(video_session_t *session)
  2477. {
  2478. LOG_FUNCTION();
  2479. Dbg("video_session_start session addr is 0x%08x, session call type is %d.", session, session->conf.nCallType);
  2480. int rc = start_ui(session);
  2481. return rc;
  2482. }
  2483. void video_session_stop(video_session_t *session)
  2484. {
  2485. LOG_FUNCTION();
  2486. Dbg("video session stop session addr is 0x%08x", session);
  2487. stop_ui(session);
  2488. }
  2489. void double_record_broadcast_video_session_stop()
  2490. {
  2491. LOG_FUNCTION();
  2492. #ifdef RVC_OS_WIN
  2493. if (NULL != pg_last_session){
  2494. if (NULL != pg_last_session->pic_record){
  2495. if (NULL != pg_last_session->pic_record->work_thread){
  2496. SetEvent(pg_last_session->pic_record->evt);
  2497. Dbg("CloseHandle pic_record ui_thread 0x%08x", pg_last_session->pic_record->work_thread);
  2498. CloseHandle(pg_last_session->pic_record->work_thread);
  2499. pg_last_session->pic_record->work_thread = NULL;
  2500. Dbg("CloseHandle pic_record ui_event 0x%08x", pg_last_session->pic_record->evt);
  2501. CloseHandle(pg_last_session->pic_record->evt);
  2502. pg_last_session->pic_record->evt = NULL;
  2503. }
  2504. }
  2505. }
  2506. if (pg_last_session && pg_last_session->local_hwnd) {
  2507. Dbg("PostMessageA WM_CLOSE session addr is 0x%08x, session->local_hwnd = 0x%08x", pg_last_session, pg_last_session->local_hwnd);
  2508. BOOL bRet = PostMessageA(pg_last_session->local_hwnd, WM_CLOSE, 0, 0);
  2509. Dbg("WaitForSingleObject session addr is %8x, and session->ui_thread is %8x.", pg_last_session, pg_last_session->ui_thread);
  2510. DWORD dCode = WaitForSingleObject(pg_last_session->ui_thread, INFINITE);
  2511. 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);
  2512. Dbg("CloseHandle ui_thread 0x%08x", pg_last_session->ui_thread);
  2513. CloseHandle(pg_last_session->ui_thread);
  2514. pg_last_session->ui_thread = NULL;
  2515. Dbg("CloseHandle ui_event 0x%08x", pg_last_session->ui_event);
  2516. CloseHandle(pg_last_session->ui_event);
  2517. pg_last_session->ui_event = NULL;
  2518. }
  2519. pg_last_session = NULL;
  2520. pg_local_hwnd = NULL;
  2521. pg_remote_hwnd = NULL;
  2522. Dbg("set pg_last_session, pg_local_hwnd, pg_remote_hwnd to null.");
  2523. #else
  2524. #endif // RVC_OS_WIN
  2525. }
  2526. void video_session_destroy(video_session_t *session)
  2527. {
  2528. LOG_FUNCTION();
  2529. #ifdef RVC_OS_WIN
  2530. if (NULL != session) {
  2531. if (NULL != session->pic_record) {
  2532. if (NULL != session->pic_record->work_thread) {
  2533. SetEvent(session->pic_record->evt);
  2534. Dbg("CloseHandle pic_record ui_thread 0x%08x", session->pic_record->work_thread);
  2535. CloseHandle(session->pic_record->work_thread);
  2536. session->pic_record->work_thread = NULL;
  2537. Dbg("CloseHandle pic_record ui_event 0x%08x", session->pic_record->evt);
  2538. CloseHandle(session->pic_record->evt);
  2539. session->pic_record->evt = NULL;
  2540. }
  2541. }
  2542. if (session->video_shm_q_env) {
  2543. delete session->video_shm_q_env;
  2544. session->video_shm_q_env = NULL;
  2545. }
  2546. if (session->video_shm_q_opt) {
  2547. delete session->video_shm_q_opt;
  2548. session->video_shm_q_opt = NULL;
  2549. }
  2550. if (session->video_shm_q_preview) {
  2551. delete session->video_shm_q_preview;
  2552. session->video_shm_q_preview = NULL;
  2553. }
  2554. }
  2555. #else
  2556. #endif // RVC_OS_WIN
  2557. if (session->video_error){
  2558. delete session->video_error->data;
  2559. delete session->video_error;
  2560. }
  2561. if (session->video_shm_q_remote) {
  2562. delete session->video_shm_q_remote;
  2563. session->video_shm_q_remote = NULL;
  2564. }
  2565. if (session->personimage){
  2566. cvReleaseImage(&session->personimage);
  2567. }
  2568. if (session->personmask){
  2569. cvReleaseImage(&session->personmask);
  2570. }
  2571. if (session->recordareaimage){
  2572. cvReleaseImage(&session->recordareaimage);
  2573. }
  2574. if (session->recordareamask){
  2575. cvReleaseImage(&session->recordareamask);
  2576. }
  2577. free(session);
  2578. }
  2579. void av_log_cb(void*ptr, int level, const char*fmt, va_list list)
  2580. {
  2581. vDbg(fmt, list);
  2582. }
  2583. int video_lib_init()
  2584. {
  2585. #ifdef RVC_OS_WIN
  2586. CoInitialize(NULL);
  2587. {
  2588. int i, n;
  2589. n = videocap_get_device_count();
  2590. for (i = 0; i < n; ++i) {
  2591. WCHAR tmp[128];
  2592. char t[128];
  2593. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  2594. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  2595. Dbg("%d = %s", i, t);
  2596. }
  2597. }
  2598. videoframework_init();
  2599. av_log_set_callback(&av_log_cb);
  2600. //av_log_set_level(AV_LOG_DEBUG);
  2601. av_log_set_level(AV_LOG_QUIET);
  2602. #else
  2603. videoframework_init();
  2604. if (0 == VideoRender_Init()){
  2605. Dbg("sdl init success.");
  2606. }
  2607. else {
  2608. Dbg("sdl init failed.");
  2609. }
  2610. //av_log_set_callback(&av_log_cb);
  2611. //av_log_set_level(AV_LOG_TRACE);
  2612. //av_log_set_level(AV_LOG_QUIET);
  2613. #endif // RVC_OS_WIN
  2614. return 0;
  2615. }
  2616. void video_lib_deinit()
  2617. {
  2618. #ifdef RVC_OS_WIN
  2619. CoUninitialize();
  2620. #else
  2621. #endif // RVC_OS_WIN
  2622. videoframework_term();
  2623. VideoRender_Term();
  2624. }