test.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. static LRESULT CALLBACK sceneProc(HWND hwnd, UINT message, WPARAM wParam,
  29. LPARAM lParam)
  30. {
  31. switch (message) {
  32. case WM_CLOSE:
  33. PostQuitMessage(0);
  34. break;
  35. default:
  36. return DefWindowProc(hwnd, message, wParam, lParam);
  37. }
  38. return 0;
  39. }
  40. static void do_log(int log_level, const char *msg, va_list args, void *param)
  41. {
  42. char bla[4096];
  43. vsnprintf(bla, 4095, msg, args);
  44. OutputDebugStringA(bla);
  45. OutputDebugStringA("\n");
  46. if (log_level <= LOG_WARNING)
  47. __debugbreak();
  48. UNUSED_PARAMETER(param);
  49. }
  50. static void CreateOBS(HWND hwnd)
  51. {
  52. RECT rc;
  53. GetClientRect(hwnd, &rc);
  54. if (!obs_startup("en-US"))
  55. throw "Couldn't create OBS";
  56. struct obs_video_info ovi;
  57. ovi.adapter = 0;
  58. ovi.base_width = rc.right;
  59. ovi.base_height = rc.bottom;
  60. ovi.fps_num = 30000;
  61. ovi.fps_den = 1001;
  62. ovi.graphics_module = "libobs-opengl";
  63. ovi.window_width = rc.right;
  64. ovi.window_height = rc.bottom;
  65. ovi.output_format = VIDEO_FORMAT_RGBA;
  66. ovi.output_width = rc.right;
  67. ovi.output_height = rc.bottom;
  68. ovi.window.hwnd = hwnd;
  69. if (obs_reset_video(&ovi) != 0)
  70. throw "Couldn't initialize video";
  71. }
  72. static void AddTestItems(obs_scene_t *scene, obs_source_t *source)
  73. {
  74. obs_sceneitem_t *item = NULL;
  75. struct vec2 scale;
  76. vec2_set(&scale, 20.0f, 20.0f);
  77. item = obs_scene_add(scene, source);
  78. obs_sceneitem_set_scale(item, &scale);
  79. }
  80. static HWND CreateTestWindow(HINSTANCE instance)
  81. {
  82. WNDCLASS wc;
  83. memset(&wc, 0, sizeof(wc));
  84. wc.lpszClassName = TEXT("bla");
  85. wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  86. wc.hInstance = instance;
  87. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  88. wc.lpfnWndProc = (WNDPROC)sceneProc;
  89. if (!RegisterClass(&wc))
  90. return 0;
  91. return CreateWindow(TEXT("bla"), TEXT("bla"),
  92. WS_OVERLAPPEDWINDOW|WS_VISIBLE,
  93. 1920/2 - cx/2, 1080/2 - cy/2, cx, cy,
  94. NULL, NULL, instance, NULL);
  95. }
  96. /* --------------------------------------------------- */
  97. static void RenderWindow(void *data, uint32_t cx, uint32_t cy)
  98. {
  99. obs_render_main_view();
  100. UNUSED_PARAMETER(data);
  101. UNUSED_PARAMETER(cx);
  102. UNUSED_PARAMETER(cy);
  103. }
  104. /* --------------------------------------------------- */
  105. int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine,
  106. int numCmd)
  107. {
  108. HWND hwnd = NULL;
  109. base_set_log_handler(do_log, nullptr);
  110. try {
  111. hwnd = CreateTestWindow(instance);
  112. if (!hwnd)
  113. throw "Couldn't create main window";
  114. /* ------------------------------------------------------ */
  115. /* create OBS */
  116. CreateOBS(hwnd);
  117. /* ------------------------------------------------------ */
  118. /* load modules */
  119. obs_load_all_modules();
  120. /* ------------------------------------------------------ */
  121. /* create source */
  122. SourceContext source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  123. "random", "some randon source", NULL);
  124. if (!source)
  125. throw "Couldn't create random test source";
  126. /* ------------------------------------------------------ */
  127. /* create filter */
  128. SourceContext filter = obs_source_create(OBS_SOURCE_TYPE_FILTER,
  129. "test_filter", "a nice green filter", NULL);
  130. if (!filter)
  131. throw "Couldn't create test filter";
  132. obs_source_filter_add(source, filter);
  133. /* ------------------------------------------------------ */
  134. /* create scene and add source to scene (twice) */
  135. SceneContext scene = obs_scene_create("test scene");
  136. if (!scene)
  137. throw "Couldn't create scene";
  138. AddTestItems(scene, source);
  139. /* ------------------------------------------------------ */
  140. /* set the scene as the primary draw source and go */
  141. obs_set_output_source(0, obs_scene_get_source(scene));
  142. /* ------------------------------------------------------ */
  143. /* set the main output render callback */
  144. obs_add_draw_callback(RenderWindow, nullptr);
  145. MSG msg;
  146. while (GetMessage(&msg, NULL, 0, 0)) {
  147. TranslateMessage(&msg);
  148. DispatchMessage(&msg);
  149. }
  150. } catch (char *error) {
  151. MessageBoxA(NULL, error, NULL, 0);
  152. }
  153. obs_shutdown();
  154. blog(LOG_INFO, "Number of memory leaks: %llu", bnum_allocs());
  155. DestroyWindow(hwnd);
  156. UNUSED_PARAMETER(prevInstance);
  157. UNUSED_PARAMETER(cmdLine);
  158. UNUSED_PARAMETER(numCmd);
  159. return 0;
  160. }