kcat / openal-soft

OpenAL Soft is a software implementation of the OpenAL 3D audio API.
Other
2.09k stars 517 forks source link

alcCaptureSamples always returning samples even when microphone is mute #516

Open minghia opened 3 years ago

minghia commented 3 years ago

When we try and see if there are any samples to be read, alcGetIntegerv is always returning a positive value so our application tries to process them. This happens even when we have turned the microphone off using alsamixer. For most of the part the samples appear to close to silence but we are trying to avoid transmitting any data around our network if there is nothing to send. We are using 1.20.1 and running under Centos 6.8 with a 5.7.8 kernel. We have tried USB sound cards and USB headsets and they are both returning samples. The only difference is that USB headsets records silence when the microphone is muted, the USB soundcards don't. What we need is a way to try and track down the problem? Is the problem likely to be in OpenAl, the kernel or ALSA lib? This is getting outside our area of expertise.

kcat commented 3 years ago

Muting just turns down the input gain to 0, but the device is left running. From OpenAL and ALSA's perspective, the device is still there producing samples, it's just being silent (or very close to it). The only way it could stop is if the device gets disconnected or disabled (though even that depends on whether the device capable of reporting a disconnection; USB devices typically can, but older on-board devices might not detect a microphone being unplugged). But that's not ideal because if the device gets disconnected or disabled, it causes an error for apps using it which OpenAL will treat as a device failure. It won't capture again until the device is physically reconnected/enabled and OpenAL closes/opens it again.

The proper way to do a temporary pause in capture is to call alcCaptureStop in the app. That will allow you to read any samples that have already been captured in the internal buffer, but won't produce any more samples until the app calls alcCaptureStart. Externally, all that can be done is silencing/strongly attenuating the captured samples (which will continue to come in for OpenAL to provide to the app), or disconnecting/disabling the device (which OpenAL will interpret as an unrecoverable device failure).

minghia commented 3 years ago

Actually my description was slightly incorrect. We also tried pulling the microphone plug out of the USB sound card, without disconnecting the USB sound card and data was still being returned. The values that are returned are just noise. I guess it could be a "feature" of the sound card. Any thoughts on why we are getting samples even with the microphone not plugged in?

kcat commented 3 years ago

Possibly because the device doesn't have a feature to signal an unplugged input. The device is still physically present, but the wires that would transmit the analog signal aren't connected to anything so it pulls in random noise/interference from the atmosphere. The system doesn't see a difference between a device with an unplugged input, and a device with a plugged-in input generating that same noise.

Certain devices have a way to detect when something is plugged into the microphone or headphones jack, so when something's there it signals to the system that the port is active and there's a usable audio signal, and when it's not there it signals to the system that the port is inactive. But it doesn't seem like your USB device can do that.