-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
PluginEnumerateDevices.swift
164 lines (134 loc) · 4.95 KB
/
PluginEnumerateDevices.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import Foundation
import AVFoundation
/**
* Doc: https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVCaptureDevice_Class/index.html
*/
struct MediaDeviceInfo {
let deviceId, groupId, kind, label: String
init(deviceId: String, kind: String, label: String) {
self.deviceId = deviceId
self.groupId = ""
self.kind = kind
self.label = label
}
}
// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertFromAVMediaType(_ input: AVMediaType) -> String {
return input.rawValue
}
class PluginEnumerateDevices {
class func call(_ callback: (_ data: NSDictionary) -> Void) {
NSLog("PluginEnumerateDevices#call()")
let audioDevices: [MediaDeviceInfo] = getAllAudioDevices()
let videoDevices: [MediaDeviceInfo] = getAllVideoDevices()
let allDevices = videoDevices + audioDevices;
let json: NSMutableDictionary = [
"devices": NSMutableArray()
]
// Casting to NSMutableDictionary
for device: MediaDeviceInfo in allDevices {
(json["devices"] as! NSMutableArray).add([
"deviceId": device.deviceId,
"kind": device.kind,
"label": device.label,
"groupId": device.groupId
])
}
print("DEVICES => ", json)
callback(json as NSDictionary)
}
}
fileprivate func getAllVideoDevices() -> [MediaDeviceInfo] {
var videoDevicesArr : [MediaDeviceInfo] = []
var deviceTypes: [AVCaptureDevice.DeviceType] = [.builtInTelephotoCamera, .builtInWideAngleCamera]
if #available(iOS 10.2, *) {
deviceTypes.append(.builtInDualCamera)
// Disabled tp prevent duplicate front camera
//if #available(iOS 11.1, *) {
// deviceTypes.append(.builtInTrueDepthCamera)
//}
}
let videoDevices: [AVCaptureDevice] = AVCaptureDevice.DiscoverySession.init(
deviceTypes: deviceTypes,
mediaType: AVMediaType.video,
position: AVCaptureDevice.Position.unspecified
).devices
for device: AVCaptureDevice in videoDevices {
var facing: String
var facingLabel: String;
let hasAudio = device.hasMediaType(AVMediaType(rawValue: convertFromAVMediaType(AVMediaType.audio)))
let hasVideo = device.hasMediaType(AVMediaType(rawValue: convertFromAVMediaType(AVMediaType.video)))
switch device.position {
case AVCaptureDevice.Position.unspecified:
facing = "unknown"
facingLabel = "";
case AVCaptureDevice.Position.back:
facing = "back"
facingLabel = "Back Camera"
case AVCaptureDevice.Position.front:
facing = "front"
facingLabel = "Front Camera"
}
if device.isConnected == false || (hasAudio == false && hasVideo == false) {
continue
}
NSLog("- device [uniqueID:'%@', localizedName:'%@', facing:%@, audio:%@, video:%@, connected:%@]",
String(device.uniqueID), String(device.localizedName), String(facing),
String(hasAudio), String(hasVideo), String(device.isConnected))
if hasAudio == false {
// Add English facingLabel suffix if localizedName does not match for facing detection using label
let deviceLabel = device.localizedName.contains(facingLabel) ?
device.localizedName : device.localizedName + " (" + facingLabel + ")";
let device = MediaDeviceInfo(
deviceId: device.uniqueID,
kind: "videoinput",
label: deviceLabel
)
// Put Front devices at beginning of the videoDevicesArr
if (facing == "front") {
// Simple Swift 4 array unshift
let videoDevicesArrFirst : [MediaDeviceInfo] = [device]
videoDevicesArr = videoDevicesArrFirst + videoDevicesArr;
} else {
videoDevicesArr.append(device)
}
}
}
return videoDevicesArr
}
// Getter function inserted by get all audio devices connected
fileprivate func getAllAudioDevices() -> [MediaDeviceInfo] {
let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
var audioDevicesArr : [MediaDeviceInfo] = []
let audioInputDevices: [AVAudioSessionPortDescription] = audioSession.availableInputs!
var bluetoothDevice: AVAudioSessionPortDescription? = nil
var wiredDevice: AVAudioSessionPortDescription? = nil
var builtMicDevice: AVAudioSessionPortDescription? = nil
for audioInput in audioInputDevices {
let device = MediaDeviceInfo(
deviceId: audioInput.uid,
kind: "audioinput",
label: audioInput.portName
);
audioDevicesArr.append(device)
// Initialize audioInputSelected. Default Built-In Microphone
if audioInput.portType == AVAudioSession.Port.builtInMic {
builtMicDevice = audioInput
}
if audioInput.portType == .bluetoothHFP || audioInput.portType == .bluetoothA2DP {
bluetoothDevice = audioInput
}
if audioInput.portType == .usbAudio || audioInput.portType == .headsetMic {
wiredDevice = audioInput
}
}
// Initialize audioInputSelected. Priority: [Wired - Wireless - Built-In Microphone]
if wiredDevice != nil {
PluginRTCAudioController.saveInputAudioDevice(inputDeviceUID: wiredDevice!.uid)
} else if bluetoothDevice != nil {
PluginRTCAudioController.saveInputAudioDevice(inputDeviceUID: bluetoothDevice!.uid)
} else if builtMicDevice != nil {
PluginRTCAudioController.saveInputAudioDevice(inputDeviceUID: builtMicDevice!.uid)
}
return audioDevicesArr
}