test.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.window_width = rc.right;
  62. ovi.window_height = rc.bottom;
  63. ovi.output_format = VIDEO_FORMAT_RGBA;
  64. ovi.output_width = rc.right;
  65. ovi.output_height = rc.bottom;
  66. ovi.window.hwnd = hwnd;
  67. if (!obs_reset_video(&ovi))
  68. throw "Couldn't initialize video";
  69. }
  70. static void AddTestItems(obs_scene_t scene, obs_source_t source)
  71. {
  72. obs_sceneitem_t item = NULL;
  73. struct vec2 v2;
  74. item = obs_scene_add(scene, source);
  75. vec2_set(&v2, 100.0f, 200.0f);
  76. obs_sceneitem_setpos(item, &v2);
  77. obs_sceneitem_setrot(item, 10.0f);
  78. vec2_set(&v2, 20.0f, 2.0f);
  79. obs_sceneitem_setscale(item, &v2);
  80. item = obs_scene_add(scene, source);
  81. vec2_set(&v2, 200.0f, 100.0f);
  82. obs_sceneitem_setpos(item, &v2);
  83. obs_sceneitem_setrot(item, -45.0f);
  84. vec2_set(&v2, 5.0f, 7.0f);
  85. obs_sceneitem_setscale(item, &v2);
  86. }
  87. static HWND CreateTestWindow(HINSTANCE instance)
  88. {
  89. WNDCLASS wc;
  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. base_set_log_handler(do_log);
  108. try {
  109. hwnd = CreateTestWindow(instance);
  110. if (!hwnd)
  111. throw "Couldn't create main window";
  112. /* ------------------------------------------------------ */
  113. /* create OBS */
  114. CreateOBS(hwnd);
  115. /* ------------------------------------------------------ */
  116. /* load module */
  117. if (obs_load_module("test-input") != 0)
  118. throw "Couldn't load module";
  119. /* ------------------------------------------------------ */
  120. /* create source */
  121. SourceContext source = obs_source_create(SOURCE_INPUT,
  122. "random", "some randon source", NULL);
  123. if (!source)
  124. throw "Couldn't create random test source";
  125. /* ------------------------------------------------------ */
  126. /* create filter */
  127. SourceContext filter = obs_source_create(SOURCE_FILTER,
  128. "test", "a nice little green filter", NULL);
  129. if (!filter)
  130. throw "Couldn't create test filter";
  131. obs_source_filter_add(source, filter);
  132. /* ------------------------------------------------------ */
  133. /* create scene and add source to scene (twice) */
  134. SceneContext scene = obs_scene_create("test scene");
  135. if (!scene)
  136. throw "Couldn't create scene";
  137. AddTestItems(scene, source);
  138. /* ------------------------------------------------------ */
  139. /* set the scene as the primary draw source and go */
  140. obs_set_output_source(0, obs_scene_getsource(scene));
  141. MSG msg;
  142. while (GetMessage(&msg, NULL, 0, 0)) {
  143. TranslateMessage(&msg);
  144. DispatchMessage(&msg);
  145. }
  146. } catch (char *error) {
  147. MessageBoxA(NULL, error, NULL, 0);
  148. }
  149. obs_shutdown();
  150. blog(LOG_INFO, "Number of memory leaks: %llu", bnum_allocs());
  151. DestroyWindow(hwnd);
  152. return 0;
  153. }