Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-audiodev/null_dev.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <pjmedia-audiodev/audiodev_imp.h>
19 #include <pj/assert.h>
20 #include <pj/log.h>
21 #include <pj/os.h>
22
23 #if PJMEDIA_AUDIO_DEV_HAS_NULL_AUDIO
24
25 #define THIS_FILE "null_dev.c"
26
27 /* null_audio device info */
28 struct null_audio_dev_info
29 {
30 pjmedia_aud_dev_info info;
31 unsigned dev_id;
32 };
33
34 /* null_audio factory */
35 struct null_audio_factory
36 {
37 pjmedia_aud_dev_factory base;
38 pj_pool_t *pool;
39 pj_pool_factory *pf;
40
41 unsigned dev_count;
42 struct null_audio_dev_info *dev_info;
43 };
44
45 /* Sound stream. */
46 struct null_audio_stream
47 {
48 pjmedia_aud_stream base; /**< Base stream */
49 pjmedia_aud_param param; /**< Settings */
50 pj_pool_t *pool; /**< Memory pool. */
51
52 pjmedia_aud_rec_cb rec_cb; /**< Capture callback. */
53 pjmedia_aud_play_cb play_cb; /**< Playback callback. */
54 void *user_data; /**< Application data. */
55 };
56
57
58 /* Prototypes */
59 static pj_status_t null_factory_init(pjmedia_aud_dev_factory *f);
60 static pj_status_t null_factory_destroy(pjmedia_aud_dev_factory *f);
61 static pj_status_t null_factory_refresh(pjmedia_aud_dev_factory *f);
62 static unsigned null_factory_get_dev_count(pjmedia_aud_dev_factory *f);
63 static pj_status_t null_factory_get_dev_info(pjmedia_aud_dev_factory *f,
64 unsigned index,
65 pjmedia_aud_dev_info *info);
66 static pj_status_t null_factory_default_param(pjmedia_aud_dev_factory *f,
67 unsigned index,
68 pjmedia_aud_param *param);
69 static pj_status_t null_factory_create_stream(pjmedia_aud_dev_factory *f,
70 const pjmedia_aud_param *param,
71 pjmedia_aud_rec_cb rec_cb,
72 pjmedia_aud_play_cb play_cb,
73 void *user_data,
74 pjmedia_aud_stream **p_aud_strm);
75
76 static pj_status_t null_stream_get_param(pjmedia_aud_stream *strm,
77 pjmedia_aud_param *param);
78 static pj_status_t null_stream_get_cap(pjmedia_aud_stream *strm,
79 pjmedia_aud_dev_cap cap,
80 void *value);
81 static pj_status_t null_stream_set_cap(pjmedia_aud_stream *strm,
82 pjmedia_aud_dev_cap cap,
83 const void *value);
84 static pj_status_t null_stream_start(pjmedia_aud_stream *strm);
85 static pj_status_t null_stream_stop(pjmedia_aud_stream *strm);
86 static pj_status_t null_stream_destroy(pjmedia_aud_stream *strm);
87
88 /* Operations */
89 static pjmedia_aud_dev_factory_op factory_op =
90 {
91 &null_factory_init,
92 &null_factory_destroy,
93 &null_factory_get_dev_count,
94 &null_factory_get_dev_info,
95 &null_factory_default_param,
96 &null_factory_create_stream,
97 &null_factory_refresh
98 };
99
100 static pjmedia_aud_stream_op stream_op =
101 {
102 &null_stream_get_param,
103 &null_stream_get_cap,
104 &null_stream_set_cap,
105 &null_stream_start,
106 &null_stream_stop,
107 &null_stream_destroy
108 };
109
110
111 /****************************************************************************
112 * Factory operations
113 */
114 /*
115 * Init null_audio audio driver.
116 */
117 pjmedia_aud_dev_factory* pjmedia_null_audio_factory(pj_pool_factory *pf)
118 {
119 struct null_audio_factory *f;
120 pj_pool_t *pool;
121
122 pool = pj_pool_create(pf, "null audio", 1000, 1000, NULL);
123 f = PJ_POOL_ZALLOC_T(pool, struct null_audio_factory);
124 f->pf = pf;
125 f->pool = pool;
126 f->base.op = &factory_op;
127
128 return &f->base;
129 }
130
131
132 /* API: init factory */
133 static pj_status_t null_factory_init(pjmedia_aud_dev_factory *f)
134 {
135 struct null_audio_factory *nf = (struct null_audio_factory*)f;
136 struct null_audio_dev_info *ndi;
137
138 /* Initialize input and output devices here */
139 nf->dev_count = 1;
140 nf->dev_info = (struct null_audio_dev_info*)
141 pj_pool_calloc(nf->pool, nf->dev_count,
142 sizeof(struct null_audio_dev_info));
143 ndi = &nf->dev_info[0];
144 pj_bzero(ndi, sizeof(*ndi));
145 pj_ansi_strxcpy(ndi->info.name, "null device", sizeof(ndi->info.name));
146 pj_ansi_strxcpy(ndi->info.driver, "null", sizeof(ndi->info.driver));
147 ndi->info.input_count = 1;
148 ndi->info.output_count = 1;
149 ndi->info.default_samples_per_sec = 16000;
150 /* Set the device capabilities here */
151 ndi->info.caps = 0;
152
153 PJ_LOG(4, (THIS_FILE, "null audio initialized"));
154
155 return PJ_SUCCESS;
156 }
157
158 /* API: destroy factory */
159 static pj_status_t null_factory_destroy(pjmedia_aud_dev_factory *f)
160 {
161 struct null_audio_factory *nf = (struct null_audio_factory*)f;
162
163 pj_pool_safe_release(&nf->pool);
164
165 return PJ_SUCCESS;
166 }
167
168 /* API: refresh the list of devices */
169 static pj_status_t null_factory_refresh(pjmedia_aud_dev_factory *f)
170 {
171 PJ_UNUSED_ARG(f);
172 return PJ_SUCCESS;
173 }
174
175 /* API: get number of devices */
176 static unsigned null_factory_get_dev_count(pjmedia_aud_dev_factory *f)
177 {
178 struct null_audio_factory *nf = (struct null_audio_factory*)f;
179 return nf->dev_count;
180 }
181
182 /* API: get device info */
183 static pj_status_t null_factory_get_dev_info(pjmedia_aud_dev_factory *f,
184 unsigned index,
185 pjmedia_aud_dev_info *info)
186 {
187 struct null_audio_factory *nf = (struct null_audio_factory*)f;
188
189 PJ_ASSERT_RETURN(index < nf->dev_count, PJMEDIA_EAUD_INVDEV);
190
191 pj_memcpy(info, &nf->dev_info[index].info, sizeof(*info));
192
193 return PJ_SUCCESS;
194 }
195
196 /* API: create default device parameter */
197 static pj_status_t null_factory_default_param(pjmedia_aud_dev_factory *f,
198 unsigned index,
199 pjmedia_aud_param *param)
200 {
201 struct null_audio_factory *nf = (struct null_audio_factory*)f;
202 struct null_audio_dev_info *di = &nf->dev_info[index];
203
204 PJ_ASSERT_RETURN(index < nf->dev_count, PJMEDIA_EAUD_INVDEV);
205
206 pj_bzero(param, sizeof(*param));
207 if (di->info.input_count && di->info.output_count) {
208 param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
209 param->rec_id = index;
210 param->play_id = index;
211 } else if (di->info.input_count) {
212 param->dir = PJMEDIA_DIR_CAPTURE;
213 param->rec_id = index;
214 param->play_id = PJMEDIA_AUD_INVALID_DEV;
215 } else if (di->info.output_count) {
216 param->dir = PJMEDIA_DIR_PLAYBACK;
217 param->play_id = index;
218 param->rec_id = PJMEDIA_AUD_INVALID_DEV;
219 } else {
220 return PJMEDIA_EAUD_INVDEV;
221 }
222
223 /* Set the mandatory settings here */
224 /* The values here are just some examples */
225 param->clock_rate = di->info.default_samples_per_sec;
226 param->channel_count = 1;
227 param->samples_per_frame = di->info.default_samples_per_sec * 20 / 1000;
228 param->bits_per_sample = 16;
229
230 /* Set the device capabilities here */
231 param->flags = 0;
232
233 return PJ_SUCCESS;
234 }
235
236 /* API: create stream */
237 static pj_status_t null_factory_create_stream(pjmedia_aud_dev_factory *f,
238 const pjmedia_aud_param *param,
239 pjmedia_aud_rec_cb rec_cb,
240 pjmedia_aud_play_cb play_cb,
241 void *user_data,
242 pjmedia_aud_stream **p_aud_strm)
243 {
244 struct null_audio_factory *nf = (struct null_audio_factory*)f;
245 pj_pool_t *pool;
246 struct null_audio_stream *strm;
247
248 /* Create and Initialize stream descriptor */
249 pool = pj_pool_create(nf->pf, "null_audio-dev", 1000, 1000, NULL);
250 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
251
252 strm = PJ_POOL_ZALLOC_T(pool, struct null_audio_stream);
253 pj_memcpy(&strm->param, param, sizeof(*param));
254 strm->pool = pool;
255 strm->rec_cb = rec_cb;
256 strm->play_cb = play_cb;
257 strm->user_data = user_data;
258
259 /* Create player stream here */
260 if (param->dir & PJMEDIA_DIR_PLAYBACK) {
261 }
262
263 /* Create capture stream here */
264 if (param->dir & PJMEDIA_DIR_CAPTURE) {
265 }
266
267 /* Apply the remaining settings */
268 /* Below is an example if you want to set the output volume */
269 if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING) {
270 null_stream_set_cap(&strm->base,
271 PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
272 &param->output_vol);
273 }
274
275 /* Done */
276 strm->base.op = &stream_op;
277 *p_aud_strm = &strm->base;
278
279 return PJ_SUCCESS;
280 }
281
282 /* API: Get stream info. */
283 static pj_status_t null_stream_get_param(pjmedia_aud_stream *s,
284 pjmedia_aud_param *pi)
285 {
286 struct null_audio_stream *strm = (struct null_audio_stream*)s;
287
288 PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
289
290 pj_memcpy(pi, &strm->param, sizeof(*pi));
291
292 /* Example: Update the volume setting */
293 if (null_stream_get_cap(s, PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
294 &pi->output_vol) == PJ_SUCCESS)
295 {
296 pi->flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING;
297 }
298
299 return PJ_SUCCESS;
300 }
301
302 /* API: get capability */
303 static pj_status_t null_stream_get_cap(pjmedia_aud_stream *s,
304 pjmedia_aud_dev_cap cap,
305 void *pval)
306 {
307 struct null_audio_stream *strm = (struct null_audio_stream*)s;
308
309 PJ_UNUSED_ARG(strm);
310
311 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
312
313 /* Example: Get the output's volume setting */
314 if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING)
315 {
316 /* Output volume setting */
317 *(unsigned*)pval = 0; // retrieve output device's volume here
318 return PJ_SUCCESS;
319 } else {
320 return PJMEDIA_EAUD_INVCAP;
321 }
322 }
323
324 /* API: set capability */
325 static pj_status_t null_stream_set_cap(pjmedia_aud_stream *s,
326 pjmedia_aud_dev_cap cap,
327 const void *pval)
328 {
329 struct null_audio_stream *strm = (struct null_audio_stream*)s;
330
331 PJ_UNUSED_ARG(strm);
332
333 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
334
335 /* Example */
336 if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING)
337 {
338 /* Output volume setting */
339 // set output's volume level here
340 return PJ_SUCCESS;
341 }
342
343 return PJMEDIA_EAUD_INVCAP;
344 }
345
346 /* API: Start stream. */
347 static pj_status_t null_stream_start(pjmedia_aud_stream *strm)
348 {
349 struct null_audio_stream *stream = (struct null_audio_stream*)strm;
350
351 PJ_UNUSED_ARG(stream);
352
353 PJ_LOG(4, (THIS_FILE, "Starting null audio stream"));
354
355 return PJ_SUCCESS;
356 }
357
358 /* API: Stop stream. */
359 static pj_status_t null_stream_stop(pjmedia_aud_stream *strm)
360 {
361 struct null_audio_stream *stream = (struct null_audio_stream*)strm;
362
363 PJ_UNUSED_ARG(stream);
364
365 PJ_LOG(4, (THIS_FILE, "Stopping null audio stream"));
366
367 return PJ_SUCCESS;
368 }
369
370
371 /* API: Destroy stream. */
372 static pj_status_t null_stream_destroy(pjmedia_aud_stream *strm)
373 {
374 struct null_audio_stream *stream = (struct null_audio_stream*)strm;
375
376 PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL);
377
378 null_stream_stop(strm);
379
380 pj_pool_release(stream->pool);
381
382 return PJ_SUCCESS;
383 }
384
385 #endif /* PJMEDIA_AUDIO_DEV_HAS_NULL_AUDIO */