Skip to content

Commit 78d003b

Browse files
committed
Add option to get OBS output status from Javascript
1 parent c083f4f commit 78d003b

File tree

13 files changed

+115
-16
lines changed

13 files changed

+115
-16
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,22 @@ window.addEventListener('obsSceneChanged', function(evt) {
5555

5656
### Get the current scene
5757
```
58-
window.obsstudio.getCurrentScene(function(data) {console.log(data);});
58+
window.obsstudio.getCurrentScene(function(data) { console.log(data); });
59+
60+
data is a Javascript object containing the following properties:
61+
* width
62+
* height
63+
* name
64+
```
65+
66+
### Get OBS output status
67+
```
68+
window.obsstudio.getStatus(function data) { console.log(data); });
69+
70+
data is a Javascript object containing the following properties:
71+
* recording (true/false)
72+
* streaming (true/false)
73+
* replaybuffer (true/false)
5974
```
6075

6176
## Building on OSX

cef-isolation/browser-obs-bridge-mac.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class BrowserOBSBridgeMac : public BrowserOBSBridge
99
BrowserOBSBridgeMac(id<CEFIsolationService> cefIsolationService);
1010

1111
const char* GetCurrentSceneJSONData() override;
12+
const char* GetStatus() override;
1213

1314
private:
1415
id<CEFIsolationService> cefIsolationService;

cef-isolation/browser-obs-bridge-mac.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@
88
{
99
return [cefIsolationService getCurrentSceneJSONData];
1010
}
11+
12+
const char* BrowserOBSBridgeMac::GetStatus()
13+
{
14+
return [cefIsolationService getStatus];
15+
}

obs-browser/apple/cef-isolation-service.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ - (const char*)getCurrentSceneJSONData
120120
return jsonString;
121121
}
122122

123+
- (const char*)getStatus
124+
{
125+
const char* jsonString = obsStatusToJSON();
126+
return jsonString;
127+
}
128+
123129
- (void)invalidateClient:(id)client withException:(NSException *)exception
124130
{
125131
UNUSED_PARAMETER(client);

obs-browser/browser-obs-bridge-base.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ const char* BrowserOBSBridgeBase::GetCurrentSceneJSONData()
1515

1616
return jsonString;
1717
}
18+
19+
const char* BrowserOBSBridgeBase::GetStatus()
20+
{
21+
const char* jsonString = obsStatusToJSON();
22+
return jsonString;
23+
}

obs-browser/browser-obs-bridge-base.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ class BrowserOBSBridgeBase : public BrowserOBSBridge
88
BrowserOBSBridgeBase();
99

1010
const char* GetCurrentSceneJSONData() override;
11+
const char* GetStatus() override;
1112
};

obs-browser/main-source.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ static void browser_source_destroy(void *data)
206206
BrowserSource *bs = static_cast<BrowserSource *>(data);
207207

208208
delete bs;
209+
bs = nullptr;
209210
}
210211

211212

shared-apple/cef-isolation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
surfaceHandle:(BrowserSurfaceHandle)surfaceHandle;
3939
- (void)invalidateClient:(id)client withException:(NSException *)exception;
4040
- (const char*)getCurrentSceneJSONData;
41+
- (const char*)getStatus;
4142
@end
4243

4344
@protocol CEFIsolatedClient

shared/browser-app.cpp

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ void BrowserApp::OnContextCreated(CefRefPtr<CefBrowser> browser,
7878

7979
CefRefPtr<CefV8Value> func = CefV8Value::CreateFunction("getCurrentScene", this);
8080
obsStudioObj->SetValue("getCurrentScene", func, V8_PROPERTY_ATTRIBUTE_NONE);
81+
82+
CefRefPtr<CefV8Value> getStatus = CefV8Value::CreateFunction("getStatus", this);
83+
obsStudioObj->SetValue("getStatus", getStatus, V8_PROPERTY_ATTRIBUTE_NONE);
8184
}
8285

8386
void BrowserApp::ExecuteJSFunction(CefRefPtr<CefBrowser> browser,
@@ -164,23 +167,33 @@ bool BrowserApp::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
164167

165168
return true;
166169
}
167-
else if (message->GetName() == "executeCallback") {
168-
CefRefPtr<CefV8Context> context = browser->GetMainFrame()->GetV8Context();
170+
else if (message->GetName() == "executeCallback")
171+
{
172+
CefRefPtr<CefV8Context> context = browser->GetMainFrame()->GetV8Context();
173+
CefRefPtr<CefV8Value> retval;
174+
CefRefPtr<CefV8Exception> exception;
169175

170-
context->Enter();
176+
context->Enter();
177+
178+
CefRefPtr<CefListValue> arguments = message->GetArgumentList();
179+
int callbackID = arguments->GetInt(0);
180+
CefString jsonString = arguments->GetString(1);
171181

172-
int callbackID = message->GetArgumentList()->GetInt(0);
173-
CefString jsonString = message->GetArgumentList()->GetString(1);
182+
std::string script = fmt::format(
183+
"JSON.parse('{}');",
184+
arguments->GetString(1).ToString(),
185+
jsonString.ToString().c_str());
174186

175187
CefRefPtr<CefV8Value> callback = callbackMap[callbackID];
176188
CefV8ValueList args;
177-
args.push_back(CefV8Value::CreateString(jsonString));
178189

179-
CefRefPtr<CefV8Value> retval;
180-
CefRefPtr<CefV8Exception> exception;
181-
callback->ExecuteFunction(NULL, args);
190+
context->Eval(script, browser->GetMainFrame()->GetURL(), 0, retval, exception);
191+
192+
args.push_back(retval);
193+
194+
callback->ExecuteFunction(NULL, args);
182195

183-
context->Exit();
196+
context->Exit();
184197

185198
callbackMap.erase(callbackID);
186199

@@ -198,8 +211,8 @@ bool BrowserApp::Execute(const CefString& name,
198211
CefRefPtr<CefV8Value>& retval,
199212
CefString& exception)
200213
{
201-
if (name == "getCurrentScene") {
202-
214+
if (name == "getCurrentScene")
215+
{
203216
if (arguments.size() == 1 && arguments[0]->IsFunction()) {
204217
callbackId++;
205218
callbackMap[callbackId] = arguments[0];
@@ -215,6 +228,22 @@ bool BrowserApp::Execute(const CefString& name,
215228

216229
return true;
217230
}
231+
else if (name == "getStatus")
232+
{
233+
if (arguments.size() == 1 && arguments[0]->IsFunction()) {
234+
callbackId++;
235+
callbackMap[callbackId] = arguments[0];
236+
}
237+
238+
CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("getStatus");
239+
CefRefPtr<CefListValue> args = msg->GetArgumentList();
240+
args->SetInt(0, callbackId);
241+
242+
CefRefPtr<CefBrowser> browser = CefV8Context::GetCurrentContext()->GetBrowser();
243+
browser->SendProcessMessage(PID_BROWSER, msg);
244+
245+
return true;
246+
}
218247

219248
// Function does not exist.
220249
return false;

shared/browser-client.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,20 @@ bool BrowserClient::OnProcessMessageReceived(
105105

106106
return true;
107107
}
108+
else if (message_name == "getStatus") {
109+
110+
int callbackID = message->GetArgumentList()->GetInt(0);
111+
112+
CefString jsonString = browserOBSBridge->GetStatus();
113+
114+
CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("executeCallback");
115+
CefRefPtr<CefListValue> args = msg->GetArgumentList();
116+
args->SetInt(0, callbackID);
117+
args->SetString(1, jsonString);
118+
119+
browser->SendProcessMessage(PID_RENDERER, msg);
120+
121+
return true;
122+
}
108123
return false;
109124
}

0 commit comments

Comments
 (0)