Browse Source

UI: Add process priority option for windows

jp9000 9 years ago
parent
commit
0b2fd1167e

+ 5 - 0
obs/data/locale/en-US.ini

@@ -509,6 +509,11 @@ Basic.Settings.Audio.UnknownAudioDevice="[Device not connected or not available]
 
 # basic mode 'advanced' settings
 Basic.Settings.Advanced="Advanced"
+Basic.Settings.Advanced.General.ProcessPriority="Process Priority"
+Basic.Settings.Advanced.General.ProcessPriority.High="High"
+Basic.Settings.Advanced.General.ProcessPriority.AboveNormal="Above Normal"
+Basic.Settings.Advanced.General.ProcessPriority.Normal="Normal"
+Basic.Settings.Advanced.General.ProcessPriority.Idle="Idle"
 Basic.Settings.Advanced.FormatWarning="Warning:  Color formats other than NV12 are primarily intended for recording, and are not recommended when streaming.  Streaming may incur increased CPU usage due to color format conversion."
 Basic.Settings.Advanced.Audio.BufferingTime="Audio Buffering Time"
 Basic.Settings.Advanced.Video.ColorFormat="Color Format"

+ 24 - 2
obs/forms/OBSBasicSettings.ui

@@ -2887,8 +2887,8 @@
              <rect>
               <x>0</x>
               <y>0</y>
-              <width>559</width>
-              <height>563</height>
+              <width>718</width>
+              <height>622</height>
              </rect>
             </property>
             <layout class="QVBoxLayout" name="verticalLayout_16">
@@ -2907,6 +2907,28 @@
              <item alignment="Qt::AlignTop">
               <widget class="QWidget" name="widget_11" native="true">
                <layout class="QVBoxLayout" name="verticalLayout_17">
+                <item>
+                 <widget class="QGroupBox" name="advancedGeneralGroupBox">
+                  <property name="title">
+                   <string>Basic.Settings.General</string>
+                  </property>
+                  <layout class="QFormLayout" name="formLayout_22">
+                   <item row="0" column="0">
+                    <widget class="QLabel" name="processPriorityLabel">
+                     <property name="text">
+                      <string>Basic.Settings.Advanced.General.ProcessPriority</string>
+                     </property>
+                     <property name="buddy">
+                      <cstring>processPriority</cstring>
+                     </property>
+                    </widget>
+                   </item>
+                   <item row="0" column="1">
+                    <widget class="QComboBox" name="processPriority"/>
+                   </item>
+                  </layout>
+                 </widget>
+                </item>
                 <item>
                  <widget class="QGroupBox" name="advancedVideoContainer">
                   <property name="title">

+ 2 - 0
obs/obs-app.cpp

@@ -328,6 +328,8 @@ bool OBSApp::InitGlobalConfigDefaults()
 	config_set_default_string(globalConfig, "General", "Language",
 			DEFAULT_LANG);
 	config_set_default_uint(globalConfig, "General", "MaxLogs", 10);
+	config_set_default_string(globalConfig, "General", "ProcessPriority",
+			"Normal");
 
 #if _WIN32
 	config_set_default_string(globalConfig, "Video", "Renderer",

+ 15 - 0
obs/platform-windows.cpp

@@ -215,3 +215,18 @@ void SetAlwaysOnTop(QMainWindow *window, bool enable)
 	SetWindowPos(hwnd, enable ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0,
 			SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
 }
+
+void SetProcessPriority(const char *priority)
+{
+	if (!priority)
+		return;
+
+	if (strcmp(priority, "High") == 0)
+		SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
+	else if (strcmp(priority, "AboveNormal") == 0)
+		SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
+	else if (strcmp(priority, "Normal") == 0)
+		SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
+	else if (strcmp(priority, "Idle") == 0)
+		SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
+}

+ 1 - 0
obs/platform.hpp

@@ -51,6 +51,7 @@ void SetAlwaysOnTop(QMainWindow *window, bool enable);
 #ifdef _WIN32
 uint32_t GetWindowsVersion();
 void SetAeroEnabled(bool enable);
+void SetProcessPriority(const char *priority);
 #endif
 
 #ifdef __APPLE__

+ 37 - 1
obs/window-basic-main.cpp

@@ -2267,13 +2267,20 @@ void OBSBasic::SetService(obs_service_t *newService)
 		service = newService;
 }
 
