Asterisk - The Open Source Telephony Project  21.4.1
Data Structures
stasis_app_recording.h File Reference

Stasis Application Recording API. See StasisApplication API" for detailed documentation. More...

#include "asterisk/app.h"
#include "asterisk/stasis_app.h"

Go to the source code of this file.

Data Structures

struct  stasis_app_recording_options
 

Functions

const char * stasis_app_stored_recording_get_file (struct stasis_app_stored_recording *recording)
 Returns the filename for this recording, for use with streamfile. More...
 
const char * stasis_app_stored_recording_get_filename (struct stasis_app_stored_recording *recording)
 Returns the full filename, with extension, for this recording. More...
 
const char * stasis_app_stored_recording_get_extension (struct stasis_app_stored_recording *recording)
 Returns the extension for this recording. More...
 
struct ast_jsonstasis_app_stored_recording_to_json (struct stasis_app_stored_recording *recording)
 Convert stored recording info to JSON. More...
 
struct ao2_containerstasis_app_stored_recording_find_all (void)
 Find all stored recordings on disk. More...
 
struct stasis_app_stored_recordingstasis_app_stored_recording_find_by_name (const char *name)
 Creates a stored recording object, with the given name. More...
 
int stasis_app_stored_recording_copy (struct stasis_app_stored_recording *src_recording, const char *dst, struct stasis_app_stored_recording **dst_recording)
 Copy a recording. More...
 
int stasis_app_stored_recording_delete (struct stasis_app_stored_recording *recording)
 Delete a recording from disk. More...
 
#define STASIS_APP_RECORDING_TERMINATE_INVALID   0
 
#define STASIS_APP_RECORDING_TERMINATE_NONE   -1
 
#define STASIS_APP_RECORDING_TERMINATE_ANY   -2
 
enum  stasis_app_recording_state {
  STASIS_APP_RECORDING_STATE_QUEUED, STASIS_APP_RECORDING_STATE_RECORDING, STASIS_APP_RECORDING_STATE_PAUSED, STASIS_APP_RECORDING_STATE_COMPLETE,
  STASIS_APP_RECORDING_STATE_FAILED, STASIS_APP_RECORDING_STATE_CANCELED, STASIS_APP_RECORDING_STATE_MAX
}
 
enum  stasis_app_recording_media_operation {
  STASIS_APP_RECORDING_CANCEL, STASIS_APP_RECORDING_STOP, STASIS_APP_RECORDING_PAUSE, STASIS_APP_RECORDING_UNPAUSE,
  STASIS_APP_RECORDING_MUTE, STASIS_APP_RECORDING_UNMUTE, STASIS_APP_RECORDING_OPER_MAX
}
 
enum  stasis_app_recording_oper_results { STASIS_APP_RECORDING_OPER_OK, STASIS_APP_RECORDING_OPER_FAILED, STASIS_APP_RECORDING_OPER_NOT_RECORDING }
 Possible results from a recording operation. More...
 
struct stasis_app_recording_optionsstasis_app_recording_options_create (const char *name, const char *format)
 Allocate a recording options object. More...
 
char stasis_app_recording_termination_parse (const char *str)
 Parse a string into the recording termination enum. More...
 
enum ast_record_if_exists stasis_app_recording_if_exists_parse (const char *str)
 Parse a string into the if_exists enum. More...
 
struct stasis_app_recordingstasis_app_control_record (struct stasis_app_control *control, struct stasis_app_recording_options *options)
 Record media from a channel. More...
 
enum stasis_app_recording_state stasis_app_recording_get_state (struct stasis_app_recording *recording)
 Gets the current state of a recording operation. More...
 
const char * stasis_app_recording_get_name (struct stasis_app_recording *recording)
 Gets the unique name of a recording object. More...
 
struct stasis_app_recordingstasis_app_recording_find_by_name (const char *name)
 Finds the recording object with the given name. More...
 
struct ast_jsonstasis_app_recording_to_json (const struct stasis_app_recording *recording)
 Construct a JSON model of a recording. More...
 
enum stasis_app_recording_oper_results stasis_app_recording_operation (struct stasis_app_recording *recording, enum stasis_app_recording_media_operation operation)
 Controls the media for a given recording operation. More...
 
struct stasis_message_typestasis_app_recording_snapshot_type (void)
 Message type for recording updates. The data is an ast_channel_blob.
 

Detailed Description

Stasis Application Recording API. See StasisApplication API" for detailed documentation.

