aboutsummaryrefslogtreecommitdiffstats
path: root/apps/app_chanspy.c
diff options
context:
space:
mode:
authorfile <file@f38db490-d61c-443f-a65b-d21fe96a405b>2007-08-08 19:30:52 +0000
committerfile <file@f38db490-d61c-443f-a65b-d21fe96a405b>2007-08-08 19:30:52 +0000
commitce30d7306b4db59ed13b13b1742b5a9f24141057 (patch)
treed8f49fe3111f2586e86ed3583f8a44a1b2444e3f /apps/app_chanspy.c
parent3472e5897e580edcc65e87ec21acefec81b12cd3 (diff)
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: http://svn.digium.com/svn/asterisk/trunk@78649 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'apps/app_chanspy.c')
-rw-r--r--apps/app_chanspy.c146
1 files changed, 37 insertions, 109 deletions
diff --git a/apps/app_chanspy.c b/apps/app_chanspy.c
index edfd5290d..bb1510bd2 100644
--- a/apps/app_chanspy.c
+++ b/apps/app_chanspy.c
@@ -40,7 +40,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#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,8 @@ AST_APP_OPTIONS(spy_opts, {
struct chanspy_translation_helper {
/* spy data */
- struct ast_channel_spy spy;
+ struct ast_audiohook spy_audiohook;
+ struct ast_audiohook whisper_audiohook;
int fd;
int volfactor;
};
@@ -185,15 +186,18 @@ static void spy_release(struct ast_channel *chan, void *data)
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_audiohook_lock(&csth->spy_audiohook);
+ if (csth->spy_audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) {
/* Channel is already gone more than likely */
+ ast_audiohook_unlock(&csth->spy_audiohook);
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->spy_audiohook, samples, AST_AUDIOHOOK_DIRECTION_BOTH, AST_FORMAT_SLINEAR);
+
+ ast_audiohook_unlock(&csth->spy_audiohook);
if (!f)
return 0;
@@ -217,16 +221,14 @@ static struct ast_generator spygen = {
.generate = spy_generate,
};
-static int start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_channel_spy *spy)
+static int start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_audiohook *audiohook)
{
- int res;
- struct ast_channel *peer;
+ 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);
@@ -234,35 +236,6 @@ static int start_spying(struct ast_channel *chan, struct ast_channel *spychan, s
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,
const struct ast_flags *flags, char *exitcontext)
{
@@ -280,49 +253,17 @@ static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int
ast_verb(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.spy_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.spy_audiohook)) {
+ ast_audiohook_destroy(&csth.spy_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);
+ ast_audiohook_init(&csth.whisper_audiohook, AST_AUDIOHOOK_TYPE_WHISPER, "ChanSpy");
+ start_spying(spyee, chan, &csth.whisper_audiohook);
}
if (ast_test_flag(flags, OPTION_PRIVATE))
@@ -344,21 +285,20 @@ static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int
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.spy_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);
+ if (ast_test_flag(flags, OPTION_WHISPER) && f->frametype == AST_FRAME_VOICE) {
+ ast_audiohook_lock(&csth.whisper_audiohook);
+ ast_audiohook_write_frame(&csth.whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f);
+ ast_audiohook_unlock(&csth.whisper_audiohook);
ast_frfree(f);
continue;
}
-
+
res = (f->frametype == AST_FRAME_DTMF) ? f->subclass : 0;
ast_frfree(f);
if (!res)
@@ -401,37 +341,25 @@ static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int
if (*volfactor > 4)
*volfactor = -4;
ast_verb(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);
- csth.spy.status = CHANSPY_DONE;
-
- /* If a channel still exists on our spy structure then we need to remove ourselves */
- if (csth.spy.chan) {
- ast_channel_lock(csth.spy.chan);
- ast_channel_spy_remove(csth.spy.chan, &csth.spy);
- ast_channel_unlock(csth.spy.chan);
+ if (ast_test_flag(flags, OPTION_WHISPER)) {
+ ast_audiohook_lock(&csth.whisper_audiohook);
+ ast_audiohook_detach(&csth.whisper_audiohook);
+ ast_audiohook_unlock(&csth.whisper_audiohook);
+ ast_audiohook_destroy(&csth.whisper_audiohook);
}
- ast_channel_spy_free(&csth.spy);
+
+ ast_audiohook_lock(&csth.spy_audiohook);
+ ast_audiohook_detach(&csth.spy_audiohook);
+ ast_audiohook_unlock(&csth.spy_audiohook);
+ ast_audiohook_destroy(&csth.spy_audiohook);
if (option_verbose >= 2)
ast_verbose(VERBOSE_PREFIX_2 "Done Spying on channel %s\n", name);