예제 #1
0
        void TestMediaTime()
        {
            int  hr;
            long TimeStart1, TimeEnd1;
            long TimeStart2, TimeEnd2;

            // Read the value
            hr = m_ims.GetMediaTime(out TimeStart1, out TimeEnd1);
            Marshal.ThrowExceptionForHR(hr);

            // Change the value
            hr = m_ims.SetMediaTime(new DsLong(TimeStart1 + 1), new DsLong(TimeEnd1 - 1));
            Marshal.ThrowExceptionForHR(hr);

            // Re-read the value
            hr = m_ims.GetMediaTime(out TimeStart2, out TimeEnd2);
            Marshal.ThrowExceptionForHR(hr);

            // Check to see if the change took
            Debug.Assert(TimeStart1 + 1 == TimeStart2, "Get/Set MediaTime start");
            Debug.Assert(TimeEnd1 - 1 == TimeEnd2, "Get/Set MediaTime End");

            // Try using nulls
            hr = m_ims.SetMediaTime(null, null);
            Marshal.ThrowExceptionForHR(hr);

            // Read the time, should fail (because of the nulls)
            hr = m_ims.GetMediaTime(out TimeStart1, out TimeEnd1);
            Debug.Assert(hr == DsResults.E_MediaTimeNotSet, "Get/Set MediaTime null");

            // Put it back to where it started
            hr = m_ims.SetMediaTime(new DsLong(TimeStart1), new DsLong(TimeEnd1));
            Marshal.ThrowExceptionForHR(hr);
        }
예제 #2
0
 public MediaSample(IMediaSample sample)
 {
     m_Size             = sample.GetSize();
     m_ActualDataLength = sample.GetActualDataLength();
     m_IsSyncPoint      = sample.IsSyncPoint() == 0;
     m_IsPreroll        = sample.IsPreroll() == 0;
     m_IsDiscontinuity  = sample.IsDiscontinuity() == 0;
     sample.GetTime(out m_TimeStart, out m_TimeEnd);
     sample.GetMediaTime(out m_MediaTimeStart, out m_MediaTimeEnd);
     m_Buffer = Marshal.AllocCoTaskMem(m_Size);
 }
예제 #3
0
        public int SampleCB(double SampleTime, IMediaSample pSample)
        {
            DateTime dt = DateTime.Now;

            lock (this)
            {
                sb.AppendFormat("{0:T}.{1:D3}: ", dt, dt.Millisecond);
                sb.AppendFormat("SampleTime={0:G4}, ", SampleTime);
                if (pSample != null)
                {
                    long start = 0, end = 0;
                    pSample.GetTime(out start, out end);
                    sb.AppendFormat("Time(start={0}, end={1}), ", start, end);
                    pSample.GetMediaTime(out start, out end);
                    sb.AppendFormat("MediaTime(start={0}, end={1}), ", start, end);
                    int len = pSample.GetActualDataLength();
                    sb.AppendFormat("data length={0}, ", len);
                    bool syncpoint = pSample.IsSyncPoint() == 0;
                    sb.AppendFormat("keyframe={0}", syncpoint);
                    if (pSample.IsDiscontinuity() == 0)
                    {
                        sb.Append(", Discontinuity");
                    }
                    if (pSample.IsPreroll() == 0)
                    {
                        sb.Append(", Preroll");
                    }
                    int    n = Math.Min(len, 8);
                    IntPtr pbuf;
                    if (pSample.GetPointer(out pbuf) == 0)
                    {
                        byte[] buf = new byte[n];
                        Marshal.Copy(pbuf, buf, 0, n);
                        sb.Append(", Data=");
                        for (int i = 0; i < n; i++)
                        {
                            sb.AppendFormat("{0:X2}", buf[i]);
                        }
                        sb.Append("...");
                    }
                }
                else
                {
                    sb.Append("pSample==NULL!");
                }
                sb.Append(Environment.NewLine);
            }
            Marshal.ReleaseComObject(pSample);
            return(0);
        }
예제 #4
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;
            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);
        }
예제 #5
0
        public MediaSample(IMediaSample sample)
        {
            m_Size             = sample.GetSize();
            m_ActualDataLength = sample.GetActualDataLength();
            m_IsSyncPoint      = sample.IsSyncPoint() == 0;
            m_IsPreroll        = sample.IsPreroll() == 0;
            m_IsDiscontinuity  = sample.IsDiscontinuity() == 0;
            m_HasTime          = sample.GetTime(out m_TimeStart, out m_TimeEnd) == 0;
            m_HasMediaTime     = sample.GetMediaTime(out m_MediaTimeStart, out m_MediaTimeEnd) == 0;
            m_Buffer           = Marshal.AllocCoTaskMem(m_Size);
            // Copy the media type
            AMMediaType mediaType;

            if (sample.GetMediaType(out mediaType) == 0)
            {
                m_MediaType = mediaType;
            }
        }
