video_render.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #include "video_render.h"
  2. #include "SpBase.h"
  3. #include "Event.h"
  4. #include "../../Other/libvideoframework/videoutil.h"
  5. #include "../../Other/libvideoqueue/libvideoqueue.h"
  6. #include "../../Other/rvcmediacommon/rvc_media_common.h"
  7. #include "fileutil.h"
  8. extern "C"{
  9. #include <libavutil/avutil.h>
  10. #include <libavcodec/avcodec.h>
  11. #include <libswscale/swscale.h>
  12. }
  13. #include "../../Other/libvideoframework/video_common/ffmpeg_api_cpp_adapter.h"
  14. #include "cv.h"
  15. #include "highgui.h"
  16. #include "y2k_time.h"
  17. static int translate_image_resolution(video_frame** dstframe, const int iwidth, const int iheight, const video_frame* psrcframe)
  18. {
  19. int i = -1;
  20. if (iwidth == psrcframe->width && iheight == psrcframe->height) {
  21. return i;
  22. }
  23. video_frame* pframe = video_frame_new(iwidth, iheight, VIDEO_FORMAT_RGB24);
  24. video_frame_fill_black(pframe);
  25. SwsContext* sws = sws_getCachedContext(NULL, psrcframe->width, psrcframe->height,
  26. PIX_FMT_BGR24,
  27. iwidth,
  28. iheight,
  29. PIX_FMT_BGR24,
  30. SWS_LANCZOS,
  31. NULL,
  32. NULL,
  33. NULL);
  34. sws_scale(sws, psrcframe->data, psrcframe->linesize, 0, psrcframe->height, pframe->data, pframe->linesize);
  35. sws_freeContext(sws);
  36. *dstframe = pframe;
  37. i = 0;
  38. return i;
  39. }
  40. static int rvc_video_shm_enqueue(Clibvideoqueue* shm_queue, video_frame* frame, int flags, int iframeid)
  41. {
  42. videoq_frame tmp_frm;
  43. tmp_frm.data = frame->data[0];
  44. tmp_frm.framesize = frame->width * frame->height * 3;
  45. tmp_frm.format = VIDEOQ_FORMAT_RGB24;
  46. tmp_frm.width = frame->width;
  47. tmp_frm.height = frame->height;
  48. tmp_frm.iframeid = iframeid;
  49. unsigned int nowtime = y2k_time_now();
  50. if (!shm_queue->InsertVideo(&tmp_frm, flags, nowtime)){
  51. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("caution: rvc render shm_queue video insert shm video failed!");
  52. return Error_Unexpect;
  53. }
  54. else
  55. {
  56. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("video render shm_queue video insert shm video ok, and nowtime is %d, and frame id is %d.", nowtime, iframeid);
  57. return Error_Succeed;
  58. }
  59. }
  60. static int get_local_video_frame(video_frame** frame, int itype, Clibvideoqueue* local_video_queue, IplImage* personimage, IplImage* personmask)
  61. {
  62. video_frame* tmp_frame_preview = NULL;
  63. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24);
  64. videoq_frame frm;
  65. frm.data = tmp_frame_preview->data[0];
  66. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d, session->video_shm_q_preview = 0x%08x.", __FUNCTION__, __LINE__, local_video_queue);
  67. bool result = local_video_queue->GetVideo(&frm, VIDEOQUEUE_FLAG_HORIZONTAL_FLIP);
  68. if (result){
  69. if (1 == itype) {
  70. if (NULL != personimage && NULL != personmask)
  71. {
  72. IplImage* img = cvCreateImageHeader(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 3);
  73. img->imageData = (char*)frm.data;
  74. if (frm.width != personimage->width) {
  75. IplImage* tmp = cvCreateImage(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 3);
  76. IplImage* tmpmask = cvCreateImage(cvSize(frm.width, frm.height), IPL_DEPTH_8U, 1);
  77. cvResize(personimage, tmp);
  78. cvResize(personmask, tmpmask);
  79. cvAdd(img, tmp, img, tmpmask);
  80. cvReleaseImage(&tmp);
  81. cvReleaseImage(&tmpmask);
  82. }
  83. else {
  84. cvAdd(img, personimage, img, personmask);
  85. }
  86. cvReleaseImageHeader(&img);
  87. }
  88. }
  89. *frame = tmp_frame_preview;
  90. }
  91. return 0;
  92. }
  93. int rvc_remote_video_render(rvc_video_render_t* prender, void* videoframe)
  94. {
  95. if (NULL != prender->premote_render) {
  96. video_frame* echoframe = NULL;
  97. if (0 == translate_image_resolution(&echoframe, prender->location_param.iremote_view_cx, prender->location_param.iremote_view_cy, (const video_frame*)videoframe)) {
  98. prender->premote_render->RenderVideoFrame(echoframe, RVC_FLIP_VERTICAL);
  99. video_frame_delete(echoframe);
  100. echoframe = NULL;
  101. }
  102. else {
  103. prender->premote_render->RenderVideoFrame((video_frame*)videoframe, RVC_FLIP_VERTICAL);
  104. }
  105. }
  106. return 0;
  107. }
  108. void* rvc_videorender_func(void* arg)
  109. {
  110. LOG_FUNCTION();
  111. rvc_video_render_t* param = (rvc_video_render_t*)arg;
  112. int ilocal_video_fresh_time = param->location_param.ilocal_fresh_time;
  113. Clibvideoqueue* local_video_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_PREVIEW_QUEUE);
  114. char strPersonPath[MAX_PATH] = { 0 };
  115. snprintf(strPersonPath, MAX_PATH, "./bin/rxk.jpg");
  116. IplImage* personimage = NULL;
  117. IplImage* personmask = NULL;
  118. if (ExistsFile(strPersonPath)){
  119. personimage = cvLoadImage(strPersonPath);
  120. personmask = cvLoadImage(strPersonPath, 0);
  121. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Load person img Success");
  122. }
  123. if (NULL != param->plocal_render) {
  124. videorender_param_t tparam = { 0 };
  125. tparam.icx = param->location_param.ilocal_view_x;
  126. tparam.icy = param->location_param.ilocal_view_y;
  127. tparam.uwidth = param->location_param.ilocal_view_cx;
  128. tparam.uheight = param->location_param.ilocal_view_cy;
  129. tparam.ivideoformat = VIDEO_FORMAT_RGB24;
  130. if (0 == param->plocal_render->VideoRenderSetParam(&tparam)) {
  131. param->plocal_render->HideVideoWindow();
  132. }
  133. else {
  134. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d set video render param failed!", __FUNCTION__, __LINE__);
  135. return 0;
  136. }
  137. }
  138. if (0 != param->location_param.iremote_view_cx && 0 != param->location_param.iremote_view_cy) {
  139. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d create remote video width = %d, height = %d.", __FUNCTION__, __LINE__, param->location_param.iremote_view_cx, param->location_param.iremote_view_cy);
  140. if (NULL != param->premote_render) {
  141. videorender_param_t tparam_remote = { 0 };
  142. tparam_remote.icx = param->location_param.iremote_view_x;
  143. tparam_remote.icy = param->location_param.iremote_view_y;
  144. tparam_remote.uwidth = param->location_param.iremote_view_cx;
  145. tparam_remote.uheight = param->location_param.iremote_view_cy;
  146. tparam_remote.ivideoformat = VIDEO_FORMAT_RGB24;
  147. if (0 == param->premote_render->VideoRenderSetParam(&tparam_remote)) {
  148. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d video render set param success.",__FUNCTION__, __LINE__);
  149. }
  150. }
  151. else {
  152. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("remote video render is null.");
  153. }
  154. }
  155. if (NULL != param->plocal_render) {
  156. bool bshow_local = false;
  157. bool bset = true;
  158. param->plocal_render->StartVideoRender();
  159. for (; ; ) {
  160. struct timespec ts;
  161. clock_gettime(CLOCK_REALTIME, &ts);
  162. long unsec = ts.tv_nsec + (1000 * 1000 * ilocal_video_fresh_time);
  163. ts.tv_sec += (unsec / 1000000000);
  164. ts.tv_nsec = (unsec % 1000000000);
  165. if (0 != sem_timedwait(&param->ui_stop_sem, &ts) && (ETIMEDOUT == errno))
  166. {
  167. video_frame* local_video_frame = NULL;
  168. int iwindowstate = param->cb.on_window_type(param->cb.user_data);
  169. get_local_video_frame(&local_video_frame, iwindowstate, local_video_queue, personimage, personmask);
  170. if (NULL != local_video_frame) {
  171. video_frame* localframe = NULL;
  172. if (0 == translate_image_resolution(&localframe, param->location_param.ilocal_view_cx, param->location_param.ilocal_view_cy, local_video_frame)) {
  173. param->plocal_render->RenderVideoFrame(localframe, RVC_FLIP_VERTICAL);
  174. video_frame_delete(localframe);
  175. localframe = NULL;
  176. }
  177. else {
  178. param->plocal_render->RenderVideoFrame(local_video_frame, RVC_FLIP_VERTICAL);
  179. }
  180. bshow_local = true;
  181. video_frame_delete(local_video_frame);
  182. local_video_frame = NULL;
  183. }
  184. else {
  185. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d,get video from shm preview queue failed!", __FUNCTION__, __LINE__);
  186. }
  187. }
  188. else {
  189. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d videorender_func exit!", __FUNCTION__, __LINE__);
  190. param->plocal_render->HideVideoWindow();
  191. break;
  192. }
  193. if (bset) {
  194. if (bshow_local) {
  195. param->plocal_render->ShowVideoWindow();
  196. param->premote_render->ShowVideoWindow();
  197. bset = false;
  198. }
  199. }
  200. }
  201. param->plocal_render->StopVideoRender();
  202. }
  203. if (NULL != personimage) {
  204. cvReleaseImage(&personimage);
  205. }
  206. if (NULL != personmask) {
  207. cvReleaseImage(&personmask);
  208. }
  209. return 0;
  210. }
  211. void* rvc_remote_videorender_func(void* arg)
  212. {
  213. LOG_FUNCTION();
  214. rvc_video_render_t* param = (rvc_video_render_t*)arg;
  215. int iremote_video_fresh_time = param->location_param.iremote_fresh_time;
  216. char strImgPath[MAX_PATH] = { 0 };
  217. int irecord_video_frame_width = REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH;
  218. int irecord_video_frame_heigt = REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_HEIGHT;
  219. Clibvideoqueue* video_shm_q_remote = new Clibvideoqueue(REC_COMMON_VIDEO_REMOTE_SHM_RTP_QUEUE);
  220. snprintf(strImgPath, MAX_PATH, "%s", "./bin/agent.jpg");
  221. video_frame* remote_frame = video_frame_new(irecord_video_frame_width, irecord_video_frame_heigt, VIDEO_FORMAT_RGB24);
  222. video_frame_fill_black(remote_frame);
  223. if (ExistsFile(strImgPath)){
  224. IplImage* img = cvLoadImage(strImgPath, 1);
  225. videoq_frame* vframe = new videoq_frame;
  226. if (NULL != img) {
  227. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("load img success");
  228. vframe->format = VIDEOQ_FORMAT_RGB24;
  229. vframe->framesize = img->imageSize;
  230. vframe->width = img->width;
  231. vframe->height = img->height;
  232. vframe->data = new unsigned char[img->imageSize];
  233. memcpy(vframe->data, img->imageData, img->imageSize);
  234. cvReleaseImage(&img);
  235. }
  236. SwsContext* sws = sws_getContext(vframe->width, vframe->height, PIX_FMT_BGR24,
  237. irecord_video_frame_width,
  238. irecord_video_frame_heigt,
  239. PIX_FMT_BGR24,
  240. SWS_POINT, NULL, NULL, NULL);
  241. uint8_t* src_data[4] = { (unsigned char*)vframe->data + (vframe->height - 1) * vframe->width * 3, NULL, NULL, NULL };
  242. int src_linesize[4] = { -vframe->width * 3,0,0,0 };
  243. unsigned char* dst[4] = { remote_frame->data[0],NULL,NULL,NULL };
  244. int dst_linesize[4] = { remote_frame->linesize[0],0,0,0 };
  245. sws_scale(sws, src_data, src_linesize, 0, vframe->height, dst, dst_linesize);
  246. sws_freeContext(sws);
  247. if (vframe) {
  248. delete vframe->data;
  249. delete vframe;
  250. vframe = NULL;
  251. }
  252. }
  253. video_frame* showframe = NULL;
  254. if (-1 == translate_image_resolution(&showframe, param->location_param.iremote_view_cx, param->location_param.iremote_view_cy, remote_frame)) {
  255. showframe = video_frame_new(param->location_param.iremote_view_cx, param->location_param.iremote_view_cy, VIDEO_FORMAT_RGB24);
  256. video_frame_fill_black(showframe);
  257. video_frame_copy(showframe, remote_frame);
  258. }
  259. int iframeid = 0;
  260. int rc = rvc_video_shm_enqueue(video_shm_q_remote, remote_frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP, iframeid++);
  261. if (rc != Error_Succeed) {
  262. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("insert agent picture to remote video queue failed.");
  263. }
  264. for (; ; ) {
  265. struct timespec ts;
  266. clock_gettime(CLOCK_REALTIME, &ts);
  267. long unsec = ts.tv_nsec + (1000 * 1000 * iremote_video_fresh_time);
  268. ts.tv_sec += (unsec / 1000000000);
  269. ts.tv_nsec = (unsec % 1000000000);
  270. if (0 != sem_timedwait(&param->remote_render_stop_sem, &ts) && (ETIMEDOUT == errno)){
  271. rvc_remote_video_render(param, showframe);
  272. if (NULL != video_shm_q_remote){
  273. rc = rvc_video_shm_enqueue(video_shm_q_remote, remote_frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP, iframeid++);
  274. if (rc != Error_Succeed){
  275. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("insert agent picture to remote video queue failed.");
  276. }
  277. }
  278. }
  279. else {
  280. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d record remote video render_func exit!", __FUNCTION__, __LINE__);
  281. break;
  282. }
  283. }
  284. video_frame_delete(showframe);
  285. showframe = NULL;
  286. video_frame_delete(remote_frame);
  287. remote_frame = NULL;
  288. return 0;
  289. }
  290. int rvc_start_video_render(rvc_video_render_t* prender, bool bremote, rvc_video_box_move_callback_t* cb)
  291. {
  292. if (0 != sem_init(&prender->ui_stop_sem, 0, 0)) {
  293. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d create stop sem failed!", __FUNCTION__, __LINE__);
  294. return Error_Resource;
  295. }
  296. else {
  297. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d create stop sem success!", __FUNCTION__, __LINE__);
  298. }
  299. int err = pthread_create(&prender->ui_threadid, NULL, rvc_videorender_func, prender);
  300. if (Error_Succeed == err) {
  301. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create video render thread success, thread id is %u.", prender->ui_threadid);
  302. }
  303. else {
  304. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create video render thread failed.");
  305. return Error_Resource;
  306. }
  307. if (bremote) {
  308. if (0 != sem_init(&prender->remote_render_stop_sem, 0, 0)) {
  309. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d create remote stop sem failed!", __FUNCTION__, __LINE__);
  310. return Error_Resource;
  311. }
  312. else {
  313. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d create remote stop sem success!", __FUNCTION__, __LINE__);
  314. }
  315. err = pthread_create(&prender->remote_render_threadid, NULL, rvc_remote_videorender_func, prender);
  316. if (Error_Succeed == err) {
  317. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create video render thread success, thread id is %u.", prender->ui_threadid);
  318. }
  319. else {
  320. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create video render thread failed.");
  321. return Error_Resource;
  322. }
  323. }
  324. return err;
  325. }
  326. int rvc_stop_video_render(rvc_video_render_t* prender)
  327. {
  328. if (NULL == prender) {
  329. return Error_Param;
  330. }
  331. sem_post(&prender->ui_stop_sem);
  332. rvc_stop_remote_video_render(prender);
  333. if (prender->ui_threadid > 0) {
  334. if (0 == pthread_join(prender->ui_threadid, NULL)) {
  335. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("video render thread %u pthread join success.", prender->ui_threadid);
  336. prender->ui_threadid = 0;
  337. }
  338. else {
  339. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("video render thread pthread join error for %s.", strerror(errno));
  340. }
  341. }
  342. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("video render thread exit!");
  343. return Error_Succeed;
  344. }
  345. int rvc_stop_remote_video_render(rvc_video_render_t* prender)
  346. {
  347. if (prender->remote_render_threadid > 0) {
  348. sem_post(&prender->remote_render_stop_sem);
  349. if (0 == pthread_join(prender->remote_render_threadid, NULL)) {
  350. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("remote video render thread %u pthread join success.", prender->remote_render_threadid);
  351. prender->remote_render_threadid = 0;
  352. }
  353. else {
  354. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("remote video render thread pthread join error for %s.", strerror(errno));
  355. }
  356. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("remote video render thread exit!");
  357. }
  358. return Error_Succeed;
  359. }