Wed Jan 8 2020 09:50:13

Asterisk developer's documentation


func_volume.c File Reference

Technology independent volume control. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/audiohook.h"
#include "asterisk/app.h"

Go to the source code of this file.

Data Structures

struct  volume_information
 

Enumerations

enum  volume_flags { VOLUMEFLAG_CHANGE = (1 << 1) }
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static void destroy_callback (void *data)
 
static int load_module (void)
 
static int unload_module (void)
 
static int volume_callback (struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
 
static int volume_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Technology independent volume control" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
 
static struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_datastore_info volume_datastore
 Static structure for datastore information. More...
 
static struct ast_custom_function volume_function
 
static struct ast_app_option volume_opts [128] = { [ 'p' ] = { .flag = VOLUMEFLAG_CHANGE }, }
 

Detailed Description

Technology independent volume control.

Author
Joshua Colp jcolp.nosp@m.@dig.nosp@m.ium.c.nosp@m.om

Definition in file func_volume.c.

Enumeration Type Documentation

Enumerator
VOLUMEFLAG_CHANGE 

Definition at line 80 of file func_volume.c.

80  {
81  VOLUMEFLAG_CHANGE = (1 << 1),
82 };

Function Documentation

static void __reg_module ( void  )
static

Definition at line 241 of file func_volume.c.

static void __unreg_module ( void  )
static

Definition at line 241 of file func_volume.c.

static void destroy_callback ( void *  data)
static

Definition at line 88 of file func_volume.c.

References ast_audiohook_destroy(), ast_audiohook_detach(), ast_audiohook_lock, ast_audiohook_unlock, ast_free, and volume_information::audiohook.

89 {
90  struct volume_information *vi = data;
91 
92  /* Destroy the audiohook, and destroy ourselves */
97  ast_free(vi);
98 
99  return;
100 }
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
Definition: audiohook.c:96
#define ast_audiohook_unlock(ah)
Unlock an audiohook.
Definition: audiohook.h:272
int ast_audiohook_detach(struct ast_audiohook *audiohook)
Detach audiohook from channel.
Definition: audiohook.c:401
#define ast_free(a)
Definition: astmm.h:97
struct ast_audiohook audiohook
Definition: func_volume.c:74
#define ast_audiohook_lock(ah)
Lock an audiohook.
Definition: audiohook.h:267
static int load_module ( void  )
static

Definition at line 236 of file func_volume.c.

References ast_custom_function_register.

237 {
239 }
static struct ast_custom_function volume_function
Definition: func_volume.c:226
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
static int unload_module ( void  )
static

Definition at line 231 of file func_volume.c.

References ast_custom_function_unregister().

232 {
234 }
static struct ast_custom_function volume_function
Definition: func_volume.c:226
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
static int volume_callback ( struct ast_audiohook audiohook,
struct ast_channel chan,
struct ast_frame frame,
enum ast_audiohook_direction  direction 
)
static

Definition at line 108 of file func_volume.c.

References AST_AUDIOHOOK_DIRECTION_READ, AST_AUDIOHOOK_STATUS_DONE, ast_channel_datastore_find(), ast_frame_adjust_volume(), AST_FRAME_DTMF, AST_FRAME_VOICE, ast_test_flag, ast_datastore::data, ast_frame::frametype, ast_frame_subclass::integer, volume_information::rx_gain, ast_audiohook::status, ast_frame::subclass, volume_information::tx_gain, and VOLUMEFLAG_CHANGE.

Referenced by volume_write().

109 {
110  struct ast_datastore *datastore = NULL;
111  struct volume_information *vi = NULL;
112  int *gain = NULL;
113 
114  /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
115  if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
116  return 0;
117 
118  /* Grab datastore which contains our gain information */
119  if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL)))
120  return 0;
121 
122  vi = datastore->data;
123 
124  /* If this is DTMF then allow them to increase/decrease the gains */
125  if (ast_test_flag(vi, VOLUMEFLAG_CHANGE)) {
126  if (frame->frametype == AST_FRAME_DTMF) {
127  /* Only use DTMF coming from the source... not going to it */
128  if (direction != AST_AUDIOHOOK_DIRECTION_READ)
129  return 0;
130  if (frame->subclass.integer == '*') {
131  vi->tx_gain += 1;
132  vi->rx_gain += 1;
133  } else if (frame->subclass.integer == '#') {
134  vi->tx_gain -= 1;
135  vi->rx_gain -= 1;
136  }
137  }
138  }
139 
140 
141  if (frame->frametype == AST_FRAME_VOICE) {
142  /* Based on direction of frame grab the gain, and confirm it is applicable */
143  if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain)
144  return 0;
145  /* Apply gain to frame... easy as pi */
146  ast_frame_adjust_volume(frame, *gain);
147  }
148 
149  return 0;
150 }
union ast_frame_subclass subclass
Definition: frame.h:146
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define AST_FRAME_DTMF
Definition: frame.h:128
Structure for a data store object.
Definition: datastore.h:54
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2604
static struct ast_datastore_info volume_datastore
Static structure for datastore information.
Definition: func_volume.c:103
void * data
Definition: datastore.h:56
int ast_frame_adjust_volume(struct ast_frame *f, int adjustment)
Adjusts the volume of the audio samples contained in a frame.
Definition: frame.c:1584
enum ast_audiohook_status status
Definition: audiohook.h:107
enum ast_frame_type frametype
Definition: frame.h:144
static int volume_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Definition at line 152 of file func_volume.c.

