Index: /pjproject/trunk/pjmedia/src/pjmedia-videodev/darwin_dev.m
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia-videodev/darwin_dev.m	(revision 5405)
+++ /pjproject/trunk/pjmedia/src/pjmedia-videodev/darwin_dev.m	(revision 5406)
@@ -233,4 +233,13 @@
     darwin_sizes[idx++].preset_str = AVCaptureSessionPreset1920x1080;
 #endif
+}
+
+static void dispatch_sync_on_main_queue(void (^block)(void))
+{
+    if ([NSThread isMainThread]) {
+        block();
+    } else {
+        dispatch_sync(dispatch_get_main_queue(), block);
+    }
 }
 
@@ -944,5 +953,5 @@
 		if (strm->prev_layer) {
 		    CALayer *prev_layer = strm->prev_layer;
-		    dispatch_async(dispatch_get_main_queue(), ^{
+                    dispatch_sync_on_main_queue(^{
 		        [prev_layer removeFromSuperlayer];
 		        [prev_layer release];
@@ -970,5 +979,5 @@
             
             /* Preview layer instantiation should be in main thread! */
-            dispatch_async(dispatch_get_main_queue(), ^{
+            dispatch_sync_on_main_queue(^{
                 /* Create preview layer */
                 AVCaptureVideoPreviewLayer *prev_layer =
@@ -1075,5 +1084,5 @@
             UIView *view = (UIView *)pval;
             strm->param.window.info.ios.window = (void *)pval;
-            dispatch_async(dispatch_get_main_queue(), ^{
+            dispatch_sync_on_main_queue(^{
                 [view addSubview:strm->render_view];
             });
@@ -1088,5 +1097,5 @@
             r.size = CGSizeMake(strm->param.disp_size.w,
                                 strm->param.disp_size.h);
-            dispatch_async(dispatch_get_main_queue(), ^{
+            dispatch_sync_on_main_queue(^{
 		strm->render_view.bounds = r;
                 if (strm->prev_layer)
@@ -1100,5 +1109,5 @@
             pj_memcpy(&strm->param.window_pos, pval,
                       sizeof(strm->param.window_pos));
-            dispatch_async(dispatch_get_main_queue(), ^{
+            dispatch_sync_on_main_queue(^{
                 strm->render_view.center =
                             CGPointMake(strm->param.window_pos.x +
@@ -1112,5 +1121,5 @@
         case PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE:
         {
-            dispatch_async(dispatch_get_main_queue(), ^{
+            dispatch_sync_on_main_queue(^{
                 strm->render_view.hidden = (BOOL)(*((pj_bool_t *)pval));
             });
@@ -1134,5 +1143,5 @@
             if (strm->param.dir == PJMEDIA_DIR_RENDER) {
 #if TARGET_OS_IPHONE
-            	dispatch_async(dispatch_get_main_queue(), ^{
+                dispatch_sync_on_main_queue(^{
                     strm->render_view.transform =
                         CGAffineTransformMakeRotation(
@@ -1219,11 +1228,7 @@
 
     if (stream->cap_session) {
-        if ([NSThread isMainThread]) {
+        dispatch_sync_on_main_queue(^{
             [stream->cap_session startRunning];
-        } else {
-            dispatch_sync(dispatch_get_main_queue(), ^{
-                [stream->cap_session startRunning];
-            });
-        }
+        });
     
 	if (![stream->cap_session isRunning]) {
@@ -1251,5 +1256,5 @@
     
     /* Perform video display in a background thread */
-    dispatch_async(dispatch_get_main_queue(), ^{
+    dispatch_sync_on_main_queue(^{
         [stream->vout_delegate update_image];
     });
@@ -1269,11 +1274,7 @@
     PJ_LOG(4, (THIS_FILE, "Stopping Darwin video stream"));
 
-    if ([NSThread isMainThread]) {
-	[stream->cap_session stopRunning];
-    } else {
-        dispatch_sync(dispatch_get_main_queue(), ^{
-            [stream->cap_session stopRunning];
-        });
-    }
+    dispatch_sync_on_main_queue(^{
+        [stream->cap_session stopRunning];
+    });
     
     return PJ_SUCCESS;
@@ -1313,5 +1314,5 @@
     if (stream->prev_layer) {
         CALayer *prev_layer = stream->prev_layer;
-        dispatch_async(dispatch_get_main_queue(), ^{
+        dispatch_sync_on_main_queue(^{
             [prev_layer removeFromSuperlayer];
             [prev_layer release];
@@ -1322,5 +1323,5 @@
     if (stream->render_view) {
         UIView *view = stream->render_view;
-        dispatch_async(dispatch_get_main_queue(), ^{
+        dispatch_sync_on_main_queue(^{
             [view removeFromSuperview];
             [view release];
Index: /pjproject/trunk/pjmedia/src/pjmedia-videodev/ios_opengl_dev.m
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia-videodev/ios_opengl_dev.m	(revision 5405)
+++ /pjproject/trunk/pjmedia/src/pjmedia-videodev/ios_opengl_dev.m	(revision 5406)
@@ -20,4 +20,5 @@
 #include <pj/assert.h>
 #include <pj/log.h>
+#include <pj/os.h>
 
 #if defined(PJMEDIA_HAS_VIDEO) && PJMEDIA_HAS_VIDEO != 0 && \
@@ -129,4 +130,13 @@
 }
 
+static void dispatch_sync_on_main_queue(void (^block)(void))
+{
+    if ([NSThread isMainThread]) {
+        block();
+    } else {
+        dispatch_sync(dispatch_get_main_queue(), block);
+    }
+}
+
 @implementation GLView
 
@@ -267,5 +277,7 @@
     
     rect = CGRectMake(0, 0, strm->param.disp_size.w, strm->param.disp_size.h);
-    strm->gl_view = [[GLView alloc] initWithFrame:rect];
+    dispatch_sync_on_main_queue(^{
+	strm->gl_view = [[GLView alloc] initWithFrame:rect];
+    });
     if (!strm->gl_view)
         return PJ_ENOMEM;
@@ -405,10 +417,9 @@
         UIView *view = (UIView *)pval;
         strm->param.window.info.ios.window = (void *)pval;
-        dispatch_async(dispatch_get_main_queue(),
-                       ^{[view addSubview:strm->gl_view];});
+        dispatch_sync_on_main_queue(^{[view addSubview:strm->gl_view];});
         return PJ_SUCCESS;
     } else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_RESIZE) {
         pj_memcpy(&strm->param.disp_size, pval, sizeof(strm->param.disp_size));
-        dispatch_async(dispatch_get_main_queue(), ^{
+        dispatch_sync_on_main_queue(^{
             strm->gl_view.bounds = CGRectMake(0, 0, strm->param.disp_size.w,
                                               strm->param.disp_size.h);
@@ -417,5 +428,5 @@
     } else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_POSITION) {
         pj_memcpy(&strm->param.window_pos, pval, sizeof(strm->param.window_pos));
-        dispatch_async(dispatch_get_main_queue(), ^{
+        dispatch_sync_on_main_queue(^{
             strm->gl_view.center = CGPointMake(strm->param.window_pos.x +
                                                strm->param.disp_size.w/2.0,
@@ -425,5 +436,5 @@
         return PJ_SUCCESS;
     } else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE) {
-        dispatch_async(dispatch_get_main_queue(), ^{
+        dispatch_sync_on_main_queue(^{
             strm->gl_view.hidden = (BOOL)(*((pj_bool_t *)pval));
         });
@@ -433,5 +444,5 @@
         if (strm->param.orient == PJMEDIA_ORIENT_UNKNOWN)
             return PJ_SUCCESS;
-        dispatch_async(dispatch_get_main_queue(), ^{
+        dispatch_sync_on_main_queue(^{
             strm->gl_view.transform =
                 CGAffineTransformMakeRotation(((int)strm->param.orient-1) *
