| 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 <dirent.h> |
| 33 |
#include <sys/stat.h> |
| 34 |
#include <sys/wait.h> |
| 35 |
#include <math.h> |
| 36 |
|
| 37 |
#include "asterisk/_private.h" |
| 38 |
#include "asterisk/paths.h" |
| 39 |
#include "asterisk/mod_format.h" |
| 40 |
#include "asterisk/cli.h" |
| 41 |
#include "asterisk/channel.h" |
| 42 |
#include "asterisk/cel.h" |
| 43 |
#include "asterisk/sched.h" |
| 44 |
#include "asterisk/translate.h" |
| 45 |
#include "asterisk/utils.h" |
| 46 |
#include "asterisk/lock.h" |
| 47 |
#include "asterisk/app.h" |
| 48 |
#include "asterisk/pbx.h" |
| 49 |
#include "asterisk/linkedlists.h" |
| 50 |
#include "asterisk/module.h" |
| 51 |
#include "asterisk/astobj2.h" |
| 52 |
#include "asterisk/test.h" |
| 53 |
#include "asterisk/stasis.h" |
| 54 |
#include "asterisk/json.h" |
| 55 |
#include "asterisk/stasis_system.h" |
| 56 |
#include "asterisk/media_cache.h" |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
int ast_language_is_prefix = 1; |
| 69 |
|
| 70 |
static AST_RWLIST_HEAD_STATIC(formats, ast_format_def); |
| 71 |
|
| 72 |
STASIS_MESSAGE_TYPE_DEFN(ast_format_register_type); |
| 73 |
STASIS_MESSAGE_TYPE_DEFN(ast_format_unregister_type); |
| 74 |
|
| 75 |
static struct ast_json *json_array_from_list(const char *list, const char *sep) |
| 76 |
{ |
| 77 |
RAII_VAR(struct ast_json *, array, ast_json_array_create(), ast_json_unref); |
| 78 |
char *stringp, *ext; |
| 79 |
|
| 80 |
stringp = ast_strdupa(list); |
| 81 |
if (!array || !stringp) { |
| 82 |
return NULL; |
| 83 |
} |
| 84 |
|
| 85 |
while ((ext = strsep(&stringp, sep))) { |
| 86 |
if (ast_json_array_append(array, ast_json_string_create(ext))) { |
| 87 |
return NULL; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return ast_json_ref(array); |
| 92 |
} |
| 93 |
|
| 94 |
static int publish_format_update(const struct ast_format_def *f, struct stasis_message_type *type) |
| 95 |
{ |
| 96 |
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); |
| 97 |
RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup); |
| 98 |
RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref); |
| 99 |
|
| 100 |
if (!type) { |
| 101 |
return -1; |
| 102 |
} |
| 103 |
|
| 104 |
json_object = ast_json_pack("{s: s, s: o}", |
| 105 |
"format", f->name, |
| 106 |
"extensions", json_array_from_list(f->exts, "|")); |
| 107 |
if (!json_object) { |
| 108 |
return -1; |
| 109 |
} |
| 110 |
|
| 111 |
json_payload = ast_json_payload_create(json_object); |
| 112 |
if (!json_payload) { |
| 113 |
return -1; |
| 114 |
} |
| 115 |
|
| 116 |
msg = stasis_message_create(type, json_payload); |
| 117 |
if (!msg) { |
| 118 |
return -1; |
| 119 |
} |
| 120 |
|
| 121 |
stasis_publish(ast_system_topic(), msg); |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
int __ast_format_def_register(const struct ast_format_def *f, struct ast_module *mod) |
| 126 |
{ |
| 127 |
struct ast_format_def *tmp; |
| 128 |
|
| 129 |
AST_RWLIST_WRLOCK(&formats); |
| 130 |
AST_RWLIST_TRAVERSE(&formats, tmp, list) { |
| 131 |
if (!strcasecmp(f->name, tmp->name)) { |
| 132 |
AST_RWLIST_UNLOCK(&formats); |
| 133 |
ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", f->name); |
| 134 |
return -1; |
| 135 |
} |
| 136 |
} |
| 137 |
if (!(tmp = ast_calloc(1, sizeof(*tmp)))) { |
| 138 |
AST_RWLIST_UNLOCK(&formats); |
| 139 |
return -1; |
| 140 |
} |
| 141 |
*tmp = *f; |
| 142 |
tmp->module = mod; |
| 143 |
if (tmp->buf_size) { |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
struct _test_align { void *a, *b; } p; |
| 149 |
int align = (char *)&p.b - (char *)&p.a; |
| 150 |
tmp->buf_size = ((f->buf_size + align - 1) / align) * align; |
| 151 |
} |
| 152 |
|
| 153 |
memset(&tmp->list, 0, sizeof(tmp->list)); |
| 154 |
|
| 155 |
AST_RWLIST_INSERT_HEAD(&formats, tmp, list); |
| 156 |
AST_RWLIST_UNLOCK(&formats); |
| 157 |
ast_verb(5, "Registered file format %s, extension(s) %s\n", f->name, f->exts); |
| 158 |
publish_format_update(f, ast_format_register_type()); |
| 159 |
|
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
int ast_format_def_unregister(const char *name) |
| 164 |
{ |
| 165 |
struct ast_format_def *tmp; |
| 166 |
int res = -1; |
| 167 |
|
| 168 |
AST_RWLIST_WRLOCK(&formats); |
| 169 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) { |
| 170 |
if (!strcasecmp(name, tmp->name)) { |
| 171 |
AST_RWLIST_REMOVE_CURRENT(list); |
| 172 |
publish_format_update(tmp, ast_format_unregister_type()); |
| 173 |
ast_free(tmp); |
| 174 |
res = 0; |
| 175 |
} |
| 176 |
} |
| 177 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 178 |
AST_RWLIST_UNLOCK(&formats); |
| 179 |
|
| 180 |
if (!res) |
| 181 |
ast_verb(5, "Unregistered format %s\n", name); |
| 182 |
else |
| 183 |
ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name); |
| 184 |
|
| 185 |
return res; |
| 186 |
} |
| 187 |
|
| 188 |
FILE *ast_file_mkftemp(char *template_name, mode_t mode) |
| 189 |
{ |
| 190 |
FILE *p = NULL; |
| 191 |
int pfd = mkstemp(template_name); |
| 192 |
chmod(template_name, mode); |
| 193 |
if (pfd > -1) { |
| 194 |
p = fdopen(pfd, "w+"); |
| 195 |
if (!p) { |
| 196 |
close(pfd); |
| 197 |
pfd = -1; |
| 198 |
} |
| 199 |
} |
| 200 |
return p; |
| 201 |
} |
| 202 |
|
| 203 |
int ast_file_fdtemp(const char *path, char **filename, const char *template_name) |
| 204 |
{ |
| 205 |
int fd; |
| 206 |
|
| 207 |
if (ast_asprintf(filename, "%s/%s", path, template_name) < 0) { |
| 208 |
ast_log(LOG_ERROR, "Failed to set up temporary file path\n"); |
| 209 |
return -1; |
| 210 |
} |
| 211 |
|
| 212 |
ast_mkdir(path, 0644); |
| 213 |
|
| 214 |
if ((fd = mkstemp(*filename)) < 0) { |
| 215 |
ast_log(LOG_NOTICE, "Failed to create temporary file\n"); |
| 216 |
ast_free(*filename); |
| 217 |
return -1; |
| 218 |
} |
| 219 |
|
| 220 |
return fd; |
| 221 |
} |
| 222 |
|
| 223 |
int ast_stopstream(struct ast_channel *tmp) |
| 224 |
{ |
| 225 |
struct ast_json * cel_event = NULL; |
| 226 |
|
| 227 |
ast_channel_lock(tmp); |
| 228 |
|
| 229 |
|
| 230 |
if (ast_channel_stream(tmp)) { |
| 231 |
ast_closestream(ast_channel_stream(tmp)); |
| 232 |
ast_channel_stream_set(tmp, NULL); |
| 233 |
|
| 234 |
cel_event = ast_json_pack("{ s: s }", "event", "FILE_STREAM_END"); |
| 235 |
if (cel_event) { |
| 236 |
ast_cel_publish_event(tmp, AST_CEL_STREAM_END, cel_event); |
| 237 |
} |
| 238 |
|
| 239 |
if (ast_channel_oldwriteformat(tmp) && ast_set_write_format(tmp, ast_channel_oldwriteformat(tmp))) |
| 240 |
ast_log(LOG_WARNING, "Unable to restore format back to %s\n", ast_format_get_name(ast_channel_oldwriteformat(tmp))); |
| 241 |
} |
| 242 |
ast_json_unref(cel_event); |
| 243 |
|
| 244 |
|
| 245 |
if (ast_channel_vstream(tmp) != NULL) { |
| 246 |
ast_closestream(ast_channel_vstream(tmp)); |
| 247 |
ast_channel_vstream_set(tmp, NULL); |
| 248 |
} |
| 249 |
|
| 250 |
ast_channel_unlock(tmp); |
| 251 |
|
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f) |
| 256 |
{ |
| 257 |
int res = -1; |
| 258 |
if (f->frametype == AST_FRAME_VIDEO) { |
| 259 |
if (ast_format_get_type(fs->fmt->format) == AST_MEDIA_TYPE_AUDIO) { |
| 260 |
|
| 261 |
if (!fs->vfs && fs->filename) { |
| 262 |
const char *type = ast_format_get_name(f->subclass.format); |
| 263 |
fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode); |
| 264 |
ast_debug(1, "Opened video output file\n"); |
| 265 |
} |
| 266 |
if (fs->vfs) |
| 267 |
return ast_writestream(fs->vfs, f); |
| 268 |
|
| 269 |
return 0; |
| 270 |
} |
| 271 |
} else if (f->frametype != AST_FRAME_VOICE) { |
| 272 |
ast_log(LOG_WARNING, "Tried to write non-voice frame\n"); |
| 273 |
return -1; |
| 274 |
} |
| 275 |
if (ast_format_cmp(f->subclass.format, fs->fmt->format) != AST_FORMAT_CMP_NOT_EQUAL) { |
| 276 |
res = fs->fmt->write(fs, f); |
| 277 |
if (res < 0) |
| 278 |
ast_log(LOG_WARNING, "Natural write failed\n"); |
| 279 |
else if (res > 0) |
| 280 |
ast_log(LOG_WARNING, "Huh??\n"); |
| 281 |
} else { |
| 282 |
|
| 283 |
|
| 284 |
if (fs->trans && (ast_format_cmp(f->subclass.format, fs->lastwriteformat) != AST_FORMAT_CMP_EQUAL)) { |
| 285 |
ast_translator_free_path(fs->trans); |
| 286 |
fs->trans = NULL; |
| 287 |
} |
| 288 |
if (!fs->trans) { |
| 289 |
fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass.format); |
| 290 |
} |
| 291 |
if (!fs->trans) { |
| 292 |
ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n", |
| 293 |
fs->fmt->name, ast_format_get_name(f->subclass.format)); |
| 294 |
} else { |
| 295 |
struct ast_frame *trf; |
| 296 |
ao2_replace(fs->lastwriteformat, f->subclass.format); |
| 297 |
|
| 298 |
if ((trf = ast_translate(fs->trans, f, 0))) { |
| 299 |
struct ast_frame *cur; |
| 300 |
|
| 301 |
|
| 302 |
for (cur = trf; cur; cur = AST_LIST_NEXT(cur, frame_list)) { |
| 303 |
if ((res = fs->fmt->write(fs, cur))) { |
| 304 |
ast_log(LOG_WARNING, "Translated frame write failed\n"); |
| 305 |
break; |
| 306 |
} |
| 307 |
} |
| 308 |
ast_frfree(trf); |
| 309 |
} else { |
| 310 |
res = 0; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
return res; |
| 315 |
} |
| 316 |
|
| 317 |
static int copy(const char *infile, const char *outfile) |
| 318 |
{ |
| 319 |
int ifd, ofd, len; |
| 320 |
char buf[4096]; |
| 321 |
|
| 322 |
if ((ifd = open(infile, O_RDONLY)) < 0) { |
| 323 |
ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile); |
| 324 |
return -1; |
| 325 |
} |
| 326 |
if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, AST_FILE_MODE)) < 0) { |
| 327 |
ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile); |
| 328 |
close(ifd); |
| 329 |
return -1; |
| 330 |
} |
| 331 |
while ( (len = read(ifd, buf, sizeof(buf)) ) ) { |
| 332 |
int res; |
| 333 |
if (len < 0) { |
| 334 |
ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno)); |
| 335 |
break; |
| 336 |
} |
| 337 |
|
| 338 |
res = write(ofd, buf, len); |
| 339 |
if (res != len) { |
| 340 |
ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno)); |
| 341 |
len = -1; |
| 342 |
break; |
| 343 |
} |
| 344 |
} |
| 345 |
close(ifd); |
| 346 |
close(ofd); |
| 347 |
if (len < 0) { |
| 348 |
unlink(outfile); |
| 349 |
return -1; |
| 350 |
} |
| 351 |
return 0; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
static char *build_filename(const char *filename, const char *ext) |
| 361 |
{ |
| 362 |
char *fn = NULL; |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
if (!strcmp(ext, "wav49")) |
| 367 |
ext = "WAV"; |
| 368 |
|
| 369 |
if (filename[0] == '/') { |
| 370 |
if (ast_asprintf(&fn, "%s.%s", filename, ext) < 0) { |
| 371 |
fn = NULL; |
| 372 |
} |
| 373 |
} else { |
| 374 |
if (ast_asprintf(&fn, "%s/sounds/%s.%s", |
| 375 |
ast_config_AST_DATA_DIR, filename, ext) < 0) { |
| 376 |
fn = NULL; |
| 377 |
} |
| 378 |
} |
| 379 |
return fn; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
static int type_in_list(const char *list, const char *type, int (*cmp)(const char *s1, const char *s2)) |
| 385 |
{ |
| 386 |
char *stringp = ast_strdupa(list), *item; |
| 387 |
|
| 388 |
while ((item = strsep(&stringp, "|"))) { |
| 389 |
if (!cmp(item, type)) { |
| 390 |
return 1; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
return 0; |
| 395 |
} |
| 396 |
|
| 397 |
#define exts_compare(list, type) (type_in_list((list), (type), strcmp)) |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
static void filestream_close(struct ast_filestream *f) |
| 404 |
{ |
| 405 |
enum ast_media_type format_type = ast_format_get_type(f->fmt->format); |
| 406 |
|
| 407 |
if (!f->owner) { |
| 408 |
return; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
switch (format_type) |
| 413 |
{ |
| 414 |
case AST_MEDIA_TYPE_AUDIO: |
| 415 |
ast_channel_stream_set(f->owner, NULL); |
| 416 |
AST_SCHED_DEL_ACCESSOR(ast_channel_sched(f->owner), f->owner, ast_channel_streamid, ast_channel_streamid_set); |
| 417 |
ast_settimeout(f->owner, 0, NULL, NULL); |
| 418 |
break; |
| 419 |
case AST_MEDIA_TYPE_VIDEO: |
| 420 |
ast_channel_vstream_set(f->owner, NULL); |
| 421 |
AST_SCHED_DEL_ACCESSOR(ast_channel_sched(f->owner), f->owner, ast_channel_vstreamid, ast_channel_vstreamid_set); |
| 422 |
break; |
| 423 |
default: |
| 424 |
ast_log(AST_LOG_WARNING, "Unable to schedule deletion of filestream with unsupported type %s\n", f->fmt->name); |
| 425 |
break; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
static void filestream_destructor(void *arg) |
| 430 |
{ |
| 431 |
struct ast_filestream *f = arg; |
| 432 |
int status; |
| 433 |
int pid = -1; |
| 434 |
|
| 435 |
|
| 436 |
filestream_close(f); |
| 437 |
|
| 438 |
|
| 439 |
if (f->trans) |
| 440 |
ast_translator_free_path(f->trans); |
| 441 |
|
| 442 |
if (f->fmt->close) { |
| 443 |
void (*closefn)(struct ast_filestream *) = f->fmt->close; |
| 444 |
closefn(f); |
| 445 |
} |
| 446 |
|
| 447 |
if (f->f) { |
| 448 |
fclose(f->f); |
| 449 |
} |
| 450 |
|
| 451 |
if (f->realfilename && f->filename) { |
| 452 |
pid = ast_safe_fork(0); |
| 453 |
if (!pid) { |
| 454 |
execl("/bin/mv", "mv", "-f", f->filename, f->realfilename, SENTINEL); |
| 455 |
_exit(1); |
| 456 |
} |
| 457 |
else if (pid > 0) { |
| 458 |
|
| 459 |
waitpid(pid, &status, 0); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
ast_free(f->filename); |
| 464 |
ast_free(f->realfilename); |
| 465 |
if (f->vfs) |
| 466 |
ast_closestream(f->vfs); |
| 467 |
ast_free(f->write_buffer); |
| 468 |
ast_free((void *)f->orig_chan_name); |
| 469 |
ao2_cleanup(f->lastwriteformat); |
| 470 |
ao2_cleanup(f->fr.subclass.format); |
| 471 |
ast_module_unref(f->fmt->module); |
| 472 |
} |
| 473 |
|
| 474 |
static struct ast_filestream *get_filestream(struct ast_format_def *fmt, FILE *bfile) |
| 475 |
{ |
| 476 |
struct ast_filestream *s; |
| 477 |
int l = sizeof(*s) + fmt->buf_size + fmt->desc_size; |
| 478 |
|
| 479 |
if (!ast_module_running_ref(fmt->module)) { |
| 480 |
return NULL; |
| 481 |
} |
| 482 |
|
| 483 |
s = ao2_alloc(l, filestream_destructor); |
| 484 |
if (!s) { |
| 485 |
ast_module_unref(fmt->module); |
| 486 |
return NULL; |
| 487 |
} |
| 488 |
s->fmt = fmt; |
| 489 |
s->f = bfile; |
| 490 |
|
| 491 |
if (fmt->desc_size) |
| 492 |
s->_private = ((char *)(s + 1)) + fmt->buf_size; |
| 493 |
if (fmt->buf_size) |
| 494 |
s->buf = (char *)(s + 1); |
| 495 |
s->fr.src = fmt->name; |
| 496 |
|
| 497 |
if (ast_format_get_type(fmt->format) == AST_MEDIA_TYPE_AUDIO) { |
| 498 |
s->fr.frametype = AST_FRAME_VOICE; |
| 499 |
} else if (ast_format_get_type(fmt->format) == AST_MEDIA_TYPE_VIDEO) { |
| 500 |
s->fr.frametype = AST_FRAME_VIDEO; |
| 501 |
} |
| 502 |
s->fr.mallocd = 0; |
| 503 |
s->fr.subclass.format = ao2_bump(fmt->format); |
| 504 |
|
| 505 |
return s; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
enum wrap_fn { WRAP_OPEN, WRAP_REWRITE }; |
| 513 |
|
| 514 |
static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode) |
| 515 |
{ |
| 516 |
struct ast_format_def *f = s->fmt; |
| 517 |
int ret = -1; |
| 518 |
int (*openfn)(struct ast_filestream *s); |
| 519 |
|
| 520 |
if (mode == WRAP_OPEN && (openfn = f->open) && openfn(s)) |
| 521 |
ast_log(LOG_WARNING, "Unable to open format %s\n", f->name); |
| 522 |
else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment)) |
| 523 |
ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name); |
| 524 |
else { |
| 525 |
|
| 526 |
ret = 0; |
| 527 |
} |
| 528 |
return ret; |
| 529 |
} |
| 530 |
|
| 531 |
static int rewrite_wrapper(struct ast_filestream *s, const char *comment) |
| 532 |
{ |
| 533 |
return fn_wrapper(s, comment, WRAP_REWRITE); |
| 534 |
} |
| 535 |
|
| 536 |
static int open_wrapper(struct ast_filestream *s) |
| 537 |
{ |
| 538 |
return fn_wrapper(s, NULL, WRAP_OPEN); |
| 539 |
} |
| 540 |
|
| 541 |
enum file_action { |
| 542 |
ACTION_EXISTS = 1, |
| 543 |
ACTION_DELETE, |
| 544 |
ACTION_RENAME, |
| 545 |
ACTION_OPEN, |
| 546 |
ACTION_COPY |
| 547 |
}; |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
static int filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action) |
| 561 |
{ |
| 562 |
struct ast_format_def *f; |
| 563 |
int res = (action == ACTION_EXISTS) ? 0 : -1; |
| 564 |
|
| 565 |
AST_RWLIST_RDLOCK(&formats); |
| 566 |
|
| 567 |
AST_RWLIST_TRAVERSE(&formats, f, list) { |
| 568 |
char *ext = NULL; |
| 569 |
char storage[strlen(f->exts) + 1]; |
| 570 |
char *stringp; |
| 571 |
|
| 572 |
if (fmt && !exts_compare(f->exts, fmt)) |
| 573 |
continue; |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
strcpy(storage, f->exts); |
| 580 |
stringp = storage; |
| 581 |
while ( (ext = strsep(&stringp, "|")) ) { |
| 582 |
struct stat st; |
| 583 |
char *fn = build_filename(filename, ext); |
| 584 |
|
| 585 |
if (fn == NULL) |
| 586 |
continue; |
| 587 |
|
| 588 |
if ( stat(fn, &st) ) { |
| 589 |
ast_free(fn); |
| 590 |
continue; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
if (action == ACTION_OPEN) { |
| 596 |
struct ast_channel *chan = (struct ast_channel *)arg2; |
| 597 |
FILE *bfile; |
| 598 |
struct ast_filestream *s; |
| 599 |
|
| 600 |
if ((ast_format_cmp(ast_channel_writeformat(chan), f->format) == AST_FORMAT_CMP_NOT_EQUAL) && |
| 601 |
!(((ast_format_get_type(f->format) == AST_MEDIA_TYPE_AUDIO) && fmt) || |
| 602 |
((ast_format_get_type(f->format) == AST_MEDIA_TYPE_VIDEO) && fmt))) { |
| 603 |
ast_free(fn); |
| 604 |
continue; |
| 605 |
} |
| 606 |
if ( (bfile = fopen(fn, "r")) == NULL) { |
| 607 |
ast_free(fn); |
| 608 |
continue; |
| 609 |
} |
| 610 |
s = get_filestream(f, bfile); |
| 611 |
if (!s) { |
| 612 |
fclose(bfile); |
| 613 |
ast_free(fn); |
| 614 |
continue; |
| 615 |
} |
| 616 |
if (open_wrapper(s)) { |
| 617 |
ast_free(fn); |
| 618 |
ast_closestream(s); |
| 619 |
continue; |
| 620 |
} |
| 621 |
if (st.st_size == 0) { |
| 622 |
ast_log(LOG_WARNING, "File %s detected to have zero size.\n", fn); |
| 623 |
} |
| 624 |
|
| 625 |
res = 1; |
| 626 |
s->lasttimeout = -1; |
| 627 |
s->fmt = f; |
| 628 |
s->trans = NULL; |
| 629 |
s->filename = NULL; |
| 630 |
if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO) { |
| 631 |
if (ast_channel_stream(chan)) |
| 632 |
ast_closestream(ast_channel_stream(chan)); |
| 633 |
ast_channel_stream_set(chan, s); |
| 634 |
} else { |
| 635 |
if (ast_channel_vstream(chan)) |
| 636 |
ast_closestream(ast_channel_vstream(chan)); |
| 637 |
ast_channel_vstream_set(chan, s); |
| 638 |
} |
| 639 |
ast_free(fn); |
| 640 |
break; |
| 641 |
} |
| 642 |
switch (action) { |
| 643 |
case ACTION_OPEN: |
| 644 |
break; |
| 645 |
|
| 646 |
case ACTION_EXISTS: |
| 647 |
|
| 648 |
|
| 649 |
if (arg2) { |
| 650 |
ast_format_cap_append((struct ast_format_cap *) arg2, f->format, 0); |
| 651 |
} |
| 652 |
res = 1; |
| 653 |
break; |
| 654 |
|
| 655 |
case ACTION_DELETE: |
| 656 |
if ( (res = unlink(fn)) ) |
| 657 |
ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno)); |
| 658 |
break; |
| 659 |
|
| 660 |
case ACTION_RENAME: |
| 661 |
case ACTION_COPY: { |
| 662 |
char *nfn = build_filename((const char *)arg2, ext); |
| 663 |
if (!nfn) |
| 664 |
ast_log(LOG_WARNING, "Out of memory\n"); |
| 665 |
else { |
| 666 |
res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn); |
| 667 |
if (res) |
| 668 |
ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n", |
| 669 |
action == ACTION_COPY ? "copy" : "rename", |
| 670 |
fn, nfn, strerror(errno)); |
| 671 |
ast_free(nfn); |
| 672 |
} |
| 673 |
} |
| 674 |
break; |
| 675 |
|
| 676 |
default: |
| 677 |
ast_log(LOG_WARNING, "Unknown helper %u\n", action); |
| 678 |
} |
| 679 |
ast_free(fn); |
| 680 |
} |
| 681 |
} |
| 682 |
AST_RWLIST_UNLOCK(&formats); |
| 683 |
return res; |
| 684 |
} |
| 685 |
|
| 686 |
static int is_absolute_path(const char *filename) |
| 687 |
{ |
| 688 |
return filename[0] == '/'; |
| 689 |
} |
| 690 |
|
| 691 |
static int is_remote_path(const char *filename) |
| 692 |
{ |
| 693 |
return strstr(filename, "://") ? 1 : 0; |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
static int fileexists_test(const char *filename, const char *fmt, const char *lang, |
| 703 |
char *buf, int buflen, struct ast_format_cap *result_cap) |
| 704 |
{ |
| 705 |
if (buf == NULL) { |
| 706 |
return 0; |
| 707 |
} |
| 708 |
|
| 709 |
if (is_remote_path(filename) && !ast_media_cache_retrieve(filename, NULL, buf, buflen)) { |
| 710 |
return filehelper(buf, result_cap, NULL, ACTION_EXISTS); |
| 711 |
} |
| 712 |
|
| 713 |
if (ast_language_is_prefix && !is_absolute_path(filename)) { |
| 714 |
if (lang) { |
| 715 |
snprintf(buf, buflen, "%s/%s", lang, filename); |
| 716 |
} else { |
| 717 |
snprintf(buf, buflen, "%s", filename); |
| 718 |
} |
| 719 |
} else { |
| 720 |
strcpy(buf, filename); |
| 721 |
if (lang) { |
| 722 |
|
| 723 |
const char *c = strrchr(filename, '/'); |
| 724 |
int offset = c ? c - filename + 1 : 0; |
| 725 |
snprintf(buf + offset, buflen - offset, "%s/%s", lang, filename + offset); |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
return filehelper(buf, result_cap, fmt, ACTION_EXISTS); |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
static int fileexists_core(const char *filename, const char *fmt, const char *preflang, |
| 753 |
char *buf, int buflen, struct ast_format_cap *result_cap) |
| 754 |
{ |
| 755 |
char *lang; |
| 756 |
|
| 757 |
if (buf == NULL) { |
| 758 |
return 0; |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
lang = ast_strdupa(preflang); |
| 769 |
|
| 770 |
|
| 771 |
while (!ast_strlen_zero(lang)) { |
| 772 |
char *end; |
| 773 |
|
| 774 |
if (fileexists_test(filename, fmt, lang, buf, buflen, result_cap)) { |
| 775 |
return 1; |
| 776 |
} |
| 777 |
|
| 778 |
if ((end = strrchr(lang, '_')) != NULL) { |
| 779 |
*end = '\0'; |
| 780 |
continue; |
| 781 |
} |
| 782 |
|
| 783 |
break; |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
if (fileexists_test(filename, fmt, NULL, buf, buflen, result_cap)) { |
| 788 |
return 1; |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
if ((ast_strlen_zero(preflang) || strcmp(preflang, DEFAULT_LANGUAGE)) && (ast_strlen_zero(lang) || strcmp(lang, DEFAULT_LANGUAGE))) { |
| 793 |
if ((fileexists_test(filename, fmt, DEFAULT_LANGUAGE, buf, buflen, result_cap)) > 0) { |
| 794 |
return 1; |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
return 0; |
| 799 |
} |
| 800 |
|
| 801 |
static struct ast_filestream *openstream_internal(struct ast_channel *chan, |
| 802 |
const char *filename, const char *preflang, int asis, int quiet) |
| 803 |
{ |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
struct ast_format_cap *file_fmt_cap; |
| 810 |
int res; |
| 811 |
int buflen; |
| 812 |
char *buf; |
| 813 |
|
| 814 |
if (!asis) { |
| 815 |
|
| 816 |
ast_stopstream(chan); |
| 817 |
if (ast_channel_generator(chan)) |
| 818 |
ast_deactivate_generator(chan); |
| 819 |
} |
| 820 |
if (preflang == NULL) |
| 821 |
preflang = ""; |
| 822 |
buflen = strlen(preflang) + strlen(filename) + 4; |
| 823 |
buf = ast_alloca(buflen); |
| 824 |
|
| 825 |
if (!(file_fmt_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) { |
| 826 |
return NULL; |
| 827 |
} |
| 828 |
if (!fileexists_core(filename, NULL, preflang, buf, buflen, file_fmt_cap) || |
| 829 |
!ast_format_cap_has_type(file_fmt_cap, AST_MEDIA_TYPE_AUDIO)) { |
| 830 |
|
| 831 |
if (!quiet) { |
| 832 |
ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename); |
| 833 |
} |
| 834 |
ao2_ref(file_fmt_cap, -1); |
| 835 |
return NULL; |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
ast_channel_lock(chan); |
| 840 |
ast_channel_set_oldwriteformat(chan, ast_channel_writeformat(chan)); |
| 841 |
|
| 842 |
res = ast_set_write_format_from_cap(chan, file_fmt_cap); |
| 843 |
ast_channel_unlock(chan); |
| 844 |
|
| 845 |
ao2_ref(file_fmt_cap, -1); |
| 846 |
|
| 847 |
if (res == -1) { |
| 848 |
return NULL; |
| 849 |
} |
| 850 |
res = filehelper(buf, chan, NULL, ACTION_OPEN); |
| 851 |
if (res >= 0) |
| 852 |
return ast_channel_stream(chan); |
| 853 |
return NULL; |
| 854 |
} |
| 855 |
|
| 856 |
struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang) |
| 857 |
{ |
| 858 |
return openstream_internal(chan, filename, preflang, 0, 0); |
| 859 |
} |
| 860 |
|
| 861 |
struct ast_filestream *ast_openstream_full(struct ast_channel *chan, |
| 862 |
const char *filename, const char *preflang, int asis) |
| 863 |
{ |
| 864 |
return openstream_internal(chan, filename, preflang, asis, 0); |
| 865 |
} |
| 866 |
|
| 867 |
struct ast_filestream *ast_openvstream(struct ast_channel *chan, |
| 868 |
const char *filename, const char *preflang) |
| 869 |
{ |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
struct ast_format_cap *nativeformats, *tmp_cap; |
| 874 |
char *buf; |
| 875 |
int buflen; |
| 876 |
int i, fd; |
| 877 |
|
| 878 |
if (preflang == NULL) { |
| 879 |
preflang = ""; |
| 880 |
} |
| 881 |
buflen = strlen(preflang) + strlen(filename) + 4; |
| 882 |
buf = ast_alloca(buflen); |
| 883 |
|
| 884 |
ast_channel_lock(chan); |
| 885 |
nativeformats = ao2_bump(ast_channel_nativeformats(chan)); |
| 886 |
ast_channel_unlock(chan); |
| 887 |
|
| 888 |
|
| 889 |
if (!ast_format_cap_has_type(nativeformats, AST_MEDIA_TYPE_VIDEO)) { |
| 890 |
ao2_cleanup(nativeformats); |
| 891 |
return NULL; |
| 892 |
} |
| 893 |
if (!(tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) { |
| 894 |
ao2_cleanup(nativeformats); |
| 895 |
return NULL; |
| 896 |
} |
| 897 |
|
| 898 |
if (!fileexists_core(filename, NULL, preflang, buf, buflen, tmp_cap)) { |
| 899 |
ao2_ref(tmp_cap, -1); |
| 900 |
ao2_cleanup(nativeformats); |
| 901 |
return NULL; |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
for (i = 0; i < ast_format_cap_count(tmp_cap); ++i) { |
| 906 |
struct ast_format *format = ast_format_cap_get_format(tmp_cap, i); |
| 907 |
|
| 908 |
if ((ast_format_get_type(format) != AST_MEDIA_TYPE_VIDEO) || |
| 909 |
!ast_format_cap_iscompatible(nativeformats, tmp_cap)) { |
| 910 |
ao2_ref(format, -1); |
| 911 |
continue; |
| 912 |
} |
| 913 |
|
| 914 |
fd = filehelper(buf, chan, ast_format_get_name(format), ACTION_OPEN); |
| 915 |
if (fd >= 0) { |
| 916 |
ao2_ref(format, -1); |
| 917 |
ao2_ref(tmp_cap, -1); |
| 918 |
ao2_cleanup(nativeformats); |
| 919 |
return ast_channel_vstream(chan); |
| 920 |
} |
| 921 |
ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename); |
| 922 |
ao2_ref(format, -1); |
| 923 |
} |
| 924 |
ao2_ref(tmp_cap, -1); |
| 925 |
ao2_cleanup(nativeformats); |
| 926 |
|
| 927 |
return NULL; |
| 928 |
} |
| 929 |
|
| 930 |
static struct ast_frame *read_frame(struct ast_filestream *s, int *whennext) |
| 931 |
{ |
| 932 |
struct ast_frame *fr, *new_fr; |
| 933 |
|
| 934 |
if (!s || !s->fmt) { |
| 935 |
return NULL; |
| 936 |
} |
| 937 |
|
| 938 |
if (!(fr = s->fmt->read(s, whennext))) { |
| 939 |
return NULL; |
| 940 |
} |
| 941 |
|
| 942 |
if (!(new_fr = ast_frisolate(fr))) { |
| 943 |
ast_frfree(fr); |
| 944 |
return NULL; |
| 945 |
} |
| 946 |
|
| 947 |
if (new_fr != fr) { |
| 948 |
ast_frfree(fr); |
| 949 |
fr = new_fr; |
| 950 |
} |
| 951 |
|
| 952 |
return fr; |
| 953 |
} |
| 954 |
|
| 955 |
struct ast_frame *ast_readframe(struct ast_filestream *s) |
| 956 |
{ |
| 957 |
int whennext = 0; |
| 958 |
|
| 959 |
return read_frame(s, &whennext); |
| 960 |
} |
| 961 |
|
| 962 |
enum fsread_res { |
| 963 |
FSREAD_FAILURE, |
| 964 |
FSREAD_SUCCESS_SCHED, |
| 965 |
FSREAD_SUCCESS_NOSCHED, |
| 966 |
}; |
| 967 |
|
| 968 |
static int ast_fsread_audio(const void *data); |
| 969 |
|
| 970 |
static enum fsread_res ast_readaudio_callback(struct ast_filestream *s) |
| 971 |
{ |
| 972 |
int whennext = 0; |
| 973 |
|
| 974 |
while (!whennext) { |
| 975 |
struct ast_frame *fr; |
| 976 |
|
| 977 |
if (s->orig_chan_name && strcasecmp(ast_channel_name(s->owner), s->orig_chan_name)) { |
| 978 |
goto return_failure; |
| 979 |
} |
| 980 |
|
| 981 |
fr = read_frame(s, &whennext); |
| 982 |
|
| 983 |
if (!fr || ast_write(s->owner, fr) ) { |
| 984 |
if (fr) { |
| 985 |
ast_debug(2, "Failed to write frame\n"); |
| 986 |
ast_frfree(fr); |
| 987 |
} |
| 988 |
goto return_failure; |
| 989 |
} |
| 990 |
|
| 991 |
if (fr) { |
| 992 |
ast_frfree(fr); |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
if (whennext != s->lasttimeout) { |
| 997 |
if (ast_channel_timingfd(s->owner) > -1) { |
| 998 |
float samp_rate = (float) ast_format_get_sample_rate(s->fmt->format); |
| 999 |
unsigned int rate; |
| 1000 |
|
| 1001 |
rate = (unsigned int) roundf(samp_rate / ((float) whennext)); |
| 1002 |
|
| 1003 |
ast_settimeout_full(s->owner, rate, ast_fsread_audio, s, 1); |
| 1004 |
} else { |
| 1005 |
ast_channel_streamid_set(s->owner, ast_sched_add(ast_channel_sched(s->owner), whennext / (ast_format_get_sample_rate(s->fmt->format) / 1000), ast_fsread_audio, s)); |
| 1006 |
} |
| 1007 |
s->lasttimeout = whennext; |
| 1008 |
return FSREAD_SUCCESS_NOSCHED; |
| 1009 |
} |
| 1010 |
return FSREAD_SUCCESS_SCHED; |
| 1011 |
|
| 1012 |
return_failure: |
| 1013 |
ast_channel_streamid_set(s->owner, -1); |
| 1014 |
ast_settimeout(s->owner, 0, NULL, NULL); |
| 1015 |
return FSREAD_FAILURE; |
| 1016 |
} |
| 1017 |
|
| 1018 |
static int ast_fsread_audio(const void *data) |
| 1019 |
{ |
| 1020 |
struct ast_filestream *fs = (struct ast_filestream *)data; |
| 1021 |
enum fsread_res res; |
| 1022 |
|
| 1023 |
res = ast_readaudio_callback(fs); |
| 1024 |
|
| 1025 |
if (res == FSREAD_SUCCESS_SCHED) |
| 1026 |
return 1; |
| 1027 |
|
| 1028 |
return 0; |
| 1029 |
} |
| 1030 |
|
| 1031 |
static int ast_fsread_video(const void *data); |
| 1032 |
|
| 1033 |
static enum fsread_res ast_readvideo_callback(struct ast_filestream *s) |
| 1034 |
{ |
| 1035 |
int whennext = 0; |
| 1036 |
|
| 1037 |
while (!whennext) { |
| 1038 |
struct ast_frame *fr = read_frame(s, &whennext); |
| 1039 |
|
| 1040 |
if (!fr || ast_write(s->owner, fr) ) { |
| 1041 |
if (fr) { |
| 1042 |
ast_debug(2, "Failed to write frame\n"); |
| 1043 |
ast_frfree(fr); |
| 1044 |
} |
| 1045 |
ast_channel_vstreamid_set(s->owner, -1); |
| 1046 |
return FSREAD_FAILURE; |
| 1047 |
} |
| 1048 |
|
| 1049 |
if (fr) { |
| 1050 |
ast_frfree(fr); |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
if (whennext != s->lasttimeout) { |
| 1055 |
ast_channel_vstreamid_set(s->owner, ast_sched_add(ast_channel_sched(s->owner), whennext / (ast_format_get_sample_rate(s->fmt->format) / 1000), ast_fsread_video, s)); |
| 1056 |
s->lasttimeout = whennext; |
| 1057 |
return FSREAD_SUCCESS_NOSCHED; |
| 1058 |
} |
| 1059 |
|
| 1060 |
return FSREAD_SUCCESS_SCHED; |
| 1061 |
} |
| 1062 |
|
| 1063 |
static int ast_fsread_video(const void *data) |
| 1064 |
{ |
| 1065 |
struct ast_filestream *fs = (struct ast_filestream *)data; |
| 1066 |
enum fsread_res res; |
| 1067 |
|
| 1068 |
res = ast_readvideo_callback(fs); |
| 1069 |
|
| 1070 |
if (res == FSREAD_SUCCESS_SCHED) |
| 1071 |
return 1; |
| 1072 |
|
| 1073 |
return 0; |
| 1074 |
} |
| 1075 |
|
| 1076 |
int ast_applystream(struct ast_channel *chan, struct ast_filestream *s) |
| 1077 |
{ |
| 1078 |
s->owner = chan; |
| 1079 |
return 0; |
| 1080 |
} |
| 1081 |
|
| 1082 |
int ast_playstream(struct ast_filestream *s) |
| 1083 |
{ |
| 1084 |
enum fsread_res res; |
| 1085 |
|
| 1086 |
if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO) |
| 1087 |
res = ast_readaudio_callback(s); |
| 1088 |
else |
| 1089 |
res = ast_readvideo_callback(s); |
| 1090 |
|
| 1091 |
return (res == FSREAD_FAILURE) ? -1 : 0; |
| 1092 |
} |
| 1093 |
|
| 1094 |
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence) |
| 1095 |
{ |
| 1096 |
return fs->fmt->seek(fs, sample_offset, whence); |
| 1097 |
} |
| 1098 |
|
| 1099 |
int ast_truncstream(struct ast_filestream *fs) |
| 1100 |
{ |
| 1101 |
return fs->fmt->trunc(fs); |
| 1102 |
} |
| 1103 |
|
| 1104 |
off_t ast_tellstream(struct ast_filestream *fs) |
| 1105 |
{ |
| 1106 |
return fs->fmt->tell(fs); |
| 1107 |
} |
| 1108 |
|
| 1109 |
int ast_ratestream(struct ast_filestream *fs) |
| 1110 |
{ |
| 1111 |
return ast_format_get_sample_rate(fs->fmt->format); |
| 1112 |
} |
| 1113 |
|
| 1114 |
int ast_stream_fastforward(struct ast_filestream *fs, off_t ms) |
| 1115 |
{ |
| 1116 |
return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR); |
| 1117 |
} |
| 1118 |
|
| 1119 |
int ast_stream_rewind(struct ast_filestream *fs, off_t ms) |
| 1120 |
{ |
| 1121 |
off_t offset = ast_tellstream(fs); |
| 1122 |
if (ms * DEFAULT_SAMPLES_PER_MS > offset) { |
| 1123 |
|
| 1124 |
ast_debug(1, "Restarting, rather than seeking to negative offset %ld\n", (long) (offset - (ms * DEFAULT_SAMPLES_PER_MS))); |
| 1125 |
return ast_seekstream(fs, 0, SEEK_SET); |
| 1126 |
} |
| 1127 |
return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR); |
| 1128 |
} |
| 1129 |
|
| 1130 |
int ast_closestream(struct ast_filestream *f) |
| 1131 |
{ |
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
if (f == NULL) { |
| 1137 |
return 0; |
| 1138 |
} |
| 1139 |
filestream_close(f); |
| 1140 |
ao2_ref(f, -1); |
| 1141 |
return 0; |
| 1142 |
} |
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
int ast_fileexists(const char *filename, const char *fmt, const char *preflang) |
| 1149 |
{ |
| 1150 |
char *buf; |
| 1151 |
int buflen; |
| 1152 |
|
| 1153 |
if (preflang == NULL) |
| 1154 |
preflang = ""; |
| 1155 |
buflen = strlen(preflang) + strlen(filename) + 4; |
| 1156 |
buf = ast_alloca(buflen); |
| 1157 |
return fileexists_core(filename, fmt, preflang, buf, buflen, NULL) ? 1 : 0; |
| 1158 |
} |
| 1159 |
|
| 1160 |
int ast_filedelete(const char *filename, const char *fmt) |
| 1161 |
{ |
| 1162 |
return filehelper(filename, NULL, fmt, ACTION_DELETE); |
| 1163 |
} |
| 1164 |
|
| 1165 |
int ast_filerename(const char *filename, const char *filename2, const char *fmt) |
| 1166 |
{ |
| 1167 |
return filehelper(filename, filename2, fmt, ACTION_RENAME); |
| 1168 |
} |
| 1169 |
|
| 1170 |
int ast_filecopy(const char *filename, const char *filename2, const char *fmt) |
| 1171 |
{ |
| 1172 |
return filehelper(filename, filename2, fmt, ACTION_COPY); |
| 1173 |
} |
| 1174 |
|
| 1175 |
static int __ast_file_read_dirs(const char *path, ast_file_on_file on_file, |
| 1176 |
void *obj, int max_depth) |
| 1177 |
{ |
| 1178 |
DIR *dir; |
| 1179 |
struct dirent *entry; |
| 1180 |
int res; |
| 1181 |
|
| 1182 |
if (!(dir = opendir(path))) { |
| 1183 |
ast_log(LOG_ERROR, "Error opening directory - %s: %s\n", |
| 1184 |
path, strerror(errno)); |
| 1185 |
return -1; |
| 1186 |
} |
| 1187 |
|
| 1188 |
--max_depth; |
| 1189 |
|
| 1190 |
res = 0; |
| 1191 |
|
| 1192 |
while ((entry = readdir(dir)) != NULL && !errno) { |
| 1193 |
int is_file = 0; |
| 1194 |
int is_dir = 0; |
| 1195 |
RAII_VAR(char *, full_path, NULL, ast_free); |
| 1196 |
|
| 1197 |
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) { |
| 1198 |
continue; |
| 1199 |
} |
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
#ifdef _DIRENT_HAVE_D_TYPE |
| 1207 |
if (entry->d_type != DT_UNKNOWN && entry->d_type != DT_LNK) { |
| 1208 |
is_file = entry->d_type == DT_REG; |
| 1209 |
is_dir = entry->d_type == DT_DIR; |
| 1210 |
} else |
| 1211 |
#endif |
| 1212 |
{ |
| 1213 |
struct stat statbuf; |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
full_path = ast_malloc(strlen(path) + strlen(entry->d_name) + 2); |
| 1220 |
if (!full_path) { |
| 1221 |
return -1; |
| 1222 |
} |
| 1223 |
sprintf(full_path, "%s/%s", path, entry->d_name); |
| 1224 |
|
| 1225 |
if (stat(full_path, &statbuf)) { |
| 1226 |
ast_log(LOG_ERROR, "Error reading path stats - %s: %s\n", |
| 1227 |
full_path, strerror(errno)); |
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
continue; |
| 1233 |
} |
| 1234 |
|
| 1235 |
is_file = S_ISREG(statbuf.st_mode); |
| 1236 |
is_dir = S_ISDIR(statbuf.st_mode); |
| 1237 |
} |
| 1238 |
|
| 1239 |
if (is_file) { |
| 1240 |
|
| 1241 |
if ((res = on_file(path, entry->d_name, obj))) { |
| 1242 |
break; |
| 1243 |
} |
| 1244 |
|
| 1245 |
continue; |
| 1246 |
} |
| 1247 |
|
| 1248 |
if (!is_dir) { |
| 1249 |
ast_debug(5, "Skipping %s: not a regular file or directory\n", full_path); |
| 1250 |
continue; |
| 1251 |
} |
| 1252 |
|
| 1253 |
|
| 1254 |
if (max_depth != 0) { |
| 1255 |
if (!full_path) { |
| 1256 |
|
| 1257 |
full_path = ast_malloc(strlen(path) + strlen(entry->d_name) + 2); |
| 1258 |
if (!full_path) { |
| 1259 |
return -1; |
| 1260 |
} |
| 1261 |
sprintf(full_path, "%s/%s", path, entry->d_name); |
| 1262 |
} |
| 1263 |
|
| 1264 |
if ((res = __ast_file_read_dirs(full_path, on_file, obj, max_depth))) { |
| 1265 |
break; |
| 1266 |
} |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
closedir(dir); |
| 1271 |
|
| 1272 |
if (!res && errno) { |
| 1273 |
ast_log(LOG_ERROR, "Error while reading directories - %s: %s\n", |
| 1274 |
path, strerror(errno)); |
| 1275 |
res = -1; |
| 1276 |
} |
| 1277 |
|
| 1278 |
return res; |
| 1279 |
} |
| 1280 |
|
| 1281 |
#if !defined(__GLIBC__) |
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
AST_MUTEX_DEFINE_STATIC(read_dirs_lock); |
| 1291 |
#endif |
| 1292 |
|
| 1293 |
int ast_file_read_dirs(const char *dir_name, ast_file_on_file on_file, void *obj, int max_depth) |
| 1294 |
{ |
| 1295 |
int res; |
| 1296 |
|
| 1297 |
errno = 0; |
| 1298 |
|
| 1299 |
#if !defined(__GLIBC__) |
| 1300 |
ast_mutex_lock(&read_dirs_lock); |
| 1301 |
#endif |
| 1302 |
|
| 1303 |
res = __ast_file_read_dirs(dir_name, on_file, obj, max_depth); |
| 1304 |
|
| 1305 |
#if !defined(__GLIBC__) |
| 1306 |
ast_mutex_unlock(&read_dirs_lock); |
| 1307 |
#endif |
| 1308 |
|
| 1309 |
return res; |
| 1310 |
} |
| 1311 |
|
| 1312 |
int ast_streamfile(struct ast_channel *chan, const char *filename, |
| 1313 |
const char *preflang) |
| 1314 |
{ |
| 1315 |
struct ast_json * cel_event = NULL; |
| 1316 |
struct ast_filestream *fs = NULL; |
| 1317 |
struct ast_filestream *vfs = NULL; |
| 1318 |
off_t pos; |
| 1319 |
int seekattempt; |
| 1320 |
int res; |
| 1321 |
char custom_filename[256]; |
| 1322 |
char *tmp_filename; |
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
if (ast_opt_sounds_search_custom && !is_absolute_path(filename)) { |
| 1328 |
memset(custom_filename, 0, sizeof(custom_filename)); |
| 1329 |
snprintf(custom_filename, sizeof(custom_filename), "custom/%s", filename); |
| 1330 |
fs = openstream_internal(chan, custom_filename, preflang, 0, 1); |
| 1331 |
if (fs) { |
| 1332 |
tmp_filename = custom_filename; |
| 1333 |
ast_debug(3, "Found file %s in custom directory\n", filename); |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
if (!fs) { |
| 1338 |
fs = ast_openstream(chan, filename, preflang); |
| 1339 |
if (!fs) { |
| 1340 |
struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN); |
| 1341 |
ast_channel_lock(chan); |
| 1342 |
ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", |
| 1343 |
filename, ast_format_cap_get_names(ast_channel_nativeformats(chan), &codec_buf), strerror(errno)); |
| 1344 |
ast_channel_unlock(chan); |
| 1345 |
return -1; |
| 1346 |
} |
| 1347 |
tmp_filename = (char *)filename; |
| 1348 |
} |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
pos = ftello(fs->f); |
| 1354 |
seekattempt = fseeko(fs->f, -1, SEEK_END); |
| 1355 |
if (seekattempt) { |
| 1356 |
if (errno == EINVAL) { |
| 1357 |
|
| 1358 |
return 0; |
| 1359 |
} else { |
| 1360 |
ast_seekstream(fs, 0, SEEK_SET); |
| 1361 |
} |
| 1362 |
} else { |
| 1363 |
fseeko(fs->f, pos, SEEK_SET); |
| 1364 |
} |
| 1365 |
|
| 1366 |
vfs = ast_openvstream(chan, tmp_filename, preflang); |
| 1367 |
if (vfs) { |
| 1368 |
ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_format_get_name(vfs->fmt->format)); |
| 1369 |
} |
| 1370 |
|
| 1371 |
if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM)) |
| 1372 |
fs->orig_chan_name = ast_strdup(ast_channel_name(chan)); |
| 1373 |
if (ast_applystream(chan, fs)) |
| 1374 |
return -1; |
| 1375 |
if (vfs && ast_applystream(chan, vfs)) |
| 1376 |
return -1; |
| 1377 |
ast_test_suite_event_notify("PLAYBACK", "Message: %s\r\nChannel: %s", tmp_filename, ast_channel_name(chan)); |
| 1378 |
res = ast_playstream(fs); |
| 1379 |
if (!res && vfs) |
| 1380 |
res = ast_playstream(vfs); |
| 1381 |
|
| 1382 |
cel_event = ast_json_pack("{ s: s, s: {s: s, s: s, s: s}}", |
| 1383 |
"event", "FILE_STREAM_BEGIN", |
| 1384 |
"extra", |
| 1385 |
"sound", tmp_filename, |
| 1386 |
"format", ast_format_get_name(ast_channel_writeformat(chan)), |
| 1387 |
"language", preflang ? preflang : "default" |
| 1388 |
); |
| 1389 |
if (cel_event) { |
| 1390 |
ast_cel_publish_event(chan, AST_CEL_STREAM_BEGIN, cel_event); |
| 1391 |
} else { |
| 1392 |
ast_log(LOG_WARNING, "Unable to build extradata for sound file STREAM_BEGIN event on channel %s", ast_channel_name(chan)); |
| 1393 |
} |
| 1394 |
ast_json_unref(cel_event); |
| 1395 |
|
| 1396 |
if (VERBOSITY_ATLEAST(3)) { |
| 1397 |
ast_channel_lock(chan); |
| 1398 |
ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", ast_channel_name(chan), tmp_filename, ast_format_get_name(ast_channel_writeformat(chan)), preflang ? preflang : "default"); |
| 1399 |
ast_channel_unlock(chan); |
| 1400 |
} |
| 1401 |
|
| 1402 |
return res; |
| 1403 |
} |
| 1404 |
|
| 1405 |
struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode) |
| 1406 |
{ |
| 1407 |
FILE *bfile; |
| 1408 |
struct ast_format_def *f; |
| 1409 |
struct ast_filestream *fs = NULL; |
| 1410 |
char *fn; |
| 1411 |
int format_found = 0; |
| 1412 |
|
| 1413 |
AST_RWLIST_RDLOCK(&formats); |
| 1414 |
|
| 1415 |
AST_RWLIST_TRAVERSE(&formats, f, list) { |
| 1416 |
fs = NULL; |
| 1417 |
if (!exts_compare(f->exts, type)) |
| 1418 |
continue; |
| 1419 |
else |
| 1420 |
format_found = 1; |
| 1421 |
|
| 1422 |
fn = build_filename(filename, type); |
| 1423 |
if (!fn) { |
| 1424 |
continue; |
| 1425 |
} |
| 1426 |
errno = 0; |
| 1427 |
bfile = fopen(fn, "r"); |
| 1428 |
|
| 1429 |
if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) { |
| 1430 |
ast_log(LOG_WARNING, "Unable to open %s\n", fn); |
| 1431 |
if (fs) { |
| 1432 |
ast_closestream(fs); |
| 1433 |
} |
| 1434 |
fs = NULL; |
| 1435 |
bfile = NULL; |
| 1436 |
ast_free(fn); |
| 1437 |
break; |
| 1438 |
} |
| 1439 |
|
| 1440 |
fs->trans = NULL; |
| 1441 |
fs->fmt = f; |
| 1442 |
fs->flags = flags; |
| 1443 |
fs->mode = mode; |
| 1444 |
fs->filename = ast_strdup(filename); |
| 1445 |
fs->vfs = NULL; |
| 1446 |
ast_free(fn); |
| 1447 |
break; |
| 1448 |
} |
| 1449 |
|
| 1450 |
AST_RWLIST_UNLOCK(&formats); |
| 1451 |
if (!format_found) |
| 1452 |
ast_log(LOG_WARNING, "No such format '%s'\n", type); |
| 1453 |
|
| 1454 |
return fs; |
| 1455 |
} |
| 1456 |
|
| 1457 |
struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode) |
| 1458 |
{ |
| 1459 |
int fd, myflags = 0; |
| 1460 |
|
| 1461 |
FILE *bfile = NULL; |
| 1462 |
struct ast_format_def *f; |
| 1463 |
struct ast_filestream *fs = NULL; |
| 1464 |
char *buf = NULL; |
| 1465 |
size_t size = 0; |
| 1466 |
int format_found = 0; |
| 1467 |
|
| 1468 |
AST_RWLIST_RDLOCK(&formats); |
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
if (flags & O_APPEND) { |
| 1473 |
flags &= ~O_APPEND; |
| 1474 |
} else { |
| 1475 |
myflags = O_TRUNC; |
| 1476 |
} |
| 1477 |
|
| 1478 |
myflags |= O_WRONLY | O_CREAT; |
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
AST_RWLIST_TRAVERSE(&formats, f, list) { |
| 1484 |
char *fn, *orig_fn = NULL; |
| 1485 |
if (fs) |
| 1486 |
break; |
| 1487 |
|
| 1488 |
if (!exts_compare(f->exts, type)) |
| 1489 |
continue; |
| 1490 |
else |
| 1491 |
format_found = 1; |
| 1492 |
|
| 1493 |
fn = build_filename(filename, type); |
| 1494 |
if (!fn) { |
| 1495 |
continue; |
| 1496 |
} |
| 1497 |
fd = open(fn, flags | myflags, mode); |
| 1498 |
if (fd > -1) { |
| 1499 |
|
| 1500 |
bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w"); |
| 1501 |
if (!bfile) { |
| 1502 |
ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno)); |
| 1503 |
close(fd); |
| 1504 |
fd = -1; |
| 1505 |
} |
| 1506 |
} |
| 1507 |
|
| 1508 |
if (ast_opt_cache_record_files && (fd > -1)) { |
| 1509 |
char *c; |
| 1510 |
|
| 1511 |
fclose(bfile); |
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
orig_fn = ast_strdup(fn); |
| 1517 |
for (c = fn; *c; c++) |
| 1518 |
if (*c == '/') |
| 1519 |
*c = '_'; |
| 1520 |
|
| 1521 |
size = strlen(fn) + strlen(record_cache_dir) + 2; |
| 1522 |
buf = ast_malloc(size); |
| 1523 |
strcpy(buf, record_cache_dir); |
| 1524 |
strcat(buf, "/"); |
| 1525 |
strcat(buf, fn); |
| 1526 |
ast_free(fn); |
| 1527 |
fn = buf; |
| 1528 |
fd = open(fn, flags | myflags, mode); |
| 1529 |
if (fd > -1) { |
| 1530 |
|
| 1531 |
bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w"); |
| 1532 |
if (!bfile) { |
| 1533 |
ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno)); |
| 1534 |
close(fd); |
| 1535 |
fd = -1; |
| 1536 |
} |
| 1537 |
} |
| 1538 |
} |
| 1539 |
if (fd > -1) { |
| 1540 |
errno = 0; |
| 1541 |
fs = get_filestream(f, bfile); |
| 1542 |
if (fs) { |
| 1543 |
if ((fs->write_buffer = ast_malloc(32768))) { |
| 1544 |
setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768); |
| 1545 |
} |
| 1546 |
} |
| 1547 |
if (!fs || rewrite_wrapper(fs, comment)) { |
| 1548 |
ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn); |
| 1549 |
close(fd); |
| 1550 |
if (orig_fn) { |
| 1551 |
unlink(fn); |
| 1552 |
unlink(orig_fn); |
| 1553 |
ast_free(orig_fn); |
| 1554 |
} |
| 1555 |
if (fs) { |
| 1556 |
ast_closestream(fs); |
| 1557 |
fs = NULL; |
| 1558 |
} |
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
ast_free(fn); |
| 1565 |
continue; |
| 1566 |
} |
| 1567 |
fs->trans = NULL; |
| 1568 |
fs->fmt = f; |
| 1569 |
fs->flags = flags; |
| 1570 |
fs->mode = mode; |
| 1571 |
if (orig_fn) { |
| 1572 |
fs->realfilename = orig_fn; |
| 1573 |
fs->filename = fn; |
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
orig_fn = NULL; |
| 1579 |
fn = NULL; |
| 1580 |
} else { |
| 1581 |
fs->realfilename = NULL; |
| 1582 |
fs->filename = ast_strdup(filename); |
| 1583 |
} |
| 1584 |
fs->vfs = NULL; |
| 1585 |
|
| 1586 |
f->seek(fs, 0, SEEK_END); |
| 1587 |
} else if (errno != EEXIST) { |
| 1588 |
ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno)); |
| 1589 |
if (orig_fn) |
| 1590 |
unlink(orig_fn); |
| 1591 |
} |
| 1592 |
|
| 1593 |
ast_free(fn); |
| 1594 |
ast_free(orig_fn); |
| 1595 |
} |
| 1596 |
|
| 1597 |
AST_RWLIST_UNLOCK(&formats); |
| 1598 |
|
| 1599 |
if (!format_found) |
| 1600 |
ast_log(LOG_WARNING, "No such format '%s'\n", type); |
| 1601 |
|
| 1602 |
return fs; |
| 1603 |
} |
| 1604 |
|
| 1605 |
static void waitstream_control(struct ast_channel *c, |
| 1606 |
enum ast_waitstream_fr_cb_values type, |
| 1607 |
ast_waitstream_fr_cb cb, |
| 1608 |
int skip_ms) |
| 1609 |
{ |
| 1610 |
switch (type) |
| 1611 |
{ |
| 1612 |
case AST_WAITSTREAM_CB_FASTFORWARD: |
| 1613 |
{ |
| 1614 |
int eoftest; |
| 1615 |
ast_stream_fastforward(ast_channel_stream(c), skip_ms); |
| 1616 |
eoftest = fgetc(ast_channel_stream(c)->f); |
| 1617 |
if (feof(ast_channel_stream(c)->f)) { |
| 1618 |
ast_stream_rewind(ast_channel_stream(c), skip_ms); |
| 1619 |
} else { |
| 1620 |
ungetc(eoftest, ast_channel_stream(c)->f); |
| 1621 |
} |
| 1622 |
} |
| 1623 |
break; |
| 1624 |
case AST_WAITSTREAM_CB_REWIND: |
| 1625 |
ast_stream_rewind(ast_channel_stream(c), skip_ms); |
| 1626 |
break; |
| 1627 |
default: |
| 1628 |
break; |
| 1629 |
} |
| 1630 |
|
| 1631 |
if (cb) { |
| 1632 |
long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_get_sample_rate(ast_channel_stream(c)->fmt->format) / 1000); |
| 1633 |
cb(c, ms_len, type); |
| 1634 |
} |
| 1635 |
|
| 1636 |
ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n" |
| 1637 |
"Control: %s\r\n" |
| 1638 |
"SkipMs: %d\r\n", |
| 1639 |
ast_channel_name(c), |
| 1640 |
(type == AST_WAITSTREAM_CB_FASTFORWARD) ? "FastForward" : "Rewind", |
| 1641 |
skip_ms); |
| 1642 |
} |
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
static int waitstream_core(struct ast_channel *c, |
| 1648 |
const char *breakon, |
| 1649 |
const char *forward, |
| 1650 |
const char *reverse, |
| 1651 |
int skip_ms, |
| 1652 |
int audiofd, |
| 1653 |
int cmdfd, |
| 1654 |
const char *context, |
| 1655 |
ast_waitstream_fr_cb cb) |
| 1656 |
{ |
| 1657 |
const char *orig_chan_name = NULL; |
| 1658 |
|
| 1659 |
int err = 0; |
| 1660 |
|
| 1661 |
if (!breakon) |
| 1662 |
breakon = ""; |
| 1663 |
if (!forward) |
| 1664 |
forward = ""; |
| 1665 |
if (!reverse) |
| 1666 |
reverse = ""; |
| 1667 |
|
| 1668 |
|
| 1669 |
ast_channel_set_flag(c, AST_FLAG_END_DTMF_ONLY); |
| 1670 |
|
| 1671 |
if (ast_test_flag(ast_channel_flags(c), AST_FLAG_MASQ_NOSTREAM)) |
| 1672 |
orig_chan_name = ast_strdupa(ast_channel_name(c)); |
| 1673 |
|
| 1674 |
if (ast_channel_stream(c) && cb) { |
| 1675 |
long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_get_sample_rate(ast_channel_stream(c)->fmt->format) / 1000); |
| 1676 |
cb(c, ms_len, AST_WAITSTREAM_CB_START); |
| 1677 |
} |
| 1678 |
|
| 1679 |
while (ast_channel_stream(c)) { |
| 1680 |
int res; |
| 1681 |
int ms; |
| 1682 |
|
| 1683 |
if (orig_chan_name && strcasecmp(orig_chan_name, ast_channel_name(c))) { |
| 1684 |
ast_stopstream(c); |
| 1685 |
err = 1; |
| 1686 |
break; |
| 1687 |
} |
| 1688 |
|
| 1689 |
ms = ast_sched_wait(ast_channel_sched(c)); |
| 1690 |
|
| 1691 |
if (ms < 0 && !ast_channel_timingfunc(c)) { |
| 1692 |
ast_stopstream(c); |
| 1693 |
break; |
| 1694 |
} |
| 1695 |
if (ms < 0) |
| 1696 |
ms = 1000; |
| 1697 |
if (cmdfd < 0) { |
| 1698 |
res = ast_waitfor(c, ms); |
| 1699 |
if (res < 0) { |
| 1700 |
ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno)); |
| 1701 |
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY); |
| 1702 |
return res; |
| 1703 |
} |
| 1704 |
} else { |
| 1705 |
int outfd; |
| 1706 |
struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms); |
| 1707 |
if (!rchan && (outfd < 0) && (ms)) { |
| 1708 |
|
| 1709 |
if (errno == EINTR) |
| 1710 |
continue; |
| 1711 |
ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno)); |
| 1712 |
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY); |
| 1713 |
return -1; |
| 1714 |
} else if (outfd > -1) { |
| 1715 |
|
| 1716 |
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY); |
| 1717 |
return 1; |
| 1718 |
} |
| 1719 |
|
| 1720 |
res = rchan ? 1 : 0; |
| 1721 |
} |
| 1722 |
if (res > 0) { |
| 1723 |
struct ast_frame *fr = ast_read(c); |
| 1724 |
if (!fr) { |
| 1725 |
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY); |
| 1726 |
return -1; |
| 1727 |
} |
| 1728 |
switch (fr->frametype) { |
| 1729 |
case AST_FRAME_DTMF_END: |
| 1730 |
if (context) { |
| 1731 |
const char exten[2] = { fr->subclass.integer, '\0' }; |
| 1732 |
if (ast_exists_extension(c, context, exten, 1, |
| 1733 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 1734 |
res = fr->subclass.integer; |
| 1735 |
ast_frfree(fr); |
| 1736 |
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY); |
| 1737 |
return res; |
| 1738 |
} |
| 1739 |
} else { |
| 1740 |
res = fr->subclass.integer; |
| 1741 |
if (strchr(forward, res)) { |
| 1742 |
waitstream_control(c, AST_WAITSTREAM_CB_FASTFORWARD, cb, skip_ms); |
| 1743 |
} else if (strchr(reverse, res)) { |
| 1744 |
waitstream_control(c, AST_WAITSTREAM_CB_REWIND, cb, skip_ms); |
| 1745 |
} else if (strchr(breakon, res)) { |
| 1746 |
ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n" |
| 1747 |
"Control: %s\r\n", |
| 1748 |
ast_channel_name(c), |
| 1749 |
"Break"); |
| 1750 |
|
| 1751 |
ast_frfree(fr); |
| 1752 |
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY); |
| 1753 |
return res; |
| 1754 |
} |
| 1755 |
} |
| 1756 |
break; |
| 1757 |
case AST_FRAME_CONTROL: |
| 1758 |
switch (fr->subclass.integer) { |
| 1759 |
case AST_CONTROL_STREAM_STOP: |
| 1760 |
case AST_CONTROL_STREAM_SUSPEND: |
| 1761 |
case AST_CONTROL_STREAM_RESTART: |
| 1762 |
|
| 1763 |
ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n" |
| 1764 |
"Control: %s\r\n", |
| 1765 |
ast_channel_name(c), |
| 1766 |
"Break"); |
| 1767 |
res = fr->subclass.integer; |
| 1768 |
ast_frfree(fr); |
| 1769 |
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY); |
| 1770 |
return res; |
| 1771 |
case AST_CONTROL_STREAM_REVERSE: |
| 1772 |
if (!skip_ms) { |
| 1773 |
skip_ms = 3000; |
| 1774 |
} |
| 1775 |
waitstream_control(c, AST_WAITSTREAM_CB_REWIND, cb, skip_ms); |
| 1776 |
break; |
| 1777 |
case AST_CONTROL_STREAM_FORWARD: |
| 1778 |
if (!skip_ms) { |
| 1779 |
skip_ms = 3000; |
| 1780 |
} |
| 1781 |
waitstream_control(c, AST_WAITSTREAM_CB_FASTFORWARD, cb, skip_ms); |
| 1782 |
break; |
| 1783 |
case AST_CONTROL_HANGUP: |
| 1784 |
case AST_CONTROL_BUSY: |
| 1785 |
case AST_CONTROL_CONGESTION: |
| 1786 |
ast_frfree(fr); |
| 1787 |
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY); |
| 1788 |
return -1; |
| 1789 |
case AST_CONTROL_PROGRESS: |
| 1790 |
case AST_CONTROL_RINGING: |
| 1791 |
case AST_CONTROL_ANSWER: |
| 1792 |
case AST_CONTROL_VIDUPDATE: |
| 1793 |
case AST_CONTROL_SRCUPDATE: |
| 1794 |
case AST_CONTROL_SRCCHANGE: |
| 1795 |
case AST_CONTROL_HOLD: |
| 1796 |
case AST_CONTROL_UNHOLD: |
| 1797 |
case AST_CONTROL_CONNECTED_LINE: |
| 1798 |
case AST_CONTROL_REDIRECTING: |
| 1799 |
case AST_CONTROL_AOC: |
| 1800 |
case AST_CONTROL_UPDATE_RTP_PEER: |
| 1801 |
case AST_CONTROL_PVT_CAUSE_CODE: |
| 1802 |
case AST_CONTROL_FLASH: |
| 1803 |
case AST_CONTROL_WINK: |
| 1804 |
case -1: |
| 1805 |
|
| 1806 |
break; |
| 1807 |
default: |
| 1808 |
ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass.integer); |
| 1809 |
} |
| 1810 |
break; |
| 1811 |
case AST_FRAME_VOICE: |
| 1812 |
|
| 1813 |
if (audiofd > -1) { |
| 1814 |
if (write(audiofd, fr->data.ptr, fr->datalen) < 0) { |
| 1815 |
ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno)); |
| 1816 |
} |
| 1817 |
} |
| 1818 |
default: |
| 1819 |
|
| 1820 |
break; |
| 1821 |
} |
| 1822 |
ast_frfree(fr); |
| 1823 |
} |
| 1824 |
ast_sched_runq(ast_channel_sched(c)); |
| 1825 |
} |
| 1826 |
|
| 1827 |
ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY); |
| 1828 |
|
| 1829 |
return (err || ast_channel_softhangup_internal_flag(c)) ? -1 : 0; |
| 1830 |
} |
| 1831 |
|
| 1832 |
int ast_waitstream_fr_w_cb(struct ast_channel *c, |
| 1833 |
const char *breakon, |
| 1834 |
const char *forward, |
| 1835 |
const char *reverse, |
| 1836 |
int ms, |
| 1837 |
ast_waitstream_fr_cb cb) |
| 1838 |
{ |
| 1839 |
return waitstream_core(c, breakon, forward, reverse, ms, |
| 1840 |
-1 , -1 , NULL , cb); |
| 1841 |
} |
| 1842 |
|
| 1843 |
int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *reverse, int ms) |
| 1844 |
{ |
| 1845 |
return waitstream_core(c, breakon, forward, reverse, ms, |
| 1846 |
-1 , -1 , NULL , NULL ); |
| 1847 |
} |
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
static int sanitize_waitstream_return(int return_value) |
| 1858 |
{ |
| 1859 |
switch (return_value) { |
| 1860 |
case AST_CONTROL_STREAM_STOP: |
| 1861 |
case AST_CONTROL_STREAM_SUSPEND: |
| 1862 |
case AST_CONTROL_STREAM_RESTART: |
| 1863 |
|
| 1864 |
return_value = 0; |
| 1865 |
break; |
| 1866 |
default: |
| 1867 |
|
| 1868 |
break; |
| 1869 |
} |
| 1870 |
|
| 1871 |
return return_value; |
| 1872 |
} |
| 1873 |
|
| 1874 |
int ast_waitstream(struct ast_channel *c, const char *breakon) |
| 1875 |
{ |
| 1876 |
int res; |
| 1877 |
|
| 1878 |
res = waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL, NULL ); |
| 1879 |
|
| 1880 |
return sanitize_waitstream_return(res); |
| 1881 |
} |
| 1882 |
|
| 1883 |
int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd) |
| 1884 |
{ |
| 1885 |
int res; |
| 1886 |
|
| 1887 |
res = waitstream_core(c, breakon, NULL, NULL, 0, |
| 1888 |
audiofd, cmdfd, NULL , NULL ); |
| 1889 |
|
| 1890 |
return sanitize_waitstream_return(res); |
| 1891 |
} |
| 1892 |
|
| 1893 |
int ast_waitstream_exten(struct ast_channel *c, const char *context) |
| 1894 |
{ |
| 1895 |
int res; |
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
if (!context) |
| 1900 |
context = ast_channel_context(c); |
| 1901 |
res = waitstream_core(c, NULL, NULL, NULL, 0, |
| 1902 |
-1, -1, context, NULL ); |
| 1903 |
|
| 1904 |
return sanitize_waitstream_return(res); |
| 1905 |
} |
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits) |
| 1913 |
{ |
| 1914 |
int res = 0; |
| 1915 |
if (!ast_strlen_zero(file)) { |
| 1916 |
res = ast_streamfile(chan, file, ast_channel_language(chan)); |
| 1917 |
if (!res) { |
| 1918 |
res = ast_waitstream(chan, digits); |
| 1919 |
} |
| 1920 |
} |
| 1921 |
if (res == -1) { |
| 1922 |
ast_stopstream(chan); |
| 1923 |
} |
| 1924 |
|
| 1925 |
return res; |
| 1926 |
} |
| 1927 |
|
| 1928 |
char *ast_format_str_reduce(char *fmts) |
| 1929 |
{ |
| 1930 |
struct ast_format_def *f; |
| 1931 |
struct ast_format_def *fmts_ptr[AST_MAX_FORMATS]; |
| 1932 |
char *fmts_str[AST_MAX_FORMATS]; |
| 1933 |
char *stringp, *type; |
| 1934 |
char *orig = fmts; |
| 1935 |
int i, j, x, first, found = 0; |
| 1936 |
int len = strlen(fmts) + 1; |
| 1937 |
int res; |
| 1938 |
|
| 1939 |
if (AST_RWLIST_RDLOCK(&formats)) { |
| 1940 |
ast_log(LOG_WARNING, "Unable to lock format list\n"); |
| 1941 |
return NULL; |
| 1942 |
} |
| 1943 |
|
| 1944 |
stringp = ast_strdupa(fmts); |
| 1945 |
|
| 1946 |
for (x = 0; (type = strsep(&stringp, "|")) && x < AST_MAX_FORMATS; x++) { |
| 1947 |
AST_RWLIST_TRAVERSE(&formats, f, list) { |
| 1948 |
if (exts_compare(f->exts, type)) { |
| 1949 |
found = 1; |
| 1950 |
break; |
| 1951 |
} |
| 1952 |
} |
| 1953 |
|
| 1954 |
fmts_str[x] = type; |
| 1955 |
if (found) { |
| 1956 |
fmts_ptr[x] = f; |
| 1957 |
} else { |
| 1958 |
fmts_ptr[x] = NULL; |
| 1959 |
} |
| 1960 |
} |
| 1961 |
AST_RWLIST_UNLOCK(&formats); |
| 1962 |
|
| 1963 |
first = 1; |
| 1964 |
for (i = 0; i < x; i++) { |
| 1965 |
|
| 1966 |
if (!fmts_ptr[i]) { |
| 1967 |
ast_log(LOG_WARNING, "ignoring unknown format '%s'\n", fmts_str[i]); |
| 1968 |
continue; |
| 1969 |
} |
| 1970 |
|
| 1971 |
|
| 1972 |
if (first) { |
| 1973 |
res = snprintf(fmts, len, "%s", fmts_str[i]); |
| 1974 |
fmts += res; |
| 1975 |
len -= res; |
| 1976 |
first = 0; |
| 1977 |
continue; |
| 1978 |
} |
| 1979 |
|
| 1980 |
found = 0; |
| 1981 |
for (j = 0; j < i; j++) { |
| 1982 |
|
| 1983 |
if (fmts_ptr[j] == fmts_ptr[i]) { |
| 1984 |
found = 1; |
| 1985 |
break; |
| 1986 |
} |
| 1987 |
} |
| 1988 |
|
| 1989 |
if (!found) { |
| 1990 |
res = snprintf(fmts, len, "|%s", fmts_str[i]); |
| 1991 |
fmts += res; |
| 1992 |
len -= res; |
| 1993 |
} |
| 1994 |
} |
| 1995 |
|
| 1996 |
if (first) { |
| 1997 |
ast_log(LOG_WARNING, "no known formats found in format list (%s)\n", orig); |
| 1998 |
return NULL; |
| 1999 |
} |
| 2000 |
|
| 2001 |
return orig; |
| 2002 |
} |
| 2003 |
|
| 2004 |
static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2005 |
{ |
| 2006 |
#define FORMAT "%-10s %-10s %-20s\n" |
| 2007 |
#define FORMAT2 "%-10s %-10s %-20s\n" |
| 2008 |
struct ast_format_def *f; |
| 2009 |
int count_fmt = 0; |
| 2010 |
|
| 2011 |
switch (cmd) { |
| 2012 |
case CLI_INIT: |
| 2013 |
e->command = "core show file formats"; |
| 2014 |
e->usage = |
| 2015 |
"Usage: core show file formats\n" |
| 2016 |
" Displays currently registered file formats (if any).\n"; |
| 2017 |
return NULL; |
| 2018 |
case CLI_GENERATE: |
| 2019 |
return NULL; |
| 2020 |
} |
| 2021 |
|
| 2022 |
if (a->argc != 4) |
| 2023 |
return CLI_SHOWUSAGE; |
| 2024 |
|
| 2025 |
ast_cli(a->fd, FORMAT, "Format", "Name", "Extensions"); |
| 2026 |
ast_cli(a->fd, FORMAT, "------", "----", "----------"); |
| 2027 |
|
| 2028 |
AST_RWLIST_RDLOCK(&formats); |
| 2029 |
AST_RWLIST_TRAVERSE(&formats, f, list) { |
| 2030 |
ast_cli(a->fd, FORMAT2, ast_format_get_name(f->format), f->name, f->exts); |
| 2031 |
count_fmt++; |
| 2032 |
} |
| 2033 |
AST_RWLIST_UNLOCK(&formats); |
| 2034 |
ast_cli(a->fd, "%d file formats registered.\n", count_fmt); |
| 2035 |
return CLI_SUCCESS; |
| 2036 |
#undef FORMAT |
| 2037 |
#undef FORMAT2 |
| 2038 |
} |
| 2039 |
|
| 2040 |
struct ast_format *ast_get_format_for_file_ext(const char *file_ext) |
| 2041 |
{ |
| 2042 |
struct ast_format_def *f; |
| 2043 |
SCOPED_RDLOCK(lock, &formats.lock); |
| 2044 |
AST_RWLIST_TRAVERSE(&formats, f, list) { |
| 2045 |
if (exts_compare(f->exts, file_ext)) { |
| 2046 |
return f->format; |
| 2047 |
} |
| 2048 |
} |
| 2049 |
|
| 2050 |
return NULL; |
| 2051 |
} |
| 2052 |
|
| 2053 |
int ast_get_extension_for_mime_type(const char *mime_type, char *buffer, size_t capacity) |
| 2054 |
{ |
| 2055 |
struct ast_format_def *f; |
| 2056 |
SCOPED_RDLOCK(lock, &formats.lock); |
| 2057 |
|
| 2058 |
ast_assert(buffer && capacity); |
| 2059 |
|
| 2060 |
AST_RWLIST_TRAVERSE(&formats, f, list) { |
| 2061 |
if (type_in_list(f->mime_types, mime_type, strcasecmp)) { |
| 2062 |
size_t item_len = strcspn(f->exts, "|"); |
| 2063 |
size_t bytes_written = snprintf(buffer, capacity, ".%.*s", (int) item_len, f->exts); |
| 2064 |
if (bytes_written < capacity) { |
| 2065 |
|
| 2066 |
return 1; |
| 2067 |
} |
| 2068 |
} |
| 2069 |
} |
| 2070 |
|
| 2071 |
return 0; |
| 2072 |
} |
| 2073 |
|
| 2074 |
static struct ast_cli_entry cli_file[] = { |
| 2075 |
AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats") |
| 2076 |
}; |
| 2077 |
|
| 2078 |
static void file_shutdown(void) |
| 2079 |
{ |
| 2080 |
ast_cli_unregister_multiple(cli_file, ARRAY_LEN(cli_file)); |
| 2081 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_format_register_type); |
| 2082 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_format_unregister_type); |
| 2083 |
} |
| 2084 |
|
| 2085 |
int ast_file_init(void) |
| 2086 |
{ |
| 2087 |
STASIS_MESSAGE_TYPE_INIT(ast_format_register_type); |
| 2088 |
STASIS_MESSAGE_TYPE_INIT(ast_format_unregister_type); |
| 2089 |
ast_cli_register_multiple(cli_file, ARRAY_LEN(cli_file)); |
| 2090 |
ast_register_cleanup(file_shutdown); |
| 2091 |
return 0; |
| 2092 |
} |