AVAssetWriter 中断 TCP 连接

问题描述

我目前正在开展一个项目,其中我们的应用程序通过 TCP(使用 Network -NW- 框架)从 IP 摄像机接收 JPEG 数据并从中构建 CMSampleBuffers。在这个过程之后,我们将它与 AVSampleBufferDisplayerLayer 同时显示。如果用户点击录制按钮,应用程序将使用 AVAssetWriter 开始录制。

在 iOS 14 之前,我们能够每秒记录大约 60 帧。但是现在当我们开始为 AVAssetWriterInput 提供 CMSampleBuffers 时;我们每秒只能通过 TCP 连接接收 30-40 帧。不知何故,使用 AVAssetWriter 开始记录会降低我们通过套接字接收到的包数量。在 iOS 14 之前,我们没有遇到过这样的问题。在录制之前/之后,我们检索的帧数稳定在 60 fps 左右。

知道是什么原因造成的吗?

提前感谢您的帮助!

我们通过 TCP 接收数据的部分:

func setupReceive(_ connection: NWConnection) {
    connection.receive(minimumIncompleteLength: 1,maximumLength: 65536) { (data,contentContext,isComplete,error) in
            if let data = data,!data.isEmpty {
                self.status = "did receive \(data.count) bytes"
                MyIPCameraManager.shared.encodeData(data)
            }
            if isComplete {
                self.stop(status: "EOF")
            } else if let error = error {
                self.connectionDidFail(error)
            } else {
                self.setupReceive(connection)
            }
        }
}

我们初始化 AVAssetWriter 的部分:

assetWriter = try? AVAssetWriter(outputURL: fileUrl,fileType: AVFileType.mp4)
        if (assetWriter == nil) {
            throw MyRecordingError.WriterInitializationError
        }
        var formatDesc: CMVideoFormatDescription? = nil
        CMVideoFormatDescriptionCreate(allocator: kCFAllocatorDefault,codecType: kCMVideoCodecType_JPEG,width: 640,height: 480,extensions: nil,formatDescriptionOut: &formatDesc)
        self.assetWriterInput = AVAssetWriterInput(mediaType: AVMediaType.video,outputSettings: nil,sourceFormatHint: formatDesc)
        self.assetWriterInput.expectsMediaDataInRealTime = true
        self.assetWriter.add(assetWriterInput)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)