private void SetupVideoSource(VideoStreamSettings settings)
        {
            logger.Debug("SetupVideoSource(...)");

            try
            {
                var captureDescr = settings.CaptureDescription;
                captureDescr.Resolution = videoSettings.EncodingParams.Resolution;

                if (captureDescr.CaptureMode == CaptureMode.CaptDevice)
                {
                    videoSource = new VideoCaptureSource();
                    videoSource.Setup(captureDescr);
                }
                else if (captureDescr.CaptureMode == CaptureMode.Screen)
                {
                    videoSource = new ScreenSource();
                    videoSource.Setup(captureDescr);
                }

                videoSource.CaptureStarted += VideoSource_CaptureStarted;
                videoSource.CaptureStopped += VideoSource_CaptureStopped;
            }
            catch (Exception ex)
            {
                logger.Error(ex);

                if (videoSource != null)
                {
                    videoSource.CaptureStarted -= VideoSource_CaptureStarted;
                    videoSource.CaptureStopped -= VideoSource_CaptureStopped;

                    videoSource.Close();
                    videoSource = null;
                }

                throw;
            }
        }
Beispiel #2
0
        private void previewButton_Click(object sender, EventArgs e)
        {
            logger.Debug("previewButton_Click(...)");

            if (VideoSettings == null)
            {
                return;
            }

            try
            {
                var captureDevice = this.VideoSettings.CaptureDevice;

                if (videoSource == null)
                {
                    if (captureDevice.CaptureMode == CaptureMode.UvcDevice)
                    {
                        videoSource = new VideoCaptureSource();
                        videoSource.Setup(captureDevice);
                    }
                    else if (captureDevice.CaptureMode == CaptureMode.Screen)
                    {
                        videoSource = new ScreenSource();
                        videoSource.Setup(captureDevice);
                    }
                    else if (captureDevice.CaptureMode == CaptureMode.AppWindow)
                    {
                        videoSource = new ScreenSource();
                        videoSource.Setup(captureDevice);
                    }

                    videoSource.CaptureStarted += VideoSource_CaptureStarted;
                    videoSource.CaptureStopped += VideoSource_CaptureStopped;

                    videoSource.BufferUpdated += VideoSource_BufferUpdated;
                }


                if (videoSource.State == CaptureState.Capturing)
                {
                    videoSource.Stop();
                }
                else
                {
                    videoSource.Start();
                }

                this.Cursor  = Cursors.WaitCursor;
                this.Enabled = false;
            }
            catch (Exception ex)
            {
                this.Cursor  = Cursors.Default;
                this.Enabled = true;

                MessageBox.Show(ex.Message);

                if (videoSource != null)
                {
                    videoSource.BufferUpdated  -= VideoSource_BufferUpdated;
                    videoSource.CaptureStarted -= VideoSource_CaptureStarted;
                    videoSource.CaptureStopped -= VideoSource_CaptureStopped;
                    videoSource.Close(true);
                    videoSource = null;
                }
            }
        }
        // private Screen currentScreen = null;


        private void httpStartButton_Click(object sender, EventArgs e)
        {
            //currentScreen = HttpGetCurrentScreen();

            var srcRect = HttpGetCurrentScreen(); //currentScreen.Bounds;
            //var srcRect = currentScreen.Bounds;

            var _destWidth  = (int)httpDestWidthNumeric.Value;
            var _destHeight = (int)httpDestHeightNumeric.Value;

            var destSize = new Size(_destWidth, _destHeight);

            var ratio      = srcRect.Width / (double)srcRect.Height;
            int destWidth  = destSize.Width;
            int destHeight = (int)(destWidth / ratio);

            if (ratio < 1)
            {
                destHeight = destSize.Height;
                destWidth  = (int)(destHeight * ratio);
            }

            destSize = new Size(destWidth, destHeight);


            var fps = httpFpsNumeric.Value;

            var addr = httpAddrTextBox.Text;
            var port = (int)httpPortNumeric.Value;

            VideoCaptureType captureType = (VideoCaptureType)captureTypesComboBox.SelectedItem;

            httpScreenSource = new ScreenSource();
            ScreenCaptureDevice captureParams = new ScreenCaptureDevice
            {
                CaptureRegion = srcRect,
                Resolution    = destSize,
            };

            captureParams.Properties.CaptureType = captureType;//CaptureType.DXGIDeskDupl,

            captureParams.Properties.Fps          = (int)fps;
            captureParams.Properties.CaptureMouse = true;
            captureParams.Properties.AspectRatio  = true;
            captureParams.Properties.UseHardware  = false;

            if (captureType == VideoCaptureType.GDI || captureType == VideoCaptureType.GDIPlus)
            {// масштабируем на энкодере
                captureParams.Resolution = new Size(srcRect.Width, srcRect.Height);
            }

            httpScreenSource.Setup(captureParams);


            httpStreamer = new VideoHttpStreamer(httpScreenSource);

            NetworkSettings networkParams = new NetworkSettings
            {
                RemoteAddr = addr,
                RemotePort = port,
            };


            VideoEncoderSettings encodingParams = new VideoEncoderSettings
            {
                Width  = destSize.Width,  // options.Width,
                Height = destSize.Height, // options.Height,
                //Resolution = destSize,
                FrameRate = new MediaRatio((int)fps, 1),
                EncoderId = "mjpeg",
            };

            httpStreamer.Setup(encodingParams, networkParams);


            httpStreamer.Start();
            httpScreenSource.Start();


            statisticForm.Location = srcRect.Location;
            // statisticForm.Start();
        }
        public void Setup(HttpScreenStreamerArgs args)
        {
            logger.Debug("HttpScreenStreamer::Setup() " + args.ToString());

            if (state != MediaState.Closed)
            {
                throw new InvalidOperationException("Invalid state " + State);
            }

            errorCode = 0;

            //var srcRect = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
            //var destSize = new Size(1920, 1080);

            var srcRect  = args.CaptureRegion;
            var destSize = args.Resolution;

            var ratio      = srcRect.Width / (double)srcRect.Height;
            int destWidth  = destSize.Width;
            int destHeight = (int)(destWidth / ratio);

            if (ratio < 1)
            {
                destHeight = destSize.Height;
                destWidth  = (int)(destHeight * ratio);
            }

            destSize = new Size(destWidth, destHeight);
            var captureType = args.CaptureTypes;

            var captureProp = new ScreenCaptureProperties
            {
                CaptureType  = captureType,
                Fps          = (int)args.Fps,
                CaptureMouse = args.CaptureMouse,
                AspectRatio  = true,
                UseHardware  = false,
            };

            ScreenCaptureDevice captureParams = new ScreenCaptureDevice
            {
                CaptureRegion = srcRect,
                Resolution    = destSize,

                Properties = captureProp,

                //CaptureType = captureType,
                //Fps = (int)args.Fps,
                //CaptureMouse = args.CaptureMouse,
                //AspectRatio = true,
                //UseHardware = false,
            };


            if (captureType == VideoCaptureType.GDI ||
                captureType == VideoCaptureType.GDILayered ||
                captureType == VideoCaptureType.GDIPlus ||
                captureType == VideoCaptureType.Datapath)
            {// масштабируем на энкодере
                captureParams.Resolution = new Size(srcRect.Width, srcRect.Height);
            }

            VideoEncoderSettings encodingParams = new VideoEncoderSettings
            {
                EncoderFormat = VideoCodingFormat.JPEG,
                //Resolution = destSize,
                Width     = destSize.Width,
                Height    = destSize.Height,
                FrameRate = new MediaRatio(captureParams.Properties.Fps, 1),
                EncoderId = "mjpeg",
            };

            NetworkSettings networkParams = new NetworkSettings
            {
                RemoteAddr = args.Addres,
                RemotePort = args.Port,
            };

            try
            {
                httpScreenSource = new ScreenSource();
                httpScreenSource.Setup(captureParams);
                httpScreenSource.CaptureStopped += HttpScreenSource_CaptureStopped;

                httpStreamer = new VideoHttpStreamer(httpScreenSource);
                httpStreamer.Setup(encodingParams, networkParams);
                httpStreamer.StreamerStopped += HttpStreamer_StreamerStopped;

                state = MediaState.Initialized;
            }
            catch (Exception ex)
            {
                logger.Error(ex);

                errorCode = 100503;

                Close();
                throw;
            }
        }