protected void StartPlayback() { TRACE("CPlayer::StartPlayback"); Debug.Assert(m_pSession != null); HResult hr; hr = m_pSession.Start(Guid.Empty, new PropVariant()); MFError.ThrowExceptionForHR(hr); }
public static void MFGetAttribute2UINT32asUINT64(IMFAttributes pAttributes, Guid g, out int nNumerator, out int nDenominator) { long ul; HResult hr; hr = pAttributes.GetUINT64(g, out ul); MFError.ThrowExceptionForHR(hr); nDenominator = (int)ul; nNumerator = (int)(ul >> 32); }
public static void MFSetAttribute2UINT32asUINT64(IMFAttributes pAttributes, Guid g, int nNumerator, int nDenominator) { HResult hr; long ul = nNumerator; ul <<= 32; ul |= (UInt32)nDenominator; hr = pAttributes.SetUINT64(g, ul); MFError.ThrowExceptionForHR(hr); }
private void OnActivate(IDeviceInfo moniker) { activeDevice = moniker as MfDevice; var format = string.Empty; var hr = camProcess.SetDevice(activeDevice, ref format); if (!string.IsNullOrEmpty(format)) { eventAggregator.GetEvent <NoticeFormatEvent>().Publish(format); } MFError.ThrowExceptionForHR(hr); }
// Sets whether pan/scan mode is enabled. public void SetPanScanEnabled(bool bEnabled) { int i = 0; if (bEnabled) { i = 1; } HResult hr = GetMediaType().SetUINT32(MFAttributesClsid.MF_MT_PAN_SCAN_ENABLED, i); MFError.ThrowExceptionForHR(hr); }
//////////////////////////////////////////////////////////////////////////////////////// // Name: CPlayer::ShutDown (Public) // Description: // Releases all resources and shuts down Media Foundation /////////////////////////////////////////////////////////////////////////////////////////// HResult ShutDown() { Debug.WriteLine("\nCPlayer::ShutDown"); HResult hr = 0; IMFMediaSource pMediaSource; try { //Call shutdown on the sequencer source pMediaSource = (IMFMediaSource)m_pSequencerSource; hr = pMediaSource.Shutdown(); MFError.ThrowExceptionForHR(hr); //Close media session if (m_pMediaSession != null) { hr = m_pMediaSession.Close(); MFError.ThrowExceptionForHR(hr); // Wait for the close operation to complete bool res = m_hCloseEvent.WaitOne(5000, false); if (!res) { Debug.WriteLine("WaitForSingleObject timed out!"); } m_hCloseEvent.Close(); m_hCloseEvent = null; } //Shutdown media session hr = m_pMediaSession.Shutdown(); MFError.ThrowExceptionForHR(hr); // Shut down Media Foundation. hr = MFExtern.MFShutdown(); MFError.ThrowExceptionForHR(hr); } catch (Exception e) { hr = (HResult)Marshal.GetHRForException(e); } SafeRelease(m_pMediaSession); SafeRelease(m_pSequencerSource); SafeRelease(m_pPresentationClock); SafeRelease(m_pAudioRendererActivate); return(hr); }
private void selectCaptureDeviceToolStripMenuItem_Click(object sender, EventArgs e) { using (CaptureDevice DevSelection = new CaptureDevice()) { if (DevSelection.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) { // Give this source to the CPlayer object for preview. HResult hr = m_pPreview.SetDevice(DevSelection.SelectedCaptureDevice); MFError.ThrowExceptionForHR(hr); } } }
// Specifies whether each sample is independent of the other samples in the stream. public void SetAllSamplesIndependent(bool bIndependent) { int i = 0; if (bIndependent) { i = 1; } HResult hr = GetMediaType().SetUINT32(MFAttributesClsid.MF_MT_ALL_SAMPLES_INDEPENDENT, i); MFError.ThrowExceptionForHR(hr); }
protected void AddBranchToPartialTopology( IMFTopology pTopology, IMFPresentationDescriptor pSourcePD, int iStream ) { TRACE("CPlayer::AddBranchToPartialTopology"); Debug.Assert(pTopology != null); HResult hr; IMFStreamDescriptor pSourceSD = null; IMFTopologyNode pSourceNode = null; IMFTopologyNode pOutputNode = null; bool fSelected = false; try { // Get the stream descriptor for this stream. hr = pSourcePD.GetStreamDescriptorByIndex(iStream, out fSelected, out pSourceSD); MFError.ThrowExceptionForHR(hr); // Create the topology branch only if the stream is selected. // Otherwise, do nothing. if (fSelected) { // Create a source node for this stream. CreateSourceStreamNode(pSourcePD, pSourceSD, out pSourceNode); // Create the output node for the renderer. CreateOutputNode(pSourceSD, out pOutputNode); // Add both nodes to the topology. hr = pTopology.AddNode(pSourceNode); MFError.ThrowExceptionForHR(hr); 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 { // Clean up. SafeRelease(pSourceSD); SafeRelease(pSourceNode); SafeRelease(pOutputNode); } }
public static MFRatio GetFrameRate(IMFMediaType pMediaType) { HResult hr; long i64; MFRatio fps; hr = pMediaType.GetUINT64(MFAttributesClsid.MF_MT_FRAME_RATE, out i64); MFError.ThrowExceptionForHR(hr); fps.Numerator = (int)(i64 >> 32); fps.Denominator = (int)i64; return(fps); }
//------------------------------------------------------------------- // Name: MoveToChunkOffset // Description: // Move the file pointer to a byte offset from the start of the // current chunk. //------------------------------------------------------------------- public void MoveToChunkOffset(int dwOffset) { HResult hr; if (dwOffset > m_chunk.DataSize()) { throw new COMException("End of chunk", (int)HResult.E_INVALIDARG); } hr = m_pStream.SetCurrentPosition(m_llCurrentChunkOffset + dwOffset + Marshal.SizeOf(typeof(RIFFCHUNK))); MFError.ThrowExceptionForHR(hr); m_dwBytesRemaining = m_chunk.DataSize() - dwOffset; }
public CaptureFormat[] GetCaptureFormats() { if (m_pSource == null) { CreateVideoCaptureSource(); } IMFPresentationDescriptor pPD = null; IMFStreamDescriptor pSD = null; IMFMediaTypeHandler pHandler = null; IMFMediaType pType = null; int hr = m_pSource.CreatePresentationDescriptor(out pPD); MFError.ThrowExceptionForHR(hr); bool fSelected; hr = pPD.GetStreamDescriptorByIndex(0, out fSelected, out pSD); MFError.ThrowExceptionForHR(hr); hr = pSD.GetMediaTypeHandler(out pHandler); MFError.ThrowExceptionForHR(hr); int cTypes = 0; hr = pHandler.GetMediaTypeCount(out cTypes); MFError.ThrowExceptionForHR(hr); CaptureFormat[] captureFormats = new CaptureFormat[cTypes]; for (int i = 0; i < cTypes; i++) { hr = pHandler.GetMediaTypeByIndex(i, out pType); MFError.ThrowExceptionForHR(hr); CaptureFormat mediatype = LogMediaType(pType); Trace.WriteLine(mediatype); Trace.WriteLine("Media Type " + i.ToString()); captureFormats[i] = mediatype; //OutputDebugString(L"\n"); Marshal.FinalReleaseComObject(pType); } Marshal.FinalReleaseComObject(pPD); Marshal.FinalReleaseComObject(pSD); Marshal.FinalReleaseComObject(pHandler); Marshal.FinalReleaseComObject(pType); return(captureFormats); }
////////////////////////////////////////////////////////////////////////// // Name: CPlayer::Initialize // Description: // Intializes Media Foundation // Creates a media session // Creates a sequencer source // Creates a presentation clock // Creates an audio renderer // Starts the event queue // ///////////////////////////////////////////////////////////////////////// public HResult Initialize() { Debug.WriteLine("\nCPlayer::Initialize"); HResult hr = 0; try { IMFClock pClock; // Initialize Media Foundation. hr = MFExtern.MFStartup(0x10070, MFStartup.Full); MFError.ThrowExceptionForHR(hr); // Create the media session. hr = MFExtern.MFCreateMediaSession(null, out m_pMediaSession); MFError.ThrowExceptionForHR(hr); // Start the event queue. hr = m_pMediaSession.BeginGetEvent(this, null); MFError.ThrowExceptionForHR(hr); // Create a sequencer Source. hr = MFExtern.MFCreateSequencerSource(null, out m_pSequencerSource); MFError.ThrowExceptionForHR(hr); //setup clock hr = m_pMediaSession.GetClock(out pClock); MFError.ThrowExceptionForHR(hr); m_pPresentationClock = (IMFPresentationClock)pClock; // Create an IMFActivate object for the audio renderer. hr = MFExtern.MFCreateAudioRendererActivate(out m_pAudioRendererActivate); MFError.ThrowExceptionForHR(hr); //Set the player state to Initialized m_State = PlayerState.Initialized; // Notify the app that the player is initialized. PostMessage(m_hWnd, Form1.WM_NOTIFY_APP, new IntPtr((int)PlayerEvent.Initialized), new IntPtr((int)m_State)); } catch (Exception e) { hr = (HResult)Marshal.GetHRForException(e); } //Clean up. return(hr); }
protected void CreateSession() { // Close the old session, if any. CloseSession(); // Create the media session. HResult hr = MFExtern.MFCreateMediaSession(null, out m_pSession); MFError.ThrowExceptionForHR(hr); // Start pulling events from the media session hr = m_pSession.BeginGetEvent(this, null); MFError.ThrowExceptionForHR(hr); }
// Constructor public CPreview(IntPtr hVideo, IntPtr hEvent) { m_pReader = null; m_hwndEvent = hEvent; m_pwszSymbolicLink = null; m_draw = new DrawDevice(); HResult hr = MFExtern.MFStartup(0x20070, MFStartup.Lite); MFError.ThrowExceptionForHR(hr); hr = m_draw.CreateDevice(hVideo); MFError.ThrowExceptionForHR(hr); }
///////////////////////////////////////////////////////////////////// // Name: CreateASFSplitter // // Creates the ASF splitter. // // pContentInfo: Pointer to an initialized instance of the ASF // content information object. // ppSplitter: Receives a pointer to the ASF splitter. ///////////////////////////////////////////////////////////////////// void CreateASFSplitter(IMFASFContentInfo pContentInfo, out IMFASFSplitter ppSplitter) { MFASFSplitterFlags f; HResult hr; hr = MFExtern.MFCreateASFSplitter(out ppSplitter); MFError.ThrowExceptionForHR(hr); hr = ppSplitter.Initialize(pContentInfo); MFError.ThrowExceptionForHR(hr); hr = ppSplitter.GetFlags(out f); MFError.ThrowExceptionForHR(hr); Console.WriteLine(string.Format("Splitter flags: {0}", f)); }
private static MFVideoArea GetArea(IMFAttributes ia, Guid g) { PropVariant pv = new PropVariant(); HResult hr = ia.GetItem(g, pv); if (hr == HResult.MF_E_ATTRIBUTENOTFOUND) { return(null); } MFError.ThrowExceptionForHR(hr); return(pv.GetBlob(typeof(MFVideoArea)) as MFVideoArea); }
protected void CreateTopologyFromSource(out IMFTopology ppTopology) { TRACE("CPlayer::CreateTopologyFromSource"); Debug.Assert(m_pSession != null); Debug.Assert(m_pSource != null); IMFTopology pTopology = null; IMFPresentationDescriptor pSourcePD = null; int cSourceStreams = 0; HResult hr; try { // Create a new topology. hr = MFExtern.MFCreateTopology(out pTopology); MFError.ThrowExceptionForHR(hr); // Create the presentation descriptor for the media source. hr = m_pSource.CreatePresentationDescriptor(out pSourcePD); MFError.ThrowExceptionForHR(hr); // Get the number of streams in the media source. hr = pSourcePD.GetStreamDescriptorCount(out cSourceStreams); MFError.ThrowExceptionForHR(hr); TRACE(string.Format("Stream count: {0}", cSourceStreams)); // For each stream, create the topology nodes and add them to the topology. for (int i = 0; i < cSourceStreams; i++) { AddBranchToPartialTopology(pTopology, m_hwndVideo, m_pSource, pSourcePD, i); } // Return the IMFTopology pointer to the caller. ppTopology = pTopology; } catch { // If we failed, release the topology SafeRelease(pTopology); throw; } finally { SafeRelease(pSourcePD); } }
public HResult OpenURL(string sURL) { TRACE("CPlayer::OpenURL"); TRACE("URL = " + sURL); // 1. Create a new media session. // 2. Create the media source. // 3. Create the topology. // 4. Queue the topology [asynchronous] // 5. Start playback [asynchronous - does not happen in this method.] HResult hr = HResult.S_OK; try { IMFTopology pTopology = null; // Create the media session. CreateSession(); // Create the media source. CreateMediaSource(sURL); // Create a partial topology. CreateTopologyFromSource(out pTopology); // Set the topology on the media session. hr = m_pSession.SetTopology(0, pTopology); MFError.ThrowExceptionForHR(hr); // Set our state to "open pending" m_state = PlayerState.OpenPending; NotifyState(); SafeRelease(pTopology); // If SetTopology succeeded, the media session will queue an // MESessionTopologySet event. } catch (Exception ce) { hr = (HResult)Marshal.GetHRForException(ce); NotifyError(hr); m_state = PlayerState.Ready; } return(hr); }
//////////////////////////////////////////////////////////////////////////////////////// // Name: CPlayer::DeleteSegment (Public) // Description: // Deletes the corresponding topology from the sequencer source // Parameter: // SegmentID: [in] The segment identifier /////////////////////////////////////////////////////////////////////////////////////////// public HResult DeleteSegment(int SegmentID) { Debug.WriteLine(string.Format("\nCPlayer::DeleteSegment: {0}", SegmentID)); HResult hr = 0; try { if (m_ActiveSegment == SegmentID) { throw new COMException("Can't delete active segment", (int)HResult.E_INVALIDARG); } int SegId = 0; hr = m_pSequencerSource.DeleteTopology(SegmentID); MFError.ThrowExceptionForHR(hr); m_Segments.GetLastSegmentId(out SegId); //Delete the segment entry from the list. m_Segments.DeleteSegmentEntry(SegmentID); //Is the deleted topology the last one? if (SegId == SegmentID) { //Get the new last segment id try { m_Segments.GetLastSegmentId(out SegId); //set this topology as the last in the sequencer hr = m_pSequencerSource.UpdateTopologyFlags(SegId, MFSequencerTopologyFlags.Last); MFError.ThrowExceptionForHR(hr); } catch { } } PostMessage(m_hWnd, Form1.WM_NOTIFY_APP, new IntPtr((int)PlayerEvent.SegmentDeleted), new IntPtr(SegmentID)); } catch (Exception e) { hr = (HResult)Marshal.GetHRForException(e); } return(hr); }
static private void DllRegisterServer(Type t) { HResult hr = MFExtern.MFTRegister( t.GUID, MFTransformCategory.MFT_CATEGORY_VIDEO_EFFECT, t.Name, MFT_EnumFlag.AsyncMFT, 0, null, 0, null, null ); MFError.ThrowExceptionForHR(hr); }
//------------------------------------------------------------------- // Name: ReadDataFromChunk // Description: // Read data from the current chunk. (Starts at the current file ptr.) //------------------------------------------------------------------- public void ReadDataFromChunk(IntPtr pData, int dwLengthInBytes) { HResult hr; if (dwLengthInBytes > m_dwBytesRemaining) { throw new COMException("End of chunk", (int)HResult.E_INVALIDARG); } int cbRead = 0; hr = m_pStream.Read(pData, dwLengthInBytes, out cbRead); MFError.ThrowExceptionForHR(hr); m_dwBytesRemaining -= cbRead; }
/////////////////////////////////////////////////////////////////////// // 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); }
// Constructor protected CProcess(IntPtr hVideo, IntPtr hEvent) { this.hVideo = hVideo; PReader = null; HwndEvent = hEvent; PwszSymbolicLink = null; var hr = MFExtern.MFStartup(0x20070, MFStartup.Lite); MFError.ThrowExceptionForHR(hr); Draw = new DrawDevice(); hr = Draw.CreateDevice(hVideo); MFError.ThrowExceptionForHR(hr); }
public HResult BeginCreateObject( IMFByteStream pByteStream, string pwszURL, MFResolution dwFlags, IPropertyStore pProps, // Can be NULL. out object ppIUnknownCancelCookie, // Can be NULL. IMFAsyncCallback pCallback, object punkState // Can be NULL ) { // Make sure we *never* leave this entry point with an exception try { HResult hr; m_Log.WriteLine("BeginCreateObject"); ppIUnknownCancelCookie = null; // We don't return a cancellation cookie. if ((pByteStream == null) || (pwszURL == null) || (pCallback == null)) { throw new COMException("bad stream, url, or callback", (int)HResult.E_INVALIDARG); } IMFAsyncResult pResult = null; WavSource pSource = new WavSource(); pSource.Open(pByteStream); hr = MFExtern.MFCreateAsyncResult(pSource as IMFMediaSource, pCallback, punkState, out pResult); MFError.ThrowExceptionForHR(hr); hr = MFExtern.MFInvokeCallback(pResult); MFError.ThrowExceptionForHR(hr); if (pResult != null) { Marshal.ReleaseComObject(pResult); } return(HResult.S_OK); } catch (Exception e) { ppIUnknownCancelCookie = null; return((HResult)Marshal.GetHRForException(e)); } }
//////////////////////////////////////////////////////////////////////////////////////// // Name: CPlayer::CreateOutputNode (Private) // Description: // Creates an output node for a stream // Sets the IActivate pointer on the node // Parameter: // pStreamDescriptor: [in] Stream descriptor for the stream // ppSourceNode: [out] Receives a pointer to the new node //////////////////////////////////////////////////////////////////////////////////////// private void CreateOutputNode( IMFStreamDescriptor pStreamDescriptor, out IMFTopologyNode ppOutputNode) { if (pStreamDescriptor == null) { throw new COMException("null pointer", (int)HResult.E_POINTER); } IMFMediaTypeHandler pHandler; Guid guidMajorType = Guid.Empty; // Create a downstream node. HResult hr = MFExtern.MFCreateTopologyNode(MFTopologyType.OutputNode, out ppOutputNode); MFError.ThrowExceptionForHR(hr); // Get the media type handler for the stream. hr = pStreamDescriptor.GetMediaTypeHandler(out pHandler); MFError.ThrowExceptionForHR(hr); try { // Get the major media type. pHandler.GetMajorType(out guidMajorType); // Set the IActivate object on the output node. if (MFMediaType.Audio == guidMajorType) { hr = ppOutputNode.SetObject(m_pAudioRendererActivate); MFError.ThrowExceptionForHR(hr); Debug.WriteLine(("Audio stream")); } //Only audio is implemented, if guidMajorType is any other type, return E_NOTIMPL else { Debug.WriteLine(("Unsupported stream")); throw new COMException("Unsupported stream", (int)HResult.E_NOTIMPL); } } finally { // Clean up. SafeRelease(pHandler); } }
/////////////////////////////////////////////////////////////////////// // 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: CPlayer::GetPresentationTime (Public) // Description: // Gets the unadjusted presentation time // Parameter: // phnsPresentationTime: [out] presentation time /////////////////////////////////////////////////////////////////////////////////////////// public HResult GetPresentationTime(out long phnsPresentationTime) { HResult hr = 0; try { hr = m_pPresentationClock.GetTime(out phnsPresentationTime); MFError.ThrowExceptionForHR(hr); } catch (Exception e) { hr = (HResult)Marshal.GetHRForException(e); phnsPresentationTime = 0; } return(hr); }
/////////////////////////////////////////////////////////////////////// // 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: CPlayer::Stop (Public) // Description: // Stops the media session with the current topology /////////////////////////////////////////////////////////////////////////////////////////// public HResult Stop() { Debug.WriteLine("CPlayer::Stop"); HResult hr = 0; try { hr = m_pMediaSession.Stop(); MFError.ThrowExceptionForHR(hr); } catch (Exception e) { hr = (HResult)Marshal.GetHRForException(e); } return(hr); }