xref: /aosp_15_r20/external/tinyalsa_new/examples/sndcardparser/sample_sndcardparser.c (revision 02e95f1a335b55495d41ca67eaf42361f13704fa)
1 /* sample_sndcardparser.c
2 **
3 ** Copyright (c) 2021, The Linux Foundation. All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions are
7 ** met:
8 **   * Redistributions of source code must retain the above copyright
9 **     notice, this list of conditions and the following disclaimer.
10 **   * Redistributions in binary form must reproduce the above
11 **     copyright notice, this list of conditions and the following
12 **     disclaimer in the documentation and/or other materials provided
13 **     with the distribution.
14 **   * Neither the name of The Linux Foundation nor the names of its
15 **     contributors may be used to endorse or promote products derived
16 **     from this software without specific prior written permission.
17 **
18 ** THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
19 ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
21 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 ** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 ** BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 ** OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28 ** IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 **/
30 
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
37 
38 #define VIRTUAL_SND_CARD_ID 100
39 #define MAX_PATH 256
40 #define BUF_SIZE 1024
41 
42 enum snd_node_type {
43     NODE_TYPE_HW = 0,
44     NODE_TYPE_PLUGIN,
45     NODE_TYPE_INVALID,
46 };
47 
48 enum {
49     NODE_PCM,
50     NODE_COMPR,
51     NODE_MIXER,
52     NODE_MAX,
53 };
54 
55 struct snd_node_ops {
56     /** Function pointer to get card definition */
57     void* (*open_card)(unsigned int card);
58     /** Function pointer to release card definition */
59     void (*close_card)(void *card);
60     /** Get interger type properties from device definition */
61     int (*get_int)(void *node, const char *prop, int *val);
62     /** Get string type properties from device definition */
63     int (*get_str)(void *node, const char *prop, char **val);
64     /** Function pointer to get mixer definition */
65     void* (*get_mixer)(void *card);
66     /** Function pointer to get PCM definition */
67     void* (*get_pcm)(void *card, unsigned int id);
68     /** Function pointer to get COMPRESS definition */
69     void* (*get_compress)(void *card, unsigned int id);
70 };
71 
72 struct snd_dev_def {
73     unsigned int device;
74     int type;
75     const char *name;
76     const char *so_name;
77     int playback; //used only for pcm node
78     int capture;  //used only for pcm node
79     /* add custom props here */
80 };
81 
82 struct snd_dev_def_card {
83     unsigned int card;
84     char *name;
85 
86     /* child device details */
87     int num_pcm_nodes;
88     struct snd_dev_def *pcm_dev_def;
89 
90     struct snd_dev_def *mixer_dev_def;
91 };
92 
93 struct snd_dev_def pcm_devs[] = {
94     {100, NODE_TYPE_PLUGIN, "PCM100", "libtinyalsav2_example_plugin_pcm.so", 1, 0},
95     /* Add other plugin info here */
96 };
97 
98 struct snd_dev_def mixer_dev =
99     {VIRTUAL_SND_CARD_ID, NODE_TYPE_PLUGIN, "virtual-snd-card", "libtinyalsav2_example_plugin_mixer.so", 0, 0};
100 
snd_card_def_open_card(unsigned int card)101 void *snd_card_def_open_card(unsigned int card)
102 {
103     struct snd_dev_def_card *card_def = NULL;
104     struct snd_dev_def *pcm_dev_def = NULL;
105     struct snd_dev_def *mixer_dev_def = NULL;
106     int num_pcm = ARRAY_SIZE(pcm_devs);
107     int i;
108 
109     if (card != VIRTUAL_SND_CARD_ID)
110         return NULL;
111 
112     card_def = calloc(1, sizeof(struct snd_dev_def_card));
113     if (!card_def)
114         return card_def;
115 
116     card_def->card = card;
117     card_def->name = strdup("virtual-snd-card");
118 
119     /* fill pcm device node info */
120     card_def->num_pcm_nodes = num_pcm;
121     pcm_dev_def = calloc(num_pcm, sizeof(struct snd_dev_def));
122     if (!pcm_dev_def)
123         goto free_card_def;
124 
125     for (i = 0; i < num_pcm; i++)
126         memcpy(&pcm_dev_def[i], &pcm_devs[i], sizeof(struct snd_dev_def));
127 
128     card_def->pcm_dev_def = pcm_dev_def;
129 
130     /* fill mixer device node info */
131     mixer_dev_def = calloc(1, sizeof(struct snd_dev_def));
132     if (!mixer_dev_def)
133         goto free_pcm_dev;
134 
135     memcpy(mixer_dev_def, &mixer_dev, sizeof(struct snd_dev_def));
136 
137     card_def->mixer_dev_def = mixer_dev_def;
138     return card_def;
139 free_pcm_dev:
140     free(pcm_dev_def);
141 free_card_def:
142     free(card_def->name);
143     free(card_def);
144     return NULL;
145 }
146 
snd_card_def_close_card(void * card_node)147 void snd_card_def_close_card(void *card_node)
148 {
149     struct snd_dev_def_card *defs = (struct snd_dev_def_card *)card_node;
150     struct snd_dev_def *pcm_dev_def = NULL;
151     struct snd_dev_def *mixer_dev_def = NULL;
152 
153     if (!defs)
154         return;
155 
156     pcm_dev_def = defs->pcm_dev_def;
157     if (pcm_dev_def)
158         free(pcm_dev_def);
159 
160     mixer_dev_def = defs->mixer_dev_def;
161     if (!mixer_dev_def)
162          goto free_defs;
163 
164     free(mixer_dev_def);
165 free_defs:
166     free(defs->name);
167     free(defs);
168 }
169 
snd_card_def_get_node(void * card_node,unsigned int id,int type)170 void *snd_card_def_get_node(void *card_node, unsigned int id, int type)
171 {
172     struct snd_dev_def_card *card_def = (struct snd_dev_def_card *)card_node;
173     struct snd_dev_def *dev_def = NULL;
174     int i;
175 
176     if (!card_def)
177         return NULL;
178 
179     if (type >= NODE_MAX)
180         return NULL;
181 
182     if (type == NODE_MIXER)
183         return card_def->mixer_dev_def;
184 
185     if (type == NODE_PCM)
186         dev_def = card_def->pcm_dev_def;
187 
188     for (i = 0; i < card_def->num_pcm_nodes; i++) {
189         if (dev_def[i].device == id) {
190             return &dev_def[i];
191         }
192     }
193 
194     return NULL;
195 }
196 
snd_card_def_get_int(void * node,const char * prop,int * val)197 int snd_card_def_get_int(void *node, const char *prop, int *val)
198 {
199     struct snd_dev_def *dev_def = (struct snd_dev_def *)node;
200     int ret = -EINVAL;
201 
202     if (!dev_def || !prop || !val)
203         return ret;
204 
205     if (!strcmp(prop, "type")) {
206         *val = dev_def->type;
207         return 0;
208     } else if (!strcmp(prop, "id")) {
209         *val = dev_def->device;
210         return 0;
211     } else if (!strcmp(prop, "playback")) {
212         *val = dev_def->playback;
213         return 0;
214     } else if (!strcmp(prop, "capture")) {
215         *val = dev_def->capture;
216         return 0;
217     }
218 
219     return ret;
220 }
221 
snd_card_def_get_str(void * node,const char * prop,char ** val)222 int snd_card_def_get_str(void *node, const char *prop, char **val)
223 {
224     struct snd_dev_def *dev_def = (struct snd_dev_def *)node;
225     int ret = -EINVAL;
226 
227     if (!dev_def || !prop)
228         return ret;
229 
230     if (!strcmp(prop, "so-name")) {
231         if (dev_def->so_name) {
232             *val = (char *)dev_def->so_name;
233             return 0;
234         }
235     }
236 
237     if (!strcmp(prop, "name")) {
238         if (dev_def->name) {
239             *val = (char *)dev_def->name;
240             return 0;
241         }
242     }
243 
244     return ret;
245 }
246 
snd_card_def_get_pcm(void * card_node,unsigned int id)247 void *snd_card_def_get_pcm(void *card_node, unsigned int id)
248 {
249     return snd_card_def_get_node(card_node, id, NODE_PCM);
250 }
251 
snd_card_def_get_compress(void * card_node,unsigned int id)252 void *snd_card_def_get_compress(void *card_node, unsigned int id)
253 {
254     return snd_card_def_get_node(card_node, id, NODE_COMPR);
255 }
256 
snd_card_def_get_mixer(void * card_node)257 void *snd_card_def_get_mixer(void *card_node)
258 {
259     return snd_card_def_get_node(card_node, 1, NODE_MIXER);
260 }
261 
262 struct snd_node_ops snd_card_ops = {
263     .open_card = snd_card_def_open_card,
264     .close_card = snd_card_def_close_card,
265     .get_int = snd_card_def_get_int,
266     .get_str = snd_card_def_get_str,
267     .get_pcm = snd_card_def_get_pcm,
268     .get_compress = snd_card_def_get_compress,
269     .get_mixer = snd_card_def_get_mixer,
270 };
271