test.cpp 4.5 KB

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