| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <string.h> |
| 37 |
#include <sys/types.h> |
| 38 |
#include <sys/stat.h> |
| 39 |
|
| 40 |
#include "asterisk/module.h" |
| 41 |
#include "asterisk/channel.h" |
| 42 |
#include "asterisk/bridge.h" |
| 43 |
#include "asterisk/bridge_technology.h" |
| 44 |
#include "asterisk/frame.h" |
| 45 |
#include "asterisk/stream.h" |
| 46 |
|
| 47 |
static void simple_bridge_stream_topology_changed(struct ast_bridge *bridge, |
| 48 |
struct ast_bridge_channel *bridge_channel); |
| 49 |
static int simple_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel); |
| 50 |
static int simple_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame); |
| 51 |
|
| 52 |
static struct ast_bridge_technology simple_bridge = { |
| 53 |
.name = "simple_bridge", |
| 54 |
.capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX, |
| 55 |
.preference = AST_BRIDGE_PREFERENCE_BASE_1TO1MIX, |
| 56 |
.join = simple_bridge_join, |
| 57 |
.write = simple_bridge_write, |
| 58 |
.stream_topology_changed = simple_bridge_stream_topology_changed, |
| 59 |
}; |
| 60 |
|
| 61 |
static struct ast_stream_topology *simple_bridge_request_stream_topology_update( |
| 62 |
struct ast_stream_topology *existing_topology, |
| 63 |
struct ast_stream_topology *requested_topology) |
| 64 |
{ |
| 65 |
struct ast_stream *stream; |
| 66 |
const struct ast_format_cap *audio_formats = NULL; |
| 67 |
struct ast_stream_topology *new_topology; |
| 68 |
int i; |
| 69 |
|
| 70 |
new_topology = ast_stream_topology_clone(requested_topology); |
| 71 |
if (!new_topology) { |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
for (i = 0; i < ast_stream_topology_get_count(existing_topology); ++i) { |
| 80 |
stream = ast_stream_topology_get_stream(existing_topology, i); |
| 81 |
|
| 82 |
if (ast_stream_get_type(stream) != AST_MEDIA_TYPE_AUDIO || |
| 83 |
ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) { |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
audio_formats = ast_stream_get_formats(stream); |
| 88 |
break; |
| 89 |
} |
| 90 |
|
| 91 |
if (audio_formats) { |
| 92 |
for (i = 0; i < ast_stream_topology_get_count(new_topology); ++i) { |
| 93 |
stream = ast_stream_topology_get_stream(new_topology, i); |
| 94 |
|
| 95 |
if (ast_stream_get_type(stream) != AST_MEDIA_TYPE_AUDIO || |
| 96 |
ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) { |
| 97 |
continue; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
ast_stream_set_formats(stream, (struct ast_format_cap *)audio_formats); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
for (i = 0; i < ast_stream_topology_get_count(new_topology); ++i) { |
| 106 |
stream = ast_stream_topology_get_stream(new_topology, i); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
if (ast_stream_get_state(stream) == AST_STREAM_STATE_RECVONLY) { |
| 115 |
ast_stream_set_state(stream, AST_STREAM_STATE_SENDONLY); |
| 116 |
} else if (ast_stream_get_state(stream) == AST_STREAM_STATE_SENDONLY) { |
| 117 |
ast_stream_set_state(stream, AST_STREAM_STATE_RECVONLY); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return new_topology; |
| 122 |
} |
| 123 |
|
| 124 |
static int simple_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel) |
| 125 |
{ |
| 126 |
struct ast_stream_topology *req_top; |
| 127 |
struct ast_stream_topology *existing_top; |
| 128 |
struct ast_stream_topology *new_top; |
| 129 |
struct ast_channel *c0 = AST_LIST_FIRST(&bridge->channels)->chan; |
| 130 |
struct ast_channel *c1 = AST_LIST_LAST(&bridge->channels)->chan; |
| 131 |
int unhold_c0, unhold_c1; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
if (c0 == c1) { |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
if (ast_channel_make_compatible(c0, c1)) { |
| 142 |
return -1; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
ast_channel_lock_both(c0, c1); |
| 149 |
req_top = ast_channel_get_stream_topology(c0); |
| 150 |
existing_top = ast_channel_get_stream_topology(c1); |
| 151 |
if (ast_stream_topology_get_count(req_top) < ast_stream_topology_get_count(existing_top)) { |
| 152 |
SWAP(req_top, existing_top); |
| 153 |
SWAP(c0, c1); |
| 154 |
} |
| 155 |
new_top = simple_bridge_request_stream_topology_update(existing_top, req_top); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
unhold_c1 = (ast_channel_hold_state(c1) == AST_CONTROL_HOLD); |
| 161 |
if (unhold_c1) { |
| 162 |
ast_debug(1, "Channel %s simulating UNHOLD for bridge simple join.\n", ast_channel_name(c1)); |
| 163 |
} |
| 164 |
|
| 165 |
unhold_c0 = (ast_channel_hold_state(c0) == AST_CONTROL_HOLD); |
| 166 |
if (unhold_c0) { |
| 167 |
ast_debug(1, "Channel %s simulating UNHOLD for bridge simple join.\n", ast_channel_name(c0)); |
| 168 |
} |
| 169 |
|
| 170 |
ast_channel_unlock(c0); |
| 171 |
ast_channel_unlock(c1); |
| 172 |
|
| 173 |
if (unhold_c1) { |
| 174 |
ast_indicate(c1, AST_CONTROL_UNHOLD); |
| 175 |
} |
| 176 |
|
| 177 |
if (unhold_c0) { |
| 178 |
ast_indicate(c0, AST_CONTROL_UNHOLD); |
| 179 |
} |
| 180 |
|
| 181 |
if (!new_top) { |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
if (!ast_stream_topology_equal(new_top, existing_top)) { |
| 187 |
ast_channel_request_stream_topology_change(c1, new_top, &simple_bridge); |
| 188 |
} else { |
| 189 |
ast_debug(3, "%s: Topologies already match. Current: %s Requested: %s\n", |
| 190 |
ast_channel_name(c1), |
| 191 |
ast_str_tmp(256, ast_stream_topology_to_str(existing_top, &STR_TMP)), |
| 192 |
ast_str_tmp(256, ast_stream_topology_to_str(new_top, &STR_TMP))); |
| 193 |
} |
| 194 |
ast_stream_topology_free(new_top); |
| 195 |
|
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
static int simple_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame) |
| 200 |
{ |
| 201 |
const struct ast_control_t38_parameters *t38_parameters; |
| 202 |
int defer = 0; |
| 203 |
|
| 204 |
if (!ast_bridge_queue_everyone_else(bridge, bridge_channel, frame)) { |
| 205 |
|
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
switch (frame->frametype) { |
| 211 |
case AST_FRAME_CONTROL: |
| 212 |
switch (frame->subclass.integer) { |
| 213 |
case AST_CONTROL_T38_PARAMETERS: |
| 214 |
t38_parameters = frame->data.ptr; |
| 215 |
switch (t38_parameters->request_response) { |
| 216 |
case AST_T38_REQUEST_NEGOTIATE: |
| 217 |
defer = -1; |
| 218 |
break; |
| 219 |
default: |
| 220 |
break; |
| 221 |
} |
| 222 |
break; |
| 223 |
default: |
| 224 |
break; |
| 225 |
} |
| 226 |
break; |
| 227 |
default: |
| 228 |
break; |
| 229 |
} |
| 230 |
|
| 231 |
return defer; |
| 232 |
} |
| 233 |
|
| 234 |
static void simple_bridge_stream_topology_changed(struct ast_bridge *bridge, |
| 235 |
struct ast_bridge_channel *bridge_channel) |
| 236 |
{ |
| 237 |
struct ast_channel *c0 = bridge_channel->chan; |
| 238 |
struct ast_channel *c1 = AST_LIST_FIRST(&bridge->channels)->chan; |
| 239 |
struct ast_stream_topology *req_top; |
| 240 |
struct ast_stream_topology *existing_top; |
| 241 |
struct ast_stream_topology *new_top; |
| 242 |
|
| 243 |
ast_bridge_channel_stream_map(bridge_channel); |
| 244 |
|
| 245 |
if (ast_channel_get_stream_topology_change_source(bridge_channel->chan) |
| 246 |
== &simple_bridge) { |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
if (c0 == c1) { |
| 251 |
c1 = AST_LIST_LAST(&bridge->channels)->chan; |
| 252 |
} |
| 253 |
|
| 254 |
if (c0 == c1) { |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
ast_channel_lock_both(c0, c1); |
| 262 |
req_top = ast_channel_get_stream_topology(c0); |
| 263 |
existing_top = ast_channel_get_stream_topology(c1); |
| 264 |
new_top = simple_bridge_request_stream_topology_update(existing_top, req_top); |
| 265 |
ast_channel_unlock(c0); |
| 266 |
ast_channel_unlock(c1); |
| 267 |
|
| 268 |
if (!new_top) { |
| 269 |
|
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
ast_channel_request_stream_topology_change(c1, new_top, &simple_bridge); |
| 274 |
ast_stream_topology_free(new_top); |
| 275 |
} |
| 276 |
|
| 277 |
static int unload_module(void) |
| 278 |
{ |
| 279 |
ast_bridge_technology_unregister(&simple_bridge); |
| 280 |
return 0; |
| 281 |
} |
| 282 |
|
| 283 |
static int load_module(void) |
| 284 |
{ |
| 285 |
if (ast_bridge_technology_register(&simple_bridge)) { |
| 286 |
unload_module(); |
| 287 |
return AST_MODULE_LOAD_DECLINE; |
| 288 |
} |
| 289 |
return AST_MODULE_LOAD_SUCCESS; |
| 290 |
} |
| 291 |
|
| 292 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Simple two channel bridging module"); |