private static async Task <Tuple <MediaFrameSourceGroup, MediaFrameSourceInfo> > EnumFrameSourcesAsync()
        {
            MediaFrameSourceInfo  result_info  = null;
            MediaFrameSourceGroup result_group = null;
            var sourcegroups = await AsyncHelper.SyncFromAsync(MediaFrameSourceGroup.FindAllAsync(), "sourcegroups");

            Log.WriteLine("found {0} Source Groups", sourcegroups.Count);
            foreach (var g in sourcegroups)
            {
                var sourceinfos = g.SourceInfos;
                Log.WriteLine("Source Group {0}", g.Id);
                Log.WriteLine("             {0}", g.DisplayName);
                Log.WriteLine("             with {0} Sources:", sourceinfos.Count);
                foreach (var s in sourceinfos)
                {
                    var d = s.DeviceInformation;
                    Log.WriteLine("\t{0}", s.Id);
                    Log.WriteLine("\t\tKind {0}", s.SourceKind);
                    Log.WriteLine("\t\tDevice {0}", d.Id);
                    Log.WriteLine("\t\t       {0}", d.Name);
                    Log.WriteLine("\t\t       Kind {0}", d.Kind);
                    if (result_info == null)
                    {
                        result_info = s; // for now just pick the first thing we find
                    }
                }
                Log.WriteLine("\r\n");
                if (result_group == null)
                {
                    result_group = g; // for now just pick the first thing we find
                }
            }
            return(new Tuple <MediaFrameSourceGroup, MediaFrameSourceInfo>(result_group, result_info));
        }
Пример #2
0
    private async Task InitializeCameraFrameReader()
    {
        var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

        MediaFrameSourceGroup selectedGroup   = null;
        MediaFrameSourceInfo  colorSourceInfo = null;

        foreach (var sourceGroup in frameSourceGroups)
        {
            foreach (var sourceInfo in sourceGroup.SourceInfos)
            {
                if (sourceInfo.MediaStreamType == MediaStreamType.VideoPreview &&
                    sourceInfo.SourceKind == MediaFrameSourceKind.Color)
                {
                    colorSourceInfo = sourceInfo;
                    break;
                }
            }
            if (colorSourceInfo != null)
            {
                selectedGroup = sourceGroup;
                break;
            }
        }

        var colorFrameSource = CameraCapture.FrameSources[colorSourceInfo.Id];

        CaptureWidth      = (int)colorFrameSource.CurrentFormat.VideoFormat.Width;
        CaptureHeight     = (int)colorFrameSource.CurrentFormat.VideoFormat.Height;
        CameraFrameReader = await CameraCapture.CreateFrameReaderAsync(colorFrameSource);

        await CameraFrameReader.StartAsync();
    }
        public async Task <List <FrameSourceInformation> > GetFrameSourceInformationAsync()
        {
            var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

            MediaFrameSourceInfo colorSourceInfo = null;

            var frameSourceInformations = new List <FrameSourceInformation>();

            foreach (var sourceGroup in frameSourceGroups)
            {
                foreach (var sourceInfo in sourceGroup.SourceInfos)
                {
                    if ((sourceInfo.MediaStreamType == MediaStreamType.VideoPreview || sourceInfo.MediaStreamType == MediaStreamType.VideoRecord) &&
                        sourceInfo.SourceKind == MediaFrameSourceKind.Color)
                    {
                        colorSourceInfo = sourceInfo;
                        break;
                    }
                }
                if (colorSourceInfo != null)
                {
                    frameSourceInformations.Add(new FrameSourceInformation(sourceGroup, colorSourceInfo));
                }
            }
            return(frameSourceInformations);
        }
        //#############################################################################################
        //###################################      private     ########################################


        //#############################################################################################
        /// <summary>
        /// Find a source coresponding to parameters of <see cref="Init(MediaStreamType, MediaFrameSourceKind)"/>
        /// </summary>
        /// <param name="streamType"> MediaStreamType object property </param>
        /// <param name="sourceKind"> MediaFrameSourceKind object property </param>
        private async Task FindSource(MediaStreamType streamType, MediaFrameSourceKind sourceKind)
        {
            var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync(); // list available sources

            // indicate that source is not set
            _selectedGroup      = null;
            _selectedSourceInfo = null;

            foreach (var sourceGroup in frameSourceGroups)
            {
                foreach (var sourceInfo in sourceGroup.SourceInfos)
                {
                    // if a source is matching with arguments
                    if (sourceInfo.MediaStreamType == streamType && sourceInfo.SourceKind == sourceKind)
                    {
                        _selectedSourceInfo = sourceInfo;
                        break;
                    }
                }
                if (_selectedSourceInfo != null)
                {
                    _selectedGroup = sourceGroup;
                    break;
                }
            }

            // in case no source was found
            if (_selectedSourceInfo == null)
            {
                System.Diagnostics.Debug.WriteLine("Source not find");
            }
        }
Пример #5
0
        private async Task SimpleSelect()
        {
            // <SnippetSimpleSelect>
            var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

            MediaFrameSourceGroup selectedGroup   = null;
            MediaFrameSourceInfo  colorSourceInfo = null;

            foreach (var sourceGroup in frameSourceGroups)
            {
                foreach (var sourceInfo in sourceGroup.SourceInfos)
                {
                    if (sourceInfo.MediaStreamType == MediaStreamType.VideoPreview &&
                        sourceInfo.SourceKind == MediaFrameSourceKind.Color)
                    {
                        colorSourceInfo = sourceInfo;
                        break;
                    }
                }
                if (colorSourceInfo != null)
                {
                    selectedGroup = sourceGroup;
                    break;
                }
            }
            // </SnippetSimpleSelect>
        }
