Example #1
0
        public static Config Default()
        {
            var config = new Config();


            var screenCaptureProperties = new ScreenCaptureProperties
            {
                CaptureMouse  = true,
                AspectRatio   = true,
                CaptureType   = VideoCaptureType.DXGIDeskDupl,
                UseHardware   = true,
                Fps           = 60,//30,
                ShowDebugInfo = false,
            };

            config.SelectAreaRectangle     = new Rectangle(0, 0, 640, 480);
            config.ScreenCaptureProperties = screenCaptureProperties;


            config.Session = StreamSession.Default();

            return(config);
        }
Example #2
0
        private StreamSession CreateSession()
        {
            var session = StreamSession.Default();

            var transport = TransportMode.Udp;

            if (PropertyNetwork.UnicastProtocol == ProtocolKind.TCP)
            {
                transport = TransportMode.Tcp;
            }


            session.StreamName       = Name;
            session.NetworkIpAddress = PropertyNetwork.Network ?? "0.0.0.0";

            session.CommunicationPort = PropertyNetwork.Port;

            if (PropertyNetwork.Port > 0)
            {
                session.CommunicationPort = PropertyNetwork.Port;
            }
            else if (PropertyNetwork.CommunicationPort > 0)
            {
                session.CommunicationPort = PropertyNetwork.CommunicationPort;
            }
            else
            {
                session.CommunicationPort = Helpers.NetworkHelper.FindAvailableTcpPort();
            }

            //if(PropertyNetwork.CommunicationPort <=0)
            //{
            //    session.CommunicationPort = PropertyNetwork.Port;
            //}
            //else
            //{
            //    session.CommunicationPort = PropertyNetwork.CommunicationPort;
            //}


            //int communicationPort = PropertyNetwork.Port;
            //if (communicationPort <= 0)
            //{// если порт не задан - ищем свободный начиная с 808

            //    //communicationPort = GetRandomTcpPort();

            //    var freeTcpPorts = MediaToolkit.Utils.NetTools.GetFreePortRange(System.Net.Sockets.ProtocolType.Tcp, 1, 808);
            //    if (freeTcpPorts != null && freeTcpPorts.Count() > 0)
            //    {
            //        communicationPort = freeTcpPorts.FirstOrDefault();
            //    }
            //}

            ////PropertyNetwork.CommunicationPort = communicationPort;

            //session.CommunicationPort = communicationPort;


            session.TransportMode = transport;

            bool isMulticast = !PropertyNetwork.IsUnicast;

            session.IsMulticast = isMulticast;

            if (isMulticast)
            {
                //VAlidate....
                session.MutlicastAddress = PropertyNetwork.MulticastIp;

                session.MutlicastPort1 = PropertyNetwork.MulticasVideoPort;
                session.MutlicastPort2 = PropertyNetwork.MulticasAudioPort;
            }

            var videoSettings = session.VideoSettings;

            var encoderResolution = new Size(PropertyVideo.ResolutionWidth, PropertyVideo.ResolutionHeight);

            videoSettings.StreamFlags &= ~VideoStreamFlags.UseEncoderResoulutionFromSource;
            if (AdvancedSettings.UseResolutionFromCaptureSource)
            {
                videoSettings.StreamFlags |= VideoStreamFlags.UseEncoderResoulutionFromSource;
            }
            else
            {
                encoderResolution = new Size(AdvancedSettings.Width, AdvancedSettings.Height);
            }


            var videoEncoderSettings = videoSettings.EncoderSettings;

            videoEncoderSettings.EncoderId  = AdvancedSettings.EncoderId;
            videoEncoderSettings.Bitrate    = AdvancedSettings.Bitrate;
            videoEncoderSettings.MaxBitrate = AdvancedSettings.MaxBitrate;
            videoEncoderSettings.Width      = encoderResolution.Width;
            videoEncoderSettings.Height     = encoderResolution.Height;


            videoEncoderSettings.FrameRate  = new MediaRatio(AdvancedSettings.Fps, 1);
            videoEncoderSettings.Profile    = AdvancedSettings.H264Profile;
            videoEncoderSettings.LowLatency = AdvancedSettings.LowLatency;

            var captureRegion = PropertyVideo.VideoRect;
            var captureType   = PropertyVideo.CaptType;
            //var captureFps = PropertyVideo.CaptFps;
            var captureFps  = AdvancedSettings.Fps;
            var useHardware = PropertyVideo.CaptUseHardware;

            var screenCaptureProperties = new ScreenCaptureProperties
            {
                CaptureMouse = PropertyVideo.CaptureMouse,

                CaptureType     = captureType, // VideoCaptureType.DXGIDeskDupl,
                UseHardware     = useHardware,
                Fps             = captureFps,
                ShowDebugInfo   = false,
                ShowDebugBorder = PropertyVideo.ShowCaptureBorder,
                AspectRatio     = AdvancedSettings.KeepAspectRatio,
            };

            VideoCaptureDevice captureDevice = null;

            //var videoSourceItem = PropertyVideo.Display;
            if (PropertyVideo.IsUvcDevice)
            {
                captureDevice = new UvcDevice
                {
                    Name       = PropertyVideo.DeviceName,
                    Resolution = captureRegion.Size,
                    DeviceId   = PropertyVideo.DeviceId,
                };
            }
            else
            {
                captureDevice = new ScreenCaptureDevice
                {
                    CaptureRegion = captureRegion,
                    DisplayRegion = captureRegion,
                    Name          = PropertyVideo.DeviceName,

                    Resolution = captureRegion.Size,
                    Properties = screenCaptureProperties,
                    DeviceId   = PropertyVideo.DeviceId,
                };
            }

            if (PropertyAudio.IsEnabled)
            {
                var deviceId = PropertyAudio.DeviceId;

                var audioDevice = MediaToolkit.AudioTool.GetAudioCaptureDevices().FirstOrDefault(d => d.DeviceId == deviceId);
                if (audioDevice != null)
                {
                    session.AudioSettings.Enabled = true;
                    audioDevice.Properties        = new WasapiCaptureProperties();

                    session.AudioSettings.CaptureDevice = audioDevice;
                }
            }
            else
            {
                session.AudioSettings.Enabled = false;
            }

            logger.Info("CaptureDevice: " + captureRegion);

            session.VideoSettings.CaptureDevice = captureDevice;

            return(session);
        }
