Exemple #1
0
    private static bool GetBestProfileAndDescription(
        DeviceInformation videoDevice,
        out MediaCaptureVideoProfile mediaProfile,
        out MediaCaptureVideoProfileMediaDescription mediaDescription)
    {
        var profiles = MediaCapture.FindAllVideoProfiles(videoDevice.Id);

        foreach (var profile in profiles)
        {
            foreach (var description in profile.SupportedRecordMediaDescription)
            {
                System.Diagnostics.Debug.WriteLine("Supported MF Video Profile Description (width: {0}) (height: {1}) (sub-type: {2}) (frame rate: {3})",
                                                   description.Width,
                                                   description.Height,
                                                   description.Subtype,
                                                   description.FrameRate);

                if (description.Width == DesiredWidth && description.Height == DesiredHeight && description.FrameRate == DesiredFramerate)
                {
                    mediaProfile     = profile;
                    mediaDescription = description;
                    return(true);
                }
            }
        }

        mediaProfile     = null;
        mediaDescription = null;
        return(false);
    }
        public async Task AddVideoTrackFromDeviceAsync(string trackName)
        {
            await RequestMediaAccessAsync(StreamingCaptureMode.Video);

            // Create the source
            VideoCaptureDeviceInfo deviceInfo = VideoCaptureDevices.SelectedItem;

            if (deviceInfo == null)
            {
                throw new InvalidOperationException("No video capture device selected");
            }
            var deviceConfig = new LocalVideoDeviceInitConfig
            {
                videoDevice = new VideoCaptureDevice {
                    id = deviceInfo.Id
                },
            };
            VideoCaptureFormatViewModel formatInfo = VideoCaptureFormats.SelectedItem;

            if (formatInfo != null)
            {
                deviceConfig.width     = formatInfo.Format.width;
                deviceConfig.height    = formatInfo.Format.height;
                deviceConfig.framerate = formatInfo.Format.framerate;
            }
            if (deviceInfo.SupportsVideoProfiles)
            {
                MediaCaptureVideoProfile profile = VideoProfiles.SelectedItem;
                deviceConfig.videoProfileId   = profile?.Id;
                deviceConfig.videoProfileKind = SelectedVideoProfileKind;
            }
            var source = await DeviceVideoTrackSource.CreateAsync(deviceConfig);

            // FIXME - this leaks the source, never disposed

            // Crate the track
            var trackConfig = new LocalVideoTrackInitConfig
            {
                trackName = trackName,
            };
            var track = LocalVideoTrack.CreateFromSource(source, trackConfig);

            // FIXME - this probably leaks the track, never disposed

            SessionModel.Current.VideoTracks.Add(new VideoTrackViewModel
            {
                Source     = source,
                Track      = track,
                TrackImpl  = track,
                IsRemote   = false,
                DeviceName = deviceInfo.DisplayName
            });
            SessionModel.Current.LocalTracks.Add(new TrackViewModel(Symbol.Video)
            {
                DisplayName = deviceInfo.DisplayName
            });
        }
