private RequestDetails MapRequestDetails(MediaTypeInfo detected, ICollection<RestEaseParameter> bodyParameterList, List<RestEaseParameter> extensionMethodParameterList, string bodyParameterDescription) { if (detected.Key == SupportedContentType.MultipartFormData) { string httpContentDescription; if (!Settings.GenerateMultipartFormDataExtensionMethods) { httpContentDescription = "Manually add an extension method to support the exact parameters. See https://github.com/canton7/RestEase#wrapping-other-methods for more info."; } else { httpContentDescription = "An extension method is generated to support the exact parameters."; extensionMethodParameterList.AddRange(detected.Value.Schema.Properties.Select(p => BuildValidParameter(p.Key, p.Value, p.Value.Nullable, p.Value.Description, null))); } bodyParameterList.Add(new RestEaseParameter { Required = true, Identifier = "content", IdentifierWithType = "HttpContent content", IdentifierWithRestEase = "[Body] HttpContent content", Summary = httpContentDescription, IsSpecial = true }); return new RequestDetails { DetectedContentType = detected.Key, IsExtension = true }; } if (detected.Key == SupportedContentType.ApplicationOctetStream) { string httpContentDescription; if (!Settings.GenerateApplicationOctetStreamExtensionMethods) { httpContentDescription = "Manually add an extension method to support the exact parameters. See https://github.com/canton7/RestEase#wrapping-other-methods for more info."; } else { httpContentDescription = "An extension method is generated to support the exact parameters."; var extensionParameter = BuildValidParameter("file", detected.Value.Schema, true, "The content.", null); extensionMethodParameterList.Add(extensionParameter); extensionMethodParameterList.AddRange(detected.Value.Schema.Properties.Select(p => BuildValidParameter(p.Key, p.Value, p.Value.Nullable, p.Value.Description, null))); } bodyParameterList.Add(new RestEaseParameter { Required = true, Identifier = "content", IdentifierWithType = "HttpContent content", IdentifierWithRestEase = "[Body] HttpContent content", Summary = httpContentDescription, IsSpecial = true }); return new RequestDetails { DetectedContentType = detected.Key, IsExtension = true }; } if (detected.Key == SupportedContentType.ApplicationFormUrlEncoded) { string description; if (!Settings.GenerateFormUrlEncodedExtensionMethods) { description = "Manually add an extension method to support the exact parameters."; } else { description = "An extension method is generated to support the exact parameters."; extensionMethodParameterList.AddRange(detected.Value.Schema.Properties.Select(p => BuildValidParameter(p.Key, p.Value, p.Value.Nullable, p.Value.Description, null))); } bodyParameterList.Add(new RestEaseParameter { Required = true, Identifier = "form", IdentifierWithType = "IDictionary<string, object> form", IdentifierWithRestEase = "[Body(BodySerializationMethod.UrlEncoded)] IDictionary<string, object> form", Summary = description, IsSpecial = true }); return new RequestDetails { DetectedContentType = detected.Key, IsExtension = true }; } if (detected.Key == SupportedContentType.ApplicationJson || detected.Key == SupportedContentType.ApplicationXml) { string bodyParameter = null; switch (detected.Value.Schema?.GetSchemaType()) { case SchemaType.Array: string arrayType = detected.Value.Schema.Items.Reference != null ? MakeValidModelName(detected.Value.Schema.Items.Reference.Id) : MapSchema(detected.Value.Schema.Items, null, false, true, null).ToString(); bodyParameter = MapArrayType(arrayType); break; case SchemaType.Object: bodyParameter = MakeValidModelName(detected.Value.Schema?.Reference.Id); break; } if (!string.IsNullOrEmpty(bodyParameter)) { string bodyParameterIdentifierName = "content"; bodyParameterList.Add(new RestEaseParameter { Required = true, Identifier = bodyParameterIdentifierName, IdentifierWithType = $"{bodyParameter} {bodyParameterIdentifierName}", IdentifierWithRestEase = $"[Body] {bodyParameter} {bodyParameterIdentifierName}", Summary = detected.Value.Schema?.Description ?? bodyParameterDescription }); } return new RequestDetails { DetectedContentType = detected.Key }; } return null; }
private RequestDetails MapRequest(OpenApiOperation operation, ICollection<RestEaseParameter> bodyParameterList, List<RestEaseParameter> extensionMethodParameterList) { MediaTypeInfo detected = null; var supportedMediaTypeInfoList = new List<MediaTypeInfo>(); foreach (SupportedContentType key in Enum.GetValues(typeof(SupportedContentType))) { if (TryGetOpenApiMediaType(operation.RequestBody.Content, key, out var mediaType, out var detectedContentType)) { supportedMediaTypeInfoList.Add(new MediaTypeInfo { Key = key, Value = mediaType, ContentType = detectedContentType }); } } if (supportedMediaTypeInfoList.Count == 0) { return null; } if (operation.RequestBody.Content.Count == 1 && supportedMediaTypeInfoList.Count == 1) { detected = supportedMediaTypeInfoList.First(); } else if (operation.RequestBody.Content.Count == supportedMediaTypeInfoList.Count) { if (Settings.PreferredContentType == ContentType.ApplicationXml && supportedMediaTypeInfoList.Any(sc => sc.Key == SupportedContentType.ApplicationXml)) { detected = supportedMediaTypeInfoList.First(sc => sc.Key == SupportedContentType.ApplicationXml); } else { detected = supportedMediaTypeInfoList.First(sc => sc.Key == SupportedContentType.ApplicationJson); } } if (detected == null) { if (Settings.ForceContentTypeToApplicationJson) { detected = supportedMediaTypeInfoList.FirstOrDefault(m => m.Key == SupportedContentType.ApplicationJson) ?? supportedMediaTypeInfoList.First(); var requestDetails = MapRequestDetails(detected, bodyParameterList, extensionMethodParameterList, operation.RequestBody.Description); requestDetails.ContentTypes = new List<string>(); requestDetails.DetectedContentType = SupportedContentType.ApplicationJson; return requestDetails; } else { detected = supportedMediaTypeInfoList.First(); var requestDetails = MapRequestDetails(detected, bodyParameterList, extensionMethodParameterList, operation.RequestBody.Description); requestDetails.ContentTypes = operation.RequestBody.Content.Keys; requestDetails.DetectedContentType = null; return requestDetails; } } return MapRequestDetails(detected, bodyParameterList, extensionMethodParameterList, operation.RequestBody.Description); }
private List <MFVideoFormatContainer> GetSupportedFormats(int SourceIndex, string FriendlyName) { MFDevice UnderlyingDevice = null; List <MFDevice> vcDevices = WMFUtils.GetDevicesByCategory(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, CLSID.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID); foreach (MFDevice device in vcDevices) { if (device.FriendlyName == FriendlyName) { UnderlyingDevice = device; break; } } if (UnderlyingDevice != null) { IMFPresentationDescriptor sourcePresentationDescriptor = null; IMFStreamDescriptor videoStreamDescriptor = null; IMFMediaTypeHandler typeHandler = null; List <MFVideoFormatContainer> formatList = new List <MFVideoFormatContainer>(); HResult hr; IMFMediaSource mediaSource = null; try { // use the device symbolic name to create the media source for the video device. Media sources are objects that generate media data. // For example, the data might come from a video file, a network stream, or a hardware device, such as a camera. Each // media source contains one or more streams, and each stream delivers data of one type, such as audio or video. mediaSource = WMFUtils.GetMediaSourceFromDevice(UnderlyingDevice); if (mediaSource == null) { throw new Exception("DisplayVideoFormatsForCurrentCaptureDevice call to mediaSource == null"); } // A presentation is a set of related media streams that share a common presentation time. // we don't need that functionality in this app but we do need to presentation descriptor // to find out the stream descriptors, these will give us the media types on offer hr = mediaSource.CreatePresentationDescriptor(out sourcePresentationDescriptor); if (hr != HResult.S_OK) { throw new Exception("DisplayVideoFormatsForCurrentCaptureDevice call to mediaSource.CreatePresentationDescriptor failed. Err=" + hr.ToString()); } if (sourcePresentationDescriptor == null) { throw new Exception("DisplayVideoFormatsForCurrentCaptureDevice call to mediaSource.CreatePresentationDescriptor failed. sourcePresentationDescriptor == null"); } // Now we get the number of stream descriptors in the presentation. // A presentation descriptor contains a list of one or more // stream descriptors. hr = sourcePresentationDescriptor.GetStreamDescriptorCount(out int sourceStreamCount); if (hr != HResult.S_OK) { throw new Exception("DisplayVideoFormatsForCurrentCaptureDevice call to sourcePresentationDescriptor.GetStreamDescriptorCount failed. Err=" + hr.ToString()); } if (sourceStreamCount == 0) { throw new Exception("DisplayVideoFormatsForCurrentCaptureDevice call to sourcePresentationDescriptor.GetStreamDescriptorCount failed. sourceStreamCount == 0"); } // look for the video stream // we require the major type to be video Guid guidMajorType = WMFUtils.GetMajorMediaTypeFromPresentationDescriptor(sourcePresentationDescriptor, SourceIndex); if (guidMajorType != MFMediaType.Video) { return(new List <MFVideoFormatContainer>()); } // we also require the stream to be enabled sourcePresentationDescriptor.SelectStream(1); hr = sourcePresentationDescriptor.GetStreamDescriptorByIndex(SourceIndex, out bool streamIsSelected, out videoStreamDescriptor); if (hr != HResult.S_OK) { throw new Exception("DisplayVideoFormatsForCurrentCaptureDevice call to sourcePresentationDescriptor.GetStreamDescriptorByIndex(v) failed. Err=" + hr.ToString()); } if (videoStreamDescriptor == null) { throw new Exception("DisplayVideoFormatsForCurrentCaptureDevice call to sourcePresentationDescriptor.GetStreamDescriptorByIndex(v) failed. videoStreamDescriptor == null"); } // if the stream is not selected (enabled) look for the next if (streamIsSelected == false) { Marshal.ReleaseComObject(videoStreamDescriptor); videoStreamDescriptor = null; return(new List <MFVideoFormatContainer>()); } // Get the media type handler for the stream. IMFMediaTypeHandler // interface is a standard way of looking at the media types on an stream hr = videoStreamDescriptor.GetMediaTypeHandler(out typeHandler); if (hr != HResult.S_OK) { throw new Exception("call to videoStreamDescriptor.GetMediaTypeHandler failed. Err=" + hr.ToString()); } if (typeHandler == null) { throw new Exception("call to videoStreamDescriptor.GetMediaTypeHandler failed. typeHandler == null"); } // Now we get the number of media types in the stream descriptor. hr = typeHandler.GetMediaTypeCount(out int mediaTypeCount); if (hr != HResult.S_OK) { throw new Exception("DisplayVideoFormatsForCurrentCaptureDevice call to typeHandler.GetMediaTypeCount failed. Err=" + hr.ToString()); } if (mediaTypeCount == 0) { throw new Exception("DisplayVideoFormatsForCurrentCaptureDevice call to typeHandler.GetMediaTypeCount failed. mediaTypeCount == 0"); } // now loop through each media type for (int mediaTypeId = 0; mediaTypeId < mediaTypeCount; mediaTypeId++) { // Now we have the handler, get the media type. hr = typeHandler.GetMediaTypeByIndex(mediaTypeId, out IMFMediaType workingMediaType); if (hr != HResult.S_OK) { throw new Exception("GetMediaTypeFromStreamDescriptorById call to typeHandler.GetMediaTypeByIndex failed. Err=" + hr.ToString()); } if (workingMediaType == null) { throw new Exception("GetMediaTypeFromStreamDescriptorById call to typeHandler.GetMediaTypeByIndex failed. workingMediaType == null"); } MFVideoFormatContainer tmpContainer = MediaTypeInfo.GetVideoFormatContainerFromMediaTypeObject(workingMediaType, UnderlyingDevice); if (tmpContainer == null) { // we failed throw new Exception("GetSupportedVideoFormatsFromSourceReaderInFormatContainers failed on call to GetVideoFormatContainerFromMediaTypeObject"); } // now add it formatList.Add(tmpContainer); Marshal.ReleaseComObject(workingMediaType); workingMediaType = null; } return(formatList); } finally { // close and release if (mediaSource != null) { Marshal.ReleaseComObject(mediaSource); } if (sourcePresentationDescriptor != null) { Marshal.ReleaseComObject(sourcePresentationDescriptor); } if (videoStreamDescriptor != null) { Marshal.ReleaseComObject(videoStreamDescriptor); } if (typeHandler != null) { Marshal.ReleaseComObject(typeHandler); } } } return(new List <MFVideoFormatContainer>()); }