public HResult GetMediaSink(out IMFMediaSinkAlt ppMediaSink) { Debug.WriteLine("StreamSink:GetMediaSink"); ppMediaSink = null; HResult hr = BaseSink == null ? E_INVALIDARG : S_OK; if (Failed(hr)) { return(hr); } lock (this) { hr = CheckShutdown(); if (Failed(hr)) { return(hr); } ppMediaSink = BaseSink; } return(hr); }
private static HResult CreateTopology(out IMFTopology topology, out IMFMediaSinkAlt mediaSink, IMFMediaSource source) { HResult hr = S_OK; topology = null; mediaSink = null; IMFPresentationDescriptor presentationDescriptor = null; hr = MF.CreateTopology(out topology); if (Failed(hr)) { return(hr); } hr = source.CreatePresentationDescriptor(out presentationDescriptor); if (Failed(hr)) { topology = null; return(hr); } hr = BuildTopology(out mediaSink, topology, presentationDescriptor, source); if (Failed(hr)) { mediaSink = null; topology = null; return(hr); } return(hr); }
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); }
/////////////////////////////////////////////////////////////////////// // Name: CreateOutputNode // Description: Creates an output node for a stream sink. // // pSink: The media sink. // iStream: Index of the stream sink on the media sink. // ppNode: Receives a pointer to the topology node. /////////////////////////////////////////////////////////////////////// static void CreateOutputNode(IMFMediaSinkAlt pSink, int iStream, out IMFTopologyNode ppNode) { HResult hr; IMFStreamSinkAlt pStream = null; hr = pSink.GetStreamSinkByIndex(iStream, out pStream); MFError.ThrowExceptionForHR(hr); hr = MFExtern.MFCreateTopologyNode(MFTopologyType.OutputNode, out ppNode); MFError.ThrowExceptionForHR(hr); hr = ppNode.SetObject(pStream); MFError.ThrowExceptionForHR(hr); //Marshal.ReleaseComObject(pStream); }
/////////////////////////////////////////////////////////////////////// // Name: CreateOutputNode // Description: Creates an output node for a stream sink. // // pSink: The media sink. // iStream: Index of the stream sink on the media sink. // ppNode: Receives a pointer to the topology node. /////////////////////////////////////////////////////////////////////// static void CreateOutputNode(IMFMediaSinkAlt pSink, int iStream, out IMFTopologyNode ppNode) { int hr; IMFStreamSinkAlt pStream = null; hr = pSink.GetStreamSinkByIndex(iStream, out pStream); MFError.ThrowExceptionForHR(hr); hr = MFExtern.MFCreateTopologyNode(MFTopologyType.OutputNode, out ppNode); MFError.ThrowExceptionForHR(hr); hr = ppNode.SetObject(pStream); MFError.ThrowExceptionForHR(hr); //Marshal.ReleaseComObject(pStream); }
/////////////////////////////////////////////////////////////////////// // 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(); } }
/////////////////////////////////////////////////////////////////////// // 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); } } }
//------------------------------------------------------------------- // Name: GetMediaSink // Description: Returns the parent media sink. //------------------------------------------------------------------- public int GetMediaSink(out IMFMediaSinkAlt ppMediaSink) { TRACE("CWavStream::GetMediaSink"); lock (this) { CheckShutdown(); ppMediaSink = (IMFMediaSinkAlt)m_pSink; } return S_Ok; }
private static HResult CreateOutputNode(IMFStreamDescriptor streamDescriptor, out IMFMediaSinkAlt mediaSink, out IMFTopologyNode node) { HResult hr = S_OK; mediaSink = null; node = null; IMFMediaTypeHandler mediaTypeHandler = null; IMFActivate activate = null; IMFStreamSinkAlt streamSink = null; Guid majorType = Guid.Empty; int streamSinkCount = 0; hr = streamDescriptor.GetMediaTypeHandler(out mediaTypeHandler); if (Failed(hr)) { return(hr); } hr = mediaTypeHandler.GetMajorType(out majorType); if (Failed(hr)) { SafeRelease(mediaTypeHandler); return(hr); } hr = MF.CreateTopologyNode(MFTopologyType.OutputNode, out node); if (Failed(hr)) { SafeRelease(mediaTypeHandler); return(hr); } if (majorType == MFMediaType.Video) { ExternalMediaSink extMediaSink = new ExternalMediaSink(); mediaSink = (IMFMediaSinkAlt)extMediaSink; hr = mediaSink.GetStreamSinkCount(out streamSinkCount); if (Failed(hr)) { mediaSink = null; return(hr); } hr = mediaSink.GetStreamSinkByIndex(0, out streamSink); if (Failed(hr)) { mediaSink = null; return(hr); } hr = node.SetObject(streamSink); if (Failed(hr)) { mediaSink = null; return(hr); } } else if (majorType == MFMediaType.Audio) { hr = MF.CreateAudioRendererActivate(out activate); if (Failed(hr)) { return(hr); } hr = node.SetObject(activate); if (Failed(hr)) { return(hr); } } mediaTypeHandler = null; activate = null; streamSink = null; return(hr); }
private static HResult BuildTopology(out IMFMediaSinkAlt mediaSink, IMFTopology topology, IMFPresentationDescriptor presentationDescriptor, IMFMediaSource source) { HResult hr = S_OK; mediaSink = null; IMFMediaSinkAlt tempMediaSink = null; IMFStreamDescriptor streamDescriptor = null; IMFTopologyNode sourceNode = null; IMFTopologyNode outputNode = null; bool selected = false; int streamCount = 0; hr = presentationDescriptor.GetStreamDescriptorCount(out streamCount); if (Failed(hr)) { return(hr); } for (int i = 0; i < streamCount; i++) { hr = presentationDescriptor.GetStreamDescriptorByIndex(i, out selected, out streamDescriptor); if (Failed(hr)) { return(hr); } if (selected) { hr = CreateSourceStreamNode(source, streamDescriptor, presentationDescriptor, out sourceNode); if (Failed(hr)) { return(hr); } hr = CreateOutputNode(streamDescriptor, out tempMediaSink, out outputNode); if (Failed(hr)) { mediaSink = null; return(hr); } if (tempMediaSink != null) { mediaSink = tempMediaSink; } hr = topology.AddNode(sourceNode); if (Failed(hr)) { mediaSink = null; return(hr); } hr = topology.AddNode(outputNode); if (Failed(hr)) { mediaSink = null; return(hr); } hr = sourceNode.ConnectOutput(0, outputNode, 0); if (Failed(hr)) { mediaSink = null; return(hr); } } } return(hr); }
/////////////////////////////////////////////////////////////////////// // 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) { HResult 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: 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); } } }
/////////////////////////////////////////////////////////////////////// // 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"); } }