The result of camera initialization.
        /// <summary>
        /// Performs the one time initialization and shows the preview (if video).
        /// </summary>
        /// <returns></returns>
        private async Task<CameraInitializationResult> InitializeAsync()
        {
            CameraInitializationResult result;

            try
            {
                Debug.Assert(Dispatcher.HasThreadAccess);

                if (_initializationTaskSource != null)
                {
                    // Already initializing or initialized
                    await _initializationTaskSource.Task;
                    _initializationTaskSource = new TaskCompletionSource<CameraInitializationResult>();
                }

                _internalState = CameraCaptureControlStates.Initializing;
                _initializationTaskSource = new TaskCompletionSource<CameraInitializationResult>();

                if (_currentAudioCaptureDeviceIndex < 0 &&
                    (this.StreamingCaptureMode == StreamingCaptureMode.AudioAndVideo ||
                    this.StreamingCaptureMode == StreamingCaptureMode.Audio))
                {
                    await FindAudioCaptureDevicesAsync();

                    if (_audioCaptureDevices.Length == 0)
                    {
                        if (this.StreamingCaptureMode == StreamingCaptureMode.Audio)
                        {
                            _internalState = CameraCaptureControlStates.Hidden;
                            Debugger.Break();

                            result = new CameraInitializationResult(false, "No audio devices found.");
                            _initializationTaskSource.SetResult(result);

                            return result;
                        }
                    }
                    else if (this.AudioDeviceId != null)
                    {
                        var device = _audioCaptureDevices.FirstOrDefault(d => d.Id == this.AudioDeviceId);

                        if (device != null)
                        {
                            _currentAudioCaptureDeviceIndex = Array.IndexOf(_audioCaptureDevices, device);
                        }
                    }
                    else if (this.PickAudioDeviceAutomatically)
                    {
                        _currentAudioCaptureDeviceIndex = 0;
                    }
                    else
                    {
                        await ShowMicrophoneSelector();
                    }
                }

                if (_currentVideoCaptureDeviceIndex < 0 &&
                    (this.StreamingCaptureMode == StreamingCaptureMode.AudioAndVideo ||
                    this.StreamingCaptureMode == StreamingCaptureMode.Video))
                {
                    await FindVideoCaptureDevicesAsync();

                    if (_videoCaptureDevices.Length == 0)
                    {
                        if (this.StreamingCaptureMode == StreamingCaptureMode.Video)
                        {
                            Debugger.Break();
                            _internalState = CameraCaptureControlStates.Hidden;

                            result = new CameraInitializationResult(false, "No video devices found.");
                            _initializationTaskSource.SetResult(result);

                            return result;
                        }

                        // Audio only
                        result = new CameraInitializationResult(true);
                        _initializationTaskSource.SetResult(result);

                        return result;
                    }

                    if (_videoCaptureDevices.Length == 1)
                    {
                        _currentVideoCaptureDeviceIndex = 0;
                        result = await this.StartPreviewAsync();
                        _initializationTaskSource.SetResult(result);

                        return result;
                    }

                    if (this.VideoDeviceId != null)
                    {
                        var device = _videoCaptureDevices.FirstOrDefault(d => d.Id == this.VideoDeviceId);

                        if (device != null)
                        {
                            _currentVideoCaptureDeviceIndex = Array.IndexOf(_videoCaptureDevices, device);
                            result = await this.StartPreviewAsync();
                            _initializationTaskSource.SetResult(result);

                            return result;
                        }

                        // If requested device was not found - don't give up, but look for another one
                    }

                    if (_preferredVideoCaptureDevice != null)
                    {
                        _currentVideoCaptureDeviceIndex = Array.IndexOf(_videoCaptureDevices, _preferredVideoCaptureDevice);
                        result = await this.StartPreviewAsync();
                        _initializationTaskSource.SetResult(result);

                        return result;
                    }

                    if (PickVideoDeviceAutomatically)
                    {
                        _currentVideoCaptureDeviceIndex = 0;
                        result = await this.StartPreviewAsync();
                        _initializationTaskSource.SetResult(result);

                        return result;
                    }

                    bool success = await ShowWebCamSelector();

                    if (success)
                    {
                        result = new CameraInitializationResult(true);
                        _initializationTaskSource.SetResult(result);

                        return result;
                        
                    }

                    result = new CameraInitializationResult(false, "Unable to select video device.");
                    _initializationTaskSource.SetResult(result);

                    return result;
                }

                // Audio only recording
                result = await this.StartPreviewAsync();
                _initializationTaskSource.SetResult(result);

                return result;
            }
            catch (Exception ex)
            {
                result = new CameraInitializationResult(false, "An unkown error has occured.", ex);

                if (_initializationTaskSource != null)
                {
                    _initializationTaskSource.SetResult(result);
                }

                return result;
            }
        }
        /// <summary>
        /// Performs the one time initialization and shows the preview (if video).
        /// </summary>
        /// <returns></returns>
        private async Task <CameraInitializationResult> InitializeAsync()
        {
            CameraInitializationResult result;

            try
            {
                Debug.Assert(Dispatcher.HasThreadAccess);

                if (_initializationTaskSource != null)
                {
                    // Already initializing or initialized
                    await _initializationTaskSource.Task;
                    _initializationTaskSource = new TaskCompletionSource <CameraInitializationResult>();
                }

                _internalState            = CameraCaptureControlStates.Initializing;
                _initializationTaskSource = new TaskCompletionSource <CameraInitializationResult>();

                if (_currentAudioCaptureDeviceIndex < 0 &&
                    (this.StreamingCaptureMode == StreamingCaptureMode.AudioAndVideo ||
                     this.StreamingCaptureMode == StreamingCaptureMode.Audio))
                {
                    await FindAudioCaptureDevicesAsync();

                    if (_audioCaptureDevices.Length == 0)
                    {
                        if (this.StreamingCaptureMode == StreamingCaptureMode.Audio)
                        {
                            _internalState = CameraCaptureControlStates.Hidden;
                            Debugger.Break();

                            result = new CameraInitializationResult(false, "No audio devices found.");
                            _initializationTaskSource.SetResult(result);

                            return(result);
                        }
                    }
                    else if (this.AudioDeviceId != null)
                    {
                        var device = _audioCaptureDevices.FirstOrDefault(d => d.Id == this.AudioDeviceId);

                        if (device != null)
                        {
                            _currentAudioCaptureDeviceIndex = Array.IndexOf(_audioCaptureDevices, device);
                        }
                    }
                    else if (this.PickAudioDeviceAutomatically)
                    {
                        _currentAudioCaptureDeviceIndex = 0;
                    }
                    else
                    {
                        await ShowMicrophoneSelector();
                    }
                }

                if (_currentVideoCaptureDeviceIndex < 0 &&
                    (this.StreamingCaptureMode == StreamingCaptureMode.AudioAndVideo ||
                     this.StreamingCaptureMode == StreamingCaptureMode.Video))
                {
                    await FindVideoCaptureDevicesAsync();

                    if (_videoCaptureDevices.Length == 0)
                    {
                        if (this.StreamingCaptureMode == StreamingCaptureMode.Video)
                        {
                            Debugger.Break();
                            _internalState = CameraCaptureControlStates.Hidden;

                            result = new CameraInitializationResult(false, "No video devices found.");
                            _initializationTaskSource.SetResult(result);

                            return(result);
                        }

                        // Audio only
                        result = new CameraInitializationResult(true);
                        _initializationTaskSource.SetResult(result);

                        return(result);
                    }

                    if (_videoCaptureDevices.Length == 1)
                    {
                        _currentVideoCaptureDeviceIndex = 0;
                        result = await this.StartPreviewAsync();

                        _initializationTaskSource.SetResult(result);

                        return(result);
                    }

                    if (this.VideoDeviceId != null)
                    {
                        var device = _videoCaptureDevices.FirstOrDefault(d => d.Id == this.VideoDeviceId);

                        if (device != null)
                        {
                            _currentVideoCaptureDeviceIndex = Array.IndexOf(_videoCaptureDevices, device);
                            result = await this.StartPreviewAsync();

                            _initializationTaskSource.SetResult(result);

                            return(result);
                        }

                        // If requested device was not found - don't give up, but look for another one
                    }

                    if (_preferredVideoCaptureDevice != null)
                    {
                        _currentVideoCaptureDeviceIndex = Array.IndexOf(_videoCaptureDevices, _preferredVideoCaptureDevice);
                        result = await this.StartPreviewAsync();

                        _initializationTaskSource.SetResult(result);

                        return(result);
                    }

                    if (PickVideoDeviceAutomatically)
                    {
                        _currentVideoCaptureDeviceIndex = 0;
                        result = await this.StartPreviewAsync();

                        _initializationTaskSource.SetResult(result);

                        return(result);
                    }

                    bool success = await ShowWebCamSelector();

                    if (success)
                    {
                        result = new CameraInitializationResult(true);
                        _initializationTaskSource.SetResult(result);

                        return(result);
                    }

                    result = new CameraInitializationResult(false, "Unable to select video device.");
                    _initializationTaskSource.SetResult(result);

                    return(result);
                }

                // Audio only recording
                result = await this.StartPreviewAsync();

                _initializationTaskSource.SetResult(result);

                return(result);
            }
            catch (Exception ex)
            {
                result = new CameraInitializationResult(false, "An unkown error has occured.", ex);

                if (_initializationTaskSource != null)
                {
                    _initializationTaskSource.SetResult(result);
                }

                return(result);
            }
        }