Changeset 3075 for pjproject/trunk/pjmedia/src/pjmedia/codec.c
- Timestamp:
- Jan 26, 2010 6:54:28 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjmedia/src/pjmedia/codec.c
r3074 r3075 23 23 #include <pj/assert.h> 24 24 #include <pj/log.h> 25 #include <pj/pool.h> 25 26 #include <pj/string.h> 26 27 … … 28 29 29 30 30 31 /* Definition of default codecs parameters */32 struct pjmedia_codec_default_param33 {34 pj_pool_t *pool;35 pjmedia_codec_param *param;36 };37 31 38 32 … … 44 38 * Initialize codec manager. 45 39 */ 46 PJ_DEF(pj_status_t) pjmedia_codec_mgr_init (pjmedia_codec_mgr *mgr, 47 pj_pool_factory *pf) 48 { 49 pj_status_t status; 50 51 PJ_ASSERT_RETURN(mgr && pf, PJ_EINVAL); 52 53 /* Init codec manager */ 54 pj_bzero(mgr, sizeof(pjmedia_codec_mgr)); 55 mgr->pf = pf; 40 PJ_DEF(pj_status_t) pjmedia_codec_mgr_init (pjmedia_codec_mgr *mgr) 41 { 42 PJ_ASSERT_RETURN(mgr, PJ_EINVAL); 43 56 44 pj_list_init (&mgr->factory_list); 57 45 mgr->codec_cnt = 0; 58 59 /* Create pool */60 mgr->pool = pj_pool_create(mgr->pf, "codec-mgr", 256, 256, NULL);61 62 /* Create mutex */63 status = pj_mutex_create_recursive(mgr->pool, "codec-mgr", &mgr->mutex);64 if (status != PJ_SUCCESS)65 return status;66 67 return PJ_SUCCESS;68 }69 70 /*71 * Initialize codec manager.72 */73 PJ_DEF(pj_status_t) pjmedia_codec_mgr_destroy (pjmedia_codec_mgr *mgr)74 {75 unsigned i;76 77 PJ_ASSERT_RETURN(mgr, PJ_EINVAL);78 79 /* Cleanup all pools of all codec default params */80 for (i=0; i<mgr->codec_cnt; ++i) {81 if (mgr->codec_desc[i].param) {82 pj_assert(mgr->codec_desc[i].param->pool);83 pj_pool_release(mgr->codec_desc[i].param->pool);84 }85 }86 87 /* Destroy mutex */88 pj_mutex_destroy(mgr->mutex);89 90 /* Release pool */91 pj_pool_release(mgr->pool);92 93 /* Just for safety, set codec manager states to zero */94 pj_bzero(mgr, sizeof(pjmedia_codec_mgr));95 46 96 47 return PJ_SUCCESS; … … 115 66 if (status != PJ_SUCCESS) 116 67 return status; 117 118 pj_mutex_lock(mgr->mutex); 68 119 69 120 70 /* Check codec count */ 121 if (count + mgr->codec_cnt > PJ_ARRAY_SIZE(mgr->codec_desc)) { 122 pj_mutex_unlock(mgr->mutex); 71 if (count + mgr->codec_cnt > PJ_ARRAY_SIZE(mgr->codec_desc)) 123 72 return PJ_ETOOMANY; 124 }125 73 126 74 … … 145 93 pj_list_push_back(&mgr->factory_list, factory); 146 94 147 pj_mutex_unlock(mgr->mutex);148 95 149 96 return PJ_SUCCESS; … … 161 108 PJ_ASSERT_RETURN(mgr && factory, PJ_EINVAL); 162 109 163 pj_mutex_lock(mgr->mutex);164 165 110 /* Factory must be registered. */ 166 if (pj_list_find_node(&mgr->factory_list, factory) != factory) { 167 pj_mutex_unlock(mgr->mutex); 168 return PJ_ENOTFOUND; 169 } 111 PJ_ASSERT_RETURN(pj_list_find_node(&mgr->factory_list, factory)==factory, 112 PJ_ENOTFOUND); 170 113 171 114 /* Erase factory from the factory list */ … … 179 122 180 123 if (mgr->codec_desc[i].factory == factory) { 181 /* Release pool of codec default param */ 182 if (mgr->codec_desc[i].param) { 183 pj_assert(mgr->codec_desc[i].param->pool); 184 pj_pool_release(mgr->codec_desc[i].param->pool); 185 } 186 187 /* Remove the codec from array of codec descriptions */ 124 188 125 pj_array_erase(mgr->codec_desc, sizeof(mgr->codec_desc[0]), 189 126 mgr->codec_cnt, i); … … 195 132 } 196 133 197 pj_mutex_unlock(mgr->mutex);198 134 199 135 return PJ_SUCCESS; … … 212 148 213 149 PJ_ASSERT_RETURN(mgr && count && codecs, PJ_EINVAL); 214 215 pj_mutex_lock(mgr->mutex);216 150 217 151 if (*count > mgr->codec_cnt) … … 229 163 } 230 164 231 pj_mutex_unlock(mgr->mutex);232 233 165 return PJ_SUCCESS; 234 166 } … … 246 178 PJ_ASSERT_RETURN(mgr && p_info && pt>=0 && pt < 96, PJ_EINVAL); 247 179 248 pj_mutex_lock(mgr->mutex);249 250 180 for (i=0; i<mgr->codec_cnt; ++i) { 251 181 if (mgr->codec_desc[i].info.pt == pt) { 252 182 *p_info = &mgr->codec_desc[i].info; 253 254 pj_mutex_unlock(mgr->mutex);255 183 return PJ_SUCCESS; 256 184 } 257 185 } 258 259 pj_mutex_unlock(mgr->mutex);260 186 261 187 return PJMEDIA_CODEC_EUNSUP; … … 305 231 PJ_ASSERT_RETURN(mgr && codec_id && count && *count, PJ_EINVAL); 306 232 307 pj_mutex_lock(mgr->mutex);308 309 233 for (i=0; i<mgr->codec_cnt; ++i) { 310 234 … … 326 250 327 251 } 328 329 pj_mutex_unlock(mgr->mutex);330 252 331 253 *count = found; … … 391 313 392 314 PJ_ASSERT_RETURN(mgr && codec_id, PJ_EINVAL); 393 394 pj_mutex_lock(mgr->mutex);395 315 396 316 /* Update the priorities of affected codecs */ … … 406 326 } 407 327 408 if (!found) { 409 pj_mutex_unlock(mgr->mutex); 328 if (!found) 410 329 return PJ_ENOTFOUND; 411 }412 330 413 331 /* Re-sort codecs */ 414 332 sort_codecs(mgr); 415 416 pj_mutex_unlock(mgr->mutex); 333 417 334 418 335 return PJ_SUCCESS; … … 434 351 *p_codec = NULL; 435 352 436 pj_mutex_lock(mgr->mutex);437 438 353 factory = mgr->factory_list.next; 439 354 while (factory != &mgr->factory_list) { … … 442 357 443 358 status = (*factory->op->alloc_codec)(factory, info, p_codec); 444 if (status == PJ_SUCCESS) { 445 pj_mutex_unlock(mgr->mutex); 359 if (status == PJ_SUCCESS) 446 360 return PJ_SUCCESS; 447 }448 361 449 362 } … … 452 365 } 453 366 454 pj_mutex_unlock(mgr->mutex);455 367 456 368 return PJMEDIA_CODEC_EUNSUP; … … 467 379 pjmedia_codec_factory *factory; 468 380 pj_status_t status; 469 pjmedia_codec_id codec_id;470 struct pjmedia_codec_desc *codec_desc = NULL;471 unsigned i;472 381 473 382 PJ_ASSERT_RETURN(mgr && info && param, PJ_EINVAL); 474 383 475 if (!pjmedia_codec_info_to_id(info, (char*)&codec_id, sizeof(codec_id)))476 return PJ_EINVAL;477 478 pj_mutex_lock(mgr->mutex);479 480 /* First, lookup default param in codec desc */481 for (i=0; i < mgr->codec_cnt; ++i) {482 if (pj_ansi_stricmp(codec_id, mgr->codec_desc[i].id) == 0) {483 codec_desc = &mgr->codec_desc[i];484 break;485 }486 }487 488 /* If we found the codec and its default param is set, return it */489 if (codec_desc && codec_desc->param) {490 pj_assert(codec_desc->param->param);491 pj_memcpy(param, codec_desc->param->param,492 sizeof(pjmedia_codec_param));493 494 pj_mutex_unlock(mgr->mutex);495 return PJ_SUCCESS;496 }497 498 /* Otherwise query the default param from codec factory */499 384 factory = mgr->factory_list.next; 500 385 while (factory != &mgr->factory_list) { … … 508 393 param->info.max_bps = param->info.avg_bps; 509 394 510 pj_mutex_unlock(mgr->mutex);511 395 return PJ_SUCCESS; 512 396 } … … 517 401 } 518 402 519 pj_mutex_unlock(mgr->mutex);520 521 403 522 404 return PJMEDIA_CODEC_EUNSUP; 523 }524 525 526 /*527 * Set default codec parameter.528 */529 PJ_DEF(pj_status_t) pjmedia_codec_mgr_set_default_param(530 pjmedia_codec_mgr *mgr,531 const pjmedia_codec_info *info,532 const pjmedia_codec_param *param )533 {534 unsigned i;535 pjmedia_codec_id codec_id;536 pj_pool_t *pool;537 struct pjmedia_codec_desc *codec_desc = NULL;538 pjmedia_codec_default_param *p;539 540 PJ_ASSERT_RETURN(mgr && info, PJ_EINVAL);541 542 if (!pjmedia_codec_info_to_id(info, (char*)&codec_id, sizeof(codec_id)))543 return PJ_EINVAL;544 545 pj_mutex_lock(mgr->mutex);546 547 /* Lookup codec desc */548 for (i=0; i < mgr->codec_cnt; ++i) {549 if (pj_ansi_stricmp(codec_id, mgr->codec_desc[i].id) == 0) {550 codec_desc = &mgr->codec_desc[i];551 break;552 }553 }554 555 /* Codec not found */556 if (!codec_desc) {557 pj_mutex_unlock(mgr->mutex);558 return PJMEDIA_CODEC_EUNSUP;559 }560 561 /* If codec param is previously set, release codec param pool */562 if (codec_desc->param) {563 pj_assert(codec_desc->param->pool);564 pj_pool_release(codec_desc->param->pool);565 codec_desc->param = NULL;566 }567 568 /* When param is set to NULL, i.e: setting default codec param to library569 * default setting, just return PJ_SUCCESS.570 */571 if (NULL == param) {572 pj_mutex_unlock(mgr->mutex);573 return PJ_SUCCESS;574 }575 576 /* Instantiate and initialize codec param */577 pool = pj_pool_create(mgr->pf, (char*)codec_id, 256, 256, NULL);578 codec_desc->param = PJ_POOL_ZALLOC_T(pool, pjmedia_codec_default_param);579 p = codec_desc->param;580 p->pool = pool;581 p->param = PJ_POOL_ZALLOC_T(pool, pjmedia_codec_param);582 583 /* Update codec param */584 pj_memcpy(p->param, param, sizeof(pjmedia_codec_param));585 for (i = 0; i < param->setting.dec_fmtp.cnt; ++i) {586 pj_strdup(pool, &p->param->setting.dec_fmtp.param[i].name,587 ¶m->setting.dec_fmtp.param[i].name);588 pj_strdup(pool, &p->param->setting.dec_fmtp.param[i].val,589 ¶m->setting.dec_fmtp.param[i].val);590 }591 for (i = 0; i < param->setting.dec_fmtp.cnt; ++i) {592 pj_strdup(pool, &p->param->setting.dec_fmtp.param[i].name,593 ¶m->setting.dec_fmtp.param[i].name);594 pj_strdup(pool, &p->param->setting.dec_fmtp.param[i].val,595 ¶m->setting.dec_fmtp.param[i].val);596 }597 598 pj_mutex_unlock(mgr->mutex);599 600 return PJ_SUCCESS;601 405 } 602 406
Note: See TracChangeset
for help on using the changeset viewer.