/// <summary>
        /// The callback from the GSSF to populate the sample.  This class isn't intended
        /// to be overridden.  Child classes should instead implement PopulateSample,
        /// which this method calls.
        /// </summary>
        /// <param name="pSample">The sample to populate</param>
        /// <returns>HRESULT</returns>
        public int SampleCallback(IMediaSample pSample)
        {
            int    hr;
            IntPtr pData;

            try
            {
                // Get the buffer into which we will copy the data
                hr = pSample.GetPointer(out pData);
                if (hr >= 0)
                {
                    // Find out the amount of space in the buffer
                    int cbData = pSample.GetSize();

                    lock (this)
                    {
                        hr = SetTimeStamps(pSample);
                        if (hr >= 0)
                        {
                            int iRead;

                            // Populate the sample
                            hr = PopulateSample(pData, cbData, out iRead);
                            if (hr >= 0)
                            {
                                if (hr == S_Ok) // 1 == End of stream
                                {
                                    // increment the frame number for next time
                                    m_iFrameNumber++;
                                }
                                else
                                {
                                    m_iFrameNumber = 0;
                                }

                                pSample.SetActualDataLength(iRead);
                            }
                        }
                    }
                }
            }
            finally
            {
                // Release our pointer the the media sample.  THIS IS ESSENTIAL!  If
                // you don't do this, the graph will stop after about 2 samples.
                Marshal.ReleaseComObject(pSample);
            }

            return(hr);
        }
Exemple #2
0
        /// <summary>
        /// Called by the GenericSampleSourceFilter.  This routine populates the MediaSample.
        /// </summary>
        /// <param name="sample">Pointer to a sample</param>
        /// <returns>0 = success, 1 = end of stream, negative values for errors</returns>
        public virtual int SampleCallback(IMediaSample sample)
        {
            int    hr;
            IntPtr dataPointer;

            try
            {
                // Get the buffer into which we will copy the data
                hr = sample.GetPointer(out dataPointer);
                if (hr >= 0)
                {
                    // Set TRUE on every sample for uncompressed frames
                    hr = sample.SetSyncPoint(true);
                    if (hr >= 0)
                    {
                        // Find out the amount of space in the buffer
                        int callbackData = sample.GetSize();

                        hr = this.SetTimeStamps(sample);
                        if (hr >= 0)
                        {
                            int read;

                            // Get copy the data into the sample
                            hr = this.GetImage(this.frameNumber, dataPointer, callbackData, out read);

                            // 1 == End of stream
                            if (hr == 0)
                            {
                                sample.SetActualDataLength(read);

                                // increment the frame number for next time
                                this.frameNumber++;
                            }
                        }
                    }
                }
            }
            finally
            {
                // Release our pointer the the media sample.  THIS IS ESSENTIAL!  If
                // you don't do this, the graph will stop after about 2 samples.
                Marshal.ReleaseComObject(sample);
            }

            return(hr);
        }
