Saving the last output device and reusing it

master
WolverinDEV 2021-03-25 11:49:00 +01:00
parent 10d239c545
commit 805f248a50
5 changed files with 44 additions and 16 deletions

View File

@ -5,6 +5,11 @@ export interface OutputDevice {
name: string;
}
export namespace OutputDevice {
export const NoDeviceId = "none";
export const DefaultDeviceId = "default";
}
export interface AudioBackendEvents {
notify_initialized: {},
notify_volume_changed: { oldVolume: number, newVolume: number }

View File

@ -111,7 +111,7 @@ export function get_master_volume() : number {
return master_volume;
}
export function set_master_volume(volume: number) {
export function setSoundMasterVolume(volume: number) {
volume_require_save = volume_require_save || master_volume != volume;
master_volume = volume;
if(master_mixed) {
@ -159,7 +159,7 @@ export function save() {
}
}
export function initialize() : Promise<void> {
export function initializeSounds() : Promise<void> {
$.ajaxSetup({
beforeSend: function(jqXHR,settings){
if (settings.dataType === 'binary') {

View File

@ -1,8 +1,5 @@
import * as loader from "tc-loader";
import * as bipc from "./ipc/BrowserIPC";
import * as sound from "./audio/Sounds";
import * as i18n from "./i18n/localize";
import {tra} from "./i18n/localize";
import {initializeI18N, tra} from "./i18n/localize";
import * as fidentity from "./profiles/identities/TeaForumIdentity";
import * as global_ev_handler from "./events/ClientGlobalControlHandler";
import {AppParameters, settings, Settings, UrlParameterBuilder, UrlParameterParser} from "tc-shared/settings";
@ -29,7 +26,7 @@ import {ActionResult} from "tc-services";
import {CommandResult} from "tc-shared/connection/ServerConnectionDeclaration";
import {ErrorCode} from "tc-shared/connection/ErrorCode";
import {bookmarks} from "tc-shared/Bookmarks";
import {getAudioBackend} from "tc-shared/audio/Player";
import {getAudioBackend, OutputDevice} from "tc-shared/audio/Player";
/* required import for init */
import "svg-sprites/client-icons";
@ -54,25 +51,44 @@ import "./ui/elements/Tab";
import "./clientservice";
import "./text/bbcode/InviteController";
import "./text/bbcode/YoutubeController";
import {initializeSounds, setSoundMasterVolume} from "./audio/Sounds";
import {getInstanceConnectHandler, setupIpcHandler} from "./ipc/BrowserIPC";
assertMainApplication();
let preventWelcomeUI = false;
async function initialize() {
try {
await i18n.initializeI18N();
await initializeI18N();
} catch(error) {
console.error(tr("Failed to initialized the translation system!\nError: %o"), error);
loader.critical_error("Failed to setup the translation system");
return;
}
bipc.setupIpcHandler();
setupIpcHandler();
}
async function initializeApp() {
global_ev_handler.initialize(global_client_actions);
getAudioBackend().setMasterVolume(settings.getValue(Settings.KEY_SOUND_MASTER) / 100);
getAudioBackend().executeWhenInitialized(() => {
const defaultDeviceId = getAudioBackend().getDefaultDeviceId();
let targetDeviceId = settings.getValue(Settings.KEY_SPEAKER_DEVICE_ID, OutputDevice.DefaultDeviceId);
if(targetDeviceId === OutputDevice.DefaultDeviceId) {
targetDeviceId = defaultDeviceId;
}
getAudioBackend().setCurrentDevice(targetDeviceId).catch(error => {
logWarn(LogCategory.AUDIO, tr("Failed to set last used output speaker device: %o"), error);
if(targetDeviceId !== defaultDeviceId) {
getAudioBackend().setCurrentDevice(defaultDeviceId).catch(error => {
logError(LogCategory.AUDIO, tr("Failed to set output device to default device: %o"), error);
createErrorModal(tr("Failed to initialize output device"), tr("Failed to initialize output device.")).open();
});
}
});
});
const recorder = new RecorderProfile("default");
try {
@ -83,10 +99,10 @@ async function initializeApp() {
}
setDefaultRecorder(recorder);
sound.initialize().then(() => {
initializeSounds().then(() => {
logInfo(LogCategory.AUDIO, tr("Sounds initialized"));
});
sound.set_master_volume(settings.getValue(Settings.KEY_SOUND_MASTER_SOUNDS) / 100);
setSoundMasterVolume(settings.getValue(Settings.KEY_SOUND_MASTER_SOUNDS) / 100);
}
/* The native client has received a connect request. */
@ -161,7 +177,6 @@ type ConnectRequestResult = {
* @param parameters General connect parameters from the connect URL
*/
async function doHandleConnectRequest(serverAddress: string, serverUniqueId: string | undefined, parameters: UrlParameterParser) : Promise<ConnectRequestResult> {
let targetServerConnection: ConnectionHandler;
let isCurrentServerConnection: boolean;
@ -423,7 +438,7 @@ const task_connect_handler: loader.Task = {
}
};
const chandler = bipc.getInstanceConnectHandler();
const chandler = getInstanceConnectHandler();
if(chandler && AppParameters.getValue(AppParameters.KEY_CONNECT_NO_SINGLE_INSTANCE)) {
try {
await chandler.post_connect_request(connectData, () => new Promise<boolean>(resolve => {

View File

@ -810,6 +810,12 @@ export class Settings {
defaultValue: 500
};
static readonly KEY_SPEAKER_DEVICE_ID: RegistryKey<string> = {
key: "speaker_device_id",
valueType: "string",
description: "The target speaker device id",
}
static readonly FN_LOG_ENABLED: (category: string) => RegistryKey<boolean> = category => {
return {
key: "log." + category.toLowerCase() + ".enabled",

View File

@ -2,7 +2,7 @@ import {createErrorModal, createInfoModal, createInputModal, createModal, Modal}
import {sliderfy} from "tc-shared/ui/elements/Slider";
import {settings, Settings} from "tc-shared/settings";
import * as sound from "tc-shared/audio/Sounds";
import {manager, set_master_volume, Sound} from "tc-shared/audio/Sounds";
import {manager, setSoundMasterVolume, Sound} from "tc-shared/audio/Sounds";
import * as profiles from "tc-shared/profiles/ConnectionProfile";
import {ConnectionProfile} from "tc-shared/profiles/ConnectionProfile";
import {IdentitifyType} from "tc-shared/profiles/Identity";
@ -653,8 +653,10 @@ function settings_audio_speaker(container: JQuery, modal: Modal) {
_old.removeClass("selected");
tag.addClass("selected");
getAudioBackend().setCurrentDevice(device?.device_id).then(() => {
const targetDeviceId = device.device_id || OutputDevice.NoDeviceId;
getAudioBackend().setCurrentDevice(targetDeviceId).then(() => {
logDebug(LogCategory.AUDIO, tr("Changed default speaker device"));
settings.setValue(Settings.KEY_SPEAKER_DEVICE_ID, targetDeviceId);
}).catch((error) => {
_old.addClass("selected");
tag.removeClass("selected");
@ -726,7 +728,7 @@ function settings_audio_speaker(container: JQuery, modal: Modal) {
});
slider.on('change', event => {
const volume = parseInt(slider.attr('value'));
set_master_volume(volume / 100);
setSoundMasterVolume(volume / 100);
settings.setValue(Settings.KEY_SOUND_MASTER_SOUNDS, volume);
});
}