/// <summary> /// Gets the current frame size /// </summary> /// <returns>The frame size in a bitmap info header</returns> private BitmapInfoHeader GetFrameSize() { BitmapInfoHeader bmiHeader = null; try { IntPtr pmt = IntPtr.Zero; AMMediaType mediaType = new AMMediaType(); try { // Get the current format info mediaType.formatType = FormatType.VideoInfo2; int hr = _streamConfig.GetFormat(out mediaType); if (hr != 0) { Log.Log.Info("GetFrameSize: FAILED to get format - {0}", hr); Marshal.ThrowExceptionForHR(hr); return(bmiHeader); } // The formatPtr member points to different structures // dependingon the formatType if (mediaType.formatType == FormatType.VideoInfo) { VideoInfoHeader temp = new VideoInfoHeader(); Marshal.PtrToStructure(mediaType.formatPtr, temp); bmiHeader = temp.BmiHeader; } else if (mediaType.formatType == FormatType.VideoInfo2) { VideoInfoHeader2 temp = new VideoInfoHeader2(); Marshal.PtrToStructure(mediaType.formatPtr, temp); bmiHeader = temp.BmiHeader; } else if (mediaType.formatType == FormatType.Mpeg2Video) { MPEG2VideoInfo temp = new MPEG2VideoInfo(); Marshal.PtrToStructure(mediaType.formatPtr, temp); bmiHeader = temp.hdr.BmiHeader; } else if (mediaType.formatType == FormatType.MpegVideo) { MPEG1VideoInfo temp = new MPEG1VideoInfo(); Marshal.PtrToStructure(mediaType.formatPtr, temp); bmiHeader = temp.hdr.BmiHeader; } } finally { Marshal.FreeCoTaskMem(pmt); } } catch (Exception) { Log.Log.Info(" VideoCaptureDevice.getStreamConfigSetting() FAILED "); } return(bmiHeader); }
/// <summary> /// Erzeugt den Datentyp für ein Bildsignal. /// </summary> /// <param name="mpeg4">Gesetzt für HDTV.</param> /// <param name="useCyberlink">Gesetzt für die Nutzung des Cyberlink Formates.</param> /// <returns>Das gewünschte Datenformat.</returns> private static MediaType CreateVideoType(bool mpeg4, bool?useCyberlink) { // Create format var fakeFormat = new MPEG2VideoInfo { BitmpapSize = 40 }; // Resulting media type MediaType type; // Check mode if (mpeg4) { // Set it up fakeFormat.BitmapWidth = 1980; fakeFormat.BitmapHeight = 1088; // Check flag if (!useCyberlink.HasValue) { // See if we should use cyberlink format specs string cl = ConfigurationManager.AppSettings["UseCyberLink"]; // Copy if set useCyberlink = (!string.IsNullOrEmpty(cl) && bool.Parse(cl)); } // Check mode if (useCyberlink.Value) { // Create media type for video type = new MediaType(Constants.KSDATAFORMAT_TYPE_VIDEO, Constants.KSDATAFORMAT_SUBTYPE_H264_VIDEO_Cyberlink, Constants.FORMAT_MPEG2_VIDEO, true, 0x10000); } else { // Create media type for video type = new MediaType(Constants.KSDATAFORMAT_TYPE_VIDEO, Constants.KSDATAFORMAT_SUBTYPE_H264_VIDEO, Constants.FORMAT_MPEG2_VIDEO, true, 0x10000); } } else { // Set it up fakeFormat.BitmapWidth = 720; fakeFormat.BitmapHeight = 576; // Create media type for video type = new MediaType(Constants.KSDATAFORMAT_TYPE_VIDEO, Constants.KSDATAFORMAT_SUBTYPE_MPEG2_VIDEO, Constants.FORMAT_MPEG2_VIDEO, true, 0x10000); } // Finish type.SetFormat(fakeFormat); // Report return(type); }
private object SetStreamConfigSetting(IAMStreamConfig streamConfig, string fieldName, object newValue) { try { object returnValue = null; IntPtr pmt = IntPtr.Zero; AMMediaType mediaType = new AMMediaType(); try { // Get the current format info int hr = streamConfig.GetFormat(out mediaType); if (hr != 0) { AppLogger.Message(String.Format(" VideoCaptureDevice:setStreamConfigSetting() FAILED to set:{0} (getformat) hr:{1}", fieldName, hr)); return(null);//Marshal.ThrowExceptionForHR(hr); } //Log.Info(" VideoCaptureDevice:setStreamConfigSetting() get formattype"); // The formatPtr member points to different structures // dependingon the formatType object formatStruct; if (mediaType.formatType == FormatType.WaveEx) { formatStruct = new WaveFormatEx(); } else if (mediaType.formatType == FormatType.VideoInfo) { formatStruct = new VideoInfoHeader(); } else if (mediaType.formatType == FormatType.VideoInfo2) { formatStruct = new VideoInfoHeader2(); } else if (mediaType.formatType == FormatType.Mpeg2Video) { formatStruct = new MPEG2VideoInfo(); } else if (mediaType.formatType == FormatType.None) { AppLogger.Message(" VideoCaptureDevice:setStreamConfigSetting() FAILED no format returned"); return(null);// throw new NotSupportedException("This device does not support a recognized format block."); } else { AppLogger.Message(" VideoCaptureDevice:setStreamConfigSetting() FAILED unknown fmt"); return(null);//throw new NotSupportedException("This device does not support a recognized format block."); } //Log.Info(" VideoCaptureDevice.setStreamConfigSetting() get formatptr"); // Retrieve the nested structure Marshal.PtrToStructure(mediaType.formatPtr, formatStruct); // Find the required field //Log.Info(" VideoCaptureDevice.setStreamConfigSetting() get field"); Type structType = formatStruct.GetType(); FieldInfo fieldInfo = structType.GetField(fieldName); if (fieldInfo == null) { AppLogger.Message(String.Format(" VideoCaptureDevice:setStreamConfigSetting() FAILED to to find member:{0}", fieldName)); throw new NotSupportedException("FAILED to find the member '" + fieldName + "' in the format block."); } //Log.Info(" VideoCaptureDevice.setStreamConfigSetting() set value"); // Update the value of the field fieldInfo.SetValue(formatStruct, newValue); // PtrToStructure copies the data so we need to copy it back Marshal.StructureToPtr(formatStruct, mediaType.formatPtr, false); //Log.Info(" VideoCaptureDevice.setStreamConfigSetting() set format"); // Save the changes hr = streamConfig.SetFormat(mediaType); if (hr != 0) { AppLogger.Message(String.Format(" VideoCaptureDevice:setStreamConfigSetting() FAILED to set:{0} {1}", fieldName, hr)); return(null);//Marshal.ThrowExceptionForHR(hr); } //else Log.Info(" VideoCaptureDevice.setStreamConfigSetting() set:{0}",fieldName); //Log.Info(" VideoCaptureDevice.setStreamConfigSetting() done"); } finally { Marshal.FreeCoTaskMem(pmt); } return(returnValue); } catch (Exception) { AppLogger.Message(" VideoCaptureDevice.:setStreamConfigSetting() FAILED "); } return(null); }
private object GetStreamConfigSetting(IAMStreamConfig streamConfig, string fieldName) { object returnValue = null; try { if (streamConfig == null) { throw new NotSupportedException(); } IntPtr pmt = IntPtr.Zero; AMMediaType mediaType = new AMMediaType(); try { // Get the current format info mediaType.formatType = FormatType.VideoInfo2; int hr = streamConfig.GetFormat(out mediaType); if (hr != 0) { AppLogger.Message(String.Format("VideoCaptureDevice:getStreamConfigSetting() FAILED to get:{0} (not supported)", fieldName)); Marshal.ThrowExceptionForHR(hr); } // The formatPtr member points to different structures // dependingon the formatType object formatStruct; //Log.Info(" VideoCaptureDevice.getStreamConfigSetting() find formattype"); if (mediaType.formatType == FormatType.WaveEx) { formatStruct = new WaveFormatEx(); } else if (mediaType.formatType == FormatType.VideoInfo) { formatStruct = new VideoInfoHeader(); } else if (mediaType.formatType == FormatType.VideoInfo2) { formatStruct = new VideoInfoHeader2(); } else if (mediaType.formatType == FormatType.Mpeg2Video) { formatStruct = new MPEG2VideoInfo(); } else if (mediaType.formatType == FormatType.None) { //Log.Info("VideoCaptureDevice:getStreamConfigSetting() FAILED no format returned"); //throw new NotSupportedException("This device does not support a recognized format block."); return(null); } else { //Log.Info("VideoCaptureDevice:getStreamConfigSetting() FAILED unknown fmt:{0} {1} {2}", mediaType.formatType, mediaType.majorType, mediaType.subType); //throw new NotSupportedException("This device does not support a recognized format block."); return(null); } //Log.Info(" VideoCaptureDevice.getStreamConfigSetting() get formatptr"); // Retrieve the nested structure Marshal.PtrToStructure(mediaType.formatPtr, formatStruct); // Find the required field //Log.Info(" VideoCaptureDevice.getStreamConfigSetting() get field"); Type structType = formatStruct.GetType(); FieldInfo fieldInfo = structType.GetField(fieldName); if (fieldInfo == null) { //Log.Info("VideoCaptureDevice.getStreamConfigSetting() FAILED to to find member:{0}", fieldName); //throw new NotSupportedException("VideoCaptureDevice:FAILED to find the member '" + fieldName + "' in the format block."); return(null); } // Extract the field's current value //Log.Info(" VideoCaptureDevice.getStreamConfigSetting() get value"); returnValue = fieldInfo.GetValue(formatStruct); //Log.Info(" VideoCaptureDevice.getStreamConfigSetting() done"); } finally { Marshal.FreeCoTaskMem(pmt); } } catch (Exception) { AppLogger.Message(" VideoCaptureDevice.getStreamConfigSetting() FAILED "); } return(returnValue); }
/// <summary> /// Erzeugt den Datentyp für ein Bildsignal. /// </summary> /// <param name="mpeg4">Gesetzt für HDTV.</param> /// <param name="useCyberlink">Gesetzt für die Nutzung des Cyberlink Formates.</param> /// <returns>Das gewünschte Datenformat.</returns> private static MediaType CreateVideoType( bool mpeg4, bool? useCyberlink ) { // Create format var fakeFormat = new MPEG2VideoInfo { BitmpapSize = 40 }; // Resulting media type MediaType type; // Check mode if (mpeg4) { // Set it up fakeFormat.BitmapWidth = 1980; fakeFormat.BitmapHeight = 1088; // Check flag if (!useCyberlink.HasValue) { // See if we should use cyberlink format specs string cl = ConfigurationManager.AppSettings["UseCyberLink"]; // Copy if set useCyberlink = (!string.IsNullOrEmpty( cl ) && bool.Parse( cl )); } // Check mode if (useCyberlink.Value) { // Create media type for video type = new MediaType( Constants.KSDATAFORMAT_TYPE_VIDEO, Constants.KSDATAFORMAT_SUBTYPE_H264_VIDEO_Cyberlink, Constants.FORMAT_MPEG2_VIDEO, true, 0x10000 ); } else { // Create media type for video type = new MediaType( Constants.KSDATAFORMAT_TYPE_VIDEO, Constants.KSDATAFORMAT_SUBTYPE_H264_VIDEO, Constants.FORMAT_MPEG2_VIDEO, true, 0x10000 ); } } else { // Set it up fakeFormat.BitmapWidth = 720; fakeFormat.BitmapHeight = 576; // Create media type for video type = new MediaType( Constants.KSDATAFORMAT_TYPE_VIDEO, Constants.KSDATAFORMAT_SUBTYPE_MPEG2_VIDEO, Constants.FORMAT_MPEG2_VIDEO, true, 0x10000 ); } // Finish type.SetFormat( fakeFormat ); // Report return type; }
/// <summary> /// Sets the frame size /// </summary> /// <param name="bmiHeader">The bitmap info header with the frame size</param> /// <returns>true, if it was successful; false otherwise</returns> private bool SetFrameSize(BitmapInfoHeader bmiHeader) { try { IntPtr pmt = IntPtr.Zero; AMMediaType mediaType; try { // Get the current format info int hr = _streamConfig.GetFormat(out mediaType); if (hr != 0) { Log.Log.Info("SetFrameSize: Failed to get the video format - {0}", hr); return(false); } // The formatPtr member points to different structures // dependingon the formatType object formatStruct; if (mediaType.formatType == FormatType.VideoInfo) { VideoInfoHeader temp = new VideoInfoHeader(); Marshal.PtrToStructure(mediaType.formatPtr, temp); temp.BmiHeader = bmiHeader; formatStruct = temp; } else if (mediaType.formatType == FormatType.VideoInfo2) { VideoInfoHeader2 temp = new VideoInfoHeader2(); Marshal.PtrToStructure(mediaType.formatPtr, temp); temp.BmiHeader = bmiHeader; formatStruct = temp; } else if (mediaType.formatType == FormatType.Mpeg2Video) { MPEG2VideoInfo temp = new MPEG2VideoInfo(); Marshal.PtrToStructure(mediaType.formatPtr, temp); temp.hdr.BmiHeader = bmiHeader; formatStruct = temp; } else if (mediaType.formatType == FormatType.MpegVideo) { MPEG1VideoInfo temp = new MPEG1VideoInfo(); Marshal.PtrToStructure(mediaType.formatPtr, temp); temp.hdr.BmiHeader = bmiHeader; formatStruct = temp; } else if (mediaType.formatType == FormatType.None) { Log.Log.Info("SetFrameSize: FAILED no format returned"); return(false); } else { Log.Log.Info("SetFrameSize: FAILED unknown fmt"); return(false); } // PtrToStructure copies the data so we need to copy it back Marshal.StructureToPtr(formatStruct, mediaType.formatPtr, false); // Save the changes hr = _streamConfig.SetFormat(mediaType); if (hr != 0) { Log.Log.Info("SetFrameSize: FAILED to set:{0}", hr); return(false); } } finally { Marshal.FreeCoTaskMem(pmt); } return(true); } catch (Exception) { Log.Log.Info("SetFrameSize: FAILED "); } return(false); }