video_session.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "video_session.h"
  4. #include "videoframework.h"
  5. #include "memutil.h"
  6. #include "libvideoqueue.h"
  7. #include "rvc_media_common.h"
  8. #include "common_video/videocommon.h"
  9. #include "videoutil.h"
  10. #include "videortp.h"
  11. #include "videoclock.h"
  12. #include "fileutil.h"
  13. #include "Event.h"
  14. #ifdef RVC_OS_WIN
  15. #include "videoplayer.h"
  16. #include "videocap.h"
  17. #include <ipp.h>
  18. #include <io.h>
  19. #include <DbgHelp.h>
  20. #pragma comment(lib, "dbghelp.lib")
  21. #else
  22. #include "ivideorenderinterface.h"
  23. #include "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 "video_common/ffmpeg_api_cpp_adapter.h"
  45. #define WNDCLS_NAME "sipmedia_video"
  46. #define MAX_PATH_SIZE 256
  47. #ifndef RVC_DBRECORD_VIDEO_BASE
  48. #define RVC_DBRECORD_VIDEO_BASE 1000
  49. #endif // !RVC_DBRECORD_VIDEO_BASE
  50. struct video_session_t
  51. {
  52. video_session_conf_t conf;
  53. Clibvideoqueue *video_shm_q_env;// env rtp queue
  54. Clibvideoqueue *video_shm_q_opt;// opt rtp queue
  55. Clibvideoqueue *video_shm_q_preview; // preview queue
  56. Clibvideoqueue *video_shm_q_remote; // preview queue
  57. videoq_frame *video_error;
  58. videortp_t *rtp;
  59. videoclock_t local_clock;
  60. struct SwsContext *local_encode_sws_ctx_env;
  61. struct SwsContext *local_encode_sws_ctx_opt;
  62. IplImage*personimage;
  63. IplImage*personmask;
  64. IplImage*recordareaimage;
  65. IplImage*recordareamask;
  66. int irecv_frameid;
  67. #ifdef RVC_OS_WIN
  68. videoplayer_t* local_player;
  69. videoplayer_t* remote_player;
  70. HANDLE ui_thread;
  71. HANDLE ui_event;
  72. HWND local_hwnd; // preview window
  73. HWND remote_hwnd; // remote window
  74. picture_record_t* pic_record;
  75. #endif // RVC_OS_WIN
  76. bool bvideorecved;
  77. volatile bool blocalrender;
  78. volatile bool bremoterender;
  79. bool bcamera_error_posted;
  80. };
  81. static void __dbg(void *user_data, const char *fmt, va_list arg)
  82. {
  83. int n = vsnprintf(NULL, 0, fmt, arg);
  84. if (n >= MAX_PATH) {
  85. char* buf = (char*)malloc((size_t)(n + 1));
  86. vsnprintf(buf, n + 1, fmt, arg);
  87. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s", buf);
  88. free(buf);
  89. }
  90. else {
  91. char strlog[MAX_PATH] = { 0 };
  92. vsnprintf(strlog, MAX_PATH, fmt, arg);
  93. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s", strlog);
  94. }
  95. }
  96. static void __logevent(void* user_data, int itype, const char* strmessage)
  97. {
  98. if (0 == itype) {
  99. LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_VIDEO_RTP_CREATE, strmessage);
  100. }
  101. else {
  102. LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_VIDEO_RTP_DESTROY, strmessage);
  103. }
  104. }
  105. static bool __camera_error_event(bool bhaspost, int icameraid)
  106. {
  107. bool bret = false;
  108. if (false == bhaspost){
  109. if (0 == icameraid){
  110. LogWarn(Severity_Middle, Error_Debug, ERROR_MOD_SIP_GET_ENV_VIDEO_FAILED, "get video from env queue failed!");
  111. }
  112. else{
  113. LogWarn(Severity_Middle, Error_Debug, ERROR_MOD_SIP_GET_OPT_VIDEO_FAILED, "get video from opt queue failed!");
  114. }
  115. LogEvent(Severity_Middle, EVENT_MOD_SIP_GET_VIDEO_FAILED, "connected and get video failed!");
  116. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setResultCode("RTA3119")("接通坐席后本地视频回显异常");
  117. bret = true;
  118. }
  119. return bret;
  120. }
  121. static int translate_image_resolution(video_frame** dstframe,const int iwidth, const int iheight, const video_frame* psrcframe)
  122. {
  123. int i = -1;
  124. if (iwidth == psrcframe->width && iheight == psrcframe->height){
  125. return i;
  126. }
  127. video_frame* pframe = video_frame_new(iwidth, iheight,VIDEO_FORMAT_RGB24);
  128. video_frame_fill_black(pframe);
  129. SwsContext* sws = sws_getCachedContext(NULL, psrcframe->width, psrcframe->height,
  130. PIX_FMT_BGR24,
  131. iwidth,
  132. iheight,
  133. PIX_FMT_BGR24,
  134. SWS_LANCZOS,
  135. NULL,
  136. NULL,
  137. NULL);
  138. sws_scale(sws, psrcframe->data, psrcframe->linesize, 0, psrcframe->height, pframe->data, pframe->linesize);
  139. sws_freeContext(sws);
  140. *dstframe = pframe;
  141. i = 0;
  142. return i;
  143. }
  144. static int calc_capture_mode(int width, int height, int *mode)
  145. {
  146. const struct {
  147. int mode;
  148. int width;
  149. int height;
  150. } modes [] = {
  151. {VIDEOCAP_FRAME_SQCIF, VIDEOCAP_SQCIF_WIDTH, VIDEOCAP_SQCIF_HEIGHT},
  152. {VIDEOCAP_FRAME_QQVGA, VIDEOCAP_QQVGA_WIDTH, VIDEOCAP_QQVGA_HEIGHT},
  153. {VIDEOCAP_FRAME_QCIF, VIDEOCAP_QCIF_WIDTH, VIDEOCAP_QCIF_HEIGHT},
  154. {VIDEOCAP_FRAME_QVGA, VIDEOCAP_QVGA_WIDTH, VIDEOCAP_QVGA_HEIGHT},
  155. {VIDEOCAP_FRAME_CIF, VIDEOCAP_CIF_WIDTH, VIDEOCAP_CIF_HEIGHT},
  156. {VIDEOCAP_FRAME_VGA, VIDEOCAP_VGA_WIDTH, VIDEOCAP_VGA_HEIGHT},
  157. {VIDEOCAP_FRAME_4CIF, VIDEOCAP_4CIF_WIDTH, VIDEOCAP_4CIF_HEIGHT},
  158. {VIDEOCAP_FRAME_SVGA, VIDEOCAP_SVGA_WIDTH, VIDEOCAP_SVGA_HEIGHT},
  159. {VIDEOCAP_FRAME_NHD, VIDEOCAP_NHD_WIDTH, VIDEOCAP_NHD_HEIGHT},
  160. {VIDEOCAP_FRAME_SXGA, VIDEOCAP_SXGA_WIDTH, VIDEOCAP_SXGA_HEIGHT},
  161. {VIDEOCAP_FRAME_720P, VIDEOCAP_720P_WIDTH, VIDEOCAP_720P_HEIGHT},
  162. {VIDEOCAP_FRAME_1080P, VIDEOCAP_1080P_WIDTH, VIDEOCAP_1080P_HEIGHT},
  163. };
  164. int i;
  165. for (i = 0; i < array_size(modes); ++i) {
  166. if (modes[i].width == width && modes[i].height == height) {
  167. *mode = modes[i].mode;
  168. return 0;
  169. }
  170. }
  171. return Error_NotExist;
  172. }
  173. #ifdef RVC_OS_WIN
  174. static void __delete_frame(videoplayer_t *player, void *user_data, video_frame *frame)
  175. {
  176. video_frame_delete(frame);
  177. }
  178. #endif
  179. static int video_shm_enqueue(Clibvideoqueue *shm_queue, video_frame *frame, int flags, int iframeid)
  180. {
  181. videoq_frame tmp_frm;
  182. tmp_frm.data = frame->data[0];
  183. tmp_frm.framesize = frame->width * frame->height * 3;
  184. tmp_frm.format = VIDEOQ_FORMAT_RGB24;
  185. tmp_frm.width = frame->width;
  186. tmp_frm.height = frame->height;
  187. tmp_frm.iframeid = iframeid;
  188. unsigned int nowtime = y2k_time_now();
  189. if (!shm_queue->InsertVideo(&tmp_frm, flags,nowtime))
  190. {
  191. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("caution: shm_queue video insert shm video failed!");
  192. return Error_Unexpect;
  193. }
  194. else
  195. {
  196. return Error_Succeed;
  197. }
  198. }
  199. static inline Clibvideoqueue *get_active_videoqueue(video_session_t *session)
  200. {
  201. int agent_option = *session->conf.ref_camera_switch;
  202. int facetracking_option = *session->conf.ref_active_camera;
  203. int camera_state = *session->conf.ref_camera_state;
  204. Clibvideoqueue *active = NULL;
  205. if (agent_option == CAMERA_TYPE_ENV)
  206. {
  207. if((camera_state!=CAMERA_TYPE_ENV)&&(camera_state!=CAMERA_TYPE_AUTO))
  208. {
  209. active = NULL;
  210. }
  211. else
  212. {
  213. active = session->video_shm_q_env;
  214. }
  215. }
  216. else if (agent_option == CAMERA_TYPE_OPT)
  217. {
  218. if((camera_state!=CAMERA_TYPE_OPT)&&(camera_state!=CAMERA_TYPE_AUTO))
  219. {
  220. active = NULL;
  221. }
  222. else
  223. {
  224. active = session->video_shm_q_opt;
  225. }
  226. }
  227. else if(agent_option == CAMERA_TYPE_ERROR)
  228. {
  229. active = NULL;
  230. }
  231. else
  232. { // auto
  233. if (camera_state == CAMERA_TYPE_AUTO)
  234. {
  235. if (facetracking_option == CAMERA_TYPE_ENV)
  236. {
  237. active = session->video_shm_q_env;
  238. }
  239. else
  240. { // opt
  241. active = session->video_shm_q_opt;
  242. }
  243. }
  244. else if (camera_state == CAMERA_TYPE_ENV)
  245. {
  246. active = session->video_shm_q_env;
  247. }
  248. else if (camera_state == CAMERA_TYPE_OPT)
  249. {
  250. active = session->video_shm_q_opt;
  251. }
  252. else if (camera_state == CAMERA_TYPE_ERROR)
  253. {
  254. active = NULL;
  255. }
  256. }
  257. return active;
  258. }
  259. static void local_get_frame(void *user_data, video_frame *frame)
  260. {
  261. video_session_t *session = (video_session_t*)user_data;
  262. Clibvideoqueue *q = get_active_videoqueue(session);
  263. if (q)
  264. {
  265. videoq_frame frm;
  266. frm.data = frame->data[0];
  267. if (q->GetVideo(&frm, 0))
  268. {
  269. //video_frame_fill_black(frame);
  270. frame->width = frm.width;
  271. frame->height = frm.height;
  272. frame->linesize[0] = frm.width * 3;
  273. }
  274. }
  275. else
  276. {
  277. //video_frame_fill_black(frame);
  278. //贴图
  279. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("get active cam is null, load error img.");
  280. if (session->video_error != NULL)
  281. {
  282. frame->width = session->video_error->width;
  283. frame->height = session->video_error->height;
  284. frame->linesize[0] = session->video_error->width * 3;
  285. memcpy(frame->data[0],session->video_error->data,frame->width*frame->height*3);
  286. }
  287. else
  288. {
  289. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("video error img is null");
  290. }
  291. }
  292. }
  293. static void local_put_frame(void *user_data, video_frame *frame)
  294. {
  295. video_session_t *session = (video_session_t*)user_data;
  296. video_frame *tmp_frame_encode = NULL;
  297. // send out
  298. {
  299. tmp_frame_encode = video_frame_new(REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_I420);
  300. video_frame_fill_black(tmp_frame_encode);
  301. if (frame->width == REC_COMMON_VIDEO_RTP_ENV_WIDTH && frame->height == REC_COMMON_VIDEO_RTP_ENV_HEIGHT) { // env
  302. int offset = (REC_COMMON_VIDEO_RTP_HEIGHT - REC_COMMON_VIDEO_RTP_ENV_HEIGHT) / 2;
  303. unsigned char *dst_data[4] = {tmp_frame_encode->data[0], tmp_frame_encode->data[1], tmp_frame_encode->data[2], NULL};
  304. dst_data[0] += offset * tmp_frame_encode->linesize[0];
  305. dst_data[1] += offset / 2 * tmp_frame_encode->linesize[1];
  306. dst_data[2] += offset / 2 * tmp_frame_encode->linesize[2];
  307. sws_scale(session->local_encode_sws_ctx_env, frame->data, frame->linesize, 0, frame->height, dst_data, tmp_frame_encode->linesize);
  308. } else if (frame->width == REC_COMMON_VIDEO_RTP_OPT_WIDTH && frame->height == REC_COMMON_VIDEO_RTP_OPT_HEIGHT) { // opt
  309. video_frame tt;
  310. video_frame_alloc(REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_RGB24, &tt);
  311. video_frame_fill_black(&tt);
  312. {
  313. int offset = (REC_COMMON_VIDEO_RTP_WIDTH - REC_COMMON_VIDEO_RTP_OPT_WIDTH) / 2;
  314. unsigned char *dst_data[4] = {tt.data[0], 0, 0, 0};
  315. dst_data[0] += offset * 3;
  316. #ifdef RVC_OS_WIN
  317. IppiSize size;
  318. size.width = frame->width;
  319. size.height = frame->height;
  320. ippiCopy_8u_C3R(frame->data[0], frame->linesize[0], tt.data[0] + offset * 3, tt.linesize[0], size);
  321. #else
  322. for (int i = 0; i < frame->height; i++){
  323. memcpy(dst_data[0] + i*tt.linesize[0] , frame->data[0] + i*frame->width*3, frame->width*3);
  324. }
  325. #endif // RVC_OS_WIN
  326. }
  327. sws_scale(session->local_encode_sws_ctx_opt, tt.data, tt.linesize, 0, tt.height, tmp_frame_encode->data, tmp_frame_encode->linesize);
  328. video_frame_free(&tt);
  329. }
  330. if (session->conf.local_pt == REC_COMMON_VIDEO_PT){
  331. videortp_send_frame(session->rtp, tmp_frame_encode);
  332. }
  333. else{
  334. videortp_send_yuvframe(session->rtp, tmp_frame_encode);
  335. }
  336. }
  337. if (tmp_frame_encode) {
  338. video_frame_delete(tmp_frame_encode);
  339. }
  340. }
  341. static int on_rx_frame(video_frame *frame, void *user_data)
  342. {
  343. video_session_t *session = (video_session_t *)user_data;
  344. session->irecv_frameid++;
  345. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType) {
  346. if (false == session->bremoterender) {
  347. session->conf.video_render_cb.on_stop_remote_video_render(session->conf.video_render_cb.user_data);
  348. }
  349. if (eStand2sType == session->conf.eDeviceType || eStand1SPlusType == session->conf.eDeviceType) {
  350. //大机对远端视频进行缩放匹配
  351. video_frame* recordframe = NULL;
  352. if (0 == translate_image_resolution(&recordframe, REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_HEIGHT, frame)) {
  353. int rc = video_shm_enqueue(session->video_shm_q_remote, recordframe, VIDEOQUEUE_FLAG_VERTICAL_FLIP, session->irecv_frameid);
  354. if (rc != Error_Succeed) {
  355. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("eStand2sType record call mod insert remote video to queue failed.");
  356. }
  357. //else {
  358. // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d video frame (session->irecv_frameid = %d) video_shm_enqueue success.", __FUNCTION__, __LINE__, session->irecv_frameid);
  359. //}
  360. video_frame_delete(recordframe);
  361. recordframe = NULL;
  362. }
  363. }
  364. else {
  365. int rc = video_shm_enqueue(session->video_shm_q_remote, frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP, session->irecv_frameid);
  366. if (rc != Error_Succeed) {
  367. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("not eStand2sType record call mod insert remote video to queue.");
  368. }
  369. }
  370. }
  371. int used = 0;
  372. #ifdef RVC_OS_WIN
  373. if (session->remote_player)
  374. {
  375. int rc = videoplayer_queue_frame(session->remote_player, frame, &__delete_frame, NULL);
  376. if (rc == 0) {
  377. used = 1;
  378. }
  379. }
  380. #else
  381. session->conf.video_render_cb.on_remote_video_render(frame, session->conf.video_render_cb.user_data);
  382. used = 1;
  383. if (false == session->bremoterender) {
  384. LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_REMOTE_VIDEO_RENDER_STARTED, "start remote video render.");
  385. session->bremoterender = true;
  386. }
  387. #endif
  388. return used;
  389. }
  390. static void on_rx_udp(const char* buf, int size, void* user_data)
  391. {
  392. video_session_t* session = (video_session_t*)user_data;
  393. if (false == session->bvideorecved) {
  394. //char strmsg[MAX_PATH] = { 0 };
  395. //snprintf(strmsg, MAX_PATH, "received first video packet, and packet size is %d.", size);
  396. //LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_VIDEO_STREAM_RECEIVED, strmsg);
  397. session->bvideorecved = true;
  398. }
  399. }
  400. #ifdef RVC_OS_WIN
  401. int GetCurrentRunPath(char* pPath)
  402. {
  403. char* pBuf = new char[MAX_PATH_SIZE];
  404. if (pBuf == NULL)
  405. return -1;
  406. ZeroMemory(pBuf, MAX_PATH_SIZE);
  407. GetModuleFileName(NULL, pBuf, MAX_PATH_SIZE);
  408. int len = strnlen_s(pBuf, MAX_PATH_SIZE);
  409. if (len <= 0)
  410. {
  411. delete[]pBuf;
  412. return -2;
  413. }
  414. char* pch;
  415. pch = strstr(pBuf, "bin");
  416. if (pch == NULL)
  417. return -3;
  418. int lenDel = strnlen_s(pch, MAX_PATH_SIZE);
  419. if (len <= 0)
  420. {
  421. delete[]pBuf;
  422. return -3;
  423. }
  424. strncpy_s(pPath, MAX_PATH_SIZE, pBuf, len - lenDel);
  425. delete[]pBuf;
  426. return strnlen_s(pPath, MAX_PATH_SIZE);
  427. }
  428. //远端视频窗口状态回调
  429. static int on_remoteWinstate(videoplayer_t *player, void *user_data, video_frame **frame)
  430. {
  431. video_session_t *session = (video_session_t*)user_data;
  432. return *session->conf.ref_window_state ;
  433. }
  434. //本地回显回调
  435. static int on_pull(videoplayer_t *player, void *user_data, video_frame **frame)
  436. {
  437. video_session_t *session = (video_session_t*)user_data;
  438. video_frame *tmp_frame_preview;
  439. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("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);
  440. if((session->conf.ref_active_img == NULL)&&(session->conf.ref_Is_ActiveInspect == NULL))
  441. {
  442. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24);
  443. videoq_frame frm;
  444. frm.data = tmp_frame_preview->data[0];
  445. BOOL result = session->video_shm_q_preview->GetVideo(&frm, VIDEOQUEUE_FLAG_HORIZONTAL_FLIP);
  446. //人形框融合处理
  447. if (result&&session->personimage!=NULL&&session->personmask!=NULL&&*session->conf.ref_Is_showPersonArea==1)
  448. {
  449. IplImage*img = cvCreateImageHeader(cvSize(frm.width,frm.height),IPL_DEPTH_8U,3);
  450. img->imageData = (char*)frm.data;
  451. if (frm.width!=session->personimage->width)
  452. {
  453. IplImage*tmp = cvCreateImage(cvSize(frm.width,frm.height),IPL_DEPTH_8U,3);
  454. IplImage*tmpmask = cvCreateImage(cvSize(frm.width,frm.height),IPL_DEPTH_8U,1);
  455. cvResize(session->personimage,tmp);
  456. cvResize(session->personmask,tmpmask);
  457. cvAdd(img,tmp,img,tmpmask);
  458. cvReleaseImage(&tmp);
  459. cvReleaseImage(&tmpmask);
  460. }
  461. else
  462. {
  463. cvAdd(img,session->personimage,img,session->personmask);
  464. }
  465. cvReleaseImageHeader(&img);
  466. }
  467. }
  468. else
  469. {
  470. if (*session->conf.ref_active_img == 1)
  471. {
  472. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_WIDTH, VIDEO_FORMAT_RGB24);
  473. video_frame_fill_black(tmp_frame_preview);
  474. if (session->video_error == NULL)
  475. {
  476. char strPath[MAX_PATH_SIZE] = {0};
  477. #ifdef RVC_OS_WIN
  478. GetCurrentRunPath(strPath);
  479. sprintf(strPath, "%s\\bin\\looklowerscreen.jpg", strPath);
  480. #else
  481. #endif // RVC_OS_WIN
  482. if (ExistsFile(strPath))
  483. {
  484. IplImage*img = cvLoadImage(strPath,1);
  485. if (img != NULL)
  486. {
  487. session->video_error = new videoq_frame;
  488. session->video_error->format = VIDEOQ_FORMAT_RGB24;
  489. session->video_error->framesize = img->imageSize;
  490. session->video_error->height = img->width;
  491. session->video_error->width = img->height;
  492. session->video_error->data = new unsigned char[img->imageSize];
  493. memcpy(session->video_error->data,img->imageData,img->imageSize);
  494. cvReleaseImage(&img);
  495. }
  496. }
  497. }
  498. if (session->video_error != NULL)
  499. {
  500. SwsContext*sws = sws_getContext(session->video_error->width, session->video_error->height,PIX_FMT_BGR24,
  501. REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  502. REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  503. PIX_FMT_BGR24,
  504. SWS_POINT, NULL, NULL, NULL);
  505. uint8_t *src_data[4] = {(unsigned char*)session->video_error->data+(session->video_error->height-1)*session->video_error->width*3,NULL,NULL,NULL};
  506. int src_linesize[4] = {-session->video_error->width*3,0,0,0};
  507. unsigned char *dst[4] = {tmp_frame_preview->data[0],NULL,NULL,NULL};
  508. int dst_linesize[4] = {tmp_frame_preview->linesize[0],0,0,0};
  509. sws_scale(sws, src_data, src_linesize, 0, session->video_error->height, dst, dst_linesize);
  510. sws_freeContext(sws);
  511. }
  512. }
  513. else if (*session->conf.ref_Is_ActiveInspect == 1)
  514. {
  515. if((session->conf.eDeviceType == eMobilePadType)||(session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  516. {
  517. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24);
  518. videoq_frame frm;
  519. frm.data = tmp_frame_preview->data[0];
  520. session->video_shm_q_preview->GetVideo(&frm, VIDEOQUEUE_FLAG_HORIZONTAL_FLIP);
  521. }
  522. else
  523. {
  524. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_WIDTH, VIDEO_FORMAT_RGB24);
  525. video_frame_fill_black(tmp_frame_preview);
  526. if ((*session->conf.ref_camera_switch == CAMERA_TYPE_ENV)||(*session->conf.ref_camera_switch == CAMERA_TYPE_AUTO))//显示上摄像头
  527. {
  528. if (session->video_shm_q_env)
  529. {
  530. videoq_frame frm;
  531. int offset = (REC_COMMON_VIDEO_SNAPSHOT_WIDTH - REC_COMMON_VIDEO_SNAPSHOT_HEIGHT) / 2;
  532. frm.data = tmp_frame_preview->data[0] + offset * tmp_frame_preview->linesize[0];
  533. session->video_shm_q_env->GetVideo(&frm, VIDEOQUEUE_FLAG_VERTICAL_FLIP);
  534. }
  535. }
  536. else if (*session->conf.ref_camera_switch == CAMERA_TYPE_OPT)//显示下摄像头
  537. {
  538. if (session->video_shm_q_opt)
  539. {
  540. video_frame tt = {0};
  541. int offset = (REC_COMMON_VIDEO_SNAPSHOT_WIDTH-REC_COMMON_VIDEO_SNAPSHOT_HEIGHT) / 2;
  542. tt.data[0] = tmp_frame_preview->data[0] + offset *3;
  543. tt.format = tmp_frame_preview->format;
  544. tt.linesize[0] = tmp_frame_preview->linesize[0];
  545. tt.width = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
  546. tt.height = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
  547. session->video_shm_q_opt->GetVideo2(&tt, VIDEOQUEUE_FLAG_VERTICAL_FLIP);
  548. }
  549. }
  550. }
  551. }
  552. else
  553. {
  554. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24);
  555. videoq_frame frm;
  556. frm.data = tmp_frame_preview->data[0];
  557. BOOL result = session->video_shm_q_preview->GetVideo(&frm, VIDEOQUEUE_FLAG_HORIZONTAL_FLIP);
  558. //人形框融合处理
  559. if (result&&session->personimage!=NULL&&session->personmask!=NULL&&*session->conf.ref_Is_showPersonArea==1)
  560. {
  561. IplImage*img = cvCreateImageHeader(cvSize(frm.width,frm.height),IPL_DEPTH_8U,3);
  562. img->imageData = (char*)frm.data;
  563. if (frm.width!=session->personimage->width)
  564. {
  565. IplImage*tmp = cvCreateImage(cvSize(frm.width,frm.height),IPL_DEPTH_8U,3);
  566. IplImage*tmpmask = cvCreateImage(cvSize(frm.width,frm.height),IPL_DEPTH_8U,1);
  567. cvResize(session->personimage,tmp);
  568. cvResize(session->personmask,tmpmask);
  569. cvAdd(img,tmp,img,tmpmask);
  570. cvReleaseImage(&tmp);
  571. cvReleaseImage(&tmpmask);
  572. }
  573. else
  574. {
  575. cvAdd(img,session->personimage,img,session->personmask);
  576. }
  577. cvReleaseImageHeader(&img);
  578. }
  579. //双录人形位置框融合处理
  580. if (result&&session->recordareaimage!=NULL&&session->recordareamask!=NULL&&*session->conf.ref_Is_showRecordArea==1)
  581. {
  582. IplImage*img = cvCreateImageHeader(cvSize(frm.width,frm.height),IPL_DEPTH_8U,3);
  583. img->imageData = (char*)frm.data;
  584. if (frm.width!=session->recordareaimage->width)
  585. {
  586. IplImage*tmp = cvCreateImage(cvSize(frm.width,frm.height),IPL_DEPTH_8U,3);
  587. IplImage*tmpmask = cvCreateImage(cvSize(frm.width,frm.height),IPL_DEPTH_8U,1);
  588. cvResize(session->recordareaimage,tmp);
  589. cvResize(session->recordareamask,tmpmask);
  590. cvAdd(img,tmp,img,tmpmask);
  591. cvReleaseImage(&tmp);
  592. cvReleaseImage(&tmpmask);
  593. }
  594. else
  595. {
  596. cvAdd(img,session->recordareaimage,img,session->recordareamask);
  597. }
  598. cvReleaseImageHeader(&img);
  599. }
  600. }
  601. }
  602. *frame = tmp_frame_preview;
  603. //if (*session->conf.ref_window_state == 1)
  604. //{
  605. // return 1;
  606. //}
  607. //else if (*session->conf.ref_window_state == 2)
  608. //{
  609. // return 2;
  610. //}
  611. //else
  612. //{
  613. // return 0;
  614. //}
  615. return *session->conf.ref_window_state;
  616. }
  617. static void free_frame(videoplayer_t *player, void *user_data, video_frame *frame)
  618. {
  619. video_frame_delete(frame);
  620. }
  621. #else
  622. //视频窗口显示状态,0:正常大小显示,1:放大一倍显示,2:隐藏全部窗口,3:从隐藏状态恢复为全部显示,4:显示远程窗口,隐藏本地窗口, 5:缩放显示
  623. int set_video_windows(video_session_t* psession, int iwindowstate)
  624. {
  625. int iret = -1;
  626. if (NULL == psession){
  627. return iret;
  628. }
  629. return iret;
  630. }
  631. //本地回显回调
  632. static int get_local_video_frame(void* user_data, video_frame** frame)
  633. {
  634. video_session_t* session = (video_session_t*)user_data;
  635. video_frame* tmp_frame_preview = NULL;
  636. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("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);
  637. if ((session->conf.ref_active_img == NULL) && (session->conf.ref_Is_ActiveInspect == NULL))
  638. {
  639. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24);
  640. videoq_frame frm;
  641. frm.data = tmp_frame_preview->data[0];
  642. BOOL result = session->video_shm_q_preview->GetVideo(&frm, VIDEOQUEUE_FLAG_HORIZONTAL_FLIP);
  643. //人形框融合处理
  644. if (result){
  645. if (session->personimage != NULL && session->personmask != NULL && *session->conf.ref_Is_showPersonArea == 1)
  646. {
  647. IplImage* img = cvCreateImageHeader(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 3);
  648. img->imageData = (char*)frm.data;
  649. if (frm.width != session->personimage->width){
  650. IplImage* tmp = cvCreateImage(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 3);
  651. IplImage* tmpmask = cvCreateImage(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 1);
  652. cvResize(session->personimage, tmp);
  653. cvResize(session->personmask, tmpmask);
  654. cvAdd(img, tmp, img, tmpmask);
  655. cvReleaseImage(&tmp);
  656. cvReleaseImage(&tmpmask);
  657. }
  658. else{
  659. cvAdd(img, session->personimage, img, session->personmask);
  660. }
  661. cvReleaseImageHeader(&img);
  662. }
  663. }
  664. else {
  665. video_frame_fill_black(tmp_frame_preview);
  666. if (__camera_error_event(session->bcamera_error_posted, 0)) {
  667. session->bcamera_error_posted = true;
  668. }
  669. }
  670. }
  671. else
  672. {
  673. if (*session->conf.ref_active_img == 1)
  674. {
  675. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_WIDTH, VIDEO_FORMAT_RGB24);
  676. video_frame_fill_black(tmp_frame_preview);
  677. if (session->video_error == NULL)
  678. {
  679. char strPath[MAX_PATH_SIZE] = { 0 };
  680. snprintf(strPath, MAX_PATH_SIZE, "%s", "./bin/looklowerscreen.jpg");
  681. if (ExistsFile(strPath))
  682. {
  683. IplImage* img = cvLoadImage(strPath, 1);
  684. if (img != NULL)
  685. {
  686. session->video_error = new videoq_frame;
  687. session->video_error->format = VIDEOQ_FORMAT_RGB24;
  688. session->video_error->framesize = img->imageSize;
  689. session->video_error->height = img->width;
  690. session->video_error->width = img->height;
  691. session->video_error->data = new unsigned char[img->imageSize];
  692. memcpy(session->video_error->data, img->imageData, img->imageSize);
  693. cvReleaseImage(&img);
  694. }
  695. }
  696. }
  697. if (session->video_error != NULL)
  698. {
  699. SwsContext* sws = sws_getContext(session->video_error->width, session->video_error->height, PIX_FMT_BGR24,
  700. REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  701. REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  702. PIX_FMT_BGR24,
  703. SWS_POINT, NULL, NULL, NULL);
  704. uint8_t* src_data[4] = { (unsigned char*)session->video_error->data + (session->video_error->height - 1) * session->video_error->width * 3,NULL,NULL,NULL };
  705. int src_linesize[4] = { -session->video_error->width * 3,0,0,0 };
  706. unsigned char* dst[4] = { tmp_frame_preview->data[0],NULL,NULL,NULL };
  707. int dst_linesize[4] = { tmp_frame_preview->linesize[0],0,0,0 };
  708. sws_scale(sws, src_data, src_linesize, 0, session->video_error->height, dst, dst_linesize);
  709. sws_freeContext(sws);
  710. }
  711. }
  712. else if (*session->conf.ref_Is_ActiveInspect == 1)
  713. {
  714. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_WIDTH, VIDEO_FORMAT_RGB24);
  715. video_frame_fill_black(tmp_frame_preview);
  716. if ((*session->conf.ref_camera_switch == CAMERA_TYPE_ENV) || (*session->conf.ref_camera_switch == CAMERA_TYPE_AUTO))//显示上摄像头
  717. {
  718. if (session->video_shm_q_env)
  719. {
  720. videoq_frame frm;
  721. int offset = (REC_COMMON_VIDEO_SNAPSHOT_WIDTH - REC_COMMON_VIDEO_SNAPSHOT_HEIGHT) / 2;
  722. frm.data = tmp_frame_preview->data[0] + offset * tmp_frame_preview->linesize[0];
  723. if (FALSE == session->video_shm_q_env->GetVideo(&frm, VIDEOQUEUE_FLAG_VERTICAL_FLIP)){
  724. if (__camera_error_event(session->bcamera_error_posted, 0)) {
  725. session->bcamera_error_posted = true;
  726. }
  727. }
  728. }
  729. }
  730. else if (*session->conf.ref_camera_switch == CAMERA_TYPE_OPT)//显示下摄像头
  731. {
  732. if (session->video_shm_q_opt)
  733. {
  734. video_frame tt = { 0 };
  735. int offset = (REC_COMMON_VIDEO_SNAPSHOT_WIDTH - REC_COMMON_VIDEO_SNAPSHOT_HEIGHT) / 2;
  736. tt.data[0] = tmp_frame_preview->data[0] + offset * 3;
  737. tt.format = tmp_frame_preview->format;
  738. tt.linesize[0] = tmp_frame_preview->linesize[0];
  739. tt.width = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
  740. tt.height = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
  741. if (FALSE == session->video_shm_q_opt->GetVideo2(&tt, VIDEOQUEUE_FLAG_VERTICAL_FLIP)) {
  742. if (__camera_error_event(session->bcamera_error_posted, 1)) {
  743. session->bcamera_error_posted = true;
  744. }
  745. }
  746. }
  747. }
  748. }
  749. else
  750. {
  751. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24);
  752. videoq_frame frm;
  753. frm.data = tmp_frame_preview->data[0];
  754. BOOL result = session->video_shm_q_preview->GetVideo(&frm, VIDEOQUEUE_FLAG_HORIZONTAL_FLIP);
  755. if (result){
  756. //人形框融合处理
  757. if (session->personimage != NULL && session->personmask != NULL && *session->conf.ref_Is_showPersonArea == 1)
  758. {
  759. IplImage* img = cvCreateImageHeader(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 3);
  760. img->imageData = (char*)frm.data;
  761. if (frm.width != session->personimage->width)
  762. {
  763. IplImage* tmp = cvCreateImage(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 3);
  764. IplImage* tmpmask = cvCreateImage(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 1);
  765. cvResize(session->personimage, tmp);
  766. cvResize(session->personmask, tmpmask);
  767. cvAdd(img, tmp, img, tmpmask);
  768. cvReleaseImage(&tmp);
  769. cvReleaseImage(&tmpmask);
  770. }
  771. else
  772. {
  773. cvAdd(img, session->personimage, img, session->personmask);
  774. }
  775. cvReleaseImageHeader(&img);
  776. }
  777. //双录人形位置框融合处理
  778. if (session->recordareaimage != NULL && session->recordareamask != NULL && *session->conf.ref_Is_showRecordArea == 1)
  779. {
  780. IplImage* img = cvCreateImageHeader(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 3);
  781. img->imageData = (char*)frm.data;
  782. if (frm.width != session->recordareaimage->width)
  783. {
  784. IplImage* tmp = cvCreateImage(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 3);
  785. IplImage* tmpmask = cvCreateImage(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 1);
  786. cvResize(session->recordareaimage, tmp);
  787. cvResize(session->recordareamask, tmpmask);
  788. cvAdd(img, tmp, img, tmpmask);
  789. cvReleaseImage(&tmp);
  790. cvReleaseImage(&tmpmask);
  791. }
  792. else
  793. {
  794. cvAdd(img, session->recordareaimage, img, session->recordareamask);
  795. }
  796. cvReleaseImageHeader(&img);
  797. }
  798. }
  799. else {
  800. video_frame_fill_black(tmp_frame_preview);
  801. if (__camera_error_event(session->bcamera_error_posted, 0)) {
  802. session->bcamera_error_posted = true;
  803. }
  804. }
  805. }
  806. }
  807. *frame = tmp_frame_preview;
  808. return *session->conf.ref_window_state;
  809. }
  810. #endif // RVC_OS_WIN
  811. static int start_video_rtpsession(video_session_t* session)
  812. {
  813. int rc = -1;
  814. videortp_config_t config = { 0 };
  815. config.fps_den = REC_COMMON_VIDEO_FPS_DEN;
  816. config.fps_num = REC_COMMON_VIDEO_FPS_NUM;
  817. config.capture_height = -1; // not use
  818. config.capture_width = -1; // not use
  819. config.dir = 3;
  820. config.tx_width = REC_COMMON_VIDEO_RTP_WIDTH;
  821. config.tx_height = REC_COMMON_VIDEO_RTP_HEIGHT;
  822. config.rx_width = session->conf.remote_video_width;
  823. config.rx_height = session->conf.remote_video_height;
  824. config.local_ip = session->conf.local_rtp_ip;
  825. config.local_pt = session->conf.local_pt;
  826. config.remote_pt = session->conf.local_pt;
  827. config.local_rtp_port = session->conf.local_rtp_port;
  828. config.mtu = session->conf.mtu;
  829. config.quant = session->conf.video_quant;
  830. config.remote_ip = session->conf.remote_rtp_ip;
  831. config.remote_rtp_port = session->conf.remote_rtp_port;
  832. config.user_data = session;
  833. config.bit_rate = session->conf.bit_rate;
  834. config.on_rx_frame = &on_rx_frame;
  835. config.on_rx_udp = &on_rx_udp;
  836. config.dbg = &__dbg;
  837. config.logevent = &__logevent;
  838. if (0 != videortp_create(&config, &session->rtp)) {
  839. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("videortp create failed!");
  840. }
  841. rc = videortp_start(session->rtp);
  842. if (rc != 0)
  843. {
  844. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start video rtp failed!");
  845. videortp_destroy(session->rtp);
  846. session->rtp = NULL;
  847. }
  848. return rc;
  849. }
  850. static int start_local_video_clock(video_session_t* session)
  851. {
  852. int rc = -1;
  853. rc = videoclock_create(REC_COMMON_VIDEO_FPS_NUM, REC_COMMON_VIDEO_FPS_DEN,
  854. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_RGB24,
  855. &local_put_frame, session, &local_get_frame, session, &session->local_clock, session->conf.ref_Up_Fps, &__dbg);
  856. if (0 == rc) {
  857. rc = videoclock_start(session->local_clock);
  858. if (rc != 0) {
  859. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start video clock failed!");
  860. videoclock_destroy(session->local_clock);
  861. session->local_clock = NULL;
  862. }
  863. }
  864. else {
  865. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create video clock failed!");
  866. }
  867. return rc;
  868. }
  869. static int start_video(video_session_t *session)
  870. {
  871. int rc = -1;
  872. if (session->video_shm_q_env)
  873. {
  874. session->local_encode_sws_ctx_env = sws_getContext(
  875. REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, PIX_FMT_BGR24,
  876. REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, PIX_FMT_YUV420P,
  877. SWS_POINT, NULL, NULL, NULL);
  878. if (!session->local_encode_sws_ctx_env) {
  879. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get session->local_encode_sws_ctx_env failed.");
  880. goto on_error;
  881. }
  882. }
  883. else{
  884. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("session->video_shm_q_env is NULL.");
  885. }
  886. if (session->video_shm_q_opt)
  887. {
  888. session->local_encode_sws_ctx_opt = sws_getContext(
  889. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, PIX_FMT_BGR24,
  890. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, PIX_FMT_YUV420P,
  891. SWS_POINT, NULL, NULL, NULL);
  892. if (!session->local_encode_sws_ctx_opt) {
  893. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get session->local_encode_sws_ctx_opt failed.");
  894. goto on_error;
  895. }
  896. }
  897. else{
  898. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("session->video_shm_q_opt is NULL.");
  899. }
  900. #ifdef RVC_OS_WIN
  901. if (DOUBLERECORD_CALLTYPE != session->conf.nCallType){
  902. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create new remote video player");
  903. if (session->remote_hwnd)
  904. {
  905. if (session->conf.eDeviceType == eMobilePadType)
  906. {
  907. rc = videoplayer_create(session->remote_hwnd,
  908. 0,
  909. 0,
  910. session->conf.remote_video_view_cx,
  911. session->conf.remote_video_view_cy,
  912. REC_COMMON_VIDEO_FPS_MOBILE_AGENT,
  913. REC_COMMON_VIDEO_FPS_DEN,
  914. session->conf.remote_video_width,
  915. session->conf.remote_video_height,
  916. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP,
  917. session->conf.iremote_wind_flags,
  918. "remote",
  919. on_remoteWinstate,
  920. NULL,
  921. session,
  922. &session->remote_player);
  923. if (rc != 0)
  924. {
  925. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create remote video player failed!");
  926. goto on_error;
  927. }
  928. else{
  929. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create remote video player suc!");
  930. }
  931. }
  932. else if((session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  933. {
  934. rc = videoplayer_create(session->remote_hwnd,
  935. 0,
  936. 0,
  937. session->conf.remote_video_view_cx,
  938. session->conf.remote_video_view_cy,
  939. REC_COMMON_VIDEO_FPS_MOBILE,
  940. REC_COMMON_VIDEO_FPS_DEN,
  941. session->conf.remote_video_width,
  942. session->conf.remote_video_height,
  943. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP,
  944. session->conf.iremote_wind_flags,
  945. "remote",
  946. on_remoteWinstate,
  947. NULL,
  948. session,
  949. &session->remote_player);
  950. if (rc != 0) {
  951. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create remote video player failed!");
  952. goto on_error;
  953. }
  954. else{
  955. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create remote video player suc!");
  956. }
  957. }
  958. else if(session->conf.eDeviceType == eStand2sType)
  959. {
  960. rc = videoplayer_create(session->remote_hwnd,
  961. 0,
  962. 0,
  963. session->conf.remote_video_view_cx,
  964. session->conf.remote_video_view_cy,
  965. REC_COMMON_VIDEO_FPS_NUM,
  966. REC_COMMON_VIDEO_FPS_DEN,
  967. session->conf.remote_video_width,
  968. session->conf.remote_video_height,
  969. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP,
  970. session->conf.iremote_wind_flags,
  971. "remote",
  972. on_remoteWinstate,
  973. NULL,
  974. session,
  975. &session->remote_player);
  976. if (rc != 0)
  977. {
  978. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create remote video player failed!");
  979. goto on_error;
  980. }
  981. else{
  982. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create remote video player suc remote_video_width = %d,remote_video_height = %d.", session->conf.remote_video_width, session->conf.remote_video_height);
  983. }
  984. }
  985. }
  986. else
  987. {
  988. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("remote_hwnd == Null");
  989. }
  990. }
  991. else{
  992. if (NULL != pg_last_session){
  993. session->remote_player = pg_last_session->remote_player;
  994. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("double record call remote_player reuse.");
  995. }
  996. else{
  997. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("pg_last_session is NULL, and call type is %d.", session->conf.nCallType);
  998. }
  999. }
  1000. BOOL bIsActiveInspect = FALSE;
  1001. if (session->conf.ref_Is_ActiveInspect != NULL){
  1002. if (*session->conf.ref_Is_ActiveInspect == 1){
  1003. bIsActiveInspect = TRUE;
  1004. }
  1005. else{
  1006. bIsActiveInspect = FALSE;
  1007. }
  1008. }
  1009. else{
  1010. bIsActiveInspect = FALSE;
  1011. }
  1012. if (DOUBLERECORD_CALLTYPE != session->conf.nCallType){
  1013. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create new local video player");
  1014. if (session->conf.eDeviceType == eMobilePadType)
  1015. {
  1016. rc = videoplayer_create(session->local_hwnd,
  1017. 0,
  1018. 0,
  1019. session->conf.local_video_view_cx,
  1020. session->conf.local_video_view_cy,
  1021. REC_COMMON_VIDEO_FPS_MOBILE,
  1022. REC_COMMON_VIDEO_FPS_DEN,
  1023. REC_COMMON_VIDEO_PREVIEW_WIDTH,
  1024. REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  1025. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP,
  1026. session->conf.ilocal_wind_flags,
  1027. "local",
  1028. on_pull, free_frame, session,
  1029. &session->local_player);
  1030. if (rc != 0)
  1031. {
  1032. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create local video player failed!");
  1033. goto on_error;
  1034. }
  1035. else{
  1036. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create local video player suc!");
  1037. }
  1038. }
  1039. else if((session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  1040. {
  1041. rc = videoplayer_create(session->local_hwnd,
  1042. 0,
  1043. 0,
  1044. session->conf.local_video_view_cx,
  1045. session->conf.local_video_view_cy,
  1046. REC_COMMON_VIDEO_FPS_MOBILE,
  1047. REC_COMMON_VIDEO_FPS_DEN,
  1048. REC_COMMON_VIDEO_PREVIEW_WIDTH,
  1049. REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  1050. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP,
  1051. session->conf.ilocal_wind_flags,
  1052. "local",
  1053. on_pull, free_frame, session,
  1054. &session->local_player);
  1055. if (rc != 0)
  1056. {
  1057. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create local video player failed!");
  1058. goto on_error;
  1059. }
  1060. else{
  1061. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create local video player suc!");
  1062. }
  1063. }
  1064. else if(session->conf.eDeviceType == eStand2sType)
  1065. {
  1066. rc = videoplayer_create(session->local_hwnd,
  1067. 0,
  1068. 0,
  1069. session->conf.local_video_view_cx,
  1070. session->conf.local_video_view_cy,
  1071. REC_COMMON_VIDEO_FPS_NUM,
  1072. REC_COMMON_VIDEO_FPS_DEN,
  1073. bIsActiveInspect?REC_COMMON_VIDEO_SNAPSHOT_WIDTH:REC_COMMON_VIDEO_PREVIEW_WIDTH,
  1074. bIsActiveInspect?REC_COMMON_VIDEO_SNAPSHOT_WIDTH:REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  1075. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP,
  1076. session->conf.ilocal_wind_flags,
  1077. "local",
  1078. on_pull, free_frame, session,
  1079. &session->local_player);
  1080. if (rc != 0)
  1081. {
  1082. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create local video player failed!");
  1083. goto on_error;
  1084. }
  1085. else{
  1086. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create local video player suc!");
  1087. }
  1088. }
  1089. }
  1090. else{
  1091. if (NULL != pg_last_session){
  1092. session->local_player = pg_last_session->local_player;
  1093. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("double record call local player reuse.");
  1094. }
  1095. }
  1096. if (session->remote_hwnd)
  1097. {
  1098. if (0 != session->conf.remote_rtp_port)
  1099. {
  1100. if (0 != start_video_rtpsession(session)) {
  1101. goto on_error;
  1102. }
  1103. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType){
  1104. if (NULL != pg_last_session){
  1105. pg_last_session->rtp = session->rtp;
  1106. }
  1107. }
  1108. if (0 != start_local_video_clock(session)) {
  1109. goto on_error;
  1110. }
  1111. }
  1112. else{
  1113. pg_last_session = session; //保存session信息
  1114. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("begin show agent static picture.");
  1115. show_remote_agnet_picture(session);
  1116. }
  1117. }
  1118. #else
  1119. rc = start_video_rtpsession(session);
  1120. if (0 != rc) {
  1121. goto on_error;
  1122. }
  1123. rc = start_local_video_clock(session);
  1124. if (0 != rc) {
  1125. goto on_error;
  1126. }
  1127. #endif // RVC_OS_WIN
  1128. on_error:
  1129. return rc;
  1130. }
  1131. static void stop_video(video_session_t *session)
  1132. {
  1133. #ifdef RVC_OS_WIN
  1134. if (session->local_clock && session->remote_hwnd)
  1135. {
  1136. videoclock_stop(session->local_clock);
  1137. videoclock_destroy(session->local_clock);
  1138. session->local_clock = NULL;
  1139. pg_remote_hwnd = NULL;
  1140. pg_local_hwnd = NULL;
  1141. }
  1142. #else
  1143. if (session->local_clock)
  1144. {
  1145. videoclock_stop(session->local_clock);
  1146. videoclock_destroy(session->local_clock);
  1147. session->local_clock = NULL;
  1148. }
  1149. #endif // RVC_OS_WIN
  1150. if (session->rtp){
  1151. videortp_stop(session->rtp);
  1152. videortp_destroy(session->rtp);
  1153. session->rtp = NULL;
  1154. }
  1155. else {
  1156. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("session->rtp == null");
  1157. }
  1158. #ifdef RVC_OS_WIN
  1159. if (session->local_player) {
  1160. videoplayer_destroy(session->local_player);
  1161. session->local_player = NULL;
  1162. }
  1163. if (session->remote_player && session->remote_hwnd)
  1164. {
  1165. videoplayer_destroy(session->remote_player);
  1166. session->remote_player = NULL;
  1167. }
  1168. #else
  1169. #endif
  1170. if (session->local_encode_sws_ctx_env) {
  1171. sws_freeContext(session->local_encode_sws_ctx_env);
  1172. session->local_encode_sws_ctx_env = NULL;
  1173. }
  1174. if (session->local_encode_sws_ctx_opt) {
  1175. sws_freeContext(session->local_encode_sws_ctx_opt);
  1176. session->local_encode_sws_ctx_opt = NULL;
  1177. }
  1178. }
  1179. #ifdef RVC_OS_WIN
  1180. static bool ReMoveOutOfScreenVideoWindow(HWND hWnd, RECT rect)
  1181. {
  1182. bool bret = false;
  1183. int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  1184. int iScreenHight = GetSystemMetrics(SM_CYSCREEN);
  1185. int iVideoWidth = rect.right - rect.left;
  1186. int iVideoHight = rect.bottom - rect.top;
  1187. if (rect.left < 0) {
  1188. if (rect.bottom <= iScreenHight) {
  1189. MoveWindow(hWnd, 0, rect.top, iVideoWidth, iVideoHight, TRUE);
  1190. }
  1191. else {
  1192. MoveWindow(hWnd, 0, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1193. }
  1194. bret = true;
  1195. }
  1196. if (rect.right > iScreenWidth) {
  1197. if (rect.bottom <= iScreenHight) {
  1198. MoveWindow(hWnd, iScreenWidth - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1199. }
  1200. else {
  1201. MoveWindow(hWnd, iScreenWidth - iVideoWidth, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1202. }
  1203. bret = true;
  1204. }
  1205. if (rect.bottom > iScreenHight) {
  1206. if (rect.left >= 0 && rect.right <= iScreenWidth) {
  1207. MoveWindow(hWnd, rect.left, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1208. bret = true;
  1209. }
  1210. }
  1211. return bret;
  1212. }
  1213. static bool ReMoveCenterOtherVideoWindow(HWND hWnd, RECT rect, RECT otherect)
  1214. {
  1215. bool bret = false;
  1216. int iVideoWidth = rect.right - rect.left;
  1217. int iVideoHight = rect.bottom - rect.top;
  1218. int iOtherVideoWidth = otherect.right - otherect.left;
  1219. int iOtherVideoHight = otherect.bottom - otherect.top;
  1220. if (iVideoWidth > iOtherVideoHight || iVideoHight > iOtherVideoHight) {
  1221. return bret;
  1222. }
  1223. if (rect.left > otherect.left + iVideoWidth && rect.right < otherect.right) {
  1224. if (rect.top > otherect.top && rect.bottom < otherect.bottom) {
  1225. if (otherect.right - rect.right <= rect.left - otherect.left) {
  1226. MoveWindow(hWnd, otherect.right - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1227. }
  1228. else {
  1229. MoveWindow(hWnd, otherect.left, rect.top, iVideoWidth, iVideoHight, TRUE);
  1230. }
  1231. bret = true;
  1232. }
  1233. }
  1234. if (rect.right < otherect.right - iVideoWidth && rect.left > otherect.left) {
  1235. if (rect.top > otherect.top && rect.bottom < otherect.bottom) {
  1236. if (otherect.right - rect.right <= rect.left - otherect.left) {
  1237. MoveWindow(hWnd, otherect.right - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1238. }
  1239. else {
  1240. MoveWindow(hWnd, otherect.left, rect.top, iVideoWidth, iVideoHight, TRUE);
  1241. }
  1242. bret = true;
  1243. }
  1244. }
  1245. return bret;
  1246. }
  1247. static bool ReMoveVideoWindow(HWND hWnd, RECT rect, RECT otherect)
  1248. {
  1249. bool bret = false;
  1250. bret = ReMoveOutOfScreenVideoWindow(hWnd, rect);
  1251. bret = ReMoveCenterOtherVideoWindow(hWnd, rect, otherect);
  1252. return bret;
  1253. }
  1254. static int HandleVideoMoveEvent(int iMessageType, HWND hWnd, video_session_t* pSession)
  1255. {
  1256. int iRet = -1;
  1257. if (NULL == pSession || NULL == hWnd) {
  1258. return iRet;
  1259. }
  1260. HWND hOtherWnd = NULL;
  1261. //本地和远端标识 1为本地,2为远端
  1262. int iVideoType = 2;
  1263. if (hWnd == pSession->local_hwnd) {
  1264. iVideoType = 1;
  1265. hOtherWnd = pSession->remote_hwnd;
  1266. }
  1267. else {
  1268. hOtherWnd = pSession->local_hwnd;
  1269. }
  1270. if (NULL == hOtherWnd) {
  1271. return iRet;
  1272. }
  1273. bool bMoved = false;
  1274. RECT rect;
  1275. GetWindowRect(hWnd, &rect);
  1276. RECT otherect;
  1277. GetWindowRect(hOtherWnd, &otherect);
  1278. bMoved = ReMoveVideoWindow(hWnd, rect, otherect);
  1279. if (bMoved) {
  1280. GetWindowRect(hWnd, &rect);
  1281. }
  1282. if (NULL != pSession->conf.video_echo_cb && NULL != pSession->conf.video_echo_cb->on_video_box_move) {
  1283. pSession->conf.video_echo_cb->on_video_box_move(iMessageType, iVideoType, rect.left, rect.bottom, pSession->conf.video_echo_cb->user_data);
  1284. iRet = 0;
  1285. }
  1286. return iRet;
  1287. }
  1288. #else
  1289. #endif // RVC_OS_WIN
  1290. #ifdef RVC_OS_WIN
  1291. static LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  1292. {
  1293. video_session_t* session = (video_session_t*)GetWindowLongPtrA(hWnd, GWLP_USERDATA);
  1294. switch (msg) {
  1295. case WM_DESTROY:
  1296. if (session->local_hwnd == hWnd) {
  1297. session->local_hwnd = 0;
  1298. }
  1299. else if (session->remote_hwnd == hWnd) {
  1300. session->remote_hwnd = 0;
  1301. }
  1302. if (session->local_hwnd == 0 && session->remote_hwnd == 0) {
  1303. PostQuitMessage(0);
  1304. }
  1305. return 0;
  1306. case WM_NCHITTEST:
  1307. if (((session->local_hwnd == hWnd) && (0 != session->conf.local_move)) || ((session->remote_hwnd == hWnd) && (0 != session->conf.remote_move))) {
  1308. return HTCAPTION;
  1309. }
  1310. else {
  1311. break;
  1312. }
  1313. case WM_ACTIVATE:
  1314. case WM_TOUCH:
  1315. case WM_GESTURE:
  1316. ReleaseCapture();
  1317. return 0;
  1318. #if 1
  1319. case WM_WINDOWPOSCHANGED:
  1320. {
  1321. LPWINDOWPOS pPos = (LPWINDOWPOS)lParam;
  1322. if (pPos->hwndInsertAfter != HWND_TOPMOST && pPos->hwndInsertAfter != HWND_TOP) {
  1323. //BringWindowToTop(hWnd);
  1324. SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  1325. }
  1326. else {
  1327. return DefWindowProc(hWnd, msg, wParam, lParam);
  1328. }
  1329. }
  1330. return 0;
  1331. //return DefWindowProc(hWnd, msg, wParam, lParam);
  1332. #endif
  1333. case WM_MBUTTONDOWN:
  1334. case WM_MBUTTONUP:
  1335. case WM_MBUTTONDBLCLK:
  1336. case WM_LBUTTONDBLCLK:
  1337. case WM_LBUTTONDOWN:
  1338. case WM_LBUTTONUP:
  1339. if (session) {
  1340. if (session->local_hwnd) {
  1341. //ReleaseCapture();
  1342. }
  1343. }
  1344. return 0;
  1345. case WM_CLOSE:
  1346. stop_video(session);
  1347. //SetEvent(session->ui_thread);
  1348. DestroyWindow(session->local_hwnd);
  1349. DestroyWindow(session->remote_hwnd);
  1350. return 0;
  1351. case WM_MOVE:
  1352. break;
  1353. case WM_ENTERSIZEMOVE:
  1354. {
  1355. HandleVideoMoveEvent(0, hWnd, session);
  1356. }
  1357. break;
  1358. case WM_EXITSIZEMOVE:
  1359. {
  1360. HandleVideoMoveEvent(1, hWnd, session);
  1361. }
  1362. break;
  1363. default:
  1364. return DefWindowProc(hWnd, msg, wParam, lParam);
  1365. }
  1366. return 0;
  1367. }
  1368. #endif //
  1369. #ifdef RVC_OS_WIN
  1370. static unsigned int __stdcall ui_proc(void *arg)
  1371. {
  1372. video_session_t *session = (video_session_t*)arg;
  1373. WNDCLASSA wc = {0};
  1374. ATOM a = 0;
  1375. HWND hWnd = NULL;
  1376. MSG msg;
  1377. HINSTANCE hInst = ModuleBase::GetModuleBase()->GetInstance();
  1378. bool breuse_local_wnd = false;
  1379. bool breuse_remote_wnd = false;
  1380. DWORD dLocal_style = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_NOACTIVATE;
  1381. DWORD dRemote_style = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_NOACTIVATE;
  1382. //双录两阶段视频窗口复用
  1383. if (NULL != pg_local_hwnd){
  1384. session->local_hwnd = pg_local_hwnd;
  1385. //SetWindowLongPtrA(session->local_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1386. breuse_local_wnd = true;
  1387. }
  1388. else{
  1389. if (0 != session->conf.local_move){
  1390. dLocal_style -= WS_EX_NOACTIVATE;
  1391. }
  1392. if (0 != session->conf.remote_move){
  1393. dRemote_style -= WS_EX_NOACTIVATE;
  1394. }
  1395. CoInitialize(0);
  1396. wc.cbClsExtra = 0;
  1397. wc.cbWndExtra = 0;
  1398. wc.hInstance = hInst;
  1399. wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  1400. wc.hCursor = NULL;
  1401. wc.hIcon = NULL;
  1402. wc.lpfnWndProc = &WndProc;
  1403. wc.lpszClassName = WNDCLS_NAME;
  1404. wc.style = CS_HREDRAW | CS_OWNDC | CS_VREDRAW;
  1405. a = RegisterClassA(&wc);
  1406. if (a == 0)
  1407. return 0;
  1408. session->local_hwnd = CreateWindowExA(dLocal_style,
  1409. WNDCLS_NAME, NULL, WS_POPUP|WS_VISIBLE,
  1410. session->conf.local_video_view_x, session->conf.local_video_view_y, session->conf.local_video_view_cx, session->conf.local_video_view_cy,
  1411. NULL, NULL, hInst, NULL);
  1412. if (session->local_hwnd) {
  1413. SetWindowLongPtrA(session->local_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1414. if (0 == session->conf.local_move){
  1415. SetWindowPos(session->local_hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
  1416. }
  1417. pg_local_hwnd = session->local_hwnd;
  1418. //LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_LOCAL_WINDOW_CREATE_SUCCESS, "create local window success and restore local video hwnd.");
  1419. }
  1420. else{
  1421. char strinfo[MAX_PATH] = {0};
  1422. _snprintf(strinfo, MAX_PATH, "%s%d", "create local window failed and last error is ", GetLastError());
  1423. LogWarn(Severity_Middle, Error_Debug, EVENT_MOD_SIP_LOCAL_WINDOW_CREATE_FAILED, strinfo);
  1424. //return -1;
  1425. }
  1426. }
  1427. 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)
  1428. {
  1429. if (NULL != pg_remote_hwnd){
  1430. session->remote_hwnd = pg_remote_hwnd;
  1431. breuse_remote_wnd = true;
  1432. if (NULL != pg_last_session){
  1433. record_agent_picture_show_session_destory(pg_last_session);
  1434. }
  1435. }
  1436. else{
  1437. session->remote_hwnd = CreateWindowExA(dRemote_style,
  1438. WNDCLS_NAME, NULL, WS_POPUP|WS_VISIBLE,
  1439. session->conf.remote_video_view_x, session->conf.remote_video_view_y, session->conf.remote_video_view_cx, session->conf.remote_video_view_cy,
  1440. NULL, NULL, hInst, NULL);
  1441. if (session->remote_hwnd) {
  1442. SetWindowLongPtrA(session->remote_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1443. if (0 == session->conf.remote_move){
  1444. SetWindowPos(session->remote_hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
  1445. }
  1446. pg_remote_hwnd = session->remote_hwnd;
  1447. //LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_REMOTE_WINDOW_CREATE_SUCCESS, "create remote window success and restore remote video hwnd.");
  1448. }
  1449. else{
  1450. char strinfo[MAX_PATH] = {0};
  1451. _snprintf(strinfo, MAX_PATH, "%s%d", "create remote window failed and last error is ", GetLastError());
  1452. LogWarn(Severity_Middle, Error_Debug, EVENT_MOD_SIP_REMOTE_WINDOW_CREATE_FAILED, strinfo);
  1453. //return -1;
  1454. }
  1455. }
  1456. }
  1457. else
  1458. {
  1459. session->remote_hwnd = NULL;
  1460. }
  1461. if (session->local_hwnd/* && session->remote_hwnd*/)
  1462. {
  1463. int rc = 0;
  1464. ShowCursor(FALSE);
  1465. SetEvent(session->ui_event);
  1466. rc = start_video(session);
  1467. if (rc != 0) {
  1468. LogWarn(Severity_Middle, Error_Debug, ERROR_MOD_SIP_START_VIDEO_FAILED, "start video failed!");
  1469. }
  1470. if (!breuse_local_wnd && !breuse_remote_wnd)
  1471. {
  1472. while (GetMessageA(&msg, NULL, NULL, NULL))
  1473. {
  1474. if (msg.message == WM_CLOSE) {
  1475. }
  1476. TranslateMessage(&msg);
  1477. DispatchMessageA(&msg);
  1478. }
  1479. SetEvent(session->ui_event);
  1480. }
  1481. }
  1482. if (a)
  1483. UnregisterClassA(WNDCLS_NAME, hInst);
  1484. CoUninitialize();
  1485. return 0;
  1486. }
  1487. #else
  1488. static void __video_render_log(render_loglevel elevel, void* user_data, const char* fmt, va_list arg)
  1489. {
  1490. int n = vsnprintf(NULL, 0, fmt, arg);
  1491. if (n >= MAX_PATH) {
  1492. char* buf = (char*)malloc((size_t)(n + 1));
  1493. vsnprintf(buf, n + 1, fmt, arg);
  1494. DbgWithLink((LOG_LEVEL_E)elevel, LOG_TYPE_SYSTEM)("%s", buf);
  1495. free(buf);
  1496. }
  1497. else {
  1498. char strlog[MAX_PATH] = { 0 };
  1499. vsnprintf(strlog, MAX_PATH, fmt, arg);
  1500. DbgWithLink((LOG_LEVEL_E)elevel, LOG_TYPE_SYSTEM)("%s", strlog);
  1501. }
  1502. }
  1503. #endif // RVC_OS_WIN
  1504. static int start_ui(video_session_t *session)
  1505. {
  1506. #ifdef RVC_OS_WIN
  1507. session->ui_event = CreateEventA(NULL, FALSE, FALSE, NULL);
  1508. if (!session->ui_event) {
  1509. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("phonemedia_start create ui event failed!");
  1510. return Error_Resource;
  1511. }
  1512. session->ui_thread = (HANDLE)_beginthreadex(NULL, 0, &ui_proc, session, 0, NULL);
  1513. if (!session->ui_thread) {
  1514. CloseHandle(session->ui_event);
  1515. session->ui_event = NULL;
  1516. return Error_Resource;
  1517. }
  1518. {
  1519. HANDLE hs[] = {session->ui_event, session->ui_thread};
  1520. DWORD dwRet = WaitForMultipleObjects(array_size(hs), hs, FALSE, INFINITE);
  1521. if (dwRet == WAIT_OBJECT_0) { //
  1522. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("wait for ui ready event ok!");
  1523. } else if (dwRet == WAIT_OBJECT_0 + 1) { // thread exit
  1524. CloseHandle(session->ui_thread);
  1525. session->ui_thread = NULL;
  1526. CloseHandle(session->ui_event);
  1527. session->ui_event = NULL;
  1528. return Error_Resource;
  1529. }
  1530. }
  1531. return 0;
  1532. #endif // RVC_OS_WIN
  1533. }
  1534. static void stop_ui(video_session_t *session)
  1535. {
  1536. #ifdef RVC_OS_WIN
  1537. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType) {
  1538. if (NULL != pg_last_session) {
  1539. pg_last_session->rtp = session->rtp;
  1540. pg_last_session->local_clock = session->local_clock;
  1541. CloseHandle(session->ui_thread);
  1542. session->ui_thread = NULL;
  1543. CloseHandle(session->ui_event);
  1544. session->ui_event = NULL;
  1545. session = pg_last_session;
  1546. }
  1547. }
  1548. if (NULL != session->pic_record) {
  1549. if (NULL != session->pic_record->work_thread) {
  1550. SetEvent(session->pic_record->evt);
  1551. CloseHandle(session->pic_record->work_thread);
  1552. session->pic_record->work_thread = NULL;
  1553. CloseHandle(session->pic_record->evt);
  1554. session->pic_record->evt = NULL;
  1555. }
  1556. }
  1557. if (session->local_hwnd) {
  1558. BOOL bRet = PostMessageA(session->local_hwnd, WM_CLOSE, 0, 0);
  1559. DWORD dCode = WaitForSingleObject(session->ui_thread, INFINITE);
  1560. CloseHandle(session->ui_thread);
  1561. session->ui_thread = NULL;
  1562. CloseHandle(session->ui_event);
  1563. session->ui_event = NULL;
  1564. }
  1565. pg_last_session = NULL;
  1566. pg_local_hwnd = NULL;
  1567. pg_remote_hwnd = NULL;
  1568. #endif
  1569. }
  1570. int video_session_create(const video_session_conf_t *conf, video_session_t **p_session)
  1571. {
  1572. video_session_t *session = ZALLOC_T(video_session_t);
  1573. if (session)
  1574. {
  1575. memcpy(&session->conf, conf, sizeof(video_session_conf_t));
  1576. //session->bshow_remote = false;
  1577. //session->ilast_windstae = 0;
  1578. session->video_shm_q_env = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE);
  1579. session->video_shm_q_remote = new Clibvideoqueue(REC_COMMON_VIDEO_REMOTE_SHM_RTP_QUEUE);
  1580. session->video_shm_q_preview = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_PREVIEW_QUEUE);
  1581. char strPath[MAX_PATH_SIZE]={0};
  1582. char strImgPath[MAX_PATH_SIZE]={0};
  1583. #ifdef RVC_OS_WIN
  1584. GetCurrentRunPath(strPath);
  1585. sprintf(strImgPath, "%s\\bin\\error.jpg", strPath);
  1586. #else
  1587. snprintf(strImgPath, MAX_PATH_SIZE, "%s", "./bin/error.jpg");
  1588. #endif // RVC_OS_WIN
  1589. if (ExistsFile(strImgPath))
  1590. {
  1591. IplImage*img = cvLoadImage(strImgPath,1);
  1592. if (img != NULL)
  1593. {
  1594. session->video_error = new videoq_frame;
  1595. session->video_error->format = VIDEOQ_FORMAT_RGB24;
  1596. session->video_error->framesize = 320*180*3;
  1597. session->video_error->height = 180;
  1598. session->video_error->width = 320;
  1599. session->video_error->data = new unsigned char[320*180*3];
  1600. memcpy(session->video_error->data,img->imageData,320*180*3);
  1601. cvReleaseImage(&img);
  1602. }
  1603. }
  1604. else
  1605. {
  1606. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Load error img Fail");
  1607. session->video_error = NULL;
  1608. }
  1609. //加载人形背景框
  1610. char strPersonPath[MAX_PATH_SIZE] = {0};
  1611. #ifdef RVC_OS_WIN
  1612. sprintf(strPersonPath, "%s\\bin\\rxk.jpg", strPath);
  1613. #else
  1614. snprintf(strPersonPath, MAX_PATH_SIZE, "./bin/rxk.jpg");
  1615. #endif // RVC_OS_WIN
  1616. if (ExistsFile(strPersonPath))
  1617. {
  1618. if (session->personimage == NULL)
  1619. {
  1620. session->personimage = cvLoadImage(strPersonPath);
  1621. }
  1622. if (session->personmask == NULL )
  1623. {
  1624. session->personmask = cvLoadImage(strPersonPath,0);
  1625. }
  1626. //IplImage*persongrey = cvLoadImage(strPersonPath,CV_LOAD_IMAGE_GRAYSCALE);
  1627. //二值化提取人形框掩码
  1628. //cvThreshold(persongrey,session->personmask,150, 255, CV_THRESH_BINARY);
  1629. }
  1630. else
  1631. {
  1632. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Load person img Fail");
  1633. session->personimage = NULL;
  1634. session->personmask = NULL;
  1635. }
  1636. #if 0
  1637. video_frame frame;
  1638. video_frame_alloc(320, 180, VIDEO_FORMAT_RGB24, &frame);
  1639. video_frame_fill_black(&frame);
  1640. videoq_frame frm;
  1641. frm.data = frame.data[0];
  1642. session->video_shm_q_env->GetVideo(&frm, 0);
  1643. video_frame_save_bmpfile("d:\\a.bmp", &frame);
  1644. #endif
  1645. if (conf->camera_count == 2) {
  1646. session->video_shm_q_opt = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
  1647. }
  1648. session->bvideorecved = false;
  1649. session->blocalrender = false;
  1650. session->bremoterender = false;
  1651. *p_session = session;
  1652. }
  1653. return 0;
  1654. }
  1655. int video_session_start(video_session_t *session)
  1656. {
  1657. int rc = -1;
  1658. #ifdef RVC_OS_WIN
  1659. rc = start_ui(session);
  1660. #else
  1661. rc = start_video(session);
  1662. #endif // RVC_OS_WIN
  1663. return rc;
  1664. }
  1665. void video_session_stop(video_session_t *session)
  1666. {
  1667. #ifdef RVC_OS_WIN
  1668. stop_ui(session);
  1669. #else
  1670. stop_video(session);
  1671. #endif
  1672. }
  1673. void video_session_destroy(video_session_t *session)
  1674. {
  1675. if (session->video_error){
  1676. delete session->video_error->data;
  1677. delete session->video_error;
  1678. }
  1679. if (session->video_shm_q_remote) {
  1680. delete session->video_shm_q_remote;
  1681. session->video_shm_q_remote = NULL;
  1682. }
  1683. if (session->personimage){
  1684. cvReleaseImage(&session->personimage);
  1685. }
  1686. if (session->personmask){
  1687. cvReleaseImage(&session->personmask);
  1688. }
  1689. if (session->recordareaimage){
  1690. cvReleaseImage(&session->recordareaimage);
  1691. }
  1692. if (session->recordareamask){
  1693. cvReleaseImage(&session->recordareamask);
  1694. }
  1695. free(session);
  1696. }
  1697. void av_log_cb(void*ptr, int level, const char*fmt, va_list list)
  1698. {
  1699. int n = vsnprintf(NULL, 0, fmt, list);
  1700. if (n >= MAX_PATH) {
  1701. char* buf = (char*)malloc((size_t)(n + 1));
  1702. vsnprintf(buf, n + 1, fmt, list);
  1703. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  1704. free(buf);
  1705. }
  1706. else {
  1707. char strlog[MAX_PATH] = { 0 };
  1708. vsnprintf(strlog, MAX_PATH, fmt, list);
  1709. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  1710. }
  1711. }
  1712. int video_lib_init()
  1713. {
  1714. #ifdef RVC_OS_WIN
  1715. videoframework_init();
  1716. av_log_set_callback(&av_log_cb);
  1717. //av_log_set_level(AV_LOG_DEBUG);
  1718. av_log_set_level(AV_LOG_QUIET);
  1719. #else
  1720. videoframework_init();
  1721. if (0 != VideoRender_Init()){
  1722. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("sdl init failed.");
  1723. }
  1724. //av_log_set_callback(&av_log_cb);
  1725. //av_log_set_level(AV_LOG_TRACE);
  1726. //av_log_set_level(AV_LOG_QUIET);
  1727. #endif // RVC_OS_WIN
  1728. return 0;
  1729. }
  1730. void video_lib_deinit()
  1731. {
  1732. #ifdef RVC_OS_WIN
  1733. CoUninitialize();
  1734. #else
  1735. #endif // RVC_OS_WIN
  1736. videoframework_term();
  1737. VideoRender_Term();
  1738. }