private void SetConfigParameters(ICaptureGraphBuilder2 captureGraphBuilder, IBaseFilter captureFilter, int frameRate, int width, int height) { object outObject; int hr = captureGraphBuilder.FindInterface(PinCategory.Capture, MediaType.Video, captureFilter, typeof(IAMStreamConfig).GUID, out outObject); IAMStreamConfig videoStreamConfig = outObject as IAMStreamConfig; if (videoStreamConfig == null) { throw new Exception("Failed to get IAMStreamConfig"); } AMMediaType outMedia; hr = videoStreamConfig.GetFormat(out outMedia); DsError.ThrowExceptionForHR(hr); VideoInfoHeader videoInfoHeader = new VideoInfoHeader(); Marshal.PtrToStructure(outMedia.formatPtr, videoInfoHeader); videoInfoHeader.AvgTimePerFrame = 10000000 / frameRate; videoInfoHeader.BmiHeader.Width = width; videoInfoHeader.BmiHeader.Height = height; Marshal.StructureToPtr(videoInfoHeader, outMedia.formatPtr, false); hr = videoStreamConfig.SetFormat(outMedia); DsError.ThrowExceptionForHR(hr); DsUtils.FreeAMMediaType(outMedia); outMedia = null; }
/// <summary> /// Configure the sample grabber with default Video RGB24 mode. /// </summary> /// <param name="sampGrabber">The <see cref="ISampleGrabber"/> to be configured.</param> private void ConfigureSampleGrabber(ISampleGrabber sampGrabber) { AMMediaType media; int hr; // Set the media type to Video/RBG24 media = new AMMediaType(); media.majorType = MediaType.Video; media.subType = MediaSubType.RGB24; media.formatType = FormatType.VideoInfo; hr = this.sampGrabber.SetMediaType(media); //if (hr != 0) // ErrorLogger.WriteLine("Could not ConfigureSampleGrabber in Camera.Capture. Message: " + // DsError.GetErrorText(hr)); DsUtils.FreeAMMediaType(media); media = null; // Configure the samplegrabber hr = this.sampGrabber.SetCallback(this, 1); //if (hr != 0) // ErrorLogger.WriteLine("Could not set callback method for sampleGrabber in Camera.Capture. Message: " + // DsError.GetErrorText(hr)); }
void SetFormat(int formatIndex, int frameRate) { int capsCount, capSize; int hr = VideoConfig.GetNumberOfCapabilities(out capsCount, out capSize); DsError.ThrowExceptionForHR(hr); IntPtr pSC = Marshal.AllocHGlobal(capSize); AMMediaType mt = null; try { VideoInfoHeader vih = new VideoInfoHeader(); hr = VideoConfig.GetStreamCaps(formatIndex, out mt, pSC); DsError.ThrowExceptionForHR(hr); if (frameRate > 0) { Marshal.PtrToStructure(mt.formatPtr, vih); vih.AvgTimePerFrame = (long)(10000000.0 / frameRate); Marshal.StructureToPtr(vih, mt.formatPtr, false); } hr = VideoConfig.SetFormat(mt); DsError.ThrowExceptionForHR(hr); } finally { DsUtils.FreeAMMediaType(mt); Marshal.FreeHGlobal(pSC); } }
/// <summary> /// Gets the pin media types. /// </summary> /// <returns>Returns collection of supported media types.</returns> public IEnumerable <AMMediaType> GetMediaTypes() { IEnumMediaTypes mediaTypes = null; try { var hr = Object.EnumMediaTypes(out mediaTypes); DsError.ThrowExceptionForHR(hr); var currentMediaType = new AMMediaType[1]; while (mediaTypes.Next(1, currentMediaType, out _) == 0) { try { yield return(currentMediaType[0]); } finally { DsUtils.FreeAMMediaType(currentMediaType[0]); } } } finally { if (mediaTypes != null) { DsUtils.ReleaseComObject(mediaTypes); } } }
// Set the Framerate, and video size private void SetConfigParms(ICaptureGraphBuilder2 capGraph, IBaseFilter capFilter, AMMediaType media) { int hr; object o; // Find the stream config interface hr = capGraph.FindInterface( PinCategory.Capture, MediaType.Video, capFilter, typeof(IAMStreamConfig).GUID, out o); IAMStreamConfig videoStreamConfig = o as IAMStreamConfig; if (videoStreamConfig == null) { throw new Exception("Failed to get IAMStreamConfig"); } // Set the new format try { hr = videoStreamConfig.SetFormat(media); } catch { } DsError.ThrowExceptionForHR(hr); DsUtils.FreeAMMediaType(media); media = null; }
internal AudioCapabilities(IAMStreamConfig audioStreamConfig) { if (audioStreamConfig == null) { throw new ArgumentNullException("audioStreamConfig"); } AMMediaType mediaType = null; AudioStreamConfigCaps caps = null; IntPtr zero = IntPtr.Zero; try { IntPtr ptr2; int num; int num2; int numberOfCapabilities = audioStreamConfig.GetNumberOfCapabilities(out num, out num2); if (numberOfCapabilities != 0) { Marshal.ThrowExceptionForHR(numberOfCapabilities); } if (num <= 0) { throw new NotSupportedException("This audio device does not report capabilities."); } if (num2 > Marshal.SizeOf(typeof(AudioStreamConfigCaps))) { throw new NotSupportedException("Unable to retrieve audio device capabilities. This audio device requires a larger AudioStreamConfigCaps structure."); } zero = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(AudioStreamConfigCaps))); numberOfCapabilities = audioStreamConfig.GetStreamCaps(0, out ptr2, zero); if (numberOfCapabilities != 0) { Marshal.ThrowExceptionForHR(numberOfCapabilities); } mediaType = (AMMediaType)Marshal.PtrToStructure(ptr2, typeof(AMMediaType)); caps = (AudioStreamConfigCaps)Marshal.PtrToStructure(zero, typeof(AudioStreamConfigCaps)); this.MinimumChannels = caps.MinimumChannels; this.MaximumChannels = caps.MaximumChannels; this.ChannelsGranularity = caps.ChannelsGranularity; this.MinimumSampleSize = caps.MinimumBitsPerSample; this.MaximumSampleSize = caps.MaximumBitsPerSample; this.SampleSizeGranularity = caps.BitsPerSampleGranularity; this.MinimumSamplingRate = caps.MinimumSampleFrequency; this.MaximumSamplingRate = caps.MaximumSampleFrequency; this.SamplingRateGranularity = caps.SampleFrequencyGranularity; } finally { if (zero != IntPtr.Zero) { Marshal.FreeCoTaskMem(zero); } zero = IntPtr.Zero; if (mediaType != null) { DsUtils.FreeAMMediaType(mediaType); } mediaType = null; } }
/// <summary> /// Gets and saves mode (mediatype, format type and etc). /// </summary> public void SaveMode() { int hr; // Get the media type from the SampleGrabber AMMediaType media = new AMMediaType(); hr = m_SampleGrabber.GetConnectedMediaType(media); DsError.ThrowExceptionForHR(hr); if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero)) { throw new NotSupportedException("Unknown Grabber Media Format"); } // Grab the size info VideoInfoHeader videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader)); m_videoWidth = videoInfoHeader.BmiHeader.Width; m_videoHeight = videoInfoHeader.BmiHeader.Height; m_videoBitCount = videoInfoHeader.BmiHeader.BitCount; m_ImageSize = videoInfoHeader.BmiHeader.ImageSize; DsUtils.FreeAMMediaType(media); media = null; }
public void Dispose() { if (!this.bDisposed) { if (null != builder) { Marshal.ReleaseComObject(builder); builder = null; } if (null != grabber) { Marshal.ReleaseComObject(grabber); grabber = null; } if (null != mediaType) { DsUtils.FreeAMMediaType(mediaType); mediaType = null; } if (samplePtr != IntPtr.Zero) { Marshal.FreeHGlobal(samplePtr); } GC.SuppressFinalize(this); this.bDisposed = true; } }
// Save the size parameters for use in SnapShot private void SaveSizeInfo(ISampleGrabber sampGrabber) { int hr; // Get the media type from the SampleGrabber AMMediaType media = new AMMediaType(); hr = sampGrabber.GetConnectedMediaType(media); DsError.ThrowExceptionForHR(hr); try { if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero)) { throw new NotSupportedException("Unknown Grabber Media Format"); } // Get the struct VideoInfoHeader videoInfoHeader = new VideoInfoHeader(); Marshal.PtrToStructure(media.formatPtr, videoInfoHeader); // Grab the size info m_videoWidth = videoInfoHeader.BmiHeader.Width; m_videoHeight = videoInfoHeader.BmiHeader.Height; m_stride = videoInfoHeader.BmiHeader.ImageSize / m_videoHeight; m_ImageSize = videoInfoHeader.BmiHeader.ImageSize; } finally { DsUtils.FreeAMMediaType(media); media = null; } }
private void ConfStreamDimensions(IAMStreamConfig streamConfig) { AMMediaType media = null; DsError.ThrowExceptionForHR(streamConfig.GetFormat(out media)); try { VideoInfoHeader v = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader)); if (m_configuration.Size.Width > 0) { v.BmiHeader.Width = m_configuration.Size.Width; } if (m_configuration.Size.Height > 0) { v.BmiHeader.Height = m_configuration.Size.Height; } if (m_configuration.BPP > 0) { v.BmiHeader.BitCount = m_configuration.BPP; } if (m_configuration.MediaSubtype != Guid.Empty) { media.subType = m_configuration.MediaSubtype; } //v.AvgTimePerFrame = 10000000 / 30; // 30 fps. FPS might be controlled by the camera, because of lightning exposure may increase and FPS decrease. Marshal.StructureToPtr(v, media.formatPtr, false); DsError.ThrowExceptionForHR(streamConfig.SetFormat(media)); } finally { DsUtils.FreeAMMediaType(media); media = null; } }
private void SetupStillPin(int RequestedHeight, int RequestedWidth, short iBPP) { int hr; if (RequestedHeight + RequestedWidth + iBPP > 0) { SetPinParameters(m_pinStill, RequestedWidth, RequestedHeight, iBPP); } stillSampleGrabber = new SampleGrabber() as ISampleGrabber; IBaseFilter baseStillGrabFlt = stillSampleGrabber as IBaseFilter; ConfigureSampleGrabber(stillSampleGrabber, 0); pStillSampleIn = DsFindPin.ByDirection(baseStillGrabFlt, PinDirection.Input, 0); hr = m_FilterGraph.AddFilter(baseStillGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.Connect(m_pinStill, pStillSampleIn); DsError.ThrowExceptionForHR(hr); hr = ((ISampleGrabber)stillSampleGrabber).SetOneShot(true); DsError.ThrowExceptionForHR(hr); AMMediaType media = new AMMediaType(); hr = stillSampleGrabber.GetConnectedMediaType(media); DsError.ThrowExceptionForHR(hr); if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero)) { throw new NotSupportedException("Unknown Grabber Media Format"); } DsUtils.FreeAMMediaType(media); mediaCtrl = m_FilterGraph as IMediaControl; hr = mediaCtrl.Run(); DsError.ThrowExceptionForHR(hr); }
private void SetPinParameters(IPin pin, int iWidth, int iHeight, short iBPP) { int hr; IAMStreamConfig videoStreamConfig = pin as IAMStreamConfig; hr = videoStreamConfig.GetFormat(out AMMediaType media); DsError.ThrowExceptionForHR(hr); try { VideoInfoHeader v = new VideoInfoHeader(); Marshal.PtrToStructure(media.formatPtr, v); if (iWidth > 0) { v.BmiHeader.Width = iWidth; } if (iHeight > 0) { v.BmiHeader.Height = iHeight; } if (iBPP > 0) { v.BmiHeader.BitCount = iBPP; } media.majorType = MediaType.Video; media.subType = MediaSubType.MJPG; Marshal.StructureToPtr(v, media.formatPtr, false); hr = videoStreamConfig.SetFormat(media); DsError.ThrowExceptionForHR(hr); } finally { DsUtils.FreeAMMediaType(media); } }
/// <summary> /// Read and store the properties /// </summary> /// <param name="sampGrabber">The samp grabber.</param> private void SaveSizeInfo(ISampleGrabber sampGrabber) { int hr; // Get the media type from the SampleGrabber AMMediaType media = new AMMediaType(); hr = sampGrabber.GetConnectedMediaType(media); checkHR(hr, "SaveSizeInfo"); if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero)) { throw new NotSupportedException("Unknown Grabber Media Format"); } // Grab the size info VideoInfoHeader videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader)); _SGvideoWidth = videoInfoHeader.BmiHeader.Width; _SGvideoHeight = videoInfoHeader.BmiHeader.Height; _SGstride = _SGvideoWidth * (videoInfoHeader.BmiHeader.BitCount / 8); _hud.VideoHeight = _SGvideoHeight; _hud.VideoWidth = _SGvideoWidth; DsUtils.FreeAMMediaType(media); media = null; }
private void SaveSizeInfo(ISampleGrabber sampGrabber) { int hr; // Get the media type from the SampleGrabber AMMediaType media = new AMMediaType(); hr = sampGrabber.GetConnectedMediaType(media); DsError.ThrowExceptionForHR(hr); if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero)) { throw new NotSupportedException("Unknown Grabber Media Format"); } // Grab the size info VideoInfoHeader videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader)); m_videoWidth = videoInfoHeader.BmiHeader.Width; m_videoHeight = videoInfoHeader.BmiHeader.Height; m_stride = m_videoWidth * (videoInfoHeader.BmiHeader.BitCount / 8); m_avgtimeperframe = videoInfoHeader.AvgTimePerFrame; DsUtils.FreeAMMediaType(media); media = null; }
/// <summary> /// Configures mode (mediatype, format type and etc). /// </summary> public void ConfigureMode() { int hr; AMMediaType media = new AMMediaType(); // Set the media type to Video/RBG24 media.majorType = MediaType.Video; media.subType = MediaSubType.RGB24; media.formatType = FormatType.VideoInfo; hr = m_SampleGrabber.SetMediaType(media); DsError.ThrowExceptionForHR(hr); DsUtils.FreeAMMediaType(media); media = null; // Configure the samplegrabber // To save current frame via SnapshotNextFrame //ISampleGrabber::SetCallback method // Note [Deprecated. This API may be removed from future releases of Windows.] // http://msdn.microsoft.com/en-us/library/windows/desktop/dd376992%28v=vs.85%29.aspx hr = m_SampleGrabber.SetCallback(this, 1); // 1 == WhichMethodToCallback, call the ISampleGrabberCB::BufferCB method DsError.ThrowExceptionForHR(hr); // To save current frame via SnapshotCurrentFrame if (m_bBufferSamplesOfCurrentFrame) { //ISampleGrabber::SetBufferSamples method // Note [Deprecated. This API may be removed from future releases of Windows.] // http://msdn.microsoft.com/en-us/windows/dd376991 hr = m_SampleGrabber.SetBufferSamples(true); DsError.ThrowExceptionForHR(hr); } }
public IGroup AddVideoGroup(string name, double fps, short bitCount, int width, int height) { OnBeforeGroupAdded(); AMMediaType mediaType = null; try { mediaType = MediaTypeUtils.GetVideoMediaType(bitCount, width, height); IGroup group = new Group(this, GroupType.Video, mediaType, name, fps); _groups.Add(group); AttachHandlers(group); OnAfterGroupAdded(group); return(group); } finally { if (mediaType != null) { DsUtils.FreeAMMediaType(mediaType); } } }
private int VideoIndex(IPin pPin) { int hr; int bRet = -1; AMMediaType[] pmt = new AMMediaType[1]; IEnumMediaTypes ppEnum; // Walk the MediaTypes for the pin hr = pPin.EnumMediaTypes(out ppEnum); DsError.ThrowExceptionForHR(hr); try { do { bRet++; // Just read the first one hr = ppEnum.Next(1, pmt, IntPtr.Zero); DsError.ThrowExceptionForHR(hr); } while (pmt[0].majorType != MediaType.Video); } catch { bRet = -1; } finally { Marshal.ReleaseComObject(ppEnum); } DsUtils.FreeAMMediaType(pmt[0]); return(bRet); }
public static bool IsVideo(IPin pin) { if (pin == null) { throw new ArgumentNullException("pin"); } int hr; bool bRet = false; var pmt = new AMMediaType[1]; IEnumMediaTypes ppEnum; IntPtr i = IntPtr.Zero; // Walk the MediaTypes for the pin hr = pin.EnumMediaTypes(out ppEnum); DESError.ThrowExceptionForHR(hr); try { // Just read the first one hr = ppEnum.Next(1, pmt, i); DESError.ThrowExceptionForHR(hr); bRet = pmt[0].majorType == MediaType.Video; } finally { Marshal.ReleaseComObject(ppEnum); } DsUtils.FreeAMMediaType(pmt[0]); return(bRet); }
public void Dispose() { timer.Stop(); CloseInterfaces(); DsUtils.FreeAMMediaType(currentFormat); }
/// <summary> /// Sets the number of DVD buttons found in the current DVD video /// </summary> /// <param name="buttonCount">The total number of buttons</param> private void SetDvdButtonCount(int buttonCount) { m_dvdButtonCount = buttonCount; if (m_dvdButtonCount == 0) { m_isOverButton = false; InvokeOnOverDvdButton(m_isOverButton); } var mediaType = new AMMediaType(); m_dummyRendererPin.ConnectionMediaType(mediaType); /* Check to see if its a video media type */ if (mediaType.formatType != FormatType.VideoInfo2 && mediaType.formatType != FormatType.VideoInfo) { DsUtils.FreeAMMediaType(mediaType); return; } var videoInfo = new VideoInfoHeader(); /* Read the video info header struct from the native pointer */ Marshal.PtrToStructure(mediaType.formatPtr, videoInfo); /* Get the target rect */ m_renderTargetRect = videoInfo.TargetRect.ToRectangle(); DsUtils.FreeAMMediaType(mediaType); }
public IGroup AddAudioGroup(string name, double fps) { OnBeforeGroupAdded(); AMMediaType mediaType = null; try { mediaType = MediaTypeUtils.GetAudioMediaType(); IGroup group = new Group(this, GroupType.Audio, mediaType, name, fps); _groups.Add(group); AttachHandlers(group); OnAfterGroupAdded(group); return(group); } finally { if (mediaType != null) { DsUtils.FreeAMMediaType(mediaType); } } }
/// <summary> /// Set the Framerate, and video size /// </summary> /// <param name="capGraph">The <see cref="ICaptureGraphBuilder2"/> interface.</param> /// <param name="capFilter">The <see cref="IBaseFilter"/> of the capture device.</param> /// <param name="frameRate">The new framerate to be used.</param> /// <param name="width">The new video width to be used.</param> /// <param name="height">The new video height to be used.</param> private void SetConfigParms(ICaptureGraphBuilder2 capGraph, IBaseFilter capFilter, int frameRate, int width, int height) { int hr; object o; AMMediaType media; // Find the stream config interface hr = this.capGraph.FindInterface(PinCategory.Capture, MediaType.Video, capFilter, typeof(IAMStreamConfig).GUID, out o); this.videoControl = capFilter as IAMVideoControl; this.videoStreamConfig = o as IAMStreamConfig; if (this.videoStreamConfig == null) { ErrorLogger.WriteLine("Error in Capture.SetConfigParams(). Failed to get IAMStreamConfig"); } // Get the existing format block hr = this.videoStreamConfig.GetFormat(out media); if (hr != 0) { ErrorLogger.WriteLine("Could not SetConfigParms in Camera.Capture. Message: " + DsError.GetErrorText(hr)); } // copy out the videoinfoheader VideoInfoHeader v = new VideoInfoHeader(); Marshal.PtrToStructure(media.formatPtr, v); // if overriding set values if (frameRate > 0) { v.AvgTimePerFrame = 10000000 / frameRate; } if (width > 0) { v.BmiHeader.Width = width; } if (height > 0) { v.BmiHeader.Height = height; } // Copy the media structure back Marshal.StructureToPtr(v, media.formatPtr, true); // Set the new format hr = this.videoStreamConfig.SetFormat(media); if (hr != 0) { ErrorLogger.WriteLine("Error while setting new camera format (videoStreamConfig) in Camera.Capture. Message: " + DsError.GetErrorText(hr)); } DsUtils.FreeAMMediaType(media); media = null; }
protected virtual void Dispose(bool disposing) { if (_mediaType != null) { DsUtils.FreeAMMediaType(_mediaType); _mediaType = null; } }
private static void FreeMediaType(ref AMMediaType media_type) { if (media_type != null) { DsUtils.FreeAMMediaType(media_type); media_type = null; } }
public void Dispose() { if (_mediaType != null) { DsUtils.FreeAMMediaType(_mediaType); _mediaType = null; } }
// Set the Framerate, and video size private bool SetConfigParms(IPin pStill, int iWidth, int iHeight, short iBPP) { bool success = true; int hr; AMMediaType media; VideoInfoHeader v; IAMStreamConfig videoStreamConfig = pStill as IAMStreamConfig; // Get the existing format block hr = videoStreamConfig.GetFormat(out media); DsError.ThrowExceptionForHR(hr); try { // copy out the videoinfoheader v = new VideoInfoHeader(); Marshal.PtrToStructure(media.formatPtr, v); // if overriding the width, set the width if (iWidth > 0) { v.BmiHeader.Width = iWidth; } // if overriding the Height, set the Height if (iHeight > 0) { v.BmiHeader.Height = iHeight; } // if overriding the bits per pixel if (iBPP > 0) { v.BmiHeader.BitCount = iBPP; } // Copy the media structure back Marshal.StructureToPtr(v, media.formatPtr, false); // Set the new format try { hr = videoStreamConfig.SetFormat(media); //DsError.ThrowExceptionForHR( hr ); } catch { success = false; } } finally { DsUtils.FreeAMMediaType(media); media = null; } return(success); }
internal VideoCapabilities(IAMStreamConfig videoStreamConfig) { if (videoStreamConfig == null) { throw new ArgumentNullException("videoStreamConfig"); } AMMediaType mediaType = null; VideoStreamConfigCaps caps = null; IntPtr zero = IntPtr.Zero; try { IntPtr ptr2; int num; int num2; int numberOfCapabilities = videoStreamConfig.GetNumberOfCapabilities(out num, out num2); if (numberOfCapabilities != 0) { Marshal.ThrowExceptionForHR(numberOfCapabilities); } if (num <= 0) { throw new NotSupportedException("This video device does not report capabilities."); } if (num2 > Marshal.SizeOf(typeof(VideoStreamConfigCaps))) { throw new NotSupportedException("Unable to retrieve video device capabilities. This video device requires a larger VideoStreamConfigCaps structure."); } zero = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(VideoStreamConfigCaps))); numberOfCapabilities = videoStreamConfig.GetStreamCaps(0, out ptr2, zero); if (numberOfCapabilities != 0) { Marshal.ThrowExceptionForHR(numberOfCapabilities); } mediaType = (AMMediaType)Marshal.PtrToStructure(ptr2, typeof(AMMediaType)); caps = (VideoStreamConfigCaps)Marshal.PtrToStructure(zero, typeof(VideoStreamConfigCaps)); this.InputSize = caps.InputSize; this.MinFrameSize = caps.MinOutputSize; this.MaxFrameSize = caps.MaxOutputSize; this.FrameSizeGranularityX = caps.OutputGranularityX; this.FrameSizeGranularityY = caps.OutputGranularityY; this.MinFrameRate = 10000000.0 / ((double)caps.MaxFrameInterval); this.MaxFrameRate = 10000000.0 / ((double)caps.MinFrameInterval); } finally { if (zero != IntPtr.Zero) { Marshal.FreeCoTaskMem(zero); } zero = IntPtr.Zero; if (mediaType != null) { DsUtils.FreeAMMediaType(mediaType); } mediaType = null; } }
private void InitAudioGrabber(IBaseFilter sourceF) { audioGrabberFilter = new SampleGrabber() as IBaseFilter; if (audioGrabberFilter == null) { throw new COMException("Cannot create SampleGrabber"); } int hr = graph.AddFilter(audioGrabberFilter, "Audio Sample Grabber"); DsError.ThrowExceptionForHR(hr); audioGrabber = audioGrabberFilter as ISampleGrabber; if (audioGrabber == null) { throw new COMException("Cannot obtain ISampleGrabber"); } { AMMediaType mt = new AMMediaType(); mt.majorType = DirectShowLib.MediaType.Audio; mt.subType = DirectShowLib.MediaSubType.PCM; hr = audioGrabber.SetMediaType(mt); DsError.ThrowExceptionForHR(hr); DsUtils.FreeAMMediaType(mt); } hr = ConnectSampleGrabber(graph, sourceF, audioGrabberFilter); if (0 != hr) { // Cannot connect the audio grabber. Remove the filter from the graph. hr = graph.RemoveFilter(audioGrabberFilter); DsError.ThrowExceptionForHR(hr); Util.ReleaseComObject(ref audioGrabberFilter); audioGrabber = null; return; } audioNullFilter = new NullRenderer() as IBaseFilter; if (audioNullFilter == null) { throw new COMException("Cannot create NullRenderer"); } hr = graph.AddFilter(audioNullFilter, "Null Filter"); DsError.ThrowExceptionForHR(hr); hr = Util.ConnectFilters(graph, audioGrabberFilter, audioNullFilter); DsError.ThrowExceptionForHR(hr); audioGrabberCB = new SampleGrabberCB(); hr = audioGrabber.SetCallback(audioGrabberCB, (int)CBMethod.Sample); DsError.ThrowExceptionForHR(hr); }
private void LoadMedia() { /*Create the COM object and query the IMediaDet interface */ m_mediaDet = new MediaDet() as IMediaDet; if (m_mediaDet == null) { throw new NullReferenceException("Could not create an instance of MediaDet COM"); } int hr = m_mediaDet.put_Filename(m_filename); DsError.ThrowExceptionForHR(hr); /* We find out how many streams exist in the * media file. These can be audio, video, etc */ hr = m_mediaDet.get_OutputStreams(out m_streamCount); DsError.ThrowExceptionForHR(hr); /* Loop over each of the streams and extract info from them */ for (int i = 0; i < m_streamCount; i++) { /* Set the interface to look at a specific stream */ hr = m_mediaDet.put_CurrentStream(i); DsError.ThrowExceptionForHR(hr); Guid majorType; /* Get the major type of the media */ hr = m_mediaDet.get_StreamType(out majorType); DsError.ThrowExceptionForHR(hr); var mediaType = new AMMediaType(); double framerate; /* Gets the AMMediaType so we can read some * metadata on the stream */ hr = m_mediaDet.get_StreamMediaType(mediaType); DsError.ThrowExceptionForHR(hr); if (majorType == MediaType.Video) { ReadVideoFormat(mediaType); hr = m_mediaDet.get_FrameRate(out framerate); m_videoFrameRate = framerate; } else if (majorType == MediaType.Audio) { ReadAudioFormat(mediaType); } /* We need to free this with the helper * because it has an unmanaged pointer * and we don't want any leaks */ DsUtils.FreeAMMediaType(mediaType); } }
// Set the Framerate, and video size private void SetConfigParms(ICaptureGraphBuilder2 capGraph, IBaseFilter capFilter, int iSampleRate, int iChannels) { int hr; object o; AMMediaType media; // Find the stream config interface hr = capGraph.FindInterface( PinCategory.Capture, MediaType.Audio, capFilter, typeof(IAMStreamConfig).GUID, out o); IAMStreamConfig audioStreamConfig = o as IAMStreamConfig; if (audioStreamConfig == null) { throw new Exception("Failed to get IAMStreamConfig"); } // Get the existing format block hr = audioStreamConfig.GetFormat(out media); DsError.ThrowExceptionForHR(hr); // copy out the videoinfoheader WaveFormatEx i = new WaveFormatEx(); Marshal.PtrToStructure(media.formatPtr, i); i.wFormatTag = 0x0001; // WAVE_FORMAT_PCM i.wBitsPerSample = 16; i.nSamplesPerSec = 44100; i.nChannels = m_Channels; i.nBlockAlign = 2; i.nAvgBytesPerSec = (i.nSamplesPerSec * i.nBlockAlign); i.cbSize = 0; // if overriding the framerate, set the frame rate if (iSampleRate > 0) { i.nSamplesPerSec = iSampleRate; } // if overriding the width, set the width if (iChannels > 0) { i.nChannels = (short)iChannels; } // Copy the media structure back Marshal.StructureToPtr(i, media.formatPtr, false); // Set the new format hr = audioStreamConfig.SetFormat(media); DsError.ThrowExceptionForHR(hr); DsUtils.FreeAMMediaType(media); media = null; }