Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include <pjmedia-audiodev/audiodev_imp.h> |
| 21 | #include <pj/errno.h> |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 22 | #include <pj/log.h> |
| 23 | #include <pj/string.h> |
| 24 | |
| 25 | #define THIS_FILE "audiodev.c" |
| 26 | |
| 27 | /* Capability names */ |
| 28 | static struct cap_info |
| 29 | { |
| 30 | const char *name; |
| 31 | const char *info; |
| 32 | } cap_infos[] = |
| 33 | { |
| 34 | {"ext-fmt", "Extended/non-PCM format"}, |
| 35 | {"latency-in", "Input latency/buffer size setting"}, |
| 36 | {"latency-out", "Output latency/buffer size setting"}, |
| 37 | {"vol-in", "Input volume setting"}, |
| 38 | {"vol-out", "Output volume setting"}, |
| 39 | {"meter-in", "Input meter"}, |
| 40 | {"meter-out", "Output meter"}, |
| 41 | {"route-in", "Input routing"}, |
| 42 | {"route-out", "Output routing"}, |
| 43 | {"aec", "Accoustic echo cancellation"}, |
| 44 | {"aec-tail", "Tail length setting for AEC"}, |
| 45 | {"vad", "Voice activity detection"}, |
| 46 | {"cng", "Comfort noise generation"}, |
| 47 | {"plg", "Packet loss concealment"} |
| 48 | }; |
| 49 | |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 50 | |
Benny Prijono | 598b01d | 2009-02-18 13:55:03 +0000 | [diff] [blame] | 51 | /* |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 52 | * The device index seen by application and driver is different. |
Benny Prijono | 598b01d | 2009-02-18 13:55:03 +0000 | [diff] [blame] | 53 | * |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 54 | * At application level, device index is index to global list of device. |
| 55 | * At driver level, device index is index to device list on that particular |
| 56 | * factory only. |
Benny Prijono | 598b01d | 2009-02-18 13:55:03 +0000 | [diff] [blame] | 57 | */ |
Benny Prijono | e3ebd55 | 2009-02-18 20:14:15 +0000 | [diff] [blame] | 58 | #define MAKE_DEV_ID(f_id, index) (((f_id & 0xFFFF) << 16) | (index & 0xFFFF)) |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 59 | #define GET_INDEX(dev_id) ((dev_id) & 0xFFFF) |
| 60 | #define GET_FID(dev_id) ((dev_id) >> 16) |
Benny Prijono | 598b01d | 2009-02-18 13:55:03 +0000 | [diff] [blame] | 61 | #define DEFAULT_DEV_ID 0 |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 62 | |
| 63 | |
Benny Prijono | 598b01d | 2009-02-18 13:55:03 +0000 | [diff] [blame] | 64 | /* extern functions to create factories */ |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 65 | pjmedia_aud_dev_factory* pjmedia_pa_factory(pj_pool_factory *pf); |
Benny Prijono | 598b01d | 2009-02-18 13:55:03 +0000 | [diff] [blame] | 66 | pjmedia_aud_dev_factory* pjmedia_wmme_factory(pj_pool_factory *pf); |
| 67 | |
Benny Prijono | e3ebd55 | 2009-02-18 20:14:15 +0000 | [diff] [blame] | 68 | #define MAX_DRIVERS 16 |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 69 | #define MAX_DEVS 64 |
| 70 | |
| 71 | /* typedef for factory creation function */ |
| 72 | typedef pjmedia_aud_dev_factory* (*create_func_ptr)(pj_pool_factory*); |
| 73 | |
| 74 | /* driver structure */ |
| 75 | struct driver |
| 76 | { |
| 77 | create_func_ptr create; /* Creation function. */ |
| 78 | pjmedia_aud_dev_factory *f; /* Factory instance. */ |
| 79 | char name[32]; /* Driver name */ |
| 80 | unsigned dev_cnt; /* Number of devices */ |
| 81 | unsigned start_idx; /* Start index in global list */ |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 82 | int rec_dev_idx;/* Default capture device. */ |
| 83 | int play_dev_idx;/* Default playback device */ |
| 84 | int dev_idx; /* Default device. */ |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 85 | }; |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 86 | |
Benny Prijono | e3ebd55 | 2009-02-18 20:14:15 +0000 | [diff] [blame] | 87 | /* The audio subsystem */ |
| 88 | static struct aud_subsys |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 89 | { |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 90 | unsigned init_count; /* How many times init() is called */ |
| 91 | pj_pool_factory *pf; /* The pool factory. */ |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 92 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 93 | unsigned drv_cnt; /* Number of drivers. */ |
| 94 | struct driver drv[MAX_DRIVERS]; /* Array of drivers. */ |
Benny Prijono | e3ebd55 | 2009-02-18 20:14:15 +0000 | [diff] [blame] | 95 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 96 | unsigned dev_cnt; /* Total number of devices. */ |
| 97 | pj_uint32_t dev_list[MAX_DEVS];/* Array of device IDs. */ |
Benny Prijono | e3ebd55 | 2009-02-18 20:14:15 +0000 | [diff] [blame] | 98 | |
| 99 | } aud_subsys; |
| 100 | |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 101 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 102 | /* Internal: init driver */ |
| 103 | static pj_status_t init_driver(unsigned drv_idx) |
| 104 | { |
| 105 | struct driver *drv = &aud_subsys.drv[drv_idx]; |
| 106 | pjmedia_aud_dev_factory *f; |
| 107 | unsigned i, dev_cnt; |
| 108 | pj_status_t status; |
| 109 | |
| 110 | /* Create the factory */ |
| 111 | f = (*drv->create)(aud_subsys.pf); |
| 112 | if (!f) |
| 113 | return PJ_EUNKNOWN; |
| 114 | |
| 115 | /* Call factory->init() */ |
| 116 | status = f->op->init(f); |
| 117 | if (status != PJ_SUCCESS) { |
| 118 | f->op->destroy(f); |
| 119 | return status; |
| 120 | } |
| 121 | |
| 122 | /* Get number of devices */ |
| 123 | dev_cnt = f->op->get_dev_count(f); |
| 124 | if (dev_cnt + aud_subsys.dev_cnt > MAX_DEVS) { |
| 125 | PJ_LOG(4,(THIS_FILE, "%d device(s) cannot be registered because" |
| 126 | " there are too many devices", |
| 127 | aud_subsys.dev_cnt + dev_cnt - MAX_DEVS)); |
| 128 | dev_cnt = MAX_DEVS - aud_subsys.dev_cnt; |
| 129 | } |
| 130 | if (dev_cnt == 0) { |
| 131 | f->op->destroy(f); |
| 132 | return PJMEDIA_EAUD_NODEV; |
| 133 | } |
| 134 | |
| 135 | /* Fill in default devices */ |
| 136 | drv->play_dev_idx = drv->rec_dev_idx = drv->dev_idx = -1; |
| 137 | for (i=0; i<dev_cnt; ++i) { |
| 138 | pjmedia_aud_dev_info info; |
| 139 | |
| 140 | status = f->op->get_dev_info(f, i, &info); |
| 141 | if (status != PJ_SUCCESS) { |
| 142 | f->op->destroy(f); |
| 143 | return status; |
| 144 | } |
| 145 | |
| 146 | if (drv->name[0]=='\0') { |
| 147 | /* Set driver name */ |
| 148 | pj_ansi_strncpy(drv->name, info.driver, sizeof(drv->name)); |
| 149 | drv->name[sizeof(drv->name)-1] = '\0'; |
| 150 | } |
| 151 | |
| 152 | if (drv->play_dev_idx < 0 && info.output_count) { |
| 153 | /* Set default playback device */ |
| 154 | drv->play_dev_idx = i; |
| 155 | } |
| 156 | if (drv->rec_dev_idx < 0 && info.input_count) { |
| 157 | /* Set default capture device */ |
| 158 | drv->rec_dev_idx = i; |
| 159 | } |
| 160 | if (drv->dev_idx < 0 && info.input_count && |
| 161 | info.output_count) |
| 162 | { |
| 163 | /* Set default capture and playback device */ |
| 164 | drv->dev_idx = i; |
| 165 | } |
| 166 | |
| 167 | if (drv->play_dev_idx >= 0 && drv->rec_dev_idx >= 0 && |
| 168 | drv->dev_idx >= 0) |
| 169 | { |
| 170 | /* Done. */ |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /* Register the factory */ |
| 176 | drv->f = f; |
| 177 | drv->f->sys.drv_idx = drv_idx; |
| 178 | drv->start_idx = aud_subsys.dev_cnt; |
| 179 | drv->dev_cnt = dev_cnt; |
| 180 | |
| 181 | /* Register devices to global list */ |
| 182 | for (i=0; i<dev_cnt; ++i) { |
| 183 | aud_subsys.dev_list[aud_subsys.dev_cnt++] = MAKE_DEV_ID(drv_idx, i); |
| 184 | } |
| 185 | |
| 186 | return PJ_SUCCESS; |
| 187 | } |
| 188 | |
| 189 | /* Internal: deinit driver */ |
| 190 | static void deinit_driver(unsigned drv_idx) |
| 191 | { |
| 192 | struct driver *drv = &aud_subsys.drv[drv_idx]; |
| 193 | |
| 194 | if (drv->f) { |
| 195 | drv->f->op->destroy(drv->f); |
| 196 | drv->f = NULL; |
| 197 | } |
| 198 | |
| 199 | drv->dev_cnt = 0; |
| 200 | drv->play_dev_idx = drv->rec_dev_idx = drv->dev_idx = -1; |
| 201 | } |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 202 | |
| 203 | /* API: Initialize the audio subsystem. */ |
| 204 | PJ_DEF(pj_status_t) pjmedia_aud_subsys_init(pj_pool_factory *pf) |
| 205 | { |
| 206 | unsigned i; |
| 207 | pj_status_t status = PJ_ENOMEM; |
| 208 | |
Benny Prijono | 555139d | 2009-02-19 12:08:19 +0000 | [diff] [blame] | 209 | /* Allow init() to be called multiple times as long as there is matching |
| 210 | * number of shutdown(). |
| 211 | */ |
| 212 | if (aud_subsys.init_count++ != 0) { |
| 213 | return PJ_SUCCESS; |
| 214 | } |
| 215 | |
Benny Prijono | e3ebd55 | 2009-02-18 20:14:15 +0000 | [diff] [blame] | 216 | aud_subsys.pf = pf; |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 217 | aud_subsys.drv_cnt = 0; |
| 218 | aud_subsys.dev_cnt = 0; |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 219 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 220 | /* Register creation functions */ |
| 221 | aud_subsys.drv[aud_subsys.drv_cnt++].create = &pjmedia_pa_factory; |
| 222 | aud_subsys.drv[aud_subsys.drv_cnt++].create = &pjmedia_wmme_factory; |
Benny Prijono | e3ebd55 | 2009-02-18 20:14:15 +0000 | [diff] [blame] | 223 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 224 | /* Initialize each factory and build the device ID list */ |
| 225 | for (i=0; i<aud_subsys.drv_cnt; ++i) { |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 226 | status = init_driver(i); |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 227 | if (status != PJ_SUCCESS) { |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 228 | deinit_driver(i); |
Benny Prijono | e3ebd55 | 2009-02-18 20:14:15 +0000 | [diff] [blame] | 229 | continue; |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 230 | } |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 233 | return aud_subsys.dev_cnt ? PJ_SUCCESS : status; |
Benny Prijono | e3ebd55 | 2009-02-18 20:14:15 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* API: get the pool factory registered to the audio subsystem. */ |
| 237 | PJ_DEF(pj_pool_factory*) pjmedia_aud_subsys_get_pool_factory(void) |
| 238 | { |
| 239 | return aud_subsys.pf; |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* API: Shutdown the audio subsystem. */ |
| 243 | PJ_DEF(pj_status_t) pjmedia_aud_subsys_shutdown(void) |
| 244 | { |
| 245 | unsigned i; |
| 246 | |
Benny Prijono | 555139d | 2009-02-19 12:08:19 +0000 | [diff] [blame] | 247 | /* Allow shutdown() to be called multiple times as long as there is matching |
| 248 | * number of init(). |
| 249 | */ |
| 250 | if (aud_subsys.init_count == 0) { |
| 251 | return PJ_SUCCESS; |
| 252 | } |
| 253 | --aud_subsys.init_count; |
| 254 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 255 | for (i=0; i<aud_subsys.drv_cnt; ++i) { |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 256 | deinit_driver(i); |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 259 | aud_subsys.pf = NULL; |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 260 | return PJ_SUCCESS; |
| 261 | } |
| 262 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 263 | /* API: get capability name/info */ |
| 264 | PJ_DEF(const char*) pjmedia_aud_dev_cap_name(pjmedia_aud_dev_cap cap, |
| 265 | const char **p_desc) |
| 266 | { |
| 267 | char *desc; |
| 268 | unsigned i; |
| 269 | |
| 270 | if (p_desc==NULL) p_desc = &desc; |
| 271 | |
| 272 | for (i=0; i<PJ_ARRAY_SIZE(cap_infos); ++i) { |
| 273 | if ((1 << i)==cap) |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | if (i==32) { |
| 278 | *p_desc = "??"; |
| 279 | return "??"; |
| 280 | } |
| 281 | |
| 282 | *p_desc = cap_infos[i].info; |
| 283 | return cap_infos[i].name; |
| 284 | } |
| 285 | |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 286 | /* API: Get the number of sound devices installed in the system. */ |
| 287 | PJ_DEF(unsigned) pjmedia_aud_dev_count(void) |
| 288 | { |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 289 | return aud_subsys.dev_cnt; |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 292 | /* Internal: convert local index to global device index */ |
| 293 | static pj_status_t make_global_index(unsigned drv_idx, |
| 294 | pjmedia_aud_dev_index *id) |
| 295 | { |
| 296 | if (*id < 0) { |
| 297 | return PJ_SUCCESS; |
| 298 | } |
| 299 | |
| 300 | /* Check that factory still exists */ |
| 301 | PJ_ASSERT_RETURN(aud_subsys.drv[drv_idx].f, PJ_EBUG); |
| 302 | |
| 303 | /* Check that device index is valid */ |
| 304 | PJ_ASSERT_RETURN(*id>=0 && *id<(int)aud_subsys.drv[drv_idx].dev_cnt, |
| 305 | PJ_EBUG); |
| 306 | |
| 307 | *id += aud_subsys.drv[drv_idx].start_idx; |
| 308 | return PJ_SUCCESS; |
| 309 | } |
| 310 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 311 | /* Internal: lookup device id */ |
| 312 | static pj_status_t lookup_dev(pjmedia_aud_dev_index id, |
| 313 | pjmedia_aud_dev_factory **p_f, |
| 314 | unsigned *p_local_index) |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 315 | { |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 316 | int f_id, index; |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 317 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 318 | if (id < 0) { |
| 319 | unsigned i; |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 320 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 321 | if (id == PJMEDIA_AUD_INVALID_DEV) |
| 322 | return PJMEDIA_EAUD_INVDEV; |
| 323 | |
| 324 | for (i=0; i<aud_subsys.drv_cnt; ++i) { |
| 325 | struct driver *drv = &aud_subsys.drv[i]; |
| 326 | if (drv->dev_idx >= 0) { |
| 327 | id = drv->dev_idx; |
| 328 | make_global_index(i, &id); |
| 329 | break; |
| 330 | } else if (id==PJMEDIA_AUD_DEFAULT_CAPTURE_DEV && |
| 331 | drv->rec_dev_idx >= 0) |
| 332 | { |
| 333 | id = drv->rec_dev_idx; |
| 334 | make_global_index(i, &id); |
| 335 | break; |
| 336 | } else if (id==PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV && |
| 337 | drv->play_dev_idx >= 0) |
| 338 | { |
| 339 | id = drv->play_dev_idx; |
| 340 | make_global_index(i, &id); |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if (id < 0) { |
| 346 | return PJMEDIA_EAUD_NODEFDEV; |
| 347 | } |
| 348 | } |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 349 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 350 | f_id = GET_FID(aud_subsys.dev_list[id]); |
| 351 | index = GET_INDEX(aud_subsys.dev_list[id]); |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 352 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 353 | if (f_id < 0 || f_id >= (int)aud_subsys.drv_cnt) |
| 354 | return PJMEDIA_EAUD_INVDEV; |
| 355 | |
| 356 | if (index < 0 || index >= (int)aud_subsys.drv[f_id].dev_cnt) |
| 357 | return PJMEDIA_EAUD_INVDEV; |
| 358 | |
| 359 | *p_f = aud_subsys.drv[f_id].f; |
| 360 | *p_local_index = (unsigned)index; |
| 361 | |
| 362 | return PJ_SUCCESS; |
| 363 | |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 366 | /* API: Get device information. */ |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 367 | PJ_DEF(pj_status_t) pjmedia_aud_dev_get_info(pjmedia_aud_dev_index id, |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 368 | pjmedia_aud_dev_info *info) |
| 369 | { |
Benny Prijono | e3ebd55 | 2009-02-18 20:14:15 +0000 | [diff] [blame] | 370 | pjmedia_aud_dev_factory *f; |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 371 | unsigned index; |
| 372 | pj_status_t status; |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 373 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 374 | PJ_ASSERT_RETURN(info && id!=PJMEDIA_AUD_INVALID_DEV, PJ_EINVAL); |
| 375 | PJ_ASSERT_RETURN(aud_subsys.pf, PJMEDIA_EAUD_INIT); |
Benny Prijono | 598b01d | 2009-02-18 13:55:03 +0000 | [diff] [blame] | 376 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 377 | status = lookup_dev(id, &f, &index); |
| 378 | if (status != PJ_SUCCESS) |
| 379 | return status; |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 380 | |
Benny Prijono | e3ebd55 | 2009-02-18 20:14:15 +0000 | [diff] [blame] | 381 | return f->op->get_dev_info(f, index, info); |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 384 | /* API: find device */ |
| 385 | PJ_DEF(pj_status_t) pjmedia_aud_dev_lookup( const char *drv_name, |
| 386 | const char *dev_name, |
| 387 | pjmedia_aud_dev_index *id) |
| 388 | { |
| 389 | pjmedia_aud_dev_factory *f = NULL; |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 390 | unsigned drv_idx, dev_idx; |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 391 | |
| 392 | PJ_ASSERT_RETURN(drv_name && dev_name && id, PJ_EINVAL); |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 393 | PJ_ASSERT_RETURN(aud_subsys.pf, PJMEDIA_EAUD_INIT); |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 394 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 395 | for (drv_idx=0; drv_idx<aud_subsys.drv_cnt; ++drv_idx) { |
| 396 | if (!pj_ansi_stricmp(drv_name, aud_subsys.drv[drv_idx].name)) { |
| 397 | f = aud_subsys.drv[drv_idx].f; |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 398 | break; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if (!f) |
| 403 | return PJ_ENOTFOUND; |
| 404 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 405 | for (dev_idx=0; dev_idx<aud_subsys.drv[drv_idx].dev_cnt; ++dev_idx) { |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 406 | pjmedia_aud_dev_info info; |
| 407 | pj_status_t status; |
| 408 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 409 | status = f->op->get_dev_info(f, dev_idx, &info); |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 410 | if (status != PJ_SUCCESS) |
| 411 | return status; |
| 412 | |
| 413 | if (!pj_ansi_stricmp(dev_name, info.name)) |
| 414 | break; |
| 415 | } |
| 416 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 417 | if (dev_idx==aud_subsys.drv[drv_idx].dev_cnt) |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 418 | return PJ_ENOTFOUND; |
| 419 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 420 | *id = dev_idx; |
| 421 | make_global_index(drv_idx, id); |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 422 | |
| 423 | return PJ_SUCCESS; |
| 424 | } |
| 425 | |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 426 | /* API: Initialize the audio device parameters with default values for the |
| 427 | * specified device. |
| 428 | */ |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 429 | PJ_DEF(pj_status_t) pjmedia_aud_dev_default_param(pjmedia_aud_dev_index id, |
| 430 | pjmedia_aud_param *param) |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 431 | { |
Benny Prijono | e3ebd55 | 2009-02-18 20:14:15 +0000 | [diff] [blame] | 432 | pjmedia_aud_dev_factory *f; |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 433 | unsigned index; |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 434 | pj_status_t status; |
| 435 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 436 | PJ_ASSERT_RETURN(param && id!=PJMEDIA_AUD_INVALID_DEV, PJ_EINVAL); |
| 437 | PJ_ASSERT_RETURN(aud_subsys.pf, PJMEDIA_EAUD_INIT); |
Benny Prijono | 598b01d | 2009-02-18 13:55:03 +0000 | [diff] [blame] | 438 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 439 | status = lookup_dev(id, &f, &index); |
| 440 | if (status != PJ_SUCCESS) |
| 441 | return status; |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 442 | |
Benny Prijono | e3ebd55 | 2009-02-18 20:14:15 +0000 | [diff] [blame] | 443 | status = f->op->default_param(f, index, param); |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 444 | if (status != PJ_SUCCESS) |
| 445 | return status; |
| 446 | |
| 447 | /* Normalize device IDs */ |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 448 | make_global_index(f->sys.drv_idx, ¶m->rec_id); |
| 449 | make_global_index(f->sys.drv_idx, ¶m->play_id); |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 450 | |
| 451 | return PJ_SUCCESS; |
| 452 | } |
| 453 | |
| 454 | /* API: Open audio stream object using the specified parameters. */ |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 455 | PJ_DEF(pj_status_t) pjmedia_aud_stream_create(const pjmedia_aud_param *prm, |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 456 | pjmedia_aud_rec_cb rec_cb, |
| 457 | pjmedia_aud_play_cb play_cb, |
| 458 | void *user_data, |
| 459 | pjmedia_aud_stream **p_aud_strm) |
| 460 | { |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 461 | pjmedia_aud_dev_factory *rec_f=NULL, *play_f=NULL, *f=NULL; |
| 462 | pjmedia_aud_param param; |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 463 | pj_status_t status; |
| 464 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 465 | PJ_ASSERT_RETURN(prm && prm->dir && p_aud_strm, PJ_EINVAL); |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 466 | PJ_ASSERT_RETURN(aud_subsys.pf, PJMEDIA_EAUD_INIT); |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 467 | |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 468 | /* Must make copy of param because we're changing device ID */ |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 469 | pj_memcpy(¶m, prm, sizeof(param)); |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 470 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 471 | /* Normalize rec_id */ |
| 472 | if (param.dir & PJMEDIA_DIR_CAPTURE) { |
| 473 | unsigned index; |
| 474 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 475 | if (param.rec_id < 0) |
| 476 | param.rec_id = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV; |
| 477 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 478 | status = lookup_dev(param.rec_id, &rec_f, &index); |
| 479 | if (status != PJ_SUCCESS) |
| 480 | return status; |
| 481 | |
| 482 | param.rec_id = index; |
| 483 | f = rec_f; |
| 484 | } |
| 485 | |
| 486 | /* Normalize play_id */ |
| 487 | if (param.dir & PJMEDIA_DIR_PLAYBACK) { |
| 488 | unsigned index; |
| 489 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 490 | if (param.play_id < 0) |
| 491 | param.play_id = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV; |
| 492 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 493 | status = lookup_dev(param.play_id, &play_f, &index); |
| 494 | if (status != PJ_SUCCESS) |
| 495 | return status; |
| 496 | |
| 497 | param.play_id = index; |
| 498 | f = play_f; |
| 499 | |
| 500 | /* For now, rec_id and play_id must belong to the same factory */ |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 501 | PJ_ASSERT_RETURN(rec_f == play_f, PJMEDIA_EAUD_INVDEV); |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 504 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 505 | /* Create the stream */ |
Benny Prijono | e3ebd55 | 2009-02-18 20:14:15 +0000 | [diff] [blame] | 506 | status = f->op->create_stream(f, ¶m, rec_cb, play_cb, |
| 507 | user_data, p_aud_strm); |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 508 | if (status != PJ_SUCCESS) |
| 509 | return status; |
| 510 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 511 | /* Assign factory id to the stream */ |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 512 | (*p_aud_strm)->sys.drv_idx = f->sys.drv_idx; |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 513 | return PJ_SUCCESS; |
| 514 | } |
| 515 | |
| 516 | /* API: Get the running parameters for the specified audio stream. */ |
| 517 | PJ_DEF(pj_status_t) pjmedia_aud_stream_get_param(pjmedia_aud_stream *strm, |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 518 | pjmedia_aud_param *param) |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 519 | { |
| 520 | pj_status_t status; |
| 521 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 522 | PJ_ASSERT_RETURN(strm && param, PJ_EINVAL); |
| 523 | PJ_ASSERT_RETURN(aud_subsys.pf, PJMEDIA_EAUD_INIT); |
| 524 | |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 525 | status = strm->op->get_param(strm, param); |
| 526 | if (status != PJ_SUCCESS) |
| 527 | return status; |
| 528 | |
| 529 | /* Normalize device id's */ |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame^] | 530 | make_global_index(strm->sys.drv_idx, ¶m->rec_id); |
| 531 | make_global_index(strm->sys.drv_idx, ¶m->play_id); |
Benny Prijono | 2cd64f8 | 2009-02-17 19:57:48 +0000 | [diff] [blame] | 532 | |
| 533 | return PJ_SUCCESS; |
| 534 | } |
| 535 | |
| 536 | /* API: Get the value of a specific capability of the audio stream. */ |
| 537 | PJ_DEF(pj_status_t) pjmedia_aud_stream_get_cap(pjmedia_aud_stream *strm, |
| 538 | pjmedia_aud_dev_cap cap, |
| 539 | void *value) |
| 540 | { |
| 541 | return strm->op->get_cap(strm, cap, value); |
| 542 | } |
| 543 | |
| 544 | /* API: Set the value of a specific capability of the audio stream. */ |
| 545 | PJ_DEF(pj_status_t) pjmedia_aud_stream_set_cap(pjmedia_aud_stream *strm, |
| 546 | pjmedia_aud_dev_cap cap, |
| 547 | const void *value) |
| 548 | { |
| 549 | return strm->op->set_cap(strm, cap, value); |
| 550 | } |
| 551 | |
| 552 | /* API: Start the stream. */ |
| 553 | PJ_DEF(pj_status_t) pjmedia_aud_stream_start(pjmedia_aud_stream *strm) |
| 554 | { |
| 555 | return strm->op->start(strm); |
| 556 | } |
| 557 | |
| 558 | /* API: Stop the stream. */ |
| 559 | PJ_DEF(pj_status_t) pjmedia_aud_stream_stop(pjmedia_aud_stream *strm) |
| 560 | { |
| 561 | return strm->op->stop(strm); |
| 562 | } |
| 563 | |
| 564 | /* API: Destroy the stream. */ |
| 565 | PJ_DEF(pj_status_t) pjmedia_aud_stream_destroy(pjmedia_aud_stream *strm) |
| 566 | { |
| 567 | return strm->op->destroy(strm); |
| 568 | } |
| 569 | |
| 570 | |