obsproject / obs-browser

CEF-based OBS Studio browser plugin
GNU General Public License v2.0
777 stars 221 forks source link

Request: js hook for scene changes #35

Closed SReject closed 6 years ago

SReject commented 8 years ago

Along with .onVisibilityChange() I'd also like to see an .onSceneChange(@visible as boolean, @scene as String) hook that triggers when the scene changes passing along the scene name that was SWITCHED TO. This would reduce the need for differing BrowserSource sources that handle minor differences between various scenes.

Currently, I use at least 4 differing BrowserSources where only one source is active dependent on the state of my stream: starting, actively streaming, be right back, and ending. All use a the same local source with a different URL parameter; file://path/index.htm?scene=starting for example.

With the suggested js hook for scene changes, only a single global BrowserSource would be required thus reducing resources, load times, ease of use, etc.

DDRBoxman commented 8 years ago

Luckily, Jim just added the frontend API. I'll take a look into this sometime this week.

https://github.com/jp9000/obs-studio/commit/8836592d92284c454f2e2d00b512837d9c53cbe9#diff-54a36ef3dbdac6170da35286f00a0532R88

SReject commented 8 years ago

Instead of (eventually) implementing each item of OBS Studio's API as a callback function, would it be possible to implement them as an EventEmitter that takes place against the BrowserSource's context window object? This would allow multiple context scripts to make use of the exposed interface without interfering with each other. As well as being more inline with current browser javascript progmas and being a tidier interface.

To further explain why emitted events are perfered over a callback, take the following scenario:

<script src="script1.js">
/*
Attempts to make use of the window.obsstudio.onSceneChange callback
*/
</script>  

<script src="script2.js">
/*
Sets its own callback for window.obsstudio.onSceneChange, resulting in
the callback from script1.js being overwritten
*/
</script>

This would be avoided with events where each file just attaches an event handler:

window.addEventListener("obsstudio:visibilityChange", (evt) => {
    if (evt.data === true) { ... }
});

window.addEventListener("obsstudio:sceneChange", (evt) => {
    if (evt.data === "someScene") { ... }
});

Along with that, it leaves the window.obsstudio object uncluttered for future additions such as getters and setters related to the current state of OBS.

theunknownartisthour commented 7 years ago

There appears to be an issue with changing scenes with hotkeys where the function doesn't fire. Fairly consistent and by fairly consistent I mean it never happens, there's a distinct difference between switching scenes by clicking or using the directional keys and by using hotkeys, if you do happen to fix this, perhaps it'd be nice to add event for onSceneChangeWithHotkey()* and at the moment onSceneChangeWithoutHotkey(). Given the current behavior it seems possible.

OsirisNL commented 7 years ago

This works fine for me. The event fires either way.

theunknownartisthour commented 7 years ago

I've been chatting with people on mantis at the moment it seems to be windows 7 specific at the very least.

OsirisNL commented 7 years ago

Yes, you were talking to me on mantis. But I doubt that it's Windows 7 specific. OBS Studio does not differentiate between scene changes done by hotkeys or by using the mouse, it's all the same event.