static IMFMetadata GetMetadata(IMFMediaSource mediaSource) { HResult hr; // Get IMFPresentationDescriptor. IMFPresentationDescriptor presentationDescriptor; hr = mediaSource.CreatePresentationDescriptor(out presentationDescriptor); MFError.ThrowExceptionForHR(hr); // Get IMFMetadataProvider. object provider; hr = MFExtern.MFGetService(mediaSource, MFServices.MF_METADATA_PROVIDER_SERVICE, typeof(IMFMetadataProvider).GUID, out provider); MFError.ThrowExceptionForHR(hr); IMFMetadataProvider metadataProvider = (IMFMetadataProvider)provider; // Get IMFMetadata. IMFMetadata metadata; hr = metadataProvider.GetMFMetadata(presentationDescriptor, 0, 0, out metadata); MFError.ThrowExceptionForHR(hr); COMBase.SafeRelease(presentationDescriptor); presentationDescriptor = null; COMBase.SafeRelease(metadataProvider); metadataProvider = null; return(metadata); }
/////////////////////////////////////////////////////////////////////// // Name: CreateMediaSource // Description: Create a media source from a URL. // // sURL: The URL to open. // ppSource: Receives a pointer to the media source. /////////////////////////////////////////////////////////////////////// static void CreateMediaSource(string sURL, out IMFMediaSource ppSource) { int hr; IMFSourceResolver pSourceResolver; object pSourceUnk; // Create the source resolver. hr = MFExtern.MFCreateSourceResolver(out pSourceResolver); MFError.ThrowExceptionForHR(hr); try { // Use the source resolver to create the media source. MFObjectType ObjectType = MFObjectType.Invalid; hr = pSourceResolver.CreateObjectFromURL( sURL, // URL of the source. MFResolution.MediaSource, // Create a source object. null, // Optional property store. out ObjectType, // Receives the object type. out pSourceUnk // Receives a pointer to the source. ); MFError.ThrowExceptionForHR(hr); // Get the IMFMediaSource interface from the media source. ppSource = (IMFMediaSource)pSourceUnk; } finally { // Clean up. if (pSourceResolver != null) { Marshal.ReleaseComObject(pSourceResolver); } } }
static int GetBitRate(IMFMediaSource mediaSource) { HResult hr; // Get IMFPresentationDescriptor. IMFPresentationDescriptor presentationDescriptor; hr = mediaSource.CreatePresentationDescriptor(out presentationDescriptor); MFError.ThrowExceptionForHR(hr); // Get IMFStreamDescriptor. bool isStreamSelected; IMFStreamDescriptor streamDescriptor; hr = presentationDescriptor.GetStreamDescriptorByIndex(0, out isStreamSelected, out streamDescriptor); MFError.ThrowExceptionForHR(hr); // Get bit rate. int bitRate; hr = streamDescriptor.GetUINT32(MFAttributesClsid.MF_SD_ASF_EXTSTRMPROP_AVG_DATA_BITRATE, out bitRate); MFError.ThrowExceptionForHR(hr); bitRate /= 1000; if (bitRate <= 0) { throw new ApplicationException($"Invalid bit rate: {bitRate}"); } return(bitRate); }
private void CreateVideoCaptureSource() { try { if (VideoCaptureDevice == null) { Trace.WriteLine("Error no videocapturedevice set"); return; } //VideoCaptureSource IMFAttributes pAttributes = null; int hr = MFExtern.MFCreateAttributes(out pAttributes, 2); hr = pAttributes.SetGUID(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, CLSID.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID); MFError.ThrowExceptionForHR(hr); hr = pAttributes.SetString(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, VideoCaptureDevice.DevicePath); MFError.ThrowExceptionForHR(hr); IMFMediaSource ppMediaSource = null; hr = MFExtern.MFCreateDeviceSource(pAttributes, out ppMediaSource); MFError.ThrowExceptionForHR(hr); m_pSource = ppMediaSource; //GetCaptureFormats(m_pSource); } catch (Exception e) { Marshal.GetHRForException(e); Trace.WriteLine("SetupGraph Exception " + e.ToString()); } }
private int OpenMediaSource(IMFMediaSource pSource, ref IMFSourceReaderAsync pReaderAsync) { // Create an attribute store to hold initialization settings. IMFAttributes pAttributes; var hr = MFExtern.MFCreateAttributes(out pAttributes, 2); //if (Succeeded(hr)) //{ // hr = pAttributes.SetUINT32(MFAttributesClsid.MF_READWRITE_DISABLE_CONVERTERS, 1); //} if (Succeeded(hr)) { hr = pAttributes.SetUnknown(MFAttributesClsid.MF_SOURCE_READER_ASYNC_CALLBACK, this); } if (Succeeded(hr)) { IMFSourceReader pReader; hr = MFExtern.MFCreateSourceReaderFromMediaSource(pSource, pAttributes, out pReader); // ReSharper disable once SuspiciousTypeConversion.Global pReaderAsync = (IMFSourceReaderAsync)pReader; } SafeRelease(pAttributes); return(hr); }
//////////////////////////////////////////////////////////////////////////////////////// // Name: CPlayer::CreateSourceNode (Private) // Description: // Creates the source node for a stream // Parameter: // pPresentationDescriptor: [in] Pointer to the presentation descriptor for the media source // pStreamDescriptor: [in] Stream descriptor for the stream // pMediaSource: [in] Pointer to the media source // ppSourceNode: [out] Receives a pointer to the new node /////////////////////////////////////////////////////////////////////////////////////////// private void CreateSourceNode( IMFPresentationDescriptor pPresentationDescriptor, IMFStreamDescriptor pStreamDescriptor, IMFMediaSource pMediaSource, out IMFTopologyNode ppSourceNode) { if (pPresentationDescriptor == null || pMediaSource == null || pStreamDescriptor == null) { throw new COMException("null pointer", (int)HResult.E_POINTER); } HResult hr; // Create the source-stream node. hr = MFExtern.MFCreateTopologyNode(MFTopologyType.SourcestreamNode, out ppSourceNode); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the media source. Necessary. hr = ppSourceNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_SOURCE, pMediaSource); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the presentation descriptor. Necessary. hr = ppSourceNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_PRESENTATION_DESCRIPTOR, pPresentationDescriptor); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the stream descriptor. Necessary. hr = ppSourceNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_STREAM_DESCRIPTOR, pStreamDescriptor); MFError.ThrowExceptionForHR(hr); }
//////////////////////////////////////////////////////////////////////////////////////// // Name: CPlayer::CreateTopology (Private) // Description: // Creates a topology for the media source // Parameter: // pMediaSource: [in] Pointer to the media source // pTopology: [in] Receives the partial topology ///////////////////////////////////////////////////////////////////////////////////////// private void CreateTopology( IMFMediaSource pMediaSource, IMFTopology pTopology) { Debug.WriteLine("CPlayer::CreateTopology"); //The caller needs to pass a valid media source //We need the media source because to set the source node attribute, media source is needed if (pMediaSource == null || pTopology == null) { throw new COMException("null pointer", (int)HResult.E_POINTER); } IMFPresentationDescriptor pPresentationDescriptor; //Create Presentation Descriptor for the media source HResult hr = pMediaSource.CreatePresentationDescriptor(out pPresentationDescriptor); MFError.ThrowExceptionForHR(hr); try { CreateNodesForStream(pPresentationDescriptor, pMediaSource, pTopology); } finally { SafeRelease(pPresentationDescriptor); } }
protected void CreateMediaSource(string sURL) { IMFSourceResolver pSourceResolver; object pSource; // Create the source resolver. HResult hr = MFExtern.MFCreateSourceResolver(out pSourceResolver); MFError.ThrowExceptionForHR(hr); try { // Use the source resolver to create the media source. MFObjectType ObjectType = MFObjectType.Invalid; hr = pSourceResolver.CreateObjectFromURL( sURL, // URL of the source. MFResolution.MediaSource, // Create a source object. null, // Optional property store. out ObjectType, // Receives the created object type. out pSource // Receives a pointer to the media source. ); MFError.ThrowExceptionForHR(hr); // Get the IMFMediaSource interface from the media source. m_pSource = (IMFMediaSource)pSource; } finally { // Clean up Marshal.ReleaseComObject(pSourceResolver); } }
private HResult OpenMediaSource(IMFMediaSource pSource) { HResult hr = HResult.S_OK; IMFAttributes pAttributes = null; hr = MFExtern.MFCreateAttributes(out pAttributes, 2); if (Succeeded(hr)) { hr = pAttributes.SetUnknown(MFAttributesClsid.MF_SOURCE_READER_ASYNC_CALLBACK, this); } if (Succeeded(hr)) { IMFSourceReader pReader; hr = MFExtern.MFCreateSourceReaderFromMediaSource( pSource, pAttributes, out pReader ); m_pReader = (IMFSourceReaderAsync)pReader; } SafeRelease(pAttributes); return(hr); }
private static HResult CreateSourceStreamNode(IMFMediaSource source, IMFStreamDescriptor streamDescriptor, IMFPresentationDescriptor presentationDescriptor, out IMFTopologyNode node) { HResult hr = S_OK; node = null; hr = MF.CreateTopologyNode(MFTopologyType.SourcestreamNode, out node); if (Failed(hr)) { return(hr); } hr = node.SetUnknown(MF_TOPONODE_SOURCE, source); if (Failed(hr)) { SafeRelease(node); return(hr); } hr = node.SetUnknown(MF_TOPONODE_PRESENTATION_DESCRIPTOR, presentationDescriptor); if (Failed(hr)) { SafeRelease(node); return(hr); } hr = node.SetUnknown(MF_TOPONODE_STREAM_DESCRIPTOR, streamDescriptor); if (Failed(hr)) { SafeRelease(node); return(hr); } return(hr); }
////////////////////////////////////////////////////////////////////// // Name: CreateSourceNode // Creates a source node for a media stream. // // pSource: Pointer to the media source. // pSourcePD: Pointer to the source's presentation descriptor. // pSourceSD: Pointer to the stream descriptor. // ppNode: Receives the IMFTopologyNode pointer. /////////////////////////////////////////////////////////////////////// static void CreateSourceNode( IMFMediaSource pSource, // Media source. IMFPresentationDescriptor pPD, // Presentation descriptor. IMFStreamDescriptor pSD, // Stream descriptor. out IMFTopologyNode ppNode // Receives the node pointer. ) { HResult hr; // Create the node. hr = MFExtern.MFCreateTopologyNode( MFTopologyType.SourcestreamNode, out ppNode); MFError.ThrowExceptionForHR(hr); // Set the attributes. hr = ppNode.SetUnknown( MFAttributesClsid.MF_TOPONODE_SOURCE, pSource); MFError.ThrowExceptionForHR(hr); hr = ppNode.SetUnknown( MFAttributesClsid.MF_TOPONODE_PRESENTATION_DESCRIPTOR, pPD); MFError.ThrowExceptionForHR(hr); hr = ppNode.SetUnknown( MFAttributesClsid.MF_TOPONODE_STREAM_DESCRIPTOR, pSD); MFError.ThrowExceptionForHR(hr); }
/////////////////////////////////////////////////////////////////////// // Name: CreateSourceStreamNode // Description: Creates a source-stream node for a stream. // // pSource: Media source. // pSourcePD: Presentation descriptor for the media source. // pSourceSD: Stream descriptor for the stream. // ppNode: Receives a pointer to the new node. // // Pre-conditions: Create the media source. ///////////////////////////////////////////////////////////////////////// void CreateSourceStreamNode( IMFMediaSource pSource, IMFPresentationDescriptor pSourcePD, IMFStreamDescriptor pSourceSD, out IMFTopologyNode ppNode ) { HResult hr; // Create the source-stream node. hr = MFExtern.MFCreateTopologyNode(MFTopologyType.SourcestreamNode, out ppNode); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the media source. hr = ppNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_SOURCE, pSource); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the presentation descriptor. hr = ppNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_PRESENTATION_DESCRIPTOR, pSourcePD); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the stream descriptor. hr = ppNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_STREAM_DESCRIPTOR, pSourceSD); MFError.ThrowExceptionForHR(hr); }
//------------------------------------------------------------------- // SetDevice // // Set up preview for a specified video capture device. //------------------------------------------------------------------- public int SetDevice(MfDevice pDevice, ref string format) { int hr; IMFMediaSource pSource = null; lock (LockSync) { try { // Release the current device, if any. hr = CloseDevice(); pActivate = pDevice.Activator; object o = null; if (Succeeded(hr)) { // Create the media source for the device. hr = pActivate.ActivateObject(typeof(IMFMediaSource).GUID, out o); } if (Succeeded(hr)) { pSource = (IMFMediaSource)o; } // Get Symbolic device link PwszSymbolicLink = pDevice.SymbolicName; // Create the source reader. if (Succeeded(hr)) { hr = OpenMediaSource(pSource, ref PReader); } if (Succeeded(hr)) { var index = GetOptimizedFormatIndex(ref format); if (index >= 0) { hr = ConfigureSourceReader(index); } } if (Failed(hr)) { pSource?.Shutdown(); //pActivate.ShutdownObject(); // NOTE: The source reader shuts down the media source // by default, but we might not have gotten that far. CloseDevice(); } } finally { SafeRelease(pSource); } } return(hr); }
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// A centralized place to close down all media devices. /// </summary> /// <history> /// 01 Nov 18 Cynic - Started /// </history> private void CloseAllMediaDevices() { HResult hr; LogMessage("CloseAllMediaDevices"); // close and release our call back handler if (mediaSessionAsyncCallbackHandler != null) { // stop any messaging or events in the call back handler mediaSessionAsyncCallbackHandler.ShutDown(); mediaSessionAsyncCallbackHandler = null; } // close the session (this is NOT the same as shutting it down) if (mediaSession != null) { hr = mediaSession.Close(); if (hr != HResult.S_OK) { // just log it LogMessage("CloseAllMediaDevices call to mediaSession.Close failed. Err=" + hr.ToString()); } } // Shut down the media source if (mediaSource != null) { hr = mediaSource.Shutdown(); if (hr != HResult.S_OK) { // just log it LogMessage("CloseAllMediaDevices call to mediaSource.Shutdown failed. Err=" + hr.ToString()); } Marshal.ReleaseComObject(mediaSource); mediaSource = null; } // Shut down the media session (note we only closed it before). if (mediaSession != null) { hr = mediaSession.Shutdown(); if (hr != HResult.S_OK) { // just log it LogMessage("CloseAllMediaDevices call to mediaSession.Shutdown failed. Err=" + hr.ToString()); } Marshal.ReleaseComObject(mediaSession); mediaSession = null; } // close the media sink if (mediaSink != null) { Marshal.ReleaseComObject(mediaSink); mediaSink = null; } }
private void CloseAllMediaDevices() { HResult hr; Console.WriteLine("CloseAllMediaDevices"); // close and release our call back handler if (mediaSessionAsyncCallbackHandler != null) { // stop any messaging or events in the call back handler mediaSessionAsyncCallbackHandler.ShutDown(); mediaSessionAsyncCallbackHandler = null; } // Shut down the source reader if (StreamReader != null) { Marshal.ReleaseComObject(StreamReader); StreamReader = null; } // close the session (this is NOT the same as shutting it down) if (mediaSession != null) { hr = mediaSession.Close(); if (hr != HResult.S_OK) { // just log it Console.WriteLine("CloseAllMediaDevices call to mediaSession.Close failed. Err=" + hr.ToString()); } } // Shut down the media source if (MediaSource != null) { hr = MediaSource.Shutdown(); if (hr != HResult.S_OK) { // just log it Console.WriteLine("CloseAllMediaDevices call to mediaSource.Shutdown failed. Err=" + hr.ToString()); } Marshal.ReleaseComObject(MediaSource); MediaSource = null; } // Shut down the media session (note we only closed it before). if (mediaSession != null) { hr = mediaSession.Shutdown(); if (hr != HResult.S_OK) { // just log it Console.WriteLine("CloseAllMediaDevices call to mediaSession.Shutdown failed. Err=" + hr.ToString()); } Marshal.ReleaseComObject(mediaSession); mediaSession = null; } if (currentVideoMediaType != null) { Marshal.ReleaseComObject(currentVideoMediaType); currentVideoMediaType = null; } }
public SimpleFastEncode() { // Create objects and bind the background worker events this.EncodeProgress = null; this.presentationClock = null; this.mediaSession = null; this.mediaSource = null; this.progressTimer = new Timer(500); this.progressTimer.Elapsed += this.ProgressTimer_Tick; }
/////////////////////////////////////////////////////////////////////// // Name: AddBranchToPartialTopology // Description: Adds a topology branch for one stream. // // pTopology: Pointer to the topology object. // hVideoWindow: Handle to the video window (for video streams). // pSource: Media source. // pSourcePD: The source's presentation descriptor. // iStream: Index of the stream to render. // // Pre-conditions: The topology must be created already. // // Notes: For each stream, we must do the following: // 1. Create a source node associated with the stream. // 2. Create an output node for the renderer. // 3. Connect the two nodes. // // Optionally we can also add an effect transform between the source // and output nodes. // // The media session will resolve the topology, so we do not have // to worry about decoders or color converters. ///////////////////////////////////////////////////////////////////////// void AddBranchToPartialTopology( IMFTopology pTopology, IntPtr hVideoWindow, IMFMediaSource pSource, IMFPresentationDescriptor pSourcePD, int iStream) { TRACE("Player::RenderStream"); IMFStreamDescriptor pSourceSD = null; IMFTopologyNode pSourceNode = null; MFError throwonhr; Guid majorType; bool fSelected = false; // Get the stream descriptor for this stream. throwonhr = pSourcePD.GetStreamDescriptorByIndex(iStream, out fSelected, out pSourceSD); // First check if the stream is selected by default. If not, ignore it. // More sophisticated applications can change the default selections. if (fSelected) { try { // Create a source node for this stream. CreateSourceStreamNode(pSource, pSourcePD, pSourceSD, out pSourceNode); // Add the source node to the topology. throwonhr = pTopology.AddNode(pSourceNode); // Get the major media type for the stream. GetStreamType(pSourceSD, out majorType); if (majorType == MFMediaType.Video) { // For video, use the grayscale transform. CreateVideoBranch(pTopology, pSourceNode, hVideoWindow, m_VideoEffect); } else if (majorType == MFMediaType.Audio) { if (!m_Audio) { CreateAudioBranch(pTopology, pSourceNode, m_AudioEffect); } } } finally { // Clean up. SafeRelease(pSourceSD); SafeRelease(pSourceNode); } } }
/// <summary> /// Starts, seeks, or restarts the media source by specifying where to start playback. /// </summary> /// <param name="mediaSource">A valid IMFMediaSource instance.</param> /// <param name="presentationDescriptor">An IMFPresentationDescriptor instance of the media source's presentation descriptor.</param> /// <returns>If this function succeeds, it returns the S_OK member. Otherwise, it returns another HResult's member that describe the error.</returns> /// <remarks>This method starts the media source at its beginning.</remarks> public static HResult Start(this IMFMediaSource mediaSource, IMFPresentationDescriptor presentationDescriptor) { if (mediaSource == null) { throw new ArgumentNullException("mediaSource"); } using (PropVariant start = new PropVariant(0)) { return(mediaSource.Start(presentationDescriptor, Guid.Empty, start)); } }
public static void MFCreateTranscodeTopology( IMFMediaSource mediaSource, string outputFilePath, IMFTranscodeProfile transcodeProfile, out IMFTopology transcodeTopology) { int result = ExternMFCreateTranscodeTopology(mediaSource, outputFilePath, transcodeProfile, out transcodeTopology); if (result < 0) { throw new COMException("Exception from HRESULT: 0x" + result.ToString("X", System.Globalization.NumberFormatInfo.InvariantInfo) + " (MFCreateTranscodeTopology failed)", result); } }
private static HResult ProcessMediaSession(string VideoFile) { HResult hr = S_OK; IMFMediaSource source = null; IMFTopology topology = null; IMFMediaSinkAlt mediaSink = null; IMFMediaSession mediaSession = null; hr = CreateMediaSource(VideoFile, out source); if (Failed(hr)) { return(hr); } hr = CreateTopology(out topology, out mediaSink, source); if (Failed(hr)) { topology = null; mediaSink = null; source = null; return(hr); } hr = MF.CreateMediaSession(null, out mediaSession); if (Failed(hr)) { topology = null; mediaSink = null; source = null; mediaSession = null; return(hr); } hr = mediaSession.SetTopology(MFSessionSetTopologyFlags.None, topology); if (Failed(hr)) { topology = null; mediaSink = null; source = null; mediaSession = null; return(hr); } hr = RunMediaSession(mediaSession); return(hr); }
/// <summary> /// Resets the local graph resources to their /// default settings /// </summary> private void ResetGraphResources() { //m_graph = null; if (m_pSource != null) { Marshal.ReleaseComObject(m_pSource); } m_pSource = null; if (m_pSession != null) { Marshal.ReleaseComObject(m_pSession); } m_pSession = null; }
public MFMediaUriPlayer(IntPtr hVideo, IntPtr hEvent) { Debug.Assert(hVideo != IntPtr.Zero); Debug.Assert(hEvent != IntPtr.Zero); m_pSession = null; m_pSource = null; m_pVideoDisplay = null; m_hwndVideo = hVideo; m_hwndEvent = hEvent; m_state = PlayerState.Ready; m_hCloseEvent = new AutoResetEvent(false); MFExtern.MFStartup(0x10070, MFStartup.Full); }
/// <summary> /// Init /// </summary> /// <param name="hVideo"></param> /// <param name="hEvent"></param> public Media(IntPtr hVideo, IntPtr hEvent) { TRACE("Media::Media"); Debug.Assert(hVideo != IntPtr.Zero); Debug.Assert(hEvent != IntPtr.Zero); m_pSession = null; m_pSource = null; m_pVideoDisplay = null; m_hwndVideo = hVideo; m_hwndEvent = hEvent; m_state = MediaState.Ready; m_hCloseEvent = new AutoResetEvent(false); MFExtern.MFStartup(MF_VERSION, MFStartup.Full); }
private HResult GetMediaSource(string webcamId, out IMFMediaSource source) { MFExtern.MFCreateAttributes(out IMFAttributes attributes, 2); attributes.SetGUID(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID); attributes.SetString(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, webcamId); HResult result = MFExtern.MFCreateDeviceSource(attributes, out source); if ((uint)result == 0xC00D36E6) { result = HResult.ERROR_DEVICE_NOT_CONNECTED; } Marshal.ReleaseComObject(attributes); return(result); }
/////////////////////////////////////////////////////////////////////// // Name: CreateWavFile // Description: Creates a .wav file from an input file. /////////////////////////////////////////////////////////////////////// static void CreateWavFile(string sURL, string sOutputFile) { IMFByteStream pStream = null; IMFMediaSinkAlt pSink = null; IMFMediaSource pSource = null; IMFTopology pTopology = null; WavSinkNS.CWavSink pObj = null; HResult hr = MFExtern.MFCreateFile(MFFileAccessMode.Write, MFFileOpenMode.DeleteIfExist, MFFileFlags.None, sOutputFile, out pStream); MFError.ThrowExceptionForHR(hr); try { pObj = new WavSinkNS.CWavSink(pStream); pSink = pObj as IMFMediaSinkAlt; // Create the media source from the URL. CreateMediaSource(sURL, out pSource); // Create the topology. CreateTopology(pSource, pSink, out pTopology); // Run the media session. RunMediaSession(pTopology); hr = pSource.Shutdown(); MFError.ThrowExceptionForHR(hr); } finally { if (pStream != null) { Marshal.ReleaseComObject(pStream); } if (pSource != null) { Marshal.ReleaseComObject(pSource); } if (pTopology != null) { Marshal.ReleaseComObject(pTopology); } //pObj.Dispose(); } }
public MediaFoundationCapturer(IMFActivate devsource) { MediaFoundationApi.Startup(); WaveFormat format = new WaveFormat(); try { devsource.ActivateObject(typeof(IMFMediaSource).GUID, out object _source); source = _source as IMFMediaSource; } catch (COMException) { throw new ArgumentException("Can't create media source with the devsource."); } SetFormat(format); recordthread = new Thread(DoRecord); }
/////////////////////////////////////////////////////////////////////// // Name: CreateTopologyBranch // Description: Adds a source and sink to the topology and // connects them. // // pTopology: The topology. // pSource: The media source. // pPD: The source's presentation descriptor. // pSD: The stream descriptor for the stream. // pSink: The media sink. // /////////////////////////////////////////////////////////////////////// static void CreateTopologyBranch( IMFTopology pTopology, IMFMediaSource pSource, // Media source. IMFPresentationDescriptor pPD, // Presentation descriptor. IMFStreamDescriptor pSD, // Stream descriptor. IMFMediaSinkAlt pSink ) { HResult hr; IMFTopologyNode pSourceNode = null; IMFTopologyNode pOutputNode = null; CreateSourceNode(pSource, pPD, pSD, out pSourceNode); try { CreateOutputNode(pSink, 0, out pOutputNode); try { hr = pTopology.AddNode(pSourceNode); MFError.ThrowExceptionForHR(hr); hr = pTopology.AddNode(pOutputNode); MFError.ThrowExceptionForHR(hr); hr = pSourceNode.ConnectOutput(0, pOutputNode, 0); MFError.ThrowExceptionForHR(hr); } finally { if (pOutputNode != null) { Marshal.ReleaseComObject(pOutputNode); } } } finally { if (pSourceNode != null) { Marshal.ReleaseComObject(pSourceNode); } } }
public HResult GetMediaSource(out IMFMediaSource ppMediaSource) { HResult hr = HResult.S_OK; ppMediaSource = null; lock (_spSource) { hr = CheckShutdown(); if (MFError.Succeeded(hr)) { ppMediaSource = _spSource; } return(hr); } }
static void ConvertFile(string sourceFileName, string destFileName) { Console.WriteLine($"------------------------------------------------------------"); Console.WriteLine($" Source: {sourceFileName}"); IMFMediaSource mediaSource = GetMediaSource(sourceFileName); int bitRate = GetBitRate(mediaSource); IMFMetadata metadata = GetMetadata(mediaSource); ID3TagData tagData = new ID3TagData() { Title = GetStringProperty(metadata, "Title"), Artist = GetStringProperty(metadata, "Author"), Album = GetStringProperty(metadata, "WM/AlbumTitle"), Year = GetStringProperty(metadata, "WM/Year"), Genre = GetStringProperty(metadata, "WM/Genre"), Track = GetUIntProperty(metadata, "WM/TrackNumber"), AlbumArtist = GetStringProperty(metadata, "WM/AlbumArtist") }; COMBase.SafeRelease(metadata); metadata = null; COMBase.SafeRelease(mediaSource); mediaSource = null; Console.WriteLine($" Title: {tagData.Title}"); Console.WriteLine($"Album artist: {tagData.AlbumArtist}"); Console.WriteLine($" Artist: {tagData.Artist}"); Console.WriteLine($" Album: {tagData.Album}"); Console.WriteLine($" Year: {tagData.Year}"); Console.WriteLine($" Genre: {tagData.Genre}"); Console.WriteLine($" Track: {tagData.Track}"); Console.WriteLine($" Bit rate: {bitRate}"); using (AudioFileReader reader = new AudioFileReader(sourceFileName)) { using (LameMP3FileWriter writer = new LameMP3FileWriter(destFileName, reader.WaveFormat, bitRate, tagData)) { reader.CopyTo(writer); } } Console.WriteLine($" Destination: {destFileName}"); }
protected void CloseSession() { int hr; /*if (m_pVideoDisplay != null) * { * Marshal.ReleaseComObject(m_pVideoDisplay); * m_pVideoDisplay = null; * } */ if (m_pSession != null) { hr = m_pSession.Close(); MFError.ThrowExceptionForHR(hr); // Wait for the close operation to complete /*bool res = m_hCloseEvent.WaitOne(5000, true); * if (!res) * { * TRACE(("WaitForSingleObject timed out!")); * }*/ } // Complete shutdown operations // 1. Shut down the media source if (m_pSource != null) { hr = m_pSource.Shutdown(); MFError.ThrowExceptionForHR(hr); COMBase.SafeRelease(m_pSource); m_pSource = null; } // 2. Shut down the media session. (Synchronous operation, no events.) if (m_pSession != null) { hr = m_pSession.Shutdown(); Marshal.ReleaseComObject(m_pSession); m_pSession = null; } }
public CPlayer(IntPtr hVideo, IntPtr hEvent) { TRACE(("CPlayer::CPlayer")); Debug.Assert(hVideo != IntPtr.Zero); Debug.Assert(hEvent != IntPtr.Zero); m_pSession = null; m_pSource = null; m_pVideoDisplay = null; m_hwndVideo = hVideo; m_hwndEvent = hEvent; m_state = PlayerState.Ready; m_hCloseEvent = new AutoResetEvent(false); int hr = MFExtern.MFStartup(0x10070, MFStartup.Full); MFError.ThrowExceptionForHR(hr); }
static IMFMediaSource GetMediaSource(string sourceFileName) { HResult hr; // Get an IMFMediaSource. IMFSourceResolver sourceResolver; hr = MFExtern.MFCreateSourceResolver(out sourceResolver); MFError.ThrowExceptionForHR(hr); MFObjectType objectType = MFObjectType.Invalid; object source; hr = sourceResolver.CreateObjectFromURL(sourceFileName, MFResolution.MediaSource, null, out objectType, out source); MFError.ThrowExceptionForHR(hr); IMFMediaSource mediaSource = (IMFMediaSource)source; COMBase.SafeRelease(sourceResolver); sourceResolver = null; return(mediaSource); }
private IMFSourceReader CreateSourceReader(IMFMediaSource mfs) { IMFSourceReader reader = null; try { MFExtern.MFCreateAttributes(out sourcevideoreaderattribute, 2); sourcevideoreaderattribute.SetGUID(MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, CLSID.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID); sourcevideoreaderattribute.SetUINT32(MFAttributesClsid.MF_SOURCE_READER_ENABLE_VIDEO_PROCESSING, 1); MFExtern.MFCreateSourceReaderFromMediaSource(mfs, sourcevideoreaderattribute, out reader); //reader.GetCurrentMediaType((int)__MIDL___MIDL_itf_mfreadwrite_0000_0001_0001.MF_SOURCE_READER_FIRST_VIDEO_STREAM, out sourcemeidatype); } catch (Exception e) { Debug.WriteLine("ERROR CreateSourceReader", e.StackTrace); } return(reader); }
public static void MFCreateSourceReaderFromMediaSource( IMFMediaSource mediaSource, IMFAttributes attributes, out IMFSourceReader sourceReader) { int result = ExternMFCreateSourceReaderFromMediaSource(mediaSource, attributes, out sourceReader); if (result < 0) { throw new COMException("Exception from HRESULT: 0x" + result.ToString("X", System.Globalization.NumberFormatInfo.InvariantInfo) + " (MFCreateTranscodeTopology failed)", result); } }
/////////////////////////////////////////////////////////////////////// // Name: AddBranchToPartialTopology // Description: Adds a topology branch for one stream. // // pTopology: Pointer to the topology object. // hVideoWindow: Handle to the video window (for video streams). // pSource: Media source. // pSourcePD: The source's presentation descriptor. // iStream: Index of the stream to render. // // Pre-conditions: The topology must be created already. // // Notes: For each stream, we must do the following: // 1. Create a source node associated with the stream. // 2. Create an output node for the renderer. // 3. Connect the two nodes. // // Optionally we can also add an effect transform between the source // and output nodes. // // The media session will resolve the topology, so we do not have // to worry about decoders or color converters. ///////////////////////////////////////////////////////////////////////// void AddBranchToPartialTopology( IMFTopology pTopology, IntPtr hVideoWindow, IMFMediaSource pSource, IMFPresentationDescriptor pSourcePD, int iStream) { TRACE("Player::RenderStream"); IMFStreamDescriptor pSourceSD = null; IMFTopologyNode pSourceNode = null; int hr; Guid majorType; bool fSelected = false; // Get the stream descriptor for this stream. hr = pSourcePD.GetStreamDescriptorByIndex(iStream, out fSelected, out pSourceSD); MFError.ThrowExceptionForHR(hr); // First check if the stream is selected by default. If not, ignore it. // More sophisticated applications can change the default selections. if (fSelected) { try { // Create a source node for this stream. CreateSourceStreamNode(pSource, pSourcePD, pSourceSD, out pSourceNode); // Add the source node to the topology. hr = pTopology.AddNode(pSourceNode); MFError.ThrowExceptionForHR(hr); // Get the major media type for the stream. GetStreamType(pSourceSD, out majorType); if (majorType == MFMediaType.Video) { // For video, use the grayscale transform. CreateVideoBranch(pTopology, pSourceNode, hVideoWindow, m_VideoEffect); } else if (majorType == MFMediaType.Audio) { CreateAudioBranch(pTopology, pSourceNode, Guid.Empty); } } finally { // Clean up. SafeRelease(pSourceSD); SafeRelease(pSourceNode); } } }
/////////////////////////////////////////////////////////////////////// // Name: CreateSourceStreamNode // Description: Creates a source-stream node for a stream. // // pSource: Media source. // pSourcePD: Presentation descriptor for the media source. // pSourceSD: Stream descriptor for the stream. // ppNode: Receives a pointer to the new node. // // Pre-conditions: Create the media source. ///////////////////////////////////////////////////////////////////////// void CreateSourceStreamNode( IMFMediaSource pSource, IMFPresentationDescriptor pSourcePD, IMFStreamDescriptor pSourceSD, out IMFTopologyNode ppNode ) { int hr; // Create the source-stream node. hr = MFExtern.MFCreateTopologyNode(MFTopologyType.SourcestreamNode, out ppNode); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the media source. hr = ppNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_SOURCE, pSource); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the presentation descriptor. hr = ppNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_PRESENTATION_DESCRIPTOR, pSourcePD); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the stream descriptor. hr = ppNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_STREAM_DESCRIPTOR, pSourceSD); MFError.ThrowExceptionForHR(hr); }
protected void CreateMediaSource(string sURL) { TRACE("CPlayer::CreateMediaSource"); IMFSourceResolver pSourceResolver; object pSource; // Create the source resolver. MFExtern.MFCreateSourceResolver(out pSourceResolver); try { // Use the source resolver to create the media source. MFObjectType ObjectType = MFObjectType.Invalid; pSourceResolver.CreateObjectFromURL( sURL, // URL of the source. MFResolution.MediaSource, // Create a source object. null, // Optional property store. out ObjectType, // Receives the created object type. out pSource // Receives a pointer to the media source. ); // Get the IMFMediaSource interface from the media source. m_pSource = (IMFMediaSource)pSource; } finally { // Clean up Marshal.ReleaseComObject(pSourceResolver); } }
/////////////////////////////////////////////////////////////////////// // Name: CreateTopology // Description: Creates the topology. // // Note: The first audio stream is conntected to the media sink. // Other streams are deselected. /////////////////////////////////////////////////////////////////////// static void CreateTopology(IMFMediaSource pSource, IMFMediaSinkAlt pSink, out IMFTopology ppTopology) { int hr; IMFPresentationDescriptor pPD = null; IMFStreamDescriptor pSD = null; int cStreams = 0; bool fConnected = false; hr = MFExtern.MFCreateTopology(out ppTopology); MFError.ThrowExceptionForHR(hr); hr = pSource.CreatePresentationDescriptor(out pPD); MFError.ThrowExceptionForHR(hr); try { hr = pPD.GetStreamDescriptorCount(out cStreams); MFError.ThrowExceptionForHR(hr); Guid majorType; bool fSelected = false; for (int iStream = 0; iStream < cStreams; iStream++) { hr = pPD.GetStreamDescriptorByIndex(iStream, out fSelected, out pSD); MFError.ThrowExceptionForHR(hr); try { // If the stream is not selected by default, ignore it. if (!fSelected) { continue; } // Get the major media type. GetStreamMajorType(pSD, out majorType); // If it's not audio, deselect it and continue. if (majorType != MFMediaType.Audio) { // Deselect this stream hr = pPD.DeselectStream(iStream); MFError.ThrowExceptionForHR(hr); continue; } // It's an audio stream, so try to create the topology branch. CreateTopologyBranch(ppTopology, pSource, pPD, pSD, pSink); } finally { if (pSD != null) { Marshal.ReleaseComObject(pSD); } } // Set our status flag. fConnected = true; // At this point we have reached the first audio stream in the // source, so we can stop looking (whether we succeeded or failed). break; } } finally { if (pPD != null) { Marshal.ReleaseComObject(pPD); } } // Even if we succeeded, if we didn't connect any streams, it's a failure. // (For example, it might be a video-only source. if (!fConnected) { throw new Exception("No audio streams"); } }
//////////////////////////////////////////////////////////////////////////////////////// // Name: CPlayer::CreateMediaSource (Private) // Description: // Creates a media source from URL // Parameter: // sURL: [in] File URL // ppMediaSource: [out] Receives the media source ///////////////////////////////////////////////////////////////////////////////////////// private void CreateMediaSource( string sURL, out IMFMediaSource ppMediaSource) { Debug.WriteLine("CPlayer::CreateMediaSource"); if (sURL == null) { throw new COMException("null pointer", E_Pointer); } int hr; IMFSourceResolver pSourceResolver; object pSourceUnk; hr = MFExtern.MFCreateSourceResolver(out pSourceResolver); MFError.ThrowExceptionForHR(hr); try { // Use the source resolver to create the media source. MFObjectType ObjectType = MFObjectType.Invalid; hr = pSourceResolver.CreateObjectFromURL( sURL, // URL of the source. MFResolution.MediaSource, // Create a source object. null, // Optional property store. out ObjectType, // Receives the created object type. out pSourceUnk // Receives a pointer to the media source. ); MFError.ThrowExceptionForHR(hr); // Get the IMFMediaSource interface from the media source. ppMediaSource = (IMFMediaSource)pSourceUnk; } finally { SafeRelease(pSourceResolver); } }
//////////////////////////////////////////////////////////////////////////////////////// // Name: CPlayer::AddTopologyToSequencer (Private) // Description: // Adds the topology to the sequencer // Parameter: // sURL: [in] File URL // pMediaSource: [in] Pointer to the media source // pTopology: [in] Pointer to the topology // pSegmentId: [out] Receives the segment id returned by the sequencer source ///////////////////////////////////////////////////////////////////////////////////////// private void AddTopologyToSequencer( string sURL, IMFMediaSource pMediaSource, IMFTopology pTopology, out int pSegmentId) { Debug.WriteLine("CPlayer::AddTopologyToSequencer"); if (sURL == null || pMediaSource == null || pTopology == null) { throw new COMException("null pointer", E_Pointer); } long hnsSegmentDuration = 0; long TopologyID = 0; int hr; IMFPresentationDescriptor pPresentationDescriptor; hr = m_pSequencerSource.AppendTopology(pTopology, 0, out pSegmentId); MFError.ThrowExceptionForHR(hr); hr = pTopology.GetTopologyID(out TopologyID); MFError.ThrowExceptionForHR(hr); //create a presentation descriptor hr = pMediaSource.CreatePresentationDescriptor(out pPresentationDescriptor); MFError.ThrowExceptionForHR(hr); //get the segment duration hr = pPresentationDescriptor.GetUINT64(MFAttributesClsid.MF_PD_DURATION, out hnsSegmentDuration); MFError.ThrowExceptionForHR(hr); Debug.Assert(hnsSegmentDuration > 0); //store the segment info: SegmentId, SegmentDuration, TopoID in the linked list. m_Segments.AddNewSegmentEntry(pSegmentId, hnsSegmentDuration, TopologyID, sURL); }
//////////////////////////////////////////////////////////////////////////////////////// // Name: CPlayer::CreateTopology (Private) // Description: // Creates a topology for the media source // Parameter: // pMediaSource: [in] Pointer to the media source // pTopology: [in] Receives the partial topology ///////////////////////////////////////////////////////////////////////////////////////// private void CreateTopology( IMFMediaSource pMediaSource, IMFTopology pTopology) { Debug.WriteLine("CPlayer::CreateTopology"); //The caller needs to pass a valid media source //We need the media source because to set the source node attribute, media source is needed if (pMediaSource == null || pTopology == null) { throw new COMException("null pointer", E_Pointer); } IMFPresentationDescriptor pPresentationDescriptor; //Create Presentation Descriptor for the media source int hr = pMediaSource.CreatePresentationDescriptor(out pPresentationDescriptor); MFError.ThrowExceptionForHR(hr); try { CreateNodesForStream(pPresentationDescriptor, pMediaSource, pTopology); } finally { SafeRelease(pPresentationDescriptor); } }
//////////////////////////////////////////////////////////////////////////////////////// // Name: CPlayer::CreateSourceNode (Private) // Description: // Creates the source node for a stream // Parameter: // pPresentationDescriptor: [in] Pointer to the presentation descriptor for the media source // pStreamDescriptor: [in] Stream descriptor for the stream // pMediaSource: [in] Pointer to the media source // ppSourceNode: [out] Receives a pointer to the new node /////////////////////////////////////////////////////////////////////////////////////////// private void CreateSourceNode( IMFPresentationDescriptor pPresentationDescriptor, IMFStreamDescriptor pStreamDescriptor, IMFMediaSource pMediaSource, out IMFTopologyNode ppSourceNode) { if (pPresentationDescriptor == null || pMediaSource == null || pStreamDescriptor == null) { throw new COMException("null pointer", E_Pointer); } int hr; // Create the source-stream node. hr = MFExtern.MFCreateTopologyNode(MFTopologyType.SourcestreamNode, out ppSourceNode); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the media source. Necessary. hr = ppSourceNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_SOURCE, pMediaSource); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the presentation descriptor. Necessary. hr = ppSourceNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_PRESENTATION_DESCRIPTOR, pPresentationDescriptor); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the stream descriptor. Necessary. hr = ppSourceNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_STREAM_DESCRIPTOR, pStreamDescriptor); MFError.ThrowExceptionForHR(hr); }
protected void CloseSession() { if (m_pVideoDisplay != null) { Marshal.ReleaseComObject(m_pVideoDisplay); m_pVideoDisplay = null; } if (m_pSession != null) { m_pSession.Close(); // Wait for the close operation to complete bool res = m_hCloseEvent.WaitOne(5000, true); if (!res) { TRACE(("WaitForSingleObject timed out!")); } } // Complete shutdown operations // 1. Shut down the media source if (m_pSource != null) { m_pSource.Shutdown(); SafeRelease(m_pSource); m_pSource = null; } // 2. Shut down the media session. (Synchronous operation, no events.) if (m_pSession != null) { m_pSession.Shutdown(); Marshal.ReleaseComObject(m_pSession); m_pSession = null; } }
private int OpenMediaSource(IMFMediaSource pSource) { int hr = S_Ok; IMFAttributes pAttributes = null; hr = MFExtern.MFCreateAttributes(out pAttributes, 2); if (Succeeded(hr)) { hr = pAttributes.SetUnknown(MFAttributesClsid.MF_SOURCE_READER_ASYNC_CALLBACK, this); } if (Succeeded(hr)) { IMFSourceReader pReader; hr = MFExtern.MFCreateSourceReaderFromMediaSource( pSource, pAttributes, out pReader ); m_pReader = (IMFSourceReaderAsync)pReader; } SafeRelease(pAttributes); return hr; }
private int OpenMediaSource(IMFMediaSource pSource, ref IMFSourceReaderAsync pReaderAsync) { // Create an attribute store to hold initialization settings. IMFAttributes pAttributes; var hr = MFExtern.MFCreateAttributes(out pAttributes, 2); //if (Succeeded(hr)) //{ // hr = pAttributes.SetUINT32(MFAttributesClsid.MF_READWRITE_DISABLE_CONVERTERS, 1); //} if (Succeeded(hr)) { hr = pAttributes.SetUnknown(MFAttributesClsid.MF_SOURCE_READER_ASYNC_CALLBACK, this); } if (Succeeded(hr)) { IMFSourceReader pReader; hr = MFExtern.MFCreateSourceReaderFromMediaSource(pSource, pAttributes, out pReader); // ReSharper disable once SuspiciousTypeConversion.Global pReaderAsync = (IMFSourceReaderAsync)pReader; } SafeRelease(pAttributes); return hr; }
//////////////////////////////////////////////////////////////////////////////////////// // Name: CPlayer::CreateNodesForStream (Private) // Description: // Creates the source and output nodes for a stream and // Adds them to the topology // Connects the source node to the output node // // Parameter: // pPresentationDescriptor: [in] Pointer to the presentation descriptor for the media source // pMediaSource: [in] Pointer to the media source // pTopology: [in] Pointer to the topology // // Notes: For each stream, the app must: // 1. Create a source node associated with the stream. // 2. Create an output node for the renderer. // 3. Connect the two nodes. // The media session will resolve the topology, transform nodes are not required ///////////////////////////////////////////////////////////////////////////////////////// private void CreateNodesForStream( IMFPresentationDescriptor pPresentationDescriptor, IMFMediaSource pMediaSource, IMFTopology pTopology) { if (pPresentationDescriptor == null || pMediaSource == null || pTopology == null) { throw new COMException("null pointer", E_Pointer); } int hr; IMFStreamDescriptor pStreamDescriptor; IMFTopologyNode pSourceNode; IMFTopologyNode pOutputNode; bool fSelected = false; // Get the stream descriptor for the only stream index =0. hr = pPresentationDescriptor.GetStreamDescriptorByIndex(0, out fSelected, out pStreamDescriptor); MFError.ThrowExceptionForHR(hr); try { if (fSelected) { // Create a source node for this stream and add it to the topology. CreateSourceNode(pPresentationDescriptor, pStreamDescriptor, pMediaSource, out pSourceNode); try { hr = pTopology.AddNode(pSourceNode); MFError.ThrowExceptionForHR(hr); // Create the output node for the renderer and add it to the topology. CreateOutputNode(pStreamDescriptor, out pOutputNode); try { hr = pTopology.AddNode(pOutputNode); MFError.ThrowExceptionForHR(hr); // Connect the source node to the output node. hr = pSourceNode.ConnectOutput(0, pOutputNode, 0); MFError.ThrowExceptionForHR(hr); } finally { SafeRelease(pOutputNode); } } finally { SafeRelease(pSourceNode); } } } finally { //clean up SafeRelease(pStreamDescriptor); } }
/////////////////////////////////////////////////////////////////////// // Name: CreateTopologyBranch // Description: Adds a source and sink to the topology and // connects them. // // pTopology: The topology. // pSource: The media source. // pPD: The source's presentation descriptor. // pSD: The stream descriptor for the stream. // pSink: The media sink. // /////////////////////////////////////////////////////////////////////// static void CreateTopologyBranch( IMFTopology pTopology, IMFMediaSource pSource, // Media source. IMFPresentationDescriptor pPD, // Presentation descriptor. IMFStreamDescriptor pSD, // Stream descriptor. IMFMediaSinkAlt pSink ) { int hr; IMFTopologyNode pSourceNode = null; IMFTopologyNode pOutputNode = null; CreateSourceNode(pSource, pPD, pSD, out pSourceNode); try { CreateOutputNode(pSink, 0, out pOutputNode); try { hr = pTopology.AddNode(pSourceNode); MFError.ThrowExceptionForHR(hr); hr = pTopology.AddNode(pOutputNode); MFError.ThrowExceptionForHR(hr); hr = pSourceNode.ConnectOutput(0, pOutputNode, 0); MFError.ThrowExceptionForHR(hr); } finally { if (pOutputNode != null) { Marshal.ReleaseComObject(pOutputNode); } } } finally { if (pSourceNode != null) { Marshal.ReleaseComObject(pSourceNode); } } }
public int GetMediaSource(out IMFMediaSource ppMediaSource) { // Make sure we *never* leave this entry point with an exception try { m_Log.WriteLine("-GetMediaSource"); ppMediaSource = null; lock (this) { if (m_pSource == null) { throw new COMException("null WavSource", E_Unexpected); } CheckShutdown(); ppMediaSource = (IMFMediaSource)m_pSource; } return S_Ok; } catch (Exception e) { ppMediaSource = null; return Marshal.GetHRForException(e); } }
////////////////////////////////////////////////////////////////////// // Name: CreateSourceNode // Creates a source node for a media stream. // // pSource: Pointer to the media source. // pSourcePD: Pointer to the source's presentation descriptor. // pSourceSD: Pointer to the stream descriptor. // ppNode: Receives the IMFTopologyNode pointer. /////////////////////////////////////////////////////////////////////// static void CreateSourceNode( IMFMediaSource pSource, // Media source. IMFPresentationDescriptor pPD, // Presentation descriptor. IMFStreamDescriptor pSD, // Stream descriptor. out IMFTopologyNode ppNode // Receives the node pointer. ) { int hr; // Create the node. hr = MFExtern.MFCreateTopologyNode( MFTopologyType.SourcestreamNode, out ppNode); MFError.ThrowExceptionForHR(hr); // Set the attributes. hr = ppNode.SetUnknown( MFAttributesClsid.MF_TOPONODE_SOURCE, pSource); MFError.ThrowExceptionForHR(hr); hr = ppNode.SetUnknown( MFAttributesClsid.MF_TOPONODE_PRESENTATION_DESCRIPTOR, pPD); MFError.ThrowExceptionForHR(hr); hr = ppNode.SetUnknown( MFAttributesClsid.MF_TOPONODE_STREAM_DESCRIPTOR, pSD); MFError.ThrowExceptionForHR(hr); }