Пример #6
0
        private async Task LinqSelectColorDepthInfrared()
        {
            // <SnippetColorInfraredDepth>
            var allGroups = await MediaFrameSourceGroup.FindAllAsync();

            var eligibleGroups = allGroups.Select(g => new
            {
                Group = g,

                // For each source kind, find the source which offers that kind of media frame,
                // or null if there is no such source.
                SourceInfos = new MediaFrameSourceInfo[]
                {
                    g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Color),
                    g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Depth),
                    g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Infrared),
                }
            }).Where(g => g.SourceInfos.Any(info => info != null)).ToList();

            if (eligibleGroups.Count == 0)
            {
                System.Diagnostics.Debug.WriteLine("No source group with color, depth or infrared found.");
                return;
            }

            var selectedGroupIndex = 0; // Select the first eligible group
            MediaFrameSourceGroup selectedGroup      = eligibleGroups[selectedGroupIndex].Group;
            MediaFrameSourceInfo  colorSourceInfo    = eligibleGroups[selectedGroupIndex].SourceInfos[0];
            MediaFrameSourceInfo  infraredSourceInfo = eligibleGroups[selectedGroupIndex].SourceInfos[1];
            MediaFrameSourceInfo  depthSourceInfo    = eligibleGroups[selectedGroupIndex].SourceInfos[2];
            // </SnippetColorInfraredDepth>
        }
        private async Task InitializeCameraFrameReader()
        {
            var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

            MediaFrameSourceGroup selectedGroup   = null;
            MediaFrameSourceInfo  colorSourceInfo = null;

            foreach (var sourceGroup in frameSourceGroups)
            {
                foreach (var sourceInfo in sourceGroup.SourceInfos)
                {
                    if (sourceInfo.MediaStreamType == MediaStreamType.VideoPreview &&
                        sourceInfo.SourceKind == MediaFrameSourceKind.Color)
                    {
                        colorSourceInfo = sourceInfo;
                        break;
                    }
                }

                if (colorSourceInfo != null)
                {
                    selectedGroup = sourceGroup;
                    break;
                }
            }

            var colorFrameSource = CameraCapture.FrameSources[colorSourceInfo.Id];
            var preferredFormat  = colorFrameSource.SupportedFormats.Where(format => {
                return(format.Subtype == MediaEncodingSubtypes.Argb32);
            }).FirstOrDefault();

            CameraFrameReader = await CameraCapture.CreateFrameReaderAsync(colorFrameSource);

            await CameraFrameReader.StartAsync();
        }
Пример #8
0
 public MediaFrameReaderHelper(MediaFrameSourceInfo sourceInfo,
                               MediaCapture mediaCapture)
 {
     this.sourceInfo   = sourceInfo;
     this.mediaCapture = mediaCapture;
     this.CreateBuffer();
 }
Пример #9
0
        private static async Task <(MediaFrameSourceInfo colorSourceInfo, MediaFrameSourceGroup selectedGroup)> MediaFrameSourceInfo()
        {
            var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

            if (frameSourceGroups.Count == 0)
            {
                return(null, null);
            }

            MediaFrameSourceInfo  colorSourceInfo = null;
            MediaFrameSourceGroup selectedGroup   = null;

            foreach (var sourceGroup in frameSourceGroups)
            {
                foreach (var sourceInfo in sourceGroup.SourceInfos)
                {
                    if (sourceInfo.MediaStreamType == MediaStreamType.VideoRecord &&
                        sourceInfo.SourceKind == MediaFrameSourceKind.Color &&
                        sourceInfo.DeviceInformation?.EnclosureLocation.Panel ==
                        Windows.Devices.Enumeration.Panel.Back)
                    {
                        colorSourceInfo = sourceInfo;
                        break;
                    }
                }

                if (colorSourceInfo != null)
                {
                    selectedGroup = sourceGroup;
                    break;
                }
            }

            if (selectedGroup == null)
            {
                foreach (var sourceGroup in frameSourceGroups)
                {
                    foreach (var sourceInfo in sourceGroup.SourceInfos)
                    {
                        if (sourceInfo.MediaStreamType == MediaStreamType.VideoRecord &&
                            sourceInfo.SourceKind == MediaFrameSourceKind.Color)
                        {
                            colorSourceInfo = sourceInfo;
                            break;
                        }
                    }

                    if (colorSourceInfo != null)
                    {
                        selectedGroup = sourceGroup;
                        break;
                    }
                }
            }

            return(colorSourceInfo, selectedGroup);
        }
Пример #10
0
 public CameraHandler(MediaFrameSourceInfo sourceInfo, MediaCapture mediaCapture, double publishPeriod)
 {
     PublishPeriod = publishPeriod;
     SourceInfo_   = sourceInfo;
     MediaCapture_ = mediaCapture;
     Height        = SourceInfo_.VideoProfileMediaDescription[0].Height;
     Width         = SourceInfo_.VideoProfileMediaDescription[0].Width;
     AllocateBuffer();
 }
