mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
48 lines
1.7 KiB
Swift
48 lines
1.7 KiB
Swift
import AVFoundation
|
|
|
|
class CameraInput {
|
|
var videoInput: AVCaptureDeviceInput?
|
|
private var audioInput: AVCaptureDeviceInput?
|
|
|
|
func configure(for session: CameraSession, device: CameraDevice, audio: Bool) {
|
|
if let videoDevice = device.videoDevice {
|
|
self.configureVideoInput(for: session, device: videoDevice)
|
|
}
|
|
if audio, let audioDevice = device.audioDevice {
|
|
self.configureAudioInput(for: session, device: audioDevice)
|
|
}
|
|
}
|
|
|
|
func invalidate(for session: CameraSession) {
|
|
for input in session.session.inputs {
|
|
session.session.removeInput(input)
|
|
}
|
|
}
|
|
|
|
private func configureVideoInput(for session: CameraSession, device: AVCaptureDevice) {
|
|
if let currentVideoInput = self.videoInput {
|
|
session.session.removeInput(currentVideoInput)
|
|
self.videoInput = nil
|
|
}
|
|
if let videoInput = try? AVCaptureDeviceInput(device: device) {
|
|
self.videoInput = videoInput
|
|
if session.session.canAddInput(videoInput) {
|
|
session.session.addInputWithNoConnections(videoInput)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func configureAudioInput(for session: CameraSession, device: AVCaptureDevice) {
|
|
if let currentAudioInput = self.audioInput {
|
|
session.session.removeInput(currentAudioInput)
|
|
self.audioInput = nil
|
|
}
|
|
if let audioInput = try? AVCaptureDeviceInput(device: device) {
|
|
self.audioInput = audioInput
|
|
if session.session.canAddInput(audioInput) {
|
|
session.session.addInput(audioInput)
|
|
}
|
|
}
|
|
}
|
|
}
|