1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <dlfcn.h>
17#include <stdio.h>
18#include <alsa/asoundlib.h>
19
20#define  D(...)  fprintf(stderr,__VA_ARGS__)
21#define  STRINGIFY(x)  _STRINGIFY(x)
22#define _STRINGIFY(x)  #x
23
24#define  DYN_SYMBOLS   \
25    DYN_FUNCTION(size_t,snd_pcm_sw_params_sizeof,(void))    \
26    DYN_FUNCTION(int,snd_pcm_hw_params_current,(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)) \
27    DYN_FUNCTION(int,snd_pcm_sw_params_set_start_threshold,(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val))  \
28    DYN_FUNCTION(int,snd_pcm_sw_params,(snd_pcm_t *pcm, snd_pcm_sw_params_t *params))  \
29    DYN_FUNCTION(int,snd_pcm_sw_params_current,(snd_pcm_t *pcm, snd_pcm_sw_params_t *params)) \
30    DYN_FUNCTION(size_t,snd_pcm_hw_params_sizeof,(void))  \
31    DYN_FUNCTION(int,snd_pcm_hw_params_any,(snd_pcm_t *pcm, snd_pcm_hw_params_t *params))  \
32    DYN_FUNCTION(int,snd_pcm_hw_params_set_access,(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t _access))  \
33    DYN_FUNCTION(int,snd_pcm_hw_params_set_format,(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val))  \
34    DYN_FUNCTION(int,snd_pcm_hw_params_set_rate_near,(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir))  \
35    DYN_FUNCTION(int,snd_pcm_hw_params_set_channels_near,(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val))  \
36    DYN_FUNCTION(int,snd_pcm_hw_params_set_buffer_time_near,(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir))  \
37    DYN_FUNCTION(int,snd_pcm_hw_params,(snd_pcm_t *pcm, snd_pcm_hw_params_t *params))  \
38    DYN_FUNCTION(int,snd_pcm_hw_params_get_buffer_size,(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val))  \
39    DYN_FUNCTION(int,snd_pcm_prepare,(snd_pcm_t *pcm))  \
40    DYN_FUNCTION(int,snd_pcm_hw_params_get_period_size,(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir))  \
41    DYN_FUNCTION(int,snd_pcm_hw_params_get_period_size_min,(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir))  \
42    DYN_FUNCTION(int,snd_pcm_hw_params_set_period_size,(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val, int dir))  \
43    DYN_FUNCTION(int,snd_pcm_hw_params_get_buffer_size_min,(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)) \
44    DYN_FUNCTION(int,snd_pcm_hw_params_set_buffer_size,(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val))  \
45    DYN_FUNCTION(int,snd_pcm_hw_params_set_period_time_near,(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir))  \
46    DYN_FUNCTION(snd_pcm_sframes_t,snd_pcm_avail_update,(snd_pcm_t *pcm)) \
47    DYN_FUNCTION(int,snd_pcm_drop,(snd_pcm_t *pcm))  \
48    DYN_FUNCTION(snd_pcm_sframes_t,snd_pcm_writei,(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size))  \
49    DYN_FUNCTION(snd_pcm_sframes_t,snd_pcm_readi,(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size))  \
50    DYN_FUNCTION(snd_pcm_state_t,snd_pcm_state,(snd_pcm_t *pcm))  \
51    DYN_FUNCTION(const char*,snd_strerror,(int errnum)) \
52    DYN_FUNCTION(int,snd_pcm_open,(snd_pcm_t **pcm, const char *name,snd_pcm_stream_t stream, int mode)) \
53    DYN_FUNCTION(int,snd_pcm_close,(snd_pcm_t *pcm))  \
54
55
56
57/* define pointers to library functions we're going to use */
58#define  DYN_FUNCTION(ret,name,sig)   \
59    static ret  (*func_ ## name)sig;
60
61DYN_SYMBOLS
62
63#undef  DYN_FUNCTION
64
65#define func_snd_pcm_hw_params_alloca(ptr) \
66    do { assert(ptr); *ptr = (snd_pcm_hw_params_t *) alloca(func_snd_pcm_hw_params_sizeof()); memset(*ptr, 0, func_snd_pcm_hw_params_sizeof()); } while (0)
67
68#define func_snd_pcm_sw_params_alloca(ptr) \
69    do { assert(ptr); *ptr = (snd_pcm_sw_params_t *) alloca(func_snd_pcm_sw_params_sizeof()); memset(*ptr, 0, func_snd_pcm_sw_params_sizeof()); } while (0)
70
71static void*  alsa_lib;
72
73int  main(void)
74{
75    int  result = 1;
76
77    alsa_lib = dlopen( "libasound.so", RTLD_NOW );
78    if (alsa_lib == NULL)
79        alsa_lib = dlopen( "libasound.so.2", RTLD_NOW );
80
81    if (alsa_lib == NULL) {
82        D("could not find libasound on this system\n");
83        return 1;
84    }
85
86#undef  DYN_FUNCTION
87#define DYN_FUNCTION(ret,name,sig)                                               \
88    do {                                                                         \
89        (func_ ##name) = dlsym( alsa_lib, STRINGIFY(name) );                     \
90        if ((func_##name) == NULL) {                                             \
91            D("could not find %s in libasound\n", STRINGIFY(name)); \
92            goto Fail;                                                           \
93        }                                                                        \
94    } while (0);
95
96    DYN_SYMBOLS
97
98    result = 0;
99    goto Exit;
100
101Fail:
102    D("failed to open library\n");
103
104Exit:
105    dlclose(alsa_lib);
106    return result;
107}
108