Example #3
0
        public void Setup(object pars)//ScreenCaptureParams captureParams)
        {
            logger.Debug("ScreenSource::Setup()");

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

            syncEvent = new AutoResetEvent(false);

            VideoCaptureDevice captureParams = pars as VideoCaptureDevice;

            if (captureParams == null)
            {
                throw new ArgumentException();
            }
            var srcRect  = Rectangle.Empty;
            var destSize = Size.Empty;
            var hwnd     = IntPtr.Zero;

            if (captureParams.CaptureMode == CaptureMode.Screen)
            {
                var screenCaptParams = (ScreenCaptureDevice)pars;
                this.CaptureProps = screenCaptParams.Properties;

                srcRect = screenCaptParams.CaptureRegion;

                var srcLocation = srcRect.Location;
                var srcSize     = GraphicTools.DecreaseToEven(srcRect.Size);
                srcRect = new Rectangle(srcLocation, srcSize);

                //srcRect = new Rectangle(x, y, width, height);
                if (screenCaptParams.CaptureRegion != srcRect)
                {
                    screenCaptParams.CaptureRegion = srcRect;
                }

                destSize = captureParams.Resolution;

                if (destSize.IsEmpty)
                {
                    destSize = new Size(srcRect.Width, srcRect.Height);
                }
            }
            else if (captureParams.CaptureMode == CaptureMode.AppWindow)
            {
                var windowCaptParams = (WindowCaptureDevice)pars;
                this.CaptureProps = windowCaptParams.Properties;

                srcRect = windowCaptParams.ClientRect;

                var srcLocation = srcRect.Location;
                var srcSize     = GraphicTools.DecreaseToEven(srcRect.Size);
                srcRect = new Rectangle(srcLocation, srcSize);

                destSize = captureParams.Resolution;

                if (destSize.IsEmpty)
                {
                    destSize = new Size(srcRect.Width, srcRect.Height);
                }

                hwnd = windowCaptParams.hWnd;
            }



            try
            {
                //var captureDescr = captureParams.CaptureDescription;

                var captArgs = new object[]
                {
                    hwnd,
                };

                screenCapture = ScreenCapture.Create(CaptureProps.CaptureType, captArgs);
                screenCapture.CaptureMouse = CaptureProps.CaptureMouse;
                screenCapture.AspectRatio  = CaptureProps.AspectRatio;

                var d3d11Capture = screenCapture as ITexture2DSource;
                if (d3d11Capture != null)
                {
                    d3d11Capture.UseHwContext = CaptureProps.UseHardware;
                    this.hwContext            = d3d11Capture;
                    this.AdapterId            = d3d11Capture.AdapterId;
                }

                screenCapture.Init(srcRect, destSize);
                //screenCapture.Init(srcRect);


                this.SharedBitmap = screenCapture.VideoBuffer;

                deviceReady = true;

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

                errorCode = (int)SharedTypes.ErrorCode.NotInitialized;

                CleanUp();

                state = CaptureState.Closed;
                throw;
            }
        }
        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;
            }
        }