Exemple #3
0
        /// <summary>
        /// Creates the initialization settings for the MediaCapture object that will support
        /// all the requested capture settings specified in the configuration object. This method
        /// will iterate through all the device's video capture profiles to find one that supports
        /// the requested capture frame dimensions and frame rate. If both Video and Preview streams
        /// are selected (e.g. for simultaneous mixed reality capture), then the selected profile must
        /// support the capture modes for both streams.
        /// </summary>
        /// <returns>
        /// A MediaCaptureInitializationSettings object for the first profile that satisfies all the
        /// requested capture settings in the configuration object, or null if no such profile was found.
        /// </returns>
        private async Task <MediaCaptureInitializationSettings> CreateMediaCaptureSettingsAsync()
        {
            MediaFrameSourceGroup    selectedSourceGroup         = null;
            MediaCaptureVideoProfile profile                     = null;
            MediaCaptureVideoProfileMediaDescription videoDesc   = null;
            MediaCaptureVideoProfileMediaDescription previewDesc = null;

            var mediaFrameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

            // Search all source groups
            foreach (var mediaFrameSourceGroup in mediaFrameSourceGroups)
            {
                // Search for a profile that supports the requested capture modes
                var knownProfiles = MediaCapture.FindAllVideoProfiles(mediaFrameSourceGroup.Id);
                foreach (var knownProfile in knownProfiles)
                {
                    // If a video stream capture mode was specified
                    if (this.configuration.VideoStreamSettings != null)
                    {
                        // Clear any partial matches and continue searching
                        profile             = null;
                        videoDesc           = null;
                        selectedSourceGroup = null;

                        // Search the supported video (recording) modes for the requested resolution and frame rate
                        foreach (var knownDesc in knownProfile.SupportedRecordMediaDescription)
                        {
                            if (knownDesc.Width == this.configuration.VideoStreamSettings.ImageWidth &&
                                knownDesc.Height == this.configuration.VideoStreamSettings.ImageHeight &&
                                knownDesc.FrameRate == this.configuration.VideoStreamSettings.FrameRate)
                            {
                                // Found a match for video. Need to also match the requested preview mode (if any)
                                // within the same profile and source group, otherwise we have to keep searching.
                                profile             = knownProfile;
                                videoDesc           = knownDesc;
                                selectedSourceGroup = mediaFrameSourceGroup;
                                break;
                            }
                        }

                        if (profile == null)
                        {
                            // This profile does not support the requested video stream capture parameters - try the next profile
                            continue;
                        }
                    }

                    // If a preview stream capture mode was specified
                    if (this.configuration.PreviewStreamSettings != null)
                    {
                        // Clear any partial matches and continue searching
                        profile             = null;
                        previewDesc         = null;
                        selectedSourceGroup = null;

                        // Search the supported preview modes for the requested resolution and frame rate
                        foreach (var knownDesc in knownProfile.SupportedPreviewMediaDescription)
                        {
                            if (knownDesc.Width == this.configuration.PreviewStreamSettings.ImageWidth &&
                                knownDesc.Height == this.configuration.PreviewStreamSettings.ImageHeight &&
                                knownDesc.FrameRate == this.configuration.PreviewStreamSettings.FrameRate)
                            {
                                // Found a match
                                profile             = knownProfile;
                                previewDesc         = knownDesc;
                                selectedSourceGroup = mediaFrameSourceGroup;
                                break;
                            }
                        }

                        if (profile == null)
                        {
                            // This profile does not support the requested preview mode - try the next profile
                            continue;
                        }
                    }

                    if (profile != null)
                    {
                        // Found a valid profile that supports the requested capture settings
                        return(new MediaCaptureInitializationSettings
                        {
                            VideoProfile = profile,
                            RecordMediaDescription = videoDesc,
                            PreviewMediaDescription = previewDesc,
                            VideoDeviceId = selectedSourceGroup.Id,
                            StreamingCaptureMode = StreamingCaptureMode.Video,
                            MemoryPreference = MediaCaptureMemoryPreference.Cpu,
                            SharingMode = MediaCaptureSharingMode.ExclusiveControl,
                            SourceGroup = selectedSourceGroup,
                        });
                    }
                }
            }

            // No matching settings were found
            return(null);
        }
    async Task Start()
    {
        // Socket listener
        audioSocketListener = new StreamSocketListener();
        audioSocketListener.ConnectionReceived += OnConnectionAudio;
        await audioSocketListener.BindServiceNameAsync(audioServiceName);

        videoSocketListener = new StreamSocketListener();
        videoSocketListener.ConnectionReceived += OnConnectionVideo;
        await videoSocketListener.BindServiceNameAsync(videoServiceName);

        // Find a media source group which gives us webcam and microphone input streams
        var sourceGroups = await MediaFrameSourceGroup.FindAllAsync();

        MediaFrameSourceGroup    selectedSourceGroup  = null;
        MediaCaptureVideoProfile selectedVideoProfile = null;
        MediaCaptureVideoProfileMediaDescription selectedDescription = null;

        foreach (MediaFrameSourceGroup sourceGroup in sourceGroups)
        {
            var videoProfiles = MediaCapture.FindKnownVideoProfiles(sourceGroup.Id, KnownVideoProfile.VideoConferencing);
            foreach (MediaCaptureVideoProfile videoProfile in videoProfiles)
            {
                foreach (var desc in videoProfile.SupportedRecordMediaDescription)
                {
                    if (desc.Width == videoWidth && desc.Height == videoHeight && desc.FrameRate == frameRate)
                    {
                        selectedSourceGroup  = sourceGroup;
                        selectedVideoProfile = videoProfile;
                        selectedDescription  = desc;
                    }
                }
            }
        }

        if (selectedSourceGroup == null)
        {
            Debug.Log("No source group was found.");
            return;
        }

        mediaCapture = new MediaCapture();
        var settings = new MediaCaptureInitializationSettings()
        {
            AudioProcessing        = AudioProcessing.Raw,
            MemoryPreference       = MediaCaptureMemoryPreference.Cpu,
            RecordMediaDescription = selectedDescription,
            SharingMode            = MediaCaptureSharingMode.ExclusiveControl,
            SourceGroup            = selectedSourceGroup,
            StreamingCaptureMode   = StreamingCaptureMode.AudioAndVideo,
            VideoProfile           = selectedVideoProfile,
        };

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

        var audioFrameSources = mediaCapture.FrameSources.Where(src => src.Value.Info.MediaStreamType == MediaStreamType.Audio);

        if (audioFrameSources.Count() == 0)
        {
            Debug.Log("No audio source was found.");
            return;
        }
        MediaFrameSource audioFrameSource = audioFrameSources.FirstOrDefault().Value;
        var videoFrameSources             = mediaCapture.FrameSources.Where(src => src.Value.Info.SourceKind == MediaFrameSourceKind.Color);

        if (videoFrameSources.Count() == 0)
        {
            Debug.Log("No video source was found.");
            return;
        }
        // MediaFrameSource videoFrameSource = videoFrameSources.FirstOrDefault().Value;
        MediaFrameSource videoFrameSource = null;
        MediaFrameFormat selectedFormat   = null;

        foreach (var kv in videoFrameSources)
        {
            MediaFrameSource source = kv.Value;
            foreach (MediaFrameFormat format in source.SupportedFormats)
            {
                if (format.VideoFormat.Width == videoWidth && format.VideoFormat.Height == videoHeight &&
                    format.FrameRate.Numerator == frameRate && format.FrameRate.Denominator == 1)
                {
                    videoFrameSource = source;
                    selectedFormat   = format;
                    break;
                }
            }
            if (videoFrameSource != null)
            {
                break;
            }
        }
        if (selectedFormat != null)
        {
            await videoFrameSource.SetFormatAsync(selectedFormat);
        }
        else
        {
            Debug.Log("Cannot find a proper MediaFrameFormat.");
            return;
        }

        // Start streaming
        audioFrameReader = await mediaCapture.CreateFrameReaderAsync(audioFrameSource);

        audioFrameReader.FrameArrived += AudioFrameArrived;
        videoFrameReader = await mediaCapture.CreateFrameReaderAsync(videoFrameSource);

        videoFrameReader.FrameArrived += VideoFrameArrived;

        var audioStartStatus = audioFrameReader.StartAsync();
        var videoStartStatus = videoFrameReader.StartAsync();

        if (await audioStartStatus != MediaFrameReaderStartStatus.Success)
        {
            Debug.Log("The audioFrameReader couldn't start.");
        }
        if (await videoStartStatus != MediaFrameReaderStartStatus.Success)
        {
            Debug.Log("The videoFrameReader couldn't start.");
        }
    }