Skip to content

Commit

Permalink
Camera changing support (opentok#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
robjperez authored Apr 24, 2017
1 parent 3f79bb4 commit eb42fac
Showing 3 changed files with 108 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12086"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="__Custom_Video_Driver" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="zee-CQ-GLa">
<rect key="frame" x="124" y="617" width="127" height="30"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="pFD-ov-Knc"/>
<constraint firstAttribute="width" constant="127" id="rzH-e2-cxL"/>
</constraints>
<state key="normal" title="Toggle Camera">
<color key="titleColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
<connections>
<action selector="toggleCamera:" destination="BYZ-38-t0r" eventType="touchUpInside" id="84i-Wg-qRC"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="zee-CQ-GLa" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="AuW-yJ-KJF"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="zee-CQ-GLa" secondAttribute="bottom" constant="20" id="g4N-TL-UpI"/>
</constraints>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="32.799999999999997" y="36.431784107946029"/>
</scene>
</scenes>
</document>
Original file line number Diff line number Diff line change
@@ -189,6 +189,77 @@ class ExampleVideoCapture: NSObject, OTVideoCapture {
videoFormat.imageHeight = captureHeight
return 0
}

fileprivate func frontFacingCamera() -> AVCaptureDevice? {
return camera(withPosition: .front)
}

fileprivate func backFacingCamera() -> AVCaptureDevice? {
return camera(withPosition: .back)
}

fileprivate var hasMultipleCameras : Bool {
return AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo).count > 1
}

func setCameraPosition(_ position: AVCaptureDevicePosition) -> Bool {
guard let preset = captureSession?.sessionPreset else {
return false
}

let newVideoInput: AVCaptureDeviceInput? = {
do {
if position == AVCaptureDevicePosition.back {
return try AVCaptureDeviceInput.init(device: backFacingCamera())
} else if position == AVCaptureDevicePosition.front {
return try AVCaptureDeviceInput.init(device: frontFacingCamera())
} else {
return nil
}
} catch {
return nil
}
}()

guard let newInput = newVideoInput else {
return false
}

var success = true

captureQueue.sync {
captureSession?.beginConfiguration()
captureSession?.removeInput(videoInput)

if captureSession?.canAddInput(newInput) ?? false {
captureSession?.addInput(newInput)
videoInput = newInput
} else {
success = false
captureSession?.addInput(videoInput)
}

captureSession?.commitConfiguration()
}

if success {
capturePreset = preset
}

return success
}

func toggleCameraPosition() -> Bool {
guard hasMultipleCameras else {
return false
}

if videoInput?.device.position == .front {
return setCameraPosition(.back)
} else {
return setCameraPosition(.front)
}
}
}

extension ExampleVideoCapture: AVCaptureVideoDataOutputSampleBufferDelegate {
Original file line number Diff line number Diff line change
@@ -102,6 +102,11 @@ class ViewController: UIViewController {
self.present(controller, animated: true, completion: nil)
}
}


@IBAction func toggleCamera(_ sender: Any) {
publisher?.exampleCapturer?.toggleCameraPosition()
}
}

// MARK: - OTSession delegate callbacks

0 comments on commit eb42fac

Please sign in to comment.