Skip to content

Commit

Permalink
feat: add filters and change function
Browse files Browse the repository at this point in the history
  • Loading branch information
tkmn0 committed Oct 27, 2019
1 parent 59b1269 commit c62b0b9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
46 changes: 39 additions & 7 deletions SimpleWebRTC/CameraFilter/CameraFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
}
13 changes: 13 additions & 0 deletions SimpleWebRTC/ViewController/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(){
Expand Down
2 changes: 1 addition & 1 deletion SimpleWebRTC/WebRTC/WebRTCClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit c62b0b9

Please sign in to comment.