video_session.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  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. //config.fresh_time = session->conf.irvideo_ft;
  839. if (0 != videortp_create(&config, &session->rtp)) {
  840. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("videortp create failed!");
  841. }
  842. rc = videortp_start(session->rtp);
  843. if (rc != 0)
  844. {
  845. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start video rtp failed!");
  846. videortp_destroy(session->rtp);
  847. session->rtp = NULL;
  848. }
  849. return rc;
  850. }
  851. static int start_local_video_clock(video_session_t* session)
  852. {
  853. int rc = -1;
  854. rc = videoclock_create(REC_COMMON_VIDEO_FPS_NUM, REC_COMMON_VIDEO_FPS_DEN,
  855. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, VIDEO_FORMAT_RGB24,
  856. &local_put_frame, session, &local_get_frame, session, &session->local_clock, session->conf.ref_Up_Fps, &__dbg);
  857. if (0 == rc) {
  858. rc = videoclock_start(session->local_clock);
  859. if (rc != 0) {
  860. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start video clock failed!");
  861. videoclock_destroy(session->local_clock);
  862. session->local_clock = NULL;
  863. }
  864. }
  865. else {
  866. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create video clock failed!");
  867. }
  868. return rc;
  869. }
  870. static int start_video(video_session_t *session)
  871. {
  872. int rc = -1;
  873. if (session->video_shm_q_env)
  874. {
  875. session->local_encode_sws_ctx_env = sws_getContext(
  876. REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, PIX_FMT_BGR24,
  877. REC_COMMON_VIDEO_RTP_ENV_WIDTH, REC_COMMON_VIDEO_RTP_ENV_HEIGHT, PIX_FMT_YUV420P,
  878. SWS_POINT, NULL, NULL, NULL);
  879. if (!session->local_encode_sws_ctx_env) {
  880. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get session->local_encode_sws_ctx_env failed.");
  881. goto on_error;
  882. }
  883. }
  884. else{
  885. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("session->video_shm_q_env is NULL.");
  886. }
  887. if (session->video_shm_q_opt)
  888. {
  889. session->local_encode_sws_ctx_opt = sws_getContext(
  890. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, PIX_FMT_BGR24,
  891. REC_COMMON_VIDEO_RTP_WIDTH, REC_COMMON_VIDEO_RTP_HEIGHT, PIX_FMT_YUV420P,
  892. SWS_POINT, NULL, NULL, NULL);
  893. if (!session->local_encode_sws_ctx_opt) {
  894. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get session->local_encode_sws_ctx_opt failed.");
  895. goto on_error;
  896. }
  897. }
  898. else{
  899. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("session->video_shm_q_opt is NULL.");
  900. }
  901. #ifdef RVC_OS_WIN
  902. if (DOUBLERECORD_CALLTYPE != session->conf.nCallType){
  903. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create new remote video player");
  904. if (session->remote_hwnd)
  905. {
  906. if (session->conf.eDeviceType == eMobilePadType)
  907. {
  908. rc = videoplayer_create(session->remote_hwnd,
  909. 0,
  910. 0,
  911. session->conf.remote_video_view_cx,
  912. session->conf.remote_video_view_cy,
  913. REC_COMMON_VIDEO_FPS_MOBILE_AGENT,
  914. REC_COMMON_VIDEO_FPS_DEN,
  915. session->conf.remote_video_width,
  916. session->conf.remote_video_height,
  917. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP,
  918. session->conf.iremote_wind_flags,
  919. "remote",
  920. on_remoteWinstate,
  921. NULL,
  922. session,
  923. &session->remote_player);
  924. if (rc != 0)
  925. {
  926. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create remote video player failed!");
  927. goto on_error;
  928. }
  929. else{
  930. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create remote video player suc!");
  931. }
  932. }
  933. else if((session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  934. {
  935. rc = videoplayer_create(session->remote_hwnd,
  936. 0,
  937. 0,
  938. session->conf.remote_video_view_cx,
  939. session->conf.remote_video_view_cy,
  940. REC_COMMON_VIDEO_FPS_MOBILE,
  941. REC_COMMON_VIDEO_FPS_DEN,
  942. session->conf.remote_video_width,
  943. session->conf.remote_video_height,
  944. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP,
  945. session->conf.iremote_wind_flags,
  946. "remote",
  947. on_remoteWinstate,
  948. NULL,
  949. session,
  950. &session->remote_player);
  951. if (rc != 0) {
  952. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create remote video player failed!");
  953. goto on_error;
  954. }
  955. else{
  956. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create remote video player suc!");
  957. }
  958. }
  959. else if(session->conf.eDeviceType == eStand2sType)
  960. {
  961. rc = videoplayer_create(session->remote_hwnd,
  962. 0,
  963. 0,
  964. session->conf.remote_video_view_cx,
  965. session->conf.remote_video_view_cy,
  966. REC_COMMON_VIDEO_FPS_NUM,
  967. REC_COMMON_VIDEO_FPS_DEN,
  968. session->conf.remote_video_width,
  969. session->conf.remote_video_height,
  970. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PUSH|VIDEOPLAYER_FLAG_CHECKTOP,
  971. session->conf.iremote_wind_flags,
  972. "remote",
  973. on_remoteWinstate,
  974. NULL,
  975. session,
  976. &session->remote_player);
  977. if (rc != 0)
  978. {
  979. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create remote video player failed!");
  980. goto on_error;
  981. }
  982. else{
  983. 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);
  984. }
  985. }
  986. }
  987. else
  988. {
  989. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("remote_hwnd == Null");
  990. }
  991. }
  992. else{
  993. if (NULL != pg_last_session){
  994. session->remote_player = pg_last_session->remote_player;
  995. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("double record call remote_player reuse.");
  996. }
  997. else{
  998. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("pg_last_session is NULL, and call type is %d.", session->conf.nCallType);
  999. }
  1000. }
  1001. BOOL bIsActiveInspect = FALSE;
  1002. if (session->conf.ref_Is_ActiveInspect != NULL){
  1003. if (*session->conf.ref_Is_ActiveInspect == 1){
  1004. bIsActiveInspect = TRUE;
  1005. }
  1006. else{
  1007. bIsActiveInspect = FALSE;
  1008. }
  1009. }
  1010. else{
  1011. bIsActiveInspect = FALSE;
  1012. }
  1013. if (DOUBLERECORD_CALLTYPE != session->conf.nCallType){
  1014. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create new local video player");
  1015. if (session->conf.eDeviceType == eMobilePadType)
  1016. {
  1017. rc = videoplayer_create(session->local_hwnd,
  1018. 0,
  1019. 0,
  1020. session->conf.local_video_view_cx,
  1021. session->conf.local_video_view_cy,
  1022. REC_COMMON_VIDEO_FPS_MOBILE,
  1023. REC_COMMON_VIDEO_FPS_DEN,
  1024. REC_COMMON_VIDEO_PREVIEW_WIDTH,
  1025. REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  1026. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP,
  1027. session->conf.ilocal_wind_flags,
  1028. "local",
  1029. on_pull, free_frame, session,
  1030. &session->local_player);
  1031. if (rc != 0)
  1032. {
  1033. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create local video player failed!");
  1034. goto on_error;
  1035. }
  1036. else{
  1037. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create local video player suc!");
  1038. }
  1039. }
  1040. else if((session->conf.eDeviceType == ePadtype)||(session->conf.eDeviceType == eDesk2SType)||(session->conf.eDeviceType == eDesk1SType)||(session->conf.eDeviceType == eDesk2SIntegratedType))
  1041. {
  1042. rc = videoplayer_create(session->local_hwnd,
  1043. 0,
  1044. 0,
  1045. session->conf.local_video_view_cx,
  1046. session->conf.local_video_view_cy,
  1047. REC_COMMON_VIDEO_FPS_MOBILE,
  1048. REC_COMMON_VIDEO_FPS_DEN,
  1049. REC_COMMON_VIDEO_PREVIEW_WIDTH,
  1050. REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  1051. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP,
  1052. session->conf.ilocal_wind_flags,
  1053. "local",
  1054. on_pull, free_frame, session,
  1055. &session->local_player);
  1056. if (rc != 0)
  1057. {
  1058. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create local video player failed!");
  1059. goto on_error;
  1060. }
  1061. else{
  1062. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create local video player suc!");
  1063. }
  1064. }
  1065. else if(session->conf.eDeviceType == eStand2sType)
  1066. {
  1067. rc = videoplayer_create(session->local_hwnd,
  1068. 0,
  1069. 0,
  1070. session->conf.local_video_view_cx,
  1071. session->conf.local_video_view_cy,
  1072. REC_COMMON_VIDEO_FPS_NUM,
  1073. REC_COMMON_VIDEO_FPS_DEN,
  1074. bIsActiveInspect?REC_COMMON_VIDEO_SNAPSHOT_WIDTH:REC_COMMON_VIDEO_PREVIEW_WIDTH,
  1075. bIsActiveInspect?REC_COMMON_VIDEO_SNAPSHOT_WIDTH:REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  1076. //*VIDEOPLAYER_FLAG_DOUBLESIZE|*/VIDEOPLAYER_FLAG_PULL|VIDEOPLAYER_FLAG_CHECKTOP,
  1077. session->conf.ilocal_wind_flags,
  1078. "local",
  1079. on_pull, free_frame, session,
  1080. &session->local_player);
  1081. if (rc != 0)
  1082. {
  1083. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create local video player failed!");
  1084. goto on_error;
  1085. }
  1086. else{
  1087. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create local video player suc!");
  1088. }
  1089. }
  1090. }
  1091. else{
  1092. if (NULL != pg_last_session){
  1093. session->local_player = pg_last_session->local_player;
  1094. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("double record call local player reuse.");
  1095. }
  1096. }
  1097. if (session->remote_hwnd)
  1098. {
  1099. if (0 != session->conf.remote_rtp_port)
  1100. {
  1101. if (0 != start_video_rtpsession(session)) {
  1102. goto on_error;
  1103. }
  1104. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType){
  1105. if (NULL != pg_last_session){
  1106. pg_last_session->rtp = session->rtp;
  1107. }
  1108. }
  1109. if (0 != start_local_video_clock(session)) {
  1110. goto on_error;
  1111. }
  1112. }
  1113. else{
  1114. pg_last_session = session; //保存session信息
  1115. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("begin show agent static picture.");
  1116. show_remote_agnet_picture(session);
  1117. }
  1118. }
  1119. #else
  1120. rc = start_video_rtpsession(session);
  1121. if (0 != rc) {
  1122. goto on_error;
  1123. }
  1124. rc = start_local_video_clock(session);
  1125. if (0 != rc) {
  1126. goto on_error;
  1127. }
  1128. #endif // RVC_OS_WIN
  1129. on_error:
  1130. return rc;
  1131. }
  1132. static void stop_video(video_session_t *session)
  1133. {
  1134. #ifdef RVC_OS_WIN
  1135. if (session->local_clock && session->remote_hwnd)
  1136. {
  1137. videoclock_stop(session->local_clock);
  1138. videoclock_destroy(session->local_clock);
  1139. session->local_clock = NULL;
  1140. pg_remote_hwnd = NULL;
  1141. pg_local_hwnd = NULL;
  1142. }
  1143. #else
  1144. if (session->local_clock)
  1145. {
  1146. videoclock_stop(session->local_clock);
  1147. videoclock_destroy(session->local_clock);
  1148. session->local_clock = NULL;
  1149. }
  1150. #endif // RVC_OS_WIN
  1151. if (session->rtp){
  1152. videortp_stop(session->rtp);
  1153. videortp_destroy(session->rtp);
  1154. session->rtp = NULL;
  1155. }
  1156. else {
  1157. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("session->rtp == null");
  1158. }
  1159. #ifdef RVC_OS_WIN
  1160. if (session->local_player) {
  1161. videoplayer_destroy(session->local_player);
  1162. session->local_player = NULL;
  1163. }
  1164. if (session->remote_player && session->remote_hwnd)
  1165. {
  1166. videoplayer_destroy(session->remote_player);
  1167. session->remote_player = NULL;
  1168. }
  1169. #else
  1170. #endif
  1171. if (session->local_encode_sws_ctx_env) {
  1172. sws_freeContext(session->local_encode_sws_ctx_env);
  1173. session->local_encode_sws_ctx_env = NULL;
  1174. }
  1175. if (session->local_encode_sws_ctx_opt) {
  1176. sws_freeContext(session->local_encode_sws_ctx_opt);
  1177. session->local_encode_sws_ctx_opt = NULL;
  1178. }
  1179. }
  1180. #ifdef RVC_OS_WIN
  1181. static bool ReMoveOutOfScreenVideoWindow(HWND hWnd, RECT rect)
  1182. {
  1183. bool bret = false;
  1184. int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  1185. int iScreenHight = GetSystemMetrics(SM_CYSCREEN);
  1186. int iVideoWidth = rect.right - rect.left;
  1187. int iVideoHight = rect.bottom - rect.top;
  1188. if (rect.left < 0) {
  1189. if (rect.bottom <= iScreenHight) {
  1190. MoveWindow(hWnd, 0, rect.top, iVideoWidth, iVideoHight, TRUE);
  1191. }
  1192. else {
  1193. MoveWindow(hWnd, 0, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1194. }
  1195. bret = true;
  1196. }
  1197. if (rect.right > iScreenWidth) {
  1198. if (rect.bottom <= iScreenHight) {
  1199. MoveWindow(hWnd, iScreenWidth - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1200. }
  1201. else {
  1202. MoveWindow(hWnd, iScreenWidth - iVideoWidth, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1203. }
  1204. bret = true;
  1205. }
  1206. if (rect.bottom > iScreenHight) {
  1207. if (rect.left >= 0 && rect.right <= iScreenWidth) {
  1208. MoveWindow(hWnd, rect.left, iScreenHight - iVideoHight, iVideoWidth, iVideoHight, TRUE);
  1209. bret = true;
  1210. }
  1211. }
  1212. return bret;
  1213. }
  1214. static bool ReMoveCenterOtherVideoWindow(HWND hWnd, RECT rect, RECT otherect)
  1215. {
  1216. bool bret = false;
  1217. int iVideoWidth = rect.right - rect.left;
  1218. int iVideoHight = rect.bottom - rect.top;
  1219. int iOtherVideoWidth = otherect.right - otherect.left;
  1220. int iOtherVideoHight = otherect.bottom - otherect.top;
  1221. if (iVideoWidth > iOtherVideoHight || iVideoHight > iOtherVideoHight) {
  1222. return bret;
  1223. }
  1224. if (rect.left > otherect.left + iVideoWidth && rect.right < otherect.right) {
  1225. if (rect.top > otherect.top && rect.bottom < otherect.bottom) {
  1226. if (otherect.right - rect.right <= rect.left - otherect.left) {
  1227. MoveWindow(hWnd, otherect.right - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1228. }
  1229. else {
  1230. MoveWindow(hWnd, otherect.left, rect.top, iVideoWidth, iVideoHight, TRUE);
  1231. }
  1232. bret = true;
  1233. }
  1234. }
  1235. if (rect.right < otherect.right - iVideoWidth && rect.left > otherect.left) {
  1236. if (rect.top > otherect.top && rect.bottom < otherect.bottom) {
  1237. if (otherect.right - rect.right <= rect.left - otherect.left) {
  1238. MoveWindow(hWnd, otherect.right - iVideoWidth, rect.top, iVideoWidth, iVideoHight, TRUE);
  1239. }
  1240. else {
  1241. MoveWindow(hWnd, otherect.left, rect.top, iVideoWidth, iVideoHight, TRUE);
  1242. }
  1243. bret = true;
  1244. }
  1245. }
  1246. return bret;
  1247. }
  1248. static bool ReMoveVideoWindow(HWND hWnd, RECT rect, RECT otherect)
  1249. {
  1250. bool bret = false;
  1251. bret = ReMoveOutOfScreenVideoWindow(hWnd, rect);
  1252. bret = ReMoveCenterOtherVideoWindow(hWnd, rect, otherect);
  1253. return bret;
  1254. }
  1255. static int HandleVideoMoveEvent(int iMessageType, HWND hWnd, video_session_t* pSession)
  1256. {
  1257. int iRet = -1;
  1258. if (NULL == pSession || NULL == hWnd) {
  1259. return iRet;
  1260. }
  1261. HWND hOtherWnd = NULL;
  1262. //本地和远端标识 1为本地,2为远端
  1263. int iVideoType = 2;
  1264. if (hWnd == pSession->local_hwnd) {
  1265. iVideoType = 1;
  1266. hOtherWnd = pSession->remote_hwnd;
  1267. }
  1268. else {
  1269. hOtherWnd = pSession->local_hwnd;
  1270. }
  1271. if (NULL == hOtherWnd) {
  1272. return iRet;
  1273. }
  1274. bool bMoved = false;
  1275. RECT rect;
  1276. GetWindowRect(hWnd, &rect);
  1277. RECT otherect;
  1278. GetWindowRect(hOtherWnd, &otherect);
  1279. bMoved = ReMoveVideoWindow(hWnd, rect, otherect);
  1280. if (bMoved) {
  1281. GetWindowRect(hWnd, &rect);
  1282. }
  1283. if (NULL != pSession->conf.video_echo_cb && NULL != pSession->conf.video_echo_cb->on_video_box_move) {
  1284. pSession->conf.video_echo_cb->on_video_box_move(iMessageType, iVideoType, rect.left, rect.bottom, pSession->conf.video_echo_cb->user_data);
  1285. iRet = 0;
  1286. }
  1287. return iRet;
  1288. }
  1289. #else
  1290. #endif // RVC_OS_WIN
  1291. #ifdef RVC_OS_WIN
  1292. static LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  1293. {
  1294. video_session_t* session = (video_session_t*)GetWindowLongPtrA(hWnd, GWLP_USERDATA);
  1295. switch (msg) {
  1296. case WM_DESTROY:
  1297. if (session->local_hwnd == hWnd) {
  1298. session->local_hwnd = 0;
  1299. }
  1300. else if (session->remote_hwnd == hWnd) {
  1301. session->remote_hwnd = 0;
  1302. }
  1303. if (session->local_hwnd == 0 && session->remote_hwnd == 0) {
  1304. PostQuitMessage(0);
  1305. }
  1306. return 0;
  1307. case WM_NCHITTEST:
  1308. if (((session->local_hwnd == hWnd) && (0 != session->conf.local_move)) || ((session->remote_hwnd == hWnd) && (0 != session->conf.remote_move))) {
  1309. return HTCAPTION;
  1310. }
  1311. else {
  1312. break;
  1313. }
  1314. case WM_ACTIVATE:
  1315. case WM_TOUCH:
  1316. case WM_GESTURE:
  1317. ReleaseCapture();
  1318. return 0;
  1319. #if 1
  1320. case WM_WINDOWPOSCHANGED:
  1321. {
  1322. LPWINDOWPOS pPos = (LPWINDOWPOS)lParam;
  1323. if (pPos->hwndInsertAfter != HWND_TOPMOST && pPos->hwndInsertAfter != HWND_TOP) {
  1324. //BringWindowToTop(hWnd);
  1325. SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  1326. }
  1327. else {
  1328. return DefWindowProc(hWnd, msg, wParam, lParam);
  1329. }
  1330. }
  1331. return 0;
  1332. //return DefWindowProc(hWnd, msg, wParam, lParam);
  1333. #endif
  1334. case WM_MBUTTONDOWN:
  1335. case WM_MBUTTONUP:
  1336. case WM_MBUTTONDBLCLK:
  1337. case WM_LBUTTONDBLCLK:
  1338. case WM_LBUTTONDOWN:
  1339. case WM_LBUTTONUP:
  1340. if (session) {
  1341. if (session->local_hwnd) {
  1342. //ReleaseCapture();
  1343. }
  1344. }
  1345. return 0;
  1346. case WM_CLOSE:
  1347. stop_video(session);
  1348. //SetEvent(session->ui_thread);
  1349. DestroyWindow(session->local_hwnd);
  1350. DestroyWindow(session->remote_hwnd);
  1351. return 0;
  1352. case WM_MOVE:
  1353. break;
  1354. case WM_ENTERSIZEMOVE:
  1355. {
  1356. HandleVideoMoveEvent(0, hWnd, session);
  1357. }
  1358. break;
  1359. case WM_EXITSIZEMOVE:
  1360. {
  1361. HandleVideoMoveEvent(1, hWnd, session);
  1362. }
  1363. break;
  1364. default:
  1365. return DefWindowProc(hWnd, msg, wParam, lParam);
  1366. }
  1367. return 0;
  1368. }
  1369. #endif //
  1370. #ifdef RVC_OS_WIN
  1371. static unsigned int __stdcall ui_proc(void *arg)
  1372. {
  1373. video_session_t *session = (video_session_t*)arg;
  1374. WNDCLASSA wc = {0};
  1375. ATOM a = 0;
  1376. HWND hWnd = NULL;
  1377. MSG msg;
  1378. HINSTANCE hInst = ModuleBase::GetModuleBase()->GetInstance();
  1379. bool breuse_local_wnd = false;
  1380. bool breuse_remote_wnd = false;
  1381. DWORD dLocal_style = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_NOACTIVATE;
  1382. DWORD dRemote_style = WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_NOACTIVATE;
  1383. //双录两阶段视频窗口复用
  1384. if (NULL != pg_local_hwnd){
  1385. session->local_hwnd = pg_local_hwnd;
  1386. //SetWindowLongPtrA(session->local_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1387. breuse_local_wnd = true;
  1388. }
  1389. else{
  1390. if (0 != session->conf.local_move){
  1391. dLocal_style -= WS_EX_NOACTIVATE;
  1392. }
  1393. if (0 != session->conf.remote_move){
  1394. dRemote_style -= WS_EX_NOACTIVATE;
  1395. }
  1396. CoInitialize(0);
  1397. wc.cbClsExtra = 0;
  1398. wc.cbWndExtra = 0;
  1399. wc.hInstance = hInst;
  1400. wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  1401. wc.hCursor = NULL;
  1402. wc.hIcon = NULL;
  1403. wc.lpfnWndProc = &WndProc;
  1404. wc.lpszClassName = WNDCLS_NAME;
  1405. wc.style = CS_HREDRAW | CS_OWNDC | CS_VREDRAW;
  1406. a = RegisterClassA(&wc);
  1407. if (a == 0)
  1408. return 0;
  1409. session->local_hwnd = CreateWindowExA(dLocal_style,
  1410. WNDCLS_NAME, NULL, WS_POPUP|WS_VISIBLE,
  1411. session->conf.local_video_view_x, session->conf.local_video_view_y, session->conf.local_video_view_cx, session->conf.local_video_view_cy,
  1412. NULL, NULL, hInst, NULL);
  1413. if (session->local_hwnd) {
  1414. SetWindowLongPtrA(session->local_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1415. if (0 == session->conf.local_move){
  1416. SetWindowPos(session->local_hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
  1417. }
  1418. pg_local_hwnd = session->local_hwnd;
  1419. //LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_LOCAL_WINDOW_CREATE_SUCCESS, "create local window success and restore local video hwnd.");
  1420. }
  1421. else{
  1422. char strinfo[MAX_PATH] = {0};
  1423. _snprintf(strinfo, MAX_PATH, "%s%d", "create local window failed and last error is ", GetLastError());
  1424. LogWarn(Severity_Middle, Error_Debug, EVENT_MOD_SIP_LOCAL_WINDOW_CREATE_FAILED, strinfo);
  1425. //return -1;
  1426. }
  1427. }
  1428. 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)
  1429. {
  1430. if (NULL != pg_remote_hwnd){
  1431. session->remote_hwnd = pg_remote_hwnd;
  1432. breuse_remote_wnd = true;
  1433. if (NULL != pg_last_session){
  1434. record_agent_picture_show_session_destory(pg_last_session);
  1435. }
  1436. }
  1437. else{
  1438. session->remote_hwnd = CreateWindowExA(dRemote_style,
  1439. WNDCLS_NAME, NULL, WS_POPUP|WS_VISIBLE,
  1440. session->conf.remote_video_view_x, session->conf.remote_video_view_y, session->conf.remote_video_view_cx, session->conf.remote_video_view_cy,
  1441. NULL, NULL, hInst, NULL);
  1442. if (session->remote_hwnd) {
  1443. SetWindowLongPtrA(session->remote_hwnd, GWLP_USERDATA, (LONG_PTR)session);
  1444. if (0 == session->conf.remote_move){
  1445. SetWindowPos(session->remote_hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
  1446. }
  1447. pg_remote_hwnd = session->remote_hwnd;
  1448. //LogWarn(Severity_Low, Error_Debug, EVENT_MOD_SIP_REMOTE_WINDOW_CREATE_SUCCESS, "create remote window success and restore remote video hwnd.");
  1449. }
  1450. else{
  1451. char strinfo[MAX_PATH] = {0};
  1452. _snprintf(strinfo, MAX_PATH, "%s%d", "create remote window failed and last error is ", GetLastError());
  1453. LogWarn(Severity_Middle, Error_Debug, EVENT_MOD_SIP_REMOTE_WINDOW_CREATE_FAILED, strinfo);
  1454. //return -1;
  1455. }
  1456. }
  1457. }
  1458. else
  1459. {
  1460. session->remote_hwnd = NULL;
  1461. }
  1462. if (session->local_hwnd/* && session->remote_hwnd*/)
  1463. {
  1464. int rc = 0;
  1465. ShowCursor(FALSE);
  1466. SetEvent(session->ui_event);
  1467. rc = start_video(session);
  1468. if (rc != 0) {
  1469. LogWarn(Severity_Middle, Error_Debug, ERROR_MOD_SIP_START_VIDEO_FAILED, "start video failed!");
  1470. }
  1471. if (!breuse_local_wnd && !breuse_remote_wnd)
  1472. {
  1473. while (GetMessageA(&msg, NULL, NULL, NULL))
  1474. {
  1475. if (msg.message == WM_CLOSE) {
  1476. }
  1477. TranslateMessage(&msg);
  1478. DispatchMessageA(&msg);
  1479. }
  1480. SetEvent(session->ui_event);
  1481. }
  1482. }
  1483. if (a)
  1484. UnregisterClassA(WNDCLS_NAME, hInst);
  1485. CoUninitialize();
  1486. return 0;
  1487. }
  1488. #else
  1489. static void __video_render_log(render_loglevel elevel, void* user_data, const char* fmt, va_list arg)
  1490. {
  1491. int n = vsnprintf(NULL, 0, fmt, arg);
  1492. if (n >= MAX_PATH) {
  1493. char* buf = (char*)malloc((size_t)(n + 1));
  1494. vsnprintf(buf, n + 1, fmt, arg);
  1495. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  1496. free(buf);
  1497. }
  1498. else {
  1499. char strlog[MAX_PATH] = { 0 };
  1500. vsnprintf(strlog, MAX_PATH, fmt, arg);
  1501. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  1502. }
  1503. }
  1504. #endif // RVC_OS_WIN
  1505. static int start_ui(video_session_t *session)
  1506. {
  1507. #ifdef RVC_OS_WIN
  1508. session->ui_event = CreateEventA(NULL, FALSE, FALSE, NULL);
  1509. if (!session->ui_event) {
  1510. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("phonemedia_start create ui event failed!");
  1511. return Error_Resource;
  1512. }
  1513. session->ui_thread = (HANDLE)_beginthreadex(NULL, 0, &ui_proc, session, 0, NULL);
  1514. if (!session->ui_thread) {
  1515. CloseHandle(session->ui_event);
  1516. session->ui_event = NULL;
  1517. return Error_Resource;
  1518. }
  1519. {
  1520. HANDLE hs[] = {session->ui_event, session->ui_thread};
  1521. DWORD dwRet = WaitForMultipleObjects(array_size(hs), hs, FALSE, INFINITE);
  1522. if (dwRet == WAIT_OBJECT_0) { //
  1523. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("wait for ui ready event ok!");
  1524. } else if (dwRet == WAIT_OBJECT_0 + 1) { // thread exit
  1525. CloseHandle(session->ui_thread);
  1526. session->ui_thread = NULL;
  1527. CloseHandle(session->ui_event);
  1528. session->ui_event = NULL;
  1529. return Error_Resource;
  1530. }
  1531. }
  1532. return 0;
  1533. #endif // RVC_OS_WIN
  1534. }
  1535. static void stop_ui(video_session_t *session)
  1536. {
  1537. #ifdef RVC_OS_WIN
  1538. if (DOUBLERECORD_CALLTYPE == session->conf.nCallType) {
  1539. if (NULL != pg_last_session) {
  1540. pg_last_session->rtp = session->rtp;
  1541. pg_last_session->local_clock = session->local_clock;
  1542. CloseHandle(session->ui_thread);
  1543. session->ui_thread = NULL;
  1544. CloseHandle(session->ui_event);
  1545. session->ui_event = NULL;
  1546. session = pg_last_session;
  1547. }
  1548. }
  1549. if (NULL != session->pic_record) {
  1550. if (NULL != session->pic_record->work_thread) {
  1551. SetEvent(session->pic_record->evt);
  1552. CloseHandle(session->pic_record->work_thread);
  1553. session->pic_record->work_thread = NULL;
  1554. CloseHandle(session->pic_record->evt);
  1555. session->pic_record->evt = NULL;
  1556. }
  1557. }
  1558. if (session->local_hwnd) {
  1559. BOOL bRet = PostMessageA(session->local_hwnd, WM_CLOSE, 0, 0);
  1560. DWORD dCode = WaitForSingleObject(session->ui_thread, INFINITE);
  1561. CloseHandle(session->ui_thread);
  1562. session->ui_thread = NULL;
  1563. CloseHandle(session->ui_event);
  1564. session->ui_event = NULL;
  1565. }
  1566. pg_last_session = NULL;
  1567. pg_local_hwnd = NULL;
  1568. pg_remote_hwnd = NULL;
  1569. #endif
  1570. }
  1571. int video_session_create(const video_session_conf_t *conf, video_session_t **p_session)
  1572. {
  1573. video_session_t *session = ZALLOC_T(video_session_t);
  1574. if (session)
  1575. {
  1576. memcpy(&session->conf, conf, sizeof(video_session_conf_t));
  1577. //session->bshow_remote = false;
  1578. //session->ilast_windstae = 0;
  1579. session->video_shm_q_env = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE);
  1580. session->video_shm_q_remote = new Clibvideoqueue(REC_COMMON_VIDEO_REMOTE_SHM_RTP_QUEUE);
  1581. session->video_shm_q_preview = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_PREVIEW_QUEUE);
  1582. char strPath[MAX_PATH_SIZE]={0};
  1583. char strImgPath[MAX_PATH_SIZE]={0};
  1584. #ifdef RVC_OS_WIN
  1585. GetCurrentRunPath(strPath);
  1586. sprintf(strImgPath, "%s\\bin\\error.jpg", strPath);
  1587. #else
  1588. snprintf(strImgPath, MAX_PATH_SIZE, "%s", "./bin/error.jpg");
  1589. #endif // RVC_OS_WIN
  1590. if (ExistsFile(strImgPath))
  1591. {
  1592. IplImage*img = cvLoadImage(strImgPath,1);
  1593. if (img != NULL)
  1594. {
  1595. session->video_error = new videoq_frame;
  1596. session->video_error->format = VIDEOQ_FORMAT_RGB24;
  1597. session->video_error->framesize = 320*180*3;
  1598. session->video_error->height = 180;
  1599. session->video_error->width = 320;
  1600. session->video_error->data = new unsigned char[320*180*3];
  1601. memcpy(session->video_error->data,img->imageData,320*180*3);
  1602. cvReleaseImage(&img);
  1603. }
  1604. }
  1605. else
  1606. {
  1607. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Load error img Fail");
  1608. session->video_error = NULL;
  1609. }
  1610. //加载人形背景框
  1611. char strPersonPath[MAX_PATH_SIZE] = {0};
  1612. #ifdef RVC_OS_WIN
  1613. sprintf(strPersonPath, "%s\\bin\\rxk.jpg", strPath);
  1614. #else
  1615. snprintf(strPersonPath, MAX_PATH_SIZE, "./bin/rxk.jpg");
  1616. #endif // RVC_OS_WIN
  1617. if (ExistsFile(strPersonPath))
  1618. {
  1619. if (session->personimage == NULL)
  1620. {
  1621. session->personimage = cvLoadImage(strPersonPath);
  1622. }
  1623. if (session->personmask == NULL )
  1624. {
  1625. session->personmask = cvLoadImage(strPersonPath,0);
  1626. }
  1627. //IplImage*persongrey = cvLoadImage(strPersonPath,CV_LOAD_IMAGE_GRAYSCALE);
  1628. //二值化提取人形框掩码
  1629. //cvThreshold(persongrey,session->personmask,150, 255, CV_THRESH_BINARY);
  1630. }
  1631. else
  1632. {
  1633. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Load person img Fail");
  1634. session->personimage = NULL;
  1635. session->personmask = NULL;
  1636. }
  1637. #if 0
  1638. video_frame frame;
  1639. video_frame_alloc(320, 180, VIDEO_FORMAT_RGB24, &frame);
  1640. video_frame_fill_black(&frame);
  1641. videoq_frame frm;
  1642. frm.data = frame.data[0];
  1643. session->video_shm_q_env->GetVideo(&frm, 0);
  1644. video_frame_save_bmpfile("d:\\a.bmp", &frame);
  1645. #endif
  1646. if (conf->camera_count == 2) {
  1647. session->video_shm_q_opt = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
  1648. }
  1649. session->bvideorecved = false;
  1650. session->blocalrender = false;
  1651. session->bremoterender = false;
  1652. *p_session = session;
  1653. }
  1654. return 0;
  1655. }
  1656. int video_session_start(video_session_t *session)
  1657. {
  1658. int rc = -1;
  1659. #ifdef RVC_OS_WIN
  1660. rc = start_ui(session);
  1661. #else
  1662. rc = start_video(session);
  1663. #endif // RVC_OS_WIN
  1664. return rc;
  1665. }
  1666. void video_session_stop(video_session_t *session)
  1667. {
  1668. #ifdef RVC_OS_WIN
  1669. stop_ui(session);
  1670. #else
  1671. stop_video(session);
  1672. #endif
  1673. }
  1674. void video_session_destroy(video_session_t *session)
  1675. {
  1676. if (session->video_error){
  1677. delete session->video_error->data;
  1678. delete session->video_error;
  1679. }
  1680. if (session->video_shm_q_remote) {
  1681. delete session->video_shm_q_remote;
  1682. session->video_shm_q_remote = NULL;
  1683. }
  1684. if (session->personimage){
  1685. cvReleaseImage(&session->personimage);
  1686. }
  1687. if (session->personmask){
  1688. cvReleaseImage(&session->personmask);
  1689. }
  1690. if (session->recordareaimage){
  1691. cvReleaseImage(&session->recordareaimage);
  1692. }
  1693. if (session->recordareamask){
  1694. cvReleaseImage(&session->recordareamask);
  1695. }
  1696. free(session);
  1697. }
  1698. void av_log_cb(void*ptr, int level, const char*fmt, va_list list)
  1699. {
  1700. int n = vsnprintf(NULL, 0, fmt, list);
  1701. if (n >= MAX_PATH) {
  1702. char* buf = (char*)malloc((size_t)(n + 1));
  1703. vsnprintf(buf, n + 1, fmt, list);
  1704. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  1705. free(buf);
  1706. }
  1707. else {
  1708. char strlog[MAX_PATH] = { 0 };
  1709. vsnprintf(strlog, MAX_PATH, fmt, list);
  1710. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  1711. }
  1712. }
  1713. int video_lib_init()
  1714. {
  1715. #ifdef RVC_OS_WIN
  1716. videoframework_init();
  1717. av_log_set_callback(&av_log_cb);
  1718. //av_log_set_level(AV_LOG_DEBUG);
  1719. av_log_set_level(AV_LOG_QUIET);
  1720. #else
  1721. videoframework_init();
  1722. if (0 != VideoRender_Init()){
  1723. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("sdl init failed.");
  1724. }
  1725. //av_log_set_callback(&av_log_cb);
  1726. //av_log_set_level(AV_LOG_TRACE);
  1727. //av_log_set_level(AV_LOG_QUIET);
  1728. #endif // RVC_OS_WIN
  1729. return 0;
  1730. }
  1731. void video_lib_deinit()
  1732. {
  1733. #ifdef RVC_OS_WIN
  1734. CoUninitialize();
  1735. #else
  1736. #endif // RVC_OS_WIN
  1737. videoframework_term();
  1738. VideoRender_Term();
  1739. }