Esempio n. 1
0
        /// <summary>
        /// Submits a usage report synchronously.  Should be called from a background thread.
        /// </summary>
        private static void PerfDataReportBackground()
        {
            try
            {
                // Generate record.
                Upload_Record record = GetPerfDataRecord();
                if (record == null)
                {
                    return;
                }

                // Submit record.
                if (saveLocalNextReport)
                {
                    File.WriteAllText("perfdata.json", JsonConvert.SerializeObject(record, Formatting.Indented));
                }

                if (uploadNextReport)
                {
                    string jsonPayload = JsonConvert.SerializeObject(record);
                    string md5         = Hash.GetMD5Hex(jsonPayload);
                    using (WebClient wc = new WebClient())
                    {
                        wc.UploadString("https://biupdatehelper.hopto.org/api/uploadUsageRecord2", jsonPayload + md5);
                    }
                }

                // Save the time so this doesn't happen again for a while.
                Program.settings.Load();
                Program.settings.lastUsageReportAt = TimeUtil.GetTimeInMsSinceEpoch();
                Program.settings.Save();
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                Logger.Debug(ex, "Unable to generate anonymous performance data record.");
            }
            finally
            {
                sem.Release();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates an anonymous usage record.  This method spends 10 seconds measuring CPU usage.  Returns null if any BlueIris.exe processes close while CPU usage is being measured, or if no BlueIris.exe processes were open.
        /// </summary>
        /// <returns></returns>
        public static Upload_Record GetPerfDataRecord()
        {
            Upload_Record record = new Upload_Record();

            BlueIrisConfiguration c = new BlueIrisConfiguration();

            c.Load();

            record.CpuUsage                = c.activeStats.CpuUsage;
            record.BiCpuUsage              = c.activeStats.BiCpuUsage;
            record.CpuThreads              = (short)Environment.ProcessorCount;
            record.BiVersion               = c.activeStats.BiVersion;
            record.BiMemUsageMB            = c.activeStats.BiMemUsageMB;
            record.BiPeakVirtualMemUsageMB = c.activeStats.BiPeakVirtualMemUsageMB;
            record.ConsoleOpen             = c.activeStats.ConsoleOpen;
            record.ConsoleWidth            = c.activeStats.ConsoleWidth;
            record.ConsoleHeight           = c.activeStats.ConsoleHeight;
            record.Secret = Program.settings.secret;
            record.OS     = c.OS;

            if (c.cpu == null)
            {
                record.CpuModel = "Unknown";
                record.CpuMHz   = 0;
            }
            else
            {
                record.CpuModel = c.cpu.GetModel();
                record.CpuMHz   = NumberUtil.ParseInt(c.cpu.maxClockSpeed);
            }

            record.HelperVersion  = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            record.HwAccel        = (byte)c.global.HardwareAcceleration;
            record.ServiceMode    = c.global.ServiceMode;
            record.LivePreviewFPS = (short)c.global.LivePreviewFPS;

            record.MemMB     = c.activeStats.MemMB;
            record.MemFreeMB = c.activeStats.MemFreeMB;

            record.RamGiB        = c.mem.GiB;
            record.RamChannels   = c.mem.Channels;
            record.DimmLocations = c.mem.DimmLocations;
            record.RamMHz        = c.mem.MHz;

            // Get camera info.
            // Get frame rates, current profile (only accessible via BI's web server).
            Dictionary <string, double> fpsMap = new Dictionary <string, double>();

            int  currentProfile = 1;
            bool isAdmin        = false;
            bool gotCamlist     = false;
            bool gotStatus      = false;

            BiServerInfo.Reload();
            //BiUserInfo.Reload();
            if (BiServerInfo.enabled)
            {
                try
                {
                    using (WebClient wc = new WebClient())
                    {
                        UserInfo user    = BiUserInfo.CreateTemporaryUser();
                        string   session = CameraWebInterfaceLinker.GetSecureAuthenticatedSession(wc, out isAdmin, user.name, user.GetDecodedPassword());
                        try
                        {
                            try
                            {
                                string          response        = wc.UploadString(CameraWebInterfaceLinker.GetJsonURL(), "{\"cmd\":\"camlist\",\"session\":\"" + session + "\"}");
                                CamListResponse camListResponse = JsonConvert.DeserializeObject <CamListResponse>(response);
                                if (camListResponse != null && camListResponse.result == "success")
                                {
                                    foreach (CameraListCamera camera in camListResponse.data)
                                    {
                                        if (camera.group == null)
                                        {
                                            fpsMap[camera.optionValue] = camera.FPS;
                                        }
                                    }
                                    gotCamlist = true;
                                }
                            }
                            catch (Exception ex)
                            {
                                Logger.Debug(ex, "Error reading camera list from web server.");
                            }

                            try
                            {
                                string         response       = wc.UploadString(CameraWebInterfaceLinker.GetJsonURL(), "{\"cmd\":\"status\",\"session\":\"" + session + "\"}");
                                StatusResponse statusResponse = JsonConvert.DeserializeObject <StatusResponse>(response);
                                if (statusResponse != null && statusResponse.result == "success")
                                {
                                    currentProfile = statusResponse.data.profile;
                                    gotStatus      = true;
                                    if (currentProfile < 1 || currentProfile > 7)
                                    {
                                        currentProfile = 1;
                                        gotStatus      = false;
                                    }
                                    else
                                    {
                                        record.ProfileConfirmed = true;
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Logger.Debug(ex, "Error reading camera list from web server.");
                            }
                        }
                        finally
                        {
                            wc.UploadString(CameraWebInterfaceLinker.GetJsonURL(), "{\"cmd\":\"logout\",\"session\":\"" + session + "\"}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Debug(ex, "Error dealing with web server.");
                }
            }
            record.webserverState = 0;
            if (gotCamlist && gotStatus)
            {
                record.webserverState = 1;
                if (isAdmin)                 // and any future admin-requiring stuff all worked
                {
                    record.webserverState = 2;
                }
            }


            // Get camera info
            List <Upload_Camera> cameras = new List <Upload_Camera>();

            foreach (Camera camSrc in c.cameras.Values)
            {
                if (!camSrc.enabled)
                {
                    continue;
                }

                Upload_Camera cam = new Upload_Camera();

                if (fpsMap.TryGetValue(camSrc.shortname, out double fps))
                {
                    cam.FPS          = (byte)Math.Round(fps).Clamp(0, 255);
                    cam.FPSConfirmed = true;
                }
                else
                {
                    cam.FPS = (byte)(Math.Round(camSrc.MaxRate).Clamp(0, 255));
                }
                cam.CapType     = (byte)camSrc.CapType;
                cam.Hwaccel     = (byte)camSrc.Hwva;
                cam.LimitDecode = camSrc.LimitDecode;
                cam.Pixels      = camSrc.Pixels;
                if (camSrc.hasSubStream)
                {
                    cam.MainPixels = camSrc.MainPixels;
                }
                cam.Type              = (byte)camSrc.Type;
                cam.MotionDetector    = camSrc.triggerSettings[currentProfile].motionDetectionEnabled;
                cam.RecordTriggerType = (byte)camSrc.recordSettings[currentProfile].triggerType;
                cam.RecordFormat      = (byte)camSrc.recordSettings[currentProfile].recordingFormat;
                cam.DirectToDisk      = camSrc.recordSettings[currentProfile].DirectToDisc;
                cam.VCodec            = camSrc.recordSettings[currentProfile].VCodec;
                cameras.Add(cam);
            }

            record.AllFPSConfirmed = cameras.All(cam => cam.FPSConfirmed);             // Ignored and recalculated by server. This exists here for the sake of local json output.
            record.cameras         = cameras.ToArray();
            record.gpus            = c.gpus.Select(g => new Upload_Gpu()
            {
                Name = g.Name, Version = g.DriverVersion
            }).ToArray();

            record.Total_FPS = record.Total_Megapixels = record.Total_MPPS = 0;
            foreach (Upload_Camera cam in cameras)
            {
                float MP = cam.Pixels / 1000000f;
                record.Total_Megapixels += MP;
                record.Total_FPS        += cam.FPS;
                record.Total_MPPS       += MP * cam.FPS;
            }

            return(record);
        }
        /// <summary>
        /// Creates an anonymous usage record.  This method spends 10 seconds measuring CPU usage.  Returns null if any BlueIris.exe processes close while CPU usage is being measured, or if no BlueIris.exe processes were open.
        /// </summary>
        /// <returns></returns>
        public static Upload_Record GetPerfDataRecord()
        {
            Upload_Record record = new Upload_Record();

            // Begin measuring CPU usage.
            using (PerformanceCounter totalCpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"))
            {
                totalCpuCounter.NextValue();
                Stopwatch sw = new Stopwatch();
                sw.Start();
                List <Process> biProcs = Process.GetProcesses().Where(p => p.ProcessName.ToLower() == "blueiris").ToList();
                if (biProcs.Count < 1)
                {
                    Logger.Info("Unable to generate anonymous performance data record because Blue Iris is not running.");
                    return(null);
                }
                TimeSpan[] startTimes = new TimeSpan[biProcs.Count];
                for (int i = 0; i < biProcs.Count; i++)
                {
                    startTimes[i] = biProcs[i].TotalProcessorTime;
                }

                // Wait for CPU usage to happen.
                Thread.Sleep(10000);

                // Take CPU usage measurements.
                record.CpuUsage = (byte)Math.Round(totalCpuCounter.NextValue());
                sw.Stop();
                TimeSpan totalTime = TimeSpan.Zero;
                for (int i = 0; i < biProcs.Count; i++)
                {
                    biProcs[i].Refresh();
                    if (biProcs[i].HasExited)
                    {
                        Logger.Info("Unable to generate anonymous performance data record because Blue Iris exited while CPU usage was being measured.");
                        return(null);
                    }
                    totalTime += biProcs[i].TotalProcessorTime - startTimes[i];
                }
                double fraction = totalTime.TotalMilliseconds / sw.Elapsed.TotalMilliseconds;
                record.BiCpuUsage = (byte)Math.Round((fraction / Environment.ProcessorCount) * 100);
                record.CpuThreads = (short)Environment.ProcessorCount;

                long physicalMemUsage = 0;
                long virtualMemUsage  = 0;
                foreach (Process p in biProcs)
                {
                    if (record.BiVersion == null)
                    {
                        record.BiVersion = p.MainModule.FileVersionInfo.FileVersion + " " + (MainSvc.Is64Bit(p) ? "x64" : "x86");
                    }
                    physicalMemUsage += p.WorkingSet64;
                    virtualMemUsage  += p.VirtualMemorySize64;
                }
                record.BiMemUsageMB            = (int)(physicalMemUsage / 1000000);
                record.BiPeakVirtualMemUsageMB = (int)(virtualMemUsage / 1000000);

                foreach (Process p in biProcs)
                {
                    IntPtr handle = p.MainWindowHandle;
                    if (handle == IntPtr.Zero)
                    {
                        // This is the service.
                    }
                    else
                    {
                        // This is the console.
                        record.ConsoleOpen = true;
                        try
                        {
                            WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
                            if (GetWindowPlacement(handle, ref placement))
                            {
                                if (placement.showCmd == 2)
                                {
                                    // Minimized
                                    record.ConsoleWidth  = -2;
                                    record.ConsoleHeight = -2;
                                }
                                else
                                {
                                    // Not Minimized
                                    RECT Rect = new RECT();
                                    if (GetWindowRect(handle, ref Rect))
                                    {
                                        record.ConsoleWidth  = (short)NumberUtil.Clamp(Rect.right - Rect.left, 0, short.MaxValue);
                                        record.ConsoleHeight = (short)NumberUtil.Clamp(Rect.bottom - Rect.top, 0, short.MaxValue);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.Debug(ex);
                        }
                    }
                }
                if (biProcs.Count > 1 && !record.ConsoleOpen)
                {
                    record.ConsoleOpen = true;
                }
            }

            record.Secret = Program.settings.secret;
            record.OS     = GetOsVersion();
            CpuInfo cpuInfo = GetCpuInfo();

            if (cpuInfo == null)
            {
                record.CpuModel = "Unknown";
                record.CpuMHz   = NumberUtil.ParseInt(cpuInfo.maxClockSpeed);
            }
            else
            {
                record.CpuModel = cpuInfo.GetModel();
                record.CpuMHz   = NumberUtil.ParseInt(cpuInfo.maxClockSpeed);
            }
            record.HelperVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            record.HwAccel       = (byte)RegistryUtil.GetHKLMValue <int>(@"SOFTWARE\Perspective Software\Blue Iris\Options", "hwaccel", 0);
            record.ServiceMode   = RegistryUtil.GetHKLMValue <int>(@"SOFTWARE\Perspective Software\Blue Iris\Options", "Service", 0) == 1;
            if (RegistryUtil.GetHKLMValue <int>(@"SOFTWARE\Perspective Software\Blue Iris\Options", "limitlive", 0) == 0)
            {
                record.LivePreviewFPS = -2;
            }
            else
            {
                record.LivePreviewFPS = (short)RegistryUtil.GetHKLMValue <int>(@"SOFTWARE\Perspective Software\Blue Iris\Options", "livefps", -1);
            }

            ComputerInfo computerInfo = new ComputerInfo();

            record.MemMB     = (int)(computerInfo.TotalPhysicalMemory / 1000000);
            record.MemFreeMB = (int)(computerInfo.AvailablePhysicalMemory / 1000000);

            RamInfo ramInfo = GetRamInfo();

            record.RamGiB        = ramInfo.GiB;
            record.RamChannels   = ramInfo.Channels;
            record.DimmLocations = ramInfo.DimmLocations;
            record.RamMHz        = ramInfo.MHz;

            // Get camera info.
            // Get frame rates (only accessible via BI's web server).
            Dictionary <string, double> fpsMap = new Dictionary <string, double>();

            BiServerInfo.Reload();
            if (BiServerInfo.enabled)
            {
                try
                {
                    using (WebClient wc = new WebClient())
                    {
                        string session  = CameraWebInterfaceLinker.GetSecureAuthenticatedSession(wc);
                        string response = wc.UploadString(CameraWebInterfaceLinker.GetJsonURL(), "{\"cmd\":\"camlist\",\"session\":\"" + session + "\"}");
                        wc.UploadString(CameraWebInterfaceLinker.GetJsonURL(), "{\"cmd\":\"logout\",\"session\":\"" + session + "\"}");
                        CamListResponse camListResponse = JsonConvert.DeserializeObject <CamListResponse>(response);
                        if (camListResponse != null && camListResponse.result == "success")
                        {
                            foreach (CameraListCamera camera in camListResponse.data)
                            {
                                if (camera.group == null)
                                {
                                    fpsMap[camera.optionValue] = camera.FPS;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Debug(ex, "Error reading camera list from web server.");
                }
            }

            // Get camera info from registry
            List <Upload_Camera> cameras = new List <Upload_Camera>();

            RegistryKey camerasKey = RegistryUtil.GetHKLMKey(@"SOFTWARE\Perspective Software\Blue Iris\Cameras");

            foreach (string camName in camerasKey.GetSubKeyNames())
            {
                RegistryKey camKey = camerasKey.OpenSubKey(camName);
                if (RegistryUtil.GetIntValue(camKey, "enabled", 0) != 1)
                {
                    continue;
                }
                string        shortName = RegistryUtil.GetStringValue(camKey, "shortname");
                Upload_Camera cam       = new Upload_Camera();

                if (fpsMap.TryGetValue(shortName, out double fps))
                {
                    cam.FPS = (byte)NumberUtil.Clamp(Math.Round(fps), 0, 255);
                }
                else
                {
                    int interval = RegistryUtil.GetIntValue(camKey, "interval", 1000000);
                    if (interval <= 0)
                    {
                        cam.FPS = 0;
                    }
                    else
                    {
                        cam.FPS = (byte)NumberUtil.Clamp(Math.Round(10000000.0 / interval), 0, 255);
                    }
                }
                cam.CapType     = (byte)RegistryUtil.GetIntValue(camKey, "screencap", 0);
                cam.Hwaccel     = (byte)RegistryUtil.GetIntValue(camKey, "ip_hwaccel", 0);
                cam.LimitDecode = RegistryUtil.GetIntValue(camKey, "smartdecode", 0) == 1;
                cam.Pixels      = RegistryUtil.GetIntValue(camKey, "fullxres", 0) * RegistryUtil.GetIntValue(camKey, "fullyres", 0);
                cam.Type        = (byte)RegistryUtil.GetIntValue(camKey, "type", 0);
                RegistryKey motionKey = camKey.OpenSubKey("Motion");
                cam.MotionDetector    = RegistryUtil.GetIntValue(motionKey, "enabled", 0) == 1;
                cam.RecordTriggerType = (byte)RegistryUtil.GetIntValue(motionKey, "continuous", 0);
                RegistryKey clipsKey = camKey.OpenSubKey("Clips");
                cam.RecordFormat = (byte)RegistryUtil.GetIntValue(clipsKey, "movieformat", 0);
                cam.DirectToDisk = RegistryUtil.GetIntValue(clipsKey, "transcode", 0) == 0;
                cam.VCodec       = RegistryUtil.GetStringValue(clipsKey, "vcodec");
                cameras.Add(cam);
            }

            record.cameras = cameras.ToArray();
            record.gpus    = GetGpuInfo().Select(g => new Upload_Gpu()
            {
                Name = g.Name, Version = g.DriverVersion
            }).ToArray();

            return(record);
        }