-bool OBSBasic::StreamingActive()
+bool OBSBasic::StreamingActive() const
 {
 	if (!outputHandler)
 		return false;
 	return outputHandler->StreamingActive();
 }
 
+bool OBSBasic::Active() const
+{
+	if (!outputHandler)
+		return false;
+	return outputHandler->Active();
+}
+
 #ifdef _WIN32
 #define IS_WIN32 1
 #else
@@ -3397,6 +3404,27 @@ void OBSBasic::StartStreaming()
 		StartRecording();
 }
 
+#ifdef _WIN32
+static inline void UpdateProcessPriority()
+{
+	const char *priority = config_get_string(App()->GlobalConfig(),
+			"General", "ProcessPriority");
+	if (priority && strcmp(priority, "Normal") != 0)
+		SetProcessPriority(priority);
+}
+
+static inline void ClearProcessPriority()
+{
+	const char *priority = config_get_string(App()->GlobalConfig(),
+			"General", "ProcessPriority");
+	if (priority && strcmp(priority, "Normal") != 0)
+		SetProcessPriority("Normal");
+}
+#else
+#define UpdateProcessPriority() do {} while(false)
+#define ClearProcessPriority() do {} while(false)
+#endif
+
 void OBSBasic::StopStreaming()
 {
 	SaveProject();
@@ -3407,6 +3435,7 @@ void OBSBasic::StopStreaming()
 	if (!outputHandler->Active() && !ui->profileMenu->isEnabled()) {
 		ui->profileMenu->setEnabled(true);
 		App()->DecrementSleepInhibition();
+		ClearProcessPriority();
 	}
 
 	bool recordWhenStreaming = config_get_bool(GetGlobalConfig(),
@@ -3427,6 +3456,7 @@ void OBSBasic::ForceStopStreaming()
 	if (!outputHandler->Active() && !ui->profileMenu->isEnabled()) {
 		ui->profileMenu->setEnabled(true);
 		App()->DecrementSleepInhibition();
+		ClearProcessPriority();
 	}
 
 	bool recordWhenStreaming = config_get_bool(GetGlobalConfig(),
@@ -3457,6 +3487,7 @@ void OBSBasic::StreamDelayStarting(int sec)
 	if (ui->profileMenu->isEnabled()) {
 		ui->profileMenu->setEnabled(false);
 		App()->IncrementSleepInhibition();
+		UpdateProcessPriority();
 	}
 }
 
@@ -3487,6 +3518,7 @@ void OBSBasic::StreamingStart()
 	if (ui->profileMenu->isEnabled()) {
 		ui->profileMenu->setEnabled(false);
 		App()->IncrementSleepInhibition();
+		UpdateProcessPriority();
 	}
 
 	blog(LOG_INFO, STREAMING_START);
@@ -3533,6 +3565,7 @@ void OBSBasic::StreamingStop(int code)
 	if (!outputHandler->Active() && !ui->profileMenu->isEnabled()) {
 		ui->profileMenu->setEnabled(true);
 		App()->DecrementSleepInhibition();
+		ClearProcessPriority();
 	}
 
 	blog(LOG_INFO, STREAMING_STOP);
@@ -3573,6 +3606,7 @@ void OBSBasic::StopRecording()
 	if (!outputHandler->Active() && !ui->profileMenu->isEnabled()) {
 		ui->profileMenu->setEnabled(true);
 		App()->DecrementSleepInhibition();
+		ClearProcessPriority();
 	}
 }
 
@@ -3584,6 +3618,7 @@ void OBSBasic::RecordingStart()
 	if (ui->profileMenu->isEnabled()) {
 		ui->profileMenu->setEnabled(false);
 		App()->IncrementSleepInhibition();
+		UpdateProcessPriority();
 	}
 
 	blog(LOG_INFO, RECORDING_START);
@@ -3614,6 +3649,7 @@ void OBSBasic::RecordingStop(int code)
 	if (!outputHandler->Active() && !ui->profileMenu->isEnabled()) {
 		ui->profileMenu->setEnabled(true);
 		App()->DecrementSleepInhibition();
+		ClearProcessPriority();
 	}
 }
 

