Example #1
0
        //-------------------------------------------------------------------
        // Name: WriteSampleToFile
        // Description: Output one media sample to the file.
        //-------------------------------------------------------------------
        void WriteSampleToFile(IMFSample pSample)
        {
            int hr;
            int i;
            long time;
            int cBufferCount; // Number of buffers in the sample.
            IntPtr pData;
            int cbData = 0;
            int cbWritten = 0;

            // Get the time stamp
            hr = pSample.GetSampleTime(out time);
            MFError.ThrowExceptionForHR(hr);

            // If the time stamp is too early, just discard this sample.
            if (time < m_StartTime)
            {
                return;
            }

            // Note: If there is no time stamp on the sample, proceed anyway.

            // Find how many buffers are in this sample.
            hr = pSample.GetBufferCount(out cBufferCount);
            MFError.ThrowExceptionForHR(hr);

            // Loop through all the buffers in the sample.
            for (int iBuffer = 0; iBuffer < cBufferCount; iBuffer++)
            {
                IMFMediaBuffer pBuffer = null;

                hr = pSample.GetBufferByIndex(iBuffer, out pBuffer);
                MFError.ThrowExceptionForHR(hr);

                try
                {
                    // Lock the buffer and write the data to the file.
                    hr = pBuffer.Lock(out pData, out i, out cbData);
                    MFError.ThrowExceptionForHR(hr);

                    hr = m_pByteStream.Write(pData, cbData, out cbWritten);
                    MFError.ThrowExceptionForHR(hr);

                    hr = pBuffer.Unlock();
                    MFError.ThrowExceptionForHR(hr);

                    // Update the running tally of bytes written.
                    m_cbDataWritten += cbData;
                }
                finally
                {
                    SafeRelease(pBuffer);
                }
            }   // for loop
        }