示例#1
0
        // Token: 0x06000945 RID: 2373 RVA: 0x0001B0A0 File Offset: 0x000192A0
        private long ConvertOneBuffer(IMFSinkWriter writer, int streamIndex, IWaveProvider inputProvider, long position, byte[] managedBuffer)
        {
            long           num            = 0L;
            IMFMediaBuffer imfmediaBuffer = MediaFoundationApi.CreateMemoryBuffer(managedBuffer.Length);
            int            count;

            imfmediaBuffer.GetMaxLength(out count);
            IMFSample imfsample = MediaFoundationApi.CreateSample();

            imfsample.AddBuffer(imfmediaBuffer);
            IntPtr destination;
            int    num2;

            imfmediaBuffer.Lock(out destination, out count, out num2);
            int num3 = inputProvider.Read(managedBuffer, 0, count);

            if (num3 > 0)
            {
                num = MediaFoundationEncoder.BytesToNsPosition(num3, inputProvider.WaveFormat);
                Marshal.Copy(managedBuffer, 0, destination, num3);
                imfmediaBuffer.SetCurrentLength(num3);
                imfmediaBuffer.Unlock();
                imfsample.SetSampleTime(position);
                imfsample.SetSampleDuration(num);
                writer.WriteSample(streamIndex, imfsample);
            }
            else
            {
                imfmediaBuffer.Unlock();
            }
            Marshal.ReleaseComObject(imfsample);
            Marshal.ReleaseComObject(imfmediaBuffer);
            return(num);
        }
示例#2
0
        private long ConvertOneBuffer(IMFSinkWriter writer, int streamIndex, IWaveProvider inputProvider, long position, byte[] managedBuffer, int seconds, ref bool flag)
        {
            long           durationConverted = 0;
            int            maxLength;
            IMFMediaBuffer buffer = MediaFoundationApi.CreateMemoryBuffer(managedBuffer.Length);

            buffer.GetMaxLength(out maxLength);

            IMFSample sample = MediaFoundationApi.CreateSample();

            sample.AddBuffer(buffer);

            IntPtr ptr;
            int    currentLength;

            buffer.Lock(out ptr, out maxLength, out currentLength);
            int oneLength = inputProvider.WaveFormat.AverageBytesPerSecond;
            int read      = 0;

            if (flag)
            {
                for (int i = 0; i < seconds; i++)
                {
                    read = inputProvider.Read(managedBuffer, 0, oneLength);
                }
                flag = false;
            }
            else
            {
                read = inputProvider.Read(managedBuffer, 0, oneLength);
            }

            if (read > 0)
            {
                durationConverted = BytesToNsPosition(read, inputProvider.WaveFormat);
                Marshal.Copy(managedBuffer, 0, ptr, read);
                buffer.SetCurrentLength(read);
                buffer.Unlock();
                sample.SetSampleTime(position);
                sample.SetSampleDuration(durationConverted);
                writer.WriteSample(streamIndex, sample);
                //writer.Flush(streamIndex);
            }
            else
            {
                buffer.Unlock();
            }

            Marshal.ReleaseComObject(sample);
            Marshal.ReleaseComObject(buffer);
            return(durationConverted);
        }
示例#3
0
        // Get the buffers and sizes to be modified, then pass them
        // to the transform routine.
        private void DoWork(IMFMediaBuffer pIn, IMFMediaBuffer pOut)
        {
            IntPtr      pSrc       = IntPtr.Zero;
            int         lSrcStride = 0;
            IMF2DBuffer pIn2D      = null;

            // Lock the input buffer. Use IMF2DBuffer if available.
            Lockit(pIn, out pIn2D, out lSrcStride, out pSrc);

            try
            {
                IntPtr  pDest;
                int     mlo;
                int     cb;
                MFError throwonhr = pOut.Lock(out pDest, out mlo, out cb);

                try
                {
                    // Invoke the image transform function.
                    YUY2_TO_RGB32(pSrc, pDest, m_imageWidthInPixels, m_imageHeightInPixels, lSrcStride);
                }
                finally
                {
                    pOut.Unlock();
                }
            }
            finally
            {
                // Ignore error
                UnlockIt(pSrc, pIn2D, pIn);
            }
        }
