Asterisk - The Open Source Telephony Project GIT-master-f36a736
smoother.c
Go to the documentation of this file.
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 smoother 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#include "asterisk/_private.h"
33#include "asterisk/frame.h"
34#include "asterisk/astobj2.h"
35#include "asterisk/time.h"
36#include "asterisk/utils.h"
37#include "asterisk/format.h"
38#include "asterisk/codec.h"
39#include "asterisk/smoother.h"
40
41#define SMOOTHER_SIZE 8000
42
44 int size;
46 int flags;
48 unsigned int opt_needs_swap:1;
49 struct ast_frame f;
50 struct timeval delivery;
53 struct ast_frame *opt;
54 int len;
55};
56
57static int smoother_frame_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
58{
60 if (s->len % 10) {
61 ast_log(LOG_NOTICE, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n");
62 return 0;
63 }
64 }
65 if (swap) {
67 } else {
68 memcpy(s->data + s->len, f->data.ptr, f->datalen);
69 }
70 /* If either side is empty, reset the delivery time */
71 if (!s->len || ast_tvzero(f->delivery) || ast_tvzero(s->delivery)) { /* XXX really ? */
72 s->delivery = f->delivery;
73 }
74 s->len += f->datalen;
75
76 return 0;
77}
78
79void ast_smoother_reset(struct ast_smoother *s, int bytes)
80{
82 memset(s, 0, sizeof(*s));
83 s->size = bytes;
84}
85
86void ast_smoother_reconfigure(struct ast_smoother *s, int bytes)
87{
88 /* if there is no change, then nothing to do */
89 if (s->size == bytes) {
90 return;
91 }
92 /* set the new desired output size */
93 s->size = bytes;
94 /* if there is no 'optimized' frame in the smoother,
95 * then there is nothing left to do
96 */
97 if (!s->opt) {
98 return;
99 }
100 /* there is an 'optimized' frame here at the old size,
101 * but it must now be put into the buffer so the data
102 * can be extracted at the new size
103 */
105 s->opt = NULL;
106}
107
109{
110 struct ast_smoother *s;
111 if (size < 1)
112 return NULL;
113 if ((s = ast_calloc(1, sizeof(*s))))
115 return s;
116}
117
119{
120 return s->flags;
121}
122
124{
125 s->flags = flags;
126}
127
129{
130 return (s->flags & flag);
131}
132
133int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
134{
135 if (f->frametype != AST_FRAME_VOICE) {
136 ast_log(LOG_WARNING, "Huh? Can't smooth a non-voice frame!\n");
137 return -1;
138 }
139 if (!s->format) {
141 s->samplesperbyte = (float)f->samples / (float)f->datalen;
143 ast_log(LOG_WARNING, "Smoother was working on %s format frames, now trying to feed %s?\n",
145 return -1;
146 }
147 if (s->len + f->datalen > SMOOTHER_SIZE) {
148 ast_log(LOG_WARNING, "Out of smoother space\n");
149 return -1;
150 }
151 if (((f->datalen == s->size) ||
152 ((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729))) &&
153 !s->opt &&
154 !s->len &&
155 (f->offset >= AST_MIN_OFFSET)) {
156 /* Optimize by sending the frame we just got
157 on the next read, thus eliminating the douple
158 copy */
159 if (swap)
161 s->opt = f;
162 s->opt_needs_swap = swap ? 1 : 0;
163 return 0;
164 }
165
166 return smoother_frame_feed(s, f, swap);
167}
168
170{
171 struct ast_frame *opt;
172 int len;
173
174 /* IF we have an optimization frame, send it */
175 if (s->opt) {
177 ast_log(LOG_WARNING, "Returning a frame of inappropriate offset (%d).\n",
178 s->opt->offset);
179 opt = s->opt;
180 s->opt = NULL;
181 return opt;
182 }
183
184 /* Make sure we have enough data */
185 if (s->len < s->size) {
186 /* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */
187 if (!((s->flags & AST_SMOOTHER_FLAG_G729) && (s->len % 10)))
188 return NULL;
189 }
190 len = s->size;
191 if (len > s->len)
192 len = s->len;
193 /* Make frame */
195 s->f.subclass.format = s->format;
198 s->f.datalen = len;
199 /* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */
200 s->f.samples = len * s->samplesperbyte; /* XXX rounding */
201 s->f.delivery = s->delivery;
202 /* Fill Data */
203 memcpy(s->f.data.ptr, s->data, len);
204 s->len -= len;
205 /* Move remaining data to the front if applicable */
206 if (s->len) {
207 /* In principle this should all be fine because if we are sending
208 G.729 VAD, the next timestamp will take over anyway */
209 memmove(s->data, s->data + len, s->len);
210 if (!ast_tvzero(s->delivery)) {
211 /* If we have delivery time, increment it, otherwise, leave it at 0 */
214 }
215 }
216 /* Return frame */
217 return &s->f;
218}
219
221{
223 ast_free(s);
224}
Prototypes for public functions only of internal interest,.
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
Codec API.
long int flag
Definition: f2c.h:83
Media Format API.
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
@ AST_FORMAT_CMP_NOT_EQUAL
Definition: format.h:38
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Asterisk internal frame definitions.
#define AST_MIN_OFFSET
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
void ast_swapcopy_samples(void *dst, const void *src, int samples)
Definition: main/frame.c:396
@ AST_FRAME_VOICE
#define LOG_NOTICE
#define LOG_WARNING
#define NULL
Definition: resample.c:96
int ast_smoother_get_flags(struct ast_smoother *s)
Definition: smoother.c:118
struct ast_smoother * ast_smoother_new(int size)
Definition: smoother.c:108
void ast_smoother_reconfigure(struct ast_smoother *s, int bytes)
Reconfigure an existing smoother to output a different number of bytes per frame.
Definition: smoother.c:86
void ast_smoother_set_flags(struct ast_smoother *s, int flags)
Definition: smoother.c:123
int ast_smoother_test_flag(struct ast_smoother *s, int flag)
Definition: smoother.c:128
void ast_smoother_free(struct ast_smoother *s)
Definition: smoother.c:220
struct ast_frame * ast_smoother_read(struct ast_smoother *s)
Definition: smoother.c:169
int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
Definition: smoother.c:133
static int smoother_frame_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
Definition: smoother.c:57
#define SMOOTHER_SIZE
Definition: smoother.c:41
void ast_smoother_reset(struct ast_smoother *s, int bytes)
Definition: smoother.c:79
Asterisk internal frame definitions.
#define AST_SMOOTHER_FLAG_G729
Definition: smoother.h:34
Definition of a media format.
Definition: format.c:43
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
union ast_frame::@226 data
struct timeval delivery
enum ast_frame_type frametype
struct ast_frame * opt
Definition: smoother.c:53
struct timeval delivery
Definition: smoother.c:50
struct ast_frame f
Definition: smoother.c:49
char framedata[SMOOTHER_SIZE+AST_FRIENDLY_OFFSET]
Definition: smoother.c:52
unsigned int opt_needs_swap
Definition: smoother.c:48
struct ast_format * format
Definition: smoother.c:45
float samplesperbyte
Definition: smoother.c:47
char data[SMOOTHER_SIZE]
Definition: smoother.c:51
Time-related functions and macros.
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
Definition: time.h:282
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:117
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: extconf.c:2282
Utility functions.