static int set_caps(struct ast_sip_session *session, struct ast_sip_session_media *session_media, const struct pjmedia_sdp_media *stream) { RAII_VAR(struct ast_format_cap *, caps, NULL, ast_format_cap_destroy); RAII_VAR(struct ast_format_cap *, peer, NULL, ast_format_cap_destroy); RAII_VAR(struct ast_format_cap *, joint, NULL, ast_format_cap_destroy); enum ast_format_type media_type = stream_to_media_type(session_media->stream_type); struct ast_rtp_codecs codecs; struct ast_format fmt; int fmts = 0; int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) && !ast_format_cap_is_empty(session->direct_media_cap); if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK)) || !(peer = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) { ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n", session_media->stream_type); return -1; } /* get the endpoint capabilities */ if (direct_media_enabled) { ast_format_cap_joint_copy(session->endpoint->media.codecs, session->direct_media_cap, caps); } else { ast_format_cap_copy(caps, session->endpoint->media.codecs); } format_cap_only_type(caps, media_type); /* get the capabilities on the peer */ get_codecs(session, stream, &codecs); ast_rtp_codecs_payload_formats(&codecs, peer, &fmts); /* get the joint capabilities between peer and endpoint */ if (!(joint = ast_format_cap_joint(caps, peer))) { char usbuf[64], thembuf[64]; ast_rtp_codecs_payloads_destroy(&codecs); ast_getformatname_multiple(usbuf, sizeof(usbuf), caps); ast_getformatname_multiple(thembuf, sizeof(thembuf), peer); ast_log(LOG_WARNING, "No joint capabilities between our configuration(%s) and incoming SDP(%s)\n", usbuf, thembuf); return -1; } ast_rtp_codecs_payloads_copy(&codecs, ast_rtp_instance_get_codecs(session_media->rtp), session_media->rtp); ast_format_cap_copy(caps, session->req_caps); ast_format_cap_remove_bytype(caps, media_type); ast_format_cap_append(caps, joint); ast_format_cap_append(session->req_caps, caps); if (session->channel) { ast_format_cap_copy(caps, ast_channel_nativeformats(session->channel)); ast_format_cap_remove_bytype(caps, media_type); ast_codec_choose(&session->endpoint->media.prefs, joint, 1, &fmt); ast_format_cap_add(caps, &fmt); /* Apply the new formats to the channel, potentially changing read/write formats while doing so */ ast_format_cap_copy(ast_channel_nativeformats(session->channel), caps); ast_set_read_format(session->channel, ast_channel_readformat(session->channel)); ast_set_write_format(session->channel, ast_channel_writeformat(session->channel)); } ast_rtp_codecs_payloads_destroy(&codecs); return 0; }
static int container_test1_helper(struct ast_format_cap *cap1, struct ast_format_cap *cap2, struct ast_test *test) { int res = AST_TEST_PASS; struct ast_format_cap *cap_joint = NULL; struct ast_format tmpformat; if (ast_format_attr_reg_interface(&test_interface)) { ast_test_status_update(test, "test_interface failed to register.\n"); ast_format_cap_destroy(cap1); ast_format_cap_destroy(cap2); return AST_TEST_FAIL; } ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)); ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)); ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_G722, 0)); ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0)); ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_H264, 0)); ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_H263, 0)); ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_T140, 0)); ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0)); ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1, TEST_ATTR_KEY_STRING, "testing caps hooray", TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_32KHZ, AST_FORMAT_ATTR_END)); /* Test is compatible */ if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0)) || !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) || !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) || !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_H264, 0)) || !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0)) || !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) { ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 1.\n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* Test things that are not compatible */ if (ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_SPEEX, 0)) || ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_SPEEX16, 0)) || ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_H261, 0))) { ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 2.\n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* Test compatiblity with format with attributes. */ if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1, TEST_ATTR_KEY_STRING, "testing caps hooray", TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ, AST_FORMAT_ATTR_END))) { ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 3.\n"); res = AST_TEST_FAIL; goto test3_cleanup; } if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, AST_FORMAT_ATTR_END))) { ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 4.\n"); res = AST_TEST_FAIL; goto test3_cleanup; } if (ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ, /* 48khz was not compatible, so this should fail iscompatible check */ AST_FORMAT_ATTR_END))) { ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 5.\n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* Lets start testing the functions that compare ast_format_cap objects. * Genreate the cap2 object to contain some similar formats as cap1 * and some different formats as well. */ ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)); ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)); ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_SIREN7, 0)); ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_H261, 0)); ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_T140, 0)); ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1, TEST_ATTR_KEY_STRING, "testing caps hooray", TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_12KHZ, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_32KHZ, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ, AST_FORMAT_ATTR_END)); /* find joint formats between cap1 and cap2 */ cap_joint = ast_format_cap_joint(cap1, cap2); if (!cap_joint) { ast_test_status_update(test, "failed to create joint capabilities correctly.\n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* determine if cap_joint is what we think it should be */ if (!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) || !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) || !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0)) || !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1, TEST_ATTR_KEY_STRING, "testing caps hooray", TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ, AST_FORMAT_ATTR_END))) { ast_test_status_update(test, "ast cap_joint failed to properly detect compatibility test 1.\n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* make sure joint cap does not have formats that should not be there */ if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_SIREN7, 0)) || ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1, TEST_ATTR_KEY_STRING, "testing caps hooray", TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ, AST_FORMAT_ATTR_END))) { ast_test_status_update(test, "ast cap_joint failed to properly detect compatibility test 1.\n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* Lets test removing a capability */ if (ast_format_cap_remove(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) { ast_test_status_update(test, "ast_format_cap_remove failed. \n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* Lets make sure what we just removed does not still exist */ if (ast_format_cap_iscompatible(cap_joint, &tmpformat)) { ast_test_status_update(test, "ast_format_cap_remove failed 2. \n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* Lets test removing a capability by id.*/ if (ast_format_cap_remove_byid(cap_joint, AST_FORMAT_GSM)) { ast_test_status_update(test, "ast_format_cap_remove failed 3. \n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* Lets make sure what we just removed does not still exist */ if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0))) { ast_test_status_update(test, "ast_format_cap_remove failed 4. \n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* lets test getting joint formats by type */ ast_format_cap_destroy(cap_joint); if (!(cap_joint = ast_format_cap_get_type(cap1, AST_FORMAT_TYPE_VIDEO))) { ast_test_status_update(test, "ast_format_cap_get_type failed.\n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* lets make sure our joint capability structure has what we expect */ if (!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_H264, 0)) || !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_H263, 0))) { ast_test_status_update(test, "get_type failed 2.\n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* now make sure joint does not have anything but video */ if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0)) || ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) || ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) || ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0)) || ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) { ast_test_status_update(test, "get_type failed 3.\n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* now lets remove everythign from cap_joint */ ast_format_cap_remove_all(cap_joint); if (!ast_format_cap_is_empty(cap_joint)) { ast_test_status_update(test, "failed to remove all\n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* now lets add all by type */ ast_format_cap_add_all_by_type(cap_joint, AST_FORMAT_TYPE_AUDIO); if (ast_format_cap_is_empty(cap_joint)) { ast_test_status_update(test, "failed to add all by type AUDIO\n"); res = AST_TEST_FAIL; } ast_format_cap_iter_start(cap_joint); while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) { if (AST_FORMAT_GET_TYPE(tmpformat.id) != AST_FORMAT_TYPE_AUDIO) { ast_test_status_update(test, "failed to add all by type AUDIO\n"); res = AST_TEST_FAIL; ast_format_cap_iter_end(cap_joint); goto test3_cleanup; } } ast_format_cap_iter_end(cap_joint); /* test append */ ast_format_cap_append(cap_joint, cap1); ast_format_cap_iter_start(cap1); while (!(ast_format_cap_iter_next(cap1, &tmpformat))) { if (!ast_format_cap_iscompatible(cap_joint, &tmpformat)) { ast_test_status_update(test, "failed to append format capabilities.\n"); res = AST_TEST_FAIL; ast_format_cap_iter_end(cap1); goto test3_cleanup; } } ast_format_cap_iter_end(cap1); /* test copy */ cap1 = ast_format_cap_destroy(cap1); cap1 = ast_format_cap_dup(cap_joint); if (!ast_format_cap_identical(cap_joint, cap1)) { ast_test_status_update(test, "failed to copy capabilities\n"); res = AST_TEST_FAIL; goto test3_cleanup; } /* test remove by type */ ast_format_cap_remove_bytype(cap_joint, AST_FORMAT_TYPE_AUDIO); if (ast_format_cap_has_type(cap_joint, AST_FORMAT_TYPE_AUDIO)) { ast_test_status_update(test, "failed to remove all by type audio\n"); res = AST_TEST_FAIL; goto test3_cleanup; } if (!ast_format_cap_has_type(cap_joint, AST_FORMAT_TYPE_TEXT)) { /* it should still have text */ ast_test_status_update(test, "failed to remove all by type audio\n"); res = AST_TEST_FAIL; goto test3_cleanup; } ast_format_cap_iter_start(cap_joint); while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) { if (AST_FORMAT_GET_TYPE(tmpformat.id) == AST_FORMAT_TYPE_AUDIO) { ast_test_status_update(test, "failed to remove all by type audio\n"); res = AST_TEST_FAIL; ast_format_cap_iter_end(cap_joint); goto test3_cleanup; } } ast_format_cap_iter_end(cap_joint); /* test add all */ ast_format_cap_remove_all(cap_joint); ast_format_cap_add_all(cap_joint); { int video = 0, audio = 0, text = 0, image = 0; ast_format_cap_iter_start(cap_joint); while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) { switch (AST_FORMAT_GET_TYPE(tmpformat.id)) { case AST_FORMAT_TYPE_AUDIO: audio++; break; case AST_FORMAT_TYPE_VIDEO: video++; break; case AST_FORMAT_TYPE_TEXT: text++; break; case AST_FORMAT_TYPE_IMAGE: image++; break; } } ast_format_cap_iter_end(cap_joint); if (!video || !audio || !text || !image) { ast_test_status_update(test, "failed to add all\n"); res = AST_TEST_FAIL; goto test3_cleanup; } } /* test copy2 */ ast_format_cap_copy(cap2, cap_joint); if (!ast_format_cap_identical(cap2, cap_joint)) { ast_test_status_update(test, "ast_format_cap_copy failed\n"); res = AST_TEST_FAIL; goto test3_cleanup; } test3_cleanup: ast_format_cap_destroy(cap1); ast_format_cap_destroy(cap2); ast_format_cap_destroy(cap_joint); /* unregister interface */ if (ast_format_attr_unreg_interface(&test_interface)) { ast_test_status_update(test, "test_interface failed to unregister.\n"); res = AST_TEST_FAIL; } return res; }
struct ast_unreal_pvt *ast_unreal_alloc(size_t size, ao2_destructor_fn destructor, struct ast_format_cap *cap) { struct ast_unreal_pvt *unreal; static const struct ast_jb_conf jb_conf = { .flags = 0, .max_size = -1, .resync_threshold = -1, .impl = "", .target_extra = -1, }; unreal = ao2_alloc(size, destructor); if (!unreal) { return NULL; } unreal->reqcap = ast_format_cap_dup(cap); if (!unreal->reqcap) { ao2_ref(unreal, -1); return NULL; } memcpy(&unreal->jb_conf, &jb_conf, sizeof(unreal->jb_conf)); return unreal; } struct ast_channel *ast_unreal_new_channels(struct ast_unreal_pvt *p, const struct ast_channel_tech *tech, int semi1_state, int semi2_state, const char *exten, const char *context, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, struct ast_callid *callid) { struct ast_channel *owner; struct ast_channel *chan; struct ast_format fmt; struct ast_assigned_ids id1 = {NULL, NULL}; struct ast_assigned_ids id2 = {NULL, NULL}; int generated_seqno = ast_atomic_fetchadd_int((int *) &name_sequence, +1); /* set unique ids for the two channels */ if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) { id1.uniqueid = assignedids->uniqueid; id2.uniqueid = assignedids->uniqueid2; } /* if id1 given but not id2, use default of id1;2 */ if (id1.uniqueid && ast_strlen_zero(id2.uniqueid)) { char *uniqueid2; uniqueid2 = ast_alloca(strlen(id1.uniqueid) + 2); strcpy(uniqueid2, id1.uniqueid);/* Safe */ strcat(uniqueid2, ";2");/* Safe */ id2.uniqueid = uniqueid2; } /* * Allocate two new Asterisk channels * * Make sure that the ;2 channel gets the same linkedid as ;1. * You can't pass linkedid to both allocations since if linkedid * isn't set, then each channel will generate its own linkedid. */ if (!(owner = ast_channel_alloc(1, semi1_state, NULL, NULL, NULL, exten, context, &id1, requestor, 0, "%s/%s-%08x;1", tech->type, p->name, generated_seqno))) { ast_log(LOG_WARNING, "Unable to allocate owner channel structure\n"); return NULL; } if (callid) { ast_channel_callid_set(owner, callid); } ast_channel_tech_set(owner, tech); ao2_ref(p, +1); ast_channel_tech_pvt_set(owner, p); ast_format_cap_copy(ast_channel_nativeformats(owner), p->reqcap); /* Determine our read/write format and set it on each channel */ ast_best_codec(p->reqcap, &fmt); ast_format_copy(ast_channel_writeformat(owner), &fmt); ast_format_copy(ast_channel_rawwriteformat(owner), &fmt); ast_format_copy(ast_channel_readformat(owner), &fmt); ast_format_copy(ast_channel_rawreadformat(owner), &fmt); ast_set_flag(ast_channel_flags(owner), AST_FLAG_DISABLE_DEVSTATE_CACHE); ast_jb_configure(owner, &p->jb_conf); if (ast_channel_cc_params_init(owner, requestor ? ast_channel_get_cc_config_params((struct ast_channel *) requestor) : NULL)) { ao2_ref(p, -1); ast_channel_unlock(owner); ast_channel_release(owner); return NULL; } p->owner = owner; ast_channel_unlock(owner); if (!(chan = ast_channel_alloc(1, semi2_state, NULL, NULL, NULL, exten, context, &id2, owner, 0, "%s/%s-%08x;2", tech->type, p->name, generated_seqno))) { ast_log(LOG_WARNING, "Unable to allocate chan channel structure\n"); ao2_ref(p, -1); ast_channel_release(owner); return NULL; } if (callid) { ast_channel_callid_set(chan, callid); } ast_channel_tech_set(chan, tech); ao2_ref(p, +1); ast_channel_tech_pvt_set(chan, p); ast_format_cap_copy(ast_channel_nativeformats(chan), p->reqcap); /* Format was already determined when setting up owner */ ast_format_copy(ast_channel_writeformat(chan), &fmt); ast_format_copy(ast_channel_rawwriteformat(chan), &fmt); ast_format_copy(ast_channel_readformat(chan), &fmt); ast_format_copy(ast_channel_rawreadformat(chan), &fmt); ast_set_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE); p->chan = chan; ast_channel_unlock(chan); return owner; }