Пример #11
0
    // Find all streaming cameras and sensors
    //public async Task<IReadOnlyList<MediaFrameSourceGroup>> GetMediaFrameSourceGroup()
    //{
    //    var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();
    //    return frameSourceGroups;
    //}

    // Assign the groups and infos to their appropriate names
    private void GetStreamingGroupsAndInfos(IReadOnlyList <MediaFrameSourceGroup> frameSourceGroups)
    {
        /// Group and Info structure for Hololens V1
        /// Assuming all feeds are working
        /// Group 0:
        ///     Info 0: Depth VideoRecord
        ///     Info 1: Infrared VideoRecord
        ///     Info 2: Depth VideoRecord
        ///     Info 3: Infrared VideoRecord
        ///     Info 4: Color VideoRecord
        ///     Info 5: Color VideoRecord
        ///     Info 6: Color VideoRecord
        ///     Info 7: Color VideoRecord
        /// Group 0:
        ///     Info 0: Color VideoPreview
        ///     Info 1: Color VideoRecord
        ///     Info 2: 5 Photo
        if (frameSourceGroups.Count >= 2)
        {
            depthGroup  = frameSourceGroups[0];
            webcamGroup = frameSourceGroups[1];
        }
        else
        {
            DebugToServer.Log.Send("SG: " + frameSourceGroups.Count.ToString());
            return;
        }

        if (depthGroup.SourceInfos.Count >= 8)
        {
            depthNearInfo    = depthGroup.SourceInfos[0];
            infraredFarInfo  = depthGroup.SourceInfos[1];
            depthFarInfo     = depthGroup.SourceInfos[2];
            infraredNearInfo = depthGroup.SourceInfos[3];
            leftFrontInfo    = depthGroup.SourceInfos[4];
            leftSideInfo     = depthGroup.SourceInfos[5];
            rightFrontInfo   = depthGroup.SourceInfos[6];
            rightSideInfo    = depthGroup.SourceInfos[7];
        }
        else
        {
            DebugToServer.Log.Send("Sensor group missing infos.");
            //DebugToServer.Log.Send("Sensor group missing infos.");
            return;
        }

        if (webcamGroup.SourceInfos.Count >= 2)
        {
            webcamInfo = webcamGroup.SourceInfos[0];
        }
        else
        {
            //DebugToServer.debugClient.SendDebug("Webcam group missing infos.");
            return;
        }
    }
        public FrameSourceInfoModel(MediaFrameSourceInfo sourceInfo)
        {
            this.SourceInfo = sourceInfo;
            this.SourceGroup = this.SourceInfo.SourceGroup;

            this.Id = sourceInfo.Id;

            var kind = sourceInfo.SourceKind.ToString();
            var type = sourceInfo.MediaStreamType.ToString();
            this.DisplayName = $"{kind} {type}";
        }
Пример #13
0
        public static async Task <VideoFrameProcessor> CreateAsync()
        {
            IReadOnlyList <MediaFrameSourceGroup> groups = await MediaFrameSourceGroup.FindAllAsync();

            MediaFrameSourceGroup selectedGroup      = null;
            MediaFrameSourceInfo  selectedSourceInfo = null;

            // Pick first color source.
            foreach (var sourceGroup in groups)
            {
                foreach (var sourceInfo in sourceGroup.SourceInfos)
                {
                    if (sourceInfo.MediaStreamType == MediaStreamType.VideoPreview &&
                        sourceInfo.SourceKind == MediaFrameSourceKind.Color)
                    {
                        selectedSourceInfo = sourceInfo;
                        break;
                    }
                }
                if (selectedSourceInfo != null)
                {
                    selectedGroup = sourceGroup;
                    break;
                }
            }

            // No valid camera was found. This will happen on the emulator.
            if (selectedGroup == null || selectedSourceInfo == null)
            {
                return(null);
            }

            MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();

            settings.MemoryPreference     = MediaCaptureMemoryPreference.Cpu; // Need SoftwareBitmaps for FaceAnalysis
            settings.StreamingCaptureMode = StreamingCaptureMode.Video;       // Only need to stream video
            settings.SourceGroup          = selectedGroup;

            MediaCapture mediaCapture = new MediaCapture();
            await mediaCapture.InitializeAsync(settings);

            MediaFrameSource selectedSource = mediaCapture.FrameSources[selectedSourceInfo.Id];
            MediaFrameReader reader         = await mediaCapture.CreateFrameReaderAsync(selectedSource);

            MediaFrameReaderStartStatus status = await reader.StartAsync();

            // Only create a VideoFrameProcessor if the reader successfully started
            if (status == MediaFrameReaderStartStatus.Success)
            {
                return(new VideoFrameProcessor(mediaCapture, reader, selectedSource));
            }

            return(null);
        }
Пример #14
0
        private static async Task <Tuple <MediaFrameSourceGroup, MediaFrameSourceInfo> > EnumFrameSourcesAsync()
        {
            MediaFrameSourceInfo  result_info  = null;
            MediaFrameSourceGroup result_group = null;
            var sourcegroups = await AsyncHelper.AsAsync(MediaFrameSourceGroup.FindAllAsync());

            Log.WriteLine("found {0} Source Groups", sourcegroups.Count);
            if (sourcegroups.Count == 0)
            {
                var dinfos = await AsyncHelper.AsAsync(DeviceInformation.FindAllAsync(MediaFrameSourceGroup.GetDeviceSelector()));

                Log.WriteLine("found {0} devices from MediaFrameSourceGroup selector", dinfos.Count);
                foreach (var info in dinfos)
                {
                    Log.WriteLine(info.Name);
                }
                if (dinfos.Count == 0)
                {
                    dinfos = await AsyncHelper.AsAsync(DeviceInformation.FindAllAsync(DeviceClass.VideoCapture));

                    Log.WriteLine("found {0} devices from Video Capture DeviceClass", dinfos.Count);
                    foreach (var info in dinfos)
                    {
                        Log.WriteLine(info.Name);
                    }
                }
            }
            foreach (var g in sourcegroups)
            {
                var sourceinfos = g.SourceInfos;
                Log.WriteLine("Source Group {0}", g.Id);
                Log.WriteLine("             {0}", g.DisplayName);
                Log.WriteLine("             with {0} Sources:", sourceinfos.Count);
                foreach (var s in sourceinfos)
                {
                    var d = s.DeviceInformation;
                    Log.WriteLine("\t{0}", s.Id);
                    Log.WriteLine("\t\tKind {0}", s.SourceKind);
                    Log.WriteLine("\t\tDevice {0}", d.Id);
                    Log.WriteLine("\t\t       {0}", d.Name);
                    Log.WriteLine("\t\t       Kind {0}", d.Kind);
                    if (result_info == null)
                    {
                        result_info = s; // for now just pick the first thing we find
                    }
                }
                Log.EndLine();
                if (result_group == null)
                {
                    result_group = g; // for now just pick the first thing we find
                }
            }
            return(new Tuple <MediaFrameSourceGroup, MediaFrameSourceInfo>(result_group, result_info));
        }
