微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AVAssetWriter 中断 TCP 连接

如何解决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 举报,一经查实,本站将立刻删除。