123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include "asterisk/module.h"
- #include "asterisk/channel.h"
- #include "asterisk/bridge.h"
- #include "asterisk/bridge_technology.h"
- #include "asterisk/frame.h"
- #include "asterisk/rtp_engine.h"
- struct native_rtp_bridge_data {
-
- int id;
- };
- static enum ast_rtp_glue_result native_rtp_bridge_get(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp_glue **glue0,
- struct ast_rtp_glue **glue1, struct ast_rtp_instance **instance0, struct ast_rtp_instance **instance1,
- struct ast_rtp_instance **vinstance0, struct ast_rtp_instance **vinstance1)
- {
- enum ast_rtp_glue_result audio_glue0_res;
- enum ast_rtp_glue_result video_glue0_res;
- enum ast_rtp_glue_result audio_glue1_res;
- enum ast_rtp_glue_result video_glue1_res;
- if (!(*glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) ||
- !(*glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
- return AST_RTP_GLUE_RESULT_FORBID;
- }
- audio_glue0_res = (*glue0)->get_rtp_info(c0, instance0);
- video_glue0_res = (*glue0)->get_vrtp_info ? (*glue0)->get_vrtp_info(c0, vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
- audio_glue1_res = (*glue1)->get_rtp_info(c1, instance1);
- video_glue1_res = (*glue1)->get_vrtp_info ? (*glue1)->get_vrtp_info(c1, vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
-
- if (audio_glue0_res == audio_glue1_res && audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
- if ((*glue0)->allow_rtp_remote && !((*glue0)->allow_rtp_remote(c0, *instance1))) {
-
- audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
- } else if ((*glue1)->allow_rtp_remote && !((*glue1)->allow_rtp_remote(c1, *instance0))) {
- audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
- }
- }
- if (video_glue0_res == video_glue1_res && video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
- if ((*glue0)->allow_vrtp_remote && !((*glue0)->allow_vrtp_remote(c0, *instance1))) {
-
- video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
- } else if ((*glue1)->allow_vrtp_remote && !((*glue1)->allow_vrtp_remote(c1, *instance0))) {
- video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
- }
- }
-
- if (video_glue0_res != AST_RTP_GLUE_RESULT_FORBID
- && (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE
- || video_glue0_res != AST_RTP_GLUE_RESULT_REMOTE)) {
- audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
- }
- if (video_glue1_res != AST_RTP_GLUE_RESULT_FORBID
- && (audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE
- || video_glue1_res != AST_RTP_GLUE_RESULT_REMOTE)) {
- audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
- }
-
- if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID ||
- audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
-
- return AST_RTP_GLUE_RESULT_FORBID;
- } else if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL ||
- audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
- return AST_RTP_GLUE_RESULT_LOCAL;
- } else {
- return AST_RTP_GLUE_RESULT_REMOTE;
- }
- }
- static void native_rtp_bridge_start(struct ast_bridge *bridge, struct ast_channel *target)
- {
- struct ast_bridge_channel *bc0 = AST_LIST_FIRST(&bridge->channels);
- struct ast_bridge_channel *bc1 = AST_LIST_LAST(&bridge->channels);
- enum ast_rtp_glue_result native_type;
- struct ast_rtp_glue *glue0, *glue1;
- RAII_VAR(struct ast_rtp_instance *, instance0, NULL, ao2_cleanup);
- RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ao2_cleanup);
- RAII_VAR(struct ast_rtp_instance *, vinstance0, NULL, ao2_cleanup);
- RAII_VAR(struct ast_rtp_instance *, vinstance1, NULL, ao2_cleanup);
- RAII_VAR(struct ast_rtp_instance *, tinstance0, NULL, ao2_cleanup);
- RAII_VAR(struct ast_rtp_instance *, tinstance1, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format_cap *, cap0, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
- RAII_VAR(struct ast_format_cap *, cap1, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
- if (bc0 == bc1) {
- return;
- }
- ast_channel_lock_both(bc0->chan, bc1->chan);
- native_type = native_rtp_bridge_get(bc0->chan, bc1->chan, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1);
- switch (native_type) {
- case AST_RTP_GLUE_RESULT_LOCAL:
- if (ast_rtp_instance_get_engine(instance0)->local_bridge) {
- ast_rtp_instance_get_engine(instance0)->local_bridge(instance0, instance1);
- }
- if (ast_rtp_instance_get_engine(instance1)->local_bridge) {
- ast_rtp_instance_get_engine(instance1)->local_bridge(instance1, instance0);
- }
- ast_rtp_instance_set_bridged(instance0, instance1);
- ast_rtp_instance_set_bridged(instance1, instance0);
- ast_debug(2, "Locally RTP bridged '%s' and '%s' in stack\n",
- ast_channel_name(bc0->chan), ast_channel_name(bc1->chan));
- break;
- case AST_RTP_GLUE_RESULT_REMOTE:
- if (glue0->get_codec) {
- glue0->get_codec(bc0->chan, cap0);
- }
- if (glue1->get_codec) {
- glue1->get_codec(bc1->chan, cap1);
- }
-
- if (!target) {
- glue0->update_peer(bc0->chan, instance1, vinstance1, tinstance1, cap1, 0);
- glue1->update_peer(bc1->chan, instance0, vinstance0, tinstance0, cap0, 0);
- ast_debug(2, "Remotely bridged '%s' and '%s' - media will flow directly between them\n",
- ast_channel_name(bc0->chan), ast_channel_name(bc1->chan));
- } else {
-
- if (bc0->chan == target) {
- glue0->update_peer(bc0->chan, instance1, vinstance1, tinstance1, cap1, 0);
- } else {
- glue1->update_peer(bc1->chan, instance0, vinstance0, tinstance0, cap0, 0);
- }
- }
- break;
- case AST_RTP_GLUE_RESULT_FORBID:
- break;
- }
- ast_channel_unlock(bc0->chan);
- ast_channel_unlock(bc1->chan);
- }
- static void native_rtp_bridge_stop(struct ast_bridge *bridge, struct ast_channel *target)
- {
- struct ast_bridge_channel *bc0 = AST_LIST_FIRST(&bridge->channels);
- struct ast_bridge_channel *bc1 = AST_LIST_LAST(&bridge->channels);
- enum ast_rtp_glue_result native_type;
- struct ast_rtp_glue *glue0, *glue1 = NULL;
- RAII_VAR(struct ast_rtp_instance *, instance0, NULL, ao2_cleanup);
- RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ao2_cleanup);
- RAII_VAR(struct ast_rtp_instance *, vinstance0, NULL, ao2_cleanup);
- RAII_VAR(struct ast_rtp_instance *, vinstance1, NULL, ao2_cleanup);
- if (bc0 == bc1) {
- return;
- }
- ast_channel_lock_both(bc0->chan, bc1->chan);
- native_type = native_rtp_bridge_get(bc0->chan, bc1->chan, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1);
- switch (native_type) {
- case AST_RTP_GLUE_RESULT_LOCAL:
- if (ast_rtp_instance_get_engine(instance0)->local_bridge) {
- ast_rtp_instance_get_engine(instance0)->local_bridge(instance0, NULL);
- }
- if (instance1 && ast_rtp_instance_get_engine(instance1)->local_bridge) {
- ast_rtp_instance_get_engine(instance1)->local_bridge(instance1, NULL);
- }
- ast_rtp_instance_set_bridged(instance0, NULL);
- if (instance1) {
- ast_rtp_instance_set_bridged(instance1, NULL);
- }
- break;
- case AST_RTP_GLUE_RESULT_REMOTE:
- if (target) {
-
- if (bc0->chan == target) {
- glue0->update_peer(bc0->chan, NULL, NULL, NULL, NULL, 0);
- } else {
- glue1->update_peer(bc1->chan, NULL, NULL, NULL, NULL, 0);
- }
- }
- break;
- case AST_RTP_GLUE_RESULT_FORBID:
- break;
- }
- if (!target && native_type != AST_RTP_GLUE_RESULT_FORBID) {
- glue0->update_peer(bc0->chan, NULL, NULL, NULL, NULL, 0);
- glue1->update_peer(bc1->chan, NULL, NULL, NULL, NULL, 0);
- }
- ast_debug(2, "Discontinued RTP bridging of '%s' and '%s' - media will flow through Asterisk core\n",
- ast_channel_name(bc0->chan), ast_channel_name(bc1->chan));
- ast_channel_unlock(bc0->chan);
- ast_channel_unlock(bc1->chan);
- }
- static struct ast_frame *native_rtp_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)
- {
- RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
- if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {
- return f;
- }
- bridge = ast_channel_get_bridge(chan);
- if (bridge) {
-
- ast_channel_unlock(chan);
- ast_bridge_lock(bridge);
- if (f->subclass.integer == AST_CONTROL_HOLD) {
- native_rtp_bridge_stop(bridge, chan);
- } else if ((f->subclass.integer == AST_CONTROL_UNHOLD) || (f->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
- native_rtp_bridge_start(bridge, chan);
- }
- ast_bridge_unlock(bridge);
- ast_channel_lock(chan);
- }
- return f;
- }
- static int native_rtp_framehook_consume(void *data, enum ast_frame_type type)
- {
- return (type == AST_FRAME_CONTROL ? 1 : 0);
- }
- static int native_rtp_bridge_capable(struct ast_channel *chan)
- {
- return !ast_channel_has_hook_requiring_audio(chan);
- }
- static int native_rtp_bridge_compatible(struct ast_bridge *bridge)
- {
- struct ast_bridge_channel *bc0 = AST_LIST_FIRST(&bridge->channels);
- struct ast_bridge_channel *bc1 = AST_LIST_LAST(&bridge->channels);
- enum ast_rtp_glue_result native_type;
- struct ast_rtp_glue *glue0, *glue1;
- RAII_VAR(struct ast_rtp_instance *, instance0, NULL, ao2_cleanup);
- RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ao2_cleanup);
- RAII_VAR(struct ast_rtp_instance *, vinstance0, NULL, ao2_cleanup);
- RAII_VAR(struct ast_rtp_instance *, vinstance1, NULL, ao2_cleanup);
- RAII_VAR(struct ast_format_cap *, cap0, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
- RAII_VAR(struct ast_format_cap *, cap1, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
- int read_ptime0, read_ptime1, write_ptime0, write_ptime1;
-
- if (bridge->num_channels != 2) {
- ast_debug(1, "Bridge '%s' can not use native RTP bridge as two channels are required\n",
- bridge->uniqueid);
- return 0;
- }
- if (!native_rtp_bridge_capable(bc0->chan)) {
- ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has features which prevent it\n",
- bridge->uniqueid, ast_channel_name(bc0->chan));
- return 0;
- }
- if (!native_rtp_bridge_capable(bc1->chan)) {
- ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has features which prevent it\n",
- bridge->uniqueid, ast_channel_name(bc1->chan));
- return 0;
- }
- if ((native_type = native_rtp_bridge_get(bc0->chan, bc1->chan, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1))
- == AST_RTP_GLUE_RESULT_FORBID) {
- ast_debug(1, "Bridge '%s' can not use native RTP bridge as it was forbidden while getting details\n",
- bridge->uniqueid);
- return 0;
- }
- if (ao2_container_count(bc0->features->dtmf_hooks) && ast_rtp_instance_dtmf_mode_get(instance0)) {
- ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has DTMF hooks\n",
- bridge->uniqueid, ast_channel_name(bc0->chan));
- return 0;
- }
- if (ao2_container_count(bc1->features->dtmf_hooks) && ast_rtp_instance_dtmf_mode_get(instance1)) {
- ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has DTMF hooks\n",
- bridge->uniqueid, ast_channel_name(bc1->chan));
- return 0;
- }
- if ((native_type == AST_RTP_GLUE_RESULT_LOCAL) && ((ast_rtp_instance_get_engine(instance0)->local_bridge !=
- ast_rtp_instance_get_engine(instance1)->local_bridge) ||
- (ast_rtp_instance_get_engine(instance0)->dtmf_compatible &&
- !ast_rtp_instance_get_engine(instance0)->dtmf_compatible(bc0->chan, instance0, bc1->chan, instance1)))) {
- ast_debug(1, "Bridge '%s' can not use local native RTP bridge as local bridge or DTMF is not compatible\n",
- bridge->uniqueid);
- return 0;
- }
-
- if (glue0->get_codec) {
- glue0->get_codec(bc0->chan, cap0);
- }
- if (glue1->get_codec) {
- glue1->get_codec(bc1->chan, cap1);
- }
- if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
- char tmp0[256] = { 0, }, tmp1[256] = { 0, };
- ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
- ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
- ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
- return 0;
- }
- read_ptime0 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance0)->pref, ast_channel_rawreadformat(bc0->chan))).cur_ms;
- read_ptime1 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance1)->pref, ast_channel_rawreadformat(bc1->chan))).cur_ms;
- write_ptime0 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance0)->pref, ast_channel_rawwriteformat(bc0->chan))).cur_ms;
- write_ptime1 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance1)->pref, ast_channel_rawwriteformat(bc1->chan))).cur_ms;
- if (read_ptime0 != write_ptime1 || read_ptime1 != write_ptime0) {
- ast_debug(1, "Packetization differs between RTP streams (%d != %d or %d != %d). Cannot native bridge in RTP\n",
- read_ptime0, write_ptime1, read_ptime1, write_ptime0);
- return 0;
- }
- return 1;
- }
- static int native_rtp_bridge_framehook_attach(struct ast_bridge_channel *bridge_channel)
- {
- struct native_rtp_bridge_data *data = ao2_alloc(sizeof(*data), NULL);
- static struct ast_framehook_interface hook = {
- .version = AST_FRAMEHOOK_INTERFACE_VERSION,
- .event_cb = native_rtp_framehook,
- .consume_cb = native_rtp_framehook_consume,
- .disable_inheritance = 1,
- };
- if (!data) {
- return -1;
- }
- ast_channel_lock(bridge_channel->chan);
- data->id = ast_framehook_attach(bridge_channel->chan, &hook);
- ast_channel_unlock(bridge_channel->chan);
- if (data->id < 0) {
- ao2_cleanup(data);
- return -1;
- }
- bridge_channel->tech_pvt = data;
- return 0;
- }
- static void native_rtp_bridge_framehook_detach(struct ast_bridge_channel *bridge_channel)
- {
- RAII_VAR(struct native_rtp_bridge_data *, data, bridge_channel->tech_pvt, ao2_cleanup);
- if (!data) {
- return;
- }
- ast_channel_lock(bridge_channel->chan);
- ast_framehook_detach(bridge_channel->chan, data->id);
- ast_channel_unlock(bridge_channel->chan);
- bridge_channel->tech_pvt = NULL;
- }
- static int native_rtp_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
- {
- native_rtp_bridge_framehook_detach(bridge_channel);
- if (native_rtp_bridge_framehook_attach(bridge_channel)) {
- return -1;
- }
- native_rtp_bridge_start(bridge, NULL);
- return 0;
- }
- static void native_rtp_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
- {
- native_rtp_bridge_join(bridge, bridge_channel);
- }
- static void native_rtp_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
- {
- native_rtp_bridge_framehook_detach(bridge_channel);
- native_rtp_bridge_stop(bridge, NULL);
- }
- static int native_rtp_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
- {
- return ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
- }
- static struct ast_bridge_technology native_rtp_bridge = {
- .name = "native_rtp",
- .capabilities = AST_BRIDGE_CAPABILITY_NATIVE,
- .preference = AST_BRIDGE_PREFERENCE_BASE_NATIVE,
- .join = native_rtp_bridge_join,
- .unsuspend = native_rtp_bridge_unsuspend,
- .leave = native_rtp_bridge_leave,
- .suspend = native_rtp_bridge_leave,
- .write = native_rtp_bridge_write,
- .compatible = native_rtp_bridge_compatible,
- };
- static int unload_module(void)
- {
- ast_format_cap_destroy(native_rtp_bridge.format_capabilities);
- return ast_bridge_technology_unregister(&native_rtp_bridge);
- }
- static int load_module(void)
- {
- if (!(native_rtp_bridge.format_capabilities = ast_format_cap_alloc(0))) {
- return AST_MODULE_LOAD_DECLINE;
- }
- ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_AUDIO);
- ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_VIDEO);
- ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_TEXT);
- return ast_bridge_technology_register(&native_rtp_bridge);
- }
- AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Native RTP bridging module");
|