title: Saving video from CMSampleBuffer while streaming using ReplayKit
body: <p>I'm streaming a content of my app to my RTMP server and using RPBroadcastSampleHandler.</p>
  
  <p>One of the methods is </p>
  
  <pre><code>override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer, with sampleBufferType: RPSampleBufferType) {
      switch sampleBufferType {
      case .video:
          streamer.appendSampleBuffer(sampleBuffer, withType: .video)
          captureOutput(sampleBuffer)
      case .audioApp:
          streamer.appendSampleBuffer(sampleBuffer, withType: .audio)
          captureAudioOutput(sampleBuffer)
      case .audioMic:
          ()
      }
  }
  </code></pre>
  
  <p>And the captureOutput method is</p>
  
  <pre><code>self.lastSampleTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
  
      // Append the sampleBuffer into videoWriterInput
      if self.isRecordingVideo {
          if self.videoWriterInput!.isReadyForMoreMediaData {
              if self.videoWriter!.status == AVAssetWriterStatus.writing {
                  let whetherAppendSampleBuffer = self.videoWriterInput!.append(sampleBuffer)
                  print("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;The time::: \(self.lastSampleTime.value)/\(self.lastSampleTime.timescale)")
                  if whetherAppendSampleBuffer {
                      print("DEBUG::: Append sample buffer successfully")
                  } else {
                      print("WARN::: Append sample buffer failed")
                  }
              } else {
                  print("WARN:::The videoWriter status is not writing")
              }
          } else {
              print("WARN:::Cannot append sample buffer into videoWriterInput")
          }
      }
  </code></pre>
  
  <p>Since this sample buffer contains audio/video data, I figured I can use AVKit to save it locally while streaming. So what I'm doing is creating an asset writer at the start of the stream:</p>
  
  <pre><code>    let fileManager = FileManager.default
      let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
      self.videoOutputFullFileName = documentsPath.stringByAppendingPathComponent(str: "test_capture_video.mp4")
  
      if self.videoOutputFullFileName == nil {
          print("ERROR:The video output file name is nil")
          return
      }
  
      self.isRecordingVideo = true
      if fileManager.fileExists(atPath: self.videoOutputFullFileName!) {
          print("WARN:::The file: \(self.videoOutputFullFileName!) exists, will delete the existing file")
          do {
              try fileManager.removeItem(atPath: self.videoOutputFullFileName!)
          } catch let error as NSError {
              print("WARN:::Cannot delete existing file: \(self.videoOutputFullFileName!), error: \(error.debugDescription)")
          }
  
      } else {
          print("DEBUG:::The file \(self.videoOutputFullFileName!) doesn't exist")
      }
  
      let screen = UIScreen.main
      let screenBounds = info.size
      let videoCompressionPropertys = [
          AVVideoAverageBitRateKey: screenBounds.width * screenBounds.height * 10.1
      ]
  
      let videoSettings: [String: Any] = [
          AVVideoCodecKey: AVVideoCodecH264,
          AVVideoWidthKey: screenBounds.width,
          AVVideoHeightKey: screenBounds.height,
          AVVideoCompressionPropertiesKey: videoCompressionPropertys
      ]
  
      self.videoWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: videoSettings)
  
      guard let videoWriterInput = self.videoWriterInput else {
          print("ERROR:::No video writer input")
          return
      }
  
      videoWriterInput.expectsMediaDataInRealTime = true
  
      // Add the audio input
      var acl = AudioChannelLayout()
      memset(&amp;acl, 0, MemoryLayout&lt;AudioChannelLayout&gt;.size)
      acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
      let audioOutputSettings: [String: Any] =
          [ AVFormatIDKey: kAudioFormatMPEG4AAC,
            AVSampleRateKey : 44100,
            AVNumberOfChannelsKey : 1,
            AVEncoderBitRateKey : 64000,
            AVChannelLayoutKey : Data(bytes: &amp;acl, count: MemoryLayout&lt;AudioChannelLayout&gt;.size)]
  
      audioWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeAudio, outputSettings: audioOutputSettings)
  
      guard let audioWriterInput = self.audioWriterInput else {
          print("ERROR:::No audio writer input")
          return
      }
  
      audioWriterInput.expectsMediaDataInRealTime = true
  
      do {
          self.videoWriter = try AVAssetWriter(outputURL: URL(fileURLWithPath: self.videoOutputFullFileName!), fileType: AVFileTypeMPEG4)
      } catch let error as NSError {
          print("ERROR:::::&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;Cannot init videoWriter, error:\(error.localizedDescription)")
      }
  
      guard let videoWriter = self.videoWriter else {
          print("ERROR:::No video writer")
          return
      }
  
      if videoWriter.canAdd(videoWriterInput) {
          videoWriter.add(videoWriterInput)
      } else {
          print("ERROR:::Cannot add videoWriterInput into videoWriter")
      }
  
      //Add audio input
      if videoWriter.canAdd(audioWriterInput) {
          videoWriter.add(audioWriterInput)
      } else {
          print("ERROR:::Cannot add audioWriterInput into videoWriter")
      }
  
      if videoWriter.status != AVAssetWriterStatus.writing {
          print("DEBUG::::::::::::::::The videoWriter status is not writing, and will start writing the video.")
  
          let hasStartedWriting = videoWriter.startWriting()
          if hasStartedWriting {
              videoWriter.startSession(atSourceTime: self.lastSampleTime)
              print("DEBUG:::Have started writting on videoWriter, session at source time: \(self.lastSampleTime)")
              LOG(videoWriter.status.rawValue)
          } else {
              print("WARN:::Fail to start writing on videoWriter")
          }
      } else {
          print("WARN:::The videoWriter.status is writing now, so cannot start writing action on videoWriter")
      }
  </code></pre>
  
  <p>And then saving and finishing writing at the end of the stream:</p>
  
  <pre><code>    print("DEBUG::: Starting to process recorder final...")
      print("DEBUG::: videoWriter status: \(self.videoWriter!.status.rawValue)")
      self.isRecordingVideo = false
  
      guard let videoWriterInput = self.videoWriterInput else {
          print("ERROR:::No video writer input")
          return
      }
      guard let videoWriter = self.videoWriter else {
          print("ERROR:::No video writer")
          return
      }
  
      guard let audioWriterInput = self.audioWriterInput else {
          print("ERROR:::No audio writer input")
          return
      }
  
      videoWriterInput.markAsFinished()
      audioWriterInput.markAsFinished()
      videoWriter.finishWriting {
          if videoWriter.status == AVAssetWriterStatus.completed {
              print("DEBUG:::The videoWriter status is completed")
  
              let fileManager = FileManager.default
              if fileManager.fileExists(atPath: self.videoOutputFullFileName!) {
                  print("DEBUG:::The file: \(self.videoOutputFullFileName ?? "") has been saved in documents folder, and is ready to be moved to camera roll")
  
  
                  let sharedFileURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.jp.awalker.co.Hotter")
                  guard let documentsPath = sharedFileURL?.path else {
                      LOG("ERROR:::No shared file URL path")
                      return
                  }
                  let finalFilename = documentsPath.stringByAppendingPathComponent(str: "test_capture_video.mp4")
  
                  //Check whether file exists
                  if fileManager.fileExists(atPath: finalFilename) {
                      print("WARN:::The file: \(finalFilename) exists, will delete the existing file")
                      do {
                          try fileManager.removeItem(atPath: finalFilename)
                      } catch let error as NSError {
                          print("WARN:::Cannot delete existing file: \(finalFilename), error: \(error.debugDescription)")
                      }
                  } else {
                      print("DEBUG:::The file \(self.videoOutputFullFileName!) doesn't exist")
                  }
  
                  do {
                      try fileManager.copyItem(at: URL(fileURLWithPath: self.videoOutputFullFileName!), to: URL(fileURLWithPath: finalFilename))
                  }
                  catch let error as NSError {
                      LOG("ERROR:::\(error.debugDescription)")
                  }
  
                  PHPhotoLibrary.shared().performChanges({
                      PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(fileURLWithPath: finalFilename))
                  }) { completed, error in
                      if completed {
                          print("Video \(self.videoOutputFullFileName ?? "") has been moved to camera roll")
                      }
  
                      if error != nil {
                          print ("ERROR:::Cannot move the video \(self.videoOutputFullFileName ?? "") to camera roll, error: \(error!.localizedDescription)")
                      }
                  }
  
              } else {
                  print("ERROR:::The file: \(self.videoOutputFullFileName ?? "") doesn't exist, so can't move this file camera roll")
              }
          } else {
              print("WARN:::The videoWriter status is not completed, stauts: \(videoWriter.status)")
          }
      }
  </code></pre>
  
  <p>The problem I'm having is the finishWriting completion code is never being reached. The writer stays in "writing" status therefore the video file is not saved.</p>
  
  <p>If I remove the "finishWriting" line and just leave the completion code to run, a file is being saved, but not properly finished and when I'm trying to view it it's unplayable because it's probably missing metadata.</p>
  
  <p>Is there any other way to do this? I don't want to actually start capturing using AVKit to save the recording, because it's taking too much of the CPU and the RPBroadcastSampleHandler's CMSampleBuffer already has the video data, but maybe using AVKit at all is a wrong move here?</p>
  
  <p>What should I change? How do I save the video from that CMSampleBuffer?</p>