|
| 1 | +// |
| 2 | +// MyVideoCapture.swift |
| 3 | +// Agora-Video-Source |
| 4 | +// |
| 5 | +// Created by GongYuhua on 2017/4/11. |
| 6 | +// Copyright © 2017年 Agora. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import AVFoundation |
| 11 | + |
| 12 | +protocol AgoraCameraSourcePushDelegate { |
| 13 | + func myVideoCapture(_ capture: AgoraCameraSourcePush, didOutputSampleBuffer pixelBuffer: CVPixelBuffer, rotation: Int, timeStamp: CMTime) |
| 14 | +} |
| 15 | + |
| 16 | +enum Camera: Int { |
| 17 | + case front = 1 |
| 18 | + case back = 0 |
| 19 | + |
| 20 | + static func defaultCamera() -> Camera { |
| 21 | + return .front |
| 22 | + } |
| 23 | + |
| 24 | + func next() -> Camera { |
| 25 | + switch self { |
| 26 | + case .back: return .front |
| 27 | + case .front: return .back |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +class AgoraCameraSourcePush: NSObject { |
| 33 | + |
| 34 | + fileprivate var delegate: AgoraCameraSourcePushDelegate? |
| 35 | + private var videoView: CustomVideoSourcePreview |
| 36 | + |
| 37 | + private var currentCamera = Camera.defaultCamera() |
| 38 | + private let captureSession: AVCaptureSession |
| 39 | + private let captureQueue: DispatchQueue |
| 40 | + private var currentOutput: AVCaptureVideoDataOutput? { |
| 41 | + if let outputs = self.captureSession.outputs as? [AVCaptureVideoDataOutput] { |
| 42 | + return outputs.first |
| 43 | + } else { |
| 44 | + return nil |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + init(delegate: AgoraCameraSourcePushDelegate?, videoView: CustomVideoSourcePreview) { |
| 49 | + self.delegate = delegate |
| 50 | + self.videoView = videoView |
| 51 | + |
| 52 | + captureSession = AVCaptureSession() |
| 53 | + captureSession.usesApplicationAudioSession = false |
| 54 | + |
| 55 | + let captureOutput = AVCaptureVideoDataOutput() |
| 56 | + captureOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] |
| 57 | + if captureSession.canAddOutput(captureOutput) { |
| 58 | + captureSession.addOutput(captureOutput) |
| 59 | + } |
| 60 | + |
| 61 | + captureQueue = DispatchQueue(label: "MyCaptureQueue") |
| 62 | + |
| 63 | + let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) |
| 64 | + videoView.insertCaptureVideoPreviewLayer(previewLayer: previewLayer) |
| 65 | + } |
| 66 | + |
| 67 | + deinit { |
| 68 | + captureSession.stopRunning() |
| 69 | + } |
| 70 | + |
| 71 | + func startCapture(ofCamera camera: Camera) { |
| 72 | + guard let currentOutput = currentOutput else { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + currentCamera = camera |
| 77 | + currentOutput.setSampleBufferDelegate(self, queue: captureQueue) |
| 78 | + |
| 79 | + captureQueue.async { [weak self] in |
| 80 | + guard let strongSelf = self else { |
| 81 | + return |
| 82 | + } |
| 83 | + strongSelf.changeCaptureDevice(toIndex: camera.rawValue, ofSession: strongSelf.captureSession) |
| 84 | + strongSelf.captureSession.beginConfiguration() |
| 85 | + if strongSelf.captureSession.canSetSessionPreset(AVCaptureSession.Preset.vga640x480) { |
| 86 | + strongSelf.captureSession.sessionPreset = AVCaptureSession.Preset.vga640x480 |
| 87 | + } |
| 88 | + strongSelf.captureSession.commitConfiguration() |
| 89 | + strongSelf.captureSession.startRunning() |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + func stopCapture() { |
| 94 | + currentOutput?.setSampleBufferDelegate(nil, queue: nil) |
| 95 | + captureQueue.async { [weak self] in |
| 96 | + self?.captureSession.stopRunning() |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + func switchCamera() { |
| 101 | + stopCapture() |
| 102 | + currentCamera = currentCamera.next() |
| 103 | + startCapture(ofCamera: currentCamera) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +private extension AgoraCameraSourcePush { |
| 108 | + func changeCaptureDevice(toIndex index: Int, ofSession captureSession: AVCaptureSession) { |
| 109 | + guard let captureDevice = captureDevice(atIndex: index) else { |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + let currentInputs = captureSession.inputs as? [AVCaptureDeviceInput] |
| 114 | + let currentInput = currentInputs?.first |
| 115 | + |
| 116 | + if let currentInputName = currentInput?.device.localizedName, |
| 117 | + currentInputName == captureDevice.uniqueID { |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + guard let newInput = try? AVCaptureDeviceInput(device: captureDevice) else { |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + captureSession.beginConfiguration() |
| 126 | + if let currentInput = currentInput { |
| 127 | + captureSession.removeInput(currentInput) |
| 128 | + } |
| 129 | + if captureSession.canAddInput(newInput) { |
| 130 | + captureSession.addInput(newInput) |
| 131 | + } |
| 132 | + captureSession.commitConfiguration() |
| 133 | + } |
| 134 | + |
| 135 | + func captureDevice(atIndex index: Int) -> AVCaptureDevice? { |
| 136 | + let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back) |
| 137 | + let devices = deviceDiscoverySession.devices |
| 138 | + |
| 139 | + let count = devices.count |
| 140 | + guard count > 0, index >= 0 else { |
| 141 | + return nil |
| 142 | + } |
| 143 | + |
| 144 | + let device: AVCaptureDevice |
| 145 | + if index >= count { |
| 146 | + device = devices.last! |
| 147 | + } else { |
| 148 | + device = devices[index] |
| 149 | + } |
| 150 | + |
| 151 | + return device |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +extension AgoraCameraSourcePush: AVCaptureVideoDataOutputSampleBufferDelegate { |
| 156 | + func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { |
| 157 | + guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { |
| 158 | + return |
| 159 | + } |
| 160 | + let time = CMSampleBufferGetPresentationTimeStamp(sampleBuffer) |
| 161 | + DispatchQueue.main.async {[weak self] in |
| 162 | + guard let weakSelf = self else { |
| 163 | + return |
| 164 | + } |
| 165 | + |
| 166 | + weakSelf.delegate?.myVideoCapture(weakSelf, didOutputSampleBuffer: pixelBuffer, rotation: 90, timeStamp: time) |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments