Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-audiodev/audiotest.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pjmedia-audiodev/audiotest.h>
20 #include <pjmedia-audiodev/audiodev.h>
21 #include <pjlib.h>
22 #include <pjlib-util.h>
23
24 #define THIS_FILE "audiotest.c"
25
26 /* Test duration in msec */
27 #define DURATION 10000
28
29 /* Skip the first msec from the calculation */
30 #define SKIP_DURATION 1000
31
32 /* Division helper */
33 #define DIV_ROUND_UP(a,b) (((a) + ((b) - 1)) / (b))
34 #define DIV_ROUND(a,b) (((a) + ((b)/2 - 1)) / (b))
35
36 struct stream_data
37 {
38 pj_uint32_t first_timestamp;
39 pj_uint32_t last_timestamp;
40 pj_timestamp last_called;
41 pj_math_stat delay;
42 };
43
44 struct test_data
45 {
46 pj_pool_t *pool;
47 const pjmedia_aud_param *param;
48 pjmedia_aud_test_results *result;
49 pj_bool_t running;
50 pj_bool_t has_error;
51 pj_mutex_t *mutex;
52
53 struct stream_data capture_data;
54 struct stream_data playback_data;
55 };
56
57 static pj_status_t play_cb(void *user_data, pjmedia_frame *frame)
58 {
59 struct test_data *test_data = (struct test_data *)user_data;
60 struct stream_data *strm_data = &test_data->playback_data;
61
62 pj_mutex_lock(test_data->mutex);
63
64 /* Skip frames when test is not started or test has finished */
65 if (!test_data->running) {
66 pj_bzero(frame->buf, frame->size);
67 pj_mutex_unlock(test_data->mutex);
68 return PJ_SUCCESS;
69 }
70
71 /* Save last timestamp seen (to calculate drift) */
72 strm_data->last_timestamp = frame->timestamp.u32.lo;
73
74 if (strm_data->last_called.u64 == 0) {
75 /* Init vars. */
76 pj_get_timestamp(&strm_data->last_called);
77 pj_math_stat_init(&strm_data->delay);
78 strm_data->first_timestamp = frame->timestamp.u32.lo;
79 } else {
80 pj_timestamp now;
81 unsigned delay;
82
83 /* Calculate frame interval */
84 pj_get_timestamp(&now);
85 delay = pj_elapsed_usec(&strm_data->last_called, &now);
86 strm_data->last_called = now;
87
88 /* Update frame interval statistic */
89 pj_math_stat_update(&strm_data->delay, delay);
90 }
91
92 pj_bzero(frame->buf, frame->size);
93
94 pj_mutex_unlock(test_data->mutex);
95
96 return PJ_SUCCESS;
97 }
98
99 static pj_status_t rec_cb(void *user_data, pjmedia_frame *frame)
100 {
101 struct test_data *test_data = (struct test_data*)user_data;
102 struct stream_data *strm_data = &test_data->capture_data;
103
104 pj_mutex_lock(test_data->mutex);
105
106 /* Skip frames when test is not started or test has finished */
107 if (!test_data->running) {
108 pj_mutex_unlock(test_data->mutex);
109 return PJ_SUCCESS;
110 }
111
112 /* Save last timestamp seen (to calculate drift) */
113 strm_data->last_timestamp = frame->timestamp.u32.lo;
114
115 if (strm_data->last_called.u64 == 0) {
116 /* Init vars. */
117 pj_get_timestamp(&strm_data->last_called);
118 pj_math_stat_init(&strm_data->delay);
119 strm_data->first_timestamp = frame->timestamp.u32.lo;
120 } else {
121 pj_timestamp now;
122 unsigned delay;
123
124 /* Calculate frame interval */
125 pj_get_timestamp(&now);
126 delay = pj_elapsed_usec(&strm_data->last_called, &now);
127 strm_data->last_called = now;
128
129 /* Update frame interval statistic */
130 pj_math_stat_update(&strm_data->delay, delay);
131 }
132
133 pj_mutex_unlock(test_data->mutex);
134 return PJ_SUCCESS;
135 }
136
137 static void app_perror(const char *title, pj_status_t status)
138 {
139 char errmsg[PJ_ERR_MSG_SIZE];
140
141 pj_strerror(status, errmsg, sizeof(errmsg));
142 printf( "%s: %s (err=%d)\n",
143 title, errmsg, status);
144 }
145
146
147 PJ_DEF(pj_status_t) pjmedia_aud_test( const pjmedia_aud_param *param,
148 pjmedia_aud_test_results *result)
149 {
150 pj_status_t status = PJ_SUCCESS;
151 pjmedia_aud_stream *strm;
152 struct test_data test_data;
153 unsigned ptime, tmp;
154
155 /*
156 * Init test parameters
157 */
158 pj_bzero(&test_data, sizeof(test_data));
159 test_data.param = param;
160 test_data.result = result;
161
162 test_data.pool = pj_pool_create(pjmedia_aud_subsys_get_pool_factory(),
163 "audtest", 1000, 1000, NULL);
164 status = pj_mutex_create_simple(test_data.pool, "sndtest",
165 &test_data.mutex);
166 if (status != PJ_SUCCESS) {
167 app_perror("Error creating mutex", status);
168 pj_pool_release(test_data.pool);
169 return status;
170 }
171
172 /*
173 * Open device.
174 */
175 status = pjmedia_aud_stream_create(test_data.param, &rec_cb, &play_cb,
176 &test_data, &strm);
177 if (status != PJ_SUCCESS) {
178 app_perror("Unable to open device", status);
179 pj_pool_release(test_data.pool);
180 return status;
181 }
182
183
184 /* Sleep for a while to let sound device "settles" */
185 pj_thread_sleep(200);
186
187 /*
188 * Start the stream.
189 */
190 status = pjmedia_aud_stream_start(strm);
191 if (status != PJ_SUCCESS) {
192 app_perror("Unable to start capture stream", status);
193 pjmedia_aud_stream_destroy(strm);
194 pj_pool_release(test_data.pool);
195 return status;
196 }
197
198 PJ_LOG(3,(THIS_FILE,
199 " Please wait while test is in progress (~%d secs)..",
200 (DURATION+SKIP_DURATION)/1000));
201
202 /* Let the stream runs for few msec/sec to get stable result.
203 * (capture normally begins with frames available simultaneously).
204 */
205 pj_thread_sleep(SKIP_DURATION);
206
207
208 /* Begin gather data */
209 test_data.running = 1;
210
211 /*
212 * Let the test runs for a while.
213 */
214 pj_thread_sleep(DURATION);
215
216
217 /*
218 * Close stream.
219 */
220 test_data.running = 0;
221 pjmedia_aud_stream_destroy(strm);
222 pj_pool_release(test_data.pool);
223
224
225 /*
226 * Gather results
227 */
228 ptime = param->samples_per_frame * 1000 / param->clock_rate;
229
230 tmp = pj_math_stat_get_stddev(&test_data.capture_data.delay);
231 result->rec.frame_cnt = test_data.capture_data.delay.n;
232 result->rec.min_interval = DIV_ROUND(test_data.capture_data.delay.min, 1000);
233 result->rec.max_interval = DIV_ROUND(test_data.capture_data.delay.max, 1000);
234 result->rec.avg_interval = DIV_ROUND(test_data.capture_data.delay.mean, 1000);
235 result->rec.dev_interval = DIV_ROUND(tmp, 1000);
236 result->rec.max_burst = DIV_ROUND_UP(result->rec.max_interval, ptime);
237
238 tmp = pj_math_stat_get_stddev(&test_data.playback_data.delay);
239 result->play.frame_cnt = test_data.playback_data.delay.n;
240 result->play.min_interval = DIV_ROUND(test_data.playback_data.delay.min, 1000);
241 result->play.max_interval = DIV_ROUND(test_data.playback_data.delay.max, 1000);
242 result->play.avg_interval = DIV_ROUND(test_data.playback_data.delay.mean, 1000);
243 result->play.dev_interval = DIV_ROUND(tmp, 1000);
244 result->play.max_burst = DIV_ROUND_UP(result->play.max_interval, ptime);
245
246 /* Check drifting */
247 if (param->dir == PJMEDIA_DIR_CAPTURE_PLAYBACK) {
248 int play_diff, cap_diff, drift;
249
250 play_diff = test_data.playback_data.last_timestamp -
251 test_data.playback_data.first_timestamp;
252 cap_diff = test_data.capture_data.last_timestamp -
253 test_data.capture_data.first_timestamp;
254 drift = play_diff > cap_diff? play_diff - cap_diff :
255 cap_diff - play_diff;
256
257 /* Allow one frame tolerance for clock drift detection */
258 if (drift < (int)param->samples_per_frame) {
259 result->rec_drift_per_sec = 0;
260 } else {
261 unsigned msec_dur;
262
263 msec_dur = (test_data.capture_data.last_timestamp -
264 test_data.capture_data.first_timestamp) * 1000 /
265 test_data.param->clock_rate;
266
267 result->rec_drift_per_sec = drift * 1000 / msec_dur;
268
269 }
270 }
271
272 return test_data.has_error? PJ_EUNKNOWN : PJ_SUCCESS;
273 }
274