[svn-commits] file: branch file/ah r58875 - in /team/file/ah: apps/
include/asterisk/ main/
svn-commits at lists.digium.com
svn-commits at lists.digium.com
Tue Mar 13 16:44:53 MST 2007
Author: file
Date: Tue Mar 13 18:44:52 2007
New Revision: 58875
URL: http://svn.digium.com/view/asterisk?view=rev&rev=58875
Log:
Add audiohook architecture. This is basically chanspy cleaned up with a few things done differently. It also implements a new type, manipulate, alongside the old spy and whisper. Currently the spy type is working fine.
Added:
team/file/ah/include/asterisk/audiohook.h (with props)
team/file/ah/main/audiohook.c (with props)
Modified:
team/file/ah/apps/app_chanspy.c
team/file/ah/apps/app_mixmonitor.c
team/file/ah/include/asterisk/channel.h
team/file/ah/main/Makefile
team/file/ah/main/channel.c
Modified: team/file/ah/apps/app_chanspy.c
URL: http://svn.digium.com/view/asterisk/team/file/ah/apps/app_chanspy.c?view=diff&rev=58875&r1=58874&r2=58875
==============================================================================
--- team/file/ah/apps/app_chanspy.c (original)
+++ team/file/ah/apps/app_chanspy.c Tue Mar 13 18:44:52 2007
@@ -40,7 +40,7 @@
#include "asterisk/file.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
-#include "asterisk/chanspy.h"
+#include "asterisk/audiohook.h"
#include "asterisk/features.h"
#include "asterisk/options.h"
#include "asterisk/app.h"
@@ -166,7 +166,7 @@
struct chanspy_translation_helper {
/* spy data */
- struct ast_channel_spy spy;
+ struct ast_audiohook audiohook;
int fd;
int volfactor;
};
@@ -185,15 +185,18 @@
static int spy_generate(struct ast_channel *chan, void *data, int len, int samples)
{
struct chanspy_translation_helper *csth = data;
- struct ast_frame *f;
+ struct ast_frame *f = NULL;
- if (csth->spy.status != CHANSPY_RUNNING)
+ ast_mutex_lock(&csth->audiohook.lock);
+ if (csth->audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) {
/* Channel is already gone more than likely */
+ ast_mutex_unlock(&csth->audiohook.lock);
return -1;
-
- ast_mutex_lock(&csth->spy.lock);
- f = ast_channel_spy_read_frame(&csth->spy, samples);
- ast_mutex_unlock(&csth->spy.lock);
+ }
+
+ f = ast_audiohook_read_frame(&csth->audiohook, samples, AST_AUDIOHOOK_DIRECTION_BOTH, AST_FORMAT_SLINEAR);
+
+ ast_mutex_unlock(&csth->audiohook.lock);
if (!f)
return 0;
@@ -217,50 +220,19 @@
.generate = spy_generate,
};
-static int start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_channel_spy *spy)
-{
- int res;
- struct ast_channel *peer;
+static int start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_audiohook *audiohook)
+{
+ int res = 0;
+ struct ast_channel *peer = NULL;
ast_log(LOG_NOTICE, "Attaching %s to %s\n", spychan->name, chan->name);
- ast_channel_lock(chan);
- res = ast_channel_spy_add(chan, spy);
- ast_channel_unlock(chan);
+ res = ast_audiohook_attach(chan, audiohook);
if (!res && ast_test_flag(chan, AST_FLAG_NBRIDGE) && (peer = ast_bridged_channel(chan)))
ast_softhangup(peer, AST_SOFTHANGUP_UNBRIDGE);
return res;
-}
-
-/* Map 'volume' levels from -4 through +4 into
- decibel (dB) settings for channel drivers
-*/
-static signed char volfactor_map[] = {
- -24,
- -18,
- -12,
- -6,
- 0,
- 6,
- 12,
- 18,
- 24,
-};
-
-/* attempt to set the desired gain adjustment via the channel driver;
- if successful, clear it out of the csth structure so the
- generator will not attempt to do the adjustment itself
-*/
-static void set_volume(struct ast_channel *chan, struct chanspy_translation_helper *csth)
-{
- signed char volume_adjust = volfactor_map[csth->volfactor + 4];
-
- if (!ast_channel_setoption(chan, AST_OPTION_TXGAIN, &volume_adjust, sizeof(volume_adjust), 0))
- csth->volfactor = 0;
- csth->spy.read_vol_adjustment = csth->volfactor;
- csth->spy.write_vol_adjustment = csth->volfactor;
}
static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor, int fd,
@@ -281,49 +253,12 @@
ast_verbose(VERBOSE_PREFIX_2 "Spying on channel %s\n", name);
memset(&csth, 0, sizeof(csth));
- ast_set_flag(&csth.spy, CHANSPY_FORMAT_AUDIO);
- ast_set_flag(&csth.spy, CHANSPY_TRIGGER_NONE);
- if (!ast_test_flag(flags, OPTION_READONLY))
- ast_set_flag(&csth.spy, CHANSPY_MIXAUDIO);
- csth.spy.type = "ChanSpy";
- csth.spy.status = CHANSPY_RUNNING;
- csth.spy.read_queue.format = AST_FORMAT_SLINEAR;
- csth.spy.write_queue.format = AST_FORMAT_SLINEAR;
- ast_mutex_init(&csth.spy.lock);
- csth.volfactor = *volfactor;
- set_volume(chan, &csth);
- if (csth.volfactor) {
- ast_set_flag(&csth.spy, CHANSPY_READ_VOLADJUST);
- csth.spy.read_vol_adjustment = csth.volfactor;
- ast_set_flag(&csth.spy, CHANSPY_WRITE_VOLADJUST);
- csth.spy.write_vol_adjustment = csth.volfactor;
- }
- csth.fd = fd;
+
+ ast_audiohook_init(&csth.audiohook, AST_AUDIOHOOK_TYPE_SPY, "ChanSpy");
- if (start_spying(spyee, chan, &csth.spy)) {
- ast_mutex_destroy(&csth.spy.lock);
+ if (start_spying(spyee, chan, &csth.audiohook)) {
+ ast_audiohook_destroy(&csth.audiohook);
return 0;
- }
-
- if (ast_test_flag(flags, OPTION_WHISPER)) {
- struct ast_filestream *beepstream;
- int old_write_format = 0;
-
- ast_channel_whisper_start(csth.spy.chan);
- old_write_format = chan->writeformat;
- if ((beepstream = ast_openstream_full(chan, "beep", chan->language, 1))) {
- struct ast_frame *f;
-
- while ((f = ast_readframe(beepstream))) {
- ast_channel_whisper_feed(csth.spy.chan, f);
- ast_frfree(f);
- }
-
- ast_closestream(beepstream);
- chan->stream = NULL;
- }
- if (old_write_format)
- ast_set_write_format(chan, old_write_format);
}
if (ast_test_flag(flags, OPTION_PRIVATE))
@@ -345,21 +280,12 @@
has arrived, since the spied-on channel could have gone away while
we were waiting
*/
- while ((res = ast_waitfor(chan, -1) > -1) &&
- csth.spy.status == CHANSPY_RUNNING &&
- csth.spy.chan) {
+ while ((res = ast_waitfor(chan, -1) > -1) && csth.audiohook.status == AST_AUDIOHOOK_STATUS_RUNNING) {
if (!(f = ast_read(chan)) || ast_check_hangup(chan)) {
running = -1;
break;
}
-
- if (ast_test_flag(flags, OPTION_WHISPER) &&
- (f->frametype == AST_FRAME_VOICE)) {
- ast_channel_whisper_feed(csth.spy.chan, f);
- ast_frfree(f);
- continue;
- }
-
+
res = (f->frametype == AST_FRAME_DTMF) ? f->subclass : 0;
ast_frfree(f);
if (!res)
@@ -403,36 +329,20 @@
*volfactor = -4;
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "Setting spy volume on %s to %d\n", chan->name, *volfactor);
- csth.volfactor = *volfactor;
- set_volume(chan, &csth);
- if (csth.volfactor) {
- ast_set_flag(&csth.spy, CHANSPY_READ_VOLADJUST);
- csth.spy.read_vol_adjustment = csth.volfactor;
- ast_set_flag(&csth.spy, CHANSPY_WRITE_VOLADJUST);
- csth.spy.write_vol_adjustment = csth.volfactor;
- } else {
- ast_clear_flag(&csth.spy, CHANSPY_READ_VOLADJUST);
- ast_clear_flag(&csth.spy, CHANSPY_WRITE_VOLADJUST);
- }
- }
- }
-
- if (ast_test_flag(flags, OPTION_WHISPER) && csth.spy.chan)
- ast_channel_whisper_stop(csth.spy.chan);
+ }
+ }
if (ast_test_flag(flags, OPTION_PRIVATE))
ast_channel_stop_silence_generator(chan, silgen);
else
ast_deactivate_generator(chan);
+ ast_mutex_lock(&csth.audiohook.lock);
+ ast_audiohook_detach(&csth.audiohook);
+ ast_mutex_unlock(&csth.audiohook.lock);
+
/* If a channel still exists on our spy structure then we need to remove ourselves */
- if (csth.spy.chan) {
- csth.spy.status = CHANSPY_DONE;
- ast_channel_lock(csth.spy.chan);
- ast_channel_spy_remove(csth.spy.chan, &csth.spy);
- ast_channel_unlock(csth.spy.chan);
- }
- ast_channel_spy_free(&csth.spy);
+ ast_audiohook_destroy(&csth.audiohook);
if (option_verbose >= 2)
ast_verbose(VERBOSE_PREFIX_2 "Done Spying on channel %s\n", name);
Modified: team/file/ah/apps/app_mixmonitor.c
URL: http://svn.digium.com/view/asterisk/team/file/ah/apps/app_mixmonitor.c?view=diff&rev=58875&r1=58874&r2=58875
==============================================================================
--- team/file/ah/apps/app_mixmonitor.c (original)
+++ team/file/ah/apps/app_mixmonitor.c Tue Mar 13 18:44:52 2007
@@ -45,7 +45,7 @@
#include "asterisk/file.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
-#include "asterisk/chanspy.h"
+#include "asterisk/audiohook.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
@@ -93,7 +93,7 @@
static const char *mixmonitor_spy_type = "MixMonitor";
struct mixmonitor {
- struct ast_channel_spy spy;
+ struct ast_audiohook audiohook;
char *filename;
char *post_process;
char *name;
@@ -123,17 +123,15 @@
AST_APP_OPTION_ARG('W', MUXFLAG_VOLUME, OPT_ARG_VOLUME),
});
-static int startmon(struct ast_channel *chan, struct ast_channel_spy *spy)
-{
- struct ast_channel *peer;
- int res;
+static int startmon(struct ast_channel *chan, struct ast_audiohook *audiohook)
+{
+ struct ast_channel *peer = NULL;
+ int res = 0;
if (!chan)
return -1;
- ast_channel_lock(chan);
- res = ast_channel_spy_add(chan, spy);
- ast_channel_unlock(chan);
+ ast_audiohook_attach(chan, audiohook);
if (!res && ast_test_flag(chan, AST_FLAG_NBRIDGE) && (peer = ast_bridged_channel(chan)))
ast_softhangup(peer, AST_SOFTHANGUP_UNBRIDGE);
@@ -146,7 +144,6 @@
static void *mixmonitor_thread(void *obj)
{
struct mixmonitor *mixmonitor = obj;
- struct ast_frame *f = NULL;
struct ast_filestream *fs = NULL;
unsigned int oflags;
char *ext;
@@ -155,59 +152,50 @@
if (option_verbose > 1)
ast_verbose(VERBOSE_PREFIX_2 "Begin MixMonitor Recording %s\n", mixmonitor->name);
- ast_mutex_lock(&mixmonitor->spy.lock);
-
- while (mixmonitor->spy.chan) {
- struct ast_frame *next;
- int write;
-
- ast_channel_spy_trigger_wait(&mixmonitor->spy);
-
- if (!mixmonitor->spy.chan || mixmonitor->spy.status != CHANSPY_RUNNING)
+ ast_mutex_lock(&mixmonitor->audiohook.lock);
+
+ while (mixmonitor->audiohook.status == AST_AUDIOHOOK_STATUS_RUNNING) {
+ struct ast_frame *fr = NULL;
+
+ ast_audiohook_trigger_wait(&mixmonitor->audiohook);
+
+ if (mixmonitor->audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING)
break;
-
- while (1) {
- if (!(f = ast_channel_spy_read_frame(&mixmonitor->spy, SAMPLES_PER_FRAME)))
- break;
-
- write = (!ast_test_flag(mixmonitor, MUXFLAG_BRIDGED) ||
- ast_bridged_channel(mixmonitor->spy.chan));
-
- /* it is possible for ast_channel_spy_read_frame() to return a chain
- of frames if a queue flush was necessary, so process them
- */
- for (; f; f = next) {
- next = AST_LIST_NEXT(f, frame_list);
- if (write && errflag == 0) {
- if (!fs) {
- /* Determine creation flags and filename plus extension for filestream */
- oflags = O_CREAT | O_WRONLY;
- oflags |= ast_test_flag(mixmonitor, MUXFLAG_APPEND) ? O_APPEND : O_TRUNC;
-
- if ((ext = strrchr(mixmonitor->filename, '.')))
- *(ext++) = '\0';
- else
- ext = "raw";
-
- /* Move onto actually creating the filestream */
- if (!(fs = ast_writefile(mixmonitor->filename, ext, NULL, oflags, 0, 0644))) {
- ast_log(LOG_ERROR, "Cannot open %s.%s\n", mixmonitor->filename, ext);
- errflag = 1;
- }
-
- }
- if (fs)
- ast_writestream(fs, f);
- }
- ast_frame_free(f, 0);
+
+ if (!(fr = ast_audiohook_read_frame(&mixmonitor->audiohook, SAMPLES_PER_FRAME, AST_AUDIOHOOK_DIRECTION_BOTH, AST_FORMAT_SLINEAR)))
+ continue;
+
+ /* Initialize the file if not already done so */
+ if (!fs && !errflag) {
+ oflags = O_CREAT | O_WRONLY;
+ oflags |= ast_test_flag(mixmonitor, MUXFLAG_APPEND) ? O_APPEND : O_TRUNC;
+
+ if ((ext = strrchr(mixmonitor->filename, '.')))
+ *(ext++) = '\0';
+ else
+ ext = "raw";
+
+ if (!(fs = ast_writefile(mixmonitor->filename, ext, NULL, oflags, 0, 0644))) {
+ ast_log(LOG_ERROR, "Cannot open %s.%s\n", mixmonitor->filename, ext);
+ errflag = 1;
}
}
- }
-
- ast_mutex_unlock(&mixmonitor->spy.lock);
-
- ast_channel_spy_free(&mixmonitor->spy);
-
+
+ /* Write out frame */
+ if (fs)
+ ast_writestream(fs, fr);
+
+ /* All done! free it. */
+ ast_frame_free(fr, 0);
+
+ }
+
+ ast_audiohook_detach(&mixmonitor->audiohook);
+
+ ast_mutex_unlock(&mixmonitor->audiohook.lock);
+
+ ast_audiohook_destroy(&mixmonitor->audiohook);
+
if (option_verbose > 1)
ast_verbose(VERBOSE_PREFIX_2 "End MixMonitor Recording %s\n", mixmonitor->name);
@@ -271,27 +259,17 @@
strcpy(mixmonitor->filename, filename);
/* Setup the actual spy before creating our thread */
- ast_set_flag(&mixmonitor->spy, CHANSPY_FORMAT_AUDIO);
- ast_set_flag(&mixmonitor->spy, CHANSPY_MIXAUDIO);
- mixmonitor->spy.type = mixmonitor_spy_type;
- mixmonitor->spy.status = CHANSPY_RUNNING;
- mixmonitor->spy.read_queue.format = AST_FORMAT_SLINEAR;
- mixmonitor->spy.write_queue.format = AST_FORMAT_SLINEAR;
- if (readvol) {
- ast_set_flag(&mixmonitor->spy, CHANSPY_READ_VOLADJUST);
- mixmonitor->spy.read_vol_adjustment = readvol;
- }
- if (writevol) {
- ast_set_flag(&mixmonitor->spy, CHANSPY_WRITE_VOLADJUST);
- mixmonitor->spy.write_vol_adjustment = writevol;
- }
- ast_mutex_init(&mixmonitor->spy.lock);
-
- if (startmon(chan, &mixmonitor->spy)) {
+ if (ast_audiohook_init(&mixmonitor->audiohook, AST_AUDIOHOOK_TYPE_SPY, mixmonitor_spy_type)) {
+ free(mixmonitor);
+ return;
+ }
+
+ ast_set_flag(&mixmonitor->audiohook, AST_AUDIOHOOK_TRIGGER_WRITE);
+
+ if (startmon(chan, &mixmonitor->audiohook)) {
ast_log(LOG_WARNING, "Unable to add '%s' spy to channel '%s'\n",
- mixmonitor->spy.type, chan->name);
- /* Since we couldn't add ourselves - bail out! */
- ast_mutex_destroy(&mixmonitor->spy.lock);
+ mixmonitor_spy_type, chan->name);
+ ast_audiohook_destroy(&mixmonitor->audiohook);
free(mixmonitor);
return;
}
@@ -390,11 +368,11 @@
struct ast_module_user *u;
u = ast_module_user_add(chan);
-
+/*
ast_channel_lock(chan);
ast_channel_spy_stop_by_type(chan, mixmonitor_spy_type);
ast_channel_unlock(chan);
-
+*/
ast_module_user_remove(u);
return 0;
@@ -411,12 +389,12 @@
ast_cli(fd, "No channel matching '%s' found.\n", argv[2]);
return RESULT_SUCCESS;
}
-
+/*
if (!strcasecmp(argv[1], "start"))
mixmonitor_exec(chan, argv[3]);
else if (!strcasecmp(argv[1], "stop"))
ast_channel_spy_stop_by_type(chan, mixmonitor_spy_type);
-
+*/
ast_channel_unlock(chan);
return RESULT_SUCCESS;
Added: team/file/ah/include/asterisk/audiohook.h
URL: http://svn.digium.com/view/asterisk/team/file/ah/include/asterisk/audiohook.h?view=auto&rev=58875
==============================================================================
--- team/file/ah/include/asterisk/audiohook.h (added)
+++ team/file/ah/include/asterisk/audiohook.h Tue Mar 13 18:44:52 2007
@@ -1,0 +1,154 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2007, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at 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
+ */
+
+#ifndef _ASTERISK_AUDIOHOOK_H
+#define _ASTERISK_AUDIOHOOK_H
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+#include "asterisk/slinfactory.h"
+
+enum ast_audiohook_type {
+ AST_AUDIOHOOK_TYPE_SPY = 0,
+ AST_AUDIOHOOK_TYPE_WHISPER,
+ AST_AUDIOHOOK_TYPE_MANIPULATE,
+};
+
+enum ast_audiohook_status {
+ AST_AUDIOHOOK_STATUS_NEW = 0,
+ AST_AUDIOHOOK_STATUS_RUNNING,
+ AST_AUDIOHOOK_STATUS_SHUTDOWN,
+ AST_AUDIOHOOK_STATUS_DONE,
+};
+
+enum ast_audiohook_direction {
+ AST_AUDIOHOOK_DIRECTION_READ = 0,
+ AST_AUDIOHOOK_DIRECTION_WRITE,
+ AST_AUDIOHOOK_DIRECTION_BOTH,
+};
+
+enum ast_audiohook_flags {
+ AST_AUDIOHOOK_TRIGGER_MODE = (3 << 0),
+ AST_AUDIOHOOK_TRIGGER_READ = (1 << 0),
+ AST_AUDIOHOOK_TRIGGER_WRITE = (2 << 0),
+};
+
+struct ast_audiohook {
+ ast_mutex_t lock; /*!< Lock that protects the audiohook structure */
+ ast_cond_t trigger; /*!< Trigger condition (if enabled) */
+ enum ast_audiohook_type type; /*!< Type of audiohook */
+ enum ast_audiohook_status status; /*!< Status of the audiohook */
+ const char *source; /*!< Who this audiohook ultimately belongs to */
+ unsigned int flags; /*!< Flags on the audiohook */
+ struct ast_slinfactory read_factory; /*!< Factory where frames read from the channel, or read from the whisper source will go through */
+ struct ast_slinfactory write_factory; /*!< Factory where frames written to the channel will go through */
+ int format; /*!< Format translation path is setup as */
+ struct ast_trans_pvt *trans_pvt; /*!< Translation path for reading frames */
+ AST_LIST_ENTRY(ast_audiohook) list; /*!< Linked list information */
+};
+
+struct ast_audiohook_list;
+
+/*! \brief Initialize an audiohook structure
+ * \param audiohook Audiohook structure
+ * \param type Type of audiohook to initialize this as
+ * \param source Who is initializing this audiohook
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source);
+
+/*! \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);
+
+/*! \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);
+
+/*! \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);
+
+/*! \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);
+
+/*! \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);
+
+/*! \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);
+
+/*! \brief Writes a frame out to spies
+ * \param list List of audiohooks
+ * \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_spy_write_frame(struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame);
+
+/*! \brief Merges frame with data from whisper sources
+ * \param list List of audiohooks
+ * \param frame Frame to merge data into
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_whisper_read_frame(struct ast_audiohook_list *audiohook_list, struct ast_frame *frame);
+
+/*! \brief Passes frame to manipulate audiohook to be modified
+ * \param list List of audiohooks
+ * \param frame Frame to be manipulated
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_manipulate_frame(struct ast_audiohook_list *audiohook_list, struct ast_frame *frame);
+
+/*! \brief Wait for audiohook trigger to be triggered
+ * \param audiohook Audiohook to wait on
+ */
+void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook);
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif /* _ASTERISK_AUDIOHOOK_H */
Propchange: team/file/ah/include/asterisk/audiohook.h
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: team/file/ah/include/asterisk/audiohook.h
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Propchange: team/file/ah/include/asterisk/audiohook.h
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: team/file/ah/include/asterisk/channel.h
URL: http://svn.digium.com/view/asterisk/team/file/ah/include/asterisk/channel.h?view=diff&rev=58875&r1=58874&r2=58875
==============================================================================
--- team/file/ah/include/asterisk/channel.h (original)
+++ team/file/ah/include/asterisk/channel.h Tue Mar 13 18:44:52 2007
@@ -314,9 +314,6 @@
int (* func_channel_write)(struct ast_channel *chan, const char *function, char *data, const char *value);
};
-struct ast_channel_spy_list; /*!< \todo Add explanation here */
-struct ast_channel_whisper_buffer; /*!< \todo Add explanation here */
-
/*!
* The high bit of the frame count is used as a debug marker, so
* increments of the counters must be done with care.
@@ -479,8 +476,8 @@
int rawreadformat; /*!< Raw read format */
int rawwriteformat; /*!< Raw write format */
- struct ast_channel_spy_list *spies; /*!< Chan Spy stuff */
- struct ast_channel_whisper_buffer *whisper; /*!< Whisper Paging buffer */
+ struct ast_audiohook_list *audiohooks;
+
AST_LIST_ENTRY(ast_channel) chan_list; /*!< For easy linking */
struct ast_jb jb; /*!< The jitterbuffer state */
Modified: team/file/ah/main/Makefile
URL: http://svn.digium.com/view/asterisk/team/file/ah/main/Makefile?view=diff&rev=58875&r1=58874&r2=58875
==============================================================================
--- team/file/ah/main/Makefile (original)
+++ team/file/ah/main/Makefile Tue Mar 13 18:44:52 2007
@@ -26,7 +26,7 @@
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
+ strcompat.o threadstorage.o dial.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
Added: team/file/ah/main/audiohook.c
URL: http://svn.digium.com/view/asterisk/team/file/ah/main/audiohook.c?view=auto&rev=58875
==============================================================================
--- team/file/ah/main/audiohook.c (added)
+++ team/file/ah/main/audiohook.c Tue Mar 13 18:44:52 2007
@@ -1,0 +1,425 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2007, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at 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 at 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_list {
+ 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 */
+ audiohook->type = type;
+
+ /* Initialize lock that protects our audiohook */
+ ast_mutex_init(&audiohook->lock);
+ ast_cond_init(&audiohook->trigger, NULL);
+
+ /* Initialize our factories */
+ ast_slinfactory_init(&audiohook->read_factory);
+ /* Manipulate type only needs one factory */
+ if (type != AST_AUDIOHOOK_TYPE_MANIPULATE)
+ ast_slinfactory_init(&audiohook->write_factory);
+
+ /* 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 */
+ ast_slinfactory_destroy(&audiohook->read_factory);
+ if (audiohook->type != AST_AUDIOHOOK_TYPE_MANIPULATE)
+ ast_slinfactory_destroy(&audiohook->write_factory);
+
+ /* 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);
+ 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;
+
+ 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;
+ } 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;
+ } 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(AST_FORMAT_SLINEAR, format))) {
+ 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);
+ }
+
+ /* 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);
+
+ /* 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)
+{
+ struct ast_audiohook *audiohook = NULL;
+
+ /* Drop any spies */
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
+ ast_mutex_lock(&audiohook->lock);
+ AST_LIST_REMOVE_CURRENT(&audiohook_list->spy_list, list);
+ audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
+ ast_cond_signal(&audiohook->trigger);
+ ast_mutex_unlock(&audiohook->lock);
+ }
+ AST_LIST_TRAVERSE_SAFE_END
+
+ /* Drop any whispering sources */
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
+ ast_mutex_lock(&audiohook->lock);
+ AST_LIST_REMOVE_CURRENT(&audiohook_list->whisper_list, list);
+ audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
+ ast_cond_signal(&audiohook->trigger);
+ ast_mutex_unlock(&audiohook->lock);
+ }
+ AST_LIST_TRAVERSE_SAFE_END
+
+ /* Drop any manipulaters */
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
+ ast_mutex_lock(&audiohook->lock);
+ AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
+ audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
+ ast_cond_signal(&audiohook->trigger);
+ ast_mutex_unlock(&audiohook->lock);
+ }
+ AST_LIST_TRAVERSE_SAFE_END
+
+ /* Free ourselves */
+ free(audiohook_list);
+
+ return 0;
+}
+
+/*! \brief Writes a frame out to spies
+ * \param list List of audiohooks
+ * \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_spy_write_frame(struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame)
+{
+ struct ast_audiohook *audiohook = NULL;
+
+ /* Write out frame to each spy */
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
+ ast_mutex_lock(&audiohook->lock);
+ 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_mutex_unlock(&audiohook->lock);
+ continue;
+ }
+ ast_audiohook_write_frame(audiohook, direction, frame);
+ ast_mutex_unlock(&audiohook->lock);
+ }
+ AST_LIST_TRAVERSE_SAFE_END
+
+ return 0;
+}
+
+/*! \brief Merges frame with data from whisper sources
+ * \param list List of audiohooks
+ * \param frame Frame to merge data into
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_whisper_read_frame(struct ast_audiohook_list *audiohook_list, struct ast_frame *frame)
+{
+ struct ast_audiohook *audiohook = NULL;
+
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
+ ast_mutex_lock(&audiohook->lock);
+ /* If the audiohook wants to detach, let it go */
+ 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_mutex_unlock(&audiohook->lock);
+ continue;
+ }
+ ast_mutex_unlock(&audiohook->lock);
+ }
+ AST_LIST_TRAVERSE_SAFE_END
+
+ return 0;
+}
+
+/*! \brief Passes frame to manipulate audiohook to be modified
+ * \param list List of audiohooks
+ * \param frame Frame to be manipulated
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_manipulate_frame(struct ast_audiohook_list *audiohook_list, struct ast_frame *frame)
+{
+ struct ast_audiohook *audiohook = NULL;
+
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
+ ast_mutex_lock(&audiohook->lock);
+ /* If the audiohook wants to detach, let it go */
+ if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
+ AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
+ audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
+ ast_cond_signal(&audiohook->trigger);
+ ast_mutex_unlock(&audiohook->lock);
+ continue;
+ }
+ ast_mutex_unlock(&audiohook->lock);
+ }
+ AST_LIST_TRAVERSE_SAFE_END
+
+ return 0;
+}
+
+/*! \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;
+}
Propchange: team/file/ah/main/audiohook.c
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: team/file/ah/main/audiohook.c
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Propchange: team/file/ah/main/audiohook.c
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: team/file/ah/main/channel.c
URL: http://svn.digium.com/view/asterisk/team/file/ah/main/channel.c?view=diff&rev=58875&r1=58874&r2=58875
==============================================================================
--- team/file/ah/main/channel.c (original)
+++ team/file/ah/main/channel.c Tue Mar 13 18:44:52 2007
@@ -65,6 +65,7 @@
#include "asterisk/sha1.h"
#include "asterisk/threadstorage.h"
#include "asterisk/slinfactory.h"
+#include "asterisk/audiohook.h"
/* uncomment if you have problems with 'monitoring' synchronized files */
#if 0
@@ -1242,6 +1243,11 @@
/* Don't actually hang up a channel that will masquerade as someone else, or
if someone is going to masquerade as us */
ast_channel_lock(chan);
+
+ if (chan->audiohooks) {
+ ast_audiohook_detach_list(chan->audiohooks);
+ chan->audiohooks = NULL;
+ }
if (chan->masq) {
if (ast_do_masquerade(chan))
@@ -1908,6 +1914,9 @@
ast_frfree(f);
f = &ast_null_frame;
} else {
+ /* Send frame to audiohooks if present */
+ if (chan->audiohooks)
+ ast_audiohook_spy_write_frame(chan->audiohooks, AST_AUDIOHOOK_DIRECTION_READ, f);
if (chan->monitor && chan->monitor->read_stream ) {
/* XXX what does this do ? */
#ifndef MONITOR_CONSTANT_DELAY
@@ -2296,6 +2305,10 @@
case AST_FRAME_VOICE:
if (chan->tech->write == NULL)
break; /*! \todo XXX should return 0 maybe ? */
+
+ /* If audiohooks are present, write the frame out */
+ if (chan->audiohooks)
+ ast_audiohook_spy_write_frame(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)
@@ -3576,7 +3589,8 @@
(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) &&
[... 4 lines stripped ...]
More information about the svn-commits
mailing list