示例#4
0
        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);
            }
        }
示例#5
0
        // Token: 0x06000A59 RID: 2649 RVA: 0x0001E1A8 File Offset: 0x0001C3A8
        private IMFSample ReadFromSource()
        {
            int num = this.sourceProvider.Read(this.sourceBuffer, 0, this.sourceBuffer.Length);

            if (num == 0)
            {
                return(null);
            }
            IMFMediaBuffer imfmediaBuffer = MediaFoundationApi.CreateMemoryBuffer(num);
            IntPtr         destination;
            int            num2;
            int            num3;

            imfmediaBuffer.Lock(out destination, out num2, out num3);
            Marshal.Copy(this.sourceBuffer, 0, destination, num);
            imfmediaBuffer.Unlock();
            imfmediaBuffer.SetCurrentLength(num);
            IMFSample imfsample = MediaFoundationApi.CreateSample();

            imfsample.AddBuffer(imfmediaBuffer);
            imfsample.SetSampleTime(this.inputPosition);
            long num4 = MediaFoundationTransform.BytesToNsPosition(num, this.sourceProvider.WaveFormat);

            imfsample.SetSampleDuration(num4);
            this.inputPosition += num4;
            Marshal.ReleaseComObject(imfmediaBuffer);
            return(imfsample);
        }
示例#6
0
        private bool ConsumeBuffer(IMFMediaBuffer buff, MediaItem item)
        {
            buff.Lock(out IntPtr ptr, out int maxLen, out int curLen);
            try
            {
                try
                {
                    byte[] temp = new byte[curLen];
                    Marshal.Copy(ptr, temp, 0, temp.Length);
                    int stride    = item.Width * 3;
                    int tmpheight = item.Height;

                    BitmapSource src = BitmapSource.Create(item.Width, tmpheight, 600, 600, PixelFormats.Bgr24, null, temp, stride);
                    image.Source = src;

                    return(true);
                }
                catch (ObjectDisposedException)
                {
                    return(false);
                }
            }
            finally
            {
                buff.Unlock();
            }
        }
示例#7
0
        // Get the buffers and sizes to be modified, then pass them
        // to the appropriate Update_* routine.
        private HResult DoWork(IMFMediaBuffer pIn)
        {
            MFError throwonhr;
            int     cb;

            IntPtr pSrc;                // Source buffer.
            int    lSrcStride = 0;      // Source stride.

            bool bLockedInputBuffer = false;

            IMF2DBuffer pIn2D;

            // While there are exceptions thrown here, they should never happen
            // in real life.

            // Lock the input buffer. Use IMF2DBuffer if available.
            pIn2D = pIn as IMF2DBuffer;
            if (pIn2D != null)
            {
                throwonhr = pIn2D.Lock2D(out pSrc, out lSrcStride);
            }
            else
            {
                int ml;
                throwonhr  = pIn.Lock(out pSrc, out ml, out cb);
                lSrcStride = m_lStrideIfContiguous;
            }
            bLockedInputBuffer = true;

            // Invoke the image transform function.
            if (m_pTransformFn != null)
            {
                m_pTransformFn(pSrc, lSrcStride,
                               m_imageWidthInPixels, m_imageHeightInPixels);
            }
            else
            {
                return(HResult.E_UNEXPECTED);
            }

            if (bLockedInputBuffer)
            {
                if (pIn2D != null)
                {
                    throwonhr = pIn2D.Unlock2D();
                }
                else
                {
                    throwonhr = pIn.Unlock();
                }
            }

            // Set the data size on the output buffer.
            throwonhr = pIn.SetCurrentLength(m_cbImageSize);

            return(HResult.S_OK);
        }
示例#8
0
        private static void UnlockIt(IntPtr pSrc, IMF2DBuffer pIn2D, IMFMediaBuffer pIn)
        {
            if (pSrc != IntPtr.Zero)
            {
                MFError throwonhr;

                if (pIn2D != null)
                {
                    throwonhr = pIn2D.Unlock2D();
                }
                else
                {
                    throwonhr = pIn.Unlock();
                }
            }
        }
