Merge audiohooks branch into trunk. This is a new API for developers to listen and manipulate the audio going through a channel.

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@78649 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Joshua Colp
2007-08-08 19:30:52 +00:00
parent 082322685f
commit 602198c402
11 changed files with 1113 additions and 913 deletions

View File

@@ -26,7 +26,7 @@ OBJS= io.o sched.o logger.o frame.o loader.o config.o channel.o \
utils.o plc.o jitterbuf.o dnsmgr.o devicestate.o \
netsock.o slinfactory.o ast_expr2.o ast_expr2f.o \
cryptostub.o sha1.o http.o fixedjitterbuf.o abstract_jb.o \
strcompat.o threadstorage.o dial.o event.o adsistub.o
strcompat.o threadstorage.o dial.o event.o adsistub.o audiohook.o
# we need to link in the objects statically, not as a library, because
# otherwise modules will not have them available if none of the static

625
main/audiohook.c Normal file
View File

@@ -0,0 +1,625 @@
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2007, Digium, Inc.
*
* Joshua Colp <jcolp@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Audiohooks Architecture
*
* \author Joshua Colp <jcolp@digium.com>
*/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/options.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/linkedlists.h"
#include "asterisk/audiohook.h"
#include "asterisk/slinfactory.h"
#include "asterisk/frame.h"
#include "asterisk/translate.h"
struct ast_audiohook_translate {
struct ast_trans_pvt *trans_pvt;
int format;
};
struct ast_audiohook_list {
struct ast_audiohook_translate in_translate[2];
struct ast_audiohook_translate out_translate[2];
AST_LIST_HEAD_NOLOCK(, ast_audiohook) spy_list;
AST_LIST_HEAD_NOLOCK(, ast_audiohook) whisper_list;
AST_LIST_HEAD_NOLOCK(, ast_audiohook) manipulate_list;
};
/*! \brief Initialize an audiohook structure
* \param audiohook Audiohook structure
* \return Returns 0 on success, -1 on failure
*/
int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source)
{
/* Need to keep the type and source */
audiohook->type = type;
audiohook->source = source;
/* Initialize lock that protects our audiohook */
ast_mutex_init(&audiohook->lock);
ast_cond_init(&audiohook->trigger, NULL);
/* Setup the factories that are needed for this audiohook type */
switch (type) {
case AST_AUDIOHOOK_TYPE_SPY:
ast_slinfactory_init(&audiohook->read_factory);
case AST_AUDIOHOOK_TYPE_WHISPER:
ast_slinfactory_init(&audiohook->write_factory);
break;
default:
break;
}
/* Since we are just starting out... this audiohook is new */
audiohook->status = AST_AUDIOHOOK_STATUS_NEW;
return 0;
}
/*! \brief Destroys an audiohook structure
* \param audiohook Audiohook structure
* \return Returns 0 on success, -1 on failure
*/
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
{
/* Drop the factories used by this audiohook type */
switch (audiohook->type) {
case AST_AUDIOHOOK_TYPE_SPY:
ast_slinfactory_destroy(&audiohook->read_factory);
case AST_AUDIOHOOK_TYPE_WHISPER:
ast_slinfactory_destroy(&audiohook->write_factory);
break;
default:
break;
}
/* Destroy translation path if present */
if (audiohook->trans_pvt)
ast_translator_free_path(audiohook->trans_pvt);
/* Lock and trigger be gone! */
ast_cond_destroy(&audiohook->trigger);
ast_mutex_destroy(&audiohook->lock);
return 0;
}
/*! \brief Writes a frame into the audiohook structure
* \param audiohook Audiohook structure
* \param direction Direction the audio frame came from
* \param frame Frame to write in
* \return Returns 0 on success, -1 on failure
*/
int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame)
{
struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
/* Write frame out to respective factory */
ast_slinfactory_feed(factory, frame);
/* If we need to notify the respective handler of this audiohook, do so */
switch (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE)) {
case AST_AUDIOHOOK_TRIGGER_READ:
if (direction == AST_AUDIOHOOK_DIRECTION_READ)
ast_cond_signal(&audiohook->trigger);
break;
case AST_AUDIOHOOK_TRIGGER_WRITE:
if (direction == AST_AUDIOHOOK_DIRECTION_WRITE)
ast_cond_signal(&audiohook->trigger);
break;
default:
break;
}
return 0;
}
static struct ast_frame *audiohook_read_frame_single(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction)
{
struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
int vol = (direction == AST_AUDIOHOOK_DIRECTION_READ ? audiohook->options.read_volume : audiohook->options.write_volume);
short buf[samples];
struct ast_frame frame = {
.frametype = AST_FRAME_VOICE,
.subclass = AST_FORMAT_SLINEAR,
.data = buf,
.datalen = sizeof(buf),
.samples = samples,
};
/* Ensure the factory is able to give us the samples we want */
if (samples > ast_slinfactory_available(factory))
return NULL;
/* Read data in from factory */
if (!ast_slinfactory_read(factory, buf, samples))
return NULL;
/* If a volume adjustment needs to be applied apply it */
if (vol)
ast_frame_adjust_volume(&frame, vol);
return ast_frdup(&frame);
}
static struct ast_frame *audiohook_read_frame_both(struct ast_audiohook *audiohook, size_t samples)
{
int i = 0;
short buf1[samples], buf2[samples], *read_buf = NULL, *write_buf = NULL, *final_buf = NULL, *data1 = NULL, *data2 = NULL;
struct ast_frame frame = {
.frametype = AST_FRAME_VOICE,
.subclass = AST_FORMAT_SLINEAR,
.data = NULL,
.datalen = sizeof(buf1),
.samples = samples,
};
/* Start with the read factory... if there are enough samples, read them in */
if (ast_slinfactory_available(&audiohook->read_factory) >= samples) {
if (ast_slinfactory_read(&audiohook->read_factory, buf1, samples))
read_buf = buf1;
/* Adjust read volume if need be */
if (audiohook->options.read_volume) {
int count = 0;
short adjust_value = abs(audiohook->options.read_volume);
for (count = 0; count < samples; count++) {
if (audiohook->options.read_volume > 0)
ast_slinear_saturated_multiply(&buf1[count], &adjust_value);
else if (audiohook->options.read_volume < 0)
ast_slinear_saturated_divide(&buf1[count], &adjust_value);
}
}
} else if (option_debug)
ast_log(LOG_DEBUG, "Failed to get %zd samples from read factory %p\n", samples, &audiohook->read_factory);
/* Move on to the write factory... if there are enough samples, read them in */
if (ast_slinfactory_available(&audiohook->write_factory) >= samples) {
if (ast_slinfactory_read(&audiohook->write_factory, buf2, samples))
write_buf = buf2;
/* Adjust write volume if need be */
if (audiohook->options.write_volume) {
int count = 0;
short adjust_value = abs(audiohook->options.write_volume);
for (count = 0; count < samples; count++) {
if (audiohook->options.write_volume > 0)
ast_slinear_saturated_multiply(&buf2[count], &adjust_value);
else if (audiohook->options.write_volume < 0)
ast_slinear_saturated_divide(&buf2[count], &adjust_value);
}
}
} else if (option_debug)
ast_log(LOG_DEBUG, "Failed to get %zd samples from write factory %p\n", samples, &audiohook->write_factory);
/* Basically we figure out which buffer to use... and if mixing can be done here */
if (!read_buf && !write_buf)
return NULL;
else if (read_buf && write_buf) {
for (i = 0, data1 = read_buf, data2 = write_buf; i < samples; i++, data1++, data2++)
ast_slinear_saturated_add(data1, data2);
final_buf = buf1;
} else if (read_buf)
final_buf = buf1;
else if (write_buf)
final_buf = buf2;
/* Make the final buffer part of the frame, so it gets duplicated fine */
frame.data = final_buf;
/* Yahoo, a combined copy of the audio! */
return ast_frdup(&frame);
}
/*! \brief Reads a frame in from the audiohook structure
* \param audiohook Audiohook structure
* \param samples Number of samples wanted
* \param direction Direction the audio frame came from
* \param format Format of frame remote side wants back
* \return Returns frame on success, NULL on failure
*/
struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, int format)
{
struct ast_frame *read_frame = NULL, *final_frame = NULL;
if (!(read_frame = (direction == AST_AUDIOHOOK_DIRECTION_BOTH ? audiohook_read_frame_both(audiohook, samples) : audiohook_read_frame_single(audiohook, samples, direction))))
return NULL;
/* If they don't want signed linear back out, we'll have to send it through the translation path */
if (format != AST_FORMAT_SLINEAR) {
/* Rebuild translation path if different format then previously */
if (audiohook->format != format) {
if (audiohook->trans_pvt) {
ast_translator_free_path(audiohook->trans_pvt);
audiohook->trans_pvt = NULL;
}
/* Setup new translation path for this format... if we fail we can't very well return signed linear so free the frame and return nothing */
if (!(audiohook->trans_pvt = ast_translator_build_path(format, AST_FORMAT_SLINEAR))) {
ast_frfree(read_frame);
return NULL;
}
}
/* Convert to requested format, and allow the read in frame to be freed */
final_frame = ast_translate(audiohook->trans_pvt, read_frame, 1);
} else {
final_frame = read_frame;
}
return final_frame;
}
/*! \brief Attach audiohook to channel
* \param chan Channel
* \param audiohook Audiohook structure
* \return Returns 0 on success, -1 on failure
*/
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
{
ast_channel_lock(chan);
if (!chan->audiohooks) {
/* Whoops... allocate a new structure */
if (!(chan->audiohooks = ast_calloc(1, sizeof(*chan->audiohooks)))) {
ast_channel_unlock(chan);
return -1;
}
AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->spy_list);
AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->whisper_list);
AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->manipulate_list);
}
/* Drop into respective list */
if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY)
AST_LIST_INSERT_TAIL(&chan->audiohooks->spy_list, audiohook, list);
else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER)
AST_LIST_INSERT_TAIL(&chan->audiohooks->whisper_list, audiohook, list);
else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE)
AST_LIST_INSERT_TAIL(&chan->audiohooks->manipulate_list, audiohook, list);
/* Change status over to running since it is now attached */
audiohook->status = AST_AUDIOHOOK_STATUS_RUNNING;
ast_channel_unlock(chan);
return 0;
}
/*! \brief Detach audiohook from channel
* \param audiohook Audiohook structure
* \return Returns 0 on success, -1 on failure
*/
int ast_audiohook_detach(struct ast_audiohook *audiohook)
{
if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
return 0;
audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
ast_audiohook_trigger_wait(audiohook);
return 0;
}
/*! \brief Detach audiohooks from list and destroy said list
* \param audiohook_list List of audiohooks
* \return Returns 0 on success, -1 on failure
*/
int ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
{
int i = 0;
struct ast_audiohook *audiohook = NULL;
/* Drop any spies */
AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
ast_audiohook_lock(audiohook);
AST_LIST_REMOVE_CURRENT(&audiohook_list->spy_list, list);
audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
ast_cond_signal(&audiohook->trigger);
ast_audiohook_unlock(audiohook);
}
AST_LIST_TRAVERSE_SAFE_END
/* Drop any whispering sources */
AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
ast_audiohook_lock(audiohook);
AST_LIST_REMOVE_CURRENT(&audiohook_list->whisper_list, list);
audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
ast_cond_signal(&audiohook->trigger);
ast_audiohook_unlock(audiohook);
}
AST_LIST_TRAVERSE_SAFE_END
/* Drop any manipulaters */
AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
ast_audiohook_lock(audiohook);
ast_mutex_lock(&audiohook->lock);
AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
ast_audiohook_unlock(audiohook);
audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
}
AST_LIST_TRAVERSE_SAFE_END
/* Drop translation paths if present */
for (i = 0; i < 2; i++) {
if (audiohook_list->in_translate[i].trans_pvt)
ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
if (audiohook_list->out_translate[i].trans_pvt)
ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt);
}
/* Free ourselves */
ast_free(audiohook_list);
return 0;
}
static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
{
struct ast_audiohook *audiohook = NULL;
AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
if (!strcasecmp(audiohook->source, source))
return audiohook;
}
AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
if (!strcasecmp(audiohook->source, source))
return audiohook;
}
AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
if (!strcasecmp(audiohook->source, source))
return audiohook;
}
return NULL;
}
/*! \brief Detach specified source audiohook from channel
* \param chan Channel to detach from
* \param source Name of source to detach
* \return Returns 0 on success, -1 on failure
*/
int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
{
struct ast_audiohook *audiohook = NULL;
ast_channel_lock(chan);
/* Ensure the channel has audiohooks on it */
if (!chan->audiohooks) {
ast_channel_unlock(chan);
return -1;
}
audiohook = find_audiohook_by_source(chan->audiohooks, source);
ast_channel_unlock(chan);
if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
return (audiohook ? 0 : -1);
}
/*! \brief Pass a DTMF 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
*/
static struct ast_frame *dtmf_audiohook_write_list(struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame)
{
struct ast_audiohook *audiohook = NULL;
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(&audiohook_list->manipulate_list, list);
audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
ast_audiohook_unlock(audiohook);
audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
continue;
}
if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF))
audiohook->manipulate_callback(audiohook, chan, frame, direction);
ast_audiohook_unlock(audiohook);
}
AST_LIST_TRAVERSE_SAFE_END
return frame;
}
/*! \brief Pass an AUDIO 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
*/
static struct ast_frame *audio_audiohook_write_list(struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame)
{
struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
struct ast_audiohook *audiohook = NULL;
int samples = frame->samples;
/* If the frame coming in is not signed linear we have to send it through the in_translate path */
if (frame->subclass != AST_FORMAT_SLINEAR) {
if (in_translate->format != frame->subclass) {
if (in_translate->trans_pvt)
ast_translator_free_path(in_translate->trans_pvt);
if (!(in_translate->trans_pvt = ast_translator_build_path(AST_FORMAT_SLINEAR, frame->subclass)))
return frame;
in_translate->format = frame->subclass;
}
if (!(middle_frame = ast_translate(in_translate->trans_pvt, frame, 0)))
return frame;
}
/* Queue up signed linear frame to each spy */
AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
ast_audiohook_lock(audiohook);
if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
AST_LIST_REMOVE_CURRENT(&audiohook_list->spy_list, list);
audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
ast_cond_signal(&audiohook->trigger);
ast_audiohook_unlock(audiohook);
continue;
}
ast_audiohook_write_frame(audiohook, direction, middle_frame);
ast_audiohook_unlock(audiohook);
}
AST_LIST_TRAVERSE_SAFE_END
/* If this frame is being written out to the channel then we need to use whisper sources */
if (direction == AST_AUDIOHOOK_DIRECTION_WRITE && !AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
int i = 0;
short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
memset(&combine_buf, 0, sizeof(combine_buf));
AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
ast_audiohook_lock(audiohook);
if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
AST_LIST_REMOVE_CURRENT(&audiohook_list->whisper_list, list);
audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
ast_cond_signal(&audiohook->trigger);
ast_audiohook_unlock(audiohook);
continue;
}
if (ast_slinfactory_available(&audiohook->write_factory) >= samples && ast_slinfactory_read(&audiohook->write_factory, read_buf, samples)) {
/* 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++)
ast_slinear_saturated_add(data1, data2);
}
ast_audiohook_unlock(audiohook);
}
AST_LIST_TRAVERSE_SAFE_END
/* We take all of the combined whisper sources and combine them into the audio being written out */
for (i = 0, data1 = middle_frame->data, data2 = combine_buf; i < samples; i++, data1++, data2++)
ast_slinear_saturated_add(data1, data2);
end_frame = middle_frame;
}
/* 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(&audiohook_list->manipulate_list, list);
audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
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);
continue;
}
/* Feed in frame to manipulation */
audiohook->manipulate_callback(audiohook, chan, middle_frame, direction);
ast_audiohook_unlock(audiohook);
}
AST_LIST_TRAVERSE_SAFE_END
end_frame = middle_frame;
}
/* Now we figure out what to do with our end frame (whether to transcode or not) */
if (middle_frame == end_frame) {
/* Middle frame was modified and became the end frame... let's see if we need to transcode */
if (end_frame->subclass != start_frame->subclass) {
if (out_translate->format != start_frame->subclass) {
if (out_translate->trans_pvt)
ast_translator_free_path(out_translate->trans_pvt);
if (!(out_translate->trans_pvt = ast_translator_build_path(start_frame->subclass, AST_FORMAT_SLINEAR))) {
/* We can't transcode this... drop our middle frame and return the original */
ast_frfree(middle_frame);
return start_frame;
}
out_translate->format = start_frame->subclass;
}
/* Transcode from our middle (signed linear) frame to new format of the frame that came in */
if (!(end_frame = ast_translate(out_translate->trans_pvt, middle_frame, 0))) {
/* Failed to transcode the frame... drop it and return the original */
ast_frfree(middle_frame);
return start_frame;
}
/* Here's the scoop... middle frame is no longer of use to us */
ast_frfree(middle_frame);
}
/* Yay let's rid ourselves of the start frame */
ast_frfree(start_frame);
} else {
/* No frame was modified, we can just drop our middle frame and pass the frame we got in out */
ast_frfree(middle_frame);
}
return end_frame;
}
/*! \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 */
if (frame->frametype == AST_FRAME_VOICE)
return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
else if (frame->frametype == AST_FRAME_DTMF)
return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
else
return frame;
}
/*! \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 tv;
struct timespec ts;
tv = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * 1000;
ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
return;
}

View File

@@ -44,7 +44,6 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/sched.h"
#include "asterisk/options.h"
#include "asterisk/channel.h"
#include "asterisk/chanspy.h"
#include "asterisk/musiconhold.h"
#include "asterisk/logger.h"
#include "asterisk/say.h"
@@ -66,27 +65,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/sha1.h"
#include "asterisk/threadstorage.h"
#include "asterisk/slinfactory.h"
struct channel_spy_trans {
int last_format;
struct ast_trans_pvt *path;
};
/*! \brief List of SPY structures
*/
struct ast_channel_spy_list {
struct channel_spy_trans read_translator;
struct channel_spy_trans write_translator;
AST_LIST_HEAD_NOLOCK(, ast_channel_spy) list;
};
/*! \brief Definition of the Whisper buffer */
struct ast_channel_whisper_buffer {
ast_mutex_t lock;
struct ast_slinfactory sf;
unsigned int original_format;
struct ast_trans_pvt *path;
};
#include "asterisk/audiohook.h"
/* uncomment if you have problems with 'monitoring' synchronized files */
#if 0
@@ -1121,10 +1100,6 @@ void ast_channel_free(struct ast_channel *chan)
if (chan->music_state)
ast_moh_cleanup(chan);
/* if someone is whispering on the channel, stop them */
if (chan->whisper)
ast_channel_whisper_stop(chan);
/* Free translators */
if (chan->readtrans)
ast_translator_free_path(chan->readtrans);
@@ -1281,176 +1256,6 @@ struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const
return datastore;
}
int ast_channel_spy_add(struct ast_channel *chan, struct ast_channel_spy *spy)
{
/* Link the owner channel to the spy */
spy->chan = chan;
if (!ast_test_flag(spy, CHANSPY_FORMAT_AUDIO)) {
ast_log(LOG_WARNING, "Could not add channel spy '%s' to channel '%s', only audio format spies are supported.\n",
spy->type, chan->name);
return -1;
}
if (ast_test_flag(spy, CHANSPY_READ_VOLADJUST) && (spy->read_queue.format != AST_FORMAT_SLINEAR)) {
ast_log(LOG_WARNING, "Cannot provide volume adjustment on '%s' format spies\n",
ast_getformatname(spy->read_queue.format));
return -1;
}
if (ast_test_flag(spy, CHANSPY_WRITE_VOLADJUST) && (spy->write_queue.format != AST_FORMAT_SLINEAR)) {
ast_log(LOG_WARNING, "Cannot provide volume adjustment on '%s' format spies\n",
ast_getformatname(spy->write_queue.format));
return -1;
}
if (ast_test_flag(spy, CHANSPY_MIXAUDIO) &&
((spy->read_queue.format != AST_FORMAT_SLINEAR) ||
(spy->write_queue.format != AST_FORMAT_SLINEAR))) {
ast_log(LOG_WARNING, "Cannot provide audio mixing on '%s'-'%s' format spies\n",
ast_getformatname(spy->read_queue.format), ast_getformatname(spy->write_queue.format));
return -1;
}
if (!chan->spies) {
if (!(chan->spies = ast_calloc(1, sizeof(*chan->spies)))) {
return -1;
}
AST_LIST_HEAD_INIT_NOLOCK(&chan->spies->list);
AST_LIST_INSERT_HEAD(&chan->spies->list, spy, list);
} else {
AST_LIST_INSERT_TAIL(&chan->spies->list, spy, list);
}
if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE) {
ast_cond_init(&spy->trigger, NULL);
ast_set_flag(spy, CHANSPY_TRIGGER_READ);
ast_clear_flag(spy, CHANSPY_TRIGGER_WRITE);
}
ast_debug(1, "Spy %s added to channel %s\n",
spy->type, chan->name);
return 0;
}
/* Clean up a channel's spy information */
static void spy_cleanup(struct ast_channel *chan)
{
if (!AST_LIST_EMPTY(&chan->spies->list))
return;
if (chan->spies->read_translator.path)
ast_translator_free_path(chan->spies->read_translator.path);
if (chan->spies->write_translator.path)
ast_translator_free_path(chan->spies->write_translator.path);
ast_free(chan->spies);
chan->spies = NULL;
return;
}
/* Detach a spy from it's channel */
static void spy_detach(struct ast_channel_spy *spy, struct ast_channel *chan)
{
/* We only need to poke them if they aren't already done */
if (spy->status != CHANSPY_DONE) {
ast_mutex_lock(&spy->lock);
/* Indicate to the spy to stop */
spy->status = CHANSPY_STOP;
spy->chan = NULL;
/* Poke the spy if needed */
if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE)
ast_cond_signal(&spy->trigger);
ast_mutex_unlock(&spy->lock);
}
/* Print it out while we still have a lock so the structure can't go away (if signalled above) */
ast_debug(1, "Spy %s removed from channel %s\n", spy->type, chan->name);
return;
}
void ast_channel_spy_stop_by_type(struct ast_channel *chan, const char *type)
{
struct ast_channel_spy *spy = NULL;
if (!chan->spies)
return;
AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->spies->list, spy, list) {
if ((spy->type == type) && (spy->status == CHANSPY_RUNNING)) {
AST_LIST_REMOVE_CURRENT(&chan->spies->list, list);
spy_detach(spy, chan);
}
}
AST_LIST_TRAVERSE_SAFE_END
spy_cleanup(chan);
}
void ast_channel_spy_trigger_wait(struct ast_channel_spy *spy)
{
struct timeval tv;
struct timespec ts;
tv = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * 1000;
ast_cond_timedwait(&spy->trigger, &spy->lock, &ts);
}
void ast_channel_spy_remove(struct ast_channel *chan, struct ast_channel_spy *spy)
{
if (!chan->spies)
return;
AST_LIST_REMOVE(&chan->spies->list, spy, list);
spy_detach(spy, chan);
spy_cleanup(chan);
}
void ast_channel_spy_free(struct ast_channel_spy *spy)
{
struct ast_frame *f = NULL;
if (spy->status == CHANSPY_DONE)
return;
/* Switch status to done in case we get called twice */
spy->status = CHANSPY_DONE;
/* Drop any frames in the queue */
while ((f = AST_LIST_REMOVE_HEAD(&spy->write_queue.list, frame_list)))
ast_frfree(f);
while ((f = AST_LIST_REMOVE_HEAD(&spy->read_queue.list, frame_list)))
ast_frfree(f);
/* Destroy the condition if in use */
if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE)
ast_cond_destroy(&spy->trigger);
/* Destroy our mutex since it is no longer in use */
ast_mutex_destroy(&spy->lock);
return;
}
static void detach_spies(struct ast_channel *chan)
{
struct ast_channel_spy *spy = NULL;
if (!chan->spies)
return;
AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->spies->list, spy, list) {
AST_LIST_REMOVE_CURRENT(&chan->spies->list, list);
spy_detach(spy, chan);
}
AST_LIST_TRAVERSE_SAFE_END
spy_cleanup(chan);
}
/*! \brief Softly hangup a channel, don't lock */
int ast_softhangup_nolock(struct ast_channel *chan, int cause)
{
@@ -1474,124 +1279,6 @@ int ast_softhangup(struct ast_channel *chan, int cause)
return res;
}
enum spy_direction {
SPY_READ,
SPY_WRITE,
};
#define SPY_QUEUE_SAMPLE_LIMIT 4000 /* half of one second */
static void queue_frame_to_spies(struct ast_channel *chan, struct ast_frame *f, enum spy_direction dir)
{
struct ast_frame *translated_frame = NULL;
struct ast_channel_spy *spy;
struct channel_spy_trans *trans;
trans = (dir == SPY_READ) ? &chan->spies->read_translator : &chan->spies->write_translator;
AST_LIST_TRAVERSE(&chan->spies->list, spy, list) {
struct ast_channel_spy_queue *queue;
struct ast_frame *duped_fr;
if (spy->status != CHANSPY_RUNNING)
continue;
ast_mutex_lock(&spy->lock);
queue = (dir == SPY_READ) ? &spy->read_queue : &spy->write_queue;
if ((queue->format == AST_FORMAT_SLINEAR) && (f->subclass != AST_FORMAT_SLINEAR)) {
if (!translated_frame) {
if (trans->path && (trans->last_format != f->subclass)) {
ast_translator_free_path(trans->path);
trans->path = NULL;
}
if (!trans->path) {
ast_debug(1, "Building translator from %s to SLINEAR for spies on channel %s\n",
ast_getformatname(f->subclass), chan->name);
if ((trans->path = ast_translator_build_path(AST_FORMAT_SLINEAR, f->subclass)) == NULL) {
ast_log(LOG_WARNING, "Cannot build a path from %s to %s\n",
ast_getformatname(f->subclass), ast_getformatname(AST_FORMAT_SLINEAR));
ast_mutex_unlock(&spy->lock);
continue;
} else {
trans->last_format = f->subclass;
}
}
if (!(translated_frame = ast_translate(trans->path, f, 0))) {
ast_log(LOG_ERROR, "Translation to %s failed, dropping frame for spies\n",
ast_getformatname(AST_FORMAT_SLINEAR));
ast_mutex_unlock(&spy->lock);
break;
}
}
duped_fr = ast_frdup(translated_frame);
} else if (f->subclass != queue->format) {
ast_log(LOG_WARNING, "Spy '%s' on channel '%s' wants format '%s', but frame is '%s', dropping\n",
spy->type, chan->name,
ast_getformatname(queue->format), ast_getformatname(f->subclass));
ast_mutex_unlock(&spy->lock);
continue;
} else
duped_fr = ast_frdup(f);
AST_LIST_INSERT_TAIL(&queue->list, duped_fr, frame_list);
queue->samples += f->samples;
if (queue->samples > SPY_QUEUE_SAMPLE_LIMIT) {
if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE) {
switch (ast_test_flag(spy, CHANSPY_TRIGGER_MODE)) {
case CHANSPY_TRIGGER_READ:
if (dir == SPY_WRITE) {
ast_set_flag(spy, CHANSPY_TRIGGER_WRITE);
ast_clear_flag(spy, CHANSPY_TRIGGER_READ);
ast_debug(1, "Switching spy '%s' on '%s' to write-trigger mode\n",
spy->type, chan->name);
}
break;
case CHANSPY_TRIGGER_WRITE:
if (dir == SPY_READ) {
ast_set_flag(spy, CHANSPY_TRIGGER_READ);
ast_clear_flag(spy, CHANSPY_TRIGGER_WRITE);
ast_debug(1, "Switching spy '%s' on '%s' to read-trigger mode\n",
spy->type, chan->name);
}
break;
}
ast_debug(1, "Triggering queue flush for spy '%s' on '%s'\n",
spy->type, chan->name);
ast_set_flag(spy, CHANSPY_TRIGGER_FLUSH);
ast_cond_signal(&spy->trigger);
} else {
ast_debug(1, "Spy '%s' on channel '%s' %s queue too long, dropping frames\n",
spy->type, chan->name, (dir == SPY_READ) ? "read" : "write");
while (queue->samples > SPY_QUEUE_SAMPLE_LIMIT) {
struct ast_frame *drop = AST_LIST_REMOVE_HEAD(&queue->list, frame_list);
queue->samples -= drop->samples;
ast_frfree(drop);
}
}
} else {
switch (ast_test_flag(spy, CHANSPY_TRIGGER_MODE)) {
case CHANSPY_TRIGGER_READ:
if (dir == SPY_READ)
ast_cond_signal(&spy->trigger);
break;
case CHANSPY_TRIGGER_WRITE:
if (dir == SPY_WRITE)
ast_cond_signal(&spy->trigger);
break;
}
}
ast_mutex_unlock(&spy->lock);
}
if (translated_frame)
ast_frfree(translated_frame);
}
static void free_translation(struct ast_channel *clone)
{
if (clone->writetrans)
@@ -1614,7 +1301,10 @@ int ast_hangup(struct ast_channel *chan)
if someone is going to masquerade as us */
ast_channel_lock(chan);
detach_spies(chan); /* get rid of spies */
if (chan->audiohooks) {
ast_audiohook_detach_list(chan->audiohooks);
chan->audiohooks = NULL;
}
if (chan->masq) {
if (ast_do_masquerade(chan))
@@ -2310,6 +2000,8 @@ static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
chan->emulate_dtmf_duration = AST_DEFAULT_EMULATE_DTMF_DURATION;
ast_log(LOG_DTMF, "DTMF begin emulation of '%c' with duration %u queued on %s\n", f->subclass, chan->emulate_dtmf_duration, chan->name);
}
if (chan->audiohooks)
f = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_READ, f);
} else {
struct timeval now = ast_tvnow();
if (ast_test_flag(chan, AST_FLAG_IN_DTMF)) {
@@ -2331,6 +2023,8 @@ static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
ast_log(LOG_DTMF, "DTMF end passthrough '%c' on %s\n", f->subclass, chan->name);
chan->dtmf_tv = now;
}
if (chan->audiohooks)
f = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_READ, f);
}
break;
case AST_FRAME_DTMF_BEGIN:
@@ -2392,6 +2086,8 @@ static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
f->subclass = chan->emulate_dtmf_digit;
f->len = ast_tvdiff_ms(now, chan->dtmf_tv);
chan->dtmf_tv = now;
if (chan->audiohooks)
f = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_READ, f);
ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass, chan->name);
} else {
/* Drop voice frames while we're still in the middle of the digit */
@@ -2406,9 +2102,9 @@ static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
ast_frfree(f);
f = &ast_null_frame;
} else if ((f->frametype == AST_FRAME_VOICE)) {
if (chan->spies)
queue_frame_to_spies(chan, f, SPY_READ);
/* Send frame to audiohooks if present */
if (chan->audiohooks)
f = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_READ, f);
if (chan->monitor && chan->monitor->read_stream ) {
/* XXX what does this do ? */
#ifndef MONITOR_CONSTANT_DELAY
@@ -2746,6 +2442,8 @@ int ast_write(struct ast_channel *chan, struct ast_frame *fr)
chan->tech->indicate(chan, fr->subclass, fr->data, fr->datalen);
break;
case AST_FRAME_DTMF_BEGIN:
if (chan->audiohooks)
fr = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_WRITE, fr);
send_dtmf_event(chan, "Sent", fr->subclass, "Yes", "No");
ast_clear_flag(chan, AST_FLAG_BLOCKING);
ast_channel_unlock(chan);
@@ -2754,6 +2452,8 @@ int ast_write(struct ast_channel *chan, struct ast_frame *fr)
CHECK_BLOCKING(chan);
break;
case AST_FRAME_DTMF_END:
if (chan->audiohooks)
fr = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_WRITE, fr);
send_dtmf_event(chan, "Sent", fr->subclass, "No", "Yes");
ast_clear_flag(chan, AST_FLAG_BLOCKING);
ast_channel_unlock(chan);
@@ -2787,42 +2487,21 @@ int ast_write(struct ast_channel *chan, struct ast_frame *fr)
if (chan->tech->write == NULL)
break; /*! \todo XXX should return 0 maybe ? */
/* If someone is whispering on this channel then we must ensure that we are always getting signed linear frames */
if (ast_test_flag(chan, AST_FLAG_WHISPER)) {
if (fr->subclass == AST_FORMAT_SLINEAR)
f = fr;
else {
ast_mutex_lock(&chan->whisper->lock);
if (chan->writeformat != AST_FORMAT_SLINEAR) {
/* Rebuild the translation path and set our write format back to signed linear */
chan->whisper->original_format = chan->writeformat;
ast_set_write_format(chan, AST_FORMAT_SLINEAR);
if (chan->whisper->path)
ast_translator_free_path(chan->whisper->path);
chan->whisper->path = ast_translator_build_path(AST_FORMAT_SLINEAR, chan->whisper->original_format);
}
/* Translate frame using the above translation path */
f = (chan->whisper->path) ? ast_translate(chan->whisper->path, fr, 0) : fr;
ast_mutex_unlock(&chan->whisper->lock);
}
} else {
/* If the frame is in the raw write format, then it's easy... just use the frame - otherwise we will have to translate */
if (fr->subclass == chan->rawwriteformat)
f = fr;
else
f = (chan->writetrans) ? ast_translate(chan->writetrans, fr, 0) : fr;
}
/* If audiohooks are present, write the frame out */
if (chan->audiohooks)
fr = ast_audiohook_write_list(chan, chan->audiohooks, AST_AUDIOHOOK_DIRECTION_WRITE, fr);
/* If the frame is in the raw write format, then it's easy... just use the frame - otherwise we will have to translate */
if (fr->subclass == chan->rawwriteformat)
f = fr;
else
f = (chan->writetrans) ? ast_translate(chan->writetrans, fr, 0) : fr;
/* If we have no frame of audio, then we have to bail out */
if (f == NULL) {
if (!f) {
res = 0;
break;
}
/* If spies are on the channel then queue the frame out to them */
if (chan->spies)
queue_frame_to_spies(chan, f, SPY_WRITE);
/* If Monitor is running on this channel, then we have to write frames out there too */
if (chan->monitor && chan->monitor->write_stream) {
/* XXX must explain this code */
@@ -2849,30 +2528,6 @@ int ast_write(struct ast_channel *chan, struct ast_frame *fr)
ast_log(LOG_WARNING, "Failed to write data to channel monitor write stream\n");
}
}
/* Finally the good part! Write this out to the channel */
if (ast_test_flag(chan, AST_FLAG_WHISPER)) {
/* frame is assumed to be in SLINEAR, since that is
required for whisper mode */
ast_frame_adjust_volume(f, -2);
if (ast_slinfactory_available(&chan->whisper->sf) >= f->samples) {
short buf[f->samples];
struct ast_frame whisper = {
.frametype = AST_FRAME_VOICE,
.subclass = AST_FORMAT_SLINEAR,
.data = buf,
.datalen = sizeof(buf),
.samples = f->samples,
};
ast_mutex_lock(&chan->whisper->lock);
if (ast_slinfactory_read(&chan->whisper->sf, buf, f->samples))
ast_frame_slinear_sum(f, &whisper);
ast_mutex_unlock(&chan->whisper->lock);
}
/* and now put it through the regular translator */
f = (chan->writetrans) ? ast_translate(chan->writetrans, f, 0) : f;
}
res = f ? chan->tech->write(chan, f) : 0;
break;
case AST_FRAME_NULL:
@@ -3460,8 +3115,6 @@ int ast_do_masquerade(struct ast_channel *original)
void *t_pvt;
struct ast_callerid tmpcid;
struct ast_channel *clone = original->masq;
struct ast_channel_spy_list *spy_list = NULL;
struct ast_channel_spy *spy = NULL;
struct ast_cdr *cdr;
int rformat = original->readformat;
int wformat = original->writeformat;
@@ -3547,27 +3200,6 @@ int ast_do_masquerade(struct ast_channel *original)
original->rawwriteformat = clone->rawwriteformat;
clone->rawwriteformat = x;
/* Swap the spies */
spy_list = original->spies;
original->spies = clone->spies;
clone->spies = spy_list;
/* Update channel on respective spy lists if present */
if (original->spies) {
AST_LIST_TRAVERSE(&original->spies->list, spy, list) {
ast_mutex_lock(&spy->lock);
spy->chan = original;
ast_mutex_unlock(&spy->lock);
}
}
if (clone->spies) {
AST_LIST_TRAVERSE(&clone->spies->list, spy, list) {
ast_mutex_lock(&spy->lock);
spy->chan = clone;
ast_mutex_unlock(&spy->lock);
}
}
/* Save any pending frames on both sides. Start by counting
* how many we're going to need... */
x = 0;
@@ -3632,15 +3264,6 @@ int ast_do_masquerade(struct ast_channel *original)
ast_app_group_update(clone, original);
/* move any whisperer over */
ast_channel_whisper_stop(original);
if (ast_test_flag(clone, AST_FLAG_WHISPER)) {
original->whisper = clone->whisper;
ast_set_flag(original, AST_FLAG_WHISPER);
clone->whisper = NULL;
ast_clear_flag(clone, AST_FLAG_WHISPER);
}
/* Move data stores over */
if (AST_LIST_FIRST(&clone->datastores))
AST_LIST_INSERT_TAIL(&original->datastores, AST_LIST_FIRST(&clone->datastores), entry);
@@ -4152,7 +3775,8 @@ enum ast_bridge_result ast_channel_bridge(struct ast_channel *c0, struct ast_cha
(config->timelimit == 0) &&
(c0->tech->bridge == c1->tech->bridge) &&
!nativefailed && !c0->monitor && !c1->monitor &&
!c0->spies && !c1->spies && !ast_test_flag(&(config->features_callee),AST_FEATURE_REDIRECT) &&
!c0->audiohooks && !c1->audiohooks &&
!ast_test_flag(&(config->features_callee),AST_FEATURE_REDIRECT) &&
!ast_test_flag(&(config->features_caller),AST_FEATURE_REDIRECT) ) {
/* Looks like they share a bridge method and nothing else is in the way */
ast_set_flag(c0, AST_FLAG_NBRIDGE);
@@ -4525,129 +4149,6 @@ void ast_set_variables(struct ast_channel *chan, struct ast_variable *vars)
pbx_builtin_setvar_helper(chan, cur->name, cur->value);
}
static void copy_data_from_queue(struct ast_channel_spy_queue *queue, short *buf, unsigned int samples)
{
struct ast_frame *f;
int tocopy;
int bytestocopy;
while (samples) {
if (!(f = AST_LIST_FIRST(&queue->list))) {
ast_log(LOG_ERROR, "Ran out of frames before buffer filled!\n");
break;
}
tocopy = (f->samples > samples) ? samples : f->samples;
bytestocopy = ast_codec_get_len(queue->format, tocopy);
memcpy(buf, f->data, bytestocopy);
samples -= tocopy;
buf += tocopy;
f->samples -= tocopy;
f->data += bytestocopy;
f->datalen -= bytestocopy;
f->offset += bytestocopy;
queue->samples -= tocopy;
if (!f->samples)
ast_frfree(AST_LIST_REMOVE_HEAD(&queue->list, frame_list));
}
}
struct ast_frame *ast_channel_spy_read_frame(struct ast_channel_spy *spy, unsigned int samples)
{
struct ast_frame *result;
/* buffers are allocated to hold SLINEAR, which is the largest format */
short read_buf[samples];
short write_buf[samples];
struct ast_frame *read_frame;
struct ast_frame *write_frame;
int need_dup;
struct ast_frame stack_read_frame = { .frametype = AST_FRAME_VOICE,
.subclass = spy->read_queue.format,
.data = read_buf,
.samples = samples,
.datalen = ast_codec_get_len(spy->read_queue.format, samples),
};
struct ast_frame stack_write_frame = { .frametype = AST_FRAME_VOICE,
.subclass = spy->write_queue.format,
.data = write_buf,
.samples = samples,
.datalen = ast_codec_get_len(spy->write_queue.format, samples),
};
/* if a flush has been requested, dump everything in whichever queue is larger */
if (ast_test_flag(spy, CHANSPY_TRIGGER_FLUSH)) {
if (spy->read_queue.samples > spy->write_queue.samples) {
if (ast_test_flag(spy, CHANSPY_READ_VOLADJUST)) {
AST_LIST_TRAVERSE(&spy->read_queue.list, result, frame_list)
ast_frame_adjust_volume(result, spy->read_vol_adjustment);
}
result = AST_LIST_FIRST(&spy->read_queue.list);
AST_LIST_HEAD_SET_NOLOCK(&spy->read_queue.list, NULL);
spy->read_queue.samples = 0;
} else {
if (ast_test_flag(spy, CHANSPY_WRITE_VOLADJUST)) {
AST_LIST_TRAVERSE(&spy->write_queue.list, result, frame_list)
ast_frame_adjust_volume(result, spy->write_vol_adjustment);
}
result = AST_LIST_FIRST(&spy->write_queue.list);
AST_LIST_HEAD_SET_NOLOCK(&spy->write_queue.list, NULL);
spy->write_queue.samples = 0;
}
ast_clear_flag(spy, CHANSPY_TRIGGER_FLUSH);
return result;
}
if ((spy->read_queue.samples < samples) || (spy->write_queue.samples < samples))
return NULL;
/* short-circuit if both head frames have exactly what we want */
if ((AST_LIST_FIRST(&spy->read_queue.list)->samples == samples) &&
(AST_LIST_FIRST(&spy->write_queue.list)->samples == samples)) {
read_frame = AST_LIST_REMOVE_HEAD(&spy->read_queue.list, frame_list);
write_frame = AST_LIST_REMOVE_HEAD(&spy->write_queue.list, frame_list);
spy->read_queue.samples -= samples;
spy->write_queue.samples -= samples;
need_dup = 0;
} else {
copy_data_from_queue(&spy->read_queue, read_buf, samples);
copy_data_from_queue(&spy->write_queue, write_buf, samples);
read_frame = &stack_read_frame;
write_frame = &stack_write_frame;
need_dup = 1;
}
if (ast_test_flag(spy, CHANSPY_READ_VOLADJUST))
ast_frame_adjust_volume(read_frame, spy->read_vol_adjustment);
if (ast_test_flag(spy, CHANSPY_WRITE_VOLADJUST))
ast_frame_adjust_volume(write_frame, spy->write_vol_adjustment);
if (ast_test_flag(spy, CHANSPY_MIXAUDIO)) {
ast_frame_slinear_sum(read_frame, write_frame);
if (need_dup)
result = ast_frdup(read_frame);
else {
result = read_frame;
ast_frfree(write_frame);
}
} else {
if (need_dup) {
result = ast_frdup(read_frame);
AST_LIST_NEXT(result, frame_list) = ast_frdup(write_frame);
} else {
result = read_frame;
AST_LIST_NEXT(result, frame_list) = write_frame;
}
}
return result;
}
static void *silence_generator_alloc(struct ast_channel *chan, void *data)
{
/* just store the data pointer in the channel structure */
@@ -4894,46 +4395,3 @@ int ast_say_digits_full(struct ast_channel *chan, int num,
snprintf(buf, sizeof(buf), "%d", num);
return ast_say_digit_str_full(chan, buf, ints, lang, audiofd, ctrlfd);
}
int ast_channel_whisper_start(struct ast_channel *chan)
{
if (chan->whisper)
return -1;
if (!(chan->whisper = ast_calloc(1, sizeof(*chan->whisper))))
return -1;
ast_mutex_init(&chan->whisper->lock);
ast_slinfactory_init(&chan->whisper->sf);
ast_set_flag(chan, AST_FLAG_WHISPER);
return 0;
}
int ast_channel_whisper_feed(struct ast_channel *chan, struct ast_frame *f)
{
if (!chan->whisper)
return -1;
ast_mutex_lock(&chan->whisper->lock);
ast_slinfactory_feed(&chan->whisper->sf, f);
ast_mutex_unlock(&chan->whisper->lock);
return 0;
}
void ast_channel_whisper_stop(struct ast_channel *chan)
{
if (!chan->whisper)
return;
ast_clear_flag(chan, AST_FLAG_WHISPER);
if (chan->whisper->path)
ast_translator_free_path(chan->whisper->path);
if (chan->whisper->original_format && chan->writeformat == AST_FORMAT_SLINEAR)
ast_set_write_format(chan, chan->whisper->original_format);
ast_slinfactory_destroy(&chan->whisper->sf);
ast_mutex_destroy(&chan->whisper->lock);
ast_free(chan->whisper);
chan->whisper = NULL;
}

View File

@@ -148,3 +148,21 @@ unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf)
{
return sf->size;
}
void ast_slinfactory_flush(struct ast_slinfactory *sf)
{
struct ast_frame *fr = NULL;
if (sf->trans) {
ast_translator_free_path(sf->trans);
sf->trans = NULL;
}
while ((fr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
ast_frfree(fr);
sf->size = sf->holdlen = 0;
sf->offset = sf->hold;
return;
}