Пример #15
0
        /// <summary>
        /// Asynchronously create a Hand Detector with the first depth camera found
        /// </summary>
        /// <param name="id">The ID of the device to look for if needed. If NULL, a device with "depth" capabilities will be randomly choose.</param>
        /// <returns>The asynchronous task</returns>
        public static async Task <HandDetector> CreateAsync(String id = null)
        {
            Debug.WriteLine("Initialize the hand detector");

            //Search for the correct media frame source
            MediaFrameSourceGroup selectedFrameSourceGroup = null;
            MediaFrameSourceInfo  selectedFrameSourceInfo  = null;

            IReadOnlyList <MediaFrameSourceGroup> allFrameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

            Debug.WriteLine($"Found {allFrameSourceGroups.Count} frame sources...");

            foreach (MediaFrameSourceGroup group in allFrameSourceGroups)
            {
                Debug.WriteLine($"Group: {group.DisplayName}");
                Debug.WriteLine($"Found {group.SourceInfos.Count} source infos...");
                foreach (MediaFrameSourceInfo info in group.SourceInfos)
                {
                    //Debug.WriteLine($"{info.SourceKind} : {info.MediaStreamType} -> {info.DeviceInformation.EnclosureLocation.Panel}");
                    //If an ID is given
                    if ((id == null || info.DeviceInformation.Id == id) && (info.MediaStreamType == MediaStreamType.VideoPreview || info.MediaStreamType == MediaStreamType.VideoRecord))
                    {
                        //Check the depth capabilities
                        if (info.SourceKind == MediaFrameSourceKind.Depth)
                        {
                            selectedFrameSourceGroup = group;
                            selectedFrameSourceInfo  = info;

                            Debug.WriteLine($"Found Device : {info.DeviceInformation.Name}:{info.DeviceInformation.Id}");
                        }
                    }

                    if (selectedFrameSourceGroup != null)
                    {
                        break;
                    }
                }
                if (selectedFrameSourceGroup != null)
                {
                    break;
                }
            }

            if (selectedFrameSourceGroup == null)
            {
                Debug.WriteLine("No frame source available found");
                return(null);
            }

            HandDetector HandDetector = new HandDetector(selectedFrameSourceGroup, selectedFrameSourceInfo);

            return(HandDetector);
        }
Пример #16
0
        public FrameSourceInfoModel(MediaFrameSourceInfo sourceInfo)
        {
            this.SourceInfo  = sourceInfo;
            this.SourceGroup = this.SourceInfo.SourceGroup;

            this.Id = sourceInfo.Id;

            var kind = sourceInfo.SourceKind.ToString();
            var type = sourceInfo.MediaStreamType.ToString();

            this.DisplayName = $"{kind} {type}";
        }
Пример #17
0
    public async Task StartPreviewAsync()
    {
#if WINDOWS_UWP
        var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

        MediaFrameSourceGroup selectedGroup   = null;
        MediaFrameSourceInfo  colorSourceInfo = null;

        foreach (var sourceGroup in frameSourceGroups)
        {
            foreach (var sourceInfo in sourceGroup.SourceInfos)
            {
                if (sourceInfo.MediaStreamType == MediaStreamType.VideoRecord &&
                    sourceInfo.SourceKind == MediaFrameSourceKind.Color)
                {
                    colorSourceInfo = sourceInfo;
                    break;
                }
            }
            if (colorSourceInfo != null)
            {
                selectedGroup = sourceGroup;
                break;
            }
        }
        var settings = new MediaCaptureInitializationSettings()
        {
            SourceGroup          = selectedGroup,
            SharingMode          = MediaCaptureSharingMode.ExclusiveControl,
            MemoryPreference     = MediaCaptureMemoryPreference.Auto,
            StreamingCaptureMode = StreamingCaptureMode.Video
        };

        try
        {
            mediaCapture = new MediaCapture();
            await mediaCapture.InitializeAsync(settings);

            var colorFrameSource = mediaCapture.FrameSources[colorSourceInfo.Id];
            mediaFrameReader = await mediaCapture.CreateFrameReaderAsync(colorFrameSource, MediaEncodingSubtypes.Argb32);

            mediaFrameReader.FrameArrived += ColorFrameReader_FrameArrived;
            await mediaFrameReader.StartAsync();

            isPreviewing = true;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed: " + ex.Message);
            return;
        }
#endif
    }
Пример #18
0
        private async Task PlayLiveVideo()
        {
            var allGroups = await MediaFrameSourceGroup.FindAllAsync();

            var eligibleGroups = allGroups.Select(g => new
            {
                Group = g,

                // For each source kind, find the source which offers that kind of media frame,
                // or null if there is no such source.
                SourceInfos = new MediaFrameSourceInfo[]
                {
                    g.SourceInfos.FirstOrDefault(info => info.DeviceInformation?.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front &&
                                                 info.SourceKind == MediaFrameSourceKind.Color),
                    g.SourceInfos.FirstOrDefault(info => info.DeviceInformation?.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back &&
                                                 info.SourceKind == MediaFrameSourceKind.Color)
                }
            }).Where(g => g.SourceInfos.Any(info => info != null)).ToList();

            if (eligibleGroups.Count == 0)
            {
                System.Diagnostics.Debug.WriteLine("No source group with front and back-facing camera found.");
                return;
            }

            var selectedGroupIndex = 0; // Select the first eligible group
            MediaFrameSourceGroup selectedGroup   = eligibleGroups[selectedGroupIndex].Group;
            MediaFrameSourceInfo  frontSourceInfo = selectedGroup.SourceInfos[0];

            MediaCapture mediaCapture = new MediaCapture();
            MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings()
            {
                SourceGroup          = selectedGroup,
                SharingMode          = MediaCaptureSharingMode.ExclusiveControl,
                MemoryPreference     = MediaCaptureMemoryPreference.Cpu,
                StreamingCaptureMode = StreamingCaptureMode.Video,
            };

            try
            {
                await mediaCapture.InitializeAsync(settings);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed: " + ex.Message);
                return;
            }
            var frameMediaSource1 = MediaSource.CreateFromMediaFrameSource(mediaCapture.FrameSources[frontSourceInfo.Id]);

            VideoStreamer.SetPlaybackSource(frameMediaSource1);
            VideoStreamer.Play();
        }
