Index: /pjproject/trunk/pjmedia/src/pjmedia-videodev/ios_dev.m
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia-videodev/ios_dev.m	(revision 4836)
+++ /pjproject/trunk/pjmedia/src/pjmedia-videodev/ios_dev.m	(revision 4837)
@@ -97,4 +97,5 @@
     VOutDelegate		*vout_delegate;
     void                        *capture_buf;
+    AVCaptureVideoPreviewLayer  *prev_layer;
     
     void		*render_buf;
@@ -715,7 +716,4 @@
             ios_init_view(strm);
             
-            CALayer *view_layer = strm->render_view.layer;
-            CGRect r = strm->render_view.bounds;
-            
             /* Preview layer instantiation should be in main thread! */
             dispatch_async(dispatch_get_main_queue(), ^{
@@ -726,7 +724,8 @@
                 
                 /* Attach preview layer to a UIView */
-                prev_layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
-                prev_layer.frame = r;
-                [view_layer addSublayer:prev_layer];
+                prev_layer.videoGravity = AVLayerVideoGravityResize;
+                prev_layer.frame = strm->render_view.bounds;
+                [strm->render_view.layer addSublayer:prev_layer];
+                strm->prev_layer = prev_layer;
                 PJ_LOG(4, (THIS_FILE, "Native preview initialized"));
             });
@@ -832,4 +831,6 @@
             dispatch_async(dispatch_get_main_queue(), ^{
 		strm->render_view.bounds = r;
+                if (strm->prev_layer)
+                    strm->prev_layer.frame = r;
             });
             return PJ_SUCCESS;
@@ -891,5 +892,11 @@
 
     if (stream->cap_session) {
-	[stream->cap_session startRunning];
+        if ([NSThread isMainThread]) {
+            [stream->cap_session startRunning];
+        } else {
+            dispatch_sync(dispatch_get_main_queue(), ^{
+                [stream->cap_session startRunning];
+            });
+        }
     
 	if (![stream->cap_session isRunning])
@@ -906,5 +913,4 @@
 {
     struct ios_stream *stream = (struct ios_stream*)strm;
-    //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     
     if (stream->frame_size >= frame->size)
@@ -914,8 +920,7 @@
     
     /* Perform video display in a background thread */
-    dispatch_async(dispatch_get_main_queue(),
-                   ^{[stream->vout_delegate update_image];});
-
-    //[pool release];
+    dispatch_async(dispatch_get_main_queue(), ^{
+        [stream->vout_delegate update_image];
+    });
     
     return PJ_SUCCESS;
@@ -931,6 +936,13 @@
     PJ_LOG(4, (THIS_FILE, "Stopping iOS video stream"));
 
-    if (stream->cap_session && [stream->cap_session isRunning])
-	[stream->cap_session stopRunning];
+    if (stream->cap_session && [stream->cap_session isRunning]) {
+        if ([NSThread isMainThread]) {
+            [stream->cap_session stopRunning];
+        } else {
+            dispatch_sync(dispatch_get_main_queue(), ^{
+                [stream->cap_session stopRunning];
+            });
+        }
+    }
     
     return PJ_SUCCESS;
@@ -965,17 +977,25 @@
     }
 
+    if (stream->prev_layer) {
+        CALayer *prev_layer = stream->prev_layer;
+        dispatch_async(dispatch_get_main_queue(), ^{
+            [prev_layer removeFromSuperlayer];
+            [prev_layer release];
+        });
+        stream->prev_layer = nil;
+    }
+    
     if (stream->render_view) {
         UIView *view = stream->render_view;
-        dispatch_async(dispatch_get_main_queue(),
-          ^{
-              [view removeFromSuperview];
-              [view release];
-           });
-        stream->render_view = NULL;
+        dispatch_async(dispatch_get_main_queue(), ^{
+            [view removeFromSuperview];
+            [view release];
+        });
+        stream->render_view = nil;
     }
     
     if (stream->render_data_provider) {
         CGDataProviderRelease(stream->render_data_provider);
-        stream->render_data_provider = NULL;
+        stream->render_data_provider = nil;
     }
 