示例#9
0
        //-------------------------------------------------------------------
        // UnlockBuffer
        //
        // Unlocks the buffer. Called automatically by the destructor.
        //-------------------------------------------------------------------

        public void UnlockBuffer()
        {
            if (bLocked)
            {
                if (p2DBuffer != null)
                {
                    p2DBuffer.Unlock2D();
                }
                else
                {
                    pBuffer.Unlock();
                }

                bLocked = false;
            }
        }
示例#10
0
        //-------------------------------------------------------------------
        // UnlockBuffer
        //
        // Unlocks the buffer. Called automatically by the destructor.
        //-------------------------------------------------------------------

        public void UnlockBuffer()
        {
            if (m_bLocked)
            {
                if (m_p2DBuffer != null)
                {
                    m_p2DBuffer.Unlock2D();
                }
                else
                {
                    m_pBuffer.Unlock();
                }

                m_bLocked = false;
            }
        }
示例#11
0
 private bool ConsumeBuffer(IMFMediaBuffer buff, MediaItem item)
 {
     buff.Lock(out IntPtr ptr, out int maxLen, out int curLen);
     try
     {
         Bitmap pic        = new Bitmap(item.Width, item.Height, PixelFormat.Format24bppRgb);
         var    bitmapData = pic.LockBits(
             new Rectangle(0, 0, pic.Width, pic.Height),
             ImageLockMode.WriteOnly,
             PixelFormat.Format24bppRgb
             );
         try
         {
             byte[] temp = new byte[curLen];
             Marshal.Copy(ptr, temp, 0, temp.Length);
             Marshal.Copy(temp, 0, bitmapData.Scan0, Math.Min(temp.Length, bitmapData.Stride * bitmapData.Height));
         }
         finally
         {
             pic.UnlockBits(bitmapData);
         }
         if (item.Stride < 0)
         {
             pic.RotateFlip(RotateFlipType.RotateNoneFlipY);
         }
         try
         {
             Invoke((Action <Bitmap>)UpdatePreview, pic);
             return(true);
         }
         catch (ObjectDisposedException)
         {
             return(false);
         }
     }
     finally
     {
         buff.Unlock();
     }
 }
示例#12
0
        /////////////////////////////////////////////////////////////////////
        // Name: ReadDataIntoBuffer
        //
        // Reads data from a byte stream and returns a media buffer that
        // contains the data.
        //
        // pStream: Pointer to the byte stream
        // cbToRead: Number of bytes to read
        // ppBuffer: Receives a pointer to the buffer.
        /////////////////////////////////////////////////////////////////////

        void ReadDataIntoBuffer(
            IMFByteStream pStream,      // Pointer to the byte stream.
            int cbToRead,               // Number of bytes to read
            out IMFMediaBuffer ppBuffer // Receives a pointer to the buffer.
            )
        {
            HResult hr;
            IntPtr  pData;
            int     cbRead; // Actual amount of data read
            int     iMax, iCur;

            // Create the media buffer. This function allocates the memory.
            hr = MFExtern.MFCreateMemoryBuffer(cbToRead, out ppBuffer);
            MFError.ThrowExceptionForHR(hr);

            // Access the buffer.
            hr = ppBuffer.Lock(out pData, out iMax, out iCur);
            MFError.ThrowExceptionForHR(hr);

            try
            {
                // Read the data from the byte stream.
                hr = pStream.Read(pData, cbToRead, out cbRead);
                MFError.ThrowExceptionForHR(hr);
            }
            finally
            {
                hr = ppBuffer.Unlock();
                MFError.ThrowExceptionForHR(hr);
                pData = IntPtr.Zero;
            }

            // Update the size of the valid data.
            hr = ppBuffer.SetCurrentLength(cbRead);
            MFError.ThrowExceptionForHR(hr);
        }