Пример #19
0
        /// <summary>
        /// Video Capture: Initialize Camera Capture.
        /// Implementation is from the UWP official tutorial.
        /// https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/process-media-frames-with-mediaframereader
        /// </summary>
        public async void InitializeCamera()
        {
            var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

            var selectedGroupObjects = frameSourceGroups.Select(group =>
                                                                new
            {
                sourceGroup     = group,
                colorSourceInfo = group.SourceInfos.FirstOrDefault((sourceInfo) =>
                {
                    // On Xbox/Kinect, omit the MediaStreamType and EnclosureLocation tests
                    return(sourceInfo.SourceKind == MediaFrameSourceKind.Color);
                })
            }).Where(t => t.colorSourceInfo != null)
                                       .FirstOrDefault();

            MediaFrameSourceGroup selectedGroup   = selectedGroupObjects?.sourceGroup;
            MediaFrameSourceInfo  colorSourceInfo = selectedGroupObjects?.colorSourceInfo;

            if (selectedGroup == null)
            {
                return;
            }

            mediaCapture = new MediaCapture();

            var settings = new MediaCaptureInitializationSettings()
            {
                SourceGroup          = selectedGroup,
                SharingMode          = MediaCaptureSharingMode.ExclusiveControl,
                MemoryPreference     = MediaCaptureMemoryPreference.Cpu,
                StreamingCaptureMode = StreamingCaptureMode.Video
            };

            try
            {
                await mediaCapture.InitializeAsync(settings);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed: " + ex.Message);
                return;
            }

            var colorFrameSource = mediaCapture.FrameSources[colorSourceInfo.Id];

            mediaFrameReader = await mediaCapture.CreateFrameReaderAsync(colorFrameSource, MediaEncodingSubtypes.Argb32);

            mediaFrameReader.FrameArrived += ColorFrameReader_FrameArrived;
            await mediaFrameReader.StartAsync();
        }
Пример #20
0
        public async Task <bool> PopulateAsync(
            Func <MediaFrameSourceInfo, bool> sourceInfoFilter,
            Func <IEnumerable <MediaFrameSourceGroup>, MediaFrameSourceGroup> sourceGroupSelector)
        {
            var mediaFrameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

            var candidates = mediaFrameSourceGroups.Where(
                group => group.SourceInfos.Any(sourceInfoFilter));

            this.FrameSourceGroup = sourceGroupSelector(candidates);

            this.FrameSourceInfo = this.FrameSourceGroup?.SourceInfos.FirstOrDefault(
                sourceInfoFilter);

            return((this.FrameSourceGroup != null) && (this.FrameSourceInfo != null));
        }
Пример #21
0
        private async void FrameReaderThread()
        {
            var list = await MediaFrameSourceGroup.FindAllAsync();

            MediaFrameSourceInfo  sourceInfo  = null;
            MediaFrameSourceGroup sourceGroup = null;

            foreach (var group in list)
            {
                if (group.SourceInfos.Count == 2)
                {
                    var tempSourceInfo = group.SourceInfos.FirstOrDefault(s =>
                                                                          s.SourceKind == MediaFrameSourceKind.Infrared &&
                                                                          (s.MediaStreamType == MediaStreamType.VideoPreview ||
                                                                           s.MediaStreamType == MediaStreamType.VideoRecord));
                    if (tempSourceInfo != null)
                    {
                        sourceInfo  = tempSourceInfo;
                        sourceGroup = group;
                        break;
                    }
                }
            }

            if (sourceGroup == null || sourceInfo == null)
            {
                return;
            }

            var settings = new MediaCaptureInitializationSettings();

            settings.SourceGroup      = sourceGroup;
            settings.SharingMode      = MediaCaptureSharingMode.SharedReadOnly;
            settings.MemoryPreference = MediaCaptureMemoryPreference.Cpu;
            await _mc.InitializeAsync(settings);

            var irSource = _mc.FrameSources[sourceInfo.Id];

            imageFrameSize.Width  = (int)irSource.CurrentFormat.VideoFormat.Width;
            imageFrameSize.Height = (int)irSource.CurrentFormat.VideoFormat.Height;

            _iRFrameReader = await _mc.CreateFrameReaderAsync(irSource);

            _iRFrameReader.FrameArrived += IrReader_FrameArrived;

            await _iRFrameReader.StartAsync();
        }
Пример #22
0
        private async void GetRGB32PreferredFormat()
        {
            var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

            var selectedGroupObjects = frameSourceGroups.Select(group =>
                                                                new
            {
                sourceGroup     = group,
                colorSourceInfo = group.SourceInfos.FirstOrDefault((sourceInfo) =>
                {
                    return(sourceInfo.MediaStreamType == MediaStreamType.VideoPreview &&
                           sourceInfo.SourceKind == MediaFrameSourceKind.Color &&
                           sourceInfo.DeviceInformation?.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);
                })
            }).Where(t => t.colorSourceInfo != null)
                                       .FirstOrDefault();

            MediaFrameSourceGroup selectedGroup   = selectedGroupObjects?.sourceGroup;
            MediaFrameSourceInfo  colorSourceInfo = selectedGroupObjects?.colorSourceInfo;

            if (selectedGroup == null)
            {
                return;
            }

            // <SnippetGetPreferredFormat>
            var colorFrameSource = mediaCapture.FrameSources[colorSourceInfo.Id];
            var preferredFormat  = colorFrameSource.SupportedFormats.Where(format =>
            {
                return(format.VideoFormat.Width >= 1080 &&
                       format.Subtype == MediaEncodingSubtypes.Argb32);
            }).FirstOrDefault();

            if (preferredFormat == null)
            {
                // Our desired format is not supported
                return;
            }

            await colorFrameSource.SetFormatAsync(preferredFormat);

            // </SnippetGetPreferredFormat>
        }