Author
David M. Lee, II dlee@.nosp@m.digi.nosp@m.um.co.nosp@m.m
Since
12

Definition in file stasis_app_recording.h.

Enumeration Type Documentation

Valid operation for controlling a recording.

Enumerator
STASIS_APP_RECORDING_CANCEL 

Stop the recording, deleting the media file(s)

STASIS_APP_RECORDING_STOP 

Stop the recording.

STASIS_APP_RECORDING_PAUSE 

Pause the recording

STASIS_APP_RECORDING_UNPAUSE 

Unpause the recording

STASIS_APP_RECORDING_MUTE 

Mute the recording (record silence)

STASIS_APP_RECORDING_UNMUTE 

Unmute the recording

STASIS_APP_RECORDING_OPER_MAX 

Sentinel

Definition at line 155 of file stasis_app_recording.h.

155  {
156  /*! Stop the recording, deleting the media file(s) */
158  /*! Stop the recording. */
160  /*! Pause the recording */
162  /*! Unpause the recording */
164  /*! Mute the recording (record silence) */
166  /*! Unmute the recording */
168  /*! Sentinel */
170 };

Possible results from a recording operation.

Enumerator
STASIS_APP_RECORDING_OPER_OK 

Operation completed successfully.

STASIS_APP_RECORDING_OPER_FAILED 

Operation failed.

STASIS_APP_RECORDING_OPER_NOT_RECORDING 

Operation failed b/c recording is not in session.

Definition at line 292 of file stasis_app_recording.h.

292  {
293  /*! Operation completed successfully. */
295  /*! Operation failed. */
297  /*! Operation failed b/c recording is not in session. */
299 };

State of a recording operation

Enumerator
STASIS_APP_RECORDING_STATE_QUEUED 

The recording has not started yet

STASIS_APP_RECORDING_STATE_RECORDING 

The media is currently recording

STASIS_APP_RECORDING_STATE_PAUSED 

The media is currently paused

STASIS_APP_RECORDING_STATE_COMPLETE 

The media has stopped recording

STASIS_APP_RECORDING_STATE_FAILED 

The media has stopped recording, with error

STASIS_APP_RECORDING_STATE_CANCELED 

The media has stopped recording, discard the recording file

STASIS_APP_RECORDING_STATE_MAX 

Sentinel

Definition at line 137 of file stasis_app_recording.h.

137  {
138  /*! The recording has not started yet */
140  /*! The media is currently recording */
142  /*! The media is currently paused */
144  /*! The media has stopped recording */
146  /*! The media has stopped recording, with error */
148  /*! The media has stopped recording, discard the recording file */
150  /*! Sentinel */
152 };

Function Documentation

struct stasis_app_recording* stasis_app_control_record ( struct stasis_app_control control,
struct stasis_app_recording_options options 
)

Record media from a channel.

A reference to the options object may be kept, so it MUST NOT be modified after calling this function.

On error, errno is set to indicate the failure reason.

  • EINVAL: Invalid input.
  • EEXIST: A recording with that name is in session.
  • ENOMEM: Out of memory.
Parameters
controlControl for res_stasis.
optionsRecording options.
Returns
Recording control object.
Return values
NULLon error.

Definition at line 370 of file res_stasis_recording.c.

References stasis_app_recording::absolute_name, ao2_bump, ao2_link, ao2_ref, ast_asprintf, ast_debug, ast_fileexists(), AST_RECORD_IF_EXISTS_FAIL, ast_safe_mkdir(), stasis_app_recording::control, stasis_app_recording::duration, stasis_app_recording::energy_only, stasis_app_recording_options::format, stasis_app_recording_options::if_exists, lock, stasis_app_recording_options::max_duration_seconds, stasis_app_recording_options::max_silence_seconds, stasis_app_recording_options::name, OBJ_KEY, OBJ_NOLOCK, stasis_app_recording::options, RAII_VAR, SCOPED_AO2LOCK, stasis_app_control_get_channel_id(), stasis_app_control_register_add_rule(), STASIS_APP_RECORDING_STATE_QUEUED, stasis_app_send_command_async(), stasis_app_recording::state, and stasis_app_recording::total.

