int ISampleGrabberCB.SampleCB(double sampleTime, IMediaSample pSample)
        {
            try
            {
                if (transcoderError != null)
                {
                    return(WinAPI.E_FAIL);
                }

                samplesProcessed += 1;

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

                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
Example #2
0
            public Int32 SampleCB(Double SampleTime, IMediaSample pSample)
            {
                if (pSample == null)
                {
                    return(-1);
                }
                var    len = pSample.GetActualDataLength( );
                IntPtr pbuf;

                if (pSample.GetPointer(out pbuf) == 0 && len > 0)
                {
                    var buf = new Byte [len];
                    Marshal.Copy(pbuf, buf, 0, len);
                    var image = new Bitmap(640, 480);
                    var at    = 0;
                    if (len % 3 != 0)
                    {
                        // image is not bitmap 24bit, what now, burn it to the ground?
                        return(0);
                    }
                    for (var bOff = 0; bOff < len; bOff += 3)
                    {
                        var color = Color.FromArgb(buf [bOff + 2], buf [bOff + 1], buf [bOff]);
                        var x     = (image.Width - 1) - (at % image.Width);
                        var y     = (image.Height - 1) - (at / image.Width % image.Height);
                        image.SetPixel(x, y, color);
                        at++;
                    }
                    lastImage?.Dispose( );
                    lastImage = image;
                }
                Marshal.ReleaseComObject(pSample);
                return(0);
            }
Example #3
0
            public int SampleCB(double SampleTime, IMediaSample pSample)
            {
                if (pSample == null)
                {
                    return(-1);
                }

                if (Trigger.WaitOne(0))
                {
                    int len = pSample.GetActualDataLength();
                    if (len > 0)
                    {
                        IntPtr buf;
                        if (pSample.GetPointer(out buf) == 0)
                        {
                            byte[] buffer = new byte[len];
                            Marshal.Copy(buf, buffer, 0, len);

                            using (var bmp = new Bitmap(Width, Height, Stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, buf))
                            {
                                bmp.RotateFlip(RotateFlipType.Rotate180FlipX);
                                _previewImage = (Bitmap)bmp.Clone();
                                callback?.Invoke();
                                //using (var ms = new MemoryStream())
                                //{
                                //    //bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                                //    bmp.Save("hallo.pnb");
                                //    //byte[] data = ms.ToArray();

                                //    //var uri = new Uri($"");
                                //    //var req = (HttpWebRequest)HttpWebRequest.Create(uri);
                                //    //req.Method = "POST";
                                //    //req.ContentType = "application/octet-stream";
                                //    //req.Headers.Add("Ocp-Apim-Subscription-Key", "");
                                //    //req.ContentLength = data.Length;
                                //    //using (var stm = req.GetRequestStream())
                                //    //    stm.Write(data, 0, data.Length);
                                //    //using (var res = req.GetResponse())
                                //    //using (var stm = res.GetResponseStream())
                                //    //using (var sr = new StreamReader(stm))
                                //    //using (var jr = new JsonTextReader(sr))
                                //    //{
                                //    //    var obj = serializer.Deserialize<ExpandoObject[]>(jr);

                                //    //}
                                //}
                            }
                        }
                    }
                    Trigger.Reset();
                }

                Marshal.ReleaseComObject(pSample);

                return(0);
            }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="SampleTime"></param>
        /// <param name="pSample"></param>
        /// <returns></returns>
        public int SampleCB(double SampleTime, IMediaSample pSample)
        {
            IntPtr pBuffer;

            pSample.GetPointer(out pBuffer);
            int BufferLen = pSample.GetActualDataLength();

            Marshal.ReleaseComObject(pSample);
            return(0);
        }
Example #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");
        }
Example #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;
     sample.GetTime(out m_TimeStart, out m_TimeEnd);
     sample.GetMediaTime(out m_MediaTimeStart, out m_MediaTimeEnd);
     m_Buffer = Marshal.AllocCoTaskMem(m_Size);
 }
Example #7
0
     public int SampleCB(double SampleTime, IMediaSample pSample)
     {
         if (pSample == null) return -1;
         int len = pSample.GetActualDataLength();
         IntPtr pBuf;
         if (pSample.GetPointer(out pBuf) == 0 && len > 0)
         { 
             m_imageBuf = null;
             m_imageBuf = new byte[len];
             Marshal.Copy(pBuf, m_imageBuf, 0, len);
         }
         return 0;
 }
