comparison spandsp-0.0.6pre17/tests/g1050_tests.c @ 4:26cd8f1ef0b1

import spandsp-0.0.6pre17
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 15:50:58 +0200
parents
children
comparison
equal deleted inserted replaced
3:c6c5a16ce2f2 4:26cd8f1ef0b1
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * g1050_tests.c - Tests for the G.1050/TIA-921 model.
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2007 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * $Id: g1050_tests.c,v 1.18 2009/05/30 15:23:13 steveu Exp $
26 */
27
28 #if defined(HAVE_CONFIG_H)
29 #include "config.h"
30 #endif
31
32 #if defined(HAVE_FL_FL_H) && defined(HAVE_FL_FL_CARTESIAN_H) && defined(HAVE_FL_FL_AUDIO_METER_H)
33 #define ENABLE_GUI
34 #endif
35
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <time.h>
42 #include <sndfile.h>
43 #if defined(HAVE_MATH_H)
44 #define GEN_CONST
45 #endif
46
47 //#if defined(WITH_SPANDSP_INTERNALS)
48 #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
49 //#endif
50
51 #include "spandsp.h"
52 #include "spandsp-sim.h"
53
54 #if defined(ENABLE_GUI)
55 #include "media_monitor.h"
56 #endif
57
58 #define PACKET_SIZE 256
59 #define PACKET_INTERVAL 20
60 #define SIMULATION_TIME 300
61
62 #define MODEL_NO 8
63 #define SPEED_PATTERN_NO 133
64
65 int main(int argc, char *argv[])
66 {
67 g1050_state_t *s;
68 double *packet_arrival_times;
69 int packets_per_sec;
70 int num_packets;
71 int model_no;
72 int speed_pattern_no;
73 int use_gui;
74 int simulation_time;
75 int i;
76 int len;
77 uint8_t put_pkt[256];
78 uint8_t get_pkt[256];
79 int put_pkt_len;
80 int get_pkt_len;
81 int get_seq_no;
82 double get_departure_time;
83 double get_arrival_time;
84 int packets_put;
85 int packets_really_put;
86 int packets_got;
87 int oos_packets_got;
88 int missing_packets_got;
89 int highest_seq_no_got;
90 int opt;
91 FILE *out_file;
92
93 model_no = MODEL_NO;
94 speed_pattern_no = SPEED_PATTERN_NO;
95 simulation_time = SIMULATION_TIME;
96 use_gui = FALSE;
97 while ((opt = getopt(argc, argv, "gm:s:t:")) != -1)
98 {
99 switch (opt)
100 {
101 case 'g':
102 #if defined(ENABLE_GUI)
103 use_gui = TRUE;
104 #else
105 fprintf(stderr, "Graphical monitoring not available\n");
106 exit(2);
107 #endif
108 break;
109 case 'm':
110 model_no = optarg[0] - 'A' + 1;
111 if (model_no < 0 || model_no > 8)
112 {
113 fprintf(stderr, "Bad model ID '%s'\n", optarg);
114 exit(2);
115 }
116 break;
117 case 's':
118 speed_pattern_no = atoi(optarg);
119 if (speed_pattern_no < 1 || speed_pattern_no > 133)
120 {
121 fprintf(stderr, "Bad link speed pattern %s\n", optarg);
122 exit(2);
123 }
124 break;
125 case 't':
126 simulation_time = atoi(optarg);
127 break;
128 default:
129 //usage();
130 exit(2);
131 break;
132 }
133 }
134 argc -= optind;
135 argv += optind;
136
137 if ((out_file = fopen("g1050_tests.txt", "w")) == NULL)
138 {
139 fprintf(stderr, "Can't open %s\n", "g1050_tests.txt");
140 return 2;
141 }
142 packets_per_sec = 1000/PACKET_INTERVAL;
143 num_packets = packets_per_sec*simulation_time;
144
145 if ((packet_arrival_times = calloc(num_packets, sizeof(double))) == NULL)
146 {
147 fprintf(stderr, "Can't allocate the data buffers\n");
148 return 2;
149 }
150 for (i = 0; i < num_packets; i++)
151 packet_arrival_times[i] = 0.0;
152
153 /* If we don't initialise this random number generator it gives endless zeros on some systems. */
154 /* Use a fixed seed to produce identical results in successive runs of the simulation, for debug purposes. */
155 srand48(0x1234567);
156
157 if ((s = g1050_init(model_no, speed_pattern_no, PACKET_SIZE, packets_per_sec)) == NULL)
158 {
159 fprintf(stderr, "Failed to start the G.1050 model\n");
160 exit(2);
161 }
162 g1050_dump_parms(model_no, speed_pattern_no);
163
164 #if defined(ENABLE_GUI)
165 if (use_gui)
166 start_media_monitor();
167 #endif
168
169 for (i = 0; i < 256; i++)
170 put_pkt[i] = i;
171 put_pkt_len = 256;
172 get_pkt_len = -1;
173 get_seq_no = -1;
174 get_arrival_time = -1;
175 packets_put = 0;
176 packets_really_put = 0;
177 packets_got = 0;
178 oos_packets_got = 0;
179 missing_packets_got = 0;
180 highest_seq_no_got = -1;
181 for (i = 0; i < num_packets; i++)
182 {
183 if ((len = g1050_put(s, put_pkt, put_pkt_len, i, (double) i*0.001*PACKET_INTERVAL)) > 0)
184 packets_really_put++;
185 packets_put++;
186 if (i == 5)
187 g1050_queue_dump(s);
188 if (i >= 5)
189 {
190 do
191 {
192 get_pkt_len = g1050_get(s, get_pkt, 256, (double) i*0.001*PACKET_INTERVAL, &get_seq_no, &get_departure_time, &get_arrival_time);
193 if (get_pkt_len >= 0)
194 {
195 #if defined(ENABLE_GUI)
196 if (use_gui)
197 media_monitor_rx(get_seq_no, get_departure_time, get_arrival_time);
198 #endif
199 packets_got++;
200 if (get_seq_no < highest_seq_no_got)
201 oos_packets_got++;
202 else if (get_seq_no > highest_seq_no_got + 1)
203 missing_packets_got += (get_seq_no - highest_seq_no_got - 1);
204 if (get_seq_no > highest_seq_no_got)
205 highest_seq_no_got = get_seq_no;
206 fprintf(out_file, "%d, %.3f, %.8f\n", get_seq_no, get_seq_no*0.001*PACKET_INTERVAL, get_arrival_time);
207 }
208 }
209 while (get_pkt_len >= 0);
210 }
211 #if defined(ENABLE_GUI)
212 if (use_gui)
213 media_monitor_update_display();
214 #endif
215 }
216 /* Clear out anything remaining in the queue, by jumping forwards in time */
217 do
218 {
219 get_pkt_len = g1050_get(s, get_pkt, 256, (double) i*0.001*PACKET_INTERVAL + 5.0, &get_seq_no, &get_departure_time, &get_arrival_time);
220 if (get_pkt_len >= 0)
221 {
222 packets_got++;
223 fprintf(out_file, "%d, %.3f, %.8f\n", get_seq_no, get_seq_no*0.001*PACKET_INTERVAL, get_arrival_time);
224 }
225 }
226 while (get_pkt_len >= 0);
227
228 fclose(out_file);
229
230 printf("Put %d packets. Really put %d packets. Got %d packets.\n", packets_put, packets_really_put, packets_got);
231 printf("%d OOS packets, %d missing packets\n", oos_packets_got, missing_packets_got - oos_packets_got);
232 if (packets_really_put != packets_got)
233 {
234 printf("%d packets queued, but only %d received\n", packets_really_put, packets_got);
235 exit(2);
236 }
237 printf("%.3f%% of packets lost\n", 100.0*(packets_put - packets_really_put)/packets_put);
238 return 0;
239 }
240 /*- End of function --------------------------------------------------------*/
241 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.