frame.c: Copy the whole subclass in ast_frdup().
[asterisk/asterisk.git] / main / frame.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@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 Frame and codec manipulation routines
22  *
23  * \author Mark Spencer <markster@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_REGISTER_FILE()
33
34 #include "asterisk/_private.h"
35 #include "asterisk/lock.h"
36 #include "asterisk/frame.h"
37 #include "asterisk/format_cache.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/cli.h"
40 #include "asterisk/term.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/threadstorage.h"
43 #include "asterisk/linkedlists.h"
44 #include "asterisk/translate.h"
45 #include "asterisk/dsp.h"
46 #include "asterisk/file.h"
47
48 #if !defined(LOW_MEMORY)
49 static void frame_cache_cleanup(void *data);
50
51 /*! \brief A per-thread cache of frame headers */
52 AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup);
53
54 /*!
55  * \brief Maximum ast_frame cache size
56  *
57  * In most cases where the frame header cache will be useful, the size
58  * of the cache will stay very small.  However, it is not always the case that
59  * the same thread that allocates the frame will be the one freeing them, so
60  * sometimes a thread will never have any frames in its cache, or the cache
61  * will never be pulled from.  For the latter case, we limit the maximum size.
62  */
63 #define FRAME_CACHE_MAX_SIZE    10
64
65 /*! \brief This is just so ast_frames, a list head struct for holding a list of
66  *  ast_frame structures, is defined. */
67 AST_LIST_HEAD_NOLOCK(ast_frames, ast_frame);
68
69 struct ast_frame_cache {
70         struct ast_frames list;
71         size_t size;
72 };
73 #endif
74
75 struct ast_frame ast_null_frame = { AST_FRAME_NULL, };
76
77 static struct ast_frame *ast_frame_header_new(void)
78 {
79         struct ast_frame *f;
80
81 #if !defined(LOW_MEMORY)
82         struct ast_frame_cache *frames;
83
84         if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
85                 if ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list))) {
86                         size_t mallocd_len = f->mallocd_hdr_len;
87                         memset(f, 0, sizeof(*f));
88                         f->mallocd_hdr_len = mallocd_len;
89                         f->mallocd = AST_MALLOCD_HDR;
90                         frames->size--;
91                         return f;
92                 }
93         }
94         if (!(f = ast_calloc_cache(1, sizeof(*f))))
95                 return NULL;
96 #else
97         if (!(f = ast_calloc(1, sizeof(*f))))
98                 return NULL;
99 #endif
100
101         f->mallocd_hdr_len = sizeof(*f);
102
103         return f;
104 }
105
106 #if !defined(LOW_MEMORY)
107 static void frame_cache_cleanup(void *data)
108 {
109         struct ast_frame_cache *frames = data;
110         struct ast_frame *f;
111
112         while ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list)))
113                 ast_free(f);
114
115         ast_free(frames);
116 }
117 #endif
118
119 static void __frame_free(struct ast_frame *fr, int cache)
120 {
121         if (!fr->mallocd)
122                 return;
123
124 #if !defined(LOW_MEMORY)
125         if (cache && fr->mallocd == AST_MALLOCD_HDR) {
126                 /* Cool, only the header is malloc'd, let's just cache those for now
127                  * to keep things simple... */
128                 struct ast_frame_cache *frames;
129                 if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames))) &&
130                     (frames->size < FRAME_CACHE_MAX_SIZE)) {
131                         if ((fr->frametype == AST_FRAME_VOICE) || (fr->frametype == AST_FRAME_VIDEO) ||
132                                 (fr->frametype == AST_FRAME_IMAGE)) {
133                                 ao2_cleanup(fr->subclass.format);
134                         }
135
136                         AST_LIST_INSERT_HEAD(&frames->list, fr, frame_list);
137                         frames->size++;
138                         return;
139                 }
140         }
141 #endif
142
143         if (fr->mallocd & AST_MALLOCD_DATA) {
144                 if (fr->data.ptr)
145                         ast_free(fr->data.ptr - fr->offset);
146         }
147         if (fr->mallocd & AST_MALLOCD_SRC) {
148                 if (fr->src)
149                         ast_free((void *) fr->src);
150         }
151         if (fr->mallocd & AST_MALLOCD_HDR) {
152                 if ((fr->frametype == AST_FRAME_VOICE) || (fr->frametype == AST_FRAME_VIDEO) ||
153                         (fr->frametype == AST_FRAME_IMAGE)) {
154                         ao2_cleanup(fr->subclass.format);
155                 }
156
157                 ast_free(fr);
158         } else {
159                 fr->mallocd = 0;
160         }
161 }
162
163
164 void ast_frame_free(struct ast_frame *frame, int cache)
165 {
166         struct ast_frame *next;
167
168         for (next = AST_LIST_NEXT(frame, frame_list);
169              frame;
170              frame = next, next = frame ? AST_LIST_NEXT(frame, frame_list) : NULL) {
171                 __frame_free(frame, cache);
172         }
173 }
174
175 void ast_frame_dtor(struct ast_frame *f)
176 {
177         if (f) {
178                 ast_frfree(f);
179         }
180 }
181
182 /*!
183  * \brief 'isolates' a frame by duplicating non-malloc'ed components
184  * (header, src, data).
185  * On return all components are malloc'ed
186  */
187 struct ast_frame *ast_frisolate(struct ast_frame *fr)
188 {
189         struct ast_frame *out;
190         void *newdata;
191
192         /* if none of the existing frame is malloc'd, let ast_frdup() do it
193            since it is more efficient
194         */
195         if (fr->mallocd == 0) {
196                 return ast_frdup(fr);
197         }
198
199         /* if everything is already malloc'd, we are done */
200         if ((fr->mallocd & (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) ==
201             (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) {
202                 return fr;
203         }
204
205         if (!(fr->mallocd & AST_MALLOCD_HDR)) {
206                 /* Allocate a new header if needed */
207                 if (!(out = ast_frame_header_new())) {
208                         return NULL;
209                 }
210                 out->frametype = fr->frametype;
211                 if ((fr->frametype == AST_FRAME_VOICE) || (fr->frametype == AST_FRAME_VIDEO) ||
212                         (fr->frametype == AST_FRAME_IMAGE)) {
213                         out->subclass.format = ao2_bump(fr->subclass.format);
214                 } else {
215                         memcpy(&out->subclass, &fr->subclass, sizeof(out->subclass));
216                 }
217                 out->datalen = fr->datalen;
218                 out->samples = fr->samples;
219                 out->offset = fr->offset;
220                 /* Copy the timing data */
221                 ast_copy_flags(out, fr, AST_FLAGS_ALL);
222                 if (ast_test_flag(fr, AST_FRFLAG_HAS_TIMING_INFO)) {
223                         out->ts = fr->ts;
224                         out->len = fr->len;
225                         out->seqno = fr->seqno;
226                 }
227         } else {
228                 out = fr;
229         }
230
231         if (!(fr->mallocd & AST_MALLOCD_SRC) && fr->src) {
232                 if (!(out->src = ast_strdup(fr->src))) {
233                         if (out != fr) {
234                                 ast_free(out);
235                         }
236                         return NULL;
237                 }
238         } else {
239                 out->src = fr->src;
240                 fr->src = NULL;
241                 fr->mallocd &= ~AST_MALLOCD_SRC;
242         }
243
244         if (!(fr->mallocd & AST_MALLOCD_DATA))  {
245                 if (!fr->datalen) {
246                         out->data.uint32 = fr->data.uint32;
247                         out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC;
248                         return out;
249                 }
250                 if (!(newdata = ast_malloc(fr->datalen + AST_FRIENDLY_OFFSET))) {
251                         if (out->src != fr->src) {
252                                 ast_free((void *) out->src);
253                         }
254                         if (out != fr) {
255                                 ast_free(out);
256                         }
257                         return NULL;
258                 }
259                 newdata += AST_FRIENDLY_OFFSET;
260                 out->offset = AST_FRIENDLY_OFFSET;
261                 out->datalen = fr->datalen;
262                 memcpy(newdata, fr->data.ptr, fr->datalen);
263                 out->data.ptr = newdata;
264         } else {
265                 out->data = fr->data;
266                 memset(&fr->data, 0, sizeof(fr->data));
267                 fr->mallocd &= ~AST_MALLOCD_DATA;
268         }
269
270         out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
271
272         return out;
273 }
274
275 struct ast_frame *ast_frdup(const struct ast_frame *f)
276 {
277         struct ast_frame *out = NULL;
278         int len, srclen = 0;
279         void *buf = NULL;
280
281 #if !defined(LOW_MEMORY)
282         struct ast_frame_cache *frames;
283 #endif
284
285         /* Start with standard stuff */
286         len = sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
287         /* If we have a source, add space for it */
288         /*
289          * XXX Watch out here - if we receive a src which is not terminated
290          * properly, we can be easily attacked. Should limit the size we deal with.
291          */
292         if (f->src)
293                 srclen = strlen(f->src);
294         if (srclen > 0)
295                 len += srclen + 1;
296
297 #if !defined(LOW_MEMORY)
298         if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
299                 AST_LIST_TRAVERSE_SAFE_BEGIN(&frames->list, out, frame_list) {
300                         if (out->mallocd_hdr_len >= len) {
301                                 size_t mallocd_len = out->mallocd_hdr_len;
302
303                                 AST_LIST_REMOVE_CURRENT(frame_list);
304                                 memset(out, 0, sizeof(*out));
305                                 out->mallocd_hdr_len = mallocd_len;
306                                 buf = out;
307                                 frames->size--;
308                                 break;
309                         }
310                 }
311                 AST_LIST_TRAVERSE_SAFE_END;
312         }
313 #endif
314
315         if (!buf) {
316                 if (!(buf = ast_calloc_cache(1, len)))
317                         return NULL;
318                 out = buf;
319                 out->mallocd_hdr_len = len;
320         }
321
322         out->frametype = f->frametype;
323         out->subclass = f->subclass;
324         if ((f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_VIDEO) ||
325                 (f->frametype == AST_FRAME_IMAGE)) {
326                 ao2_bump(out->subclass.format);
327         }
328         out->datalen = f->datalen;
329         out->samples = f->samples;
330         out->delivery = f->delivery;
331         /* Even though this new frame was allocated from the heap, we can't mark it
332          * with AST_MALLOCD_HDR, AST_MALLOCD_DATA and AST_MALLOCD_SRC, because that
333          * would cause ast_frfree() to attempt to individually free each of those
334          * under the assumption that they were separately allocated. Since this frame
335          * was allocated in a single allocation, we'll only mark it as if the header
336          * was heap-allocated; this will result in the entire frame being properly freed.
337          */
338         out->mallocd = AST_MALLOCD_HDR;
339         out->offset = AST_FRIENDLY_OFFSET;
340         if (out->datalen) {
341                 out->data.ptr = buf + sizeof(*out) + AST_FRIENDLY_OFFSET;
342                 memcpy(out->data.ptr, f->data.ptr, out->datalen);
343         } else {
344                 out->data.uint32 = f->data.uint32;
345         }
346         if (srclen > 0) {
347                 /* This may seem a little strange, but it's to avoid a gcc (4.2.4) compiler warning */
348                 char *src;
349                 out->src = buf + sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
350                 src = (char *) out->src;
351                 /* Must have space since we allocated for it */
352                 strcpy(src, f->src);
353         }
354         ast_copy_flags(out, f, AST_FLAGS_ALL);
355         out->ts = f->ts;
356         out->len = f->len;
357         out->seqno = f->seqno;
358         return out;
359 }
360
361 void ast_swapcopy_samples(void *dst, const void *src, int samples)
362 {
363         int i;
364         unsigned short *dst_s = dst;
365         const unsigned short *src_s = src;
366
367         for (i = 0; i < samples; i++)
368                 dst_s[i] = (src_s[i]<<8) | (src_s[i]>>8);
369 }
370
371 void ast_frame_subclass2str(struct ast_frame *f, char *subclass, size_t slen, char *moreinfo, size_t mlen)
372 {
373         switch(f->frametype) {
374         case AST_FRAME_DTMF_BEGIN:
375                 if (slen > 1) {
376                         subclass[0] = f->subclass.integer;
377                         subclass[1] = '\0';
378                 }
379                 break;
380         case AST_FRAME_DTMF_END:
381                 if (slen > 1) {
382                         subclass[0] = f->subclass.integer;
383                         subclass[1] = '\0';
384                 }
385                 break;
386         case AST_FRAME_CONTROL:
387                 switch (f->subclass.integer) {
388                 case AST_CONTROL_HANGUP:
389                         ast_copy_string(subclass, "Hangup", slen);
390                         break;
391                 case AST_CONTROL_RING:
392                         ast_copy_string(subclass, "Ring", slen);
393                         break;
394                 case AST_CONTROL_RINGING:
395                         ast_copy_string(subclass, "Ringing", slen);
396                         break;
397                 case AST_CONTROL_ANSWER:
398                         ast_copy_string(subclass, "Answer", slen);
399                         break;
400                 case AST_CONTROL_BUSY:
401                         ast_copy_string(subclass, "Busy", slen);
402                         break;
403                 case AST_CONTROL_TAKEOFFHOOK:
404                         ast_copy_string(subclass, "Take Off Hook", slen);
405                         break;
406                 case AST_CONTROL_OFFHOOK:
407                         ast_copy_string(subclass, "Line Off Hook", slen);
408                         break;
409                 case AST_CONTROL_CONGESTION:
410                         ast_copy_string(subclass, "Congestion", slen);
411                         break;
412                 case AST_CONTROL_FLASH:
413                         ast_copy_string(subclass, "Flash", slen);
414                         break;
415                 case AST_CONTROL_WINK:
416                         ast_copy_string(subclass, "Wink", slen);
417                         break;
418                 case AST_CONTROL_OPTION:
419                         ast_copy_string(subclass, "Option", slen);
420                         break;
421                 case AST_CONTROL_RADIO_KEY:
422                         ast_copy_string(subclass, "Key Radio", slen);
423                         break;
424                 case AST_CONTROL_RADIO_UNKEY:
425                         ast_copy_string(subclass, "Unkey Radio", slen);
426                         break;
427                 case AST_CONTROL_HOLD:
428                         ast_copy_string(subclass, "Hold", slen);
429                         break;
430                 case AST_CONTROL_UNHOLD:
431                         ast_copy_string(subclass, "Unhold", slen);
432                         break;
433                 case AST_CONTROL_T38_PARAMETERS: {
434                         char *message = "Unknown";
435                         if (f->datalen != sizeof(struct ast_control_t38_parameters)) {
436                                 message = "Invalid";
437                         } else {
438                                 struct ast_control_t38_parameters *parameters = f->data.ptr;
439                                 enum ast_control_t38 state = parameters->request_response;
440                                 if (state == AST_T38_REQUEST_NEGOTIATE)
441                                         message = "Negotiation Requested";
442                                 else if (state == AST_T38_REQUEST_TERMINATE)
443                                         message = "Negotiation Request Terminated";
444                                 else if (state == AST_T38_NEGOTIATED)
445                                         message = "Negotiated";
446                                 else if (state == AST_T38_TERMINATED)
447                                         message = "Terminated";
448                                 else if (state == AST_T38_REFUSED)
449                                         message = "Refused";
450                         }
451                         snprintf(subclass, slen, "T38_Parameters/%s", message);
452                         break;
453                 }
454                 case -1:
455                         ast_copy_string(subclass, "Stop generators", slen);
456                         break;
457                 default:
458                         snprintf(subclass, slen, "Unknown control '%d'", f->subclass.integer);
459                 }
460                 break;
461         case AST_FRAME_NULL:
462                 ast_copy_string(subclass, "N/A", slen);
463                 break;
464         case AST_FRAME_IAX:
465                 /* Should never happen */
466                 snprintf(subclass, slen, "IAX Frametype %d", f->subclass.integer);
467                 break;
468         case AST_FRAME_BRIDGE_ACTION:
469                 /* Should never happen */
470                 snprintf(subclass, slen, "Bridge Frametype %d", f->subclass.integer);
471                 break;
472         case AST_FRAME_BRIDGE_ACTION_SYNC:
473                 /* Should never happen */
474                 snprintf(subclass, slen, "Synchronous Bridge Frametype %d", f->subclass.integer);
475                 break;
476         case AST_FRAME_TEXT:
477                 ast_copy_string(subclass, "N/A", slen);
478                 if (moreinfo) {
479                         ast_copy_string(moreinfo, f->data.ptr, mlen);
480                 }
481                 break;
482         case AST_FRAME_IMAGE:
483                 snprintf(subclass, slen, "Image format %s\n", ast_format_get_name(f->subclass.format));
484                 break;
485         case AST_FRAME_HTML:
486                 switch (f->subclass.integer) {
487                 case AST_HTML_URL:
488                         ast_copy_string(subclass, "URL", slen);
489                         if (moreinfo) {
490                                 ast_copy_string(moreinfo, f->data.ptr, mlen);
491                         }
492                         break;
493                 case AST_HTML_DATA:
494                         ast_copy_string(subclass, "Data", slen);
495                         break;
496                 case AST_HTML_BEGIN:
497                         ast_copy_string(subclass, "Begin", slen);
498                         break;
499                 case AST_HTML_END:
500                         ast_copy_string(subclass, "End", slen);
501                         break;
502                 case AST_HTML_LDCOMPLETE:
503                         ast_copy_string(subclass, "Load Complete", slen);
504                         break;
505                 case AST_HTML_NOSUPPORT:
506                         ast_copy_string(subclass, "No Support", slen);
507                         break;
508                 case AST_HTML_LINKURL:
509                         ast_copy_string(subclass, "Link URL", slen);
510                         if (moreinfo) {
511                                 ast_copy_string(moreinfo, f->data.ptr, mlen);
512                         }
513                         break;
514                 case AST_HTML_UNLINK:
515                         ast_copy_string(subclass, "Unlink", slen);
516                         break;
517                 case AST_HTML_LINKREJECT:
518                         ast_copy_string(subclass, "Link Reject", slen);
519                         break;
520                 default:
521                         snprintf(subclass, slen, "Unknown HTML frame '%d'\n", f->subclass.integer);
522                         break;
523                 }
524                 break;
525         case AST_FRAME_MODEM:
526                 switch (f->subclass.integer) {
527                 case AST_MODEM_T38:
528                         ast_copy_string(subclass, "T.38", slen);
529                         break;
530                 case AST_MODEM_V150:
531                         ast_copy_string(subclass, "V.150", slen);
532                         break;
533                 default:
534                         snprintf(subclass, slen, "Unknown MODEM frame '%d'\n", f->subclass.integer);
535                         break;
536                 }
537                 break;
538         default:
539                 ast_copy_string(subclass, "Unknown Subclass", slen);
540                 break;
541         }
542 }
543
544 void ast_frame_type2str(enum ast_frame_type frame_type, char *ftype, size_t len)
545 {
546         switch (frame_type) {
547         case AST_FRAME_DTMF_BEGIN:
548                 ast_copy_string(ftype, "DTMF Begin", len);
549                 break;
550         case AST_FRAME_DTMF_END:
551                 ast_copy_string(ftype, "DTMF End", len);
552                 break;
553         case AST_FRAME_CONTROL:
554                 ast_copy_string(ftype, "Control", len);
555                 break;
556         case AST_FRAME_NULL:
557                 ast_copy_string(ftype, "Null Frame", len);
558                 break;
559         case AST_FRAME_IAX:
560                 /* Should never happen */
561                 ast_copy_string(ftype, "IAX Specific", len);
562                 break;
563         case AST_FRAME_BRIDGE_ACTION:
564                 /* Should never happen */
565                 ast_copy_string(ftype, "Bridge Specific", len);
566                 break;
567         case AST_FRAME_BRIDGE_ACTION_SYNC:
568                 /* Should never happen */
569                 ast_copy_string(ftype, "Bridge Specific", len);
570                 break;
571         case AST_FRAME_TEXT:
572                 ast_copy_string(ftype, "Text", len);
573                 break;
574         case AST_FRAME_IMAGE:
575                 ast_copy_string(ftype, "Image", len);
576                 break;
577         case AST_FRAME_HTML:
578                 ast_copy_string(ftype, "HTML", len);
579                 break;
580         case AST_FRAME_MODEM:
581                 ast_copy_string(ftype, "Modem", len);
582                 break;
583         case AST_FRAME_VOICE:
584                 ast_copy_string(ftype, "Voice", len);
585                 break;
586         case AST_FRAME_VIDEO:
587                 ast_copy_string(ftype, "Video", len);
588                 break;
589         default:
590                 snprintf(ftype, len, "Unknown Frametype '%u'", frame_type);
591                 break;
592         }
593 }
594
595 /*! Dump a frame for debugging purposes */
596 void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix)
597 {
598         const char noname[] = "unknown";
599         char ftype[40] = "Unknown Frametype";
600         char cft[80];
601         char subclass[40] = "Unknown Subclass";
602         char csub[80];
603         char moreinfo[40] = "";
604         char cn[60];
605         char cp[40];
606         char cmn[40];
607
608         if (!name) {
609                 name = noname;
610         }
611
612         if (!f) {
613                 ast_verb(-1, "%s [ %s (NULL) ] [%s]\n",
614                         term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
615                         term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
616                         term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
617                 return;
618         }
619         /* XXX We should probably print one each of voice and video when the format changes XXX */
620         if (f->frametype == AST_FRAME_VOICE) {
621                 return;
622         }
623         if (f->frametype == AST_FRAME_VIDEO) {
624                 return;
625         }
626
627         ast_frame_type2str(f->frametype, ftype, sizeof(ftype));
628         ast_frame_subclass2str(f, subclass, sizeof(subclass), moreinfo, sizeof(moreinfo));
629
630         if (!ast_strlen_zero(moreinfo))
631                 ast_verb(-1, "%s [ TYPE: %s (%u) SUBCLASS: %s (%d) '%s' ] [%s]\n",
632                             term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
633                             term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
634                             f->frametype,
635                             term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
636                             f->subclass.integer,
637                             term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)),
638                             term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
639         else
640                 ast_verb(-1, "%s [ TYPE: %s (%u) SUBCLASS: %s (%d) ] [%s]\n",
641                             term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
642                             term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
643                             f->frametype,
644                             term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
645                             f->subclass.integer,
646                             term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
647 }
648
649 int ast_frame_adjust_volume(struct ast_frame *f, int adjustment)
650 {
651         int count;
652         short *fdata = f->data.ptr;
653         short adjust_value = abs(adjustment);
654
655         if ((f->frametype != AST_FRAME_VOICE) || !(ast_format_cache_is_slinear(f->subclass.format))) {
656                 return -1;
657         }
658
659         if (!adjustment) {
660                 return 0;
661         }
662
663         for (count = 0; count < f->samples; count++) {
664                 if (adjustment > 0) {
665                         ast_slinear_saturated_multiply(&fdata[count], &adjust_value);
666                 } else if (adjustment < 0) {
667                         ast_slinear_saturated_divide(&fdata[count], &adjust_value);
668                 }
669         }
670
671         return 0;
672 }
673
674 int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2)
675 {
676         int count;
677         short *data1, *data2;
678
679         if ((f1->frametype != AST_FRAME_VOICE) || (ast_format_cmp(f1->subclass.format, ast_format_slin) != AST_FORMAT_CMP_NOT_EQUAL))
680                 return -1;
681
682         if ((f2->frametype != AST_FRAME_VOICE) || (ast_format_cmp(f2->subclass.format, ast_format_slin) != AST_FORMAT_CMP_NOT_EQUAL))
683                 return -1;
684
685         if (f1->samples != f2->samples)
686                 return -1;
687
688         for (count = 0, data1 = f1->data.ptr, data2 = f2->data.ptr;
689              count < f1->samples;
690              count++, data1++, data2++)
691                 ast_slinear_saturated_add(data1, data2);
692
693         return 0;
694 }
695
696 int ast_frame_clear(struct ast_frame *frame)
697 {
698         struct ast_frame *next;
699
700         for (next = AST_LIST_NEXT(frame, frame_list);
701                  frame;
702                  frame = next, next = frame ? AST_LIST_NEXT(frame, frame_list) : NULL) {
703                 memset(frame->data.ptr, 0, frame->datalen);
704         }
705         return 0;
706 }