References args, AST_APP_ARG, ast_app_parse_options(), ast_audiohook_attach(), ast_audiohook_init(), AST_AUDIOHOOK_TYPE_MANIPULATE, AST_AUDIOHOOK_WANTS_DTMF, ast_calloc, ast_channel_datastore_add(), ast_channel_datastore_find(), ast_channel_lock, ast_channel_unlock, ast_datastore_alloc(), ast_datastore_free(), AST_DECLARE_APP_ARGS, ast_log(), ast_set_flag, AST_STANDARD_APP_ARGS, ast_strlen_zero(), volume_information::audiohook, ast_datastore::data, volume_information::flags, ast_flags::flags, LOG_ERROR, LOG_WARNING, ast_audiohook::manipulate_callback, volume_information::rx_gain, volume_information::tx_gain, volume_callback(), and volume_opts.

153 {
154  struct ast_datastore *datastore = NULL;
155  struct volume_information *vi = NULL;
156  int is_new = 0;
157 
158  /* Separate options from argument */
159 
161  AST_APP_ARG(direction);
162  AST_APP_ARG(options);
163  );
164 
165  if (!chan) {
166  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
167  return -1;
168  }
169 
171 
172  ast_channel_lock(chan);
173  if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL))) {
174  ast_channel_unlock(chan);
175  /* Allocate a new datastore to hold the reference to this volume and audiohook information */
176  if (!(datastore = ast_datastore_alloc(&volume_datastore, NULL)))
177  return 0;
178  if (!(vi = ast_calloc(1, sizeof(*vi)))) {
179  ast_datastore_free(datastore);
180  return 0;
181  }
185  is_new = 1;
186  } else {
187  ast_channel_unlock(chan);
188  vi = datastore->data;
189  }
190 
191  /* Adjust gain on volume information structure */
192  if (ast_strlen_zero(args.direction)) {
193  ast_log(LOG_ERROR, "Direction must be specified for VOLUME function\n");
194  return -1;
195  }
196 
197  if (!strcasecmp(args.direction, "tx")) {
198  vi->tx_gain = atoi(value);
199  } else if (!strcasecmp(args.direction, "rx")) {
200  vi->rx_gain = atoi(value);
201  } else {
202  ast_log(LOG_ERROR, "Direction must be either RX or TX\n");
203  }
204 
205  if (is_new) {
206  datastore->data = vi;
207  ast_channel_lock(chan);
208  ast_channel_datastore_add(chan, datastore);
209  ast_channel_unlock(chan);
210  ast_audiohook_attach(chan, &vi->audiohook);
211  }
212 
213  /* Add Option data to struct */
214 
215  if (!ast_strlen_zero(args.options)) {
216  struct ast_flags flags = { 0 };
217  ast_app_parse_options(volume_opts, &flags, NULL, args.options);
218  vi->flags = flags.flags;
219  } else {
220  vi->flags = 0;
221  }
222 
223  return 0;
224 }
#define ast_channel_lock(chan)
Definition: channel.h:2466
#define ast_set_flag(p, flag)
Definition: utils.h:70
int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source)
Initialize an audiohook structure.
Definition: audiohook.c:64
#define LOG_WARNING
Definition: logger.h:144
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: app.c:2101
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
unsigned int flags
Definition: utils.h:201
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
Definition: audiohook.c:348
unsigned int flags
Definition: func_volume.c:77
Structure for a data store object.
Definition: datastore.h:54
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2604
int value
Definition: syslog.c:39
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:65
ast_audiohook_manipulate_callback manipulate_callback
Definition: audiohook.h:116
static struct ast_datastore_info volume_datastore
Static structure for datastore information.
Definition: func_volume.c:103
static struct ast_app_option volume_opts[128]
Definition: func_volume.c:86
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define LOG_ERROR
Definition: logger.h:155
static struct @350 args
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
struct ast_datastore * ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
Definition: datastore.c:98
#define ast_channel_unlock(chan)
Definition: channel.h:2467
Structure used to handle boolean flags.
Definition: utils.h:200
void * data
Definition: datastore.h:56
struct ast_audiohook audiohook
Definition: func_volume.c:74
#define ast_calloc(a, b)
Definition: astmm.h:82
static int volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
Definition: func_volume.c:108
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2590

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Technology independent volume control" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static

Definition at line 241 of file func_volume.c.

Definition at line 241 of file func_volume.c.

struct ast_datastore_info volume_datastore
static
Initial value:
= {
.type = "volume",
.destroy = destroy_callback
}
static void destroy_callback(void *data)
Definition: func_volume.c:88

Static structure for datastore information.

Definition at line 103 of file func_volume.c.

struct ast_custom_function volume_function
static
Initial value:
= {
.name = "VOLUME",
.write = volume_write,
}
static int volume_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Definition: func_volume.c:152

Definition at line 226 of file func_volume.c.

struct ast_app_option volume_opts[128] = { [ 'p' ] = { .flag = VOLUMEFLAG_CHANGE }, }
static

Definition at line 86 of file func_volume.c.

Referenced by volume_write().