+ 2 - 1
obs/window-basic-main.hpp

@@ -370,7 +370,8 @@ public:
 	obs_service_t *GetService();
 	void          SetService(obs_service_t *service);
 
-	bool StreamingActive();
+	bool StreamingActive() const;
+	bool Active() const;
 
 	int  ResetVideo();
 	bool ResetAudio();

+ 39 - 0
obs/window-basic-settings.cpp

@@ -371,6 +371,7 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent)
 	HookWidget(ui->reconnectEnable,      CHECK_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->reconnectRetryDelay,  SCROLL_CHANGED, ADV_CHANGED);
 	HookWidget(ui->reconnectMaxRetries,  SCROLL_CHANGED, ADV_CHANGED);
+	HookWidget(ui->processPriority,      COMBO_CHANGED,  ADV_CHANGED);
 
 #ifdef _WIN32
 	uint32_t winVer = GetWindowsVersion();
@@ -386,15 +387,39 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent)
 		connect(toggleAero, &QAbstractButton::toggled,
 				this, &OBSBasicSettings::ToggleDisableAero);
 	}
+
+#define PROCESS_PRIORITY(val) \
+	{"Basic.Settings.Advanced.General.ProcessPriority." ## val , val}
+
+	static struct ProcessPriority {
+		const char *name;
+		const char *val;
+	} processPriorities[] = {
+		PROCESS_PRIORITY("High"),
+		PROCESS_PRIORITY("AboveNormal"),
+		PROCESS_PRIORITY("Normal"),
+		PROCESS_PRIORITY("Idle")
+	};
+#undef PROCESS_PRIORITY
+
+	for (ProcessPriority pri : processPriorities)
+		ui->processPriority->addItem(QTStr(pri.name), pri.val);
+
 #else
 	delete ui->rendererLabel;
 	delete ui->renderer;
 	delete ui->adapterLabel;
 	delete ui->adapter;
+	delete ui->processPriorityLabel;
+	delete ui->processPriority;
+	delete ui->advancedGeneralGroupBox;
 	ui->rendererLabel = nullptr;
 	ui->renderer = nullptr;
 	ui->adapterLabel = nullptr;
 	ui->adapter = nullptr;
+	ui->processPriorityLabel = nullptr;
+	ui->processPriority = nullptr;
+	ui->advancedGeneralGroupBox = nullptr;
 #endif
 
 #ifndef __APPLE__
@@ -1817,6 +1842,13 @@ void OBSBasicSettings::LoadAdvancedSettings()
 	ui->disableOSXVSync->setChecked(disableOSXVSync);
 	ui->resetOSXVSync->setChecked(resetOSXVSync);
 	ui->resetOSXVSync->setEnabled(disableOSXVSync);
+#elif _WIN32
+	const char *processPriority = config_get_string(App()->GlobalConfig(),
+			"General", "ProcessPriority");
+	int idx = ui->processPriority->findData(processPriority);
+	if (idx == -1)
+		idx = ui->processPriority->findData("Normal");
+	ui->processPriority->setCurrentIndex(idx);
 #endif
 
 	loading = false;
@@ -2250,6 +2282,13 @@ void OBSBasicSettings::SaveAdvancedSettings()
 	if (WidgetChanged(ui->renderer))
 		config_set_string(App()->GlobalConfig(), "Video", "Renderer",
 				QT_TO_UTF8(ui->renderer->currentText()));
+
+	std::string priority =
+		QT_TO_UTF8(ui->processPriority->currentData().toString());
+	config_set_string(App()->GlobalConfig(), "General", "ProcessPriority",
+			priority.c_str());
+	if (main->Active())
+		SetProcessPriority(priority.c_str());
 #endif
 
 #ifdef __APPLE__