Permalink
Browse files

Some code cleanup.. now Safari can properly quit without having an ac…

…cess violation (whoo hoo!)
  • Loading branch information...
azinman authored and jeremylightsmith committed Mar 5, 2011
1 parent 2dbbffc commit a08998909d31de34d3ddc8556afa5632218b541a
Showing with 58 additions and 79 deletions.
  1. +1 −0 webkit-plugin-mac/ocv_freenect.hpp
  2. +18 −22 webkit-plugin-mac/ocv_freenect.mm
  3. +39 −57 webkit-plugin-mac/webkit_plugin_macView.mm
@@ -33,5 +33,6 @@ using namespace cv;
int initFreenect();
void* ocvFreenectThread(void *arg);
void killOcvFreenect();
+BOOL isDead();
#endif // __DEPTHJS_OCV_FREENECT_HPP__
@@ -11,40 +11,33 @@
#include "ocv_freenect.hpp"
#include <math.h>
-pthread_t ocv_thread;
-pthread_t freenect_thread;
-volatile int die = 0;
+static pthread_t freenect_thread = 0;
+static int die = 0;
+static BOOL dead = YES;
-int g_argc;
-char **g_argv;
+static pthread_mutex_t buf_mutex = PTHREAD_MUTEX_INITIALIZER;
-int window;
+static Mat depthMat(cv::Size(640,480),CV_16UC1,Scalar(0));
+static Mat rgbMat(cv::Size(640,480),CV_8UC3,Scalar(0));
-pthread_mutex_t buf_mutex = PTHREAD_MUTEX_INITIALIZER;
-Mat depthMat(cv::Size(640,480),CV_16UC1,Scalar(0)),
-rgbMat(cv::Size(640,480),CV_8UC3,Scalar(0));
+static freenect_device *f_dev;
+static int freenect_angle = 15;
+static pthread_cond_t frame_cond = PTHREAD_COND_INITIALIZER;
+static int got_frames = 0;
-freenect_device *f_dev;
-int freenect_angle = 15;
-int freenect_led;
-
-pthread_cond_t frame_cond = PTHREAD_COND_INITIALIZER;
-int got_frames = 0;
-
-uint16_t t_gamma[2048];
-freenect_context *f_ctx;
+static freenect_context *f_ctx;
void *freenect_threadfunc(void* arg);
// void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp);
// void rgb_cb(freenect_device *dev, freenect_pixel *rgb, uint32_t timestamp);
void send_event(const string& etype, const string& edata);
void* freenect_threadfunc(void* arg) { //all this thread does is to fetch events from freenect
- cout << "freenect thread"<<endl;
+ cout << "freenect thread started"<<endl;
while(!die && freenect_process_events(f_ctx) >= 0 ) {}
- cout << "freenect die"<<endl;
+ cout << "freenect dead"<<endl;
return NULL;
}
@@ -239,6 +232,7 @@ Mat prevImg(frameMat.size(),CV_8UC1),
vector<int> hc_stack(20); int hc_stack_ptr = 0;
die = false;
+ dead = NO;
while (!die) {
fr++;
@@ -464,9 +458,8 @@ Mat prevImg(frameMat.size(),CV_8UC1),
}
printf("-- done!\n");
-
pthread_join(freenect_thread, NULL);
- pthread_exit(NULL);
+ dead = YES;
return NULL;
}
@@ -476,3 +469,6 @@ void killOcvFreenect() {
die = true;
}
+BOOL isDead() {
+ return dead;
+}
@@ -27,36 +27,29 @@ - (void) ocvMainLoop;
// BRIDGE BACK FROM C++ LAND -----------------------------------------------------------------------
-static volatile webkit_plugin_macView* hostPlugin = nil;
+static webkit_plugin_macView* hostPlugin = nil;
bool SendEventToBrowser(const string& _eventJson) {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Top-level pool
-
- NSString *eventJson = [NSString stringWithCString:_eventJson.c_str() encoding:[NSString defaultCStringEncoding]];
- DLog(@"Going to send the following eventJson nsstring: %@", eventJson);
-
-
if (hostPlugin == nil) {
- DLog(@"DepthJS Plugin: Ignoring event for uninit host");
return false;
}
-
+
+ BOOL success = false;
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Top-level pool
id pluginContainer = [[hostPlugin pluginArguments] objectForKey:WebPlugInContainerKey];
- if (pluginContainer) {
- /* retrieve a reference to the webview */
+ if (pluginContainer != nil) {
WebView *myWebView = [[pluginContainer webFrame] webView];
+ NSString *eventJson = [NSString stringWithCString:_eventJson.c_str() encoding:[NSString defaultCStringEncoding]];
NSString *js = [NSString stringWithFormat:@"if(DepthJS && DepthJS.npBackend)DepthJS.npBackend.receiveEvent(%@)", eventJson];
-
- [[myWebView windowScriptObject] performSelectorOnMainThread:@selector(evaluateWebScript:) withObject:js waitUntilDone:YES];
- // [[myWebView windowScriptObject] evaluateWebScript:js];
- DLog(@"Sent to javascript");
- [pool release]; // Release the objects in the pool.
- return true;
+ [[myWebView windowScriptObject] performSelectorOnMainThread:@selector(evaluateWebScript:) withObject:js waitUntilDone:NO];
+ DLog(@"[DepthJS] Sent: %@", eventJson);
+ success = true;
} else {
- DLog(@"Could not find pluginContainer?!;");
- [pool release]; // Release the objects in the pool.
+ DLog(@"[DepthJS] Could not find pluginContainer?!;");
return false;
}
+ [pool release]; // Release the objects in the pool.
+ return success;
}
@implementation webkit_plugin_macView
@@ -97,38 +90,18 @@ - (void)webPlugInInitialize {
DLog(@"webPlugInInitialize");
haveInitDevice = NO;
ocvThread = nil;
- hostPlugin = self; // TODO remove later after testing
-}
-
-- (void)webPlugInStart {
- // The plug-in usually begins drawing, playing sounds and/or animation in this method.
- // You are not required to implement this method. It may safely be removed.
- DLog(@"webPlugInStart");
-}
-
-- (void)webPlugInStop {
- // The plug-in normally stop animations/sounds in this method.
- // You are not required to implement this method. It may safely be removed.
- DLog(@"webPlugInStop; killing ocv");
- killOcvFreenect();
+ if (hostPlugin == nil) {
+ hostPlugin = self;
+ }
}
- (void)webPlugInDestroy {
DLog(@"webPlugInDestroy");
- haveInitDevice = FALSE;
-}
-
-- (void)webPlugInSetIsSelected:(BOOL)isSelected {
- // This is typically used to allow the plug-in to alter its appearance when selected.
- // You are not required to implement this method. It may safely be removed.
- DLog(@"webPlugInSetIsSelected");
+ [self ShutdownDepthJS];
+ DLog(@"webPlugInDestroy finished");
}
- (id)objectForWebScript {
- // Returns the object that exposes the plug-in's interface. The class of this object can implement
- // methods from the WebScripting informal protocol.
- // You are not required to implement this method. It may safely be removed.
- DLog(@"objectForWebScript");
return self;
}
@@ -173,6 +146,7 @@ - (id)_initWithArguments:(NSDictionary *)newArguments {
- (void) ocvMainLoop {
ocvThread = [NSThread currentThread];
+ [ocvThread setName:@"ocvMainLoop"];
ocvFreenectThread(NULL);
}
@@ -199,11 +173,8 @@ @implementation webkit_plugin_macView (JsExposed)
}
}
-
-// This isn't really used... but it shows how the JavaScriptCore framework can be used if needed.
- (void) CallbackTest {
- DLog(@"CallbackTest");
- id pluginContainer = [[hostPlugin pluginArguments] objectForKey:WebPlugInContainerKey];
+ id pluginContainer = [[self pluginArguments] objectForKey:WebPlugInContainerKey];
WebView *myWebView = [[pluginContainer webFrame] webView];
JSObjectRef globalObj = [[myWebView windowScriptObject] JSObject];
JSValueRef exception;
@@ -228,32 +199,43 @@ - (void) CallbackTest {
}
- (void) InitDepthJS {
- DLog(@"DepthJS Plugin: InitDepthJS");
+ DLog(@"[DepthJS] InitDepthJS");
+ // If the windowScriptObject is not first referenced from this thread, we will later get a thread
+ // exception. Even though this doesn't do anything. Yah. I have no idea either.
+ id pluginContainer = [[hostPlugin pluginArguments] objectForKey:WebPlugInContainerKey];
+ WebView *myWebView = [[pluginContainer webFrame] webView];
+ [myWebView windowScriptObject];
if (!haveInitDevice) {
- DLog(@"DepthJS Plugin: Device not yet init; initing");
+ DLog(@"[DepthJS] Device not yet init; initing");
+ hostPlugin = self;
int failed = initFreenect();
haveInitDevice = !failed;
if (haveInitDevice) {
- DLog(@"DepthJS Plugin: Successfully inited Kinect; Starting ocv thread");
- hostPlugin = self;
+ DLog(@"[DepthJS] Successfully inited Kinect; Starting ocv thread");
[NSThread detachNewThreadSelector:@selector(ocvMainLoop) toTarget:self withObject:nil];
} else {
- DLog(@"DepthJS Plugin: Failed to init Kinect");
+ DLog(@"[DepthJS] Failed to init Kinect");
+ hostPlugin = nil;
}
} else {
- DLog(@"DepthJS Plugin: Already init, ignoring");
+ DLog(@"[DepthJS] Already init, ignoring");
}
}
- (void) ShutdownDepthJS {
- DLog(@"DepthJS Plugin: ShutdownDepthJS");
- killOcvFreenect();
- DLog(@"DepthJS Plugin: ShutdownDepthJS complete");
+ DLog(@"[DepthJS] ShutdownDepthJS");
haveInitDevice = false;
if (hostPlugin == self) {
hostPlugin = NULL;
+ killOcvFreenect();
+ if (ocvThread != nil) [ocvThread cancel];
+ ocvThread = nil;
+ while (!isDead()) {
+ [NSThread sleepForTimeInterval:0.01];
+ }
}
+ DLog(@"[DepthJS] ShutdownDepthJS complete");
}
@end

0 comments on commit a089989

Please sign in to comment.