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- appRunning = true ;
90- 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 ();
9199
92- @autoreleasepool {
93- NSApplication *app = [NSApplication sharedApplication ];
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,37 @@ @implementation UtilImplObj
191213 return 0 ;
192214 return physical_cores;
193215}
216+
217+ std::string UtilObjCInt::getCpuName (void )
218+ {
219+ char buffer[256 ];
220+ size_t size = sizeof (buffer);
221+ std::string name;
222+
223+ if (sysctlbyname (" machdep.cpu.brand_string" , buffer, &size, nullptr , 0 ) == 0 ) {
224+ name = std::string (buffer);
225+ } else if (sysctlbyname (" hw.model" , buffer, &size, nullptr , 0 ) == 0 ) {
226+ name = std::string (buffer);
227+ }
228+
229+ return name;
230+ }
231+
232+ void UtilObjCInt::nextState (void )
233+ {
234+ if (state + 1 < UtilObjCInt::EState::Max) {
235+ state = state + 1 ;
236+ }
237+ }
238+
239+ bool UtilObjCInt::hasInitApi (void )
240+ {
241+ return state == UtilObjCInt::EState::InitializedApi;
242+ }
243+
244+ bool UtilObjCInt::hasInitCef (void )
245+ {
246+ return state == UtilObjCInt::EState::InitializedCEF;
247+ }
248+
194249@end
0 commit comments