ari: Add Snoop operation for spying/whispering on channels.
[asterisk/asterisk.git] / res / res_stasis_snoop.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Stasis application snoop control support.
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  */
25
26 /*** MODULEINFO
27         <depend type="module">res_stasis</depend>
28         <support_level>core</support_level>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/module.h"
36 #include "asterisk/stasis_app_impl.h"
37 #include "asterisk/stasis_app_snoop.h"
38 #include "asterisk/audiohook.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/timing.h"
41 #include "asterisk/stasis_channels.h"
42 #include "asterisk/json.h"
43
44 /*! \brief The interval (in milliseconds) that the Snoop timer is triggered, also controls length of audio within frames */
45 #define SNOOP_INTERVAL 20
46
47 /*! \brief Index used to keep Snoop channel names unique */
48 static unsigned int chan_idx;
49
50 /*! \brief Structure which contains all of the snoop information */
51 struct stasis_app_snoop {
52         /*! \brief Timer used for waking up Stasis thread */
53         struct ast_timer *timer;
54         /*! \brief Audiohook used to spy on the channel */
55         struct ast_audiohook spy;
56         /*! \brief Direction for spying */
57         enum ast_audiohook_direction spy_direction;
58         /*! \brief Number of samples to be read in when spying */
59         unsigned int spy_samples;
60         /*! \brief Format in use by the spy audiohook */
61         struct ast_format spy_format;
62         /*! \brief Audiohook used to whisper on the channel */
63         struct ast_audiohook whisper;
64         /*! \brief Direction for whispering */
65         enum ast_audiohook_direction whisper_direction;
66         /*! \brief Stasis application and arguments */
67         struct ast_str *app;
68         /*! \brief Snoop channel */
69         struct ast_channel *chan;
70         /*! \brief Whether the spy capability is active or not */
71         unsigned int spy_active:1;
72         /*! \brief Whether the whisper capability is active or not */
73         unsigned int whisper_active:1;
74         /*! \brief Uniqueid of the channel this snoop is snooping on */
75         char uniqueid[AST_MAX_UNIQUEID];
76 };
77
78 /*! \brief Destructor for snoop structure */
79 static void snoop_destroy(void *obj)
80 {
81         struct stasis_app_snoop *snoop = obj;
82
83         if (snoop->timer) {
84                 ast_timer_close(snoop->timer);
85         }
86
87         if (snoop->spy_active) {
88                 ast_audiohook_destroy(&snoop->spy);
89         }
90
91         if (snoop->whisper_active) {
92                 ast_audiohook_destroy(&snoop->whisper);
93         }
94
95         ast_free(snoop->app);
96
97         ast_channel_cleanup(snoop->chan);
98 }
99
100 /*! \internal
101  * \brief Publish the chanspy message over Stasis-Core
102  * \param snoop The snoop structure
103  * \start start If non-zero, the spying is starting. Otherwise, the spyer is
104  * finishing
105  */
106 static void publish_chanspy_message(struct stasis_app_snoop *snoop, int start)
107 {
108         RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
109         RAII_VAR(struct ast_multi_channel_blob *, payload, NULL, ao2_cleanup);
110         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
111         RAII_VAR(struct ast_channel_snapshot *, snoop_snapshot, NULL, ao2_cleanup);
112         RAII_VAR(struct ast_channel_snapshot *, spyee_snapshot, NULL, ao2_cleanup);
113
114         blob = ast_json_null();
115         if (!blob) {
116                 return;
117         }
118
119         payload = ast_multi_channel_blob_create(blob);
120         if (!payload) {
121                 return;
122         }
123
124         snoop_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(snoop->chan));
125         if (!snoop_snapshot) {
126                 return;
127         }
128         ast_multi_channel_blob_add_channel(payload, "spyer_channel", snoop_snapshot);
129
130         spyee_snapshot = ast_channel_snapshot_get_latest(snoop->uniqueid);
131         if (spyee_snapshot) {
132                 ast_multi_channel_blob_add_channel(payload, "spyee_channel", spyee_snapshot);
133         }
134
135         message = stasis_message_create(
136                         start ? ast_channel_chanspy_start_type(): ast_channel_chanspy_stop_type(),
137                                         payload);
138         if (!message) {
139                 return;
140         }
141
142         stasis_publish(ast_channel_topic(snoop->chan), message);
143 }
144
145 /*! \brief Callback function for writing to a Snoop whisper audiohook */
146 static int snoop_write(struct ast_channel *chan, struct ast_frame *frame)
147 {
148         struct stasis_app_snoop *snoop = ast_channel_tech_pvt(chan);
149
150         if (!snoop->whisper_active) {
151                 return 0;
152         }
153
154         ast_audiohook_lock(&snoop->whisper);
155         if (snoop->whisper_direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
156                 ast_audiohook_write_frame(&snoop->whisper, AST_AUDIOHOOK_DIRECTION_READ, frame);
157                 ast_audiohook_write_frame(&snoop->whisper, AST_AUDIOHOOK_DIRECTION_WRITE, frame);
158         } else {
159                 ast_audiohook_write_frame(&snoop->whisper, snoop->whisper_direction, frame);
160         }
161         ast_audiohook_unlock(&snoop->whisper);
162
163         return 0;
164 }
165
166 /*! \brief Callback function for reading from a Snoop channel */
167 static struct ast_frame *snoop_read(struct ast_channel *chan)
168 {
169         struct stasis_app_snoop *snoop = ast_channel_tech_pvt(chan);
170         struct ast_frame *frame = NULL;
171
172         /* If we fail to ack the timer OR if any active audiohooks are done hangup */
173         if ((ast_timer_ack(snoop->timer, 1) < 0) ||
174                 (snoop->spy_active && snoop->spy.status != AST_AUDIOHOOK_STATUS_RUNNING) ||
175                 (snoop->whisper_active && snoop->whisper.status != AST_AUDIOHOOK_STATUS_RUNNING)) {
176                 return NULL;
177         }
178
179         /* Only get audio from the spy audiohook if it is active */
180         if (snoop->spy_active) {
181                 ast_audiohook_lock(&snoop->spy);
182                 frame = ast_audiohook_read_frame(&snoop->spy, snoop->spy_samples, snoop->spy_direction, &snoop->spy_format);
183                 ast_audiohook_unlock(&snoop->spy);
184         }
185
186         return frame ? frame : &ast_null_frame;
187 }
188
189 /*! \brief Callback function for hanging up a Snoop channel */
190 static int snoop_hangup(struct ast_channel *chan)
191 {
192         struct stasis_app_snoop *snoop = ast_channel_tech_pvt(chan);
193
194         if (snoop->spy_active) {
195                 ast_audiohook_lock(&snoop->spy);
196                 ast_audiohook_detach(&snoop->spy);
197                 ast_audiohook_unlock(&snoop->spy);
198         }
199
200         if (snoop->whisper_active) {
201                 ast_audiohook_lock(&snoop->whisper);
202                 ast_audiohook_detach(&snoop->whisper);
203                 ast_audiohook_unlock(&snoop->whisper);
204         }
205
206         publish_chanspy_message(snoop, 0);
207
208         ao2_cleanup(snoop);
209
210         ast_channel_tech_pvt_set(chan, NULL);
211
212         return 0;
213 }
214
215 static int snoop_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
216 {
217         struct stasis_app_snoop *snoop = ast_channel_tech_pvt(oldchan);
218
219         if (snoop->chan != oldchan) {
220                 return -1;
221         }
222
223         ast_channel_unref(snoop->chan);
224         ast_channel_ref(newchan);
225         snoop->chan = newchan;
226
227         return 0;
228 }
229
230 /*! \brief Channel interface declaration */
231 static struct ast_channel_tech snoop_tech = {
232         .type = "Snoop",
233         .description = "Snoop Channel Driver",
234         .write = snoop_write,
235         .read = snoop_read,
236         .hangup = snoop_hangup,
237         .fixup = snoop_fixup,
238 };
239
240 /*! \brief Thread used for running the Stasis application */
241 static void *snoop_stasis_thread(void *obj)
242 {
243         RAII_VAR(struct stasis_app_snoop *, snoop, obj, ao2_cleanup);
244         struct ast_app *stasis = pbx_findapp("Stasis");
245
246         if (!stasis) {
247                 ast_hangup(snoop->chan);
248                 return NULL;
249         }
250
251         pbx_exec(snoop->chan, stasis, ast_str_buffer(snoop->app));
252
253         ast_hangup(snoop->chan);
254
255         return NULL;
256 }
257
258 /*! \brief Internal helper function which sets up and attaches a snoop audiohook */
259 static int snoop_setup_audiohook(struct ast_channel *chan, enum ast_audiohook_type type, enum stasis_app_snoop_direction requested_direction,
260         enum ast_audiohook_direction *direction, struct ast_audiohook *audiohook)
261 {
262         ast_audiohook_init(audiohook, type, "Snoop", 0);
263
264         if (requested_direction == STASIS_SNOOP_DIRECTION_OUT) {
265                 *direction = AST_AUDIOHOOK_DIRECTION_WRITE;
266         } else if (requested_direction == STASIS_SNOOP_DIRECTION_IN) {
267                 *direction = AST_AUDIOHOOK_DIRECTION_READ;
268         } else if (requested_direction == STASIS_SNOOP_DIRECTION_BOTH) {
269                 *direction = AST_AUDIOHOOK_DIRECTION_BOTH;
270         } else {
271                 return -1;
272         }
273
274         return ast_audiohook_attach(chan, audiohook);
275 }
276
277 struct ast_channel *stasis_app_control_snoop(struct ast_channel *chan,
278         enum stasis_app_snoop_direction spy, enum stasis_app_snoop_direction whisper,
279         const char *app, const char *app_args)
280 {
281         RAII_VAR(struct stasis_app_snoop *, snoop, NULL, ao2_cleanup);
282         unsigned int rate;
283         pthread_t thread;
284
285         if (spy == STASIS_SNOOP_DIRECTION_NONE &&
286                 whisper == STASIS_SNOOP_DIRECTION_NONE) {
287                 return NULL;
288         }
289
290         snoop = ao2_alloc_options(sizeof(*snoop), snoop_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
291         if (!snoop) {
292                 return NULL;
293         }
294
295         /* Allocate a buffer to store the Stasis application and arguments in */
296         snoop->app = ast_str_create(64);
297         if (!snoop->app) {
298                 return NULL;
299         }
300
301         ast_str_set(&snoop->app, 0, "%s", app);
302         if (!ast_strlen_zero(app_args)) {
303                 ast_str_append(&snoop->app, 0, ",%s", app_args);
304         }
305
306         /* Set up a timer for the Snoop channel so it wakes up at a specific interval */
307         snoop->timer = ast_timer_open();
308         if (!snoop->timer) {
309                 return NULL;
310         }
311         ast_timer_set_rate(snoop->timer, 1000 / SNOOP_INTERVAL);
312
313         /* Allocate a Snoop channel and set up various parameters */
314         snoop->chan = ast_channel_alloc(1, AST_STATE_UP, "", "", "", "", "", "", 0, "Snoop/%s-%08x", ast_channel_uniqueid(chan),
315                 ast_atomic_fetchadd_int((int *)&chan_idx, +1));
316         if (!snoop->chan) {
317                 return NULL;
318         }
319
320         strcpy(snoop->uniqueid, ast_channel_uniqueid(chan));
321
322         /* To keep the channel valid on the Snoop structure until it is destroyed we bump the ref up here */
323         ast_channel_ref(snoop->chan);
324
325         ast_channel_tech_set(snoop->chan, &snoop_tech);
326         ao2_ref(snoop, +1);
327         ast_channel_tech_pvt_set(snoop->chan, snoop);
328         ast_channel_set_fd(snoop->chan, 0, ast_timer_fd(snoop->timer));
329
330         {
331                 SCOPED_CHANNELLOCK(lock, chan);
332
333                 /* Determine the "best" signed linear format capable by the channel we are snooping on */
334                 rate = MAX(ast_format_rate(ast_channel_rawwriteformat(chan)), ast_format_rate(ast_channel_rawreadformat(chan)));
335                 ast_format_set(&snoop->spy_format, ast_format_slin_by_rate(rate), 0);
336
337                 /* The format on the Snoop channel will be this signed linear format, and it will never change */
338                 ast_format_cap_set(ast_channel_nativeformats(snoop->chan), &snoop->spy_format);
339                 ast_format_copy(ast_channel_writeformat(snoop->chan), &snoop->spy_format);
340                 ast_format_copy(ast_channel_rawwriteformat(snoop->chan), &snoop->spy_format);
341                 ast_format_copy(ast_channel_readformat(snoop->chan), &snoop->spy_format);
342                 ast_format_copy(ast_channel_rawreadformat(snoop->chan), &snoop->spy_format);
343
344                 if (spy != STASIS_SNOOP_DIRECTION_NONE) {
345                         if (snoop_setup_audiohook(chan, AST_AUDIOHOOK_TYPE_SPY, spy, &snoop->spy_direction, &snoop->spy)) {
346                                 ast_hangup(snoop->chan);
347                                 return NULL;
348                         }
349
350                         snoop->spy_samples = rate / (1000 / SNOOP_INTERVAL);
351                         snoop->spy_active = 1;
352                 }
353
354                 /* If whispering is enabled set up the audiohook */
355                 if (whisper != STASIS_SNOOP_DIRECTION_NONE) {
356                         if (snoop_setup_audiohook(chan, AST_AUDIOHOOK_TYPE_WHISPER, whisper, &snoop->whisper_direction, &snoop->whisper)) {
357                                 ast_hangup(snoop->chan);
358                                 return NULL;
359                         }
360
361                         snoop->whisper_active = 1;
362                 }
363         }
364
365         /* Create the thread which services the Snoop channel */
366         ao2_ref(snoop, +1);
367         if (ast_pthread_create_detached_background(&thread, NULL, snoop_stasis_thread, snoop)) {
368                 ao2_cleanup(snoop);
369
370                 /* No other thread is servicing this channel so we can immediately hang it up */
371                 ast_hangup(snoop->chan);
372                 return NULL;
373         }
374
375         publish_chanspy_message(snoop, 1);
376
377         /* The caller of this has a reference as well */
378         return ast_channel_ref(snoop->chan);
379 }
380
381 static int load_module(void)
382 {
383         return AST_MODULE_LOAD_SUCCESS;
384 }
385
386 static int unload_module(void)
387 {
388         return 0;
389 }
390
391 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Stasis application snoop support",
392         .load = load_module,
393         .unload = unload_module,
394         .nonoptreq = "res_stasis");