| 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 |
#include "asterisk.h" |
| 32 |
|
| 33 |
#include "asterisk/dsp.h" |
| 34 |
#include "asterisk/file.h" |
| 35 |
#include "asterisk/module.h" |
| 36 |
#include "asterisk/paths.h" |
| 37 |
#include "asterisk/stasis_app_impl.h" |
| 38 |
#include "asterisk/stasis_app_recording.h" |
| 39 |
#include "asterisk/stasis_channels.h" |
| 40 |
|
| 41 |
|
| 42 |
#define RECORDING_BUCKETS 127 |
| 43 |
|
| 44 |
|
| 45 |
#define RECORDING_COMMENT NULL |
| 46 |
|
| 47 |
|
| 48 |
#define RECORDING_CHECK 0 |
| 49 |
|
| 50 |
|
| 51 |
static struct ao2_container *recordings; |
| 52 |
|
| 53 |
struct stasis_app_recording { |
| 54 |
|
| 55 |
struct stasis_app_recording_options *options; |
| 56 |
|
| 57 |
char *absolute_name; |
| 58 |
|
| 59 |
struct stasis_app_control *control; |
| 60 |
|
| 61 |
enum stasis_app_recording_state state; |
| 62 |
|
| 63 |
struct { |
| 64 |
|
| 65 |
int total; |
| 66 |
|
| 67 |
int energy_only; |
| 68 |
} duration; |
| 69 |
|
| 70 |
int muted:1; |
| 71 |
}; |
| 72 |
|
| 73 |
static struct ast_json *recording_to_json(struct stasis_message *message, |
| 74 |
const struct stasis_message_sanitizer *sanitize) |
| 75 |
{ |
| 76 |
struct ast_channel_blob *channel_blob = stasis_message_data(message); |
| 77 |
struct ast_json *blob = channel_blob->blob; |
| 78 |
const char *state = |
| 79 |
ast_json_string_get(ast_json_object_get(blob, "state")); |
| 80 |
const char *type; |
| 81 |
|
| 82 |
if (!strcmp(state, "recording")) { |
| 83 |
type = "RecordingStarted"; |
| 84 |
} else if (!strcmp(state, "done") || !strcasecmp(state, "canceled")) { |
| 85 |
type = "RecordingFinished"; |
| 86 |
} else if (!strcmp(state, "failed")) { |
| 87 |
type = "RecordingFailed"; |
| 88 |
} else { |
| 89 |
return NULL; |
| 90 |
} |
| 91 |
|
| 92 |
return ast_json_pack("{s: s, s: o?, s: O}", |
| 93 |
"type", type, |
| 94 |
"timestamp", ast_json_timeval(*stasis_message_timestamp(message), NULL), |
| 95 |
"recording", blob); |
| 96 |
} |
| 97 |
|
| 98 |
STASIS_MESSAGE_TYPE_DEFN(stasis_app_recording_snapshot_type, |
| 99 |
.to_json = recording_to_json, |
| 100 |
); |
| 101 |
|
| 102 |
static int recording_hash(const void *obj, int flags) |
| 103 |
{ |
| 104 |
const struct stasis_app_recording *recording = obj; |
| 105 |
const char *id = flags & OBJ_KEY ? obj : recording->options->name; |
| 106 |
return ast_str_hash(id); |
| 107 |
} |
| 108 |
|
| 109 |
static int recording_cmp(void *obj, void *arg, int flags) |
| 110 |
{ |
| 111 |
struct stasis_app_recording *lhs = obj; |
| 112 |
struct stasis_app_recording *rhs = arg; |
| 113 |
const char *rhs_id = flags & OBJ_KEY ? arg : rhs->options->name; |
| 114 |
|
| 115 |
if (strcmp(lhs->options->name, rhs_id) == 0) { |
| 116 |
return CMP_MATCH | CMP_STOP; |
| 117 |
} else { |
| 118 |
return 0; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
static const char *state_to_string(enum stasis_app_recording_state state) |
| 123 |
{ |
| 124 |
switch (state) { |
| 125 |
case STASIS_APP_RECORDING_STATE_QUEUED: |
| 126 |
return "queued"; |
| 127 |
case STASIS_APP_RECORDING_STATE_RECORDING: |
| 128 |
return "recording"; |
| 129 |
case STASIS_APP_RECORDING_STATE_PAUSED: |
| 130 |
return "paused"; |
| 131 |
case STASIS_APP_RECORDING_STATE_COMPLETE: |
| 132 |
return "done"; |
| 133 |
case STASIS_APP_RECORDING_STATE_FAILED: |
| 134 |
return "failed"; |
| 135 |
case STASIS_APP_RECORDING_STATE_CANCELED: |
| 136 |
return "canceled"; |
| 137 |
case STASIS_APP_RECORDING_STATE_MAX: |
| 138 |
return "?"; |
| 139 |
} |
| 140 |
|
| 141 |
return "?"; |
| 142 |
} |
| 143 |
|
| 144 |
static void recording_options_dtor(void *obj) |
| 145 |
{ |
| 146 |
struct stasis_app_recording_options *options = obj; |
| 147 |
|
| 148 |
ast_string_field_free_memory(options); |
| 149 |
} |
| 150 |
|
| 151 |
struct stasis_app_recording_options *stasis_app_recording_options_create( |
| 152 |
const char *name, const char *format) |
| 153 |
{ |
| 154 |
RAII_VAR(struct stasis_app_recording_options *, options, NULL, |
| 155 |
ao2_cleanup); |
| 156 |
|
| 157 |
options = ao2_alloc(sizeof(*options), recording_options_dtor); |
| 158 |
|
| 159 |
if (!options || ast_string_field_init(options, 128)) { |
| 160 |
return NULL; |
| 161 |
} |
| 162 |
ast_string_field_set(options, name, name); |
| 163 |
ast_string_field_set(options, format, format); |
| 164 |
|
| 165 |
ao2_ref(options, +1); |
| 166 |
return options; |
| 167 |
} |
| 168 |
|
| 169 |
char stasis_app_recording_termination_parse(const char *str) |
| 170 |
{ |
| 171 |
if (ast_strlen_zero(str)) { |
| 172 |
return STASIS_APP_RECORDING_TERMINATE_NONE; |
| 173 |
} |
| 174 |
|
| 175 |
if (strcasecmp(str, "none") == 0) { |
| 176 |
return STASIS_APP_RECORDING_TERMINATE_NONE; |
| 177 |
} |
| 178 |
|
| 179 |
if (strcasecmp(str, "any") == 0) { |
| 180 |
return STASIS_APP_RECORDING_TERMINATE_ANY; |
| 181 |
} |
| 182 |
|
| 183 |
if (strcasecmp(str, "#") == 0) { |
| 184 |
return '#'; |
| 185 |
} |
| 186 |
|
| 187 |
if (strcasecmp(str, "*") == 0) { |
| 188 |
return '*'; |
| 189 |
} |
| 190 |
|
| 191 |
return STASIS_APP_RECORDING_TERMINATE_INVALID; |
| 192 |
} |
| 193 |
|
| 194 |
enum ast_record_if_exists stasis_app_recording_if_exists_parse( |
| 195 |
const char *str) |
| 196 |
{ |
| 197 |
if (ast_strlen_zero(str)) { |
| 198 |
|
| 199 |
return AST_RECORD_IF_EXISTS_FAIL; |
| 200 |
} |
| 201 |
|
| 202 |
if (strcasecmp(str, "fail") == 0) { |
| 203 |
return AST_RECORD_IF_EXISTS_FAIL; |
| 204 |
} |
| 205 |
|
| 206 |
if (strcasecmp(str, "overwrite") == 0) { |
| 207 |
return AST_RECORD_IF_EXISTS_OVERWRITE; |
| 208 |
} |
| 209 |
|
| 210 |
if (strcasecmp(str, "append") == 0) { |
| 211 |
return AST_RECORD_IF_EXISTS_APPEND; |
| 212 |
} |
| 213 |
|
| 214 |
return AST_RECORD_IF_EXISTS_ERROR; |
| 215 |
} |
| 216 |
|
| 217 |
static void recording_publish(struct stasis_app_recording *recording, const char *cause) |
| 218 |
{ |
| 219 |
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); |
| 220 |
RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup); |
| 221 |
|
| 222 |
ast_assert(recording != NULL); |
| 223 |
|
| 224 |
json = stasis_app_recording_to_json(recording); |
| 225 |
if (json == NULL) { |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
if (!ast_strlen_zero(cause)) { |
| 230 |
struct ast_json *failure_cause = ast_json_string_create(cause); |
| 231 |
|
| 232 |
if (!failure_cause) { |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
if (ast_json_object_set(json, "cause", failure_cause)) { |
| 237 |
return; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
message = ast_channel_blob_create_from_cache( |
| 242 |
stasis_app_control_get_channel_id(recording->control), |
| 243 |
stasis_app_recording_snapshot_type(), json); |
| 244 |
if (message == NULL) { |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
stasis_app_control_publish(recording->control, message); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
static void recording_set_state(struct stasis_app_recording *recording, |
| 253 |
enum stasis_app_recording_state state, |
| 254 |
const char *cause) |
| 255 |
{ |
| 256 |
SCOPED_AO2LOCK(lock, recording); |
| 257 |
recording->state = state; |
| 258 |
recording_publish(recording, cause); |
| 259 |
} |
| 260 |
|
| 261 |
static enum stasis_app_control_channel_result check_rule_recording( |
| 262 |
const struct stasis_app_control *control) |
| 263 |
{ |
| 264 |
return STASIS_APP_CHANNEL_RECORDING; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
static struct stasis_app_control_rule rule_recording = { |
| 274 |
.check_rule = check_rule_recording |
| 275 |
}; |
| 276 |
|
| 277 |
static void recording_fail(struct stasis_app_control *control, |
| 278 |
struct stasis_app_recording *recording, |
| 279 |
const char *cause) |
| 280 |
{ |
| 281 |
stasis_app_control_unregister_add_rule(control, &rule_recording); |
| 282 |
|
| 283 |
recording_set_state( |
| 284 |
recording, STASIS_APP_RECORDING_STATE_FAILED, cause); |
| 285 |
} |
| 286 |
|
| 287 |
static void recording_cleanup(void *data) |
| 288 |
{ |
| 289 |
struct stasis_app_recording *recording = data; |
| 290 |
|
| 291 |
ao2_unlink_flags(recordings, recording, |
| 292 |
OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA); |
| 293 |
ao2_ref(recording, -1); |
| 294 |
} |
| 295 |
|
| 296 |
static int record_file(struct stasis_app_control *control, |
| 297 |
struct ast_channel *chan, void *data) |
| 298 |
{ |
| 299 |
struct stasis_app_recording *recording = data; |
| 300 |
char *acceptdtmf; |
| 301 |
int res; |
| 302 |
|
| 303 |
ast_assert(recording != NULL); |
| 304 |
|
| 305 |
if (stasis_app_get_bridge(control)) { |
| 306 |
ast_log(LOG_ERROR, "Cannot record channel while in bridge\n"); |
| 307 |
recording_fail(control, recording, "Cannot record channel while in bridge"); |
| 308 |
return -1; |
| 309 |
} |
| 310 |
|
| 311 |
switch (recording->options->terminate_on) { |
| 312 |
case STASIS_APP_RECORDING_TERMINATE_NONE: |
| 313 |
case STASIS_APP_RECORDING_TERMINATE_INVALID: |
| 314 |
acceptdtmf = ""; |
| 315 |
break; |
| 316 |
case STASIS_APP_RECORDING_TERMINATE_ANY: |
| 317 |
acceptdtmf = "#*0123456789abcd"; |
| 318 |
break; |
| 319 |
default: |
| 320 |
acceptdtmf = ast_alloca(2); |
| 321 |
acceptdtmf[0] = recording->options->terminate_on; |
| 322 |
acceptdtmf[1] = '\0'; |
| 323 |
} |
| 324 |
|
| 325 |
res = ast_auto_answer(chan); |
| 326 |
if (res != 0) { |
| 327 |
ast_debug(3, "%s: Failed to answer\n", |
| 328 |
ast_channel_uniqueid(chan)); |
| 329 |
recording_fail(control, recording, "Failed to answer channel"); |
| 330 |
return -1; |
| 331 |
} |
| 332 |
|
| 333 |
recording_set_state( |
| 334 |
recording, STASIS_APP_RECORDING_STATE_RECORDING, NULL); |
| 335 |
ast_play_and_record_full(chan, |
| 336 |
NULL, |
| 337 |
recording->absolute_name, |
| 338 |
recording->options->max_duration_seconds, |
| 339 |
recording->options->format, |
| 340 |
&recording->duration.total, |
| 341 |
recording->options->max_silence_seconds ? &recording->duration.energy_only : NULL, |
| 342 |
recording->options->beep, |
| 343 |
-1, |
| 344 |
recording->options->max_silence_seconds * 1000, |
| 345 |
NULL, |
| 346 |
acceptdtmf, |
| 347 |
NULL, |
| 348 |
1, |
| 349 |
recording->options->if_exists); |
| 350 |
|
| 351 |
ast_debug(3, "%s: Recording complete\n", ast_channel_uniqueid(chan)); |
| 352 |
|
| 353 |
recording_set_state( |
| 354 |
recording, STASIS_APP_RECORDING_STATE_COMPLETE, NULL); |
| 355 |
|
| 356 |
stasis_app_control_unregister_add_rule(control, &rule_recording); |
| 357 |
|
| 358 |
return 0; |
| 359 |
} |
| 360 |
|
| 361 |
static void recording_dtor(void *obj) |
| 362 |
{ |
| 363 |
struct stasis_app_recording *recording = obj; |
| 364 |
|
| 365 |
ast_free(recording->absolute_name); |
| 366 |
ao2_cleanup(recording->control); |
| 367 |
ao2_cleanup(recording->options); |
| 368 |
} |
| 369 |
|
| 370 |
struct stasis_app_recording *stasis_app_control_record( |
| 371 |
struct stasis_app_control *control, |
| 372 |
struct stasis_app_recording_options *options) |
| 373 |
{ |
| 374 |
struct stasis_app_recording *recording; |
| 375 |
char *last_slash; |
| 376 |
|
| 377 |
errno = 0; |
| 378 |
|
| 379 |
if (options == NULL || |
| 380 |
ast_strlen_zero(options->name) || |
| 381 |
ast_strlen_zero(options->format) || |
| 382 |
options->max_silence_seconds < 0 || |
| 383 |
options->max_duration_seconds < 0) { |
| 384 |
errno = EINVAL; |
| 385 |
return NULL; |
| 386 |
} |
| 387 |
|
| 388 |
ast_debug(3, "%s: Sending record(%s.%s) command\n", |
| 389 |
stasis_app_control_get_channel_id(control), options->name, |
| 390 |
options->format); |
| 391 |
|
| 392 |
recording = ao2_alloc(sizeof(*recording), recording_dtor); |
| 393 |
if (!recording) { |
| 394 |
errno = ENOMEM; |
| 395 |
return NULL; |
| 396 |
} |
| 397 |
recording->duration.total = -1; |
| 398 |
recording->duration.energy_only = -1; |
| 399 |
|
| 400 |
ast_asprintf(&recording->absolute_name, "%s/%s", |
| 401 |
ast_config_AST_RECORDING_DIR, options->name); |
| 402 |
|
| 403 |
if (recording->absolute_name == NULL) { |
| 404 |
errno = ENOMEM; |
| 405 |
ao2_ref(recording, -1); |
| 406 |
return NULL; |
| 407 |
} |
| 408 |
|
| 409 |
if ((last_slash = strrchr(recording->absolute_name, '/'))) { |
| 410 |
*last_slash = '\0'; |
| 411 |
if (ast_safe_mkdir(ast_config_AST_RECORDING_DIR, |
| 412 |
recording->absolute_name, 0777) != 0) { |
| 413 |
|
| 414 |
ao2_ref(recording, -1); |
| 415 |
return NULL; |
| 416 |
} |
| 417 |
*last_slash = '/'; |
| 418 |
} |
| 419 |
|
| 420 |
ao2_ref(options, +1); |
| 421 |
recording->options = options; |
| 422 |
ao2_ref(control, +1); |
| 423 |
recording->control = control; |
| 424 |
recording->state = STASIS_APP_RECORDING_STATE_QUEUED; |
| 425 |
|
| 426 |
if ((recording->options->if_exists == AST_RECORD_IF_EXISTS_FAIL) && |
| 427 |
(ast_fileexists(recording->absolute_name, NULL, NULL))) { |
| 428 |
ast_log(LOG_WARNING, "Recording file '%s' already exists and ifExists option is failure.\n", |
| 429 |
recording->absolute_name); |
| 430 |
errno = EEXIST; |
| 431 |
ao2_ref(recording, -1); |
| 432 |
return NULL; |
| 433 |
} |
| 434 |
|
| 435 |
{ |
| 436 |
RAII_VAR(struct stasis_app_recording *, old_recording, NULL, |
| 437 |
ao2_cleanup); |
| 438 |
|
| 439 |
SCOPED_AO2LOCK(lock, recordings); |
| 440 |
|
| 441 |
old_recording = ao2_find(recordings, options->name, |
| 442 |
OBJ_KEY | OBJ_NOLOCK); |
| 443 |
if (old_recording) { |
| 444 |
ast_log(LOG_WARNING, |
| 445 |
"Recording %s already in progress\n", |
| 446 |
recording->options->name); |
| 447 |
errno = EEXIST; |
| 448 |
ao2_ref(recording, -1); |
| 449 |
return NULL; |
| 450 |
} |
| 451 |
ao2_link(recordings, recording); |
| 452 |
} |
| 453 |
|
| 454 |
stasis_app_control_register_add_rule(control, &rule_recording); |
| 455 |
|
| 456 |
stasis_app_send_command_async(control, record_file, ao2_bump(recording), recording_cleanup); |
| 457 |
|
| 458 |
return recording; |
| 459 |
} |
| 460 |
|
| 461 |
enum stasis_app_recording_state stasis_app_recording_get_state( |
| 462 |
struct stasis_app_recording *recording) |
| 463 |
{ |
| 464 |
return recording->state; |
| 465 |
} |
| 466 |
|
| 467 |
const char *stasis_app_recording_get_name( |
| 468 |
struct stasis_app_recording *recording) |
| 469 |
{ |
| 470 |
return recording->options->name; |
| 471 |
} |
| 472 |
|
| 473 |
struct stasis_app_recording *stasis_app_recording_find_by_name(const char *name) |
| 474 |
{ |
| 475 |
return ao2_find(recordings, name, OBJ_KEY); |
| 476 |
} |
| 477 |
|
| 478 |
struct ast_json *stasis_app_recording_to_json( |
| 479 |
const struct stasis_app_recording *recording) |
| 480 |
{ |
| 481 |
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); |
| 482 |
|
| 483 |
if (recording == NULL) { |
| 484 |
return NULL; |
| 485 |
} |
| 486 |
|
| 487 |
json = ast_json_pack("{s: s, s: s, s: s, s: s}", |
| 488 |
"name", recording->options->name, |
| 489 |
"format", recording->options->format, |
| 490 |
"state", state_to_string(recording->state), |
| 491 |
"target_uri", recording->options->target); |
| 492 |
if (json && recording->duration.total > -1) { |
| 493 |
ast_json_object_set(json, "duration", |
| 494 |
ast_json_integer_create(recording->duration.total)); |
| 495 |
} |
| 496 |
if (json && recording->duration.energy_only > -1) { |
| 497 |
ast_json_object_set(json, "talking_duration", |
| 498 |
ast_json_integer_create(recording->duration.energy_only)); |
| 499 |
ast_json_object_set(json, "silence_duration", |
| 500 |
ast_json_integer_create(recording->duration.total - recording->duration.energy_only)); |
| 501 |
} |
| 502 |
|
| 503 |
return ast_json_ref(json); |
| 504 |
} |
| 505 |
|
| 506 |
typedef int (*recording_operation_cb)(struct stasis_app_recording *recording); |
| 507 |
|
| 508 |
static int recording_noop(struct stasis_app_recording *recording) |
| 509 |
{ |
| 510 |
return 0; |
| 511 |
} |
| 512 |
|
| 513 |
static int recording_disregard(struct stasis_app_recording *recording) |
| 514 |
{ |
| 515 |
recording->state = STASIS_APP_RECORDING_STATE_CANCELED; |
| 516 |
return 0; |
| 517 |
} |
| 518 |
|
| 519 |
static int recording_cancel(struct stasis_app_recording *recording) |
| 520 |
{ |
| 521 |
int res = 0; |
| 522 |
recording->state = STASIS_APP_RECORDING_STATE_CANCELED; |
| 523 |
res |= stasis_app_control_queue_control(recording->control, |
| 524 |
AST_CONTROL_RECORD_CANCEL); |
| 525 |
res |= ast_filedelete(recording->absolute_name, NULL); |
| 526 |
return res; |
| 527 |
} |
| 528 |
|
| 529 |
static int recording_stop(struct stasis_app_recording *recording) |
| 530 |
{ |
| 531 |
recording->state = STASIS_APP_RECORDING_STATE_COMPLETE; |
| 532 |
return stasis_app_control_queue_control(recording->control, |
| 533 |
AST_CONTROL_RECORD_STOP); |
| 534 |
} |
| 535 |
|
| 536 |
static int recording_pause(struct stasis_app_recording *recording) |
| 537 |
{ |
| 538 |
recording->state = STASIS_APP_RECORDING_STATE_PAUSED; |
| 539 |
return stasis_app_control_queue_control(recording->control, |
| 540 |
AST_CONTROL_RECORD_SUSPEND); |
| 541 |
} |
| 542 |
|
| 543 |
static int recording_unpause(struct stasis_app_recording *recording) |
| 544 |
{ |
| 545 |
recording->state = STASIS_APP_RECORDING_STATE_RECORDING; |
| 546 |
return stasis_app_control_queue_control(recording->control, |
| 547 |
AST_CONTROL_RECORD_SUSPEND); |
| 548 |
} |
| 549 |
|
| 550 |
static int toggle_recording_mute(struct stasis_app_recording *recording, int desired_mute_state) |
| 551 |
{ |
| 552 |
if (recording->muted == desired_mute_state) { |
| 553 |
|
| 554 |
return 0; |
| 555 |
} |
| 556 |
|
| 557 |
recording->muted = desired_mute_state; |
| 558 |
|
| 559 |
return stasis_app_control_queue_control(recording->control, |
| 560 |
AST_CONTROL_RECORD_MUTE); |
| 561 |
} |
| 562 |
|
| 563 |
static int recording_mute(struct stasis_app_recording *recording) |
| 564 |
{ |
| 565 |
return toggle_recording_mute(recording, 1); |
| 566 |
} |
| 567 |
|
| 568 |
static int recording_unmute(struct stasis_app_recording *recording) |
| 569 |
{ |
| 570 |
return toggle_recording_mute(recording, 0); |
| 571 |
} |
| 572 |
|
| 573 |
recording_operation_cb operations[STASIS_APP_RECORDING_STATE_MAX][STASIS_APP_RECORDING_OPER_MAX] = { |
| 574 |
[STASIS_APP_RECORDING_STATE_QUEUED][STASIS_APP_RECORDING_CANCEL] = recording_disregard, |
| 575 |
[STASIS_APP_RECORDING_STATE_QUEUED][STASIS_APP_RECORDING_STOP] = recording_disregard, |
| 576 |
[STASIS_APP_RECORDING_STATE_RECORDING][STASIS_APP_RECORDING_CANCEL] = recording_cancel, |
| 577 |
[STASIS_APP_RECORDING_STATE_RECORDING][STASIS_APP_RECORDING_STOP] = recording_stop, |
| 578 |
[STASIS_APP_RECORDING_STATE_RECORDING][STASIS_APP_RECORDING_PAUSE] = recording_pause, |
| 579 |
[STASIS_APP_RECORDING_STATE_RECORDING][STASIS_APP_RECORDING_UNPAUSE] = recording_noop, |
| 580 |
[STASIS_APP_RECORDING_STATE_RECORDING][STASIS_APP_RECORDING_MUTE] = recording_mute, |
| 581 |
[STASIS_APP_RECORDING_STATE_RECORDING][STASIS_APP_RECORDING_UNMUTE] = recording_unmute, |
| 582 |
[STASIS_APP_RECORDING_STATE_PAUSED][STASIS_APP_RECORDING_CANCEL] = recording_cancel, |
| 583 |
[STASIS_APP_RECORDING_STATE_PAUSED][STASIS_APP_RECORDING_STOP] = recording_stop, |
| 584 |
[STASIS_APP_RECORDING_STATE_PAUSED][STASIS_APP_RECORDING_PAUSE] = recording_noop, |
| 585 |
[STASIS_APP_RECORDING_STATE_PAUSED][STASIS_APP_RECORDING_UNPAUSE] = recording_unpause, |
| 586 |
[STASIS_APP_RECORDING_STATE_PAUSED][STASIS_APP_RECORDING_MUTE] = recording_mute, |
| 587 |
[STASIS_APP_RECORDING_STATE_PAUSED][STASIS_APP_RECORDING_UNMUTE] = recording_unmute, |
| 588 |
}; |
| 589 |
|
| 590 |
enum stasis_app_recording_oper_results stasis_app_recording_operation( |
| 591 |
struct stasis_app_recording *recording, |
| 592 |
enum stasis_app_recording_media_operation operation) |
| 593 |
{ |
| 594 |
recording_operation_cb cb; |
| 595 |
SCOPED_AO2LOCK(lock, recording); |
| 596 |
|
| 597 |
if ((unsigned int)recording->state >= STASIS_APP_RECORDING_STATE_MAX) { |
| 598 |
ast_log(LOG_WARNING, "Invalid recording state %u\n", |
| 599 |
recording->state); |
| 600 |
return -1; |
| 601 |
} |
| 602 |
|
| 603 |
if ((unsigned int)operation >= STASIS_APP_RECORDING_OPER_MAX) { |
| 604 |
ast_log(LOG_WARNING, "Invalid recording operation %u\n", |
| 605 |
operation); |
| 606 |
return -1; |
| 607 |
} |
| 608 |
|
| 609 |
cb = operations[recording->state][operation]; |
| 610 |
|
| 611 |
if (!cb) { |
| 612 |
if (recording->state != STASIS_APP_RECORDING_STATE_RECORDING) { |
| 613 |
|
| 614 |
return STASIS_APP_RECORDING_OPER_NOT_RECORDING; |
| 615 |
} else { |
| 616 |
|
| 617 |
|
| 618 |
ast_log(LOG_ERROR, |
| 619 |
"Unhandled operation during recording: %u\n", |
| 620 |
operation); |
| 621 |
return STASIS_APP_RECORDING_OPER_FAILED; |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
return cb(recording) ? |
| 626 |
STASIS_APP_RECORDING_OPER_FAILED : STASIS_APP_RECORDING_OPER_OK; |
| 627 |
} |
| 628 |
|
| 629 |
static int load_module(void) |
| 630 |
{ |
| 631 |
int r; |
| 632 |
|
| 633 |
r = STASIS_MESSAGE_TYPE_INIT(stasis_app_recording_snapshot_type); |
| 634 |
if (r != 0) { |
| 635 |
return AST_MODULE_LOAD_DECLINE; |
| 636 |
} |
| 637 |
|
| 638 |
recordings = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, RECORDING_BUCKETS, |
| 639 |
recording_hash, NULL, recording_cmp); |
| 640 |
if (!recordings) { |
| 641 |
STASIS_MESSAGE_TYPE_CLEANUP(stasis_app_recording_snapshot_type); |
| 642 |
return AST_MODULE_LOAD_DECLINE; |
| 643 |
} |
| 644 |
return AST_MODULE_LOAD_SUCCESS; |
| 645 |
} |
| 646 |
|
| 647 |
static int unload_module(void) |
| 648 |
{ |
| 649 |
ao2_cleanup(recordings); |
| 650 |
recordings = NULL; |
| 651 |
STASIS_MESSAGE_TYPE_CLEANUP(stasis_app_recording_snapshot_type); |
| 652 |
return 0; |
| 653 |
} |
| 654 |
|
| 655 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Stasis application recording support", |
| 656 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 657 |
.load = load_module, |
| 658 |
.unload = unload_module, |
| 659 |
.requires = "res_stasis", |
| 660 |
.load_pri = AST_MODPRI_APP_DEPEND |
| 661 |
); |