From c62b0b916929a7439d8b0b9b62461a5848515868 Mon Sep 17 00:00:00 2001 From: tkmn0 Date: Sun, 27 Oct 2019 23:09:56 +0900 Subject: [PATCH] feat: add filters and change function --- SimpleWebRTC/CameraFilter/CameraFilter.swift | 46 ++++++++++++++++--- .../ViewController/ViewController.swift | 13 ++++++ SimpleWebRTC/WebRTC/WebRTCClient.swift | 2 +- 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/SimpleWebRTC/CameraFilter/CameraFilter.swift b/SimpleWebRTC/CameraFilter/CameraFilter.swift index 2bb22a1..a34dd15 100644 --- a/SimpleWebRTC/CameraFilter/CameraFilter.swift +++ b/SimpleWebRTC/CameraFilter/CameraFilter.swift @@ -10,29 +10,41 @@ import Foundation import AVFoundation import CoreImage +enum FilterType: Int{ + case None + case Sepia + case Monochrome + case ColorControls + + mutating func next() -> FilterType{ + return FilterType(rawValue: rawValue + 1) ?? FilterType(rawValue: 0)! + } +} + class CameraFilter { - private let filter: CIFilter + private var filter: CIFilter private let context: CIContext + var filterType: FilterType = FilterType.None init() { - self.filter = CIFilter(name: "CISepiaTone")! + self.filter = CIFilter() self.context = CIContext() } func apply(_ sampleBuffer: CVPixelBuffer) -> CVPixelBuffer?{ - let ciimage = CIImage(cvPixelBuffer: sampleBuffer) - self.filter.setValue(ciimage, forKey: kCIInputImageKey) - self.filter.setValue(0.8, forKey: "inputIntensity") + if self.filterType == .None{ return sampleBuffer } + let ciimage = CIImage(cvPixelBuffer: sampleBuffer) let size: CGSize = ciimage.extent.size + self.filter.setValue(ciimage, forKey: kCIInputImageKey) let filtered = self.filter.outputImage var pixelBuffer: CVPixelBuffer? = nil let options = [ - kCVPixelBufferCGImageCompatibilityKey as String: kCFBooleanTrue, - kCVPixelBufferCGBitmapContextCompatibilityKey as String: kCFBooleanTrue + kCVPixelBufferCGImageCompatibilityKey as String: kCFBooleanTrue as Any, + kCVPixelBufferCGBitmapContextCompatibilityKey as String: kCFBooleanTrue as Any ] as [String : Any] let status: CVReturn = CVPixelBufferCreate(kCFAllocatorDefault, @@ -47,4 +59,24 @@ class CameraFilter { } return pixelBuffer } + + func changeFilter(_ filterType: FilterType){ + switch filterType { + case .Sepia: + self.filter = CIFilter(name: "CISepiaTone")! + self.filter.setValue(0.8, forKey: "inputIntensity") + case .Monochrome: + self.filter = CIFilter(name: "CIColorMonochrome")! + self.filter.setValue(CIColor(red: 0.75, green: 0.75, blue: 0.75), forKey: "inputColor") + self.filter.setValue(1.0, forKey: "inputIntensity") + case .ColorControls: + self.filter = CIFilter(name: "CIColorControls" )! + self.filter.setValue(1.0, forKey: "inputSaturation") + self.filter.setValue(0.5, forKey: "inputBrightness") + self.filter.setValue(3.0, forKey: "inputContrast") + case .None: + break + } + self.filterType = filterType + } } diff --git a/SimpleWebRTC/ViewController/ViewController.swift b/SimpleWebRTC/ViewController/ViewController.swift index 80c443f..2bbe04e 100644 --- a/SimpleWebRTC/ViewController/ViewController.swift +++ b/SimpleWebRTC/ViewController/ViewController.swift @@ -107,8 +107,14 @@ class ViewController: UIViewController, WebSocketDelegate, WebRTCClientDelegate, let localVideoView = webRTCClient.localVideoView() webRTCClient.setupLocalViewFrame(frame: CGRect(x: 0, y: 0, width: ScreenSizeUtil.width()/3, height: ScreenSizeUtil.height()/3)) localVideoView.center.y = self.view.center.y + localVideoView.subviews.last?.isUserInteractionEnabled = true self.view.addSubview(localVideoView) + let localVideoViewButton = UIButton(frame: CGRect(x: 0, y: 0, width: localVideoView.frame.width, height: localVideoView.frame.height)) + localVideoViewButton.backgroundColor = UIColor.clear + localVideoViewButton.addTarget(self, action: #selector(self.localVideoViewTapped(_:)), for: .touchUpInside) + localVideoView.addSubview(localVideoViewButton) + let likeButton = UIButton(frame: CGRect(x: remoteVideoViewContainter.right - 50, y: remoteVideoViewContainter.bottom - 50, width: 40, height: 40)) likeButton.backgroundColor = UIColor.clear likeButton.addTarget(self, action: #selector(self.likeButtonTapped(_:)), for: .touchUpInside) @@ -191,6 +197,13 @@ class ViewController: UIViewController, WebSocketDelegate, WebRTCClientDelegate, @objc func likeButtonTapped(_ sender: UIButton){ let data = likeStr.data(using: String.Encoding.utf8) webRTCClient.sendData(data: data!) + self.cameraFilter?.changeFilter((cameraFilter?.filterType.next())!) + } + + @objc func localVideoViewTapped(_ sender: UITapGestureRecognizer) { + if let filter = self.cameraFilter { + filter.changeFilter(filter.filterType.next()) + } } private func startLikeAnimation(){ diff --git a/SimpleWebRTC/WebRTC/WebRTCClient.swift b/SimpleWebRTC/WebRTC/WebRTCClient.swift index d4eba18..fc45273 100644 --- a/SimpleWebRTC/WebRTC/WebRTCClient.swift +++ b/SimpleWebRTC/WebRTC/WebRTCClient.swift @@ -470,7 +470,7 @@ extension WebRTCClient{ } if(isLandScape){ - let ratio = size.width/size.height + let ratio = size.width / size.height _renderView.frame = CGRect(x: 0, y: 0, width: _parentView.frame.height * ratio, height: _parentView.frame.height) _renderView.center.x = _parentView.frame.width/2 }else{