1717******************************************************************************/
1818
1919#include " util-osx-impl.h"
20+ #include " obs.h"
2021#include < iostream>
2122#include < unistd.h>
2223#include < sys/types.h>
4344 return ;
4445
4546 while (true ) {
46- if (!appRunning ) {
47+ if (state == UtilObjCInt::EState::Stop ) {
4748 break ;
4849 }
4950 int ret = ::read (file_descriptor, buffer.data (), count);
5051 if (ret > 0 ) {
5152 bool appCrashed = *reinterpret_cast <bool *>(buffer.data ());
52- if (appCrashed && appRunning )
53+ if (appCrashed && state != UtilObjCInt::EState::Stop )
5354 this ->stopApplication ();
5455 break ;
5556 }
@@ -86,17 +87,33 @@ @implementation UtilImplObj
8687
8788void UtilObjCInt::runApplication (void )
8889{
89- worker = new std::thread (&UtilObjCInt::wait_terminate, this );
90+ // Wait for OBS_API_initAPI to be invoked
91+ while (!hasInitApi () && isRunning ()) {
92+ std::this_thread::sleep_for (std::chrono::milliseconds (50 ));
93+ }
94+ if (isRunning ()) {
95+ // Create a dummy source to init obs-browser plugin. Must be init from main thread before [NSApplication run]
96+ obs_source_t *source = obs_source_create (" browser_source" , " dummy" , NULL , NULL );
97+ obs_source_release (source);
98+ nextState ();
9099
91- @autoreleasepool {
92- NSApplication *app = [NSApplication sharedApplication ];
93- appRunning = true ;
94- [app run ];
100+ worker = new std::thread (&UtilObjCInt::wait_terminate, this );
101+
102+ @autoreleasepool {
103+ NSApplication *app = [NSApplication sharedApplication ];
104+ [app run ];
105+ }
95106 }
96107}
97108
98109void UtilObjCInt::stopApplication (void )
99110{
111+ if (!hasInitCef ()) {
112+ state = UtilObjCInt::EState::Stop;
113+ return ;
114+ } else {
115+ state = UtilObjCInt::EState::Stop;
116+ }
100117 dispatch_async (dispatch_get_main_queue (), ^{
101118 [[NSApplication sharedApplication ] stop: nil ];
102119 NSEvent *event = [NSEvent otherEventWithType: NSEventTypeApplicationDefined
@@ -110,12 +127,17 @@ @implementation UtilImplObj
110127 data2: 0 ];
111128 [NSApp postEvent: event atStart: TRUE ];
112129
113- appRunning = false ;
130+ state = UtilObjCInt::EState::Stop ;
114131 if (worker->joinable ())
115132 worker->join ();
116133 });
117134}
118135
136+ bool UtilObjCInt::isRunning (void )
137+ {
138+ return state != UtilObjCInt::EState::Stop;
139+ }
140+
119141unsigned long long UtilObjCInt::getTotalPhysicalMemory (void )
120142{
121143 return [NSProcessInfo processInfo ].physicalMemory ;
@@ -191,4 +213,22 @@ @implementation UtilImplObj
191213 return 0 ;
192214 return physical_cores;
193215}
216+
217+ void UtilObjCInt::nextState (void )
218+ {
219+ if (state + 1 < UtilObjCInt::EState::Max) {
220+ state = state + 1 ;
221+ }
222+ }
223+
224+ bool UtilObjCInt::hasInitApi (void )
225+ {
226+ return state == UtilObjCInt::EState::InitializedApi;
227+ }
228+
229+ bool UtilObjCInt::hasInitCef (void )
230+ {
231+ return state == UtilObjCInt::EState::InitializedCEF;
232+ }
233+
194234@end
0 commit comments