Skip to content

Instantly share code, notes, and snippets.

@sinkingsugar
Last active May 3, 2020 14:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinkingsugar/225392b3032bef0f743cb79f411d430b to your computer and use it in GitHub Desktop.
Save sinkingsugar/225392b3032bef0f743cb79f411d430b to your computer and use it in GitHub Desktop.
Basic OBS Studio plugin, written in nim, supporting C++ (C fine too)
# nim cpp --app:lib .\obsplugin.nim
import nimline
cppincludes("obs-studio/libobs")
defineCppType(ObsData, "obs_data_t" , "obs-module.h")
defineCppType(ObsModule, "obs_module_t" , "obs-module.h")
defineCppType(ObsSource, "obs_source_t" , "obs-module.h")
defineCppType(ObsSourceInfo, "struct obs_source_info" , "obs-module.h")
type
ObsPlugin = object
proc obsplugin_create(data: ptr ObsData; source: ptr ObsSource): pointer {.cdecl, exportc, dynlib.} =
result = cast[ptr ObsPlugin](alloc0(sizeof ObsPlugin))
proc obsplugin_destroy(data: pointer) {.cdecl, exportc, dynlib.} =
dealloc(data)
proc obsplugin_get_name(_: pointer): cstring {.cdecl, exportc, dynlib.} =
result = "ObsPlugin".cstring
## OBS_DECLARE_MODULE()
var obsModulePointer: ptr ObsModule
proc obs_module_set_pointer(module: ptr ObsModule) {.cdecl, exportc, dynlib.} =
obsModulePointer = module
proc obs_current_module(): ptr ObsModule {.cdecl, exportc, dynlib.} = obsModulePointer
proc obs_module_ver(): uint32 {.cdecl, exportc, dynlib.} = global.LIBOBS_API_VER.to(uint32)
##
## Loader + Struct
proc obsRegisterPlugin(info: ptr ObsSourceInfo; size: csize) {.cdecl, dynlib: "obs.dll", importc: "obs_register_source_s".}
var obsplugin_info {.importc, nodecl.}: ObsSourceInfo # must be outside the proc or nim comiler fails
proc obs_module_load(): bool {.cdecl, exportc, dynlib.} =
{.emit: """struct obs_source_info obsplugin_info = { 0 };
obsplugin_info.id = "obsplugin";
obsplugin_info.type = OBS_SOURCE_TYPE_INPUT;
obsplugin_info.output_flags = 0;
obsplugin_info.get_name = obsplugin_get_name;
obsplugin_info.create = obsplugin_create;
obsplugin_info.destroy = obsplugin_destroy;
""".}
obsRegisterPlugin(addr obsplugin_info, sizeof(ObsSourceInfo))
return true
##
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment