// IMFSourceReaderCallback methods //------------------------------------------------------------------- // OnReadSample // // Called when the IMFMediaSource::ReadSample method completes. //------------------------------------------------------------------- public int OnReadSample(int hrStatus, int dwStreamIndex, MF_SOURCE_READER_FLAG dwStreamFlags, long llTimestamp, IMFSample pSample) { var hr = hrStatus; IMFMediaBuffer pBuffer = null; lock (LockSync) { try { if (pSample != null) { // Get the video frame buffer from the sample. if (Succeeded(hr)) { hr = pSample.GetBufferByIndex(0, out pBuffer); } if (Succeeded(hr)) { hr = OnFrame(pSample, pBuffer, llTimestamp, snapFormat); snapFormat = string.Empty; } } // Request the next frame. if (Succeeded(hr)) { // Read next sample. hr = PReader.ReadSample( (int)MF_SOURCE_READER.FirstVideoStream, 0, IntPtr.Zero, // actual IntPtr.Zero, // flags IntPtr.Zero, // time stamp IntPtr.Zero // sample ); } if (Failed(hr)) { NotifyError(hr); } } finally { SafeRelease(pBuffer); SafeRelease(pSample); } } return(hr); }
// IMFSourceReaderCallback methods //------------------------------------------------------------------- // OnReadSample // // Called when the IMFMediaSource::ReadSample method completes. //------------------------------------------------------------------- public HResult OnReadSample(HResult hrStatus, int dwStreamIndex, MF_SOURCE_READER_FLAG dwStreamFlags, long llTimestamp, IMFSample pSample) { HResult hr = hrStatus; IMFMediaBuffer pBuffer = null; lock (this) { try { if (Succeeded(hr)) { if (pSample != null) { // Get the video frame buffer from the sample. hr = pSample.GetBufferByIndex(0, out pBuffer); // Draw the frame. if (Succeeded(hr)) { hr = m_draw.DrawFrame(pBuffer); } } } // Request the next frame. if (Succeeded(hr)) { // Ask for the first sample. hr = m_pReader.ReadSample((int)MF_SOURCE_READER.FirstVideoStream, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } if (Failed(hr)) { NotifyError(hr); } } finally { //SafeRelease(pBuffer); SafeRelease(pSample); } } return(hr); }
// IMFSourceReaderCallback methods //------------------------------------------------------------------- // OnReadSample // // Called when the IMFMediaSource::ReadSample method completes. //------------------------------------------------------------------- public int OnReadSample(int hrStatus, int dwStreamIndex, MF_SOURCE_READER_FLAG dwStreamFlags, long llTimestamp, IMFSample pSample) { int hr = hrStatus; IMFMediaBuffer pBuffer = null; lock (this) { try { if (Succeeded(hr)) { if (pSample != null) { // Get the video frame buffer from the sample. hr = pSample.GetBufferByIndex(0, out pBuffer); // Draw the frame. if (Succeeded(hr)) { hr = m_draw.DrawFrame(pBuffer); } } } // Request the next frame. if (Succeeded(hr)) { // Ask for the first sample. hr = m_pReader.ReadSample((int)MF_SOURCE_READER.FirstVideoStream, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } if (Failed(hr)) { NotifyError(hr); } } finally { //SafeRelease(pBuffer); SafeRelease(pSample); } } return hr; }
/// <summary> /// Sets the native media type for a stream on the media source. /// </summary> /// <param name="sourceReader">A valid IMFSourceReaderEx instance.</param></param> /// <param name="streamIndex">The stream to set.</param> /// <param name="mediaType">An instance of the IMFMediaType interface for the media type.</param> /// <param name="streamFlags">Receives one or more members of the MF_SOURCE_READER_FLAG enumeration.</param> /// <returns>If this function succeeds, it returns the S_OK member. Otherwise, it returns another HResult's member that describe the error.</returns> public static HResult SetNativeMediaType(this IMFSourceReaderEx sourceReader, SourceReaderFirstStream streamIndex, IMFMediaType mediaType, out MF_SOURCE_READER_FLAG streamFlags) { if (sourceReader == null) { throw new ArgumentNullException("sourceReader"); } return(sourceReader.SetNativeMediaType((int)streamIndex, mediaType, out streamFlags)); }
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// This gets called when a Called IMFSourceReader.ReadSample method completes /// (assuming the SourceReader has been given this class during setup with /// an attribute of MFAttributesClsid.MF_SOURCE_READER_ASYNC_CALLBACK). /// /// The first ReadSample triggers it after that it continues by itself /// </summary> /// <param name="hrStatus">The status code. If an error occurred while processing the next sample, this parameter contains the error code.</param> /// <param name="streamIndex">The zero-based index of the stream that delivered the sample.</param> /// <param name="streamFlags">A bitwise OR of zero or more flags from the MF_SOURCE_READER_FLAG enumeration.</param> /// <param name="sampleTimeStamp">The time stamp of the sample, or the time of the stream event indicated in streamFlags. The time is given in 100-nanosecond units. </param> /// <param name="mediaSample">A pointer to the IMFSample interface of a media sample. This parameter might be NULL.</param> /// <returns>Returns an HRESULT value. Reputedly, the source reader ignores the return value.</returns> /// <history> /// 01 Nov 18 Cynic - Started /// </history> public HResult OnReadSample(HResult hrStatus, int streamIndex, MF_SOURCE_READER_FLAG streamFlags, long sampleTimeStamp, IMFSample mediaSample) { HResult hr = HResult.S_OK; try { lock (this) { // are we capturing? if not leave if (IsCapturing() == false) { return(HResult.S_OK); } // have we got an error? if (Failed(hrStatus)) { string errMsg = "OnReadSample, Error on call =" + hrStatus.ToString(); SourceReaderAsyncCallBackError(this, errMsg, null); return(hrStatus); } // have we got a sample? It seems this can be null on the first sample // in after the ReadSample that triggered this. So we just ignore it // and request the next to get things rolling if (mediaSample != null) { /* This rebases the timestamps coming off the webcam so that * the first one starts at zero. Not needed in later versions * of WMF apparently. It seems the Media Sink now does this automatically * * If the data is not being written correctly, try uncommenting this. * * // we have a sample, if so is it the first non null one? * if (isFirstSample) * { * // yes it is set up our timestamp * firstSampleBaseTime = sampleTimeStamp; * isFirstSample = false; * } * * // Samples have a time stamp and a duration. The time stamp indicates when the data in the sample * // should be rendered, relative to the presentation clock. The duration is the length of time * // for which the data should be rendered. * // * // We now set the presentation time of the sample. This is the presentation time * // in 100-nanosecond units. We rebase this using the first sample * // processed as zero. * * // rebase the time stamp * sampleTimeStamp -= firstSampleBaseTime; * hr = mediaSample.SetSampleTime(sampleTimeStamp); * if (Failed(hr)) * { * string errMsg = "OnReadSample, Error on SetSampleTime =" + hr.ToString(); * SourceReaderAsyncCallBackError(this, errMsg, null); * return hr; * } */ // write the sample out hr = sinkWriter.WriteSample(0, mediaSample); if (Failed(hr)) { string errMsg = "OnReadSample, Error on WriteSample =" + hr.ToString(); SourceReaderAsyncCallBackError(this, errMsg, null); return(hr); } } // Read another sample. hr = (sourceReader as IMFSourceReaderAsync).ReadSample( TantaWMFUtils.MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, IntPtr.Zero, // actual IntPtr.Zero, // flags IntPtr.Zero, // timestamp IntPtr.Zero // sample ); if (Failed(hr)) { string errMsg = "OnReadSample, Error on ReadSample =" + hr.ToString(); SourceReaderAsyncCallBackError(this, errMsg, null); return(hr); } } } catch (Exception ex) { if (SourceReaderAsyncCallBackError != null) { SourceReaderAsyncCallBackError(this, ex.Message, ex); } } finally { SafeRelease(mediaSample); } return(hr); }
/// <summary> /// Reads the next sample from the media source. /// </summary> /// <param name="sourceReader">A valid IMFSourceReader instance.</param></param></param> /// <param name="streamIndex">The stream to pull data from.</param> /// <param name="controlFlags">One or more members of the MF_SOURCE_READER_CONTROL_FLAG enumeration.</param> /// <param name="actualStreamIndex">Receives the zero-based index of the stream.</param> /// <param name="streamFlags">Receives one or more members of the MF_SOURCE_READER_FLAG enumeration.</param> /// <param name="timestamp">Receives the time stamp of the sample, or the time of the stream event indicated in <paramref name="streamFlags"/>.</param> /// <param name="sample">Receives an instance of the IMFSample interface or the value null.</param> /// <returns>If this function succeeds, it returns the S_OK member. Otherwise, it returns another HResult's member that describe the error.</returns> public static HResult ReadSample(this IMFSourceReader sourceReader, SourceReaderStreams streamIndex, MF_SOURCE_READER_CONTROL_FLAG controlFlags, out int actualStreamIndex, out MF_SOURCE_READER_FLAG streamFlags, out TimeSpan timestamp, out IMFSample sample) { if (sourceReader == null) { throw new ArgumentNullException("sourceReader"); } long tmp = 0; HResult hr = sourceReader.ReadSample((int)streamIndex, controlFlags, out actualStreamIndex, out streamFlags, out tmp, out sample); timestamp = hr.Succeeded() ? TimeSpan.FromTicks(tmp) : default(TimeSpan); return(hr); }
/// <summary> /// This gets called when a Called IMFSourceReader.ReadSample method completes /// (assuming the SourceReader has been given this class during setup with /// an attribute of MFAttributesClsid.MF_SOURCE_READER_ASYNC_CALLBACK). /// The first ReadSample triggers it after that it continues by itself /// </summary> /// <param name="hrStatus">The status code. If an error occurred while processing the next sample, this parameter contains the error code.</param> /// <param name="streamIndex">The zero-based index of the stream that delivered the sample.</param> /// <param name="streamFlags">A bitwise OR of zero or more flags from the MF_SOURCE_READER_FLAG enumeration.</param> /// <param name="sampleTimeStamp">The time stamp of the sample, or the time of the stream event indicated in streamFlags. The time is given in 100-nanosecond units. </param> /// <param name="mediaSample">A pointer to the IMFSample interface of a media sample. This parameter might be NULL.</param> /// <returns>Returns an HRESULT value. Reputedly, the source reader ignores the return value.</returns> public HResult OnReadSample(HResult hrStatus, int streamIndex, MF_SOURCE_READER_FLAG streamFlags, long sampleTimeStamp, IMFSample mediaSample) { HResult hr = HResult.S_OK; switch (streamIndex) { case 0: Console.WriteLine(streamFlags); break; case 1: break; } try { lock (this) { // have we got an error? if (Failed(hrStatus)) { } else { // have we got a sample? It seems this can be null on the first sample // in after the ReadSample that triggered this. So we just ignore it // and request the next to get things rolling if (mediaSample != null) { if (streamIndex == 1) { } try { if (!HandlingImage) { HandlingImage = true; mediaSample.GetBufferByIndex(0, out IMFMediaBuffer Buffer); Buffer.Lock(out IntPtr bufPointer, out int MaxLength, out int CurrLength); byte[] managedArray = new byte[MaxLength * 480]; Marshal.Copy(bufPointer, managedArray, 0, MaxLength); Buffer.Unlock(); SKImage temp = SKImage.FromEncodedData(SKImage.FromEncodedData(managedArray).Encode()); switch (streamIndex) { case 0: OnFrame(this, new FrameReadyEventArgs { FrameBuffer = null, Image = temp }); break; case 1: OnMFB(this, new FrameReadyEventArgs { FrameBuffer = null, Image = temp }); break; } HandlingImage = false; } } catch { HandlingImage = false; } } else { } } switch (streamIndex) { case 0: //StreamReader.Flush(1); // Read another sample. hr = StreamReader.ReadSample( 0, MediaFoundation.ReadWrite.MF_SOURCE_READER_CONTROL_FLAG.None, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero ); // Read another sample. hr = StreamReader.ReadSample( 1, MediaFoundation.ReadWrite.MF_SOURCE_READER_CONTROL_FLAG.Drain, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero ); break; } } } catch { } finally { SafeRelease(mediaSample); } return(hr); }
public HResult OnReadSample( HResult hrStatus, int dwStreamIndex, MF_SOURCE_READER_FLAG dwStreamFlags, long llTimeStamp, IMFSample pSample // Can be null ) { HResult hr = HResult.S_OK; try { lock (this) { if (!IsCapturing()) { return(HResult.S_OK); } if (Failed(hrStatus)) { hr = hrStatus; goto done; } if (pSample != null) { if (m_bFirstSample) { m_llBaseTime = llTimeStamp; m_bFirstSample = false; } // rebase the time stamp llTimeStamp -= m_llBaseTime; hr = pSample.SetSampleTime(llTimeStamp); if (Failed(hr)) { goto done; } hr = m_pWriter.WriteSample(0, pSample); if (Failed(hr)) { goto done; } } // Read another sample. hr = m_pReader.ReadSample( MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, IntPtr.Zero, // actual IntPtr.Zero, // flags IntPtr.Zero, // timestamp IntPtr.Zero // sample ); done: if (Failed(hr)) { PostMessage(m_hwndEvent, m_iMessageID, new IntPtr((int)hr), IntPtr.Zero); } } } catch (Exception e) { hr = (HResult)Marshal.GetHRForException(e); } finally { SafeRelease(pSample); } return(hr); }
public int OnReadSample( int hrStatus, int dwStreamIndex, MF_SOURCE_READER_FLAG dwStreamFlags, long llTimeStamp, IMFSample pSample // Can be null ) { int hr = S_Ok; try { lock (this) { if (!IsCapturing()) { return S_Ok; } if (Failed(hrStatus)) { hr = hrStatus; goto done; } if (pSample != null) { if (m_bFirstSample) { m_llBaseTime = llTimeStamp; m_bFirstSample = false; } // rebase the time stamp llTimeStamp -= m_llBaseTime; hr = pSample.SetSampleTime(llTimeStamp); if (Failed(hr)) { goto done; } hr = m_pWriter.WriteSample(0, pSample); if (Failed(hr)) { goto done; } } // Read another sample. hr = m_pReader.ReadSample( MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, IntPtr.Zero, // actual IntPtr.Zero, // flags IntPtr.Zero, // timestamp IntPtr.Zero // sample ); done: if (Failed(hr)) { PostMessage(m_hwndEvent, m_iMessageID, new IntPtr(hr), IntPtr.Zero); } } } catch (Exception e) { hr = Marshal.GetHRForException(e); } finally { SafeRelease(pSample); } return hr; }
// IMFSourceReaderCallback methods //------------------------------------------------------------------- // OnReadSample // // Called when the IMFMediaSource::ReadSample method completes. //------------------------------------------------------------------- public int OnReadSample(int hrStatus, int dwStreamIndex, MF_SOURCE_READER_FLAG dwStreamFlags, long llTimestamp, IMFSample pSample) { var hr = hrStatus; IMFMediaBuffer pBuffer = null; lock (LockSync) { try { if (pSample != null) { // Get the video frame buffer from the sample. if (Succeeded(hr)) hr = pSample.GetBufferByIndex(0, out pBuffer); if (Succeeded(hr)) { hr = OnFrame(pSample, pBuffer, llTimestamp, snapFormat); snapFormat = string.Empty; } } // Request the next frame. if (Succeeded(hr)) { // Read next sample. hr = PReader.ReadSample( (int) MF_SOURCE_READER.FirstVideoStream, 0, IntPtr.Zero, // actual IntPtr.Zero, // flags IntPtr.Zero, // time stamp IntPtr.Zero // sample ); } if (Failed(hr)) { NotifyError(hr); } } finally { SafeRelease(pBuffer); SafeRelease(pSample); } } return hr; }
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"); }