rfc2198_sim_tests.c 7.4 KB

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