Example #8
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);
        }
Example #9
0
        int ISampleGrabberCB.SampleCB(double SampleTime, IMediaSample pSample)
        {
            // SampleCB was called
            m_Called |= 1;

            // Simple test to see if parms are correct
            Debug.Assert(pSample.GetActualDataLength() > 0, "SampleCB size");

            // Set the completion event so DoTests can return
            m_TestComplete.Set();

            Marshal.ReleaseComObject(pSample);

            return(0);
        }
 public int SampleCB(double SampleTime, IMediaSample pSample)
 {
     //  Console.WriteLine("**********************55555555555555555555555**********************");
     if (pSample == null) return -1;
     int len = pSample.GetActualDataLength();
     IntPtr pbuf;
     if (pSample.GetPointer(out pbuf) == 0 && len > 0)
     {
         byte[] buf = new byte[len];
         Marshal.Copy(pbuf, buf, 0, len);
         for (int i = 0; i < len; i += 2)
             buf[i] = (byte)(255 - buf[i]);
         Marshal.Copy(buf, 0, pbuf, len);
     }
     return 0;
 }
Example #11
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);
        }
Example #12
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;
            }
        }
Example #13
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);
        }
        int ISampleGrabberCB.SampleCB(double SampleTime, IMediaSample pSample)
        {
            int result = 0;

            if (_fSampleCB != null)
            {
                _fSampleCB(SampleTime, pSample);
            }

            IntPtr pBuff = IntPtr.Zero;
            var    size  = pSample.GetActualDataLength();

            pSample.GetPointer(out pBuff);
            if (_fBufferCB != null)
            {
                _fBufferCB(SampleTime, pBuff, size);
            }

            Marshal.FinalReleaseComObject(pSample);
            return(result);
        }
        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
Example #16
0
        public int SampleCB(double SampleTime, IMediaSample pSample)
        {
            //  Console.WriteLine("**********************55555555555555555555555**********************");
            if (pSample == null)
            {
                return(-1);
            }
            int    len = pSample.GetActualDataLength();
            IntPtr pbuf;

            if (pSample.GetPointer(out pbuf) == 0 && len > 0)
            {
                byte[] buf = new byte[len];
                Marshal.Copy(pbuf, buf, 0, len);
                for (int i = 0; i < len; i += 2)
                {
                    buf[i] = (byte)(255 - buf[i]);
                }
                Marshal.Copy(buf, 0, pbuf, len);
            }
            return(0);
        }
Example #17
0
        // Set the timestamps
        override public int SetTimeStamps(IMediaSample pSample)
        {
            int hr;

            int  iRead      = pSample.GetActualDataLength();
            long SampleTime = GetTimeFromBytes(iRead);
            long TimeStart  = m_FSampleTime;

            m_FSampleTime = m_FSampleTime + SampleTime;
            long TimeStop = m_FSampleTime;

            hr = pSample.SetTime(TimeStart, TimeStop);

            if (hr >= 0)
            {
                TimeStart    = m_FMediaTime;
                m_FMediaTime = m_FMediaTime + SampleTime;
                TimeStop     = m_FMediaTime;
                hr           = pSample.SetMediaTime(TimeStart, TimeStop);
                DsError.ThrowExceptionForHR(hr);
            }

            return(hr);
        }
Example #18
0
        // Set the timestamps
        public override int SetTimeStamps(IMediaSample pSample)
        {
            int hr;

            int iRead = pSample.GetActualDataLength();
            long SampleTime = GetTimeFromBytes(iRead);
            long TimeStart = m_FSampleTime;
            m_FSampleTime = m_FSampleTime + SampleTime;
            long TimeStop = m_FSampleTime;
            hr = pSample.SetTime(TimeStart, TimeStop);

            if (hr >= 0)
            {
                TimeStart = m_FMediaTime;
                m_FMediaTime = m_FMediaTime + SampleTime;
                TimeStop = m_FMediaTime;
                hr = pSample.SetMediaTime(TimeStart, TimeStop);
                DsError.ThrowExceptionForHR(hr);
            }

            return hr;
        }
        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