Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
WebRTC Filter Example
- (instancetype)initWithSource:(RTCVideoSource *)source {
if (self = [super init]) {
self.source = source;
// CI ContextとColorSpaceを準備しておく
self.context =
[CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer: @NO}];
_colorSpace = CGColorSpaceCreateDeviceRGB();
self.filters = @[].mutableCopy;
}
return self;
}
- (void)capturer:(RTCVideoCapturer *)capturer didCaptureVideoFrame:(RTCVideoFrame *)frame {
// RTCVideoFrameから次のようにCVPixelBufferRefを取得できる
CVPixelBufferRef pixelBuffer = [frame nativeHandle];
// CVPixelBufferを編集する際のお決まりの処理
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
CIImage *inputImage =
[CIImage imageWithCVPixelBuffer:pixelBuffer
options:@{kCIImageColorSpace: (__bridge id)_colorSpace}];
CIImage *outputImage = [self filterImage:inputImage];
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
[self.context render:outputImage
toCVPixelBuffer:pixelBuffer
bounds:[outputImage extent]
colorSpace:_colorSpace];
[self.source capturer:capturer didCaptureVideoFrame:frame];
}
- (CIImage *)filterImage:(CIImage *)inputImage {
 // あとはここで好きなようにCIFilter等を使ってフィルタ処理を書いていく
return outputImage;
}
- (void)dealloc {
CGColorSpaceRelease(_colorSpace);
self.source = nil;
self.context = nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment