test.mm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. template <typename T, typename D_T, D_T D>
  14. struct OBSUniqueHandle : std::unique_ptr<T, std::function<D_T>>
  15. {
  16. using base = std::unique_ptr<T, std::function<D_T>>;
  17. explicit OBSUniqueHandle(T *obj=nullptr) : base(obj, D) {}
  18. operator T*() { return base::get(); }
  19. };
  20. #define DECLARE_DELETER(x) decltype(x), x
  21. using SourceContext = OBSUniqueHandle<obs_source,
  22. DECLARE_DELETER(obs_source_release)>;
  23. using SceneContext = OBSUniqueHandle<obs_scene,
  24. DECLARE_DELETER(obs_scene_release)>;
  25. #undef DECLARE_DELETER
  26. /* --------------------------------------------------- */
  27. static void CreateOBS(NSView *view)
  28. {
  29. if (!obs_startup())
  30. throw "Couldn't create OBS";
  31. struct obs_video_info ovi;
  32. ovi.adapter = 0;
  33. ovi.fps_num = 30000;
  34. ovi.fps_den = 1001;
  35. ovi.graphics_module = "libobs-opengl";
  36. ovi.output_format = VIDEO_FORMAT_RGBA;
  37. ovi.base_width = cx;
  38. ovi.base_height = cy;
  39. ovi.output_width = cx;
  40. ovi.output_height = cy;
  41. ovi.window_width = cx;
  42. ovi.window_height = cy;
  43. ovi.window.view = view;
  44. if (!obs_reset_video(&ovi))
  45. throw "Couldn't initialize video";
  46. }
  47. static SceneContext SetupScene()
  48. {
  49. /* ------------------------------------------------------ */
  50. /* load module */
  51. if (obs_load_module("test-input") != 0)
  52. throw "Couldn't load module";
  53. /* ------------------------------------------------------ */
  54. /* create source */
  55. SourceContext source{obs_source_create(OBS_SOURCE_TYPE_INPUT,
  56. "random", "a test source", nullptr)};
  57. if (!source)
  58. throw "Couldn't create random test source";
  59. /* ------------------------------------------------------ */
  60. /* create scene and add source to scene */
  61. SceneContext scene{obs_scene_create("test scene")};
  62. if (!scene)
  63. throw "Couldn't create scene";
  64. obs_scene_add(scene, source);
  65. /* ------------------------------------------------------ */
  66. /* set the scene as the primary draw source and go */
  67. obs_set_output_source(0, obs_scene_getsource(scene));
  68. return scene;
  69. }
  70. @interface OBSTest : NSObject<NSApplicationDelegate, NSWindowDelegate>
  71. {
  72. NSWindow *win;
  73. NSView *view;
  74. SceneContext scene;
  75. }
  76. - (void)applicationDidFinishLaunching:(NSNotification*)notification;
  77. - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)app;
  78. - (void)windowWillClose:(NSNotification*)notification;
  79. @end
  80. @implementation OBSTest
  81. - (void)applicationDidFinishLaunching:(NSNotification*)notification
  82. {
  83. UNUSED_PARAMETER(notification);
  84. try {
  85. NSRect content_rect = NSMakeRect(0, 0, cx, cy);
  86. win = [[NSWindow alloc]
  87. initWithContentRect:content_rect
  88. styleMask:NSTitledWindowMask | NSClosableWindowMask
  89. backing:NSBackingStoreBuffered
  90. defer:NO];
  91. if (!win)
  92. throw "Could not create window";
  93. view = [[NSView alloc] initWithFrame:content_rect];
  94. if (!view)
  95. throw "Could not create view";
  96. win.title = @"foo";
  97. win.delegate = self;
  98. win.contentView = view;
  99. [win orderFrontRegardless];
  100. [win center];
  101. [win makeMainWindow];
  102. CreateOBS(view);
  103. scene = SetupScene();
  104. obs_add_draw_callback(
  105. [](void *, uint32_t, uint32_t) {
  106. obs_render_main_view();
  107. }, nullptr);
  108. } catch (char const *error) {
  109. NSLog(@"%s\n", error);
  110. [NSApp terminate:nil];
  111. }
  112. }
  113. - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)app
  114. {
  115. UNUSED_PARAMETER(app);
  116. return YES;
  117. }
  118. - (void)windowWillClose:(NSNotification*)notification
  119. {
  120. UNUSED_PARAMETER(notification);
  121. obs_set_output_source(0, nullptr);
  122. scene.reset();
  123. obs_shutdown();
  124. NSLog(@"Number of memory leaks: %lu", bnum_allocs());
  125. }
  126. @end
  127. /* --------------------------------------------------- */
  128. int main()
  129. {
  130. @autoreleasepool {
  131. [NSApplication sharedApplication];
  132. OBSTest *test = [[OBSTest alloc] init];
  133. [NSApp setDelegate:test];
  134. [NSApp run];
  135. }
  136. return 0;
  137. }