test.mm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <functional>
  4. #include <memory>
  5. #import <Cocoa/Cocoa.h>
  6. #import <AppKit/AppKit.h>
  7. #import <OpenGL/OpenGL.h>
  8. #include "util/base.h"
  9. #include "obs.h"
  10. static const int cx = 800;
  11. static const int cy = 600;
  12. /* --------------------------------------------------- */
  13. using SourceContext = std::unique_ptr<obs_source,
  14. std::function<void(obs_source_t)>>;
  15. static SourceContext autorelease(obs_source_t s)
  16. {
  17. return SourceContext(s, obs_source_release);
  18. }
  19. using SceneContext = std::unique_ptr<obs_scene,
  20. std::function<void(obs_scene_t)>>;
  21. static SceneContext autorelease(obs_scene_t s)
  22. {
  23. return SceneContext(s, obs_scene_destroy);
  24. }
  25. /* --------------------------------------------------- */
  26. static void CreateOBS(NSWindow *win)
  27. {
  28. if (!obs_startup())
  29. throw "Couldn't create OBS";
  30. struct obs_video_info ovi;
  31. ovi.adapter = 0;
  32. ovi.base_width = cx;
  33. ovi.base_height = cy;
  34. ovi.fps_num = 30000;
  35. ovi.fps_den = 1001;
  36. ovi.graphics_module = "libobs-opengl";
  37. ovi.output_format = VIDEO_FORMAT_RGBA;
  38. ovi.output_width = cx;
  39. ovi.output_height = cy;
  40. ovi.window.view = [win contentView];
  41. if (!obs_reset_video(&ovi))
  42. throw "Couldn't initialize video";
  43. }
  44. static void AddTestItems(obs_scene_t scene, obs_source_t source)
  45. {
  46. obs_sceneitem_t item = NULL;
  47. struct vec2 v2;
  48. item = obs_scene_add(scene, source);
  49. vec2_set(&v2, 100.0f, 200.0f);
  50. obs_sceneitem_setpos(item, &v2);
  51. obs_sceneitem_setrot(item, 10.0f);
  52. vec2_set(&v2, 20.0f, 2.0f);
  53. obs_sceneitem_setscale(item, &v2);
  54. item = obs_scene_add(scene, source);
  55. vec2_set(&v2, 200.0f, 100.0f);
  56. obs_sceneitem_setpos(item, &v2);
  57. obs_sceneitem_setrot(item, -45.0f);
  58. vec2_set(&v2, 5.0f, 7.0f);
  59. obs_sceneitem_setscale(item, &v2);
  60. }
  61. @interface window_closer : NSObject {}
  62. @end
  63. @implementation window_closer
  64. +(id)window_closer
  65. {
  66. return [[window_closer alloc] init];
  67. }
  68. -(void)windowWillClose:(NSNotification *)notification
  69. {
  70. [NSApp stop:self];
  71. }
  72. @end
  73. static NSWindow *CreateTestWindow()
  74. {
  75. [NSApplication sharedApplication];
  76. ProcessSerialNumber psn = {0, kCurrentProcess};
  77. TransformProcessType(&psn, kProcessTransformToForegroundApplication);
  78. #pragma clang diagnostic push
  79. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  80. SetFrontProcess(&psn);
  81. #pragma clang diagnostic pop
  82. NSRect content_rect = NSMakeRect(0, 0, cx, cy);
  83. NSWindow *win = [[NSWindow alloc]
  84. initWithContentRect:content_rect
  85. styleMask:NSTitledWindowMask | NSClosableWindowMask
  86. backing:NSBackingStoreBuffered
  87. defer:NO];
  88. if(!win)
  89. return nil;
  90. [win orderFrontRegardless];
  91. NSView *view = [[NSView alloc] initWithFrame:content_rect];
  92. if(!view)
  93. return nil;
  94. [win setContentView:view];
  95. [win setTitle:@"foo"];
  96. [win center];
  97. [win makeMainWindow];
  98. [win setDelegate:[window_closer window_closer]];
  99. return win;
  100. }
  101. static void test()
  102. {
  103. try {
  104. NSWindow *win = CreateTestWindow();
  105. if (!win)
  106. throw "Couldn't create main window";
  107. CreateOBS(win);
  108. /* ------------------------------------------------------ */
  109. /* load module */
  110. if (obs_load_module("test-input") != 0)
  111. throw "Couldn't load module";
  112. /* ------------------------------------------------------ */
  113. /* create source */
  114. SourceContext source = autorelease(obs_source_create(SOURCE_INPUT,
  115. "random", NULL));
  116. if (!source)
  117. throw "Couldn't create random test source";
  118. /* ------------------------------------------------------ */
  119. /* create filter */
  120. SourceContext filter = autorelease(obs_source_create(SOURCE_FILTER,
  121. "test", NULL));
  122. if (!filter)
  123. throw "Couldn't create test filter";
  124. obs_source_filter_add(source.get(), filter.get());
  125. /* ------------------------------------------------------ */
  126. /* create scene and add source to scene (twice) */
  127. SceneContext scene = autorelease(obs_scene_create());
  128. if (!scene)
  129. throw "Couldn't create scene";
  130. AddTestItems(scene.get(), source.get());
  131. /* ------------------------------------------------------ */
  132. /* set the scene as the primary draw source and go */
  133. obs_set_output_source(0, obs_scene_getsource(scene.get()));
  134. [NSApp run];
  135. obs_set_output_source(0, NULL);
  136. } catch (char const *error) {
  137. printf("%s\n", error);
  138. }
  139. obs_shutdown();
  140. blog(LOG_INFO, "Number of memory leaks: %zu", bnum_allocs());
  141. }
  142. /* --------------------------------------------------- */
  143. int main()
  144. {
  145. @autoreleasepool {
  146. test();
  147. }
  148. return 0;
  149. }