Asterisk - The Open Source Telephony Project GIT-master-35be64c
Loading...
Searching...
No Matches
Enumerations | Functions
stasis_app_snoop.h File Reference

Stasis Application Snoop API. See StasisApplication API" for detailed documentation. More...

#include "asterisk/stasis_app.h"
Include dependency graph for stasis_app_snoop.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  stasis_app_snoop_direction { STASIS_SNOOP_DIRECTION_NONE = 0 , STASIS_SNOOP_DIRECTION_OUT , STASIS_SNOOP_DIRECTION_IN , STASIS_SNOOP_DIRECTION_BOTH }
 Directions for audio stream flow. More...
 

Functions

struct ast_channel * stasis_app_control_snoop (struct ast_channel *chan, enum stasis_app_snoop_direction spy, enum stasis_app_snoop_direction whisper, const char *app, const char *app_args, const char *snoop_id)
 Create a snoop on the provided channel.
 

Detailed Description

Stasis Application Snoop API. See StasisApplication API" for detailed documentation.

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

Definition in file stasis_app_snoop.h.

Enumeration Type Documentation

◆ stasis_app_snoop_direction

Directions for audio stream flow.

Enumerator
STASIS_SNOOP_DIRECTION_NONE 

No direction.

STASIS_SNOOP_DIRECTION_OUT 

Audio stream out to the channel.

STASIS_SNOOP_DIRECTION_IN 

Audio stream in from the channel.

STASIS_SNOOP_DIRECTION_BOTH 

Audio stream to AND from the channel.

Definition at line 34 of file stasis_app_snoop.h.

34 {
35 /*! \brief No direction */
37 /*! \brief Audio stream out to the channel */
39 /*! \brief Audio stream in from the channel */
41 /*! \brief Audio stream to AND from the channel */
43};
@ STASIS_SNOOP_DIRECTION_IN
Audio stream in from the channel.
@ STASIS_SNOOP_DIRECTION_OUT
Audio stream out to the channel.
@ STASIS_SNOOP_DIRECTION_NONE
No direction.
@ STASIS_SNOOP_DIRECTION_BOTH
Audio stream to AND from the channel.

Function Documentation

◆ stasis_app_control_snoop()

struct ast_channel * stasis_app_control_snoop ( struct ast_channel *  chan,
enum stasis_app_snoop_direction  spy,
enum stasis_app_snoop_direction  whisper,
const char *  app,
const char *  app_args,
const char *  snoop_id 
)

Create a snoop on the provided channel.

Parameters
chanChannel to snoop on.
spyDirection of media that should be spied on.
whisperDirection of media that should be whispered into.
appStasis application to execute on the snoop channel.
app_argsStasis application arguments.
snoop_id
Returns
ast_channel ast_channel_unref() when done.
Return values
NULLif snoop channel couldn't be created.

Definition at line 304 of file res_stasis_snoop.c.

