test.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <windows.h>
  4. #include "util/base.h"
  5. #include "obs.h"
  6. #include <intrin.h>
  7. static const int cx = 800;
  8. static const int cy = 600;
  9. /* --------------------------------------------------- */
  10. class SourceContext {
  11. obs_source_t source;
  12. public:
  13. inline SourceContext(obs_source_t source) : source(source) {}
  14. inline ~SourceContext() {obs_source_destroy(source);}
  15. inline operator obs_source_t() {return source;}
  16. };
  17. /* --------------------------------------------------- */
  18. class SceneContext {
  19. obs_scene_t scene;
  20. public:
  21. inline SceneContext(obs_scene_t scene) : scene(scene) {}
  22. inline ~SceneContext() {obs_scene_destroy(scene);}
  23. inline operator obs_scene_t() {return scene;}
  24. };
  25. /* --------------------------------------------------- */
  26. static LRESULT CALLBACK sceneProc(HWND hwnd, UINT message, WPARAM wParam,
  27. LPARAM lParam)
  28. {
  29. switch (message) {
  30. case WM_CLOSE:
  31. PostQuitMessage(0);
  32. break;
  33. default:
  34. return DefWindowProc(hwnd, message, wParam, lParam);
  35. }
  36. return 0;
  37. }
  38. static void do_log(enum log_type type, const char *msg, va_list args)
  39. {
  40. char bla[4096];
  41. vsnprintf(bla, 4095, msg, args);
  42. OutputDebugStringA(bla);
  43. OutputDebugStringA("\n");
  44. /*if (type >= LOG_WARNING)
  45. __debugbreak();*/
  46. }
  47. static void CreateOBS(HWND hwnd)
  48. {
  49. RECT rc;
  50. GetClientRect(hwnd, &rc);
  51. struct video_info vi;
  52. memset(&vi, 0, sizeof(struct video_info));
  53. vi.format = "RGBA";
  54. vi.fps_num = 30000;
  55. vi.fps_den = 1001;
  56. vi.width = rc.right;
  57. vi.height = rc.bottom;
  58. vi.name = "video";
  59. struct gs_init_data gsid;
  60. memset(&gsid, 0, sizeof(gsid));
  61. gsid.hwnd = hwnd;
  62. gsid.cx = rc.right;
  63. gsid.cy = rc.bottom;
  64. gsid.num_backbuffers = 2;
  65. gsid.format = GS_RGBA;
  66. if (!obs_startup("libobs-opengl.dll", &gsid, &vi, NULL))
  67. throw "Couldn't create OBS";
  68. }
  69. static void AddTestItems(obs_scene_t scene, obs_source_t source)
  70. {
  71. obs_sceneitem_t item = NULL;
  72. struct vec2 v2;
  73. item = obs_scene_add(scene, source);
  74. vec2_set(&v2, 100.0f, 200.0f);
  75. obs_sceneitem_setpos(item, &v2);
  76. obs_sceneitem_setrot(item, 10.0f);
  77. vec2_set(&v2, 20.0f, 2.0f);
  78. obs_sceneitem_setscale(item, &v2);
  79. item = obs_scene_add(scene, source);
  80. vec2_set(&v2, 200.0f, 100.0f);
  81. obs_sceneitem_setpos(item, &v2);
  82. obs_sceneitem_setrot(item, -45.0f);
  83. vec2_set(&v2, 5.0f, 7.0f);
  84. obs_sceneitem_setscale(item, &v2);
  85. }
  86. static HWND CreateTestWindow(HINSTANCE instance)
  87. {
  88. WNDCLASS wc;
  89. base_set_log_handler(do_log);
  90. memset(&wc, 0, sizeof(wc));
  91. wc.lpszClassName = L"bla";
  92. wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  93. wc.hInstance = instance;
  94. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  95. wc.lpfnWndProc = (WNDPROC)sceneProc;
  96. if (!RegisterClass(&wc))
  97. return 0;
  98. return CreateWindow(L"bla", L"bla", WS_OVERLAPPEDWINDOW|WS_VISIBLE,
  99. 1920/2 - cx/2, 1080/2 - cy/2, cx, cy,
  100. NULL, NULL, instance, NULL);
  101. }
  102. /* --------------------------------------------------- */
  103. int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine,
  104. int numCmd)
  105. {
  106. HWND hwnd = NULL;
  107. try {
  108. hwnd = CreateTestWindow(instance);
  109. if (!hwnd)
  110. throw "Couldn't create main window";
  111. /* ------------------------------------------------------ */
  112. /* create OBS */
  113. CreateOBS(hwnd);
  114. /* ------------------------------------------------------ */
  115. /* load module */
  116. if (obs_load_module("test-input.dll") != 0)
  117. throw "Couldn't load module";
  118. /* ------------------------------------------------------ */
  119. /* create source */
  120. SourceContext source = obs_source_create(SOURCE_INPUT,
  121. "random", NULL);
  122. if (!source)
  123. throw "Couldn't create random test source";
  124. /* ------------------------------------------------------ */
  125. /* create filter */
  126. SourceContext filter = obs_source_create(SOURCE_FILTER,
  127. "test", NULL);
  128. if (!filter)
  129. throw "Couldn't create test filter";
  130. obs_source_filter_add(source, filter);
  131. /* ------------------------------------------------------ */
  132. /* create scene and add source to scene (twice) */
  133. SceneContext scene = obs_scene_create();
  134. if (!scene)
  135. throw "Couldn't create scene";
  136. AddTestItems(scene, source);
  137. /* ------------------------------------------------------ */
  138. /* set the scene as the primary draw source and go */
  139. obs_set_primary_source(obs_scene_getsource(scene));
  140. MSG msg;
  141. while (GetMessage(&msg, NULL, 0, 0)) {
  142. TranslateMessage(&msg);
  143. DispatchMessage(&msg);
  144. }
  145. obs_set_primary_source(NULL);
  146. } catch (char *error) {
  147. MessageBoxA(NULL, error, NULL, 0);
  148. }
  149. obs_shutdown();
  150. blog(LOG_INFO, "Number of memory leaks: %u", bnum_allocs());
  151. DestroyWindow(hwnd);
  152. return 0;
  153. }