예제 #6
0
        public int SampleCB(double sampleTime, IMediaSample pSample)
        {
            try
            {
                if (!bProcess)
                {
                    return(WinAPI.E_FAIL);
                }

                // internal stats
                ++sampleIndex;
                long tStart, tEnd;

                pSample.GetMediaTime(out tStart, out tEnd);
                Debug.Assert(tStart < tEnd);
                Debug.Assert(tStart > lastMediaTime);
                sampleProcessed += tEnd - tStart;
                sampleDropped   += tStart - lastMediaTime - 1;
                lastMediaTime    = tEnd - 1;

                int    dataLen = pSample.GetActualDataLength();
                IntPtr bufPtr;
                int    hr = pSample.GetPointer(out bufPtr);
                Debug.Assert(0 == hr);

                bool processed = ProcessSample(bufPtr, dataLen, sampleTime);

                return(processed ? WinAPI.S_OK : WinAPI.E_FAIL);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
            finally
            {
                Marshal.ReleaseComObject(pSample);
            }

            return(WinAPI.E_FAIL);
        } // end of SampleCB
예제 #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);
        }
        public int SampleCB(double SampleTime, IMediaSample pSample)
        {
            if (!bProcess)
            {
                lastSampleTime = SampleTime;
                return(WinAPI.E_FAIL);
            }

            // internal stats
            ++sampleIndex;
            long tStart, tEnd;

            pSample.GetMediaTime(out tStart, out tEnd);
            Debug.Assert(tStart < tEnd);
            Debug.Assert(tStart > lastMediaTime);
            sampleProcessed += tEnd - tStart;
            sampleDropped   += tStart - lastMediaTime - 1;
            lastMediaTime    = tEnd - 1;

            int    dataLen = pSample.GetActualDataLength();
            IntPtr bufPtr;
            int    hr = pSample.GetPointer(out bufPtr);

            Debug.Assert(0 == hr);

            // BEGIN TRACE

            int bufSize = pSample.GetSize();

            long timeStart, timeEnd;

            pSample.GetTime(out timeStart, out timeEnd);

            string msg = string.Format(
                "SampleCB ({0}) {1}, sampleTime:{2} datalen:{3} bufsize:{4} mediaTime:{5}-{6} time:{7}-{8}",
                name, sampleIndex, SampleTime, dataLen, bufSize, tStart, tEnd, timeStart, timeEnd);

            Trace.WriteLine(msg);

            if (tStart - lastMediaTime - 1 > 0)
            {
                msg = string.Format("!!! Frame drop: {0}", tStart - lastMediaTime - 1 > 0);
                Trace.WriteLine(msg);
            }

            //END TRACE

            byte[] buf = new byte[dataLen];
            Marshal.Copy(bufPtr, buf, 0, dataLen);

            if (file != null)
            {
                file.Write(buf, 0, dataLen);
            }

            //DBG - simulate encoding error
            //if (sampleIndex > 100)
            //    goto STOP_CAPTURE;

            if (mediaState != null && mediaState.mpeg2Enc != null)
            {
                PrimoSoftware.AVBlocks.Transcoder enc = mediaState.mpeg2Enc;
                MediaSample inputSample = new MediaSample();
                inputSample.Buffer    = new MediaBuffer(buf);
                inputSample.StartTime = Math.Max(SampleTime, 0);
                //TODO: end time

                try
                {
                    bool pushed = false;

                    // transcoder.Push() is not threads safe.
                    // lock (enc){ } ensure that only one thread is calling transcoder.Push()
                    lock (enc)
                    {
                        pushed = enc.Push(StreamNumber, inputSample);
                    }

                    if (pushed)
                    {
                        return(0);
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.WriteLine(ex.ToString());
                }

                Trace.WriteLine("PushSample FAILED");
            }

            //STOP_CAPTURE:

            Trace.WriteLine("SampleCB: Before Post STOP_CAPTURE");
            WinAPI.PostMessage(MainWindow, Util.WM_STOP_CAPTURE, new IntPtr(streamNumber), IntPtr.Zero);
            Trace.WriteLine("SampleCB: After Post STOP_CAPTURE");
            bProcess = false;
            return(WinAPI.E_FAIL);
        } // end of SampleCB
 public int SyncReadAligned(IMediaSample pSample)
 {
     Monitor.Enter(this);
     long start, end;
     long mediaStart, mediaEnd;
     int size;
     int offset;
     int hr;
     int ans = S_OK;
     IntPtr ptr = new IntPtr();
     hr = pSample.GetPointer(out ptr);
     hr = pSample.GetTime(out start, out end);
     hr = pSample.GetMediaTime(out mediaStart, out mediaEnd);
     size = pSample.GetSize();
     int rescale = DS_RESCALE_FACTOR; // (int)((end - start) / size);
     //start += startTime;
     offset = (int)(start / rescale);
     if ((offset + size) > reader.BufferSize)
     {
         Memory.Set(ptr, offset, size);
         log.InfoFormat("SyncReadAligned went off the end ({0}, {1}, {2})", offset, (offset + size), reader.BufferSize);
         size = (int)(reader.BufferSize - offset);
         ans = S_FALSE;
     }
     if ((offset + size) > reader.BufferEnd)
     {
         log.InfoFormat("SyncReadAligned wait for buffer ({0}, {1}, {2})", offset, (offset + size), reader.BufferEnd);
         BufferData(offset + size);
     }
     log.InfoFormat("SyncReadAligned ({0} / {1} ({2}), {3} / {4}) - {5}, {6}", start, mediaStart, offset, end, mediaEnd, size, rescale);
     byte[] buffer = reader.GetBuffer();
     Marshal.Copy(buffer, offset, ptr, size);
     reader.ReleaseBuffer();
     Monitor.Exit(this);
     return ans;
 }