hyperion.ng/libsrc/grabber/audio/AudioGrabberLinux.cpp

318 lines
7.7 KiB
C++
Raw Normal View History

Audio Grabber Feature (#1570) * Creating Audio Grabber Creating Audio Grabber Creating Audio Grabber. Successfully began capturing audio in windows. Starting to implement a hard-coded UV Visualizer. Got Windows DirectSound Implementation working. Hardcoded basic VU Meter. Begin working on linux audio grabber implementation. Finished Linux Draft Implementation. Minor Mods to windows implementation. Windows: - Free memory used by device id. - Prevent starting audio if the grabber is disabled - More debug logging Linux: - Prevent starting audio if the grabber is disabled Added strings to english Removed "custom" from device selection Made hard-coded visualizer values configurable. wrote values to imageData with BGR priority to enable configurable values to be set in RGB format. created logic to support "Automatic" to enable the API to select the default device. Add language key for audio in "Remote Control" section. Removed audio configuration for number of channels. This was causing an error with some devices. Fixed logic to update capture while its active. Optimizing code . UI Tweaks Destructuring. Fixed build error on linux. Custom Effects - Clean-ups and Enhancements (#1163) * Cleanup EffectFileHandler * Support Custom Effect Schemas and align EffectFileHandler * Change back to colon prefix for system effects * WebSockets - Fix error in handling fragmented frames * Correct missing colon updates * Update json with image file location for custom gif effects * Image effect deletion - considere full filename is stored in JSON * Correct selection lists indentions Creating Audio Grabber Creating Audio Grabber Creating Audio Grabber. Successfully began capturing audio in windows. Starting to implement a hard-coded UV Visualizer. Got Windows DirectSound Implementation working. Hardcoded basic VU Meter. Begin working on linux audio grabber implementation. Finished Linux Draft Implementation. Minor Mods to windows implementation. Windows: - Free memory used by device id. - Prevent starting audio if the grabber is disabled - More debug logging Linux: - Prevent starting audio if the grabber is disabled Added strings to english Removed "custom" from device selection Made hard-coded visualizer values configurable. wrote values to imageData with BGR priority to enable configurable values to be set in RGB format. created logic to support "Automatic" to enable the API to select the default device. Add language key for audio in "Remote Control" section. Removed audio configuration for number of channels. This was causing an error with some devices. Fixed logic to update capture while its active. Optimizing code . UI Tweaks Destructuring. Fixed build error on linux. Commented setVideoMode from AudioGrabber. Linux Threading changes. Implementing new API Continuing to implement audio into new APIs Fixed Audio Grabber for DirectSound on Windows Fixed UI for Audio Grabber Configuration Default AUDIO to off unless specified. fixed missing #ifdef for audio grabber. Added logic to calculate a dynamic multiplier from the signal input. Updating linux api for discovering devices. Fixed HTML/JS issues with view. Fixed NPE in Windows. Disabled setting thread priority in linux. updated the schema options check to pass through hidden states and commented the change. Updated grabber start conditions Updated Audio grabber to instantiate similar to video grabber Updated windows grabber to set "started" flag to false when shutting down. Removed "tryStart" to prevent enabling audio capture unnecessarily. Fixing instance audio grabber device configuration Added configurable resolution Reduced tolerance to 5% Fixed issue where grabber failed for additional instances when "start" was called multiple times. Fixed resolution calculation Change averaging algorithm to prevent overflowing the sum. Updated logic to stop audio grabber when disabled. Fix integer casting and rounding. Restart grabber on configuration change. Fix missing include/grabber/AudioGrabber. Disable tolerance. Added configurable tolerance. Fixed tolerance algorithm. reset multiplier on configuration change. Line Endings Proposed change and questions/request to fix implementing more of LordGrey's suggestions. Fix mode for snd_pcm_open. Latest ALSA uses SND_PCM_NONBLOCK instead of SND_PCM_OPEN_NONBLOCK defaulted multiplier to 0 "auto" defaulted tolerance to 20% changed 100 to 100.0 for pixel value percentage calculation to fix value from being 0. missed a 100 as a double so precision isn't lost during math operation. Fix Windows grabber and further cleanups Enable Audio grabbing in standard build Remove empty methods Fix audio capture priority setting Remove unused code Clean-up default config Allow additional json-editor attributes Allow multiple effects and resetting to defaults Correct default values Allow to build for Qt < 5.14 Update CodeQL build dependency Update build dependencies Remove effect1 placeholder * Renamed uvMeter to VU Meter (Volume Unit) - Fixed issues flagged by code scanning bot. * Moved stop call into destructor of implementing class. * Removed commented linux audio channel configuration logic. --------- Co-authored-by: Michael Rochelle <michael@j2inn.com>
2023-02-19 09:36:39 +01:00
#include <grabber/AudioGrabberLinux.h>
#include <alsa/asoundlib.h>
#include <QJsonObject>
#include <QJsonArray>
typedef void* (*THREADFUNCPTR)(void*);
AudioGrabberLinux::AudioGrabberLinux()
: AudioGrabber()
, _isRunning{ false }
, _captureDevice {nullptr}
, _sampleRate(44100)
{
}
AudioGrabberLinux::~AudioGrabberLinux()
{
this->stop();
}
void AudioGrabberLinux::refreshDevices()
{
Debug(_log, "Enumerating Audio Input Devices");
_deviceProperties.clear();
snd_ctl_t* deviceHandle;
int soundCard {-1};
int error {-1};
int cardInput {-1};
snd_ctl_card_info_t* cardInfo;
snd_pcm_info_t* deviceInfo;
snd_ctl_card_info_alloca(&cardInfo);
snd_pcm_info_alloca(&deviceInfo);
while (snd_card_next(&soundCard) > -1)
{
if (soundCard < 0)
{
break;
}
char cardId[32];
sprintf(cardId, "hw:%d", soundCard);
if ((error = snd_ctl_open(&deviceHandle, cardId, SND_CTL_NONBLOCK)) < 0)
{
Error(_log, "Erorr opening device: (%i): %s", soundCard, snd_strerror(error));
continue;
}
if ((error = snd_ctl_card_info(deviceHandle, cardInfo)) < 0)
{
Error(_log, "Erorr getting hardware info: (%i): %s", soundCard, snd_strerror(error));
snd_ctl_close(deviceHandle);
continue;
}
cardInput = -1;
while (true)
{
if (snd_ctl_pcm_next_device(deviceHandle, &cardInput) < 0)
Error(_log, "Error selecting device input");
if (cardInput < 0)
break;
snd_pcm_info_set_device(deviceInfo, static_cast<uint>(cardInput));
snd_pcm_info_set_subdevice(deviceInfo, 0);
snd_pcm_info_set_stream(deviceInfo, SND_PCM_STREAM_CAPTURE);
if ((error = snd_ctl_pcm_info(deviceHandle, deviceInfo)) < 0)
{
if (error != -ENOENT)
Error(_log, "Digital Audio Info: (%i): %s", soundCard, snd_strerror(error));
continue;
}
AudioGrabber::DeviceProperties device;
device.id = QString("hw:%1,%2").arg(snd_pcm_info_get_card(deviceInfo)).arg(snd_pcm_info_get_device(deviceInfo));
device.name = QString("%1: %2").arg(snd_ctl_card_info_get_name(cardInfo),snd_pcm_info_get_name(deviceInfo));
Debug(_log, "Found sound card (%s): %s", QSTRING_CSTR(device.id), QSTRING_CSTR(device.name));
_deviceProperties.insert(device.id, device);
}
snd_ctl_close(deviceHandle);
}
}
bool AudioGrabberLinux::configureCaptureInterface()
{
int error = -1;
QString name = (_device.isEmpty() || _device == "auto") ? "default" : (_device);
if ((error = snd_pcm_open(&_captureDevice, QSTRING_CSTR(name) , SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0)
{
Error(_log, "Failed to open audio device: %s, - %s", QSTRING_CSTR(_device), snd_strerror(error));
return false;
}
if ((error = snd_pcm_hw_params_malloc(&_captureDeviceConfig)) < 0)
{
Error(_log, "Failed to create hardware parameters: %s", snd_strerror(error));
snd_pcm_close(_captureDevice);
return false;
}
if ((error = snd_pcm_hw_params_any(_captureDevice, _captureDeviceConfig)) < 0)
{
Error(_log, "Failed to initialize hardware parameters: %s", snd_strerror(error));
snd_pcm_hw_params_free(_captureDeviceConfig);
snd_pcm_close(_captureDevice);
return false;
}
if ((error = snd_pcm_hw_params_set_access(_captureDevice, _captureDeviceConfig, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
{
Error(_log, "Failed to configure interleaved mode: %s", snd_strerror(error));
snd_pcm_hw_params_free(_captureDeviceConfig);
snd_pcm_close(_captureDevice);
return false;
}
if ((error = snd_pcm_hw_params_set_format(_captureDevice, _captureDeviceConfig, SND_PCM_FORMAT_S16_LE)) < 0)
{
Error(_log, "Failed to configure capture format: %s", snd_strerror(error));
snd_pcm_hw_params_free(_captureDeviceConfig);
snd_pcm_close(_captureDevice);
return false;
}
if ((error = snd_pcm_hw_params_set_rate_near(_captureDevice, _captureDeviceConfig, &_sampleRate, nullptr)) < 0)
{
Error(_log, "Failed to configure sample rate: %s", snd_strerror(error));
snd_pcm_hw_params_free(_captureDeviceConfig);
snd_pcm_close(_captureDevice);
return false;
}
if ((error = snd_pcm_hw_params(_captureDevice, _captureDeviceConfig)) < 0)
{
Error(_log, "Failed to configure hardware parameters: %s", snd_strerror(error));
snd_pcm_hw_params_free(_captureDeviceConfig);
snd_pcm_close(_captureDevice);
return false;
}
snd_pcm_hw_params_free(_captureDeviceConfig);
if ((error = snd_pcm_prepare(_captureDevice)) < 0)
{
Error(_log, "Failed to prepare audio interface: %s", snd_strerror(error));
snd_pcm_close(_captureDevice);
return false;
}
if ((error = snd_pcm_start(_captureDevice)) < 0)
{
Error(_log, "Failed to start audio interface: %s", snd_strerror(error));
snd_pcm_close(_captureDevice);
return false;
}
return true;
}
bool AudioGrabberLinux::start()
{
if (!_isEnabled)
return false;
if (_isRunning.load(std::memory_order_acquire))
return true;
Debug(_log, "Start Audio With %s", QSTRING_CSTR(getDeviceName(_device)));
if (!configureCaptureInterface())
return false;
_isRunning.store(true, std::memory_order_release);
pthread_attr_t threadAttributes;
int threadPriority = 1;
sched_param schedulerParameter;
schedulerParameter.sched_priority = threadPriority;
if (pthread_attr_init(&threadAttributes) != 0)
{
Debug(_log, "Failed to create thread attributes");
stop();
return false;
}
if (pthread_create(&_audioThread, &threadAttributes, static_cast<THREADFUNCPTR>(&AudioThreadRunner), static_cast<void*>(this)) != 0)
{
Debug(_log, "Failed to create audio capture thread");
stop();
return false;
}
AudioGrabber::start();
return true;
}
void AudioGrabberLinux::stop()
{
if (!_isRunning.load(std::memory_order_acquire))
return;
Debug(_log, "Stopping Audio Interface");
_isRunning.store(false, std::memory_order_release);
if (_audioThread != 0) {
pthread_join(_audioThread, NULL);
}
snd_pcm_close(_captureDevice);
AudioGrabber::stop();
}
void AudioGrabberLinux::processAudioBuffer(snd_pcm_sframes_t frames)
{
if (!_isRunning.load(std::memory_order_acquire))
return;
ssize_t bytes = snd_pcm_frames_to_bytes(_captureDevice, frames);
int16_t * buffer = static_cast<int16_t*>(calloc(static_cast<size_t>(bytes / 2), sizeof(int16_t)));
if (frames == 0)
{
buffer[0] = 0;
processAudioFrame(buffer, 1);
}
else
{
snd_pcm_sframes_t framesRead = snd_pcm_readi(_captureDevice, buffer, static_cast<snd_pcm_uframes_t>(frames));
if (framesRead < frames)
{
Error(_log, "Error reading audio. Got %d frames instead of %d", framesRead, frames);
}
else
{
processAudioFrame(buffer, static_cast<int>(snd_pcm_frames_to_bytes(_captureDevice, framesRead)) / 2);
}
}
free(buffer);
}
QJsonArray AudioGrabberLinux::discover(const QJsonObject& /*params*/)
{
refreshDevices();
QJsonArray devices;
for (auto deviceIterator = _deviceProperties.begin(); deviceIterator != _deviceProperties.end(); ++deviceIterator)
{
// Device
QJsonObject device;
QJsonArray deviceInputs;
device["device"] = deviceIterator.key();
device["device_name"] = deviceIterator.value().name;
device["type"] = "audio";
devices.append(device);
}
return devices;
}
QString AudioGrabberLinux::getDeviceName(const QString& devicePath) const
{
if (devicePath.isEmpty() || devicePath == "auto")
{
return "Default Audio Device";
}
return _deviceProperties.value(devicePath).name;
}
static void * AudioThreadRunner(void* params)
{
AudioGrabberLinux* This = static_cast<AudioGrabberLinux*>(params);
Debug(This->getLog(), "Audio Thread Started");
snd_pcm_sframes_t framesAvailable = 0;
while (This->_isRunning.load(std::memory_order_acquire))
{
snd_pcm_wait(This->_captureDevice, 1000);
if ((framesAvailable = snd_pcm_avail(This->_captureDevice)) > 0)
This->processAudioBuffer(framesAvailable);
sched_yield();
}
Debug(This->getLog(), "Audio Thread Shutting Down");
return nullptr;
}