g1050_tests.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #if defined(HAVE_CONFIG_H)
  26. #include "config.h"
  27. #endif
  28. #if defined(HAVE_FL_FL_H) && defined(HAVE_FL_FL_CARTESIAN_H) && defined(HAVE_FL_FL_AUDIO_METER_H)
  29. #define ENABLE_GUI
  30. #endif
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <fcntl.h>
  34. #include <unistd.h>
  35. #include <string.h>
  36. #include <time.h>
  37. #include <sndfile.h>
  38. #if defined(HAVE_MATH_H)
  39. #define GEN_CONST
  40. #endif
  41. #include "spandsp.h"
  42. #include "spandsp-sim.h"
  43. #if defined(ENABLE_GUI)
  44. #include "media_monitor.h"
  45. #endif
  46. #define PACKET_SIZE 256
  47. #define PACKET_INTERVAL 20
  48. #define SIMULATION_TIME 300
  49. #define MODEL_NO 8
  50. #define SPEED_PATTERN_NO 133
  51. int main(int argc, char *argv[])
  52. {
  53. g1050_state_t *s;
  54. double *packet_arrival_times;
  55. int packets_per_sec;
  56. int num_packets;
  57. int model_no;
  58. int speed_pattern_no;
  59. int simulation_time;
  60. int i;
  61. int len;
  62. uint8_t put_pkt[256];
  63. uint8_t get_pkt[256];
  64. int put_pkt_len;
  65. int get_pkt_len;
  66. int get_seq_no;
  67. double get_departure_time;
  68. double get_arrival_time;
  69. int packets_put;
  70. int packets_really_put;
  71. int packets_got;
  72. int oos_packets_got;
  73. int missing_packets_got;
  74. int highest_seq_no_got;
  75. int opt;
  76. FILE *out_file;
  77. #if defined(ENABLE_GUI)
  78. int use_gui;
  79. #endif
  80. #if defined(ENABLE_GUI)
  81. use_gui = false;
  82. #endif
  83. model_no = MODEL_NO;
  84. speed_pattern_no = SPEED_PATTERN_NO;
  85. simulation_time = SIMULATION_TIME;
  86. while ((opt = getopt(argc, argv, "gm:s:t:")) != -1)
  87. {
  88. switch (opt)
  89. {
  90. case 'g':
  91. #if defined(ENABLE_GUI)
  92. use_gui = true;
  93. #else
  94. fprintf(stderr, "Graphical monitoring not available\n");
  95. exit(2);
  96. #endif
  97. break;
  98. case 'm':
  99. model_no = optarg[0] - 'A' + 1;
  100. if (model_no < 0 || model_no > 8)
  101. {
  102. fprintf(stderr, "Bad model ID '%s'\n", optarg);
  103. exit(2);
  104. }
  105. break;
  106. case 's':
  107. speed_pattern_no = atoi(optarg);
  108. if (speed_pattern_no < 1 || speed_pattern_no > 133)
  109. {
  110. fprintf(stderr, "Bad link speed pattern %s\n", optarg);
  111. exit(2);
  112. }
  113. break;
  114. case 't':
  115. simulation_time = atoi(optarg);
  116. break;
  117. default:
  118. //usage();
  119. exit(2);
  120. break;
  121. }
  122. }
  123. argc -= optind;
  124. argv += optind;
  125. if ((out_file = fopen("g1050_tests.txt", "w")) == NULL)
  126. {
  127. fprintf(stderr, "Can't open %s\n", "g1050_tests.txt");
  128. return 2;
  129. }
  130. packets_per_sec = 1000/PACKET_INTERVAL;
  131. num_packets = packets_per_sec*simulation_time;
  132. if ((packet_arrival_times = calloc(num_packets, sizeof(double))) == NULL)
  133. {
  134. fprintf(stderr, "Can't allocate the data buffers\n");
  135. return 2;
  136. }
  137. for (i = 0; i < num_packets; i++)
  138. packet_arrival_times[i] = 0.0;
  139. /* If we don't initialise this random number generator it gives endless zeros on some systems. */
  140. /* Use a fixed seed to produce identical results in successive runs of the simulation, for debug purposes. */
  141. srand48(0x1234567);
  142. if ((s = g1050_init(model_no, speed_pattern_no, PACKET_SIZE, packets_per_sec)) == NULL)
  143. {
  144. fprintf(stderr, "Failed to start the G.1050 model\n");
  145. exit(2);
  146. }
  147. g1050_dump_parms(model_no, speed_pattern_no);
  148. #if defined(ENABLE_GUI)
  149. if (use_gui)
  150. start_media_monitor();
  151. #endif
  152. for (i = 0; i < 256; i++)
  153. put_pkt[i] = i;
  154. put_pkt_len = 256;
  155. get_pkt_len = -1;
  156. get_seq_no = -1;
  157. get_arrival_time = -1;
  158. packets_put = 0;
  159. packets_really_put = 0;
  160. packets_got = 0;
  161. oos_packets_got = 0;
  162. missing_packets_got = 0;
  163. highest_seq_no_got = -1;
  164. for (i = 0; i < num_packets; i++)
  165. {
  166. if ((len = g1050_put(s, put_pkt, put_pkt_len, i, (double) i*0.001*PACKET_INTERVAL)) > 0)
  167. packets_really_put++;
  168. packets_put++;
  169. if (i == 5)
  170. g1050_queue_dump(s);
  171. if (i >= 5)
  172. {
  173. do
  174. {
  175. get_pkt_len = g1050_get(s, get_pkt, 256, (double) i*0.001*PACKET_INTERVAL, &get_seq_no, &get_departure_time, &get_arrival_time);
  176. if (get_pkt_len >= 0)
  177. {
  178. #if defined(ENABLE_GUI)
  179. if (use_gui)
  180. media_monitor_rx(get_seq_no, get_departure_time, get_arrival_time);
  181. #endif
  182. packets_got++;
  183. if (get_seq_no < highest_seq_no_got)
  184. oos_packets_got++;
  185. else if (get_seq_no > highest_seq_no_got + 1)
  186. missing_packets_got += (get_seq_no - highest_seq_no_got - 1);
  187. if (get_seq_no > highest_seq_no_got)
  188. highest_seq_no_got = get_seq_no;
  189. fprintf(out_file, "%d, %.3f, %.8f\n", get_seq_no, get_seq_no*0.001*PACKET_INTERVAL, get_arrival_time);
  190. }
  191. }
  192. while (get_pkt_len >= 0);
  193. }
  194. #if defined(ENABLE_GUI)
  195. if (use_gui)
  196. media_monitor_update_display();
  197. #endif
  198. }
  199. /* Clear out anything remaining in the queue, by jumping forwards in time */
  200. do
  201. {
  202. 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);
  203. if (get_pkt_len >= 0)
  204. {
  205. packets_got++;
  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. fclose(out_file);
  211. printf("Put %d packets. Really put %d packets. Got %d packets.\n", packets_put, packets_really_put, packets_got);
  212. printf("%d OOS packets, %d missing packets\n", oos_packets_got, missing_packets_got - oos_packets_got);
  213. if (packets_really_put != packets_got)
  214. {
  215. printf("%d packets queued, but only %d received\n", packets_really_put, packets_got);
  216. exit(2);
  217. }
  218. printf("%.3f%% of packets lost\n", 100.0*(packets_put - packets_really_put)/packets_put);
  219. g1050_free(s);
  220. free(packet_arrival_times);
  221. return 0;
  222. }
  223. /*- End of function --------------------------------------------------------*/
  224. /*- End of file ------------------------------------------------------------*/