Exemple #3
0
        private int PopulateMediaSample(IMediaSample pSample)
        {
            int  hr;
            long tStart, tStop;

            // Read the file offsets they are seeking
            hr = pSample.GetTime(out tStart, out tStop);
            if (hr >= 0)
            {
                IntPtr pBuffer = IntPtr.Zero;

                // Turn the sample time into a file position and length
                long llPos       = tStart / UNIT;
                int  lLength     = (int)((tStop - tStart) / UNIT);
                int  iBufferSize = pSample.GetSize();

                Debug.Write(string.Format(" pos: {0} len: {1} size: {2}", llPos, lLength, iBufferSize));

                // Make sure the buffer they passed it big enough to hold
                // all the data they requested
                if (iBufferSize >= lLength)
                {
                    // This is where we store the data
                    hr = pSample.GetPointer(out pBuffer);
                }
                else
                {
                    hr = E_BufferTooSmall;
                }

                if (hr >= 0)
                {
                    int iBytesRead;

                    // Read the data into the buffer
                    hr = SyncReadInternal(llPos, lLength, pBuffer, out iBytesRead);
                    if (hr >= 0)
                    {
                        // How much of the buffer we used.
                        pSample.SetActualDataLength(iBytesRead);
                        Debug.Write(string.Format(" read: {0}", iBytesRead));
                    }
                }
            }

            return(hr);
        }
        public static void CopySample(IMediaSample src, IMediaSample dest, bool copySamples)
        {
            var sourceSize = src.GetActualDataLength();

            if (copySamples)
            {
                IntPtr sourceBuffer;
                src.GetPointer(out sourceBuffer);

                IntPtr destBuffer;
                dest.GetPointer(out destBuffer);

                CopyMemory(destBuffer, sourceBuffer, sourceSize);
            }

            // Copy the sample times
            long start, end;

            if (src.GetTime(out start, out end) == S_OK)
            {
                dest.SetTime(start, end);
            }

            if (src.GetMediaTime(out start, out end) == S_OK)
            {
                dest.SetMediaTime(start, end);
            }

            // Copy the media type
            AMMediaType mediaType;
            var         changed = src.GetMediaType(out mediaType) == 0;

            if (changed)
            {
                dest.SetMediaType(mediaType);
                DsUtils.FreeAMMediaType(mediaType);
            }

            dest.SetSyncPoint(src.IsSyncPoint() == S_OK);
            dest.SetPreroll(src.IsPreroll() == S_OK);
            dest.SetDiscontinuity(src.IsDiscontinuity() == S_OK);

            // Copy the actual data length
            dest.SetActualDataLength(sourceSize);
        }
Exemple #5
0
        void TestLength()
        {
            int hr;
            int iSize, iLen, iLen2;

            iSize = m_ims.GetSize();
            iLen  = m_ims.GetActualDataLength();

            // Since we aren't using compression, size and datalen should
            // be the same.
            Debug.Assert(iSize == iLen, "GetSize, GetActualDatalen");

            // Change the datalen
            hr = m_ims.SetActualDataLength(iLen - 1);
            Marshal.ThrowExceptionForHR(hr);

            // Make sure the change, changed
            iLen2 = m_ims.GetActualDataLength();
            Debug.Assert(iLen - 1 == iLen2, "Get/Set ActualDataLen");
        }
