video_session.cpp 84 KB

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