Debian Codegraph @

Xpjproject/pjsip-apps/src/samples/latency.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
20 /* See http://trac.pjsip.org/repos/wiki/MeasuringSoundLatency on
21 * how to use this program.
22 */
23
24 #include <pjmedia.h>
25 #include <pjlib.h>
26
27 #include <stdio.h>
28
29 #define THIS_FILE "lacency.c"
30
31
32 /* Util to display the error message for the specified error code */
33 static int app_perror( const char *sender, const char *title,
34 pj_status_t status)
35 {
36 char errmsg[PJ_ERR_MSG_SIZE];
37
38 PJ_UNUSED_ARG(sender);
39
40 pj_strerror(status, errmsg, sizeof(errmsg));
41
42 printf("%s: %s [code=%d]\n", title, errmsg, status);
43 return 1;
44 }
45
46 /*
47 * Find out latency
48 */
49 static int calculate_latency(pj_pool_t *pool, pjmedia_port *wav)
50 {
51 pjmedia_frame frm;
52 short *buf;
53 unsigned i, samples_per_frame;
54 pj_size_t read, len;
55 unsigned start_pos;
56 pj_status_t status;
57
58 unsigned lat_sum = 0,
59 lat_cnt = 0,
60 lat_min = 10000,
61 lat_max = 0;
62
63 samples_per_frame = PJMEDIA_PIA_SPF(&wav->info);
64 frm.buf = pj_pool_alloc(pool, samples_per_frame * 2);
65 frm.size = samples_per_frame * 2;
66 len = pjmedia_wav_player_get_len(wav);
67 buf = pj_pool_alloc(pool, len + samples_per_frame);
68
69 read = 0;
70 while (read < len/2) {
71 status = pjmedia_port_get_frame(wav, &frm);
72 if (status != PJ_SUCCESS)
73 break;
74
75 pjmedia_copy_samples(buf+read, (short*)frm.buf, samples_per_frame);
76 read += samples_per_frame;
77 }
78
79 if (read < 2 * PJMEDIA_PIA_SRATE(&wav->info)) {
80 puts("Error: too short");
81 return -1;
82 }
83
84 start_pos = 0;
85 while (start_pos < len/2 - PJMEDIA_PIA_SRATE(&wav->info)) {
86 int max_signal = 0;
87 unsigned max_signal_pos = start_pos;
88 unsigned max_echo_pos = 0;
89 unsigned pos;
90 unsigned lat;
91
92 /* Get the largest signal in the next 0.7s */
93 for (i=start_pos; i<start_pos + PJMEDIA_PIA_SRATE(&wav->info) * 700 / 1000; ++i) {
94 if (abs(buf[i]) > max_signal) {
95 max_signal = abs(buf[i]);
96 max_signal_pos = i;
97 }
98 }
99
100 /* Advance 10ms from max_signal_pos */
101 pos = max_signal_pos + 10 * PJMEDIA_PIA_SRATE(&wav->info) / 1000;
102
103 /* Get the largest signal in the next 500ms */
104 max_signal = 0;
105 max_echo_pos = pos;
106 for (i=pos; i<pos+PJMEDIA_PIA_SRATE(&wav->info)/2; ++i) {
107 if (abs(buf[i]) > max_signal) {
108 max_signal = abs(buf[i]);
109 max_echo_pos = i;
110 }
111 }
112
113 lat = (max_echo_pos - max_signal_pos) * 1000 / PJMEDIA_PIA_SRATE(&wav->info);
114
115 #if 0
116 printf("Latency = %u\n", lat);
117 #endif
118
119 lat_sum += lat;
120 lat_cnt++;
121 if (lat < lat_min)
122 lat_min = lat;
123 if (lat > lat_max)
124 lat_max = lat;
125
126 /* Advance next loop */
127 start_pos += PJMEDIA_PIA_SRATE(&wav->info);
128 }
129
130 if (lat_cnt)
131 printf("Latency average = %u\n", lat_sum / lat_cnt);
132 printf("Latency minimum = %u\n", lat_min);
133 printf("Latency maximum = %u\n", lat_max);
134 printf("Number of data = %u\n", lat_cnt);
135 return 0;
136 }
137
138
139 /*
140 * main()
141 */
142 int main(int argc, char *argv[])
143 {
144 enum { NSAMPLES = 160, COUNT=100 };
145 pj_caching_pool cp;
146 pj_pool_t *pool;
147 pjmedia_port *wav;
148 pj_status_t status;
149
150
151 /* Verify cmd line arguments. */
152 if (argc != 2) {
153 puts("Error: missing argument(s)");
154 puts("Usage: latency REV.WAV");
155 return 1;
156 }
157
158 pj_log_set_level(0);
159
160 status = pj_init();
161 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
162
163 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
164
165 pool = pj_pool_create( &cp.factory, /* pool factory */
166 "wav", /* pool name. */
167 4000, /* init size */
168 4000, /* increment size */
169 NULL /* callback on error */
170 );
171
172 status = pj_register_strerror(PJMEDIA_ERRNO_START, PJ_ERRNO_SPACE_SIZE,
173 &pjmedia_strerror);
174 pj_assert(status == PJ_SUCCESS);
175
176 /* Wav */
177 status = pjmedia_wav_player_port_create( pool, /* memory pool */
178 argv[1], /* file to play */
179 0, /* use default ptime*/
180 0, /* flags */
181 0, /* default buffer */
182 &wav /* returned port */
183 );
184 if (status != PJ_SUCCESS) {
185 app_perror(THIS_FILE, argv[1], status);
186 return 1;
187 }
188
189 status = calculate_latency(pool, wav);
190 if (status != PJ_SUCCESS)
191 return 1;
192
193 status = pjmedia_port_destroy( wav );
194 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
195
196 pj_pool_release( pool );
197 pj_caching_pool_destroy( &cp );
198 pj_shutdown();
199
200 /* Done. */
201 return 0;
202 }
203