test.mm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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("en"))
  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) != 0)
  45. throw "Couldn't initialize video";
  46. }
  47. static SceneContext SetupScene()
  48. {
  49. /* ------------------------------------------------------ */
  50. /* load modules */
  51. obs_load_all_modules();
  52. /* ------------------------------------------------------ */
  53. /* create source */
  54. SourceContext source{obs_source_create(OBS_SOURCE_TYPE_INPUT,
  55. "random", "a test source", nullptr)};
  56. if (!source)
  57. throw "Couldn't create random test source";
  58. /* ------------------------------------------------------ */
  59. /* create scene and add source to scene */
  60. SceneContext scene{obs_scene_create("test scene")};
  61. if (!scene)
  62. throw "Couldn't create scene";
  63. obs_scene_add(scene, source);
  64. /* ------------------------------------------------------ */
  65. /* set the scene as the primary draw source and go */
  66. obs_set_output_source(0, obs_scene_get_source(scene));
  67. return scene;
  68. }
  69. @interface OBSTest : NSObject<NSApplicationDelegate, NSWindowDelegate>
  70. {
  71. NSWindow *win;
  72. NSView *view;
  73. SceneContext scene;
  74. }
  75. - (void)applicationDidFinishLaunching:(NSNotification*)notification;
  76. - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)app;
  77. - (void)windowWillClose:(NSNotification*)notification;
  78. @end
  79. @implementation OBSTest
  80. - (void)applicationDidFinishLaunching:(NSNotification*)notification
  81. {
  82. UNUSED_PARAMETER(notification);
  83. try {
  84. NSRect content_rect = NSMakeRect(0, 0, cx, cy);
  85. win = [[NSWindow alloc]
  86. initWithContentRect:content_rect
  87. styleMask:NSTitledWindowMask | NSClosableWindowMask
  88. backing:NSBackingStoreBuffered
  89. defer:NO];
  90. if (!win)
  91. throw "Could not create window";
  92. view = [[NSView alloc] initWithFrame:content_rect];
  93. if (!view)
  94. throw "Could not create view";
  95. win.title = @"foo";
  96. win.delegate = self;
  97. win.contentView = view;
  98. [win orderFrontRegardless];
  99. [win center];
  100. [win makeMainWindow];
  101. CreateOBS(view);
  102. scene = SetupScene();
  103. obs_add_draw_callback(
  104. [](void *, uint32_t, uint32_t) {
  105. obs_render_main_view();
  106. }, nullptr);
  107. } catch (char const *error) {
  108. NSLog(@"%s\n", error);
  109. [NSApp terminate:nil];
  110. }
  111. }
  112. - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)app
  113. {
  114. UNUSED_PARAMETER(app);
  115. return YES;
  116. }
  117. - (void)windowWillClose:(NSNotification*)notification
  118. {
  119. UNUSED_PARAMETER(notification);
  120. obs_set_output_source(0, nullptr);
  121. scene.reset();
  122. obs_shutdown();
  123. NSLog(@"Number of memory leaks: %lu", bnum_allocs());
  124. }
  125. @end
  126. /* --------------------------------------------------- */
  127. int main()
  128. {
  129. @autoreleasepool {
  130. NSApplication *app = [NSApplication sharedApplication];
  131. OBSTest *test = [[OBSTest alloc] init];
  132. app.delegate = test;
  133. [app run];
  134. }
  135. return 0;
  136. }