Exemplo n.º 1
0
        void TestTime()
        {
            int  hr;
            long TimeStart1, TimeEnd1;
            long TimeStart2, TimeEnd2;

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

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

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

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

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

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

            // Put it back to where it started
            hr = m_ims.SetTime(new DsLong(TimeStart1), new DsLong(TimeEnd1));
            Marshal.ThrowExceptionForHR(hr);
        }
Exemplo n.º 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);
 }
Exemplo n.º 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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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;
            }
        }
Exemplo n.º 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;
 }
 public int Request(IMediaSample pSample, IntPtr dwUser)
 {
     // queues up a request for a new sample
     Monitor.Enter(this);
     long start, end;
     pSample.GetTime(out start, out end);
     log.InfoFormat("Request ({0}, {1} - {2})", start, end, pSample.GetSize());
     sampleList.Add(pSample);
     samplePtrList.Add(dwUser);
     Monitor.Exit(this);
     return S_OK;
 }