Referenced by ast_ari_bridges_record(), and ast_ari_channels_record().

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  /* errno set by ast_mkdir */
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;
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 
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 }
struct stasis_app_recording::@488 duration
enum stasis_app_recording_state state
#define OBJ_KEY
Definition: astobj2.h:1151
struct stasis_app_control * control
Assume that the ao2_container is already locked.
Definition: astobj2.h:1063
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:267
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
void stasis_app_control_register_add_rule(struct stasis_app_control *control, struct stasis_app_control_rule *rule)
Registers an add channel to bridge rule.
Definition: control.c:232
ast_mutex_t lock
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
int stasis_app_send_command_async(struct stasis_app_control *control, stasis_app_command_cb command, void *data, command_data_destructor_fn data_destructor)
Asynchronous version of stasis_app_send_command().
Definition: control.c:926
#define ast_debug(level,...)
Log a DEBUG message.
struct stasis_app_recording_options * options
const char * stasis_app_control_get_channel_id(const struct stasis_app_control *control)
Returns the uniqueid of the channel associated with this control.
Definition: control.c:1452
#define SCOPED_AO2LOCK(varname, obj)
scoped lock specialization for ao2 mutexes.
Definition: lock.h:604
int ast_safe_mkdir(const char *base_path, const char *path, int mode)
Recursively create directory path, but only if it resolves within the given base_path.
Definition: utils.c:2584
enum ast_record_if_exists if_exists
int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
Checks for the existence of a given file.
Definition: file.c:1129
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
static struct ao2_container * recordings
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
struct stasis_app_recording* stasis_app_recording_find_by_name ( const char *  name)

Finds the recording object with the given name.

Parameters
nameName of the recording object to find.
Returns
Associated stasis_app_recording object.
Return values
NULLif name not found.

Definition at line 473 of file res_stasis_recording.c.

References OBJ_KEY.

Referenced by ast_ari_recordings_get_live().

474 {
475  return ao2_find(recordings, name, OBJ_KEY);
476 }
#define OBJ_KEY
Definition: astobj2.h:1151
static struct ao2_container * recordings
const char* stasis_app_recording_get_name ( struct stasis_app_recording recording)

Gets the unique name of a recording object.

Parameters
recordingRecording control object.
Returns
recording's name.
Return values
NULLif recording ic NULL

Definition at line 467 of file res_stasis_recording.c.

References stasis_app_recording_options::name, and stasis_app_recording::options.

469 {
470  return recording->options->name;
471 }
struct stasis_app_recording_options * options
enum stasis_app_recording_state stasis_app_recording_get_state ( struct stasis_app_recording recording)

Gets the current state of a recording operation.

Parameters
recordingRecording control object.
Returns
The state of the recording object.

Definition at line 461 of file res_stasis_recording.c.

References stasis_app_recording::state.

463 {
464  return recording->state;
465 }
enum stasis_app_recording_state state
enum ast_record_if_exists stasis_app_recording_if_exists_parse ( const char *  str)

Parse a string into the if_exists enum.

Parameters
strString to parse.
Returns
How to handle an existing file.
-1 on error.

Definition at line 194 of file res_stasis_recording.c.

References AST_RECORD_IF_EXISTS_APPEND, AST_RECORD_IF_EXISTS_ERROR, AST_RECORD_IF_EXISTS_FAIL, and AST_RECORD_IF_EXISTS_OVERWRITE.

Referenced by ast_ari_bridges_record(), and ast_ari_channels_record().

196 {
197  if (ast_strlen_zero(str)) {
198  /* Default value */
200  }
201 
202  if (strcasecmp(str, "fail") == 0) {
204  }
205 
206  if (strcasecmp(str, "overwrite") == 0) {
208  }
209 
210  if (strcasecmp(str, "append") == 0) {
212  }
213 
215 }
enum stasis_app_recording_oper_results stasis_app_recording_operation ( struct stasis_app_recording recording,
enum stasis_app_recording_media_operation  operation 
)

Controls the media for a given recording operation.

Parameters
recordingRecording control object.
operationMedia control operation.
Return values
STASIS_APP_RECORDING_OPER_OKon success.
Returns
stasis_app_recording_oper_results indicating failure.

Definition at line 590 of file res_stasis_recording.c.

