 		rtcMediaStream = self.rtcPeerConnectionFactory.mediaStream(withStreamId: UUID().uuidString)
 
 		if videoRequested == true {
+			// No specific video device requested.
+			if videoDeviceId == nil {
+				NSLog("PluginGetUserMedia#call() | video requested (device not specified)")
+				videoDevice = AVCaptureDevice.DiscoverySession.init(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera, AVCaptureDevice.DeviceType.builtInDualCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.front).devices[0]
+			}
 			// Video device specified.
 			if videoDeviceId != nil {
 				NSLog("PluginGetUserMedia#call() | video requested (specified device id: '%@')", String(videoDeviceId!))
 				let videoDevices: [AVCaptureDevice] = AVCaptureDevice.DiscoverySession.init(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera, AVCaptureDevice.DeviceType.builtInDualCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified).devices
 				for device: AVCaptureDevice in videoDevices {
 					if device.uniqueID == videoDeviceId {
-						if device.position == AVCaptureDevice.Position.back {
-							usingFront = false
-							break
-						}
+						videoDevice = device
 					}
 				}
 			}
 
-			NSLog("PluginGetUserMedia#call() | chosen video device usingFront: %@", String(describing: usingFront))
+			if videoDevice == nil {
+				NSLog("PluginGetUserMedia#call() | video requested but no suitable device found")
+				
+				errback("no suitable camera device found")
+				return
+			}
 
+			NSLog("PluginGetUserMedia#call() | chosen video device: %@", String(describing: videoDevice!))
+			
 			if videoMinWidth > 0 {
 				NSLog("PluginGetUserMedia#call() | adding media constraint [minWidth:%@]", String(videoMinWidth))
 				mandatoryConstraints["minWidth"] = String(videoMinWidth)

import Foundation
import AVFoundation


class PluginGetUserMedia {
	static var rtcVideoSource: RTCAVFoundationVideoSource?
	let kARDMediaStreamId = "ARDAMS";
	let kARDAudioTrackId = "ARDAMSa0";
	let kARDVideoTrackId = "ARDAMSv0";
	let kARDVideoTrackKind = "video";

	var rtcPeerConnectionFactory: RTCPeerConnectionFactory


	init(rtcPeerConnectionFactory: RTCPeerConnectionFactory) {
		NSLog("PluginGetUserMedia#init()")

		self.rtcPeerConnectionFactory = rtcPeerConnectionFactory
	}


	deinit {
		NSLog("PluginGetUserMedia#deinit()")
	}


	func call(
		_ constraints: NSDictionary,

				optionalConstraints: [:]
			)

			rtcVideoSource = self.rtcPeerConnectionFactory.videoSource()
			
			// If videoSource state is "ended" it means that constraints were not satisfied so
			// invoke the given errback.
			if (rtcVideoSource!.state == RTCSourceState.ended) {
				NSLog("PluginGetUserMedia() | rtcVideoSource.state is 'ended', constraints not satisfied")

				errback("constraints not satisfied")
				return
			}

			rtcVideoTrack = self.rtcPeerConnectionFactory.videoTrack(with: rtcVideoSource!,trackId: UUID().uuidString)
			
			#if !TARGET_IPHONE_SIMULATOR
				rtcVideoCapturer = RTCCameraVideoCapturer(delegate: rtcVideoSource!)

				// TODO use constraints
				let formats: [AVCaptureDevice.Format] = RTCCameraVideoCapturer.supportedFormats(for: videoDevice!);
				
				rtcVideoCapturer?.startCapture(
					with: videoDevice!,
					format: formats[0],
					fps: Int(videoMaxFrameRate)
				)
			#endif

			rtcMediaStream.addVideoTrack(rtcVideoTrack!)
		}
