| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 28 |
struct null_audio_dev_info |
| 29 |
{ |
| 30 |
pjmedia_aud_dev_info info; |
| 31 |
unsigned dev_id; |
| 32 |
}; |
| 33 |
|
| 34 |
|
| 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 |
|
| 46 |
struct null_audio_stream |
| 47 |
{ |
| 48 |
pjmedia_aud_stream base; |
| 49 |
pjmedia_aud_param param; |
| 50 |
pj_pool_t *pool; |
| 51 |
|
| 52 |
pjmedia_aud_rec_cb rec_cb; |
| 53 |
pjmedia_aud_play_cb play_cb; |
| 54 |
void *user_data; |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 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 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 151 |
ndi->info.caps = 0; |
| 152 |
|
| 153 |
PJ_LOG(4, (THIS_FILE, "null audio initialized")); |
| 154 |
|
| 155 |
return PJ_SUCCESS; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 224 |
|
| 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 |
|
| 231 |
param->flags = 0; |
| 232 |
|
| 233 |
return PJ_SUCCESS; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 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 |
|
| 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 |
|
| 260 |
if (param->dir & PJMEDIA_DIR_PLAYBACK) { |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
if (param->dir & PJMEDIA_DIR_CAPTURE) { |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 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 |
¶m->output_vol); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
strm->base.op = &stream_op; |
| 277 |
*p_aud_strm = &strm->base; |
| 278 |
|
| 279 |
return PJ_SUCCESS; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 314 |
if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING) |
| 315 |
{ |
| 316 |
|
| 317 |
*(unsigned*)pval = 0; |
| 318 |
return PJ_SUCCESS; |
| 319 |
} else { |
| 320 |
return PJMEDIA_EAUD_INVCAP; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 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 |
|
| 336 |
if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING) |
| 337 |
{ |
| 338 |
|
| 339 |
|
| 340 |
return PJ_SUCCESS; |
| 341 |
} |
| 342 |
|
| 343 |
return PJMEDIA_EAUD_INVCAP; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 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 |
|
| 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 |
|
| 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 |