Пример #23
0
        private async Task GetMediaFrameGroup()
        {
            var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

            foreach (MediaFrameSourceGroup sourceGroup in frameSourceGroups)
            {
                foreach (MediaFrameSourceInfo sourceInfo in sourceGroup.SourceInfos)
                {
                    if (sourceInfo.MediaStreamType == MediaStreamType.VideoPreview && sourceInfo.SourceKind == MediaFrameSourceKind.Color)
                    {
                        colorSourceInfo = sourceInfo;
                        break;
                    }
                }
                if (colorSourceInfo != null)
                {
                    selectedGroup = sourceGroup;
                    break;
                }
            }
        }
Пример #24
0
        /// <summary>
        /// Initializes a new MediaCapture instance and starts the Preview streaming to the CamPreview UI element.
        /// </summary>
        /// <returns>Async Task object returning true if initialization and streaming were successful and false if an exception occurred.</returns>
        private async Task <bool> StartWebcamStreaming()
        {
            bool successful = true;

            try
            {
                this.mediaCapture = new MediaCapture();

                var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

                MediaFrameSourceGroup selectedGroup   = null;
                MediaFrameSourceInfo  colorSourceInfo = null;

                foreach (var sourceGroup in frameSourceGroups)
                {
                    foreach (var sourceInfo in sourceGroup.SourceInfos)
                    {
                        if ((sourceInfo.MediaStreamType == MediaStreamType.VideoPreview || sourceInfo.MediaStreamType == MediaStreamType.VideoRecord) &&
                            sourceInfo.SourceKind == MediaFrameSourceKind.Color)
                        {
                            colorSourceInfo = sourceInfo;
                            break;
                        }
                    }
                    if (colorSourceInfo != null)
                    {
                        selectedGroup = sourceGroup;
                        break;
                    }
                }

                if (colorSourceInfo != null)
                {
                    var settings = new MediaCaptureInitializationSettings()
                    {
                        SourceGroup          = selectedGroup,
                        SharingMode          = MediaCaptureSharingMode.ExclusiveControl,
                        MemoryPreference     = MediaCaptureMemoryPreference.Cpu,
                        StreamingCaptureMode = StreamingCaptureMode.Video
                    };

                    await mediaCapture.InitializeAsync(settings);

                    this.mediaCapture.Failed += this.MediaCapture_CameraStreamFailed;
                    var colorFrameSource = mediaCapture.FrameSources[colorSourceInfo.Id];
                    var preferredFormat  = colorFrameSource.SupportedFormats
                                           .OrderByDescending(x => x.VideoFormat.Width)
                                           .FirstOrDefault(x => x.VideoFormat.Width <= 1920 && x.Subtype.Equals(MediaEncodingSubtypes.Nv12, StringComparison.OrdinalIgnoreCase));

                    await colorFrameSource.SetFormatAsync(preferredFormat);

                    this.mediaFrameReader = await this.mediaCapture.CreateFrameReaderAsync(colorFrameSource);

                    this.mediaFrameReader.FrameArrived += MediaFrameReader_FrameArrived;
                    await this.mediaFrameReader.StartAsync();
                }

                // Cache the media properties as we'll need them later.
                var deviceController = this.mediaCapture.VideoDeviceController;
                this.videoProperties = deviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;

                // Immediately start streaming to our CaptureElement UI.
                // NOTE: CaptureElement's Source must be set before streaming is started.
                this.CamPreview.Source                 = this.mediaCapture;
                this.CamPreview.FlowDirection          = FlowDirection.RightToLeft;
                this.VisualizationCanvas.FlowDirection = FlowDirection.RightToLeft;
                await this.mediaCapture.StartPreviewAsync();
            }
            catch (System.UnauthorizedAccessException)
            {
                // If the user has disabled their webcam this exception is thrown; provide a descriptive message to inform the user of this fact.
                await LogError("Webcam is disabled or access to the webcam is disabled for this app.\nEnsure Privacy Settings allow webcam usage.");

                successful = false;
            }
            catch (Exception ex)
            {
                await LogError(ex.ToString());

                successful = false;
            }

            return(successful);
        }
Пример #25
0
        private async void ActionButton_Click(object sender, RoutedEventArgs e)
        {
            // <SnippetImageElementSource>
            imageElement.Source = new SoftwareBitmapSource();
            // </SnippetImageElementSource>

            // <SnippetFindAllAsync>
            var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

            // </SnippetFindAllAsync>

            // Color, infrared, and depth


            // <SnippetSelectColor>
            var selectedGroupObjects = frameSourceGroups.Select(group =>
                                                                new
            {
                sourceGroup     = group,
                colorSourceInfo = group.SourceInfos.FirstOrDefault((sourceInfo) =>
                {
                    // On XBox/Kinect, omit the MediaStreamType and EnclosureLocation tests
                    return(sourceInfo.MediaStreamType == MediaStreamType.VideoPreview &&
                           sourceInfo.SourceKind == MediaFrameSourceKind.Color &&
                           sourceInfo.DeviceInformation?.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);
                })
            }).Where(t => t.colorSourceInfo != null)
                                       .FirstOrDefault();

            MediaFrameSourceGroup selectedGroup   = selectedGroupObjects?.sourceGroup;
            MediaFrameSourceInfo  colorSourceInfo = selectedGroupObjects?.colorSourceInfo;

            if (selectedGroup == null)
            {
                return;
            }
            // </SnippetSelectColor>

            // <SnippetInitMediaCapture>
            mediaCapture = new MediaCapture();

            var settings = new MediaCaptureInitializationSettings()
            {
                SourceGroup          = selectedGroup,
                SharingMode          = MediaCaptureSharingMode.ExclusiveControl,
                MemoryPreference     = MediaCaptureMemoryPreference.Cpu,
                StreamingCaptureMode = StreamingCaptureMode.Video
            };

            try
            {
                await mediaCapture.InitializeAsync(settings);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed: " + ex.Message);
                return;
            }
            // </SnippetInitMediaCapture>


            var colorFrameSource = mediaCapture.FrameSources[colorSourceInfo.Id];
            var preferredFormat  = colorFrameSource.SupportedFormats.Where(format =>
            {
                return(format.VideoFormat.Width == 1920);
            }).FirstOrDefault();

            if (preferredFormat == null)
            {
                // Our desired format is not supported
                return;
            }
            await colorFrameSource.SetFormatAsync(preferredFormat);

            // <SnippetCreateFrameReader>
            mediaFrameReader = await mediaCapture.CreateFrameReaderAsync(colorFrameSource, MediaEncodingSubtypes.Argb32);

            mediaFrameReader.FrameArrived += ColorFrameReader_FrameArrived;
            await mediaFrameReader.StartAsync();

            // </SnippetCreateFrameReader>
        }
