public static byte[] GetDataFromMediaSample(IMFSample sample) { IMFMediaBuffer pMediaBuffer = null; IntPtr pBuffer; int maxLen = 0, curLen = 0; //HResult hr = sample.LockStore(); HResult hr = sample.ConvertToContiguousBuffer(out pMediaBuffer); MFError.ThrowExceptionForHR(hr); try { hr = pMediaBuffer.Lock(out pBuffer, out maxLen, out curLen); MFError.ThrowExceptionForHR(hr); try { byte[] data = new byte[curLen]; Marshal.Copy(pBuffer, data, 0, curLen); return(data); } finally { hr = pMediaBuffer.Unlock(); } } finally { //hr = sample.UnlockStore(); COMBase.SafeRelease(pMediaBuffer); } }
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); }
/// <summary> /// Opens the media by initializing the DirectShow graph /// </summary> protected virtual void OpenSource() { /* Make sure we clean up any remaining mess */ FreeResources(); string sURL = m_sourceUri.ToString(); if (string.IsNullOrEmpty(sURL)) { return; } int hr = 0; try { IMFTopology pTopology = null; // Create the media session. CreateSession(); // Create the media source. CreateMediaSource(sURL); // Create a partial topology. pTopology = CreateTopologyFromSource(); // 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(); m_pSession.GetClock(out pClock); COMBase.SafeRelease(pTopology); SetDuration(); //AddAudioInterface(); // If SetTopology succeeded, the media session will queue an // MESessionTopologySet event. } catch (Exception ce) { hr = Marshal.GetHRForException(ce); /* This exection will happen usually if the media does * not exist or could not open due to not having the * proper filters installed */ FreeResources(); /* Fire our failed event */ InvokeMediaFailed(new MediaFailedEventArgs(ce.Message, ce)); } InvokeMediaOpened(); }
protected IMFTopologyNode CreateSourceStreamNode(IMFPresentationDescriptor pSourcePD, IMFStreamDescriptor pSourceSD) { int hr; //Debug.Assert(m_pSource != null); IMFTopologyNode pNode = null; try { // Create the source-stream node. hr = MFExtern.MFCreateTopologyNode(MFTopologyType.SourcestreamNode, out pNode); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the media source. hr = pNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_SOURCE, m_pSource); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the presentation descriptor. hr = pNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_PRESENTATION_DESCRIPTOR, pSourcePD); MFError.ThrowExceptionForHR(hr); // Set attribute: Pointer to the stream descriptor. hr = pNode.SetUnknown(MFAttributesClsid.MF_TOPONODE_STREAM_DESCRIPTOR, pSourceSD); MFError.ThrowExceptionForHR(hr); // Return the IMFTopologyNode pointer to the caller. return(pNode); } catch { // If we failed, release the pnode COMBase.SafeRelease(pNode); throw; } }
protected void AddBranchToPartialTopology(IMFTopology pTopology, IMFPresentationDescriptor pSourcePD, int iStream) { int hr; //TRACE("CPlayer::AddBranchToPartialTopology"); //Debug.Assert(pTopology != null); 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. pSourceNode = CreateSourceStreamNode(pSourcePD, pSourceSD); // Add both nodes to the topology. hr = pTopology.AddNode(pSourceNode); MFError.ThrowExceptionForHR(hr); // Create the output node for the renderer. pOutputNode = CreateOutputNode(pSourceSD); if (pOutputNode == null) { throw new Exception("Could not create output node"); } 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. COMBase.SafeRelease(pSourceSD); COMBase.SafeRelease(pSourceNode); COMBase.SafeRelease(pOutputNode); } }
protected IMFTopology CreateTopologyFromSource() { int hr = 0; //TRACE("CPlayer::CreateTopologyFromSource"); //Assert(m_pSession != null); //Debug.Assert(m_pSource != null); IMFTopology pTopology = null; IMFPresentationDescriptor pSourcePD = null; int cSourceStreams = 0; 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, pSourcePD, i); } } catch { // If we failed, release the topology COMBase.SafeRelease(pTopology); throw; } finally { COMBase.SafeRelease(pSourcePD); } return(pTopology); }
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; } }
protected void SetDuration() { if (m_pSource == null) { return; } long duration = 0; IMFPresentationDescriptor pPD = null; int hr = m_pSource.CreatePresentationDescriptor(out pPD); if (hr == 0) { pPD.GetUINT64(MFAttributesClsid.MF_PD_DURATION, out duration); } COMBase.SafeRelease(pPD); Duration = duration; }
/// <summary> /// Configures the DirectShow graph to play the selected video capture /// device with the selected parameters /// </summary> private void SetupGraph() { /* Clean up any messes left behind */ FreeResources(); Trace.WriteLine("setting up graph 1"); try { Trace.WriteLine("setting up graph 2"); // Create the media session. CreateSession(); Trace.WriteLine("setting up graph 3"); CreateVideoCaptureSource(); // Create a partial topology. IMFTopology pTopology = CreateTopologyFromSource(); Trace.WriteLine("setting up graph 4"); // Set the topology on the media session. int hr = m_pSession.SetTopology(0, pTopology); MFError.ThrowExceptionForHR(hr); //m_pSession.GetClock(out pClock); Trace.WriteLine("setting up graph 5"); COMBase.SafeRelease(pTopology); //Marshal.ReleaseComObject(); } catch (Exception ex) { Marshal.GetHRForException(ex); Trace.WriteLine("SetupGraph Exception " + ex.ToString()); FreeResources(); InvokeMediaFailed(new MediaFailedEventArgs(ex.Message, ex)); } /* Success */ InvokeMediaOpened(); }
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); }
public IMFSample CreateSampleAndAllocateTexture() { IMFSample pSample; IMFTrackedSample pTrackedSample; HResult hr; // Create the video sample. This function returns an IMFTrackedSample per MSDN hr = MFExtern.MFCreateVideoSampleFromSurface(null, out pSample); MFError.ThrowExceptionForHR(hr); // Query the IMFSample to see if it implements IMFTrackedSample pTrackedSample = pSample as IMFTrackedSample; if (pTrackedSample == null) { // Throw an exception if we didn't get an IMFTrackedSample // but this shouldn't happen in practice. throw new InvalidCastException("MFCreateVideoSampleFromSurface returned a sample that did not implement IMFTrackedSample"); } // Use our own class to allocate a texture SharpDX.Direct3D11.Texture2D availableTexture = AllocateTexture(); // Convert the texture's native ID3D11Texture2D pointer into // an IUnknown (represented as as System.Object) object texNativeObject = Marshal.GetObjectForIUnknown(availableTexture.NativePointer); // Create the media buffer from the texture IMFMediaBuffer p2DBuffer; hr = MFExtern.MFCreateDXGISurfaceBuffer(s_IID_ID3D11Texture2D, texNativeObject, 0, false, out p2DBuffer); // Release the object-as-IUnknown we created above COMBase.SafeRelease(texNativeObject); // If media buffer creation failed, throw an exception MFError.ThrowExceptionForHR(hr); // Set the owning instance of this class as the allocator // for IMFTrackedSample to notify when the sample is released pTrackedSample.SetAllocator(this, null); // Attach the created buffer to the sample pTrackedSample.AddBuffer(p2DBuffer); return(pTrackedSample); }
protected IMFTopologyNode CreateOutputNode(IMFStreamDescriptor pSourceSD) { IMFTopologyNode pNode = null; IMFMediaTypeHandler pHandler = null; IMFActivate pRendererActivate = null; Guid guidMajorType = Guid.Empty; int hr = 0; // Get the stream ID. int streamID = 0; try { try { hr = pSourceSD.GetStreamIdentifier(out streamID); // Just for debugging, ignore any failures. MFError.ThrowExceptionForHR(hr); } catch { //TRACE("IMFStreamDescriptor::GetStreamIdentifier" + hr.ToString()); } // Get the media type handler for the stream. hr = pSourceSD.GetMediaTypeHandler(out pHandler); MFError.ThrowExceptionForHR(hr); // Get the major media type. hr = pHandler.GetMajorType(out guidMajorType); MFError.ThrowExceptionForHR(hr); // Create a downstream node. hr = MFExtern.MFCreateTopologyNode(MFTopologyType.OutputNode, out pNode); MFError.ThrowExceptionForHR(hr); // Create an IMFActivate object for the renderer, based on the media type. if (MFMediaType.Audio == guidMajorType) { // Create the audio renderer. hr = MFExtern.MFCreateAudioRendererActivate(out pRendererActivate); MFError.ThrowExceptionForHR(hr); object sar; pRendererActivate.ActivateObject(typeof(IMFMediaSink).GUID, out sar); StreamingAudioRenderer = sar as IMFMediaSink; } else if (MFMediaType.Video == guidMajorType) { // Create the video renderer. pRendererActivate = CreateVideoRenderer(); } else { //TRACE(string.Format("Stream {0}: Unknown format", streamID)); throw new COMException("Unknown format"); } // Set the IActivate object on the output node. hr = pNode.SetObject(pRendererActivate); MFError.ThrowExceptionForHR(hr); } catch (Exception ex) { // If we failed, release the pNode COMBase.SafeRelease(pNode); throw; } finally { // Clean up. COMBase.SafeRelease(pHandler); COMBase.SafeRelease(pRendererActivate); } return(pNode); }
static void Main(string[] args) { while (true) { Console.WriteLine("0: Demo,1: MF"); string select; select = Console.ReadLine(); if (select == "0") { Console.WriteLine("---------Press Enter to intialize NvEncoder ---------"); Console.ReadLine(); NvEncoder nvEncoder = new NvEncoder(); Console.ReadLine(); //nvEncoder.GetAPIFromManaged(dllPointer, procAddress); Console.WriteLine("Start EncodeMain"); Console.ReadLine(); string stop; while (true) { int result = nvEncoder.EncodeMain(); Console.WriteLine("Key in 'stop' to stop the while loop"); stop = Console.ReadLine(); if (stop == "stop") { break; } } break; } else if (select == "1") { HResult hr = MFExtern.MFStartup(MF_VERSION, MFStartup.Full); IMFSourceReader ppSourceReader; //public extern static HResult MFCreateSourceReaderFromByteStream(IMFByteStream pByteStream, IMFAttributes pAttributes, out IMFSourceReader ppSourceReader); hr = MFExtern.MFCreateSourceReaderFromURL(@"06-13-17-15-58-17-1.mp4", null, out ppSourceReader); //IMFSourceResolver sourceResolver; //MFObjectType pObjectType; //object ppObject; //MFExtern.MFCreateSourceResolver(out sourceResolver); //HResult CreateObjectFromURL(string pwszURL, MFResolution dwFlags, IPropertyStore pProps, out MFObjectType pObjectType, out object ppObject); //hr = sourceResolver.CreateObjectFromURL(@"tulips_yvu420_prog_planar_qcif.yuv", MFResolution.MediaSource, null, out pObjectType, out ppObject); //IMFMediaSource medaiaSource; //hr = MFExtern.MFCreateSourceReaderFromMediaSource(); int pdwActualStreamIndex; MF_SOURCE_READER_FLAG pdwStreamFlags = MF_SOURCE_READER_FLAG.None; long pllTimestamp; IMFSample ppSample; //Guid temp; IMFMediaType mediaType; MFExtern.MFCreateMediaType(out mediaType); mediaType.SetGUID(MFAttributesClsid.MF_MT_MAJOR_TYPE, MFMediaType.Video); mediaType.SetGUID(MFAttributesClsid.MF_MT_SUBTYPE, MFMediaType.NV12); hr = MFExtern.MFSetAttributeSize(mediaType, MFAttributesClsid.MF_MT_FRAME_SIZE, 1920, 1080); hr = MFExtern.MFSetAttributeRatio(mediaType, MFAttributesClsid.MF_MT_FRAME_RATE, 30, 1); mediaType.SetUINT32(MFAttributesClsid.MF_MT_INTERLACE_MODE, (int)MFVideoInterlaceMode.Progressive); hr = MFExtern.MFSetAttributeRatio(mediaType, MFAttributesClsid.MF_MT_PIXEL_ASPECT_RATIO, 1, 1); hr = ppSourceReader.SetCurrentMediaType(1, null, mediaType); MFError.ThrowExceptionForHR(hr); COMBase.SafeRelease(mediaType); //IMFMediaType tempMediaType; //ppSourceReader.GetCurrentMediaType(1, out tempMediaType); //tempMediaType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out temp); //COMBase.SafeRelease(tempMediaType); Console.WriteLine("---------Press Enter to intialize NvEncoder ---------"); NvEncoder nvEncoder = new NvEncoder(); Console.ReadLine(); //nvEncoder.GetAPIFromManaged(dllPointer, procAddress); Console.WriteLine("Initialize NvEncoder"); bool result = true; result = nvEncoder.InitializeNvEncoder(MFMediaType.NV12, 1920, 1088, 30); //byte[] inputData = null; //byte[] outputData = null; //IntPtr outputData = IntPtr.Zero; //for (int i = 0; i < inputData.Length; i++ ) //{ // inputData[i] = 1; //} //IntPtr inputPointer = Marshal.AllocHGlobal(inputData.Length); //Marshal.Copy(inputData, 0, inputPointer, inputData.Length); //IntPtr outputPointer = Marshal.AllocHGlobal(outputData.Length); //Marshal.Copy(outputData, 0, outputPointer, outputData.Length); int processResult = 0; StartByteArrayToFile("Newh264AtProgram.h264"); while (result) { byte[] inputData = null; byte[] outputData = null; bool isKey = false; //HResult ReadSample(int dwStreamIndex, MF_SOURCE_READER_CONTROL_FLAG dwControlFlags, out int pdwActualStreamIndex, out MF_SOURCE_READER_FLAG pdwStreamFlags, out long pllTimestamp, out IMFSample ppSample); ppSourceReader.ReadSample(1, MF_SOURCE_READER_CONTROL_FLAG.None, out pdwActualStreamIndex, out pdwStreamFlags, out pllTimestamp, out ppSample); if (pdwStreamFlags == MF_SOURCE_READER_FLAG.EndOfStream) { Console.WriteLine("End of stream"); nvEncoder.EndOfProcessData(); while (true) { processResult = nvEncoder.ProcessData(null, 1920, 1088, out outputData); if (outputData != null) { ByteArrayToFile(outputData); } if (processResult == -1) { break; } } StopByteArrayToFile(); nvEncoder.FinalizeEncoder(); break; } if (ppSample != null) { inputData = MFLib.GetDataFromMediaSample(ppSample); nvEncoder.ProcessData(inputData, 1920, 1088, out outputData); if (outputData != null) { Console.WriteLine(outputData[4]); ByteArrayToFile(outputData); } // Call unmanaged code //Marshal.FreeHGlobal(inputPointer); //Marshal.FreeHGlobal(outputPointer); COMBase.SafeRelease(ppSample); } System.Threading.Thread.Sleep(100); } //Release COM objects COMBase.SafeRelease(ppSourceReader); MFExtern.MFShutdown(); Console.WriteLine("Shutdown MF"); Console.ReadLine(); break; } } //double x = 1; //double y = 2; //double result = 0; //Arithmetics ar = new Arithmetics(); //result = ar.Add(x, y); //Console.WriteLine("x = {0}, y = {1}, result = {2}", x, y, result); //Console.ReadLine(); //Console.WriteLine("---------Press Enter to get API---------"); //Console.ReadLine(); //IntPtr dllPointer = LoadLibrary("nvEncodeAPI.dll"); //IntPtr procAddress = GetProcAddress(dllPointer, "NvEncodeAPICreateInstance"); }