You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过SSH隧道将Linux端ALSA音频设备输出重定向至Windows系统?

SSH Audio Redirect for ALSA Playback (No Code Changes, MobaXTerm on Windows)

Great question! Since you're working with a headless Linux server using raw ALSA for audio playback and want to redirect output to your Windows machine via MobaXTerm SSH, here are two practical approaches that let you avoid modifying your core playback code (only adjusting the ALSA output device is needed):

Approach 1: Use MobaXTerm's Built-in PulseAudio Forwarding (Simplest)

MobaXTerm has native support for PulseAudio audio forwarding, which integrates seamlessly with ALSA via a plugin. This is the easiest method since it requires minimal setup:

  1. Install PulseAudio & ALSA Plugin on Linux
    On Debian/Ubuntu-based systems, run:

    sudo apt update && sudo apt install pulseaudio alsa-utils pulseaudio-utils
    

    For RHEL/CentOS/Fedora, use:

    sudo dnf install pulseaudio alsa-utils pulseaudio-libs
    
  2. Configure ALSA to Use PulseAudio by Default
    Create or edit the user-specific ALSA config file ~/.asoundrc with this content (this makes PulseAudio your default ALSA device, so your existing code doesn't need changes):

    pcm.!default {
        type pulse
    }
    ctl.!default {
        type pulse
    }
    

    If you want system-wide configuration, edit /etc/asound.conf instead.

  3. Enable PulseAudio Forwarding in MobaXTerm

    • Open MobaXTerm, go to Settings > Configuration > SSH
    • Check the box labeled Enable PulseAudio forwarding (this is usually enabled by default, but double-check)
  4. Test & Run Your Program
    Connect to your Linux server via SSH in MobaXTerm. Now run your ALSA program—if you're using the default device in snd_pcm_open, it will automatically route audio through the SSH tunnel to your Windows speakers. You can test with a sample audio file first:

    aplay /usr/share/sounds/alsa/Front_Center.wav
    

Approach 2: ALSA TCP Plugin + SSH Port Forwarding (No PulseAudio Needed)

If you prefer not to use PulseAudio, you can set up an ALSA TCP output device and forward the port via SSH to a Windows audio player like ffplay (included with FFmpeg, which you can install on Windows easily):

  1. Configure ALSA TCP Output on Linux
    Add this to your ~/.asoundrc file (adjust the audio format parameters to match your program's settings—SND_PCM_FORMAT_S32, your sample rate, and channel count):

    pcm.tcp_audio {
        type tcp
        slave {
            pcm {
                type raw
                format S32_LE
                rate 44100  # Replace with your program's sampleRate
                channels 2  # Replace with your program's width (channel count)
            }
        }
        server 127.0.0.1
        port 4567
    }
    
  2. Set Up SSH Port Forwarding in MobaXTerm

    • When creating/editing your SSH session in MobaXTerm, go to the Advanced SSH settings tab
    • Under Port forwarding, add a new rule:
      • Type: Local
      • Forwarded port: 4567
      • Remote server: 127.0.0.1
      • Remote port: 4567
        This forwards traffic from your Windows machine's localhost:4567 to the server's localhost:4567.
  3. Run Audio Receiver on Windows
    Install FFmpeg on Windows, then open a Command Prompt/PowerShell and run:

    ffplay -f s32le -ar 44100 -ac 2 tcp://localhost:4567
    

    Adjust the parameters (-ar for sample rate, -ac for channels) to match your ALSA program's configuration.

  4. Update Your Program's Output Device
    Modify the device parameter in snd_pcm_open to use tcp_audio instead of your local speaker device:

    snd_pcm_open(&handle, "tcp_audio", SND_PCM_STREAM_PLAYBACK, 0);
    

    No other code changes are needed—your program will send audio over the SSH tunnel to the Windows FFplay instance.


内容的提问来源于stack exchange,提问作者ZioByte

火山引擎 最新活动