API BUG: Create an invisible duplicated source leaks one frame on creation

Llorx

New Member
Hi,

I was fiddling with the OBS API to duplicate sources on-the-fly and found a bug. Or maybe I'm not doing it right.

With a plugin I just create a duplicated source from a webcam and at the moment of creation I set the source invisible. The problem is that some times the source leaks one frame before turning invisible.

This is an example code:

C++:
obs_source_addref(fromSource); // Add reference
obs_sceneitem_t* newItem = obs_scene_add(obs_scene_from_source(toScene), fromSource); // Add the source to another scene
obs_sceneitem_set_visible(newItem, false); // Set new source visibility
/* [...] Set position [...] */

No callbacks, no nothing in between.

These are the timings:
- Duplicate source with this code, set visibility to false and change its position. Leak occurs.
- Wait 300 ms.
- Set visibility to true.
- Wait 200 ms.
- Delete element.
- Wait 500 ms.
- Loop again

And with that you can see that sometimes the frame is leaked on creation: https://vgy.me/CzKjfA.gif
(Sorry about my hair. It wasn't combed xD)
 

RytoEX

Forum Admin
Forum Moderator
Developer
I don't think this is a bug.

You could try wrapping the block in defer_update calls, if you had access to the item before it was added/moved/changed:
C++:
obs_sceneitem_defer_update_begin(item);
/* do stuff */
obs_sceneitem_defer_update_end(item);

Or use what the AddExisting function uses (in UI/window-basic-source-select.cpp), obs_scene_atomic_update, though you might have to reimplement parts of how AddExisting works since they are some static functions involved.

Or if you are using the OBS Studio UI, just call OBSBasicSourceSelect::SourcePaste, which will do it for you. See around here for examples of copy/paste source functionality in the UI code.
 

Llorx

New Member
Ok, the defer method don't works as for that I need the sceneitem, and to have the sceneitem I have to add it to the scene, and the problem occurs at this point, when the item is added to the scene.

I guess I have to check the `obs_scene_atomic_update ` method.
 
Top