Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow application to set ALSA devices manually #4133

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions pjmedia/include/pjmedia-audiodev/alsa.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2024 Teluu Inc. (http://www.teluu.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __PJMEDIA_AUDIODEV_ALSA_H__
#define __PJMEDIA_AUDIODEV_ALSA_H__

/**
* @file pjmedia-audiodev/alsa.h
* @brief ALSA Audio Device.
*/

#include <pjmedia/audiodev.h>

/**
* @defgroup PJMED_AUDDEV_ALSA ALSA Audio Device
* @ingroup audio_device_api
* @brief ALSA specific Audio Device API
* @{
*
* This section describes specific functions for ALSA audio devices.
* Application can use @ref PJMEDIA_AUDIODEV_API API to manipulate
* the ALSA audio device.
*
*/

PJ_BEGIN_DECL


/**
* Manually set ALSA devices. This function will remove all devices registered
* in the factory and register the specified devices.
*
* Note that by default the library will automatically try to enumerate and
* register the ALSA devices during factory initialization. Application can
* override the registered devices using this function.
*
* If application wish to let the library do the device enumeration again,
* just call this function with zero device, i.e: \a count is set to zero.
*
* @param af The ALSA factory, or NULL to use the default.
* @param count The number of ALSA device names.
* @param names The ALSA device names to be registered.
*
* @return PJ_SUCCESS on success.
*/
PJ_DECL(pj_status_t) pjmedia_aud_alsa_set_devices(pjmedia_aud_dev_factory *af,
unsigned count,
const char* names[]);



PJ_END_DECL


/**
* @}
*/

#endif /* __PJMEDIA_AUDIODEV_ALSA_H__ */

92 changes: 73 additions & 19 deletions pjmedia/src/pjmedia-audiodev/alsa_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <pj/os.h>
#include <pj/pool.h>
#include <pjmedia/errno.h>
#include <pjmedia-audiodev/alsa.h>

#if defined(PJMEDIA_AUDIO_DEV_HAS_ALSA) && PJMEDIA_AUDIO_DEV_HAS_ALSA

Expand Down Expand Up @@ -101,8 +102,13 @@ struct alsa_factory
pjmedia_aud_dev_info devs[MAX_DEVICES];
char pb_mixer_name[MAX_MIX_NAME_LEN];
char cap_mixer_name[MAX_MIX_NAME_LEN];

unsigned custom_dev_cnt;
pj_str_t custom_dev[MAX_DEVICES];
};

static pjmedia_aud_dev_factory *default_factory;

struct alsa_stream
{
pjmedia_aud_stream base;
Expand Down Expand Up @@ -352,6 +358,9 @@ static pj_status_t alsa_factory_init(pjmedia_aud_dev_factory *f)
if (PJ_SUCCESS != status)
return status;

if (!default_factory)
default_factory = f;

PJ_LOG(4,(THIS_FILE, "ALSA initialized"));
return PJ_SUCCESS;
}
Expand All @@ -362,6 +371,9 @@ static pj_status_t alsa_factory_destroy(pjmedia_aud_dev_factory *f)
{
struct alsa_factory *af = (struct alsa_factory*)f;

if (default_factory == f)
default_factory = NULL;

if (af->pool)
pj_pool_release(af->pool);

Expand All @@ -387,18 +399,14 @@ static pj_status_t alsa_factory_refresh(pjmedia_aud_dev_factory *f)

TRACE_((THIS_FILE, "pjmedia_snd_init: Enumerate sound devices"));

if (af->pool != NULL) {
pj_pool_release(af->pool);
af->pool = NULL;
}

af->pool = pj_pool_create(af->pf, "alsa_aud", 256, 256, NULL);
af->dev_cnt = 0;

/* Enumerate sound devices */
err = snd_device_name_hint(-1, "pcm", (void***)&hints);
if (err != 0)
return PJMEDIA_EAUD_SYSERR;
if (af->custom_dev_cnt == 0) {
/* Enumerate sound devices */
err = snd_device_name_hint(-1, "pcm", (void***)&hints);
if (err != 0)
return PJMEDIA_EAUD_SYSERR;
}

#if ENABLE_TRACING
snd_lib_error_set_handler(alsa_error_handler);
Expand All @@ -407,15 +415,22 @@ static pj_status_t alsa_factory_refresh(pjmedia_aud_dev_factory *f)
snd_lib_error_set_handler(null_alsa_error_handler);
#endif

n = hints;
while (*n != NULL) {
char *name = snd_device_name_get_hint(*n, "NAME");
if (name != NULL) {
if (0 != strcmp("null", name))
add_dev(af, name);
free(name);
if (af->custom_dev_cnt == 0) {
n = hints;
while (*n != NULL) {
char *name = snd_device_name_get_hint(*n, "NAME");
if (name != NULL) {
if (0 != strcmp("null", name))
add_dev(af, name);
free(name);
}
n++;
}
} else {
unsigned i;
for (i = 0; i < af->custom_dev_cnt; ++i) {
add_dev(af, af->custom_dev[i].ptr);
}
n++;
}

/* Get the mixer name */
Expand All @@ -426,7 +441,9 @@ static pj_status_t alsa_factory_refresh(pjmedia_aud_dev_factory *f)
*/
snd_lib_error_set_handler(alsa_error_handler);

err = snd_device_name_free_hint((void**)hints);
if (af->custom_dev_cnt == 0) {
err = snd_device_name_free_hint((void**)hints);
}

PJ_LOG(4,(THIS_FILE, "ALSA driver found %d devices", af->dev_cnt));

Expand Down Expand Up @@ -1155,4 +1172,41 @@ static pj_status_t alsa_stream_destroy (pjmedia_aud_stream *s)
return PJ_SUCCESS;
}


/*
* Manually set ALSA devices.
*/
PJ_DEF(pj_status_t) pjmedia_aud_alsa_set_devices( pjmedia_aud_dev_factory *f,
unsigned count,
const char* names[] )
{
struct alsa_factory *af = (struct alsa_factory*)f;

if (!af)
af = (struct alsa_factory*)default_factory;

PJ_ASSERT_RETURN(af, PJ_EINVAL);
PJ_ASSERT_RETURN(count <= MAX_DEVICES, PJ_ETOOMANY);

PJ_LOG(4,(THIS_FILE, "ALSA driver set manually %d devices", count));

if (af->pool != NULL) {
pj_pool_release(af->pool);
af->pool = NULL;
}

if (count > 0) {
unsigned i;
af->pool = pj_pool_create(af->pf, "alsa_custom_dev", 256, 256, NULL);

for (i = 0; i < count; ++i) {
pj_strdup2_with_null(af->pool, &af->custom_dev[i], names[i]);
}
}
af->custom_dev_cnt = count;

return pjmedia_aud_dev_refresh();
}


#endif /* PJMEDIA_AUDIO_DEV_HAS_ALSA */
Loading