Index: pjproject/trunk/pjmedia/src/pjmedia-videodev/ios_dev.m
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia-videodev/ios_dev.m	(revision 5002)
+++ pjproject/trunk/pjmedia/src/pjmedia-videodev/ios_dev.m	(revision 5051)
@@ -66,5 +66,5 @@
 };
 
-/* qt device info */
+/* ios device info */
 struct ios_dev_info
 {
@@ -73,5 +73,5 @@
 };
 
-/* qt factory */
+/* ios factory */
 struct ios_factory
 {
@@ -112,4 +112,5 @@
     AVCaptureVideoDataOutput	*video_output;
     VOutDelegate		*vout_delegate;
+    dispatch_queue_t 		 queue;
     void                        *capture_buf;
     AVCaptureVideoPreviewLayer  *prev_layer;
@@ -628,4 +629,5 @@
 	strm->cap_session = [[AVCaptureSession alloc] init];
 	if (!strm->cap_session) {
+	    PJ_LOG(2, (THIS_FILE, "Unable to create AV capture session"));
 	    status = PJ_ENOMEM;
 	    goto on_error;
@@ -668,33 +670,47 @@
 			   deviceInputWithDevice:dev
 			   error: &error];
-	if (!strm->dev_input) {
+	if (error || !strm->dev_input) {
+	    PJ_LOG(2, (THIS_FILE, "Unable to get input capture device"));
 	    status = PJMEDIA_EVID_SYSERR;
 	    goto on_error;
 	}
-	[strm->cap_session addInput:strm->dev_input];
 	
-	strm->video_output = [[[AVCaptureVideoDataOutput alloc] init]
-			      autorelease];
-	if (!strm->video_output) {
+	if ([strm->cap_session canAddInput:strm->dev_input]) {
+	    [strm->cap_session addInput:strm->dev_input];
+	} else {
+	    PJ_LOG(2, (THIS_FILE, "Unable to add input capture device"));
 	    status = PJMEDIA_EVID_SYSERR;
 	    goto on_error;
 	}
-        
+	
+	strm->video_output = [[AVCaptureVideoDataOutput alloc] init];
+	if (!strm->video_output) {
+	    PJ_LOG(2, (THIS_FILE, "Unable to create AV video output"));
+	    status = PJ_ENOMEM;
+	    goto on_error;
+	}
+        
+	/* Configure the video output */
         strm->video_output.alwaysDiscardsLateVideoFrames = YES;
-	[strm->cap_session addOutput:strm->video_output];
-	
-	/* Configure the video output */
-	strm->vout_delegate = [VOutDelegate alloc];
-	strm->vout_delegate->stream = strm;
-	dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
-	[strm->video_output setSampleBufferDelegate:strm->vout_delegate
-                                              queue:queue];
-	dispatch_release(queue);
-	
 	strm->video_output.videoSettings =
 	    [NSDictionary dictionaryWithObjectsAndKeys:
 			  [NSNumber numberWithInt:ifi->ios_format],
 			  kCVPixelBufferPixelFormatTypeKey, nil];
-        
+
+	strm->vout_delegate = [VOutDelegate alloc];
+	strm->vout_delegate->stream = strm;
+	strm->queue = dispatch_queue_create("vout_queue",
+					    DISPATCH_QUEUE_SERIAL);
+	[strm->video_output setSampleBufferDelegate:strm->vout_delegate
+                            queue:strm->queue];
+        
+        if ([strm->cap_session canAddOutput:strm->video_output]) {
+	    [strm->cap_session addOutput:strm->video_output];
+	} else {
+	    PJ_LOG(2, (THIS_FILE, "Unable to add video data output"));
+	    status = PJMEDIA_EVID_SYSERR;
+	    goto on_error;
+	}
+	
         /* Prepare capture buffer if it's planar format */
         if (strm->is_planar)
@@ -819,5 +835,4 @@
 	    if (!strm->render_view)
 	        ios_init_view(strm);
-	    
             
             /* Preview layer instantiation should be in main thread! */
@@ -1005,6 +1020,8 @@
         }
     
-	if (![stream->cap_session isRunning])
+	if (![stream->cap_session isRunning]) {
+	    PJ_LOG(3, (THIS_FILE, "Unable to start iOS capture session"));
 	    return PJ_EUNKNOWN;
+	}
     }
     
@@ -1037,16 +1054,15 @@
     struct ios_stream *stream = (struct ios_stream*)strm;
 
-    PJ_UNUSED_ARG(stream);
-
+    if (!stream->cap_session || ![stream->cap_session isRunning])
+        return PJ_SUCCESS;
+    
     PJ_LOG(4, (THIS_FILE, "Stopping iOS video stream"));
 
-    if (stream->cap_session && [stream->cap_session isRunning]) {
-        if ([NSThread isMainThread]) {
+    if ([NSThread isMainThread]) {
+	[stream->cap_session stopRunning];
+    } else {
+        dispatch_sync(dispatch_get_main_queue(), ^{
             [stream->cap_session stopRunning];
-        } else {
-            dispatch_sync(dispatch_get_main_queue(), ^{
-                [stream->cap_session stopRunning];
-            });
-        }
+        });
     }
     
@@ -1065,19 +1081,21 @@
     
     if (stream->cap_session) {
-        [stream->cap_session removeInput:stream->dev_input];
+        if (stream->dev_input) {
+            [stream->cap_session removeInput:stream->dev_input];
+            stream->dev_input = nil;
+        }
         [stream->cap_session removeOutput:stream->video_output];
 	[stream->cap_session release];
 	stream->cap_session = nil;
-    }    
-    if (stream->dev_input) {
-        stream->dev_input = nil;
     }
  
+    if (stream->video_output) {
+        [stream->video_output release];
+        stream->video_output = nil;
+    }
+
     if (stream->vout_delegate) {
 	[stream->vout_delegate release];
 	stream->vout_delegate = nil;
-    }
-    if (stream->video_output) {
-        stream->video_output = nil;
     }
 
@@ -1105,4 +1123,9 @@
     }
 
+    if (stream->queue) {
+        dispatch_release(stream->queue);
+        stream->queue = nil;
+    }
+
     pj_pool_release(stream->pool);
 