307{
308 RAII_VAR(struct stasis_app_snoop *, snoop, NULL, ao2_cleanup);
309 struct ast_format_cap *caps;
310 pthread_t thread;
311 struct ast_assigned_ids assignedids = {
312 .uniqueid = snoop_id,
313 };
314
315 if (spy == STASIS_SNOOP_DIRECTION_NONE &&
316 whisper == STASIS_SNOOP_DIRECTION_NONE) {
317 return NULL;
318 }
319
321 if (!snoop) {
322 return NULL;
323 }
324
325 /* Allocate a buffer to store the Stasis application and arguments in */
326 snoop->app = ast_str_create(64);
327 if (!snoop->app) {
328 return NULL;
329 }
330
331 ast_str_set(&snoop->app, 0, "%s", app);
332 if (!ast_strlen_zero(app_args)) {
333 ast_str_append(&snoop->app, 0, ",%s", app_args);
334 }
335
336 /* Set up a timer for the Snoop channel so it wakes up at a specific interval */
337 snoop->timer = ast_timer_open();
338 if (!snoop->timer) {
339 return NULL;
340 }
341 ast_timer_set_rate(snoop->timer, 1000 / SNOOP_INTERVAL);
342
343 /* Determine which signed linear format should be used */
344 snoop_determine_format(chan, snoop);
345
346 /* Allocate a Snoop channel and set up various parameters */
347 snoop->chan = ast_channel_alloc(1, AST_STATE_UP, "", "", "", "", "", &assignedids, NULL, 0, "Snoop/%s-%08x", ast_channel_uniqueid(chan),
348 (unsigned)ast_atomic_fetchadd_int((int *)&chan_idx, +1));
349 if (!snoop->chan) {
350 return NULL;
351 }
352
353 /* To keep the channel valid on the Snoop structure until it is destroyed we bump the ref up here */
354 ast_channel_ref(snoop->chan);
355 /* Prevent device state caching as this channel involves ephemeral destinations or sources */
357
358 ast_channel_tech_set(snoop->chan, &snoop_tech);
359 ao2_ref(snoop, +1);
360 ast_channel_tech_pvt_set(snoop->chan, snoop);
361 ast_channel_set_fd(snoop->chan, 0, ast_timer_fd(snoop->timer));
362
363 /* The format on the Snoop channel will be this signed linear format, and it will never change */
365 if (!caps) {
366 ast_channel_unlock(snoop->chan);
367 ast_hangup(snoop->chan);
368 return NULL;
369 }
370 ast_format_cap_append(caps, snoop->spy_format, 0);
371 ast_channel_nativeformats_set(snoop->chan, caps);
372 ao2_ref(caps, -1);
373
374 ast_channel_set_writeformat(snoop->chan, snoop->spy_format);
375 ast_channel_set_rawwriteformat(snoop->chan, snoop->spy_format);
376 ast_channel_set_readformat(snoop->chan, snoop->spy_format);
377 ast_channel_set_rawreadformat(snoop->chan, snoop->spy_format);
378
379 ast_channel_unlock(snoop->chan);
380
381 if (spy != STASIS_SNOOP_DIRECTION_NONE) {
382 if (snoop_setup_audiohook(chan, AST_AUDIOHOOK_TYPE_SPY, spy, &snoop->spy_direction, &snoop->spy)) {
383 ast_hangup(snoop->chan);
384 return NULL;
385 }
386
387 snoop->spy_samples = ast_format_get_sample_rate(snoop->spy_format) / (1000 / SNOOP_INTERVAL);
388 snoop->spy_active = 1;
389
390 snoop->silence.frametype = AST_FRAME_VOICE,
391 snoop->silence.datalen = snoop->spy_samples * sizeof(uint16_t),
392 snoop->silence.samples = snoop->spy_samples,
393 snoop->silence.mallocd = 0,
394 snoop->silence.offset = 0,
395 snoop->silence.src = __PRETTY_FUNCTION__,
396 snoop->silence.subclass.format = snoop->spy_format,
397 snoop->silence.data.ptr = ast_calloc(snoop->spy_samples, sizeof(uint16_t));
398 if (!snoop->silence.data.ptr) {
399 ast_hangup(snoop->chan);
400 return NULL;
401 }
402 }
403
404 /* If whispering is enabled set up the audiohook */
405 if (whisper != STASIS_SNOOP_DIRECTION_NONE) {
406 if (snoop_setup_audiohook(chan, AST_AUDIOHOOK_TYPE_WHISPER, whisper, &snoop->whisper_direction, &snoop->whisper)) {
407 ast_hangup(snoop->chan);
408 return NULL;
409 }
410
411 snoop->whisper_active = 1;
412 }
413
414 /* Create the thread which services the Snoop channel */
415 ao2_ref(snoop, +1);
417 ao2_cleanup(snoop);
418
419 /* No other thread is servicing this channel so we can immediately hang it up */
420 ast_hangup(snoop->chan);
421 return NULL;
422 }
423
424 /* Keep a reference to the channel we are spying on */
425 snoop->spyee_chan = ast_channel_ref(chan);
426
427 publish_chanspy_message(snoop, 1);
428
429 /* The caller of this has a reference as well */
430 return ast_channel_ref(snoop->chan);
431}
static const char app[]
pthread_t thread
Definition app_sla.c:335
#define ast_calloc(num, len)
A wrapper for calloc()
Definition astmm.h:202
@ AO2_ALLOC_OPT_LOCK_NOLOCK
Definition astobj2.h:367
#define ao2_cleanup(obj)
Definition astobj2.h:1934
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition astobj2.h:459
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition astobj2.h:404
@ AST_AUDIOHOOK_TYPE_SPY
Definition audiohook.h:36
@ AST_AUDIOHOOK_TYPE_WHISPER
Definition audiohook.h:37
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition channel.c:2540
#define ast_channel_alloc(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag,...)
Create a channel structure.
Definition channel.h:1299
void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
@ AST_FLAG_DISABLE_DEVSTATE_CACHE
Definition channel.h:1049
struct ast_flags * ast_channel_flags(struct ast_channel *chan)
#define ast_channel_ref(c)
Increase channel reference count.
Definition channel.h:3008
const char * ast_channel_uniqueid(const struct ast_channel *chan)
void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
void ast_channel_tech_pvt_set(struct ast_channel *chan, void *value)
void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
Definition channel.c:2417
void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
#define ast_channel_unlock(chan)
Definition channel.h:2984
void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
@ AST_STATE_UP
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition format.c:379
@ AST_FORMAT_CAP_FLAG_DEFAULT
Definition format_cap.h:38
#define ast_format_cap_append(cap, format, framing)
Add format capability to capabilities structure.
Definition format_cap.h:99
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition format_cap.h:49
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition lock.h:764
static void publish_chanspy_message(struct stasis_app_snoop *snoop, int start)
static int snoop_setup_audiohook(struct ast_channel *chan, enum ast_audiohook_type type, enum stasis_app_snoop_direction requested_direction, enum ast_audiohook_direction *direction, struct ast_audiohook *audiohook)
Internal helper function which sets up and attaches a snoop audiohook.
static void snoop_destroy(void *obj)
Destructor for snoop structure.
static void * snoop_stasis_thread(void *obj)
Thread used for running the Stasis application.
static struct ast_channel_tech snoop_tech
Channel interface declaration.
static void snoop_determine_format(struct ast_channel *chan, struct stasis_app_snoop *snoop)
Helper function which gets the format for a Snoop channel based on the channel being snooped on.
static unsigned int chan_idx
Index used to keep Snoop channel names unique.
#define SNOOP_INTERVAL
The interval (in milliseconds) that the Snoop timer is triggered, also controls length of audio withi...
#define NULL
Definition resample.c:96
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition strings.h:1139
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition strings.h:659
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition strings.h:1113
Structure to pass both assignedid values to channel drivers.
Definition channel.h:606
const char * uniqueid
Definition channel.h:607
Format capabilities structure, holds formats + preference order + etc.
Definition format_cap.c:54
Structure which contains all of the snoop information.
int ast_timer_set_rate(const struct ast_timer *handle, unsigned int rate)
Set the timing tick rate.
Definition timing.c:166
struct ast_timer * ast_timer_open(void)
Open a timer.
Definition timing.c:122
int ast_timer_fd(const struct ast_timer *handle)
Get a poll()-able file descriptor for a timer.
Definition timing.c:161
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition utils.h:981
#define ast_pthread_create_detached_background(a, b, c, d)
Definition utils.h:637
#define ast_set_flag(p, flag)
Definition utils.h:71

