Swift
Create the Screen Capturer
The Vonage SDK expects a custom video capturer that conforms to OTVideoCapture. Create a new file ScreenCapturer.swift and implement:
- Store a reference to the view to capture – even though our app is set up in SwiftUI, the simplest way to do it is with
UIViewwhich we will pass to the Capturer, alternatively this can be done with aUIViewRepresentablewrapper. - Render the view periodically – use
drawHierarchy(in:afterScreenUpdates:). - Convert to
CVPixelBuffer/OTVideoFrame– and pass frames tovideoCaptureConsumer.
Capture a view
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.
}
Copy the full ScreenCapturer.swift implementation from this sample project. It handles:
- Timer-based capture at ~10 fps
- Resizing and padding for encoder compatibility
CVPixelBuffercreation andOTVideoFramedelivery
Screen sharing
Learn how to implement tha screensharing capability using the Vonage Video API platform.
手順
1
Introduction2
Getting Started3
Creating a New Project4
Adding the Vonage Video SDK5
Setting Up Authentication6
Create the Screen Capturer7
Expose the Root View for FullScreen Capture8
Expose the Root View for FullScreen Capture9
Summary10
Conclusion