References lock, SCOPED_AO2LOCK, STASIS_APP_RECORDING_OPER_FAILED, STASIS_APP_RECORDING_OPER_MAX, STASIS_APP_RECORDING_OPER_NOT_RECORDING, STASIS_APP_RECORDING_OPER_OK, STASIS_APP_RECORDING_STATE_MAX, STASIS_APP_RECORDING_STATE_RECORDING, and stasis_app_recording::state.

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  /* So we can be specific in our error message. */
615  } else {
616  /* And, really, all operations should be valid during
617  * recording */
618  ast_log(LOG_ERROR,
619  "Unhandled operation during recording: %u\n",
620  operation);
622  }
623  }
624 
625  return cb(recording) ?
627 }
enum stasis_app_recording_state state
playback_operation_cb operations[STASIS_PLAYBACK_STATE_MAX][STASIS_PLAYBACK_MEDIA_OP_MAX]
A sparse array detailing how commands should be handled in the various playback states. Unset entries imply invalid operations.
ast_mutex_t lock
#define SCOPED_AO2LOCK(varname, obj)
scoped lock specialization for ao2 mutexes.
Definition: lock.h:604
struct stasis_app_recording_options* stasis_app_recording_options_create ( const char *  name,
const char *  format 
)

Allocate a recording options object.

Clean up with ao2_cleanup().

Parameters
nameName of the recording.
formatFormat to record in.
Returns
Newly allocated options object.
Return values
NULLon error.

Definition at line 151 of file res_stasis_recording.c.

References ao2_ref, ast_string_field_init, ast_string_field_set, and RAII_VAR.

Referenced by ast_ari_bridges_record(), and ast_ari_channels_record().

153 {
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 }
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:359
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
struct stasis_app_recording_options * options
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:521
char stasis_app_recording_termination_parse ( const char *  str)

Parse a string into the recording termination enum.

Parameters
strString to parse.
Returns
DTMF value to terminate on.
Return values
STASIS_APP_RECORDING_TERMINATE_NONEto not terminate on DTMF.
STASIS_APP_RECORDING_TERMINATE_ANYto terminate on any DTMF.
STASIS_APP_RECORDING_TERMINATE_INVALIDif input was invalid.

Definition at line 169 of file res_stasis_recording.c.

Referenced by ast_ari_bridges_record(), and ast_ari_channels_record().

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 }
struct ast_json* stasis_app_recording_to_json ( const struct stasis_app_recording recording)

Construct a JSON model of a recording.

Parameters
recordingRecording to conver.
Returns
JSON model.
Return values
NULLon error.

Definition at line 478 of file res_stasis_recording.c.

References ast_json_integer_create(), ast_json_object_set(), ast_json_pack(), ast_json_ref(), ast_json_unref(), stasis_app_recording::duration, stasis_app_recording::energy_only, stasis_app_recording_options::format, stasis_app_recording_options::name, stasis_app_recording::options, RAII_VAR, stasis_app_recording::state, stasis_app_recording_options::target, and stasis_app_recording::total.

