// Constructor
 public Grabber(CaptureDevice parent)
 {
     this.parent = parent;
 }
        private void startBtn_Click(object sender, RoutedEventArgs e)
        {
            started = !started;
            if (!simulated)
            {
                if (captureDevicesLst.SelectedIndex == -1)
                {
                    started = false;
                    return;
                }
            }

            if (started)
            {
                int portNo = Int32.Parse(portBox.Text);

                server         = new WebServer(portNo);
                testBlock.Text = "http://localhost:" + portNo.ToString() + "/";



                Int32.TryParse(fpsBox.Text, out fps);
                int delay = 1000 / fps;
                startBtn.Content = "Stop streaming";
                if (simulated)
                {
                    List <byte[]> pics = new List <byte[]>();
                    foreach (var file in Directory.EnumerateFiles("SimulatedCamPics"))
                    {
                        if (file.ToLower().Contains(".jpg") || file.ToLower().Contains(".png"))
                        {
                            pics.Add(File.ReadAllBytes(file));
                        }
                    }

                    Log("simulated: readed " + pics.Count.ToString() + " image(s)");
                    simTim = new Timer(delay);
                    int i = 0;
                    simTim.Elapsed += (s, ev) =>
                    {
                        simTim.Stop();
                        DateTime start = DateTime.Now;
                        server.SendImg(pics[i % pics.Count]);
                        TimeSpan finish = DateTime.Now - start;
                        Log("simulated send finished: " + finish.TotalMilliseconds.ToString() + "ms");
                        i++;
                        if (i % pics.Count == 0)
                        {
                            i = 0;
                        }
                        simTim.Start();
                    };
                    simTim.Start();
                }
                else
                {
                    cd             = new CaptureDevice();
                    cd.VideoSource = captureDevices[captureDevicesLst.SelectedIndex].MonikerString;
                    DateTime lastFrame = DateTime.Now;
                    bool     doResize  = (bool)doResizeCheck.IsChecked;
                    cd.NewFrame += (s, evt) =>
                    {
                        //send jpg to all client...
                        if ((DateTime.Now - lastFrame).TotalMilliseconds >= delay)
                        {
                            using (MemoryStream ms = new MemoryStream())
                            {
                                DateTime firstStart = DateTime.Now;
                                DateTime start      = DateTime.Now;
                                Bitmap   resized;
                                TimeSpan finish;
                                if (doResize)
                                {
                                    AForge.Imaging.Filters.Resize resizeFilter = new AForge.Imaging.Filters.Resize(1280, 1024);
                                    resized = resizeFilter.Apply(evt.Bitmap);
                                    finish  = DateTime.Now - start;
                                    Log("resize finished: " + finish.TotalMilliseconds.ToString() + "ms");
                                }
                                else
                                {
                                    resized = evt.Bitmap;
                                }

                                start = DateTime.Now;
                                resized.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                                finish = DateTime.Now - start;
                                Log("saveToStream finished: " + finish.TotalMilliseconds.ToString() + "ms");

                                start = DateTime.Now;
                                server.SendImg(ms.ToArray());
                                finish = DateTime.Now - start;
                                Log("send finished: " + finish.TotalMilliseconds.ToString() + "ms");

                                finish    = DateTime.Now - lastFrame;
                                lastFrame = DateTime.Now;
                                Log("cam refresh: " + finish.TotalMilliseconds.ToString() + "ms (FPS: " + cd.FramesReceived.ToString() + ")");

                                finish = DateTime.Now - firstStart;
                                delay  = (int)(1000 / fps) - (int)finish.TotalMilliseconds;
                            }
                            GC.Collect();
                        }
                    };
                    cd.Start();
                }
            }
            else
            {
                startBtn.Content = "Start streaming";
                if (cd != null)
                {
                    cd.Stop();
                }
                if (simTim != null)
                {
                    simTim.Stop();
                }
                server.DisconnectAll();
                server = null;
                GC.Collect();
            }
        }
        private void startBtn_Click(object sender, RoutedEventArgs e)
        {
            started = !started;
            if (!simulated)
            {
                if (captureDevicesLst.SelectedIndex == -1)
                {
                    started = false;
                    return;
                }
            }

            if (started)
            {
                int portNo = Int32.Parse(portBox.Text);

                server = new WebServer(portNo);
                testBlock.Text = "http://localhost:" + portNo.ToString() + "/";

                Int32.TryParse(fpsBox.Text, out fps);
                int delay = 1000 / fps;
                startBtn.Content = "Stop streaming";
                if (simulated)
                {
                    List<byte[]> pics = new List<byte[]>();
                    foreach (var file in Directory.EnumerateFiles("SimulatedCamPics"))
                    {
                        if (file.ToLower().Contains(".jpg") || file.ToLower().Contains(".png"))
                        {
                            pics.Add(File.ReadAllBytes(file));
                        }
                    }

                    Log("simulated: readed " + pics.Count.ToString() + " image(s)");
                    simTim = new Timer(delay);
                    int i = 0;
                    simTim.Elapsed += (s, ev) =>
                    {
                        simTim.Stop();
                        DateTime start = DateTime.Now;
                        server.SendImg(pics[i % pics.Count]);
                        TimeSpan finish = DateTime.Now - start;
                        Log("simulated send finished: " + finish.TotalMilliseconds.ToString() + "ms");
                        i++;
                        if (i % pics.Count == 0)
                            i = 0;
                        simTim.Start();
                    };
                    simTim.Start();
                }
                else
                {
                    cd = new CaptureDevice();
                    cd.VideoSource = captureDevices[captureDevicesLst.SelectedIndex].MonikerString;
                    DateTime lastFrame = DateTime.Now;
                    bool doResize = (bool)doResizeCheck.IsChecked;
                    cd.NewFrame += (s, evt) =>
                    {
                        //send jpg to all client...
                        if ((DateTime.Now - lastFrame).TotalMilliseconds >= delay)
                        {
                            using (MemoryStream ms = new MemoryStream())
                            {
                                DateTime firstStart = DateTime.Now;
                                DateTime start = DateTime.Now;
                                Bitmap resized;
                                TimeSpan finish;
                                if (doResize)
                                {
                                    AForge.Imaging.Filters.Resize resizeFilter = new AForge.Imaging.Filters.Resize(1280, 1024);
                                    resized = resizeFilter.Apply(evt.Bitmap);
                                    finish = DateTime.Now - start;
                                    Log("resize finished: " + finish.TotalMilliseconds.ToString() + "ms");
                                }
                                else
                                    resized = evt.Bitmap;

                                start = DateTime.Now;
                                resized.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                                finish = DateTime.Now - start;
                                Log("saveToStream finished: " + finish.TotalMilliseconds.ToString() + "ms");

                                start = DateTime.Now;
                                server.SendImg(ms.ToArray());
                                finish = DateTime.Now - start;
                                Log("send finished: " + finish.TotalMilliseconds.ToString() + "ms");

                                finish = DateTime.Now - lastFrame;
                                lastFrame = DateTime.Now;
                                Log("cam refresh: " + finish.TotalMilliseconds.ToString() + "ms (FPS: " + cd.FramesReceived.ToString() + ")");

                                finish = DateTime.Now - firstStart;
                                delay = (int)(1000 / fps) - (int)finish.TotalMilliseconds;
                            }
                            GC.Collect();
                        }
                    };
                    cd.Start();
                }
            }
            else
            {
                startBtn.Content = "Start streaming";
                if(cd != null)
                    cd.Stop();
                if (simTim != null)
                    simTim.Stop();
                server.DisconnectAll();
                server = null;
                GC.Collect();
            }
        }
Example #4
0
 // Constructor
 public Grabber(CaptureDevice parent)
 {
     this.parent = parent;
 }