Пример #26
0
        private async void MediaSourceFromFrameSource_Click(object sender, RoutedEventArgs e)
        {
            // <SnippetMediaSourceSelectGroup>
            var allGroups = await MediaFrameSourceGroup.FindAllAsync();

            var eligibleGroups = allGroups.Select(g => new
            {
                Group = g,

                // For each source kind, find the source which offers that kind of media frame,
                // or null if there is no such source.
                SourceInfos = new MediaFrameSourceInfo[]
                {
                    g.SourceInfos.FirstOrDefault(info => info.DeviceInformation?.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front &&
                                                 info.SourceKind == MediaFrameSourceKind.Color),
                    g.SourceInfos.FirstOrDefault(info => info.DeviceInformation?.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back &&
                                                 info.SourceKind == MediaFrameSourceKind.Color)
                }
            }).Where(g => g.SourceInfos.Any(info => info != null)).ToList();

            if (eligibleGroups.Count == 0)
            {
                System.Diagnostics.Debug.WriteLine("No source group with front and back-facing camera found.");
                return;
            }

            var selectedGroupIndex = 0; // Select the first eligible group
            MediaFrameSourceGroup selectedGroup   = eligibleGroups[selectedGroupIndex].Group;
            MediaFrameSourceInfo  frontSourceInfo = selectedGroup.SourceInfos[0];
            MediaFrameSourceInfo  backSourceInfo  = selectedGroup.SourceInfos[1];

            // </SnippetMediaSourceSelectGroup>

            // <SnippetMediaSourceInitMediaCapture>
            mediaCapture = new MediaCapture();

            var settings = new MediaCaptureInitializationSettings()
            {
                SourceGroup          = selectedGroup,
                SharingMode          = MediaCaptureSharingMode.ExclusiveControl,
                MemoryPreference     = MediaCaptureMemoryPreference.Cpu,
                StreamingCaptureMode = StreamingCaptureMode.Video
            };

            try
            {
                await mediaCapture.InitializeAsync(settings);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed: " + ex.Message);
                return;
            }
            // </SnippetMediaSourceInitMediaCapture>


            // <SnippetMediaSourceMediaPlayer>
            var frameMediaSource1 = MediaSource.CreateFromMediaFrameSource(mediaCapture.FrameSources[frontSourceInfo.Id]);

            mediaPlayerElement1.SetMediaPlayer(new Windows.Media.Playback.MediaPlayer());
            mediaPlayerElement1.MediaPlayer.Source = frameMediaSource1;
            mediaPlayerElement1.AutoPlay           = true;

            var frameMediaSource2 = MediaSource.CreateFromMediaFrameSource(mediaCapture.FrameSources[backSourceInfo.Id]);

            mediaPlayerElement2.SetMediaPlayer(new Windows.Media.Playback.MediaPlayer());
            mediaPlayerElement2.MediaPlayer.Source = frameMediaSource2;
            mediaPlayerElement2.AutoPlay           = true;
            // </SnippetMediaSourceMediaPlayer>
        }
Пример #27
0
        // </SnippetMultiFrameDeclarations>

        private async void InitMultiFrame()
        {
            // <SnippetSelectColorAndDepth>
            var allGroups = await MediaFrameSourceGroup.FindAllAsync();

            var eligibleGroups = allGroups.Select(g => new
            {
                Group = g,

                // For each source kind, find the source which offers that kind of media frame,
                // or null if there is no such source.
                SourceInfos = new MediaFrameSourceInfo[]
                {
                    g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Color),
                    g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Depth)
                }
            }).Where(g => g.SourceInfos.Any(info => info != null)).ToList();

            if (eligibleGroups.Count == 0)
            {
                System.Diagnostics.Debug.WriteLine("No source group with color, depth or infrared found.");
                return;
            }

            var selectedGroupIndex = 0; // Select the first eligible group
            MediaFrameSourceGroup selectedGroup   = eligibleGroups[selectedGroupIndex].Group;
            MediaFrameSourceInfo  colorSourceInfo = eligibleGroups[selectedGroupIndex].SourceInfos[0];
            MediaFrameSourceInfo  depthSourceInfo = eligibleGroups[selectedGroupIndex].SourceInfos[1];

            // </SnippetSelectColorAndDepth>


            // <SnippetMultiFrameInitMediaCapture>
            mediaCapture = new MediaCapture();

            var settings = new MediaCaptureInitializationSettings()
            {
                SourceGroup          = selectedGroup,
                SharingMode          = MediaCaptureSharingMode.ExclusiveControl,
                MemoryPreference     = MediaCaptureMemoryPreference.Cpu,
                StreamingCaptureMode = StreamingCaptureMode.Video
            };

            await mediaCapture.InitializeAsync(settings);

            // </SnippetMultiFrameInitMediaCapture>


            // <SnippetGetColorAndDepthSource>
            MediaFrameSource colorSource =
                mediaCapture.FrameSources.Values.FirstOrDefault(
                    s => s.Info.SourceKind == MediaFrameSourceKind.Color);

            MediaFrameSource depthSource =
                mediaCapture.FrameSources.Values.FirstOrDefault(
                    s => s.Info.SourceKind == MediaFrameSourceKind.Depth);

            if (colorSource == null || depthSource == null)
            {
                System.Diagnostics.Debug.WriteLine("MediaCapture doesn't have the Color and Depth streams");
                return;
            }

            _colorSourceId = colorSource.Info.Id;
            _depthSourceId = depthSource.Info.Id;
            // </SnippetGetColorAndDepthSource>

            // <SnippetInitMultiFrameReader>
            _multiFrameReader = await mediaCapture.CreateMultiSourceFrameReaderAsync(
                new[] { colorSource, depthSource });

            _multiFrameReader.FrameArrived += MultiFrameReader_FrameArrived;

            _frameRenderer = new FrameRenderer(imageElement);

            MultiSourceMediaFrameReaderStartStatus startStatus =
                await _multiFrameReader.StartAsync();

            if (startStatus != MultiSourceMediaFrameReaderStartStatus.Success)
            {
                throw new InvalidOperationException(
                          "Unable to start reader: " + startStatus);
            }

            this.CorrelationFailed += MainPage_CorrelationFailed;
            Task.Run(() => NotifyAboutCorrelationFailure(_tokenSource.Token));
            // </SnippetInitMultiFrameReader>
        }
