Asterisk media architecture conversion - no more format bitfields
[asterisk/asterisk.git] / funcs / func_frame_trace.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * David Vossel <dvossel@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 Trace internal ast_frames on a channel.
22  *
23  * \author David Vossel <dvossel@digium.com>
24  *
25  * \ingroup functions
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/module.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/framehook.h"
36
37 /*** DOCUMENTATION
38         <function name="FRAME_TRACE" language="en_US">
39                 <synopsis>
40                         View internal ast_frames as they are read and written on a channel.
41                 </synopsis>
42                 <syntax>
43                         <parameter name="filter list type" required="true">
44                                 <para>A filter can be applied to the trace to limit what frames are viewed.  This
45                                 filter can either be a <literal>white</literal> or <literal>black</literal> list
46                                 of frame types.  When no filter type is present, <literal>white</literal> is
47                                 used.  If no arguments are provided at all, all frames will be output.
48                                 </para>
49
50                                 <para>Below are the different types of frames that can be filtered.</para>
51                                 <enumlist>
52                                         <enum name = "DTMF_BEGIN" />
53                                         <enum name = "DTMF_END" />
54                                         <enum name = "VOICE" />
55                                         <enum name = "VIDEO" />
56                                         <enum name = "CONTROL" />
57                                         <enum name = "NULL" />
58                                         <enum name = "IAX" />
59                                         <enum name = "TEXT" />
60                                         <enum name = "IMAGE" />
61                                         <enum name = "HTML" />
62                                         <enum name = "CNG" />
63                                         <enum name = "MODEM" />
64                                 </enumlist>
65                         </parameter>
66                 </syntax>
67                 <description>
68                         <para>Examples:</para>
69                         <para>exten => 1,1,Set(FRAME_TRACE(white)=DTMF_BEGIN,DTMF_END); view only DTMF frames. </para>
70                         <para>exten => 1,1,Set(FRAME_TRACE()=DTMF_BEGIN,DTMF_END); view only DTMF frames. </para>
71                         <para>exten => 1,1,Set(FRAME_TRACE(black)=DTMF_BEGIN,DTMF_END); view everything except DTMF frames. </para>
72                 </description>
73         </function>
74  ***/
75
76 static void print_frame(struct ast_frame *frame);
77 static struct {
78         enum ast_frame_type type;
79         const char *str;
80 } frametype2str[] = {
81         { AST_FRAME_DTMF_BEGIN,   "DTMF_BEGIN" },
82         { AST_FRAME_DTMF_END,   "DTMF_END" },
83         { AST_FRAME_VOICE,   "VOICE" },
84         { AST_FRAME_VIDEO,   "VIDEO" },
85         { AST_FRAME_CONTROL,   "CONTROL" },
86         { AST_FRAME_NULL,   "NULL" },
87         { AST_FRAME_IAX,   "IAX" },
88         { AST_FRAME_TEXT,   "TEXT" },
89         { AST_FRAME_IMAGE,   "IMAGE" },
90         { AST_FRAME_HTML,   "HTML" },
91         { AST_FRAME_CNG,   "CNG" },
92         { AST_FRAME_MODEM,   "MODEM" },
93 };
94
95 struct frame_trace_data {
96         int list_type; /* 0 = white, 1 = black */
97         int values[ARRAY_LEN(frametype2str)];
98 };
99
100 static void datastore_destroy_cb(void *data) {
101         ast_free(data);
102 }
103
104 static const struct ast_datastore_info frame_trace_datastore = {
105         .type = "frametrace",
106         .destroy = datastore_destroy_cb
107 };
108
109 static void hook_destroy_cb(void *framedata)
110 {
111         ast_free(framedata);
112 }
113
114 static struct ast_frame *hook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
115 {
116         int i;
117         int show_frame = 0;
118         struct frame_trace_data *framedata = data;
119         if (!frame) {
120                 return frame;
121         }
122
123         if ((event != AST_FRAMEHOOK_EVENT_WRITE) && (event != AST_FRAMEHOOK_EVENT_READ)) {
124                 return frame;
125         }
126
127         for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
128                 if (frame->frametype == frametype2str[i].type) {
129                         if ((framedata->list_type == 0) && (framedata->values[i])) { /* white list */
130                                 show_frame = 1;
131                         } else if ((framedata->list_type == 1) && (!framedata->values[i])){ /* black list */
132                                 show_frame = 1;
133                         }
134                         break;
135                 }
136         }
137
138         if (show_frame) {
139                 ast_verbose("%s on Channel %s\n", event == AST_FRAMEHOOK_EVENT_READ ? "<--Read" : "--> Write", chan->name);
140                 print_frame(frame);
141         }
142         return frame;
143 }
144
145 static int frame_trace_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
146 {
147         struct frame_trace_data *framedata;
148         struct ast_datastore *datastore = NULL;
149         struct ast_framehook_interface interface = {
150                 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
151                 .event_cb = hook_event_cb,
152                 .destroy_cb = hook_destroy_cb,
153         };
154         int i = 0;
155
156         if (!(framedata = ast_calloc(1, sizeof(*framedata)))) {
157                 return 0;
158         }
159
160         interface.data = framedata;
161
162         if (!strcasecmp(data, "black")) {
163                 framedata->list_type = 1;
164         }
165         for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
166                 if (strcasestr(value, frametype2str[i].str)) {
167                         framedata->values[i] = 1;
168                 }
169         }
170
171         ast_channel_lock(chan);
172         i = ast_framehook_attach(chan, &interface);
173         if (i >= 0) {
174                 int *id;
175                 if ((datastore = ast_channel_datastore_find(chan, &frame_trace_datastore, NULL))) {
176                         id = datastore->data;
177                         ast_framehook_detach(chan, *id);
178                         ast_channel_datastore_remove(chan, datastore);
179                 }
180
181                 if (!(datastore = ast_datastore_alloc(&frame_trace_datastore, NULL))) {
182                         ast_framehook_detach(chan, i);
183                         ast_channel_unlock(chan);
184                         return 0;
185                 }
186
187                 if (!(id = ast_calloc(1, sizeof(int)))) {
188                         ast_datastore_free(datastore);
189                         ast_framehook_detach(chan, i);
190                         ast_channel_unlock(chan);
191                         return 0;
192                 }
193
194                 *id = i; /* Store off the id. The channel is still locked so it is safe to access this ptr. */
195                 datastore->data = id;
196                 ast_channel_datastore_add(chan, datastore);
197         }
198         ast_channel_unlock(chan);
199
200         return 0;
201 }
202
203 static void print_frame(struct ast_frame *frame)
204 {
205         switch (frame->frametype) {
206         case AST_FRAME_DTMF_END:
207                 ast_verbose("FrameType: DTMF END\n");
208                 ast_verbose("Digit: %d\n", frame->subclass.integer);
209                 break;
210         case AST_FRAME_VOICE:
211                 ast_verbose("FrameType: VOICE\n");
212                 ast_verbose("Codec: %s\n", ast_getformatname(&frame->subclass.format));
213                 ast_verbose("MS: %ld\n", frame->len);
214                 ast_verbose("Samples: %d\n", frame->samples);
215                 ast_verbose("Bytes: %d\n", frame->datalen);
216                 break;
217         case AST_FRAME_VIDEO:
218                 ast_verbose("FrameType: VIDEO\n");
219                 ast_verbose("Codec: %s\n", ast_getformatname(&frame->subclass.format));
220                 ast_verbose("MS: %ld\n", frame->len);
221                 ast_verbose("Samples: %d\n", frame->samples);
222                 ast_verbose("Bytes: %d\n", frame->datalen);
223                 break;
224         case AST_FRAME_CONTROL:
225                 ast_verbose("FrameType: CONTROL\n");
226                 switch (frame->subclass.integer) {
227                 case AST_CONTROL_HANGUP:
228                         ast_verbose("SubClass: HANGUP\n");
229                         break;
230                 case AST_CONTROL_RING:
231                         ast_verbose("SubClass: RING\n");
232                         break;
233                 case AST_CONTROL_RINGING:
234                         ast_verbose("SubClass: RINGING\n");
235                         break;
236                 case AST_CONTROL_ANSWER:
237                         ast_verbose("SubClass: ANSWER\n");
238                         break;
239                 case AST_CONTROL_BUSY:
240                         ast_verbose("SubClass: BUSY\n");
241                         break;
242                 case AST_CONTROL_TAKEOFFHOOK:
243                         ast_verbose("SubClass: TAKEOFFHOOK\n");
244                         break;
245                 case AST_CONTROL_OFFHOOK:
246                         ast_verbose("SubClass: OFFHOOK\n");
247                         break;
248                 case AST_CONTROL_CONGESTION:
249                         ast_verbose("SubClass: CONGESTION\n");
250                         break;
251                 case AST_CONTROL_FLASH:
252                         ast_verbose("SubClass: FLASH\n");
253                         break;
254                 case AST_CONTROL_WINK:
255                         ast_verbose("SubClass: WINK\n");
256                         break;
257                 case AST_CONTROL_OPTION:
258                         ast_verbose("SubClass: OPTION\n");
259                         break;
260                 case AST_CONTROL_RADIO_KEY:
261                         ast_verbose("SubClass: RADIO KEY\n");
262                         break;
263                 case AST_CONTROL_RADIO_UNKEY:
264                         ast_verbose("SubClass: RADIO UNKEY\n");
265                         break;
266                 case AST_CONTROL_PROGRESS:
267                         ast_verbose("SubClass: PROGRESS\n");
268                         break;
269                 case AST_CONTROL_PROCEEDING:
270                         ast_verbose("SubClass: PROCEEDING\n");
271                         break;
272                 case AST_CONTROL_HOLD:
273                         ast_verbose("SubClass: HOLD\n");
274                         break;
275                 case AST_CONTROL_UNHOLD:
276                         ast_verbose("SubClass: UNHOLD\n");
277                         break;
278                 case AST_CONTROL_VIDUPDATE:
279                         ast_verbose("SubClass: VIDUPDATE\n");
280                         break;
281                 case _XXX_AST_CONTROL_T38:
282                         ast_verbose("SubClass: XXX T38\n");
283                         break;
284                 case AST_CONTROL_SRCUPDATE:
285                         ast_verbose("SubClass: SRCUPDATE\n");
286                         break;
287                 case AST_CONTROL_TRANSFER:
288                         ast_verbose("SubClass: TRANSFER\n");
289                         break;
290                 case AST_CONTROL_CONNECTED_LINE:
291                         ast_verbose("SubClass: CONNECTED LINE\n");
292                         break;
293                 case AST_CONTROL_REDIRECTING:
294                         ast_verbose("SubClass: REDIRECTING\n");
295                         break;
296                 case AST_CONTROL_T38_PARAMETERS:
297                         ast_verbose("SubClass: T38 PARAMETERS\n");
298                         break;
299                 case AST_CONTROL_CC:
300                         ast_verbose("SubClass: CC\n");
301                         break;
302                 case AST_CONTROL_SRCCHANGE:
303                         ast_verbose("SubClass: SRCCHANGE\n");
304                         break;
305                 case AST_CONTROL_READ_ACTION:
306                         ast_verbose("SubClass: READ ACTION\n");
307                         break;
308                 case AST_CONTROL_AOC:
309                         ast_verbose("SubClass: AOC\n");
310                         break;
311                 }
312                 if (frame->subclass.integer == -1) {
313                         ast_verbose("SubClass: %d\n", frame->subclass.integer);
314                 }
315                 ast_verbose("Bytes: %d\n", frame->datalen);
316                 break;
317         case AST_FRAME_NULL:
318                 ast_verbose("FrameType: NULL\n");
319                 break;
320         case AST_FRAME_IAX:
321                 ast_verbose("FrameType: IAX\n");
322                 break;
323         case AST_FRAME_TEXT:
324                 ast_verbose("FrameType: TXT\n");
325                 break;
326         case AST_FRAME_IMAGE:
327                 ast_verbose("FrameType: IMAGE\n");
328                 break;
329         case AST_FRAME_HTML:
330                 ast_verbose("FrameType: HTML\n");
331                 break;
332         case AST_FRAME_CNG:
333                 ast_verbose("FrameType: CNG\n");
334                 break;
335         case AST_FRAME_MODEM:
336                 ast_verbose("FrameType: MODEM\n");
337                 break;
338         case AST_FRAME_DTMF_BEGIN:
339                 ast_verbose("FrameType: DTMF BEGIN\n");
340                 ast_verbose("Digit: %d\n", frame->subclass.integer);
341                 break;
342         }
343
344         ast_verbose("Src: %s\n", ast_strlen_zero(frame->src) ? "NOT PRESENT" : frame->src);
345         ast_verbose("\n");
346 }
347
348 static struct ast_custom_function frame_trace_function = {
349         .name = "FRAME_TRACE",
350         .write = frame_trace_helper,
351 };
352
353 static int unload_module(void)
354 {
355         return ast_custom_function_unregister(&frame_trace_function);
356 }
357
358 static int load_module(void)
359 {
360         int res = ast_custom_function_register(&frame_trace_function);
361         return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
362 }
363
364 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Frame Trace for internal ast_frame debugging.");
365