Newer
Older
David Vossel
committed
removed = 1;
ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
Joshua Colp
committed
ast_audiohook_unlock(audiohook);
Richard Mudgett
committed
if (ast_channel_is_bridged(chan)) {
ast_channel_set_unbridged_nolock(chan, 1);
}
Joshua Colp
committed
continue;
}
audiohook_list_set_hook_rate(audiohook_list, audiohook, &internal_sample_rate);
if (ast_slinfactory_available(factory) >= samples && ast_slinfactory_read(factory, read_buf, samples)) {
Joshua Colp
committed
/* Take audio from this whisper source and combine it into our main buffer */
for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++) {
Joshua Colp
committed
ast_slinear_saturated_add(data1, data2);
Joshua Colp
committed
}
ast_audiohook_unlock(audiohook);
}
Tilghman Lesher
committed
AST_LIST_TRAVERSE_SAFE_END;
Joshua Colp
committed
/* We take all of the combined whisper sources and combine them into the audio being written out */
David Vossel
committed
for (i = 0, data1 = middle_frame->data.ptr, data2 = combine_buf; i < samples; i++, data1++, data2++) {
Joshua Colp
committed
ast_slinear_saturated_add(data1, data2);
David Vossel
committed
}
middle_frame_manipulated = 1;
Joshua Colp
committed
}
/* Pass off frame to manipulate audiohooks */
if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
ast_audiohook_lock(audiohook);
if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
AST_LIST_REMOVE_CURRENT(list);
David Vossel
committed
removed = 1;
ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
Joshua Colp
committed
ast_audiohook_unlock(audiohook);
/* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
audiohook->manipulate_callback(audiohook, chan, NULL, direction);
Richard Mudgett
committed
if (ast_channel_is_bridged(chan)) {
ast_channel_set_unbridged_nolock(chan, 1);
}
Joshua Colp
committed
continue;
}
audiohook_list_set_hook_rate(audiohook_list, audiohook, &internal_sample_rate);
/*
* Feed in frame to manipulation.
*/
if (!audiohook->manipulate_callback(audiohook, chan, middle_frame, direction)) {
/*
* XXX FAILURES ARE IGNORED XXX
* If the manipulation fails then the frame will be returned in its original state.
* Since there are potentially more manipulator callbacks in the list, no action should
* be taken here to exit early.
*/
middle_frame_manipulated = 1;
}
Joshua Colp
committed
ast_audiohook_unlock(audiohook);
}
Tilghman Lesher
committed
AST_LIST_TRAVERSE_SAFE_END;
Joshua Colp
committed
}
/* ---Part_3: Decide what to do with the end_frame (whether to transcode or not) */
David Vossel
committed
if (middle_frame_manipulated) {
if (!(end_frame = audiohook_list_translate_to_native(audiohook_list, direction, middle_frame, start_frame->subclass.format))) {
David Vossel
committed
/* translation failed, so just pass back the input frame */
end_frame = start_frame;
Joshua Colp
committed
}
} else {
David Vossel
committed
end_frame = start_frame;
}
/* clean up our middle_frame if required */
if (middle_frame != end_frame) {
David Vossel
committed
middle_frame = NULL;
}
/* Before returning, if an audiohook got removed, reset samplerate compatibility */
if (removed) {
audiohook_list_set_samplerate_compatibility(audiohook_list);
} else {
/*
* Set the audiohook_list's rate to the updated rate. Note that if a hook
* was removed then the list's internal rate is reset to the default.
*/
audiohook_list->list_internal_samp_rate = internal_sample_rate;
Joshua Colp
committed
}
return end_frame;
}
int ast_audiohook_write_list_empty(struct ast_audiohook_list *audiohook_list)
{
return !audiohook_list
|| (AST_LIST_EMPTY(&audiohook_list->spy_list)
&& AST_LIST_EMPTY(&audiohook_list->whisper_list)
&& AST_LIST_EMPTY(&audiohook_list->manipulate_list));
Joshua Colp
committed
/*! \brief Pass a frame off to be handled by the audiohook core
* \param chan Channel that the list is coming off of
* \param audiohook_list List of audiohooks
* \param direction Direction frame is coming in from
* \param frame The frame itself
* \return Return frame on success, NULL on failure
*/
struct ast_frame *ast_audiohook_write_list(struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame)
{
/* Pass off frame to it's respective list write function */
Joshua Colp
committed
return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
Joshua Colp
committed
return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
Joshua Colp
committed
return frame;
Joshua Colp
committed
}
/*! \brief Wait for audiohook trigger to be triggered
* \param audiohook Audiohook to wait on
*/
void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
{
struct timeval wait;
Joshua Colp
committed
struct timespec ts;
wait = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
ts.tv_sec = wait.tv_sec;
ts.tv_nsec = wait.tv_usec * 1000;
Joshua Colp
committed
ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
Joshua Colp
committed
return;
}
/* Count number of channel audiohooks by type, regardless of type */
int ast_channel_audiohook_count_by_source(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
{
int count = 0;
struct ast_audiohook *ah = NULL;
return -1;
switch (type) {
case AST_AUDIOHOOK_TYPE_SPY:
AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, ah, list) {
if (!strcmp(ah->source, source)) {
count++;
}
}
break;
case AST_AUDIOHOOK_TYPE_WHISPER:
AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->whisper_list, ah, list) {
if (!strcmp(ah->source, source)) {
count++;
}
}
break;
case AST_AUDIOHOOK_TYPE_MANIPULATE:
AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->manipulate_list, ah, list) {
if (!strcmp(ah->source, source)) {
count++;
}
}
break;
default:
ast_debug(1, "Invalid audiohook type supplied, (%u)\n", type);
return -1;
}
return count;
}
/* Count number of channel audiohooks by type that are running */
int ast_channel_audiohook_count_by_source_running(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
{
int count = 0;
struct ast_audiohook *ah = NULL;
if (!ast_channel_audiohooks(chan))
return -1;
switch (type) {
case AST_AUDIOHOOK_TYPE_SPY:
AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, ah, list) {
if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
count++;
}
break;
case AST_AUDIOHOOK_TYPE_WHISPER:
AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->whisper_list, ah, list) {
if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
count++;
}
break;
case AST_AUDIOHOOK_TYPE_MANIPULATE:
AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->manipulate_list, ah, list) {
if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
count++;
}
break;
default:
ast_debug(1, "Invalid audiohook type supplied, (%u)\n", type);
return -1;
}
return count;
}
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
/*! \brief Audiohook volume adjustment structure */
struct audiohook_volume {
struct ast_audiohook audiohook; /*!< Audiohook attached to the channel */
int read_adjustment; /*!< Value to adjust frames read from the channel by */
int write_adjustment; /*!< Value to adjust frames written to the channel by */
};
/*! \brief Callback used to destroy the audiohook volume datastore
* \param data Volume information structure
* \return Returns nothing
*/
static void audiohook_volume_destroy(void *data)
{
struct audiohook_volume *audiohook_volume = data;
/* Destroy the audiohook as it is no longer in use */
ast_audiohook_destroy(&audiohook_volume->audiohook);
/* Finally free ourselves, we are of no more use */
ast_free(audiohook_volume);
return;
}
/*! \brief Datastore used to store audiohook volume information */
static const struct ast_datastore_info audiohook_volume_datastore = {
.type = "Volume",
.destroy = audiohook_volume_destroy,
};
/*! \brief Helper function which actually gets called by audiohooks to perform the adjustment
* \param audiohook Audiohook attached to the channel
* \param chan Channel we are attached to
* \param frame Frame of audio we want to manipulate
* \param direction Direction the audio came in from
* \return Returns 0 on success, -1 on failure
*/
static int audiohook_volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
{
struct ast_datastore *datastore = NULL;
struct audiohook_volume *audiohook_volume = NULL;
int *gain = NULL;
/* If the audiohook is shutting down don't even bother */
if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
return 0;
}
/* Try to find the datastore containg adjustment information, if we can't just bail out */
if (!(datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
return 0;
}
audiohook_volume = datastore->data;
/* Based on direction grab the appropriate adjustment value */
if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
gain = &audiohook_volume->read_adjustment;
} else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
gain = &audiohook_volume->write_adjustment;
}
/* If an adjustment value is present modify the frame */
if (gain && *gain) {
ast_frame_adjust_volume(frame, *gain);
}
return 0;
}
/*! \brief Helper function which finds and optionally creates an audiohook_volume_datastore datastore on a channel
* \param chan Channel to look on
* \param create Whether to create the datastore if not found
* \return Returns audiohook_volume structure on success, NULL on failure
*/
static struct audiohook_volume *audiohook_volume_get(struct ast_channel *chan, int create)
{
struct ast_datastore *datastore = NULL;
struct audiohook_volume *audiohook_volume = NULL;
/* If we are able to find the datastore return the contents (which is actually an audiohook_volume structure) */
if ((datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
return datastore->data;
}
/* If we are not allowed to create a datastore or if we fail to create a datastore, bail out now as we have nothing for them */
Kevin P. Fleming
committed
if (!create || !(datastore = ast_datastore_alloc(&audiohook_volume_datastore, NULL))) {
return NULL;
}
/* Create a new audiohook_volume structure to contain our adjustments and audiohook */
if (!(audiohook_volume = ast_calloc(1, sizeof(*audiohook_volume)))) {
Kevin P. Fleming
committed
ast_datastore_free(datastore);
return NULL;
}
/* Setup our audiohook structure so we can manipulate the audio */
David Vossel
committed
ast_audiohook_init(&audiohook_volume->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
audiohook_volume->audiohook.manipulate_callback = audiohook_volume_callback;
/* Attach the audiohook_volume blob to the datastore and attach to the channel */
datastore->data = audiohook_volume;
ast_channel_datastore_add(chan, datastore);
/* All is well... put the audiohook into motion */
ast_audiohook_attach(chan, &audiohook_volume->audiohook);
return audiohook_volume;
}
/*! \brief Adjust the volume on frames read from or written to a channel
* \param chan Channel to muck with
* \param direction Direction to set on
* \param volume Value to adjust the volume by
* \return Returns 0 on success, -1 on failure
*/
int ast_audiohook_volume_set(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
{
struct audiohook_volume *audiohook_volume = NULL;
/* Attempt to find the audiohook volume information, but only create it if we are not setting the adjustment value to zero */
if (!(audiohook_volume = audiohook_volume_get(chan, (volume ? 1 : 0)))) {
return -1;
}
/* Now based on the direction set the proper value */
if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
audiohook_volume->read_adjustment = volume;
}
if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
audiohook_volume->write_adjustment = volume;
}
return 0;
}
/*! \brief Retrieve the volume adjustment value on frames read from or written to a channel
* \param chan Channel to retrieve volume adjustment from
* \param direction Direction to retrieve
* \return Returns adjustment value
*/
int ast_audiohook_volume_get(struct ast_channel *chan, enum ast_audiohook_direction direction)
{
struct audiohook_volume *audiohook_volume = NULL;
int adjustment = 0;
/* Attempt to find the audiohook volume information, but do not create it as we only want to look at the values */
if (!(audiohook_volume = audiohook_volume_get(chan, 0))) {
return 0;
}
/* Grab the adjustment value based on direction given */
if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
adjustment = audiohook_volume->read_adjustment;
} else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
adjustment = audiohook_volume->write_adjustment;
}
return adjustment;
}
/*! \brief Adjust the volume on frames read from or written to a channel
* \param chan Channel to muck with
* \param direction Direction to increase
* \param volume Value to adjust the adjustment by
* \return Returns 0 on success, -1 on failure
*/
int ast_audiohook_volume_adjust(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
{
struct audiohook_volume *audiohook_volume = NULL;
/* Attempt to find the audiohook volume information, and create an audiohook if none exists */
if (!(audiohook_volume = audiohook_volume_get(chan, 1))) {
return -1;
}
/* Based on the direction change the specific adjustment value */
if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
audiohook_volume->read_adjustment += volume;
}
if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
audiohook_volume->write_adjustment += volume;
}
return 0;
}
/*! \brief Mute frames read from or written to a channel
* \param chan Channel to muck with
* \param source Type of audiohook
* \param flag which flag to set / clear
* \param clear set or clear
* \return Returns 0 on success, -1 on failure
*/
int ast_audiohook_set_mute(struct ast_channel *chan, const char *source, enum ast_audiohook_flags flag, int clear)
{
struct ast_audiohook *audiohook = NULL;
ast_channel_lock(chan);
/* Ensure the channel has audiohooks on it */
if (!ast_channel_audiohooks(chan)) {
ast_channel_unlock(chan);
return -1;
}
audiohook = find_audiohook_by_source(ast_channel_audiohooks(chan), source);