Referenced by ast_ari_bridges_record(), ast_ari_channels_record(), and ast_ari_recordings_get_live().

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",
495  }
496  if (json && recording->duration.energy_only > -1) {
497  ast_json_object_set(json, "talking_duration",
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 }
struct stasis_app_recording::@488 duration
enum stasis_app_recording_state state
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
Definition: json.c:414
struct stasis_app_recording_options * options
Abstract JSON element (object, array, string, int, ...).
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
struct ast_json * ast_json_integer_create(intmax_t value)
Create a JSON integer.
Definition: json.c:327
int stasis_app_stored_recording_copy ( struct stasis_app_stored_recording src_recording,
const char *  dst,
struct stasis_app_stored_recording **  dst_recording 
)

Copy a recording.

Parameters
src_recordingThe recording to copy
dstThe destination of the recording to make
dst_recordingIf successful, the stored recording created as a result of the copy
Return values
0on success
Non-zeroon error

Definition at line 393 of file stored.c.

Referenced by ast_ari_recordings_copy_stored().

400 {
401  RAII_VAR(char *, full_path, NULL, ast_free);
402  char *dst_file = ast_strdupa(dst);
403  char *format;
404  char *last_slash;
405  int res;
406 
407  /* Drop the extension if specified, core will do this for us */
408  format = strrchr(dst_file, '.');
409  if (format) {
410  *format = '\0';
411  }
412 
413  /* See if any intermediary directories need to be made */
414  last_slash = strrchr(dst_file, '/');
415  if (last_slash) {
416  RAII_VAR(char *, tmp_path, NULL, ast_free);
417 
418  *last_slash = '\0';
419  if (ast_asprintf(&tmp_path, "%s/%s", ast_config_AST_RECORDING_DIR, dst_file) < 0) {
420  return -1;
421  }
422  if (ast_safe_mkdir(ast_config_AST_RECORDING_DIR,
423  tmp_path, 0777) != 0) {
424  /* errno set by ast_mkdir */
425  return -1;
426  }
427  *last_slash = '/';
428  if (ast_asprintf(&full_path, "%s/%s", ast_config_AST_RECORDING_DIR, dst_file) < 0) {
429  return -1;
430  }
431  } else {
432  /* There is no directory portion */
433  if (ast_asprintf(&full_path, "%s/%s", ast_config_AST_RECORDING_DIR, dst_file) < 0) {
434  return -1;
435  }
436  }
437 
438  ast_verb(4, "Copying recording %s to %s (format %s)\n", src_recording->file,
439  full_path, src_recording->format);
440  res = ast_filecopy(src_recording->file, full_path, src_recording->format);
441  if (!res) {
int ast_filecopy(const char *oldname, const char *newname, const char *fmt)
Copies a file.
Definition: file.c:1151
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:267
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
int ast_safe_mkdir(const char *base_path, const char *path, int mode)
Recursively create directory path, but only if it resolves within the given base_path.
Definition: utils.c:2584
const ast_string_field file
Definition: stored.c:41
const char * format
Definition: stored.c:41
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
int stasis_app_stored_recording_delete ( struct stasis_app_stored_recording recording)

Delete a recording from disk.

Parameters
recordingRecording to delete.
Returns
0 on success.
Non-zero on error.

Definition at line 443 of file stored.c.

Referenced by ast_ari_recordings_delete_stored().

450 {
struct ao2_container* stasis_app_stored_recording_find_all ( void  )

Find all stored recordings on disk.

Returns
Container of stasis_app_stored_recording objects.
Return values
NULLon error.

Definition at line 294 of file stored.c.

Referenced by ast_ari_recordings_list_stored().

298 {
299  struct ao2_container *recordings;
300  int res;
301 
304  if (!recordings) {
305  return NULL;
306  }
307 
308  res = ast_file_read_dirs(ast_config_AST_RECORDING_DIR,
309  handle_scan_file, recordings, -1);
310  if (res) {
311  ao2_ref(recordings, -1);
312  return NULL;
int ast_file_read_dirs(const char *dir_name, ast_file_on_file on_file, void *obj, int max_depth)
Recursively iterate through files and directories up to max_depth.
Definition: file.c:1274
static struct stasis_rest_handlers recordings
REST handler for /api-docs/recordings.json.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
Replace objects with duplicate keys in container.
Definition: astobj2.h:1211
static int recording_sort(const void *obj_left, const void *obj_right, int flags)
Definition: stored.c:223
#define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn)
Allocate and initialize a red-black tree container.
Definition: astobj2.h:1349
Generic container type.
struct stasis_app_stored_recording* stasis_app_stored_recording_find_by_name ( const char *  name)

Creates a stored recording object, with the given name.

Parameters
nameName of the recording.
Returns
New recording object.
Return values
NULLif recording is not found. errno is set to indicate why
  • ENOMEM - out of memeory
  • EACCES - file permissions (or recording is outside the config dir)
  • Any of the error codes for stat(), opendir(), readdir()

Definition at line 314 of file stored.c.

Referenced by ast_ari_recordings_copy_stored(), ast_ari_recordings_delete_stored(), ast_ari_recordings_get_stored(), and ast_ari_recordings_get_stored_file().

320 {
321  RAII_VAR(struct stasis_app_stored_recording *, recording, NULL,
322  ao2_cleanup);
323  RAII_VAR(char *, dir, NULL, ast_free);
324  RAII_VAR(char *, file, NULL, ast_free);
325  RAII_VAR(char *, file_with_ext, NULL, ast_free);
326  int res;
327  struct stat file_stat;
328  int prefix_len = strlen(ast_config_AST_RECORDING_DIR);
329 
330  errno = 0;
331 
332  if (!name) {
333  errno = EINVAL;
334  return NULL;
335  }
336 
337  recording = recording_alloc();
338  if (!recording) {
339  return NULL;
340  }
341 
342  res = split_path(name, &dir, &file);
343  if (res != 0) {
344  return NULL;
345  }
346  ast_string_field_build(recording, file, "%s/%s", dir, file);
347 
348  if (!ast_begins_with(dir, ast_config_AST_RECORDING_DIR)) {
349  /* It's possible that one or more component of the recording path is
350  * a symbolic link, this would prevent dir from ever matching. */
351  char *real_basedir = realpath(ast_config_AST_RECORDING_DIR, NULL);
352 
353  if (!real_basedir || !ast_begins_with(dir, real_basedir)) {
354  /* Attempt to escape the recording directory */
355  ast_log(LOG_WARNING, "Attempt to access invalid recording directory %s\n",
356  dir);
357  ast_std_free(real_basedir);
358  errno = EACCES;
359 
360  return NULL;
361  }
362 
363  prefix_len = strlen(real_basedir);
364  ast_std_free(real_basedir);
365  }
366 
367  /* The actual name of the recording is file with the config dir
368  * prefix removed.
369  */
370  ast_string_field_set(recording, name, recording->file + prefix_len + 1);
371 
372  file_with_ext = find_recording(dir, file);
373  if (!file_with_ext) {
374  return NULL;
375  }
376  ast_string_field_set(recording, file_with_ext, file_with_ext);
377  recording->format = strrchr(recording->file_with_ext, '.');
378  if (!recording->format) {
379  return NULL;
380  }
381  ++(recording->format);
382 
383  res = stat(file_with_ext, &file_stat);
384  if (res != 0) {
385  return NULL;
386  }
387 
388  if (!S_ISREG(file_stat.st_mode)) {
389  /* Let's not play if it's not a regular file */
390  errno = EACCES;
391  return NULL;
static char * find_recording(const char *dir_name, const char *file)
Finds a recording in the given directory.
Definition: stored.c:187
static struct stasis_app_stored_recording * recording_alloc(void)
Allocate a recording object.
Definition: stored.c:204
static int split_path(const char *path, char **dir, char **file)
Split a path into directory and file, resolving canonical directory.
Definition: stored.c:92
#define ast_string_field_build(x, field, fmt, args...)
Set a field to a complex (built) value.
Definition: stringfields.h:555
static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
Checks whether a string begins with another.
Definition: strings.h:97
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:521
const char* stasis_app_stored_recording_get_extension ( struct stasis_app_stored_recording recording)

Returns the extension for this recording.

Since
14.0.0
Parameters
recordingRecording to query.
Returns
The extension associated with this recording.
Return values
NULLon error

Definition at line 71 of file stored.c.

References stasis_app_stored_recording::format.

Referenced by ast_ari_recordings_get_stored_file().

73 {
74  if (!recording) {
75  return NULL;
76  }
77  return recording->format;
78 }
const char * format
Definition: stored.c:41
const char* stasis_app_stored_recording_get_file ( struct stasis_app_stored_recording recording)

Returns the filename for this recording, for use with streamfile.

The returned string will be valid until the recording object is freed.

Parameters
recordingRecording to query.
Returns
Absolute path to the recording file, without the extension.
Return values
NULLon error.

Definition at line 53 of file stored.c.

References stasis_app_stored_recording::file.

55 {
56  if (!recording) {
57  return NULL;
58  }
59  return recording->file;
60 }
const ast_string_field file
Definition: stored.c:41
const char* stasis_app_stored_recording_get_filename ( struct stasis_app_stored_recording recording)

Returns the full filename, with extension, for this recording.

Since
14.0.0
Parameters
recordingRecording to query.
Returns
Absolute path to the recording file, with the extension.
Return values
NULLon error

Definition at line 62 of file stored.c.

References stasis_app_stored_recording::file_with_ext.

Referenced by ast_ari_recordings_get_stored_file().

64 {
65  if (!recording) {
66  return NULL;
67  }
68  return recording->file_with_ext;
69 }
const ast_string_field file_with_ext
Definition: stored.c:41
struct ast_json* stasis_app_stored_recording_to_json ( struct stasis_app_stored_recording recording)

Convert stored recording info to JSON.

Parameters
recordingRecording to convert.
Returns
JSON representation.
Return values
NULLon error.

Definition at line 450 of file stored.c.

References stasis_app_stored_recording::file_with_ext.

Referenced by ast_ari_recordings_copy_stored(), ast_ari_recordings_get_stored(), and ast_ari_recordings_list_stored().

450 {
451  /* Path was validated when the recording object was created */
452  return unlink(recording->file_with_ext);
453 }
454 
456  struct stasis_app_stored_recording *recording)
457 {
458  if (!recording) {
459  return NULL;
460  }
const ast_string_field file_with_ext
Definition: stored.c:41
struct ast_json * stasis_app_stored_recording_to_json(struct stasis_app_stored_recording *recording)
Convert stored recording info to JSON.
Definition: stored.c:450
Abstract JSON element (object, array, string, int, ...).