Ejemplo n.º 1
0
        private void enumerateVideoDevices()
        {
            // enumerate video devices
            var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            foreach (FilterInfo item in videoDevices)
            {
                VideoCaptureDevices.Add(
                    new NamedItemWrapper <FilterInfo> {
                    Name = item.Name, Item = item
                });
            }
        }
Ejemplo n.º 2
0
        public async Task RefreshVideoCaptureDevicesAsync()
        {
            Logger.Log($"Refreshing list of video capture devices");

            ErrorMessage = null;
            try
            {
                await Utils.RequestMediaAccessAsync(StreamingCaptureMode.Video);
            }
            catch (UnauthorizedAccessException uae)
            {
                ErrorMessage = "This application is not authorized to access the local camera device. Change permission settings and restart the application.";
                throw uae;
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message;
                throw ex;
            }

            // Populate the list of video capture devices (webcams).
            // On UWP this uses internally the API:
            //   Devices.Enumeration.DeviceInformation.FindAllAsync(VideoCapture)
            // Note that there's no API to pass a given device to WebRTC,
            // so there's no way to monitor and update that list if a device
            // gets plugged or unplugged. Even using DeviceInformation.CreateWatcher()
            // would yield some devices that might become unavailable by the time
            // WebRTC internally opens the video capture device.
            // This is more for demo purpose here because using the UWP API is nicer.
            var devices = await DeviceVideoTrackSource.GetCaptureDevicesAsync();

            var deviceList = new CollectionViewModel <VideoCaptureDeviceInfo>();

            foreach (var device in devices)
            {
                Logger.Log($"Found video capture device: id={device.id} name={device.name}");
                deviceList.Add(new VideoCaptureDeviceInfo(id: device.id, displayName: device.name));
            }
            VideoCaptureDevices = deviceList;

            // Auto-select first device for convenience
            VideoCaptureDevices.SelectFirstItemIfAny();
        }
Ejemplo n.º 3
0
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            LogMessage("Initializing the WebRTC native plugin...");

            // Populate the list of video capture devices (webcams).
            // On UWP this uses internally the API:
            //   Devices.Enumeration.DeviceInformation.FindAllAsync(VideoCapture)
            // Note that there's no API to pass a given device to WebRTC,
            // so there's no way to monitor and update that list if a device
            // gets plugged or unplugged. Even using DeviceInformation.CreateWatcher()
            // would yield some devices that might become unavailable by the time
            // WebRTC internally opens the video capture device.
            // This is more for demo purpose here because using the UWP API is nicer.
            {
                // Use a local list accessible from a background thread
                List <VideoCaptureDevice> vcds = new List <VideoCaptureDevice>(4);
                PeerConnection.GetVideoCaptureDevicesAsync().ContinueWith((prevTask) =>
                {
                    if (prevTask.Exception != null)
                    {
                        throw prevTask.Exception;
                    }

                    var devices   = prevTask.Result;
                    vcds.Capacity = devices.Count;
                    foreach (var device in devices)
                    {
                        vcds.Add(new VideoCaptureDevice()
                        {
                            Id          = device.id,
                            DisplayName = device.name,
                            Symbol      = Symbol.Video
                        });
                    }

                    // Assign on main UI thread because of XAML binding; otherwise it fails.
                    RunOnMainThread(() =>
                    {
                        VideoCaptureDevices.Clear();
                        foreach (var vcd in vcds)
                        {
                            VideoCaptureDevices.Add(vcd);
                            LogMessage($"VCD id={vcd.Id} name={vcd.DisplayName}");
                        }
                    });
                });
            }

            //localVideo.TransportControls = localVideoControls;

            PluginInitialized = false;

            // Assign STUN server(s) before calling InitializeAsync()
            _peerConnection.Servers.Clear(); // We use only one server in this demo
            _peerConnection.Servers.Add("stun:" + stunServer.Text);

            // Ensure that the UWP app was authorized to capture audio (cap:microphone)
            // and video (cap:webcam), otherwise the native plugin will fail.
            try
            {
                MediaCapture mediaAccessRequester = new MediaCapture();
                var          mediaSettings        = new MediaCaptureInitializationSettings
                {
                    AudioDeviceId        = "",
                    VideoDeviceId        = "",
                    StreamingCaptureMode = StreamingCaptureMode.AudioAndVideo,
                    PhotoCaptureSource   = PhotoCaptureSource.VideoPreview
                };
                var uiThreadScheduler = TaskScheduler.FromCurrentSynchronizationContext();
                mediaAccessRequester.InitializeAsync(mediaSettings).AsTask()
                .ContinueWith((accessTask) =>
                {
                    if (accessTask.Exception != null)
                    {
                        LogMessage($"Access to A/V denied, check app permissions: {accessTask.Exception.Message}");
                        throw accessTask.Exception;
                    }
                    _peerConnection.InitializeAsync().ContinueWith((initTask) =>
                    {
                        if (initTask.Exception != null)
                        {
                            LogMessage($"WebRTC native plugin init failed: {initTask.Exception.Message}");
                            throw initTask.Exception;
                        }
                        OnPluginPostInit();
                    }, uiThreadScheduler);     // run task on caller (UI) thread
                });
            }
            //< TODO - This below shouldn't do anything since exceptions are caught and stored inside Task.Exception...
            catch (UnauthorizedAccessException uae)
            {
                LogMessage("Access to A/V denied: " + uae.Message);
            }
            catch (Exception ex)
            {
                if (ex.InnerException is UnauthorizedAccessException uae)
                {
                    LogMessage("Access to A/V denied: " + uae.Message);
                }
                else
                {
                    LogMessage("Failed to initialize A/V with unknown exception: " + ex.Message);
                }
            }
        }