video_render.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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)
  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. unsigned int nowtime = y2k_time_now();
  49. if (!shm_queue->InsertVideo(&tmp_frm, flags, nowtime)){
  50. Dbg("caution: insert shm video failed!");
  51. return Error_Unexpect;
  52. }
  53. else
  54. {
  55. //Dbg("insert shm video ok!");
  56. return Error_Succeed;
  57. }
  58. }
  59. static int get_local_video_frame(Clibvideoqueue* local_video_queue, video_frame** frame)
  60. {
  61. video_frame* tmp_frame_preview = NULL;
  62. tmp_frame_preview = video_frame_new(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24);
  63. videoq_frame frm;
  64. frm.data = tmp_frame_preview->data[0];
  65. //Dbg("%s:%d, session->video_shm_q_preview = 0x%08x.", __FUNCTION__, __LINE__, local_video_queue);
  66. BOOL result = local_video_queue->GetVideo(&frm, VIDEOQUEUE_FLAG_HORIZONTAL_FLIP);
  67. *frame = tmp_frame_preview;
  68. return 0;
  69. }
  70. int rvc_remote_video_render(rvc_video_render_t* prender, void* videoframe)
  71. {
  72. if (NULL != prender->premote_render) {
  73. video_frame* echoframe = NULL;
  74. if (0 == translate_image_resolution(&echoframe, prender->location_param.iremote_view_cx, prender->location_param.iremote_view_cy, (const video_frame*)videoframe)) {
  75. prender->premote_render->RenderVideoFrame(echoframe, RVC_FLIP_VERTICAL);
  76. video_frame_delete(echoframe);
  77. echoframe = NULL;
  78. }
  79. else {
  80. prender->premote_render->RenderVideoFrame((video_frame*)videoframe, RVC_FLIP_VERTICAL);
  81. }
  82. }
  83. return 0;
  84. }
  85. void* rvc_videorender_func(void* arg)
  86. {
  87. LOG_FUNCTION();
  88. rvc_video_render_t* param = (rvc_video_render_t*)arg;
  89. int ilocal_video_fresh_time = param->location_param.ilocal_fresh_time;
  90. Clibvideoqueue* local_video_queue = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_PREVIEW_QUEUE);
  91. if (NULL != param->plocal_render) {
  92. videorender_param_t tparam = { 0 };
  93. tparam.icx = param->location_param.ilocal_view_x;
  94. tparam.icy = param->location_param.ilocal_view_y;
  95. tparam.uwidth = param->location_param.ilocal_view_cx;
  96. tparam.uheight = param->location_param.ilocal_view_cy;
  97. tparam.ivideoformat = VIDEO_FORMAT_RGB24;
  98. if (0 == param->plocal_render->VideoRenderSetParam(&tparam)) {
  99. param->plocal_render->HideVideoWindow();
  100. }
  101. else {
  102. Dbg("%s:%d set video render param failed!", __FUNCTION__, __LINE__);
  103. return 0;
  104. }
  105. }
  106. if (0 != param->location_param.iremote_view_cx && 0 != param->location_param.iremote_view_cy) {
  107. Dbg("%s:%d create remote video width = %d, height = %d.", __FUNCTION__, __LINE__, param->location_param.iremote_view_cx, param->location_param.iremote_view_cy);
  108. if (NULL != param->premote_render) {
  109. videorender_param_t tparam_remote = { 0 };
  110. tparam_remote.icx = param->location_param.iremote_view_x;
  111. tparam_remote.icy = param->location_param.iremote_view_y;
  112. tparam_remote.uwidth = param->location_param.iremote_view_cx;
  113. tparam_remote.uheight = param->location_param.iremote_view_cy;
  114. tparam_remote.ivideoformat = VIDEO_FORMAT_RGB24;
  115. if (0 == param->premote_render->VideoRenderSetParam(&tparam_remote)) {
  116. //param->premote_render->ShowVideoWindow();
  117. }
  118. }
  119. else {
  120. Dbg("remote video render is null.");
  121. }
  122. }
  123. if (NULL != param->plocal_render) {
  124. bool bshow_local = false;
  125. bool bset = true;
  126. param->plocal_render->StartVideoRender();
  127. for (; ; ) {
  128. struct timespec ts;
  129. clock_gettime(CLOCK_REALTIME, &ts);
  130. long unsec = ts.tv_nsec + (1000 * 1000 * ilocal_video_fresh_time);
  131. ts.tv_sec += (unsec / 1000000000);
  132. ts.tv_nsec = (unsec % 1000000000);
  133. if (0 != sem_timedwait(&param->ui_stop_sem, &ts) && (ETIMEDOUT == errno))
  134. {
  135. video_frame* local_video_frame = NULL;
  136. int iwindowstate = get_local_video_frame(local_video_queue, &local_video_frame);
  137. //if (iwindowstate != session->ilast_windstae) {
  138. // //set_video_windows(session, iwindowstate);
  139. // session->ilast_windstae = iwindowstate;
  140. // Dbg("%s:%d, window state is %d.", __FUNCTION__, __LINE__, iwindowstate);
  141. //}
  142. if (NULL != local_video_frame) {
  143. video_frame* localframe = NULL;
  144. if (0 == translate_image_resolution(&localframe, param->location_param.ilocal_view_cx, param->location_param.ilocal_view_cy, local_video_frame)) {
  145. param->plocal_render->RenderVideoFrame(localframe, RVC_FLIP_VERTICAL);
  146. video_frame_delete(localframe);
  147. localframe = NULL;
  148. }
  149. else {
  150. param->plocal_render->RenderVideoFrame(local_video_frame, RVC_FLIP_VERTICAL);
  151. }
  152. bshow_local = true;
  153. video_frame_delete(local_video_frame);
  154. local_video_frame = NULL;
  155. }
  156. else {
  157. Dbg("%s:%d,get video from shm preview queue failed!", __FUNCTION__, __LINE__);
  158. }
  159. }
  160. else {
  161. Dbg("%s:%d videorender_func exit!", __FUNCTION__, __LINE__);
  162. param->plocal_render->HideVideoWindow();
  163. break;
  164. }
  165. if (bset) {
  166. if (bshow_local) {
  167. param->plocal_render->ShowVideoWindow();
  168. param->premote_render->ShowVideoWindow();
  169. bset = false;
  170. }
  171. }
  172. }
  173. param->plocal_render->StopVideoRender();
  174. }
  175. return 0;
  176. }
  177. void* rvc_remote_videorender_func(void* arg)
  178. {
  179. LOG_FUNCTION();
  180. rvc_video_render_t* param = (rvc_video_render_t*)arg;
  181. int iremote_video_fresh_time = param->location_param.iremote_fresh_time;
  182. char strImgPath[MAX_PATH] = { 0 };
  183. int irecord_video_frame_width = REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH;
  184. int irecord_video_frame_heigt = REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_HEIGHT;
  185. Clibvideoqueue* video_shm_q_remote = new Clibvideoqueue(REC_COMMON_VIDEO_REMOTE_SHM_RTP_QUEUE);
  186. _snprintf(strImgPath, MAX_PATH, "%s", "./bin/agent.jpg");
  187. video_frame* remote_frame = video_frame_new(irecord_video_frame_width, irecord_video_frame_heigt, VIDEO_FORMAT_RGB24);
  188. video_frame_fill_black(remote_frame);
  189. if (ExistsFile(strImgPath)){
  190. IplImage* img = cvLoadImage(strImgPath, 1);
  191. videoq_frame* vframe = new videoq_frame;
  192. if (NULL != img) {
  193. Dbg("load img success");
  194. vframe->format = VIDEOQ_FORMAT_RGB24;
  195. vframe->framesize = img->imageSize;
  196. vframe->width = img->width;
  197. vframe->height = img->height;
  198. vframe->data = new unsigned char[img->imageSize];
  199. memcpy(vframe->data, img->imageData, img->imageSize);
  200. cvReleaseImage(&img);
  201. }
  202. SwsContext* sws = sws_getContext(vframe->width, vframe->height, PIX_FMT_BGR24,
  203. irecord_video_frame_width,
  204. irecord_video_frame_heigt,
  205. PIX_FMT_BGR24,
  206. SWS_POINT, NULL, NULL, NULL);
  207. uint8_t* src_data[4] = { (unsigned char*)vframe->data + (vframe->height - 1) * vframe->width * 3, NULL, NULL, NULL };
  208. int src_linesize[4] = { -vframe->width * 3,0,0,0 };
  209. unsigned char* dst[4] = { remote_frame->data[0],NULL,NULL,NULL };
  210. int dst_linesize[4] = { remote_frame->linesize[0],0,0,0 };
  211. sws_scale(sws, src_data, src_linesize, 0, vframe->height, dst, dst_linesize);
  212. sws_freeContext(sws);
  213. if (vframe) {
  214. delete vframe->data;
  215. delete vframe;
  216. vframe = NULL;
  217. }
  218. }
  219. video_frame* showframe = NULL;
  220. if (-1 == translate_image_resolution(&showframe, param->location_param.iremote_view_cx, param->location_param.iremote_view_cy, remote_frame)) {
  221. showframe = video_frame_new(param->location_param.iremote_view_cx, param->location_param.iremote_view_cy, VIDEO_FORMAT_RGB24);
  222. video_frame_fill_black(showframe);
  223. video_frame_copy(showframe, remote_frame);
  224. }
  225. for (; ; ) {
  226. struct timespec ts;
  227. clock_gettime(CLOCK_REALTIME, &ts);
  228. long unsec = ts.tv_nsec + (1000 * 1000 * iremote_video_fresh_time);
  229. ts.tv_sec += (unsec / 1000000000);
  230. ts.tv_nsec = (unsec % 1000000000);
  231. if (0 != sem_timedwait(&param->remote_render_stop_sem, &ts) && (ETIMEDOUT == errno)){
  232. rvc_remote_video_render(param, showframe);
  233. if (NULL != video_shm_q_remote){
  234. int rc = rvc_video_shm_enqueue(video_shm_q_remote, remote_frame, VIDEOQUEUE_FLAG_VERTICAL_FLIP);
  235. if (rc != Error_Succeed){
  236. Dbg("insert agent picture to remote video queue.");
  237. }
  238. }
  239. }
  240. else {
  241. Dbg("%s:%d record remote video render_func exit!", __FUNCTION__, __LINE__);
  242. break;
  243. }
  244. }
  245. video_frame_delete(showframe);
  246. showframe = NULL;
  247. video_frame_delete(remote_frame);
  248. remote_frame = NULL;
  249. return 0;
  250. }
  251. int rvc_start_video_render(rvc_video_render_t* prender, bool bremote, rvc_video_box_move_callback_t* cb)
  252. {
  253. if (0 != sem_init(&prender->ui_stop_sem, 0, 0)) {
  254. Dbg("%s:%d create stop sem failed!", __FUNCTION__, __LINE__);
  255. return Error_Resource;
  256. }
  257. else {
  258. Dbg("%s:%d create stop sem success!", __FUNCTION__, __LINE__);
  259. }
  260. int err = pthread_create(&prender->ui_threadid, NULL, rvc_videorender_func, prender);
  261. if (Error_Succeed == err) {
  262. Dbg("create video render thread success, thread id is %u.", prender->ui_threadid);
  263. }
  264. else {
  265. Dbg("create video render thread failed.");
  266. return Error_Resource;
  267. }
  268. if (bremote) {
  269. if (0 != sem_init(&prender->remote_render_stop_sem, 0, 0)) {
  270. Dbg("%s:%d create remote stop sem failed!", __FUNCTION__, __LINE__);
  271. return Error_Resource;
  272. }
  273. else {
  274. Dbg("%s:%d create remote stop sem success!", __FUNCTION__, __LINE__);
  275. }
  276. err = pthread_create(&prender->remote_render_threadid, NULL, rvc_remote_videorender_func, prender);
  277. if (Error_Succeed == err) {
  278. Dbg("create video render thread success, thread id is %u.", prender->ui_threadid);
  279. }
  280. else {
  281. Dbg("create video render thread failed.");
  282. return Error_Resource;
  283. }
  284. }
  285. return err;
  286. }
  287. int rvc_stop_video_render(rvc_video_render_t* prender)
  288. {
  289. sem_post(&prender->ui_stop_sem);
  290. rvc_stop_remote_video_render(prender);
  291. if (prender->ui_threadid > 0) {
  292. if (0 == pthread_join(prender->ui_threadid, NULL)) {
  293. Dbg("video render thread %u pthread join success.", prender->ui_threadid);
  294. prender->ui_threadid = 0;
  295. }
  296. else {
  297. Dbg("video render thread pthread join error for %s.", strerror(errno));
  298. }
  299. }
  300. Dbg("video render thread exit!");
  301. return Error_Succeed;
  302. }
  303. int rvc_stop_remote_video_render(rvc_video_render_t* prender)
  304. {
  305. if (prender->remote_render_threadid > 0) {
  306. sem_post(&prender->remote_render_stop_sem);
  307. if (0 == pthread_join(prender->remote_render_threadid, NULL)) {
  308. Dbg("remote video render thread %u pthread join success.", prender->remote_render_threadid);
  309. prender->remote_render_threadid = 0;
  310. }
  311. else {
  312. Dbg("remote video render thread pthread join error for %s.", strerror(errno));
  313. }
  314. Dbg("remote video render thread exit!");
  315. }
  316. return Error_Succeed;
  317. }