Mercurial > hg > audiostuff
comparison spandsp-0.0.3/spandsp-0.0.3/tests/ip_network_model.c @ 5:f762bf195c4b
import spandsp-0.0.3
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 16:00:21 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4:26cd8f1ef0b1 | 5:f762bf195c4b |
---|---|
1 /* | |
2 * SpanDSP - a series of DSP components for telephony | |
3 * | |
4 * ip_network_model.c - Model an IP networks latency, jitter and loss. | |
5 * | |
6 * Written by Steve Underwood <steveu@coppice.org> | |
7 * | |
8 * Copyright (C) 2006 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: ip_network_model.c,v 1.4 2006/11/19 14:07:27 steveu Exp $ | |
26 */ | |
27 | |
28 #ifdef HAVE_CONFIG_H | |
29 #include "config.h" | |
30 #endif | |
31 | |
32 #include <stdlib.h> | |
33 #include <unistd.h> | |
34 #include <inttypes.h> | |
35 #include <string.h> | |
36 #include <time.h> | |
37 #include <stdio.h> | |
38 #include <fcntl.h> | |
39 #include <audiofile.h> | |
40 #include <tiffio.h> | |
41 #if defined(HAVE_TGMATH_H) | |
42 #include <tgmath.h> | |
43 #endif | |
44 #if defined(HAVE_MATH_H) | |
45 #define GEN_CONST | |
46 #include <math.h> | |
47 #endif | |
48 | |
49 #include "spandsp.h" | |
50 | |
51 #include "ip_network_model.h" | |
52 | |
53 #if !defined(NULL) | |
54 #define NULL (void *) 0 | |
55 #endif | |
56 | |
57 void ip_network_model_send(ip_network_model_state_t *s, | |
58 int seq_no, | |
59 int count, | |
60 const uint8_t *buf, | |
61 int len) | |
62 { | |
63 int delay[2]; | |
64 int i; | |
65 | |
66 for (i = 0; i < count; i++) | |
67 { | |
68 if (rand()%1000000 >= s->packet_loss) | |
69 { | |
70 delay[0] = s->current_samples + s->bulk_delay + (rand()%s->jitter); | |
71 delay[1] = seq_no; | |
72 queue_write_msg(&s->packet_queue, (uint8_t *) delay, sizeof(delay)); | |
73 queue_write_msg(&s->packet_queue, buf, len); | |
74 } | |
75 else | |
76 { | |
77 fprintf(stderr, "Dropping packet - seq %d\n", seq_no); | |
78 } | |
79 } | |
80 } | |
81 /*- End of function --------------------------------------------------------*/ | |
82 | |
83 int ip_network_model_get(ip_network_model_state_t *s, | |
84 int time_step, | |
85 uint8_t *msg, | |
86 int max_len, | |
87 int *seq_no) | |
88 { | |
89 int len; | |
90 | |
91 s->current_samples += time_step; | |
92 | |
93 if (s->delay[0] < 0) | |
94 { | |
95 /* Wait for a new packet */ | |
96 if (queue_empty(&s->packet_queue)) | |
97 return -1; | |
98 len = queue_read_msg(&s->packet_queue, (uint8_t *) s->delay, sizeof(s->delay)); | |
99 if (len != sizeof(s->delay)) | |
100 return -1; | |
101 } | |
102 *seq_no = s->delay[1]; | |
103 /* Wait for the appropriate time */ | |
104 if (s->delay[0] > s->current_samples) | |
105 return -1; | |
106 /* Wait for a message */ | |
107 if (queue_empty(&s->packet_queue)) | |
108 return -1; | |
109 s->delay[0] = -1; | |
110 s->delay[1] = -1; | |
111 len = queue_read_msg(&s->packet_queue, msg, max_len); | |
112 return len; | |
113 } | |
114 /*- End of function --------------------------------------------------------*/ | |
115 | |
116 ip_network_model_state_t *ip_network_model_init(int bulk_delay, | |
117 int jitter, | |
118 int loss) | |
119 { | |
120 ip_network_model_state_t *s; | |
121 | |
122 if ((s = (ip_network_model_state_t *) malloc(sizeof(*s))) == NULL) | |
123 return NULL; | |
124 memset(s, 0, sizeof(*s)); | |
125 | |
126 s->bulk_delay = bulk_delay; | |
127 s->jitter = jitter; | |
128 s->packet_loss = loss; | |
129 | |
130 s->delay[0] = -1; | |
131 s->delay[1] = -1; | |
132 | |
133 if (queue_create(&s->packet_queue, 32768, QUEUE_WRITE_ATOMIC | QUEUE_READ_ATOMIC) < 0) | |
134 { | |
135 free(s); | |
136 return NULL; | |
137 } | |
138 return s; | |
139 } | |
140 /*- End of function --------------------------------------------------------*/ | |
141 | |
142 int ip_network_model_release(ip_network_model_state_t *s) | |
143 { | |
144 queue_delete(&s->packet_queue); | |
145 free(s); | |
146 return 0; | |
147 } | |
148 /*- End of function --------------------------------------------------------*/ | |
149 /*- End of file ------------------------------------------------------------*/ |