示例#13
0
        /// <summary>Generates the "tail" of the audio effect.</summary>
        /// <param name="InputMessageNumber">Message number to use with OutputSample.</param>
        /// <remarks>
        /// Generates the "tail" of the audio effect. The tail is the portion
        /// of the delay effect that is heard after the input stream ends.
        ///
        /// To generate the tail, the client must drain the MFT by sending
        /// the MFT_MESSAGE_COMMAND_DRAIN message and then call ProcessOutput
        /// to get the tail samples.
        /// </remarks>
        private void ProcessEffectTail(int InputMessageNumber)
        {
            IMFMediaBuffer pOutputBuffer = null;

            MFError throwonhr;
            IntPtr  pbOutputData     = IntPtr.Zero; // Pointer to the memory in the output buffer.
            int     cbOutputLength   = 0;           // Size of the output buffer.
            int     cbBytesProcessed = 0;           // How much data we processed.

            IMFSample pOutSample = null;

            // Allocate an output buffer.
            throwonhr = MFExtern.MFCreateMemoryBuffer(m_cbTailSamples, out pOutputBuffer);

            try
            {
                throwonhr = MFExtern.MFCreateSample(out pOutSample);
                throwonhr = pOutSample.AddBuffer(pOutputBuffer);

                // Lock the output buffer.
                int cb;
                throwonhr = pOutputBuffer.Lock(out pbOutputData, out cbOutputLength, out cb);

                // Calculate how many audio samples we can process.
                cbBytesProcessed = Math.Min(m_cbTailSamples, cbOutputLength);

                // Round to the next lowest multiple of nBlockAlign.
                cbBytesProcessed -= (cbBytesProcessed % m_Alignment);

                // Fill the output buffer with silence, because we are also using it as the input buffer.
                FillBufferWithSilence(pbOutputData, cbBytesProcessed);

                // Process the data.
                ProcessAudio(pbOutputData, pbOutputData, cbBytesProcessed / m_Alignment);

                // Set the data length on the output buffer.
                throwonhr = pOutputBuffer.SetCurrentLength(cbBytesProcessed);

                if (m_rtTimestamp >= 0)
                {
                    long hnsDuration = (cbBytesProcessed / m_AvgBytesPerSec) * UNITS;

                    // Set the time stamp and duration on the output sample.
                    throwonhr = pOutSample.SetSampleTime(m_rtTimestamp);
                    throwonhr = pOutSample.SetSampleDuration(hnsDuration);
                }

                // Done.
                m_cbTailSamples = 0;

                OutputSample(pOutSample, InputMessageNumber);
            }
            catch
            {
                SafeRelease(pOutSample);
                throw;
            }
            finally
            {
                if (pbOutputData != IntPtr.Zero)
                {
                    pOutputBuffer.Unlock();
                }
                SafeRelease(pOutputBuffer);
            }
        }
示例#14
0
        /////////////////////////////////////////////////////////////////////
        // Name: ReadDataIntoBuffer
        //
        // Reads data from a byte stream and returns a media buffer that
        // contains the data.
        //
        // pStream: Pointer to the byte stream
        // cbToRead: Number of bytes to read
        // ppBuffer: Receives a pointer to the buffer.
        /////////////////////////////////////////////////////////////////////
        void ReadDataIntoBuffer(
            IMFByteStream pStream,     // Pointer to the byte stream.
            int cbToRead,             // Number of bytes to read
            out IMFMediaBuffer ppBuffer   // Receives a pointer to the buffer.
            )
        {
            IntPtr pData;
            int cbRead;   // Actual amount of data read
            int iMax, iCur;

            // Create the media buffer. This function allocates the memory.
            int hr = MFExtern.MFCreateMemoryBuffer(cbToRead, out ppBuffer);
            MFError.ThrowExceptionForHR(hr);

            // Access the buffer.
            hr = ppBuffer.Lock(out pData, out iMax, out iCur);
            MFError.ThrowExceptionForHR(hr);

            try
            {
                // Read the data from the byte stream.
                hr = pStream.Read(pData, cbToRead, out cbRead);
                MFError.ThrowExceptionForHR(hr);
            }
            finally
            {
                hr = ppBuffer.Unlock();
                MFError.ThrowExceptionForHR(hr);
                pData = IntPtr.Zero;
            }

            // Update the size of the valid data.
            hr = ppBuffer.SetCurrentLength(cbRead);
            MFError.ThrowExceptionForHR(hr);
        }