Esempio n. 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);
        }
Esempio n. 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);
        }