ビデオモディファイアー
ビデオモディファイアー機能を使用すると、ビデオ送信前にローカルビデオフレームを修正できます。この機能により、アプリケーションはリアルタイムエフェクト、フィルター、または変換を送信ビデオストリームに適用できます。
| 対応する通話タイプ | SDKの最低バージョン |
|---|---|
| 1対1通話、グループ通話(カンファレンス) | PlanetKit 7.0 |
Note
- ビデオモディファイアーは、カメラでキャプチャーしたローカルビデオにのみ適用されます。
- ビデオ修正処理時にフレームのドロップや画質低下を防ぐため、演算コストと遅延時間を最小限に抑える必要があります。
活用事例
ビデオモディファイアーの主な活用例は次のとおりです。
- リアルタイムのビデオエフェクトとフィルター
- ビューティーフィルターと顔補正
- 色補正と調整
実装段階
ビデオモディファイアー機能を実装するには、次の手順に従います。
ビデオモディファイアーの実装
ビデオフレームを修正するためにPlanetKitVideoModifierDelegateを実装するクラスを作成します。
import PlanetKit
class VideoEffectModifier: NSObject, PlanetKitVideoModifierDelegate {
func videoOutput(_ videoBuffer: PlanetKitVideoBuffer) {
// Access video buffer properties
let sampleBuffer = videoBuffer.sampleBuffer // CMSampleBuffer?: video sample buffer
let timestamp = videoBuffer.timestamp // CMTime: timestamp
let rotation = videoBuffer.rotation // PlanetKitVideoRotation: rotation angle
let position = videoBuffer.position // PlanetKitCameraPosition: camera position
let source = videoBuffer.source // PlanetKitVideoSource: video source
// Extract pixel buffer from sample buffer
guard let originalSampleBuffer = sampleBuffer,
let pixelBuffer = CMSampleBufferGetImageBuffer(originalSampleBuffer) else {
return
}
// Get video dimensions
let width = CVPixelBufferGetWidth(pixelBuffer)
let height = CVPixelBufferGetHeight(pixelBuffer)
// Apply your custom effect and create a new sample buffer
guard let modifiedSampleBuffer = applyVideoEffect(
to: originalSampleBuffer,
width: width,
height: height
) else {
// If effect fails, keep original buffer
return
}
// Set the modified sample buffer back to the video buffer
videoBuffer.sampleBuffer = modifiedSampleBuffer
}
func didEncounterError(_ error: PlanetKitVideoModifierError) {
// Handle errors during video modification
switch error {
case .videoFrameRejectedByUpscaling:
print("Error: Video frame was upscaled (resolution increased)")
case .videoFrameRejectedByFormatChange:
print("Error: Video frame pixel format was changed")
@unknown default:
print("Error: Unknown video modifier error")
}
}
private func applyVideoEffect(
to sampleBuffer: CMSampleBuffer,
width: Int,
height: Int
) -> CMSampleBuffer? {
// Implement your video effect logic here
// Note: This runs on the camera capture thread, keep processing lightweight
}
}
ビデオモディファイアーの設定
ビデオフレームの修正を開始するには、カメラマネージャーにモディファイアーを登録します。
// Get camera manager instance
let cameraManager = PlanetKitCameraManager.shared
// Create and set the modifier
let videoModifier = VideoEffectModifier()
cameraManager.modifier = videoModifier
ビデオモディファイアーの解除
ビデオフレームの編集を中止するには、モディファイアーの登録を解除します。
let cameraManager = PlanetKitCameraManager.shared
// Clear the modifier by setting it to nil
cameraManager.modifier = nil
関連API
ビデオモディファイアー機能とこれに関連するAPIは以下のとおりです。