Changeset 278 for pjproject/trunk/pjmedia/src/pjmedia/g711.c
- Timestamp:
- Mar 4, 2006 8:43:52 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjmedia/src/pjmedia/g711.c
r205 r278 21 21 */ 22 22 #include <pjmedia/codec.h> 23 #include <pjmedia/endpoint.h> 23 24 #include <pjmedia/errno.h> 24 25 #include <pjmedia/port.h> … … 101 102 102 103 /* G711 factory private data */ 103 struct g711_factory_private 104 { 105 pj_pool_t *pool; 106 pjmedia_codec codec_list; 107 }; 104 static struct g711_factory 105 { 106 pjmedia_codec_factory base; 107 pjmedia_endpt *endpt; 108 pj_pool_t *pool; 109 pj_mutex_t *mutex; 110 pjmedia_codec codec_list; 111 } g711_factory; 108 112 109 113 /* G711 codec private data. */ … … 114 118 115 119 116 PJ_DEF(pj_status_t) g711_init_factory (pjmedia_codec_factory *factory, pj_pool_t *pool) 117 { 118 struct g711_factory_private *priv; 119 //enum { CODEC_MEM_SIZE = sizeof(pjmedia_codec) + sizeof(struct g711_private) + 4 }; 120 121 /* Create pool. */ 122 /* 123 pool = pj_pool_pool_create_pool(pp, "g711ftry", 124 G711_CODEC_CNT*CODEC_MEM_SIZE + 125 sizeof(struct g711_factory_private), 126 CODEC_MEM_SIZE, NULL); 127 if (!pool) 128 return -1; 129 */ 130 131 priv = pj_pool_alloc(pool, sizeof(struct g711_factory_private)); 132 if (!priv) 133 return -1; 134 135 factory->factory_data = priv; 136 factory->op = &g711_factory_op; 137 138 priv->pool = pool; 139 pj_list_init(&priv->codec_list); 120 PJ_DEF(pj_status_t) g711_deinit_factory (pjmedia_codec_factory *factory) 121 { 122 PJ_ASSERT_RETURN(factory==&g711_factory.base, PJ_EINVAL); 123 124 /* Invalidate member to help detect errors */ 125 g711_factory.codec_list.next = g711_factory.codec_list.prev = NULL; 140 126 return 0; 141 127 } 142 128 143 PJ_DEF(pj_status_t) g711_deinit_factory (pjmedia_codec_factory *factory) 144 { 145 struct g711_factory_private *priv = factory->factory_data; 146 147 /* Invalidate member to help detect errors */ 148 priv->pool = NULL; 149 priv->codec_list.next = priv->codec_list.prev = NULL; 150 return 0; 129 PJ_DEF(pj_status_t) pjmedia_codec_g711_init(pjmedia_endpt *endpt) 130 { 131 pjmedia_codec_mgr *codec_mgr; 132 pj_status_t status; 133 134 if (g711_factory.endpt != NULL) { 135 /* Already initialized. */ 136 return PJ_SUCCESS; 137 } 138 139 /* Init factory */ 140 g711_factory.base.op = &g711_factory_op; 141 g711_factory.base.factory_data = NULL; 142 g711_factory.endpt = endpt; 143 144 pj_list_init(&g711_factory.codec_list); 145 146 /* Create pool */ 147 g711_factory.pool = pjmedia_endpt_create_pool(endpt, "g711", 4000, 4000); 148 if (!g711_factory.pool) 149 return PJ_ENOMEM; 150 151 /* Create mutex. */ 152 status = pj_mutex_create_simple(g711_factory.pool, "g611", 153 &g711_factory.mutex); 154 if (status != PJ_SUCCESS) 155 goto on_error; 156 157 /* Get the codec manager. */ 158 codec_mgr = pjmedia_endpt_get_codec_mgr(endpt); 159 if (!codec_mgr) { 160 return PJ_EINVALIDOP; 161 } 162 163 /* Register codec factory to endpoint. */ 164 status = pjmedia_codec_mgr_register_factory(codec_mgr, 165 &g711_factory.base); 166 if (status != PJ_SUCCESS) 167 return status; 168 169 170 return PJ_SUCCESS; 171 172 on_error: 173 if (g711_factory.mutex) { 174 pj_mutex_destroy(g711_factory.mutex); 175 g711_factory.mutex = NULL; 176 } 177 if (g711_factory.pool) { 178 pj_pool_release(g711_factory.pool); 179 g711_factory.pool = NULL; 180 } 181 return status; 182 } 183 184 PJ_DEF(pj_status_t) pjmedia_codec_g711_deinit(void) 185 { 186 pjmedia_codec_mgr *codec_mgr; 187 pj_status_t status; 188 189 if (g711_factory.endpt == NULL) { 190 /* Not registered. */ 191 return PJ_SUCCESS; 192 } 193 194 /* Lock mutex. */ 195 pj_mutex_lock(g711_factory.mutex); 196 197 /* Get the codec manager. */ 198 codec_mgr = pjmedia_endpt_get_codec_mgr(g711_factory.endpt); 199 if (!codec_mgr) { 200 g711_factory.endpt = NULL; 201 pj_mutex_unlock(g711_factory.mutex); 202 return PJ_EINVALIDOP; 203 } 204 205 /* Unregister G711 codec factory. */ 206 status = pjmedia_codec_mgr_unregister_factory(codec_mgr, 207 &g711_factory.base); 208 g711_factory.endpt = NULL; 209 210 /* Destroy mutex. */ 211 pj_mutex_destroy(g711_factory.mutex); 212 g711_factory.mutex = NULL; 213 214 215 /* Release pool. */ 216 pj_pool_release(g711_factory.pool); 217 g711_factory.pool = NULL; 218 219 220 return status; 151 221 } 152 222 … … 209 279 pjmedia_codec **p_codec) 210 280 { 211 struct g711_factory_private *priv = factory->factory_data;212 281 pjmedia_codec *codec = NULL; 213 282 283 PJ_ASSERT_RETURN(factory==&g711_factory.base, PJ_EINVAL); 284 285 /* Lock mutex. */ 286 pj_mutex_lock(g711_factory.mutex); 287 214 288 /* Allocate new codec if no more is available */ 215 if (pj_list_empty(& priv->codec_list)) {289 if (pj_list_empty(&g711_factory.codec_list)) { 216 290 struct g711_private *codec_priv; 217 291 218 codec = pj_pool_alloc(priv->pool, sizeof(pjmedia_codec)); 219 codec_priv = pj_pool_alloc(priv->pool, sizeof(struct g711_private)); 220 if (!codec || !codec_priv) 292 codec = pj_pool_alloc(g711_factory.pool, sizeof(pjmedia_codec)); 293 codec_priv = pj_pool_alloc(g711_factory.pool, sizeof(struct g711_private)); 294 if (!codec || !codec_priv) { 295 pj_mutex_unlock(g711_factory.mutex); 221 296 return PJ_ENOMEM; 297 } 222 298 223 299 codec_priv->pt = id->pt; … … 227 303 codec->codec_data = codec_priv; 228 304 } else { 229 codec = priv->codec_list.next;305 codec = g711_factory.codec_list.next; 230 306 pj_list_erase(codec); 231 307 } … … 235 311 236 312 *p_codec = codec; 313 314 /* Unlock mutex. */ 315 pj_mutex_unlock(g711_factory.mutex); 316 237 317 return PJ_SUCCESS; 238 318 } … … 240 320 static pj_status_t g711_dealloc_codec( pjmedia_codec_factory *factory, pjmedia_codec *codec ) 241 321 { 242 struct g711_factory_private *priv = factory->factory_data; 322 323 PJ_ASSERT_RETURN(factory==&g711_factory.base, PJ_EINVAL); 243 324 244 325 /* Check that this node has not been deallocated before */ … … 248 329 } 249 330 331 /* Lock mutex. */ 332 pj_mutex_lock(g711_factory.mutex); 333 250 334 /* Insert at the back of the list */ 251 pj_list_insert_before(&priv->codec_list, codec); 335 pj_list_insert_before(&g711_factory.codec_list, codec); 336 337 /* Unlock mutex. */ 338 pj_mutex_unlock(g711_factory.mutex); 252 339 253 340 return PJ_SUCCESS;
Note: See TracChangeset
for help on using the changeset viewer.