isReadyForMoreMediaData and AVAssetWriterInput

271 views Asked by At

I created a screen recording app.

Before appending the buffer to the writer, I check if the isReadyForMoreMediaData is true.

But sometimes it will constantly return false and won’t become true until I call markAsFinished

I have no idea how to fix this.

let videoCompressionProperties = [

    AVVideoAverageBitRateKey: resolution.dataRate

]

let videoSettings: [String: Any] = [

    AVVideoCodecKey:                  encoding,

    AVVideoWidthKey:                  resolution.size.width,

    AVVideoHeightKey:                 resolution.size.height,

    AVVideoCompressionPropertiesKey:  videoCompressionProperties,

    AVVideoScalingModeKey:            AVVideoScalingModeResizeAspect,

    AVVideoColorPropertiesKey:        colorSpace.properties.dictionary,

]

videoInput = AVAssetWriterInput(mediaType: .video, outputSettings: videoSettings)
videoInput?.expectsMediaDataInRealTime = true

Append buffers:

if videoInput?.isReadyForMoreMediaData == true {
    guard buffer.imageBuffer != nil else { return }
    processQueue.async { [self] in
        videoInput?.append(buffer)
    }
} else {
    logger.warning("Dropped a frame.")
    // I found this in stackoverflow. But no effect
    // RunLoop.current.run(until: .now.addingTimeInterval(0.1))
    // The code below will notify me when the bug occurrs.
    // errorFixedPublisher.send()
    // if !self.errorOccured {
    //     self.errorOccured = true
    //     let center = UNUserNotificationCenter.current()
    //     let content = UNMutableNotificationContent()
    //     content.sound = UNNotificationSound.defaultCritical
    //     content.badge = 1
    //     content.body = "Dropping frames at \(Date.now.formatted(date: .omitted, time: .standard))"
    //     let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
    //     let req = UNNotificationRequest(identifier: "ASWRI_ERR", content: content, trigger: trigger)
    //     center.add(req)
    }
}

Any idea on it?? Please help me.

Have a good day!

0

There are 0 answers