Swift

Criar o Capturador de Tela

O SDK da Vonage espera um capturador de vídeo personalizado que esteja em conformidade com OTVideoCapture. Criar um novo arquivo ScreenCapturer.swift e implementar:

  1. Armazene uma referência à visualização a ser capturada – embora nosso aplicativo tenha sido desenvolvido em SwiftUI, a maneira mais simples de fazer isso é com UIView que passaremos para o Capturer; alternativamente, isso pode ser feito com um UIViewRepresentable wrapper.
  2. Renderize a visualização periodicamente – usar drawHierarchy(in:afterScreenUpdates:).
  3. Converter para CVPixelBuffer / OTVideoFrame – e passar os quadros para videoCaptureConsumer.

Capture uma imagem

import Foundation
import UIKit
import OpenTok

class ScreenCapturer: NSObject, OTVideoCapture {
    var videoContentHint: OTVideoContentHint
    var videoCaptureConsumer: OTVideoCaptureConsumer?

    private let captureView: UIView
    private let captureQueue = DispatchQueue(label: "screen-capture")
    private var timer: DispatchSourceTimer
    private var capturing = false
    private var videoFrame: OTVideoFrame
    private var pixelBuffer: CVPixelBuffer?

    init(withView view: UIView) {
        self.videoContentHint = .none
        self.captureView = view
        self.timer = DispatchSource.makeTimerSource(flags: .strict, queue: captureQueue)
        self.videoFrame = OTVideoFrame(format: OTVideoFormat(argbWithWidth: 0, height: 0))
    }

    private func captureFrame() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(captureView.bounds.size, false, 0)
        defer { UIGraphicsEndImageContext() }
        captureView.drawHierarchy(in: captureView.bounds, afterScreenUpdates: false)
        return UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
    }

    // Implement initCapture, start, stop, releaseCapture, isCaptureStarted, captureSettings
    // and feed frames to videoCaptureConsumer. See ScreenCapturer.swift for full implementation.
}

Copie o texto completo ScreenCapturer.swift implementação deste projeto de exemplo. Ela lida com:

  • Captura programada a cerca de 10 fps
  • Redimensionamento e preenchimento para compatibilidade com o codificador
  • CVPixelBuffer criação e OTVideoFrame entrega