title: AVCaptureSession starts and then immediately fails
body: <p>I have a feed of videos that use a custom video player, based on an AVPlayer, to play HLS streams. On a navigation bar, there is a button to launch the camera. As of the iOS 7.0.3 update, the capture session now encounters SERIOUS problems where it will start, and then immediately tear itself down, emitting an error like so:</p>
  
  <pre><code>Capture session error:
  
  
  NSConcreteNotification 0x146eae70 {name = AVCaptureSessionRuntimeErrorNotification; object = &lt;AVCaptureSession: 0x15976e70 [AVCaptureSessionPresetHigh]&gt;
  &lt;AVCaptureDeviceInput: 0x145d3ee0 [Back Camera]&gt; -&gt; &lt;AVCaptureVideoDataOutput: 0x159546f0&gt;
  &lt;AVCaptureDeviceInput: 0x1594bd60 [iPhone Microphone]&gt; -&gt; &lt;AVCaptureAudioDataOutput: 0x159823e0&gt;
  &lt;AVCaptureDeviceInput: 0x145d3ee0 [Back Camera]&gt; -&gt; &lt;AVCaptureVideoPreviewLayer: 0x1467b370&gt;; userInfo = {
  AVCaptureSessionErrorKey = "Error Domain=AVFoundationErrorDomain Code=-11819 \"Cannot Complete Action\" UserInfo=0x146f0ec0 {NSLocalizedRecoverySuggestion=Try again later., NSLocalizedDescription=Cannot Complete Action}";
  
  }}
  </code></pre>
  
  <p>My method to setup the capture session looks like this...</p>
  
  <pre><code>[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureSessionDidStartRunning:) name:AVCaptureSessionDidStartRunningNotification object:captureSession];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureSessionDidStopRunning:) name:AVCaptureSessionDidStopRunningNotification object:captureSession];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureSessionDidFailWithError:) name:AVCaptureSessionRuntimeErrorNotification object:captureSession];
  
  
  discontinuous = NO;
  _recording = NO;
  _paused = NO;
  
  // Alloc and initialize a capture session
  captureSession = [[AVCaptureSession alloc] init];
  
  // Setup and add the video device
  AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  
  NSError *videoError = nil;
  _videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&amp;videoError];
  
  if (videoError) {
      ErrorLog(@"%@", [videoError userInfo]);
  }else {
      if ([captureSession canAddInput:_videoInput])
          [captureSession addInput:_videoInput];
      else
          ErrorLog(@"Cannot add video input");
  }
  
  // Setup and add the audio device
  AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
  
  NSError *audioError = nil;
  _audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&amp;audioError];
  
  if (audioError) {
      ErrorLog(@"%@", [audioError userInfo]);
  }else {
      if ([captureSession canAddInput:_audioInput])
          [captureSession addInput:_audioInput];
      else
          ErrorLog(@"Cannot add audio input");
  }
  
  // Alloc and initialize video data output
  AVCaptureVideoDataOutput *videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
  captureQueue = dispatch_queue_create("tv.present.captureQueue", DISPATCH_QUEUE_SERIAL);
  [videoDataOutput setSampleBufferDelegate:self queue:captureQueue];
  
  // Setup default video capture settings (H.264 video pixel format)
  NSDictionary *videoCaptureSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange], kCVPixelBufferPixelFormatTypeKey,
                                        nil];
  [videoDataOutput setVideoSettings:videoCaptureSettings];
  
  if ([captureSession canAddOutput:videoDataOutput])
      [captureSession addOutput:videoDataOutput];
  else
      ErrorLog(@"Cannot add video data output");
  
  // Alloc and initialize audio data output
  AVCaptureAudioDataOutput *audioDataOutput = [[AVCaptureAudioDataOutput alloc] init];
  [audioDataOutput setSampleBufferDelegate:self queue:captureQueue];
  
  // Add the output
  if ([captureSession canAddOutput:audioDataOutput])
      [captureSession addOutput:audioDataOutput];
  else
      ErrorLog(@"Cannot add audio data output");
  
  // Setup the video connection
  if ([videoDataOutput connectionWithMediaType:AVMediaTypeVideo]) {
      videoConnection = [videoDataOutput connectionWithMediaType:AVMediaTypeVideo];
      [videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
  
  
      if ([videoConnection isVideoStabilizationSupported])
          [videoConnection setEnablesVideoStabilizationWhenAvailable:YES];
  }
  
  // Setup the audio connection
  if ([audioDataOutput connectionWithMediaType:AVMediaTypeAudio])
      audioConnection = [audioDataOutput connectionWithMediaType:AVMediaTypeAudio];
  
  PLog(@"Will start capture session!");
  // Start running the capture session
  [captureSession startRunning];
  PLog(@"Did start capture session!");
  
  // Setup the preview layer
  _previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
  [_previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
  </code></pre>
  
  <p>...and is called in viewDidAppear of my CameraViewController.</p>
  
  <p>From what I'm gathering, it seems that when the video player is deallocated while presenting the CameraViewController over the FeedTableViewController, something happens causing a mediaserverd and mediaremoted error. Here are the console logs:</p>
  
  <pre><code>Nov  1 17:28:19 Justin-Makailas-iPhone Present[1320] &lt;Warning&gt;: -[PVideoProcessor setupAndStartCaptureSession] [Line 158]
  
   Will start capture session!
  Nov  1 17:28:20 Justin-Makailas-iPhone kernel[0] &lt;Debug&gt;: AppleH4CamIn::setPowerStateGated: 1
  Nov  1 17:28:20 Justin-Makailas-iPhone kernel[0] &lt;Debug&gt;: AppleH4CamIn::power_on_hardware
  Nov  1 17:28:20 Justin-Makailas-iPhone voiced[1324] &lt;Warning&gt;: Error (hex)80000008 (int)-2147483640 at /SourceCache/VoiceServices/VoiceServices-225.1/Daemon/VSSpeechServer.m:1286 (destroying TTS instance)
  Nov  1 17:28:29 Justin-Makailas-iPhone Present[1320] &lt;Warning&gt;: -[PVideoProcessor captureSessionDidStartRunning:] [Line 218]
  
   Capture session did start running
  Nov  1 17:28:29 Justin-Makailas-iPhone kernel[0] &lt;Debug&gt;: 016737.297413 wlan.A[1075] AppleBCMWLANNetManager::checkRealTimeTraffic():  now 16737.297403541 num entries 4
  Nov  1 17:28:29 Justin-Makailas-iPhone kernel[0] &lt;Debug&gt;: 016737.297437 wlan.A[1076] AppleBCMWLANCore::dumpWmeCounters():  per TIDs tx counters: 43722 18715 0 0 0 68128 1584 0, per TIDs rx counters: 30945 256327 1484 0 0 473 104 0 
  Nov  1 17:28:29 Justin-Makailas-iPhone kernel[0] &lt;Debug&gt;: 016737.297458 wlan.A[1077] AppleBCMWLANCore::dumpWmeCounters():                AWDL: Tx 0 0 0 0 0 0 0 0,                   Rx: 0 0 0 0 0 0 0 0 
  Nov  1 17:28:29 Justin-Makailas-iPhone Present[1320] &lt;Warning&gt;: -[PVideoProcessor setupAndStartCaptureSession] [Line 161]
  
   Did start capture session!
  Nov  1 17:28:30 Justin-Makailas-iPhone ReportCrash[1327] &lt;Notice&gt;: Saved crashreport to /Library/Logs/CrashReporter/stacks+mediaserverd-2013-11-01-172830.plist using uid: 0 gid: 0, synthetic_euid: 0 egid: 0
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone kernel[0] &lt;Debug&gt;: AppleH4CamInUserClient::clientDied
  Nov  1 17:28:30 Justin-Makailas-iPhone Present[1320] &lt;Warning&gt;: NSConcreteNotification 0x14678540 {name = AVCaptureSessionDidStopRunningNotification; object = &lt;AVCaptureSession: 0x15976e70 [AVCaptureSessionPresetHigh]&gt;
        &lt;AVCaptureDeviceInput: 0x145d3ee0 [Back Camera]&gt; -&gt; &lt;AVCaptureVideoDataOutput: 0x159546f0&gt;
        &lt;AVCaptureDeviceInput: 0x1594bd60 [iPhone Microphone]&gt; -&gt; &lt;AVCaptureAudioDataOutput: 0x159823e0&gt;
        &lt;AVCaptureDeviceInput: 0x145d3ee0 [Back Camera]&gt; -&gt; &lt;AVCaptureVideoPreviewLayer: 0x1467b370&gt;}
  Nov  1 17:28:30 Justin-Makailas-iPhone Present[1320] &lt;Warning&gt;: -[PVideoProcessor captureSessionDidStopRunning:] [Line 214]
  
   Capture session did stop running
  Nov  1 17:28:30 Justin-Makailas-iPhone Present[1320] &lt;Warning&gt;: -[PVideoProcessor captureSessionDidFailWithError:] [Line 222]
  
   Capture session error: NSConcreteNotification 0x146eae70 {name = AVCaptureSessionRuntimeErrorNotification; object = &lt;AVCaptureSession: 0x15976e70 [AVCaptureSessionPresetHigh]&gt;
        &lt;AVCaptureDeviceInput: 0x145d3ee0 [Back Camera]&gt; -&gt; &lt;AVCaptureVideoDataOutput: 0x159546f0&gt;
        &lt;AVCaptureDeviceInput: 0x1594bd60 [iPhone Microphone]&gt; -&gt; &lt;AVCaptureAudioDataOutput: 0x159823e0&gt;
        &lt;AVCaptureDeviceInput: 0x145d3ee0 [Back Camera]&gt; -&gt; &lt;AVCaptureVideoPreviewLayer: 0x1467b370&gt;; userInfo = {
       AVCaptureSessionErrorKey = "Error Domain=AVFoundationErrorDomain Code=-11819 \"Cannot Complete Action\" UserInfo=0x146f0ec0 {NSLocalizedRecoverySuggestion=Try again later., NSLocalizedDescription=Cannot Complete Action}";
   }}
  Nov  1 17:28:30 Justin-Makailas-iPhone kernel[0] &lt;Debug&gt;: AppleH4CamInUserClient::clientDied
  Nov  1 17:28:30 Justin-Makailas-iPhone kernel[0] &lt;Debug&gt;: AppleH4CamIn::setPowerStateGated: 0
  Nov  1 17:28:30 Justin-Makailas-iPhone kernel[0] &lt;Debug&gt;: AppleH4CamIn::power_off_hardware
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone com.apple.launchd[1] (com.apple.mediaserverd[1308]) &lt;Notice&gt;: (com.apple.mediaserverd) Exited: Killed: 9
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaserverd[1328] &lt;Notice&gt;: 
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaserverd[1328] &lt;Notice&gt;: 2013-11-01 05:28:30.579219 PM [AirPlay] HAL plugin initializing
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaserverd[1328] &lt;Notice&gt;: 2013-11-01 05:28:30.581993 PM [AirPlay] HAL plugin initialized
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaserverd[1328] &lt;Notice&gt;: &lt;vad&gt; NOTE:     17:28:30.616 [tid 0x3c6af18c] [304]: Logging defaults: [ General Priority: Note; Trace Priority: Note; Async Priority: Error; Traced Scopes: { } ].
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:30 Justin-Makailas-iPhone mediaremoted[21] &lt;Error&gt;: Property list invalid for format: 200 (property lists cannot contain NULL)
  Nov  1 17:28:33 Justin-Makailas-iPhone Present[1320] &lt;Warning&gt;: -[PVideoProcessor stopAndTeardownCaptureSession] [Line 169]
  
   Stop and teardown
  </code></pre>