public void run()
        {
            try
            {
                using (CaptureDeviceDirectShow cam = new CaptureDeviceDirectShow())
                {
                    cam.Start();
                    while (running)
                    {
                        IntPtr ip = cam.GetBitMap();
                        Bitmap bm = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, ip);
                        bm.RotateFlip(RotateFlipType.RotateNoneFlipY);

                        lock (bm)
                        {
                            if (OnNewFrame != null)
                            {
                                OnNewFrame.Invoke(bm);
                            }
                            bitmap = bm;
                        }
                    }
                }
            }
            catch (Exception ex) { running = false; }
        }
示例#2
0
        private void OnNewSourceFrame(object sender, NewFrameEventArgs e)
        {
            var frame = frames[frameCount % 2];

            ++frameCount;
            e.Frame.FastCopyTo(frame);

            OnNewFrame?.Invoke(frame);
        }
        private void Tick()
        {
            while (true)
            {
                System.Threading.Thread.Sleep(1000 / 30);

                if (data != null && data.Count > 0)
                {
                    OnNewFrame?.Invoke(this, new PlaybackEventArgs(data[frame].Item1, data[frame].Item2));
                    frame = frame + 1 < data.Count ? frame + 1 : frame = 0;
                }
            }
        }
示例#4
0
        private void OnNewInputFrame(Bitmap frame)
        {
            bool mustSaveFrame = true;

            if (VideoInjector != null)
            {
                VideoInjector.UpdateFrame(frame, out mustSaveFrame);
            }

            if (mustSaveFrame)
            {
                if (!videoFileWriter.IsOpen)
                {
                    int frameRate = (int)Math.Round(videoInput.FramesPerSecond);
                    videoFileWriter.Open(VideoOutputFileName, width, height, frameRate, VideoCodec.MPEG4, 8 * 1024 * 1024);
                }
                videoFileWriter.WriteVideoFrame(frame);
            }

            OnNewFrame?.Invoke(frame);
        }
示例#5
0
        private void samplerProc()
        {
            try
            {
                long currentSampleTime = 0;
                int  streamRef         = 0;
                SourceReaderFlags sourceReaderFlags = 0;

                int fpsStoreLen = 25;
                var fpsStore    = new Queue <long>();

                Stopwatch sw             = new Stopwatch();
                long      lastSampleTime = -1;
                bool      justUnfreezed  = false;
                double    fps            = double.NaN;

                while (!cancelTokenSource.Token.IsCancellationRequested)
                {
                    bool gonnaFreeze        = WantToFreezeFlag;
                    bool skipBufferedFrames = gonnaFreeze || justUnfreezed;

                    lock (startLocker)
                        if (WantToConfigureReader || reader == null || reader.IsDisposed)
                        {
                            newSourceReader();
                            WantToConfigureReader = false;
                            WantToChangeMediaType = true;
                            fpsStore.Clear();
                        }

                    if (WantToChangeMediaType)
                    {
                        setMediaType();
                        WantToChangeMediaType = false;
                        fpsStore.Clear();
                    }

                    if (skipBufferedFrames)
                    {
                        sw.Restart();
                    }
                    Sample sample = reader.ReadSample(SourceReaderIndex.FirstVideoStream, 0, out streamRef, out sourceReaderFlags, out currentSampleTime);
                    if (skipBufferedFrames)
                    {
                        sw.Stop();
                    }

                    if (sample == null && sourceReaderFlags.HasFlag(SourceReaderFlags.StreamTick))
                    {
                        continue;
                    }

                    if (sourceReaderFlags != SourceReaderFlags.None)
                    {
                        if (sample != null)
                        {
                            sample.Dispose();
                        }
                        throw new Exception("Something went really wrong");
                    }

                    if (skipBufferedFrames)
                    {
                        if (sw.ElapsedMilliseconds <= 5)
                        {
                            SharpDX.Utilities.Dispose(ref sample); lastSampleTime = currentSampleTime; continue;
                        }
                    }

                    justUnfreezed = false;

                    if (lastSampleTime != -1)
                    {
                        fpsStore.Enqueue(currentSampleTime - lastSampleTime);
                        if (fpsStore.Count() > fpsStoreLen)
                        {
                            fpsStore.Dequeue();
                        }
                        fps = 10000000.0 / fpsStore.Average();
                    }
                    lastSampleTime = currentSampleTime;

                    if (cancelTokenSource.Token.IsCancellationRequested)
                    {
                        break;
                    }

                    lock (sampleLocker)
                    {
                        if (lastSample == null || lastSample.TotalLength != sample.TotalLength)
                        {
                            var tp    = reader.GetCurrentMediaType(SourceReaderIndex.FirstVideoStream);
                            var fsize = tp.Get(MediaTypeAttributeKeys.FrameSize);
                            tp.Dispose();
                            long w = fsize >> 32;
                            long h = fsize & 0xFFFF;

                            lastSampleFrameH = (int)h;
                            lastSampleFrameW = (int)w;
                        }

                        SharpDX.Utilities.Dispose(ref lastSample);
                        lastSample = sample;
                    }

                    OnNewFrame?.Invoke(this, new NewFrameEventArgs(gonnaFreeze ? double.NaN : Math.Round(fps, 1)));

                    if (!gonnaFreeze)
                    {
                        continue;
                    }

                    WantToFreezeFlag = false;
                    freezedEvent.Set();
                    kickstartEvent.Reset();
                    kickstartEvent.Wait();
                    justUnfreezed = true;
                    fpsStore.Clear();
                }
            }
            catch (SharpDXException ex)
            {
                if ((ex.ResultCode == SharpDX.MediaFoundation.ResultCode.TopoCodecNotFound ||
                     ex.ResultCode == SharpDX.MediaFoundation.ResultCode.InvalidMediaType))
                {
                    String msg = $"Uncompatible MediaType format for current Source Reader configuration.\nSampler stopped\n\n{ex.Message}";
                    MessageBox.Show(msg, "Oops");
                }
                if (ex.ResultCode == SharpDX.MediaFoundation.ResultCode.VideoRecordingDeviceInvalidated)
                {
                    SharpDX.Utilities.Dispose(ref reader);
                    if (settingsForm != null)
                    {
                        settingsForm.BeginInvoke(new Action(() => settingsForm.Close()));
                    }
                    WantToConfigureReader = true;   //  Recreate the device & reader on possible new connection attempt
                }
                throw;
            }
        }
示例#6
0
 public void NewFrame(FrameEntry frameEntry)
 {
     OnNewFrame?.Invoke(this, frameEntry);
 }
示例#7
0
 private void RenderProcessHandler_OnContextCreated(object sender, CfrOnContextCreatedEventArgs e)
 {
     OnNewFrame?.Invoke(e);
 }
示例#8
0
 private void NewFrame()
 {
     this._ballEntered = null;
     Instantiate(this.BowlingBall);
     OnNewFrame.Invoke();
 }
示例#9
0
 private void VideoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
 {
     CurrentBitmap = eventArgs.Frame;
     OnNewFrame?.Invoke(this, null);
 }