From 44faedbbdd847e624f738bdffe7e16a92e978f6b Mon Sep 17 00:00:00 2001 From: Niellun Date: Mon, 2 Jun 2025 07:15:13 +0300 Subject: [PATCH] Audio driver and audio delay settings --- README.md | 9 +++++---- settings.txt | 10 ++++++++++ src/main.cpp | 29 +++++++++++++++++++++++++---- src/protocol.cpp | 3 +-- src/settings.h | 4 ++++ 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index be445fc..164de1c 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,12 @@ You will see list of devices, try to find the one that looks like dongle. Also y Bus 003 Device 066: ID 1314:1520 Magic Communication Tec. Auto Box ``` So in my case ID 1314:1520 shows idVendor 1314 and idProduct 1520. We need to use those to create udev rules. If yours are different you also need to put them in settings.txt and run application with settings.txt as argument. Remember that in settings.txt values are in decimal, so you need to convert hex values to base 10 first. -Create udev rules, replace <__Vendor__> <__Product__> and <__Your user__> with your informations +Create udev rules and add user to plugdev group, replace <__Vendor__> <__Product__> and <__Your user name__> with your informations ``` -echo 'SUBSYSTEM=="usb", ATTRS{idVendor}=="", ATTRS{idProduct}=="", GROUP="", MODE="0660"' | sudo tee /etc/udev/rules.d/50-carlinkit.rules -## example -## SUBSYSTEM=="usb", ATTRS{idVendor}=="1314", ATTRS{idProduct}=="1520", GROUP="linux", MODE="0660" +echo 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="", ATTRS{idProduct}=="", GROUP="plugdev", MODE="0660"' | sudo tee /etc/udev/rules.d/50-carlinkit.rules +## example: SUBSYSTEM=="usb", ATTRS{idVendor}=="1314", ATTRS{idProduct}=="1520", GROUP="plugdev", MODE="0660" +sudo usermod -aG plugdev +## example: sudo usermod -aG plugdev linux sudo udevadm control --reload-rules sudo udevadm trigger ``` diff --git a/settings.txt b/settings.txt index 3ab3414..9c56f31 100644 --- a/settings.txt +++ b/settings.txt @@ -77,6 +77,9 @@ # 3 - 1080p => 1920x1080 #android-resolution = 1 +# Media delay for android auto +#android-media-delay = 300 + ############################################################################## # 3.Application configuration ############################################################################## @@ -106,6 +109,13 @@ # Reduction level from 1 (no reduction) to 0 (fully silenced) #audio-fade = 0.3 +# Force application to use following audio driver as audio output, if empty use default driver +# See SDL documentation for options. Some of them are +# alsa - Not supporting multiple channels, navigation over music will not work +# pulseaudio +# pipewire +#audio-driver = + # Run script or app on phone connected and disconnected. # This script/app should be fast, otherwise it will block system. # If you need to start application in background use scripts with fork diff --git a/src/main.cpp b/src/main.cpp index 31472f5..52841fc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,7 +59,7 @@ void processKey(Protocol &protocol, SDL_Keysym key, RunParams ¶ms) case SDLK_r: params.dirty = true; - break; + break; case SDLK_LEFT: protocol.sendKey(100); @@ -206,11 +206,11 @@ void application() Uint32 frameStart = SDL_GetTicks(); while (active) { - if(processEvents(protocol, p, interface)) + if (processEvents(protocol, p, interface)) { - if(p.connected) + if (p.connected) { - decoder.flush(); + decoder.flush(); videoBuffer.reset(); } } @@ -253,6 +253,23 @@ void application() SDL_HideWindow(window); } +bool setAudioDriver() +{ + if (Settings::audioDriver.value.length() < 2) + return true; + + int num = SDL_GetNumAudioDrivers(); + for (int i = 0; i < num; ++i) + { + if (SDL_GetAudioDriver(i) == Settings::audioDriver.value) + { + SDL_setenv("SDL_AUDIODRIVER", SDL_GetAudioDriver(i), 1); + return true; + } + } + return false; +} + int start() { if (!Settings::logging) @@ -260,12 +277,16 @@ int start() else Settings::print(); + if (!setAudioDriver()) + std::cerr << "[Main] Not supported audio driver " << Settings::audioDriver.value << std::endl; + // Initialize SDL video subsystem if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) != 0) { std::cerr << "[Main] SDL initialisation failed: " << SDL_GetError() << std::endl; return 1; } + std::cout << "[Main] SDL audio driver: " << SDL_GetCurrentAudioDriver() << std::endl; if (TTF_Init() != 0) { diff --git a/src/protocol.cpp b/src/protocol.cpp index c5611cf..a00242e 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -52,7 +52,6 @@ void Protocol::sendInit(int width, int height, int fps) void Protocol::sendConfig() { int syncTime = std::time(nullptr); - int mediaDelay = 300; int drivePosition = Settings::leftDrive ? 0 : 1; // 0==left, 1==right int nightMode = Settings::nightMode; // 0==day, 1==night, 2==auto if (nightMode < 0 || nightMode > 2) @@ -95,7 +94,7 @@ void Protocol::sendConfig() "{\"syncTime\":%d,\"mediaDelay\":%d,\"drivePosition\":%d," "\"androidAutoSizeW\":%d,\"androidAutoSizeH\":%d,\"HiCarConnectMode\":0," "\"GNSSCapability\":7,\"DashboardInfo\":1,\"UseBTPhone\":0}", - syncTime, mediaDelay, drivePosition, width, height); + syncTime, Settings::mediaDelay.value, drivePosition, width, height); sendString(CMD_JSON_CONTROL, buffer); diff --git a/src/settings.h b/src/settings.h index f2f38b1..d323d9b 100644 --- a/src/settings.h +++ b/src/settings.h @@ -28,6 +28,9 @@ public: static inline Setting micType{"mic-type", 1}; static inline Setting dpi{"android-dpi", 120}; static inline Setting androidMode{"android-resolution", 0}; + static inline Setting mediaDelay{"android-media-delay", 300}; + + // Application configuration section static inline Setting fontSize{"font-size", 30}; @@ -38,6 +41,7 @@ public: static inline Setting audioQueue{"audio-buffer-size", 16}; static inline Setting audioDelay{"audio-buffer-wait", 2}; static inline Setting audioFade{"audio-fade", 0.3}; + static inline Setting audioDriver{"audio-driver", ""}; static inline Setting onConnect{"on-connect-script", ""}; static inline Setting onDisconnect{"on-disconnect-script", ""};