RPScreenRecorder startCapture issues on generated file

Hello all,

This is my first post on the developer forums.

I am developing an app that records the screen of my app, using AVAssetWriter and RPScreenRecorder startCapture.

Everything is working as it should on most cases. There are some seemingly random times where the file generated is of some kb and it is corrupted. There seems to be no pattern on what the device is or the iOS version is. It can happen on various phones and iOS versions.

The steps I have followed in order to create the file are:

  • configuring the AssetWritter
        videoAssetWriter = try? AVAssetWriter(outputURL: url!, fileType: AVFileType.mp4)
        
        let size = UIScreen.main.bounds.size
        let width = (Int(size.width / 4)) * 4
        let height = (Int(size.height / 4)) * 4
        
        let videoOutputSettings: Dictionary<String, Any> = [
            AVVideoCodecKey : AVVideoCodecType.h264,
            AVVideoWidthKey : width,
            AVVideoHeightKey : height
        ]
        
        videoInput = AVAssetWriterInput(mediaType: AVMediaType.video, outputSettings: videoOutputSettings)
        
        videoInput?.expectsMediaDataInRealTime = true
        
        guard let videoInput = videoInput else { return }
        
        if videoAssetWriter?.canAdd(videoInput) ?? false {
            videoAssetWriter?.add(videoInput)
        }
        
        let audioInputsettings = [
            AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
            AVSampleRateKey: 12000,
            AVNumberOfChannelsKey: 1,
            AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
        ]
        
        audioInput = AVAssetWriterInput(mediaType: .audio, outputSettings: audioInputsettings)
        
        audioInput?.expectsMediaDataInRealTime = true
        
        guard let audioInput = audioInput else { return }
        
        if videoAssetWriter?.canAdd(audioInput) ?? false {
            videoAssetWriter?.add(audioInput)
        }

The urlForVideo returns the URL to the documentDirectory, after appending and creating the folders needed. This part seems to be working as it should as the directories are created and the video file exists on them.

  • Start the recording
if RPScreenRecorder.shared().isRecording { return } 
        RPScreenRecorder.shared().startCapture(handler: { [weak self] sample, bufferType, error in
            if let error = error {
                onError?(error.localizedDescription)
            } else {
                if (!RPScreenRecorder.shared().isMicrophoneEnabled) {
                    RPScreenRecorder.shared().stopCapture { error in
                        if let error = error { return }
                    }
                    onError?("Microphone was not enabled")
                }
                else {
                    succesCompletion?()
                    succesCompletion = nil
                    self?.processSampleBuffer(sample, with: bufferType)
                }
            }
        }) { error in
            if let error = error {
                onError?(error.localizedDescription)
            }
        }
  • Process the sampleBuffers
guard CMSampleBufferDataIsReady(sampleBuffer) else { return }
        DispatchQueue.main.async { [weak self] in
            switch sampleBufferType {
            case .video:
                self?.handleVideoBaffer(sampleBuffer)
            case .audioMic:
                self?.add(sample: sampleBuffer, to: self?.audioInput)
self?.audioInput)
            default:
                break
            }
        }

// The add function from above
fileprivate func add(sample: CMSampleBuffer, to writerInput: AVAssetWriterInput?) {
        if writerInput?.isReadyForMoreMediaData ?? false {
            writerInput?.append(sample)
        }

// The handleVideoBaffer function from above
fileprivate func handleVideoBaffer(_ sampleBuffer: CMSampleBuffer) {
        if self.videoAssetWriter?.status == AVAssetWriter.Status.unknown {
            self.videoAssetWriter?.startWriting()
            self.videoAssetWriter?.startSession(atSourceTime: CMSampleBufferGetPresentationTimeStamp(sampleBuffer))
        } else {
            if (self.videoInput?.isReadyForMoreMediaData) ?? false {
                
                if self.videoAssetWriter?.status == AVAssetWriter.Status.writing {
                    self.videoInput?.append(sampleBuffer)
                }
            }
        }
    }
    }
  • Finally the stop recording
func stopRecording(completion: @escaping (URL?, URL?, Error?) -> Void) {
        RPScreenRecorder.shared().stopCapture { error in
            if let error = error {
                completion(nil, nil, error)
                return
            }
            self.finish { videoURL, _ in
                completion(videoURL, nil, nil)
            }
        }
    }

// The finish function mentioned above
fileprivate func finish(completion: @escaping (URL?, URL?) -> Void) {
        let dispatchGroup = DispatchGroup()
        
        dispatchGroup.enter()
        
        finishRecordVideo {
            dispatchGroup.leave()
        }
        
        
        dispatchGroup.notify(queue: .main) {
            print("Finish with url:\(String(describing: self.urlForVideo()))")
            completion(self.urlForVideo(), nil)
        }
    }

// The finishRecordVideo mentioned above
fileprivate func finishRecordVideo(completion: @escaping ()-> Void) {
        videoInput?.markAsFinished()
        
        audioInput?.markAsFinished()
        
        videoAssetWriter?.finishWriting {
            if let writer = self.videoAssetWriter {
                
                if writer.status == .completed {
                    completion()
                }
                else if writer.status == .failed {
                    // Print the error to find out what went wrong
                    if let error = writer.error {
                        print("Video asset writing failed with error: \(error.localizedDescription). Url: \(writer.outputURL.path)")
                    } else {
                        print("Video asset writing failed, but no error description available.")
                    }
                    completion()
                }else {
                    completion()
                }
            }
        }
    }

What could it be the reason of the corrupted files generated? This issue has never happened to my devices so there is no way to debug using xcode. Also there are no errors popping out on the logs.

Can you spot any issues on the code that can create this kind of issue? Do you have any suggestions on the problem at hand?

Thanks

RPScreenRecorder startCapture issues on generated file
 
 
Q