References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_alloc_options, ao2_cleanup, ao2_ref, app, ast_atomic_fetchadd_int(), AST_AUDIOHOOK_TYPE_SPY, AST_AUDIOHOOK_TYPE_WHISPER, ast_calloc, ast_channel_alloc, ast_channel_flags(), ast_channel_nativeformats_set(), ast_channel_ref, ast_channel_set_fd(), ast_channel_set_rawreadformat(), ast_channel_set_rawwriteformat(), ast_channel_set_readformat(), ast_channel_set_writeformat(), ast_channel_tech_pvt_set(), ast_channel_tech_set(), ast_channel_uniqueid(), ast_channel_unlock, AST_FLAG_DISABLE_DEVSTATE_CACHE, ast_format_cap_alloc, ast_format_cap_append, AST_FORMAT_CAP_FLAG_DEFAULT, ast_format_get_sample_rate(), AST_FRAME_VOICE, ast_hangup(), ast_pthread_create_detached_background, ast_set_flag, AST_STATE_UP, ast_str_append(), ast_str_create, ast_str_set(), ast_strlen_zero(), ast_timer_fd(), ast_timer_open(), ast_timer_set_rate(), chan_idx, NULL, publish_chanspy_message(), RAII_VAR, snoop_destroy(), snoop_determine_format(), SNOOP_INTERVAL, snoop_setup_audiohook(), snoop_stasis_thread(), snoop_tech, STASIS_SNOOP_DIRECTION_NONE, thread, and ast_assigned_ids::uniqueid.

Referenced by ari_channels_handle_snoop_channel().