test.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <windows.h>
  4. #include <util/base.h>
  5. #include <graphics/vec2.h>
  6. #include <media-io/audio-resampler.h>
  7. #include <obs.h>
  8. #include <intrin.h>
  9. static const int cx = 800;
  10. static const int cy = 600;
  11. /* --------------------------------------------------- */
  12. class SourceContext {
  13. obs_source_t *source;
  14. public:
  15. inline SourceContext(obs_source_t *source) : source(source) {}
  16. inline ~SourceContext() {obs_source_release(source);}
  17. inline operator obs_source_t*() {return source;}
  18. };
  19. /* --------------------------------------------------- */
  20. class SceneContext {
  21. obs_scene_t *scene;
  22. public:
  23. inline SceneContext(obs_scene_t *scene) : scene(scene) {}
  24. inline ~SceneContext() {obs_scene_release(scene);}
  25. inline operator obs_scene_t*() {return scene;}
  26. };
  27. /* --------------------------------------------------- */
  28. class DisplayContext {
  29. obs_display_t *display;
  30. public:
  31. inline DisplayContext(obs_display_t *display) : display(display) {}
  32. inline ~DisplayContext() {obs_display_destroy(display);}
  33. inline operator obs_display_t*() {return display;}
  34. };
  35. /* --------------------------------------------------- */
  36. static LRESULT CALLBACK sceneProc(HWND hwnd, UINT message, WPARAM wParam,
  37. LPARAM lParam)
  38. {
  39. switch (message) {
  40. case WM_CLOSE:
  41. PostQuitMessage(0);
  42. break;
  43. default:
  44. return DefWindowProc(hwnd, message, wParam, lParam);
  45. }
  46. return 0;
  47. }
  48. static void do_log(int log_level, const char *msg, va_list args, void *param)
  49. {
  50. char bla[4096];
  51. vsnprintf(bla, 4095, msg, args);
  52. OutputDebugStringA(bla);
  53. OutputDebugStringA("\n");
  54. if (log_level < LOG_WARNING)
  55. __debugbreak();
  56. UNUSED_PARAMETER(param);
  57. }
  58. static void CreateOBS(HWND hwnd)
  59. {
  60. RECT rc;
  61. GetClientRect(hwnd, &rc);
  62. if (!obs_startup("en-US", nullptr, nullptr))
  63. throw "Couldn't create OBS";
  64. struct obs_video_info ovi;
  65. ovi.adapter = 0;
  66. ovi.base_width = rc.right;
  67. ovi.base_height = rc.bottom;
  68. ovi.fps_num = 30000;
  69. ovi.fps_den = 1001;
  70. ovi.graphics_module = DL_OPENGL;
  71. ovi.output_format = VIDEO_FORMAT_RGBA;
  72. ovi.output_width = rc.right;
  73. ovi.output_height = rc.bottom;
  74. if (obs_reset_video(&ovi) != 0)
  75. throw "Couldn't initialize video";
  76. }
  77. static DisplayContext CreateDisplay(HWND hwnd)
  78. {
  79. RECT rc;
  80. GetClientRect(hwnd, &rc);
  81. gs_init_data info = {};
  82. info.cx = rc.right;
  83. info.cy = rc.bottom;
  84. info.format = GS_RGBA;
  85. info.zsformat = GS_ZS_NONE;
  86. info.window.hwnd = hwnd;
  87. return obs_display_create(&info);
  88. }
  89. static void AddTestItems(obs_scene_t *scene, obs_source_t *source)
  90. {
  91. obs_sceneitem_t *item = NULL;
  92. struct vec2 scale;
  93. vec2_set(&scale, 20.0f, 20.0f);
  94. item = obs_scene_add(scene, source);
  95. obs_sceneitem_set_scale(item, &scale);
  96. }
  97. static HWND CreateTestWindow(HINSTANCE instance)
  98. {
  99. WNDCLASS wc;
  100. memset(&wc, 0, sizeof(wc));
  101. wc.lpszClassName = TEXT("bla");
  102. wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  103. wc.hInstance = instance;
  104. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  105. wc.lpfnWndProc = (WNDPROC)sceneProc;
  106. if (!RegisterClass(&wc))
  107. return 0;
  108. return CreateWindow(TEXT("bla"), TEXT("bla"),
  109. WS_OVERLAPPEDWINDOW|WS_VISIBLE,
  110. 1920/2 - cx/2, 1080/2 - cy/2, cx, cy,
  111. NULL, NULL, instance, NULL);
  112. }
  113. /* --------------------------------------------------- */
  114. static void RenderWindow(void *data, uint32_t cx, uint32_t cy)
  115. {
  116. obs_render_main_texture();
  117. UNUSED_PARAMETER(data);
  118. UNUSED_PARAMETER(cx);
  119. UNUSED_PARAMETER(cy);
  120. }
  121. /* --------------------------------------------------- */
  122. int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine,
  123. int numCmd)
  124. {
  125. HWND hwnd = NULL;
  126. base_set_log_handler(do_log, nullptr);
  127. try {
  128. hwnd = CreateTestWindow(instance);
  129. if (!hwnd)
  130. throw "Couldn't create main window";
  131. /* ------------------------------------------------------ */
  132. /* create OBS */
  133. CreateOBS(hwnd);
  134. /* ------------------------------------------------------ */
  135. /* load modules */
  136. obs_load_all_modules();
  137. /* ------------------------------------------------------ */
  138. /* create source */
  139. SourceContext source = obs_source_create("random",
  140. "some randon source", NULL, nullptr);
  141. if (!source)
  142. throw "Couldn't create random test source";
  143. /* ------------------------------------------------------ */
  144. /* create filter */
  145. SourceContext filter = obs_source_create("test_filter",
  146. "a nice green filter", NULL, nullptr);
  147. if (!filter)
  148. throw "Couldn't create test filter";
  149. obs_source_filter_add(source, filter);
  150. /* ------------------------------------------------------ */
  151. /* create scene and add source to scene (twice) */
  152. SceneContext scene = obs_scene_create("test scene");
  153. if (!scene)
  154. throw "Couldn't create scene";
  155. AddTestItems(scene, source);
  156. /* ------------------------------------------------------ */
  157. /* set the scene as the primary draw source and go */
  158. obs_set_output_source(0, obs_scene_get_source(scene));
  159. /* ------------------------------------------------------ */
  160. /* create display for output and set the output render callback */
  161. DisplayContext display = CreateDisplay(hwnd);
  162. obs_display_add_draw_callback(display, RenderWindow, nullptr);
  163. MSG msg;
  164. while (GetMessage(&msg, NULL, 0, 0)) {
  165. TranslateMessage(&msg);
  166. DispatchMessage(&msg);
  167. }
  168. } catch (char *error) {
  169. MessageBoxA(NULL, error, NULL, 0);
  170. }
  171. obs_shutdown();
  172. blog(LOG_INFO, "Number of memory leaks: %ld", bnum_allocs());
  173. DestroyWindow(hwnd);
  174. UNUSED_PARAMETER(prevInstance);
  175. UNUSED_PARAMETER(cmdLine);
  176. UNUSED_PARAMETER(numCmd);
  177. return 0;
  178. }