Debian Codegraph @

src/libout123/modules/qsa.c

raw
1 /*
2 qsa: sound output with QNX Sound Architecture 0.5.2 API
3
4 copyright 2013 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6
7 written by Mike Gorchak <[email protected]>
8 */
9
10 #include "../out123_int.h"
11 #include <errno.h>
12
13 #include <stdint.h>
14 #include <sys/asoundlib.h>
15
16 #include "../../common/debug.h"
17
18 typedef struct _qsa_mp_map
19 {
20 uint32_t qsa_format;
21 int mp_format;
22 } qsa_mp_map_t;
23
24 /* in order best format first */
25 qsa_mp_map_t format_map[]=
26 {
27 #ifdef WORDS_BIGENDIAN
28 {SND_PCM_SFMT_FLOAT64_BE, MPG123_ENC_FLOAT_64 },
29 {SND_PCM_SFMT_FLOAT_BE, MPG123_ENC_FLOAT_32 },
30 {SND_PCM_SFMT_S32_BE, MPG123_ENC_SIGNED_32 },
31 {SND_PCM_SFMT_U32_BE, MPG123_ENC_UNSIGNED_32 },
32 {SND_PCM_SFMT_S24_BE, MPG123_ENC_SIGNED_24 },
33 {SND_PCM_SFMT_U24_BE, MPG123_ENC_UNSIGNED_24 },
34 {SND_PCM_SFMT_S16_BE, MPG123_ENC_SIGNED_16 },
35 {SND_PCM_SFMT_U16_BE, MPG123_ENC_UNSIGNED_16 },
36 #else
37 {SND_PCM_SFMT_FLOAT64_LE, MPG123_ENC_FLOAT_64 },
38 {SND_PCM_SFMT_FLOAT_LE, MPG123_ENC_FLOAT_32 },
39 {SND_PCM_SFMT_S32_LE, MPG123_ENC_SIGNED_32 },
40 {SND_PCM_SFMT_U32_LE, MPG123_ENC_UNSIGNED_32 },
41 {SND_PCM_SFMT_S24_LE, MPG123_ENC_SIGNED_24 },
42 {SND_PCM_SFMT_U24_LE, MPG123_ENC_UNSIGNED_24 },
43 {SND_PCM_SFMT_S16_LE, MPG123_ENC_SIGNED_16 },
44 {SND_PCM_SFMT_U16_LE, MPG123_ENC_UNSIGNED_16 },
45 #endif
46 {SND_PCM_SFMT_U8, MPG123_ENC_UNSIGNED_8 },
47 {SND_PCM_SFMT_S8, MPG123_ENC_SIGNED_8 },
48 {SND_PCM_SFMT_A_LAW, MPG123_ENC_ALAW_8 },
49 {SND_PCM_SFMT_MU_LAW, MPG123_ENC_ULAW_8 },
50 {0, 0 },
51 };
52
53 typedef struct _qsa_internal
54 {
55 int cardno;
56 int deviceno;
57 snd_pcm_t* audio_handle;
58 snd_pcm_channel_params_t cpars;
59 } qsa_internal_t;
60
61 static int open_qsa(out123_handle* ao)
62 {
63 int status;
64 int cardno;
65 int deviceno;
66 int it;
67 snd_pcm_t* audio_handle;
68 qsa_internal_t* userptr;
69
70 ao->userptr=NULL;
71
72 status=snd_pcm_open_preferred(&audio_handle, &cardno, &deviceno, SND_PCM_OPEN_PLAYBACK);
73 if (status<0)
74 {
75 return FALSE;
76 }
77
78 status=snd_pcm_plugin_set_disable(audio_handle, PLUGIN_DISABLE_MMAP);
79 if (status<0)
80 {
81 return FALSE;
82 }
83
84 userptr=calloc(1, sizeof(qsa_internal_t));
85 if (userptr==NULL)
86 {
87 return FALSE;
88 }
89 ao->userptr=userptr;
90 userptr->audio_handle=audio_handle;
91 userptr->cardno=cardno;
92 userptr->deviceno=deviceno;
93
94 memset(&userptr->cpars, 0, sizeof(userptr->cpars));
95
96 userptr->cpars.channel=SND_PCM_CHANNEL_PLAYBACK;
97 userptr->cpars.mode=SND_PCM_MODE_BLOCK;
98 userptr->cpars.start_mode=SND_PCM_START_DATA;
99 userptr->cpars.stop_mode=SND_PCM_STOP_STOP;
100 userptr->cpars.format.format=0;
101 it=0;
102 do {
103 if ((format_map[it].qsa_format==0) && (format_map[it].mp_format==0))
104 {
105 break;
106 }
107 if (ao->format==format_map[it].mp_format)
108 {
109 userptr->cpars.format.format=format_map[it].qsa_format;
110 break;
111 }
112 it++;
113 } while(1);
114 userptr->cpars.format.interleave=1;
115 userptr->cpars.format.rate=ao->rate;
116 userptr->cpars.format.voices=ao->channels;
117 userptr->cpars.buf.block.frag_size=4096;
118 userptr->cpars.buf.block.frags_min=8;
119 userptr->cpars.buf.block.frags_max=16;
120
121 if ((ao->channels!=-1) && (ao->rate!=-1))
122 {
123 status=snd_pcm_plugin_params(userptr->audio_handle, &userptr->cpars);
124 if (status<0)
125 {
126 return FALSE;
127 }
128
129 status=snd_pcm_plugin_prepare(userptr->audio_handle, SND_PCM_CHANNEL_PLAYBACK);
130 if (status<0)
131 {
132 return FALSE;
133 }
134 }
135
136 return TRUE;
137 }
138
139 static int get_formats_qsa(out123_handle* ao)
140 {
141 qsa_internal_t* userptr;
142 int status;
143 int it=0;
144
145 userptr=ao->userptr;
146 if (userptr!=NULL);
147 {
148 userptr->cpars.format.rate=ao->rate;
149 userptr->cpars.format.voices=ao->channels;
150 it=0;
151 do {
152 if ((format_map[it].qsa_format==0) && (format_map[it].mp_format==0))
153 {
154 break;
155 }
156 userptr->cpars.format.format=format_map[it].qsa_format;
157 status=snd_pcm_plugin_params(userptr->audio_handle, &userptr->cpars);
158 if (status<0)
159 {
160 it++;
161 }
162 else
163 {
164 return format_map[it].mp_format;
165 }
166 } while(1);
167 }
168
169 return 0;
170 }
171
172 static int write_qsa(out123_handle* ao, unsigned char* buf, int bytes)
173 {
174 int written;
175 int status;
176 snd_pcm_channel_status_t cstatus;
177 qsa_internal_t* userptr;
178
179 userptr=ao->userptr;
180 if (userptr!=NULL);
181 {
182 written=snd_pcm_plugin_write(userptr->audio_handle, buf, bytes);
183 if (written!=bytes)
184 {
185 /* Check if samples playback got stuck somewhere in hardware or in */
186 /* the audio device driver */
187 if ((errno==EAGAIN) && (written==0))
188 {
189 return 0;
190 }
191 if ((errno==EINVAL) || (errno==EIO))
192 {
193 memset(&cstatus, 0, sizeof(cstatus));
194 cstatus.channel=SND_PCM_CHANNEL_PLAYBACK;
195 status=snd_pcm_plugin_status(userptr->audio_handle, &cstatus);
196 if (status>0)
197 {
198 return 0;
199 }
200 if ((cstatus.status == SND_PCM_STATUS_UNDERRUN) ||
201 (cstatus.status == SND_PCM_STATUS_READY))
202 {
203 status=snd_pcm_plugin_prepare(userptr->audio_handle, SND_PCM_CHANNEL_PLAYBACK);
204 if (status<0)
205 {
206 return 0;
207 }
208 }
209 }
210 }
211 }
212
213 return written;
214 }
215
216 static void flush_qsa(out123_handle* ao)
217 {
218 qsa_internal_t* userptr;
219
220 userptr=ao->userptr;
221 if (userptr!=NULL);
222 {
223 snd_pcm_playback_flush(userptr->audio_handle);
224 }
225 }
226
227 static int close_qsa(out123_handle* ao)
228 {
229 qsa_internal_t* userptr;
230
231 userptr=ao->userptr;
232 if (userptr!=NULL);
233 {
234 snd_pcm_close(userptr->audio_handle);
235 free(ao->userptr);
236 }
237
238 return TRUE;
239 }
240
241 static int init_qsa(out123_handle* ao)
242 {
243 if (ao==NULL) return -1;
244
245 /* Set callbacks */
246 ao->open = open_qsa;
247 ao->flush = flush_qsa;
248 ao->write = write_qsa;
249 ao->get_formats = get_formats_qsa;
250 ao->close = close_qsa;
251
252 /* Success */
253 return 0;
254 }
255
256 /*
257 Module information data structure
258 */
259 mpg123_module_t mpg123_output_module_info = {
260 /* api_version */ MPG123_MODULE_API_VERSION,
261 /* name */ "qsa",
262 /* description */ "Output audio using QNX Sound Architecture (QSA).",
263 /* revision */ "$Rev:$",
264 /* handle */ NULL,
265 /* init_output */ init_qsa,
266 };