Пример #28
0
        private async void ActionButton2_Click(object sender, RoutedEventArgs e)
        {
            var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();


            // Color, infrared, and depth

            var selectedGroupObjects = frameSourceGroups.Select(group =>
                                                                new
            {
                sourceGroup     = group,
                colorSourceInfo = group.SourceInfos.FirstOrDefault((sourceInfo) =>
                {
                    return(sourceInfo.SourceKind == MediaFrameSourceKind.Color);
                })
            }).Where(t => t.colorSourceInfo != null)
                                       .FirstOrDefault();

            MediaFrameSourceGroup selectedGroup   = selectedGroupObjects?.sourceGroup;
            MediaFrameSourceInfo  colorSourceInfo = selectedGroupObjects?.colorSourceInfo;

            if (selectedGroup == null)
            {
                return;
            }

            mediaCapture = new MediaCapture();

            var settings = new MediaCaptureInitializationSettings()
            {
                SourceGroup          = selectedGroup,
                SharingMode          = MediaCaptureSharingMode.ExclusiveControl,
                MemoryPreference     = MediaCaptureMemoryPreference.Cpu,
                StreamingCaptureMode = StreamingCaptureMode.Video
            };

            try
            {
                await mediaCapture.InitializeAsync(settings);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed: " + ex.Message);
                return;
            }

            var colorFrameSource = mediaCapture.FrameSources[colorSourceInfo.Id];
            var preferredFormat  = colorFrameSource.SupportedFormats.Where(format =>
            {
                return(format.VideoFormat.Width == 1920);
            }).FirstOrDefault();


            if (preferredFormat == null)
            {
                // Our desired format is not supported
                return;
            }
            await colorFrameSource.SetFormatAsync(preferredFormat);


            mediaFrameReader = await mediaCapture.CreateFrameReaderAsync(colorFrameSource, MediaEncodingSubtypes.Argb32);

            mediaFrameReader.FrameArrived += ColorFrameReader_FrameArrived_FrameRenderer;

            _frameRenderer = new FrameRenderer(imageElement);


            await mediaFrameReader.StartAsync();
        }
Пример #29
0
        public static async Task <FrameGrabber> CreateAsync()
        {
            MediaCapture     mediaCapture     = null;
            MediaFrameReader mediaFrameReader = null;

            MediaFrameSourceGroup selectedGroup      = null;
            MediaFrameSourceInfo  selectedSourceInfo = null;

            // Pick first color source
            var groups = await MediaFrameSourceGroup.FindAllAsync();

            foreach (MediaFrameSourceGroup sourceGroup in groups)
            {
                foreach (MediaFrameSourceInfo sourceInfo in sourceGroup.SourceInfos)
                {
                    if (sourceInfo.SourceKind == MediaFrameSourceKind.Color)
                    {
                        selectedSourceInfo = sourceInfo;
                        break;
                    }
                }

                if (selectedSourceInfo != null)
                {
                    selectedGroup = sourceGroup;
                    break;
                }
            }

            // No valid camera was found. This will happen on the emulator.
            if (selectedGroup == null || selectedSourceInfo == null)
            {
                Debug.WriteLine("Failed to find Group and SourceInfo");
                return(new FrameGrabber());
            }

            // Create settings
            var settings = new MediaCaptureInitializationSettings
            {
                SourceGroup = selectedGroup,

                // This media capture can share streaming with other apps.
                SharingMode = MediaCaptureSharingMode.SharedReadOnly,

                // Only stream video and don't initialize audio capture devices.
                StreamingCaptureMode = StreamingCaptureMode.Video,

                // Set to CPU to ensure frames always contain CPU SoftwareBitmap images
                // instead of preferring GPU D3DSurface images.
                MemoryPreference = MediaCaptureMemoryPreference.Cpu,
            };

            // Create and initilize capture device
            mediaCapture = new MediaCapture();

            try
            {
                await mediaCapture.InitializeAsync(settings);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Failed to initilise mediacaptrue {e.ToString()}");
                return(new FrameGrabber());
            }

            MediaFrameSource selectedSource = mediaCapture.FrameSources[selectedSourceInfo.Id];

            // create new frame reader
            mediaFrameReader = await mediaCapture.CreateFrameReaderAsync(selectedSource);

            MediaFrameReaderStartStatus status = await mediaFrameReader.StartAsync();

            if (status == MediaFrameReaderStartStatus.Success)
            {
                Debug.WriteLine("MediaFrameReaderStartStatus == Success");
                return(new FrameGrabber(mediaCapture, selectedSource, mediaFrameReader));
            }
            else
            {
                Debug.WriteLine($"MediaFrameReaderStartStatus != Success; {status}");
                return(new FrameGrabber());
            }
        }
Пример #30
0
 public static bool VideoPreviewFilter(MediaFrameSourceInfo sourceInfo) =>
 sourceInfo.MediaStreamType == MediaStreamType.VideoPreview;
Пример #31
0
 public static bool ColorFilter(MediaFrameSourceInfo sourceInfo) =>
 sourceInfo.SourceKind == MediaFrameSourceKind.Color;