| 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 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "asterisk/app.h" |
| 33 |
#include "asterisk/conversions.h" |
| 34 |
#include "asterisk/file.h" |
| 35 |
#include "asterisk/module.h" |
| 36 |
#include "asterisk/channel.h" |
| 37 |
#include "asterisk/stream.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
static const char app[] = "StreamEcho"; |
| 80 |
|
| 81 |
static int stream_echo_write_error(struct ast_channel *chan, int stream_num, struct ast_frame *frame) |
| 82 |
{ |
| 83 |
char frame_type[32]; |
| 84 |
const char *media_type; |
| 85 |
struct ast_stream *stream; |
| 86 |
|
| 87 |
ast_frame_type2str(frame->frametype, frame_type, sizeof(frame_type)); |
| 88 |
|
| 89 |
stream = stream_num < 0 ? |
| 90 |
ast_channel_get_default_stream(chan, ast_format_get_type(frame->subclass.format)) : |
| 91 |
ast_stream_topology_get_stream(ast_channel_get_stream_topology(chan), stream_num); |
| 92 |
|
| 93 |
if (!stream) { |
| 94 |
return -1; |
| 95 |
} |
| 96 |
|
| 97 |
media_type = ast_codec_media_type2str(ast_stream_get_type(stream)); |
| 98 |
|
| 99 |
ast_log(LOG_ERROR, "%s - unable to write frame type '%s' to stream type '%s' at " |
| 100 |
"position '%d'\n", ast_channel_name(chan), frame_type, media_type, |
| 101 |
ast_stream_get_position(stream)); |
| 102 |
|
| 103 |
return -1; |
| 104 |
} |
| 105 |
|
| 106 |
static int stream_echo_write(struct ast_channel *chan, struct ast_frame *frame, |
| 107 |
enum ast_media_type type, int one_to_one) |
| 108 |
{ |
| 109 |
int i; |
| 110 |
int num; |
| 111 |
struct ast_stream_topology *topology; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
num = ast_channel_is_multistream(chan) ? frame->stream_num : -1; |
| 118 |
if (ast_write_stream(chan, num, frame)) { |
| 119 |
return stream_echo_write_error(chan, num, frame); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
if (one_to_one || !frame->subclass.format || |
| 130 |
ast_format_get_type(frame->subclass.format) != type) { |
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
topology = ast_channel_get_stream_topology(chan); |
| 148 |
|
| 149 |
for (i = 0; i < ast_stream_topology_get_count(topology); ++i) { |
| 150 |
struct ast_stream *stream = ast_stream_topology_get_stream(topology, i); |
| 151 |
if (num != i && ast_stream_get_type(stream) == type) { |
| 152 |
if (ast_write_stream(chan, i, frame)) { |
| 153 |
return stream_echo_write_error(chan, i, frame); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
static int stream_echo_perform(struct ast_channel *chan, |
| 162 |
struct ast_stream_topology *topology, enum ast_media_type type) |
| 163 |
{ |
| 164 |
int update_sent = 0; |
| 165 |
int request_change = topology != NULL; |
| 166 |
int one_to_one = 1; |
| 167 |
|
| 168 |
while (ast_waitfor(chan, -1) > -1) { |
| 169 |
struct ast_frame *f; |
| 170 |
|
| 171 |
if (request_change) { |
| 172 |
|
| 173 |
if (ast_channel_request_stream_topology_change(chan, topology, NULL)) { |
| 174 |
ast_log(LOG_WARNING, "Request stream topology change not supported " |
| 175 |
"by channel '%s'\n", ast_channel_name(chan)); |
| 176 |
} |
| 177 |
request_change = 0; |
| 178 |
} |
| 179 |
|
| 180 |
f = ast_read_stream(chan); |
| 181 |
if (!f) { |
| 182 |
return -1; |
| 183 |
} |
| 184 |
|
| 185 |
if ((f->frametype == AST_FRAME_DTMF) && (f->subclass.integer == '#')) { |
| 186 |
ast_frfree(f); |
| 187 |
break; |
| 188 |
} |
| 189 |
|
| 190 |
f->delivery.tv_sec = 0; |
| 191 |
f->delivery.tv_usec = 0; |
| 192 |
|
| 193 |
if (f->frametype == AST_FRAME_CONTROL) { |
| 194 |
if (f->subclass.integer == AST_CONTROL_VIDUPDATE && !update_sent) { |
| 195 |
if (stream_echo_write(chan, f, type, one_to_one)) { |
| 196 |
ast_frfree(f); |
| 197 |
return -1; |
| 198 |
} |
| 199 |
update_sent = 1; |
| 200 |
} else if (f->subclass.integer == AST_CONTROL_SRCCHANGE) { |
| 201 |
update_sent = 0; |
| 202 |
} else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_CHANGED) { |
| 203 |
update_sent = 0; |
| 204 |
one_to_one = 0; |
| 205 |
} |
| 206 |
} else if (f->frametype == AST_FRAME_VIDEO && !update_sent){ |
| 207 |
struct ast_frame frame = { |
| 208 |
.frametype = AST_FRAME_CONTROL, |
| 209 |
.subclass.integer = AST_CONTROL_VIDUPDATE, |
| 210 |
}; |
| 211 |
stream_echo_write(chan, &frame, type, one_to_one); |
| 212 |
update_sent = 1; |
| 213 |
} |
| 214 |
|
| 215 |
if (f->frametype != AST_FRAME_CONTROL && |
| 216 |
f->frametype != AST_FRAME_MODEM && |
| 217 |
f->frametype != AST_FRAME_NULL && |
| 218 |
stream_echo_write(chan, f, type, one_to_one)) { |
| 219 |
ast_frfree(f); |
| 220 |
return -1; |
| 221 |
} |
| 222 |
|
| 223 |
ast_frfree(f); |
| 224 |
} |
| 225 |
|
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
static struct ast_stream_topology *stream_echo_topology_alloc( |
| 230 |
struct ast_stream_topology *original, unsigned int num, enum ast_media_type type) |
| 231 |
{ |
| 232 |
int i, n = num; |
| 233 |
struct ast_stream_topology *res = ast_stream_topology_alloc(); |
| 234 |
|
| 235 |
if (!res) { |
| 236 |
return NULL; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
for (i = 0; i < ast_stream_topology_get_count(original); ++i) { |
| 252 |
struct ast_stream *stream = ast_stream_topology_get_stream(original, i); |
| 253 |
|
| 254 |
if (!n && ast_stream_get_type(stream) == type) { |
| 255 |
|
| 256 |
continue; |
| 257 |
} |
| 258 |
|
| 259 |
if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) { |
| 260 |
|
| 261 |
continue; |
| 262 |
} |
| 263 |
|
| 264 |
do { |
| 265 |
stream = ast_stream_clone(stream, NULL); |
| 266 |
|
| 267 |
if (!stream || ast_stream_topology_append_stream(res, stream) < 0) { |
| 268 |
ast_stream_free(stream); |
| 269 |
ast_stream_topology_free(res); |
| 270 |
return NULL; |
| 271 |
} |
| 272 |
|
| 273 |
if (ast_stream_get_type(stream) != type) { |
| 274 |
|
| 275 |
break; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
ast_stream_set_state(stream, n == num ? |
| 284 |
AST_STREAM_STATE_SENDRECV : AST_STREAM_STATE_SENDONLY); |
| 285 |
} while (--n); |
| 286 |
} |
| 287 |
|
| 288 |
return res; |
| 289 |
} |
| 290 |
|
| 291 |
static int stream_echo_exec(struct ast_channel *chan, const char *data) |
| 292 |
{ |
| 293 |
int res; |
| 294 |
unsigned int num = 0; |
| 295 |
enum ast_media_type type; |
| 296 |
char *parse; |
| 297 |
struct ast_stream_topology *topology; |
| 298 |
|
| 299 |
AST_DECLARE_APP_ARGS(args, |
| 300 |
AST_APP_ARG(num); |
| 301 |
AST_APP_ARG(type); |
| 302 |
); |
| 303 |
|
| 304 |
parse = ast_strdupa(data); |
| 305 |
AST_STANDARD_APP_ARGS(args, parse); |
| 306 |
|
| 307 |
if (ast_strlen_zero(args.num)) { |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
return stream_echo_perform(chan, NULL, AST_MEDIA_TYPE_UNKNOWN); |
| 314 |
} |
| 315 |
|
| 316 |
if (ast_str_to_uint(args.num, &num)) { |
| 317 |
ast_log(LOG_ERROR, "Failed to parse the first parameter '%s' into a" |
| 318 |
" greater than or equal to zero\n", args.num); |
| 319 |
return -1; |
| 320 |
} |
| 321 |
|
| 322 |
type = ast_strlen_zero(args.type) ? AST_MEDIA_TYPE_VIDEO : |
| 323 |
ast_media_type_from_str(args.type); |
| 324 |
|
| 325 |
topology = stream_echo_topology_alloc( |
| 326 |
ast_channel_get_stream_topology(chan), num, type); |
| 327 |
if (!topology) { |
| 328 |
ast_log(LOG_ERROR, "Unable to create '%u' streams of type '%s' to" |
| 329 |
" the topology\n", num, ast_codec_media_type2str(type)); |
| 330 |
return -1; |
| 331 |
} |
| 332 |
|
| 333 |
res = stream_echo_perform(chan, topology, type); |
| 334 |
|
| 335 |
if (ast_channel_get_stream_topology(chan) != topology) { |
| 336 |
ast_stream_topology_free(topology); |
| 337 |
} |
| 338 |
|
| 339 |
return res; |
| 340 |
} |
| 341 |
|
| 342 |
static int unload_module(void) |
| 343 |
{ |
| 344 |
return ast_unregister_application(app); |
| 345 |
} |
| 346 |
|
| 347 |
static int load_module(void) |
| 348 |
{ |
| 349 |
return ast_register_application_xml(app, stream_echo_exec); |
| 350 |
} |
| 351 |
|
| 352 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stream Echo Application"); |