videoplayer.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. #include "precompile.h"
  2. #include "videoplayer.h"
  3. #define WIN32_LEAN_AND_MEAN
  4. #include <windows.h>
  5. #include <vfw.h>
  6. #include <assert.h>
  7. #define av_always_inline __inline
  8. #define inline __inline
  9. #include <libswscale/swscale.h>
  10. #include "video_common/ffmpeg_api_adapter.h"
  11. #define WM_MSG_INIT (WM_APP+1)
  12. #define WM_MSG_CLEAR (WM_APP+2)
  13. #define WM_MSG_FRAME (WM_APP+3)
  14. #define FRAME_MIN_CACHE_MSEC 125
  15. #define FRAME_MAX_CACHE_MSEC 500
  16. #define FRAME_QUEUE_LEGNTH 50
  17. enum eVideoSizeMode{
  18. eNormalSize,
  19. eDoubleSize,
  20. eZoomOutSize
  21. };
  22. typedef enum eVideoSizeMode eVideoSizeMode;
  23. typedef struct frame_entry
  24. {
  25. video_frame *raw_frame;
  26. videoplayer_free_frame free_frame;
  27. void *free_frame_user_data;
  28. DWORD dwTick;
  29. }frame_entry;
  30. #pragma pack(push,1)
  31. struct winthunk_t
  32. {
  33. DWORD m_mov;
  34. DWORD m_this;
  35. BYTE m_jmp;
  36. DWORD m_relproc;
  37. };
  38. #pragma pack(pop)
  39. struct videoplayer_t
  40. {
  41. struct winthunk_t thunk;
  42. int x;
  43. int y;
  44. int cx;
  45. int cy;
  46. int fps_num;
  47. int fps_den;
  48. int width;
  49. int height;
  50. int flags;
  51. eVideoSizeMode esizemode; // double size mode
  52. struct SwsContext *double_size_sws_context;
  53. LPVOID double_size_data;
  54. struct SwsContext *zoomout_size_sws_context;
  55. LPVOID zoomout_size_data;
  56. frame_entry current_frame;
  57. frame_entry frames[FRAME_QUEUE_LEGNTH];
  58. int q_head;
  59. int q_tail;
  60. int q_caching;
  61. LPVOID black_frame_data;
  62. LPVOID res_frame_data;
  63. struct SwsContext *sws_context;
  64. DWORD dwPlayTimer;
  65. int min_cache_size;
  66. int max_cache_size;
  67. videoplayer_on_pull on_pull;
  68. videoplayer_free_frame free_frame;
  69. void *user_data;
  70. char name[16];
  71. #ifdef _WIN32
  72. HWND hWndVideo;
  73. WNDPROC oldWndProc;
  74. CRITICAL_SECTION q_lock;
  75. HDRAWDIB hdib;
  76. #endif
  77. };
  78. #ifndef SMALL_RECORD_VIDEO_WIDTH
  79. #define SMALL_RECORD_VIDEO_WIDTH 244
  80. #endif
  81. #ifndef SMALL_RECORD_VIDEO_HEIGHT
  82. #define SMALL_RECORD_VIDEO_HEIGHT 138
  83. #endif
  84. // use thunk tech
  85. #define QUEUE_SIZE(head, tail, length) (((tail)+(length)-(head))%(length))
  86. #define QUEUE_INC(x, length) (((x) + 1) % (length))
  87. int nWindowState = 0;
  88. int bMove = 0;
  89. static RECT g_Rect;
  90. int IsHideRemoteWin = 0;
  91. int IsHideLocalWin = 0;
  92. static void Dbg(const char *fmt, ...)
  93. {
  94. int n;
  95. va_list arg;
  96. va_start(arg, fmt);
  97. n = _vscprintf(fmt, arg);
  98. if (n > 0) {
  99. char *buf = (char*)_alloca(n+3);
  100. vsprintf(buf, fmt, arg);
  101. strcat(buf, "\r\n");
  102. OutputDebugStringA(buf);
  103. }
  104. va_end(arg);
  105. }
  106. static __inline int InRectangle(int xPos, int yPos, int x0, int y0, int cx, int cy)
  107. {
  108. return xPos >= x0 && xPos <= x0+cx && yPos >= y0 && yPos <= y0+cy;
  109. }
  110. static __inline void free_frame_entry(videoplayer_t *player, struct frame_entry *entry)
  111. {
  112. if (entry && entry->raw_frame) {
  113. (*entry->free_frame)(player, entry->free_frame_user_data, entry->raw_frame);
  114. }
  115. }
  116. static int init_thunk(struct winthunk_t *thunk, DWORD_PTR proc, videoplayer_t *player)
  117. {
  118. thunk->m_mov = 0x042444C7;
  119. thunk->m_this = (DWORD)player;
  120. thunk->m_jmp = 0xe9;
  121. thunk->m_relproc = (DWORD)((INT_PTR)proc - ((INT_PTR)player+sizeof(struct winthunk_t)));
  122. FlushInstructionCache(GetCurrentProcess(), thunk, sizeof(struct winthunk_t));
  123. return 0;
  124. }
  125. static void display_frame_entry(videoplayer_t *player, video_frame *frame);
  126. static BOOL is_parent_window_of(HWND x, HWND y)
  127. {
  128. HWND t = GetParent(y);
  129. while (t) {
  130. if (t == x)
  131. return TRUE;
  132. t = GetParent(t);
  133. }
  134. return FALSE;
  135. }
  136. static void check_on_top(videoplayer_t *player)
  137. {
  138. RECT rc;
  139. if (GetWindowRect(player->hWndVideo, &rc)) {
  140. HWND hWnd;
  141. POINT t;
  142. t.x = (rc.left + rc.right) >> 1;
  143. t.y = (rc.top + rc.bottom) >> 1;
  144. hWnd = WindowFromPoint(t);
  145. if (hWnd && hWnd != player->hWndVideo) {
  146. //if (!is_parent_window_of(hWnd, player->hWndVideo) && !is_parent_window_of(player->hWndVideo, hWnd)) {
  147. BringWindowToTop(player->hWndVideo);
  148. //MessageBeep(MB_OK);
  149. //}
  150. }
  151. }
  152. }
  153. static void OnMsgFrame(videoplayer_t *player, HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  154. {
  155. frame_entry *entry = (frame_entry*)wParam;
  156. display_frame_entry(player, entry->raw_frame);
  157. free_frame_entry(player, &player->current_frame);
  158. player->current_frame.raw_frame = entry->raw_frame;
  159. player->current_frame.free_frame = entry->free_frame;
  160. player->current_frame.free_frame_user_data = entry->free_frame_user_data;
  161. player->current_frame.dwTick = entry->dwTick;
  162. entry->raw_frame = NULL;
  163. free(entry);
  164. }
  165. static void display_frame_entry(videoplayer_t *player, video_frame *frame)
  166. {
  167. if (NULL == player || NULL == frame){
  168. return;
  169. }
  170. if (player->flags & VIDEOPLAYER_FLAG_CHECKTOP) {
  171. check_on_top(player);
  172. }
  173. if (player->hdib) {
  174. HDC hdc;
  175. hdc = GetDC(player->hWndVideo);
  176. if (hdc) {
  177. if (eDoubleSize == player->esizemode) {
  178. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  179. unsigned char *src_data[4] = {frame->data[0], NULL, NULL, NULL};
  180. int src_linesize[4] = {player->width*3, 0, 0, 0};
  181. unsigned char *dst_data[4] = {player->double_size_data, NULL, NULL, NULL};
  182. int dst_linesize[4] = {player->cx*2*3, 0, 0, 0};
  183. bmpheader.biPlanes = 1;
  184. bmpheader.biBitCount = 24;//pr->bpp;
  185. bmpheader.biCompression = BI_RGB;
  186. bmpheader.biHeight = player->cy<<1;
  187. bmpheader.biWidth = player->cx<<1;
  188. sws_scale(player->double_size_sws_context, src_data, src_linesize,
  189. 0, player->height, dst_data, dst_linesize);
  190. DrawDibDraw(player->hdib, hdc, 0, 0, -1, -1, &bmpheader, player->double_size_data, 0, 0, player->cx<<1, player->cy<<1, DDF_SAME_DRAW|DDF_SAME_HDC);
  191. }
  192. else if(eZoomOutSize == player->esizemode){
  193. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  194. unsigned char *src_data[4] = {frame->data[0], NULL, NULL, NULL};
  195. int src_linesize[4] = {player->width*3, 0, 0, 0};
  196. unsigned char *dst_data[4] = {player->zoomout_size_data, NULL, NULL, NULL};
  197. int dst_linesize[4] = {SMALL_RECORD_VIDEO_WIDTH*3, 0, 0, 0};
  198. //{
  199. // static int icount = 0;
  200. // if (icount == 0)
  201. // {
  202. // video_frame frame;
  203. // video_frame_alloc(320, 240, VIDEO_FORMAT_RGB24, &frame);
  204. // video_frame_fill_black(&frame);
  205. // frame.data[0] = src_data[0];
  206. // frame.linesize[0] = src_linesize[0];
  207. // video_frame_save_bmpfile("d:\\src.bmp", &frame);
  208. // icount++;
  209. // }
  210. //}
  211. bmpheader.biPlanes = 1;
  212. bmpheader.biBitCount = 24;//pr->bpp;
  213. bmpheader.biCompression = BI_RGB;
  214. bmpheader.biHeight = SMALL_RECORD_VIDEO_HEIGHT;
  215. bmpheader.biWidth = SMALL_RECORD_VIDEO_WIDTH;
  216. sws_scale(player->zoomout_size_sws_context, src_data, src_linesize,
  217. 0, player->height, dst_data, dst_linesize);
  218. //{
  219. // static int icount = 0;
  220. // if (icount == 0)
  221. // {
  222. // video_frame frame;
  223. // video_frame_alloc(SMALL_RECORD_VIDEO_WIDTH, SMALL_RECORD_VIDEO_HEIGHT, VIDEO_FORMAT_RGB24, &frame);
  224. // video_frame_fill_black(&frame);
  225. // frame.data[0] = /*dst_data[0];*/ (unsigned char*)player->zoomout_size_data;
  226. // frame.linesize[0] = dst_linesize[0];
  227. // video_frame_save_bmpfile("d:\\dest.bmp", &frame);
  228. // icount++;
  229. // }
  230. //}
  231. DrawDibDraw(player->hdib, hdc, 0, 0, -1, -1, &bmpheader, player->zoomout_size_data, 0, 0, SMALL_RECORD_VIDEO_WIDTH, SMALL_RECORD_VIDEO_HEIGHT, DDF_SAME_DRAW|DDF_SAME_HDC);
  232. }
  233. else{
  234. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  235. bmpheader.biPlanes = 1;
  236. bmpheader.biBitCount = 24;//pr->bpp;
  237. bmpheader.biCompression = BI_RGB;
  238. bmpheader.biHeight = player->cy;
  239. bmpheader.biWidth = player->cx;
  240. if (player->sws_context) {
  241. unsigned char *src_data[4] = {frame->data[0], NULL, NULL, NULL};
  242. int src_linesize[4] = {player->width*3, 0, 0, 0};
  243. unsigned char *dst_data[4] = {player->res_frame_data, NULL, NULL, NULL};
  244. int dst_linesize[4] = {player->cx*3, 0, 0, 0};
  245. sws_scale(player->sws_context, src_data, src_linesize,
  246. 0, player->height, dst_data, dst_linesize);
  247. DrawDibDraw(player->hdib, hdc, 0, 0, -1, -1, &bmpheader, player->res_frame_data, 0, 0, player->cx, player->cy, DDF_SAME_DRAW|DDF_SAME_HDC);
  248. } else {
  249. DrawDibDraw(player->hdib, hdc, 0, 0, -1, -1, &bmpheader, frame->data[0], 0, 0, player->cx, player->cy, DDF_SAME_DRAW|DDF_SAME_HDC);
  250. }
  251. }
  252. ReleaseDC(player->hWndVideo, hdc);
  253. }
  254. }
  255. }
  256. static void display_black_frame(videoplayer_t *player)
  257. {
  258. if (player->flags & VIDEOPLAYER_FLAG_CHECKTOP) {
  259. check_on_top(player);
  260. }
  261. if (player->hdib) {
  262. HDC hdc;
  263. hdc = GetDC(player->hWndVideo);
  264. if (hdc) {
  265. if (eDoubleSize == player->esizemode) {
  266. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  267. unsigned char *src_data[4] = {player->black_frame_data, NULL, NULL, NULL};
  268. int src_linesize[4] = {player->cx*3, 0, 0, 0};
  269. unsigned char *dst_data[4] = {player->double_size_data, NULL, NULL, NULL};
  270. int dst_linesize[4] = {player->cx*2*3, 0, 0, 0};
  271. sws_scale(player->double_size_sws_context, src_data, src_linesize,
  272. 0, player->height, dst_data, dst_linesize);
  273. bmpheader.biPlanes = 1;
  274. bmpheader.biBitCount = 24;//pr->bpp;
  275. bmpheader.biCompression = BI_RGB;
  276. bmpheader.biHeight = player->cy<<1;
  277. bmpheader.biWidth = player->cx<<1;
  278. DrawDibDraw(player->hdib, hdc, 0, 0, -1, -1, &bmpheader, player->double_size_data, 0, 0, player->cx<<1, player->cy<<1, DDF_SAME_DRAW|DDF_SAME_HDC);
  279. }
  280. else if(eZoomOutSize == player->esizemode){
  281. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  282. unsigned char *src_data[4] = {player->black_frame_data, NULL, NULL, NULL};
  283. int src_linesize[4] = {player->cx*3, 0, 0, 0};
  284. unsigned char *dst_data[4] = {player->zoomout_size_data, NULL, NULL, NULL};
  285. int dst_linesize[4] = {SMALL_RECORD_VIDEO_WIDTH*3, 0, 0, 0};
  286. sws_scale(player->zoomout_size_sws_context, src_data, src_linesize,
  287. 0, player->height, dst_data, dst_linesize);
  288. bmpheader.biPlanes = 1;
  289. bmpheader.biBitCount = 24;//pr->bpp;
  290. bmpheader.biCompression = BI_RGB;
  291. bmpheader.biHeight = SMALL_RECORD_VIDEO_HEIGHT;
  292. bmpheader.biWidth = SMALL_RECORD_VIDEO_WIDTH;
  293. DrawDibDraw(player->hdib, hdc, 0, 0, -1, -1, &bmpheader, player->zoomout_size_data, 0, 0, SMALL_RECORD_VIDEO_WIDTH, SMALL_RECORD_VIDEO_HEIGHT, DDF_SAME_DRAW|DDF_SAME_HDC);
  294. }
  295. else{
  296. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  297. bmpheader.biPlanes = 1;
  298. bmpheader.biBitCount = 24;//pr->bpp;
  299. bmpheader.biCompression = BI_RGB;
  300. bmpheader.biHeight = player->cy;
  301. bmpheader.biWidth = player->cx;
  302. DrawDibDraw(player->hdib, hdc, 0, 0, -1, -1, &bmpheader, player->black_frame_data, 0, 0, player->cx, player->cy, DDF_SAME_DRAW|DDF_SAME_HDC);
  303. }
  304. ReleaseDC(player->hWndVideo, hdc);
  305. }
  306. }
  307. }
  308. static void OnMsgTimer_PushMode(videoplayer_t *player, HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  309. {
  310. DWORD dwNow = GetTickCount();
  311. EnterCriticalSection(&player->q_lock);
  312. //获取窗口显示状态
  313. nWindowState = player->on_pull(player, player->user_data,NULL);
  314. if (player->q_caching) {
  315. OutputDebugStringA("caching\n");
  316. while (player->q_tail != player->q_head) {
  317. frame_entry *entry = &player->frames[player->q_head];
  318. if (dwNow - entry->dwTick > 3000) {
  319. player->q_head = QUEUE_INC(player->q_head, FRAME_QUEUE_LEGNTH);
  320. free_frame_entry(player, entry);
  321. } else {
  322. break;
  323. }
  324. }
  325. } else {
  326. bool bdisplayed = false;
  327. //if (player->q_head == player->q_tail)
  328. while (player->q_tail != player->q_head) {
  329. frame_entry *entry = &player->frames[player->q_head];
  330. player->q_head = QUEUE_INC(player->q_head, FRAME_QUEUE_LEGNTH);
  331. if (dwNow - entry->dwTick > 3000) {
  332. //Dbg("clear frame");
  333. free_frame_entry(player, entry);
  334. } else {
  335. //Dbg("display frame");
  336. display_frame_entry(player, entry->raw_frame);
  337. bdisplayed = true;
  338. free_frame_entry(player, &player->current_frame);
  339. player->current_frame.raw_frame = entry->raw_frame;
  340. player->current_frame.free_frame = entry->free_frame;
  341. player->current_frame.free_frame_user_data = entry->free_frame_user_data;
  342. player->current_frame.dwTick = entry->dwTick;
  343. entry->raw_frame = NULL;
  344. break;
  345. }
  346. }
  347. if (!bdisplayed) {
  348. player->q_caching = TRUE;
  349. //Dbg("recaching");
  350. }
  351. }
  352. LeaveCriticalSection(&player->q_lock);
  353. }
  354. static void OnMsgTimer_PullMode(videoplayer_t *player, HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  355. {
  356. video_frame *frame = NULL;
  357. nWindowState = player->on_pull(player, player->user_data, &frame);
  358. display_frame_entry(player, frame);
  359. player->free_frame(player, player->user_data, frame);
  360. }
  361. static void OnMsgInit(videoplayer_t *player, HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  362. {
  363. HDC hdc;
  364. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  365. bmpheader.biPlanes = 1;
  366. bmpheader.biBitCount = 24;
  367. bmpheader.biCompression = BI_RGB;
  368. bmpheader.biHeight = player->cy;
  369. bmpheader.biWidth = player->cx;
  370. player->hdib = DrawDibOpen();
  371. if (!player->hdib)
  372. return;
  373. hdc = GetDC(hWnd);
  374. if (!DrawDibBegin(player->hdib, hdc, player->cx, player->cy, &bmpheader,
  375. player->cx, player->cy, DDF_SAME_DRAW|DDF_SAME_HDC)) {
  376. DrawDibClose(player->hdib);
  377. player->hdib = NULL;
  378. ReleaseDC(hWnd, hdc);
  379. return;
  380. }
  381. ReleaseDC(hWnd, hdc);
  382. player->q_head = player->q_tail = 0;
  383. player->q_caching = 1;
  384. player->black_frame_data = malloc(player->cx*player->cy*3);
  385. memset(player->black_frame_data, 0, player->cx*player->cy*3);
  386. if (player->cx != player->width || player->cy != player->height) {
  387. player->res_frame_data = malloc(player->cx*player->cy*3);
  388. player->sws_context = sws_getCachedContext(
  389. NULL,
  390. player->width,
  391. player->height,
  392. PIX_FMT_BGR24,
  393. player->cx,
  394. player->cy,
  395. PIX_FMT_BGR24,
  396. SWS_LANCZOS,
  397. NULL,
  398. NULL,
  399. NULL);
  400. }
  401. if (player->flags & VIDEOPLAYER_FLAG_DOUBLESIZE) {
  402. player->double_size_data = malloc(player->cx * player->cy * 4 * 3);
  403. player->double_size_sws_context = sws_getCachedContext(
  404. NULL,
  405. player->width,
  406. player->height,
  407. PIX_FMT_BGR24,
  408. 2 * player->cx,
  409. 2 * player->cy,
  410. PIX_FMT_BGR24,
  411. SWS_LANCZOS,
  412. NULL,
  413. NULL,
  414. NULL);
  415. }
  416. if (player->flags & VIDEOPLAYER_FLAG_ZOOMOUTSIZE) {
  417. player->zoomout_size_data = malloc(SMALL_RECORD_VIDEO_WIDTH*SMALL_RECORD_VIDEO_HEIGHT*3);
  418. player->zoomout_size_sws_context = sws_getCachedContext(
  419. NULL,
  420. player->width,
  421. player->height,
  422. PIX_FMT_BGR24,
  423. SMALL_RECORD_VIDEO_WIDTH,
  424. SMALL_RECORD_VIDEO_HEIGHT,
  425. PIX_FMT_BGR24,
  426. SWS_LANCZOS,
  427. NULL,
  428. NULL,
  429. NULL);
  430. }
  431. if (!(player->flags & VIDEOPLAYER_FLAG_NOTIMER)) {
  432. player->dwPlayTimer = SetTimer(hWnd, 3, 1000*player->fps_den/player->fps_num, NULL);
  433. }
  434. }
  435. static void OnMsgClear(videoplayer_t *player, HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  436. {
  437. if (player->dwPlayTimer != 0) {
  438. KillTimer(hWnd, player->dwPlayTimer);
  439. }
  440. display_black_frame(player);
  441. free(player->black_frame_data);
  442. player->black_frame_data = NULL;
  443. while (QUEUE_SIZE(player->q_head, player->q_tail, FRAME_QUEUE_LEGNTH)) {
  444. frame_entry *entry = &player->frames[player->q_head];
  445. player->q_head = QUEUE_INC(player->q_head, FRAME_QUEUE_LEGNTH);
  446. free_frame_entry(player, entry);
  447. }
  448. assert(player->q_head == player->q_tail);
  449. if (player->sws_context) {
  450. free(player->res_frame_data);
  451. player->res_frame_data = NULL;
  452. sws_freeContext(player->sws_context);
  453. player->sws_context = NULL;
  454. }
  455. if (player->double_size_sws_context) {
  456. free(player->double_size_data);
  457. player->double_size_data = NULL;
  458. sws_freeContext(player->double_size_sws_context);
  459. player->double_size_sws_context = NULL;
  460. }
  461. if (player->zoomout_size_sws_context) {
  462. free(player->zoomout_size_data);
  463. player->zoomout_size_data = NULL;
  464. sws_freeContext(player->zoomout_size_sws_context);
  465. player->zoomout_size_sws_context = NULL;
  466. }
  467. free_frame_entry(player, &player->current_frame);
  468. player->q_caching = 0;
  469. }
  470. static LRESULT VideoWndProc(videoplayer_t *player, HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  471. {
  472. {
  473. char tmp[128];
  474. sprintf(tmp, "%s, msg=%d, wparam=%d,lparam=%d\n", player->name, msg, wParam, lParam);
  475. OutputDebugStringA(tmp);
  476. }
  477. switch (msg) {
  478. case WM_MSG_FRAME:
  479. OnMsgFrame(player, hWnd, msg, wParam, lParam);
  480. break;
  481. case WM_MSG_INIT:
  482. OnMsgInit(player, hWnd, msg, wParam, lParam);
  483. break;
  484. case WM_MSG_CLEAR:
  485. OnMsgClear(player, hWnd, msg, wParam, lParam);
  486. break;
  487. case WM_TIMER:
  488. //是否隐藏本地视频窗口
  489. if (player->flags & VIDEOPLAYER_FLAG_PULL)
  490. {
  491. if ((nWindowState == 2)||(nWindowState == 4))
  492. {
  493. if (IsHideLocalWin==0)
  494. {
  495. ShowWindow(hWnd,SW_HIDE);
  496. IsHideLocalWin = 1;
  497. }
  498. }
  499. else if((nWindowState == 0)||(nWindowState == 1)||(nWindowState == 3))
  500. {
  501. if (IsHideLocalWin==1)
  502. {
  503. ShowWindow(hWnd,SW_SHOW);
  504. IsHideLocalWin = 0;
  505. }
  506. }
  507. }
  508. //是否隐藏远端视频窗口
  509. if (player->flags & VIDEOPLAYER_FLAG_PUSH)
  510. {
  511. if (nWindowState == 2)
  512. {
  513. if (IsHideRemoteWin==0)
  514. {
  515. ShowWindow(hWnd,SW_HIDE);
  516. IsHideRemoteWin = 1;
  517. }
  518. }
  519. else if((nWindowState == 0)||(nWindowState == 1)||(nWindowState == 3)||(nWindowState == 4))
  520. {
  521. if (IsHideRemoteWin==1)
  522. {
  523. ShowWindow(hWnd,SW_SHOW);
  524. IsHideRemoteWin = 0;
  525. }
  526. }
  527. }
  528. //是否将本地窗口放大显示
  529. if (((nWindowState==1 && player->cx != 960)&&(bMove==0))&&(player->flags & VIDEOPLAYER_FLAG_PULL)&&(player->flags & VIDEOPLAYER_FLAG_DOUBLESIZE)&&(IsHideLocalWin==0))
  530. {
  531. GetWindowRect(hWnd, &g_Rect);
  532. MoveWindow(hWnd, g_Rect.left, g_Rect.top, player->cx<<1, player->cy<<1, TRUE);
  533. player->esizemode = eDoubleSize;
  534. if (player->flags & VIDEOPLAYER_FLAG_DOUBLESIZE)
  535. {
  536. int xPos = LOWORD(lParam);
  537. int yPos = HIWORD(lParam);
  538. HDC hdc = GetDC(hWnd);
  539. if (hdc) {
  540. if (player->hdib) {
  541. if (eDoubleSize == player->esizemode)
  542. {
  543. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  544. bmpheader.biPlanes = 1;
  545. bmpheader.biBitCount = 24;
  546. bmpheader.biCompression = BI_RGB;
  547. bmpheader.biHeight = player->cy<<1;
  548. bmpheader.biWidth = player->cx<<1;
  549. DrawDibBegin(player->hdib, hdc, player->cx<<1, player->cy<<1, &bmpheader, player->cx<<1, player->cy<<1, DDF_SAME_DRAW|DDF_SAME_HDC);
  550. }
  551. else if(eNormalSize == player->esizemode)
  552. {
  553. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  554. bmpheader.biPlanes = 1;
  555. bmpheader.biBitCount = 24;
  556. bmpheader.biCompression = BI_RGB;
  557. bmpheader.biHeight = player->cy;
  558. bmpheader.biWidth = player->cx;
  559. DrawDibBegin(player->hdib, hdc, player->cx, player->cy, &bmpheader, player->cx, player->cy, DDF_SAME_DRAW|DDF_SAME_HDC);
  560. }
  561. }
  562. ReleaseDC(hWnd, hdc);
  563. }
  564. }
  565. bMove = 1;
  566. }
  567. else if ((nWindowState==0)&&bMove&&(player->flags & VIDEOPLAYER_FLAG_PULL)&&(player->flags & VIDEOPLAYER_FLAG_DOUBLESIZE || player->flags & VIDEOPLAYER_FLAG_ZOOMOUTSIZE)&&(IsHideLocalWin==0))
  568. {
  569. MoveWindow(hWnd, g_Rect.left, g_Rect.top, player->cx, player->cy, TRUE);
  570. player->esizemode = eNormalSize;
  571. bMove = 0;
  572. if (player->flags & VIDEOPLAYER_FLAG_DOUBLESIZE||player->flags & VIDEOPLAYER_FLAG_ZOOMOUTSIZE)
  573. {
  574. int xPos = LOWORD(lParam);
  575. int yPos = HIWORD(lParam);
  576. HDC hdc = GetDC(hWnd);
  577. if (hdc) {
  578. if (player->hdib) {
  579. if (eDoubleSize == player->esizemode)
  580. {
  581. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  582. bmpheader.biPlanes = 1;
  583. bmpheader.biBitCount = 24;
  584. bmpheader.biCompression = BI_RGB;
  585. bmpheader.biHeight = player->cy<<1;
  586. bmpheader.biWidth = player->cx<<1;
  587. DrawDibBegin(player->hdib, hdc, player->cx<<1, player->cy<<1, &bmpheader, player->cx<<1, player->cy<<1, DDF_SAME_DRAW|DDF_SAME_HDC);
  588. }
  589. else if(eNormalSize == player->esizemode)
  590. {
  591. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  592. bmpheader.biPlanes = 1;
  593. bmpheader.biBitCount = 24;
  594. bmpheader.biCompression = BI_RGB;
  595. bmpheader.biHeight = player->cy;
  596. bmpheader.biWidth = player->cx;
  597. DrawDibBegin(player->hdib, hdc, player->cx, player->cy, &bmpheader, player->cx, player->cy, DDF_SAME_DRAW|DDF_SAME_HDC);
  598. }
  599. }
  600. ReleaseDC(hWnd, hdc);
  601. }
  602. }
  603. }
  604. if ((nWindowState==5)&&(player->flags&VIDEOPLAYER_FLAG_ZOOMOUTSIZE))
  605. {
  606. //Dbg("zoom out size video echo.");
  607. GetWindowRect(hWnd, &g_Rect);
  608. MoveWindow(hWnd, g_Rect.left, g_Rect.top, SMALL_RECORD_VIDEO_WIDTH, SMALL_RECORD_VIDEO_HEIGHT, TRUE);
  609. player->esizemode = eZoomOutSize;
  610. if (player->flags&VIDEOPLAYER_FLAG_ZOOMOUTSIZE)
  611. {
  612. HDC hdc = GetDC(hWnd);
  613. if (hdc){
  614. if (player->hdib) {
  615. if (eZoomOutSize == player->esizemode)
  616. {
  617. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  618. bmpheader.biPlanes = 1;
  619. bmpheader.biBitCount = 24;
  620. bmpheader.biCompression = BI_RGB;
  621. bmpheader.biHeight = SMALL_RECORD_VIDEO_HEIGHT;
  622. bmpheader.biWidth = SMALL_RECORD_VIDEO_WIDTH;
  623. DrawDibBegin(player->hdib, hdc, SMALL_RECORD_VIDEO_WIDTH, SMALL_RECORD_VIDEO_HEIGHT, &bmpheader, SMALL_RECORD_VIDEO_WIDTH, SMALL_RECORD_VIDEO_HEIGHT, DDF_SAME_DRAW|DDF_SAME_HDC);
  624. }
  625. else if(eNormalSize == player->esizemode)
  626. {
  627. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  628. bmpheader.biPlanes = 1;
  629. bmpheader.biBitCount = 24;
  630. bmpheader.biCompression = BI_RGB;
  631. bmpheader.biHeight = player->cy;
  632. bmpheader.biWidth = player->cx;
  633. DrawDibBegin(player->hdib, hdc, player->cx, player->cy, &bmpheader, player->cx, player->cy, DDF_SAME_DRAW|DDF_SAME_HDC);
  634. }
  635. }
  636. ReleaseDC(hWnd, hdc);
  637. }
  638. }
  639. bMove = 1;
  640. }
  641. if (wParam == player->dwPlayTimer)
  642. {
  643. if (player->flags & VIDEOPLAYER_FLAG_PUSH)
  644. {
  645. OnMsgTimer_PushMode(player, hWnd, msg, wParam, lParam);
  646. }
  647. else if (player->flags & VIDEOPLAYER_FLAG_PULL)
  648. {
  649. OnMsgTimer_PullMode(player, hWnd, msg, wParam, lParam);
  650. }
  651. }
  652. break;
  653. case WM_LBUTTONDOWN:
  654. if((player->flags & VIDEOPLAYER_FLAG_DOUBLESIZE)&&!(player->flags & VIDEOPLAYER_FLAG_PULL))
  655. {
  656. eVideoSizeMode old = player->esizemode;
  657. if (eNormalSize == player->esizemode)
  658. {
  659. player->esizemode = eDoubleSize;
  660. }
  661. if (old != player->esizemode)
  662. {
  663. RECT rc;
  664. GetWindowRect(hWnd, &rc);
  665. if (eDoubleSize == player->esizemode)
  666. {
  667. MoveWindow(hWnd, rc.left, rc.top, player->cx<<1, player->cy<<1, TRUE);
  668. }
  669. else
  670. {
  671. MoveWindow(hWnd, rc.left, rc.top, player->cx, player->cy, TRUE);
  672. }
  673. }
  674. }
  675. else
  676. {
  677. return CallWindowProcA(player->oldWndProc, hWnd, msg, wParam, lParam);
  678. }
  679. break;
  680. case WM_LBUTTONUP:
  681. if ((player->flags & VIDEOPLAYER_FLAG_DOUBLESIZE)&&!(player->flags & VIDEOPLAYER_FLAG_PULL))
  682. {
  683. eVideoSizeMode old = player->esizemode;
  684. if (eDoubleSize == player->esizemode) {
  685. player->esizemode = eNormalSize;
  686. }
  687. if (old != player->esizemode) {
  688. RECT rc;
  689. GetWindowRect(hWnd, &rc);
  690. if (eDoubleSize == player->esizemode)
  691. {
  692. MoveWindow(hWnd, rc.left, rc.top, player->cx<<1, player->cy<<1, TRUE);
  693. }
  694. else
  695. {
  696. MoveWindow(hWnd, rc.left, rc.top, player->cx, player->cy, TRUE);
  697. }
  698. }
  699. } else {
  700. return CallWindowProcA(player->oldWndProc, hWnd, msg, wParam, lParam);
  701. }
  702. break;
  703. case WM_MOUSEMOVE:
  704. if ((player->flags & VIDEOPLAYER_FLAG_DOUBLESIZE)&&!(player->flags & VIDEOPLAYER_FLAG_PULL))
  705. {
  706. eVideoSizeMode old = player->esizemode;
  707. if (eDoubleSize == player->esizemode) {
  708. POINT pt = {LOWORD(lParam), HIWORD(lParam)};
  709. RECT rc;
  710. GetWindowRect(hWnd, &rc);
  711. ClientToScreen(hWnd, &pt);
  712. if (!InRectangle(pt.x, pt.y, rc.left, rc.top, player->cx, player->cy)) { // once out of rectangle, taken as LBUTTONUP
  713. player->esizemode = eNormalSize;
  714. }
  715. }
  716. if (old != player->esizemode) {
  717. RECT rc;
  718. GetWindowRect(hWnd, &rc);
  719. if (eDoubleSize == player->esizemode) {
  720. MoveWindow(hWnd, rc.left, rc.top, player->cx<<1, player->cy<<1, TRUE);
  721. } else {
  722. MoveWindow(hWnd, rc.left, rc.top, player->cx, player->cy, TRUE);
  723. }
  724. }
  725. } else {
  726. return CallWindowProcA(player->oldWndProc, hWnd, msg, wParam, lParam);
  727. }
  728. break;
  729. case WM_SIZE:
  730. if (player->flags & VIDEOPLAYER_FLAG_DOUBLESIZE || player->flags & VIDEOPLAYER_FLAG_ZOOMOUTSIZE)
  731. {
  732. int xPos = LOWORD(lParam);
  733. int yPos = HIWORD(lParam);
  734. HDC hdc = GetDC(hWnd);
  735. if (hdc) {
  736. if (player->hdib) {
  737. if (eDoubleSize == player->esizemode) {
  738. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  739. bmpheader.biPlanes = 1;
  740. bmpheader.biBitCount = 24;
  741. bmpheader.biCompression = BI_RGB;
  742. bmpheader.biHeight = player->cy<<1;
  743. bmpheader.biWidth = player->cx<<1;
  744. DrawDibBegin(player->hdib, hdc, player->cx<<1, player->cy<<1, &bmpheader, player->cx<<1, player->cy<<1, DDF_SAME_DRAW|DDF_SAME_HDC);
  745. }
  746. else if(eZoomOutSize == player->esizemode){
  747. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  748. bmpheader.biPlanes = 1;
  749. bmpheader.biBitCount = 24;
  750. bmpheader.biCompression = BI_RGB;
  751. bmpheader.biHeight = SMALL_RECORD_VIDEO_HEIGHT;
  752. bmpheader.biWidth = SMALL_RECORD_VIDEO_WIDTH;
  753. DrawDibBegin(player->hdib, hdc, SMALL_RECORD_VIDEO_WIDTH, SMALL_RECORD_VIDEO_HEIGHT, &bmpheader, SMALL_RECORD_VIDEO_WIDTH, SMALL_RECORD_VIDEO_HEIGHT, DDF_SAME_DRAW|DDF_SAME_HDC);
  754. }
  755. else{
  756. BITMAPINFOHEADER bmpheader = {sizeof(BITMAPINFOHEADER)};
  757. bmpheader.biPlanes = 1;
  758. bmpheader.biBitCount = 24;
  759. bmpheader.biCompression = BI_RGB;
  760. bmpheader.biHeight = player->cy;
  761. bmpheader.biWidth = player->cx;
  762. DrawDibBegin(player->hdib, hdc, player->cx, player->cy, &bmpheader, player->cx, player->cy, DDF_SAME_DRAW|DDF_SAME_HDC);
  763. }
  764. }
  765. ReleaseDC(hWnd, hdc);
  766. }
  767. } else {
  768. return CallWindowProcA(player->oldWndProc, hWnd, msg, wParam, lParam);
  769. }
  770. break;
  771. case WM_PAINT:
  772. if (player->flags & VIDEOPLAYER_FLAG_DOUBLESIZE || player->flags & VIDEOPLAYER_FLAG_ZOOMOUTSIZE)
  773. {
  774. if (player->current_frame.raw_frame) {
  775. display_frame_entry(player, player->current_frame.raw_frame);
  776. ValidateRect(hWnd, NULL);
  777. return 0;
  778. }
  779. }
  780. else
  781. {
  782. ValidateRect(hWnd, NULL);
  783. return 0;
  784. }
  785. return CallWindowProcA(player->oldWndProc, hWnd, msg, wParam, lParam);
  786. default:
  787. return CallWindowProcA(player->oldWndProc, hWnd, msg, wParam, lParam);
  788. }
  789. return 0;
  790. }
  791. static LRESULT CALLBACK __VideoWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  792. {
  793. videoplayer_t *player = (videoplayer_t*)hWnd;
  794. return VideoWndProc(player, player->hWndVideo, msg, wParam, lParam);
  795. }
  796. static int hook_window(videoplayer_t *player)
  797. {
  798. init_thunk(&player->thunk, (DWORD_PTR)&__VideoWndProc, player);
  799. player->oldWndProc = SetWindowLongA(player->hWndVideo, GWL_WNDPROC, (LONG)&player->thunk);
  800. SendMessageA(player->hWndVideo, WM_MSG_INIT, 0, 0);
  801. return 0;
  802. }
  803. static void unhook_window(videoplayer_t *player)
  804. {
  805. SendMessageA(player->hWndVideo, WM_MSG_CLEAR, 0, 0);
  806. SetWindowLongA(player->hWndVideo, GWL_WNDPROC, (LONG)player->oldWndProc);
  807. }
  808. int videoplayer_create(HWND hWndVideo,
  809. int x, int y, int cx, int cy,
  810. int fps_num,int fps_den,
  811. int width, int height, int flags, const char *name,
  812. videoplayer_on_pull on_pull,
  813. videoplayer_free_frame free_frame,
  814. void *user_data,
  815. videoplayer_t **p_player)
  816. {
  817. videoplayer_t *player = (videoplayer_t*)VirtualAlloc(NULL, sizeof(videoplayer_t), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  818. if (player) {
  819. memset(player, 0, sizeof(videoplayer_t));
  820. assert(fps_den != 0);
  821. assert(fps_num < FRAME_QUEUE_LEGNTH * fps_den);
  822. player->hWndVideo = hWndVideo;
  823. player->x = x;
  824. player->y = y;
  825. player->cx = cx;
  826. player->cy = cy;
  827. player->fps_den = fps_den;
  828. player->fps_num = fps_num;
  829. player->width = width;
  830. player->height = height;
  831. player->min_cache_size = FRAME_MIN_CACHE_MSEC * fps_num / 1000 * fps_den;
  832. player->max_cache_size = FRAME_MAX_CACHE_MSEC * fps_num / 1000 * fps_den;
  833. player->res_frame_data = player->black_frame_data = NULL;
  834. player->sws_context = NULL;
  835. player->esizemode = eNormalSize;
  836. player->double_size_sws_context = NULL;
  837. player->double_size_data = NULL;
  838. player->zoomout_size_sws_context = NULL;
  839. player->zoomout_size_data = NULL;
  840. player->current_frame.raw_frame = NULL;
  841. player->flags = flags;
  842. player->hdib = NULL;
  843. player->on_pull = on_pull;
  844. player->free_frame = free_frame;
  845. player->user_data = user_data;
  846. strncpy(player->name, name, sizeof(player->name)-1);
  847. player->name[sizeof(player->name)-1] = 0;
  848. InitializeCriticalSection(&player->q_lock);
  849. if (hook_window(player) != 0) {
  850. VirtualFree(player, sizeof(videoplayer_t), MEM_RELEASE);
  851. return -1;
  852. }
  853. *p_player = player;
  854. } else {
  855. return -1;
  856. }
  857. //初始化窗口状态
  858. nWindowState = 0;
  859. bMove = 0;
  860. IsHideRemoteWin = 0;
  861. IsHideLocalWin = 0;
  862. return 0;
  863. }
  864. void videoplayer_destroy(videoplayer_t *player)
  865. {
  866. MSG msg;
  867. while (PeekMessageA(&msg, player->hWndVideo, WM_MSG_FRAME, WM_MSG_FRAME, PM_REMOVE)) {
  868. frame_entry *entry = (frame_entry *)msg.wParam;
  869. free_frame_entry(player, entry);
  870. free(entry);
  871. }
  872. unhook_window(player);
  873. DeleteCriticalSection(&player->q_lock);
  874. VirtualFree(player, 0, MEM_RELEASE|MEM_DECOMMIT);
  875. }
  876. int videoplayer_queue_frame(videoplayer_t *player, video_frame *frame, videoplayer_free_frame free_frame, void *free_frame_user_data)
  877. {
  878. assert(frame->format == VIDEO_FORMAT_RGB24);
  879. if (player && player->hWndVideo && (player->flags&VIDEOPLAYER_FLAG_PUSH)) {
  880. if (player->flags & VIDEOPLAYER_FLAG_NOTIMER) {
  881. frame_entry *entry = (frame_entry*)malloc(sizeof(frame_entry));
  882. entry->free_frame = free_frame;
  883. entry->free_frame_user_data = free_frame_user_data;
  884. entry->raw_frame = frame;
  885. entry->dwTick = GetTickCount();
  886. if (!PostMessageA(player->hWndVideo, WM_MSG_FRAME, (WPARAM)entry, 0))
  887. {
  888. free(entry);
  889. return -1;
  890. }
  891. } else {
  892. EnterCriticalSection(&player->q_lock);
  893. player->frames[player->q_tail].raw_frame = frame;
  894. player->frames[player->q_tail].free_frame = free_frame;
  895. player->frames[player->q_tail].free_frame_user_data = free_frame_user_data;
  896. player->frames[player->q_tail].dwTick = GetTickCount();
  897. player->q_tail = QUEUE_INC(player->q_tail, FRAME_QUEUE_LEGNTH);
  898. if (player->q_caching) {
  899. int qlen = QUEUE_SIZE(player->q_head, player->q_tail, FRAME_QUEUE_LEGNTH);
  900. //Dbg("qlen:%d, %d, %d", qlen, player->min_cache_size, player->max_cache_size);
  901. if (qlen >= player->min_cache_size)
  902. player->q_caching = 0;
  903. }
  904. while (QUEUE_SIZE(player->q_head, player->q_tail, FRAME_QUEUE_LEGNTH) > player->max_cache_size) {
  905. int qlen = QUEUE_SIZE(player->q_head, player->q_tail, FRAME_QUEUE_LEGNTH);
  906. //Dbg("erase qlen:%d", qlen);
  907. free_frame_entry(player, &player->frames[player->q_head]);
  908. player->q_head = QUEUE_INC(player->q_head, FRAME_QUEUE_LEGNTH);
  909. }
  910. LeaveCriticalSection(&player->q_lock);
  911. }
  912. } else {
  913. return -1;
  914. }
  915. return 0;
  916. }