Exemple #6
0
        /// <summary>
        /// This routine populates the MediaSample.
        /// </summary>
        /// <param name="pSample">Pointer to a sample</param>
        /// <returns>0 = success, 1 = end of stream, negative values for errors</returns>
        public int MediaSampleCB(IMediaSample pSample)
        {
            int hr;

            try
            {
                IntPtr pData;

                hr = pSample.GetPointer(out pData);
                if (hr >= 0)
                {
                    hr = pSample.SetSyncPoint(true);
                    if (hr >= 0)
                    {
                        var len = Source.Read(pData, Constants.DShowNumberOfPacketsTS);
                        if (len != 0)
                        {
                            hr = 0;
                            pSample.SetActualDataLength(len);
                        }
                        else
                        {
                            //hr = 1;
                            hr = 0;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.ErrorException("Error in Sample handler.", ex);
                hr = -1;
            }
            finally
            {
                Marshal.ReleaseComObject(pSample);
            }

            return(hr);
        }
Exemple #7
0
        public static void CopySample(IMediaSample src, IMediaSample dest, bool copySamples)
        {
            var sourceSize = src.GetActualDataLength();

            if (copySamples)
            {
                IntPtr sourceBuffer;
                src.GetPointer(out sourceBuffer);

                IntPtr destBuffer;
                dest.GetPointer(out destBuffer);

                CopyMemory(destBuffer, sourceBuffer, sourceSize);
            }

            // Copy the sample times
            long start, end;

            if (src.GetTime(out start, out end) == S_OK)
            {
                dest.SetTime(start, end);
            }

            if (src.GetMediaTime(out start, out end) == S_OK)
            {
                dest.SetMediaTime(start, end);
            }

            // Copy the media type
            AMMediaType mediaType;
            src.GetMediaType(out mediaType);
            dest.SetMediaType(mediaType);
            DsUtils.FreeAMMediaType(mediaType);

            dest.SetSyncPoint(src.IsSyncPoint() == S_OK);
            dest.SetPreroll(src.IsPreroll() == S_OK);
            dest.SetDiscontinuity(src.IsDiscontinuity() == S_OK);

            // Copy the actual data length
            dest.SetActualDataLength(sourceSize);
        }
        /// <summary>
        /// The callback from the GSSF to populate the sample.  This class isn't intended
        /// to be overridden.  Child classes should instead implement PopulateSample,
        /// which this method calls.
        /// </summary>
        /// <param name="pSample">The sample to populate</param>
        /// <returns>HRESULT</returns>
        public int SampleCallback(IMediaSample pSample)
        {
            int hr;
            IntPtr pData;

            try
            {
                // Get the buffer into which we will copy the data
                hr = pSample.GetPointer(out pData);
                if (hr >= 0)
                {
                    // Find out the amount of space in the buffer
                    int cbData = pSample.GetSize();

                    lock (this)
                    {
                        hr = SetTimeStamps(pSample);
                        if (hr >= 0)
                        {
                            int iRead;

                            // Populate the sample
                            hr = PopulateSample(pData, cbData, out iRead);
                            if (hr >= 0)
                            {
                                if (hr == S_Ok) // 1 == End of stream
                                {
                                    // increment the frame number for next time
                                    m_iFrameNumber++;
                                }
                                else
                                {
                                    m_iFrameNumber = 0;
                                }

                                pSample.SetActualDataLength(iRead);
                            }
                        }
                    }
                }
            }
            finally
            {
                // Release our pointer the the media sample.  THIS IS ESSENTIAL!  If
                // you don't do this, the graph will stop after about 2 samples.
                Marshal.ReleaseComObject(pSample);
            }

            return hr;
        }
        /// <summary>
        /// Called by the GenericSampleSourceFilter.  This routine populates the MediaSample.
        /// </summary>
        /// <param name="pSample">Pointer to a sample</param>
        /// <returns>0 = success, 1 = end of stream, negative values for errors</returns>
        public virtual int SampleCallback(IMediaSample pSample)
        {
            int hr;
            IntPtr pData;

            try
            {
                // Get the buffer into which we will copy the data
                hr = pSample.GetPointer(out pData);
                if (hr >= 0)
                {
                    // Set TRUE on every sample for uncompressed frames
                    hr = pSample.SetSyncPoint(true);
                    if (hr >= 0)
                    {
                        // Find out the amount of space in the buffer
                        int cbData = pSample.GetSize();

                        hr = SetTimeStamps(pSample);
                        if (hr >= 0)
                        {
                            int iRead;

                            // Get copy the data into the sample
                            hr = GetImage(m_iFrameNumber, pData, cbData, out iRead);
                            if (hr == 0) // 1 == End of stream
                            {
                                pSample.SetActualDataLength(iRead);

                                // increment the frame number for next time
                                m_iFrameNumber++;
                            }
                        }
                    }
                }
            }
            finally
            {
                // Release our pointer the the media sample.  THIS IS ESSENTIAL!  If
                // you don't do this, the graph will stop after about 2 samples.
                Marshal.ReleaseComObject(pSample);
            }

            return hr;
        }
        /// <summary>
        /// This routine populates the MediaSample.
        /// </summary>
        /// <param name="pSample">Pointer to a sample</param>
        /// <returns>0 = success, 1 = end of stream, negative values for errors</returns>
        public int MediaSampleCB(IMediaSample pSample)
        {
            int hr;

            try
            {
                IntPtr pData;

                hr = pSample.GetPointer(out pData);
                if (hr >= 0)
                {
                    hr = pSample.SetSyncPoint(true);
                    if (hr >= 0)
                    {
                        var len = Source.Read(pData, Constants.DShowNumberOfPacketsTS);
                        if (len != 0)
                        {
                            hr = 0;
                            pSample.SetActualDataLength(len);
                        }
                        else
                        {
                            //hr = 1;
                            hr = 0;
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                Log.ErrorException("Error in Sample handler.", ex);
                hr = -1;
            }
            finally
            {
                Marshal.ReleaseComObject(pSample);
            }

            return hr;
        }