diff options
Diffstat (limited to 'toxrtp/tests/test_headers.c')
-rw-r--r-- | toxrtp/tests/test_headers.c | 316 |
1 files changed, 316 insertions, 0 deletions
diff --git a/toxrtp/tests/test_headers.c b/toxrtp/tests/test_headers.c new file mode 100644 index 00000000..be3f1375 --- /dev/null +++ b/toxrtp/tests/test_headers.c | |||
@@ -0,0 +1,316 @@ | |||
1 | /* test_headers.c | ||
2 | * | ||
3 | * Tests header parsing. You probably won't need this. !Red! | ||
4 | * | ||
5 | * | ||
6 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
7 | * | ||
8 | * This file is part of Tox. | ||
9 | * | ||
10 | * Tox is free software: you can redistribute it and/or modify | ||
11 | * it under the terms of the GNU General Public License as published by | ||
12 | * the Free Software Foundation, either version 3 of the License, or | ||
13 | * (at your option) any later version. | ||
14 | * | ||
15 | * Tox is distributed in the hope that it will be useful, | ||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
18 | * GNU General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU General Public License | ||
21 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
22 | * | ||
23 | */ | ||
24 | |||
25 | #include "test_helper.h" | ||
26 | #include "../toxrtp.h" | ||
27 | #include "../toxrtp_message.h" | ||
28 | |||
29 | #include <stdio.h> | ||
30 | #include <stdlib.h> | ||
31 | #include <string.h> | ||
32 | #include <utime.h> | ||
33 | #include <assert.h> | ||
34 | #include "../toxrtp_error_id.h" | ||
35 | |||
36 | #define _CT_HEADERS_ | ||
37 | |||
38 | #ifdef _CT_HEADERS | ||
39 | |||
40 | int _socket; | ||
41 | pthread_mutex_t _mutex; | ||
42 | |||
43 | int print_help() | ||
44 | { | ||
45 | puts ( | ||
46 | " Usage: Tuxrtp [-s (send mode) -d IP ( destination ) -p PORT ( dest Port )] \n" | ||
47 | " [-r ( recv mode ) ]" | ||
48 | ); | ||
49 | return FAILURE; | ||
50 | } | ||
51 | |||
52 | void print_session_stats ( rtp_session_t* _m_session ) | ||
53 | { | ||
54 | printf | ||
55 | ( | ||
56 | "Session test:\n" | ||
57 | "\tPackets sent:%d\n" | ||
58 | "\tPackets recv:%d\n\n" | ||
59 | "\tBytes sent:%d\n" | ||
60 | "\tBytes recv:%d\n\n" | ||
61 | "\tHeader CCSRs:%d\n" | ||
62 | , | ||
63 | _m_session->_packets_sent, | ||
64 | _m_session->_packets_recv, | ||
65 | _m_session->_bytes_sent, | ||
66 | _m_session->_bytes_recv, | ||
67 | _m_session->_cc | ||
68 | ); | ||
69 | |||
70 | uint8_t i; | ||
71 | for ( i = 0; i < _m_session->_cc; i++ ) { | ||
72 | printf ( | ||
73 | "\t%d > :%d\n", i, _m_session->_csrc[i] | ||
74 | ); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | void print_header_info ( rtp_header_t* _header ) | ||
79 | { | ||
80 | printf | ||
81 | ( | ||
82 | "Header info:\n" | ||
83 | "\tVersion :%d\n" | ||
84 | "\tPadding :%d\n" | ||
85 | "\tExtension :%d\n" | ||
86 | "\tCSRC count :%d\n" | ||
87 | "\tPayload type :%d\n" | ||
88 | "\tMarker :%d\n\n" | ||
89 | |||
90 | "\tSSrc :%d\n" | ||
91 | "\tSequence num :%d\n" | ||
92 | "\tLenght: :%d\n" | ||
93 | "\tCSRC's:\n" | ||
94 | , | ||
95 | rtp_header_get_flag_version ( _header ), | ||
96 | rtp_header_get_flag_padding ( _header ), | ||
97 | rtp_header_get_flag_extension ( _header ), | ||
98 | rtp_header_get_flag_CSRC_count ( _header ), | ||
99 | rtp_header_get_setting_payload_type ( _header ), | ||
100 | rtp_header_get_setting_marker ( _header ), | ||
101 | |||
102 | _header->_ssrc, | ||
103 | _header->_sequence_number, | ||
104 | _header->_length | ||
105 | ); | ||
106 | |||
107 | |||
108 | uint8_t i; | ||
109 | for ( i = 0; i < rtp_header_get_flag_CSRC_count ( _header ); i++ ) { | ||
110 | printf ( | ||
111 | "\t%d > :%d\n", i, _header->_csrc[i] | ||
112 | ); | ||
113 | } | ||
114 | |||
115 | puts ( "\n" ); | ||
116 | } | ||
117 | |||
118 | void print_ext_header_info(rtp_ext_header_t* _ext_header) | ||
119 | { | ||
120 | printf | ||
121 | ( | ||
122 | "External Header info: \n" | ||
123 | "\tLenght :%d\n" | ||
124 | "\tID :%d\n" | ||
125 | "\tValue H :%d\n" | ||
126 | "\tValue W :%d\n\n", | ||
127 | _ext_header->_ext_len, | ||
128 | _ext_header->_ext_type, | ||
129 | rtp_get_resolution_marking_height(_ext_header, 0), | ||
130 | rtp_get_resolution_marking_width(_ext_header, 0) | ||
131 | ); | ||
132 | } | ||
133 | |||
134 | int rtp_handlepacket ( rtp_session_t* _session, rtp_msg_t* _msg ) | ||
135 | { | ||
136 | if ( !_msg ) | ||
137 | return FAILURE; | ||
138 | |||
139 | if ( rtp_check_late_message(_session, _msg) < 0 ) { | ||
140 | rtp_register_msg(_session, _msg); | ||
141 | } | ||
142 | |||
143 | if ( _session->_last_msg ) { | ||
144 | _session->_last_msg->_next = _msg; | ||
145 | _session->_last_msg = _msg; | ||
146 | } else { | ||
147 | _session->_last_msg = _session->_oldest_msg = _msg; | ||
148 | } | ||
149 | |||
150 | |||
151 | return SUCCESS; | ||
152 | } | ||
153 | |||
154 | void* receivepacket_callback(void* _p_session) | ||
155 | { | ||
156 | rtp_msg_t* _msg; | ||
157 | rtp_session_t* _session = _p_session; | ||
158 | |||
159 | uint32_t _bytes; | ||
160 | tox_IP_Port _from; | ||
161 | uint8_t _socket_data[MAX_UDP_PACKET_SIZE]; | ||
162 | |||
163 | int _m_socket = _socket; | ||
164 | |||
165 | while ( 1 ) | ||
166 | { | ||
167 | int _status = receivepacket ( _m_socket, &_from, _socket_data, &_bytes ); | ||
168 | |||
169 | if ( _status == FAILURE ) { /* nothing recved */ | ||
170 | usleep(1000); | ||
171 | continue; | ||
172 | } | ||
173 | |||
174 | pthread_mutex_lock ( &_mutex ); | ||
175 | |||
176 | _msg = rtp_msg_parse ( NULL, _socket_data, _bytes ); | ||
177 | rtp_handlepacket(_session, _msg); | ||
178 | |||
179 | pthread_mutex_unlock ( &_mutex ); | ||
180 | } | ||
181 | |||
182 | pthread_exit(NULL); | ||
183 | } | ||
184 | |||
185 | int main ( int argc, char* argv[] ) | ||
186 | { | ||
187 | arg_t* _list = parse_args ( argc, argv ); | ||
188 | |||
189 | if ( _list == NULL ) { /* failed */ | ||
190 | return print_help(); | ||
191 | } | ||
192 | |||
193 | pthread_mutex_init ( &_mutex, NULL ); | ||
194 | |||
195 | int status; | ||
196 | IP_Port Ip_port; | ||
197 | const char* ip; | ||
198 | uint16_t port; | ||
199 | |||
200 | |||
201 | const uint8_t* test_bytes [300]; | ||
202 | memset(test_bytes, 'a', 300); | ||
203 | |||
204 | rtp_session_t* _m_session; | ||
205 | rtp_msg_t* _m_msg; | ||
206 | |||
207 | if ( find_arg_simple ( _list, "-r" ) != FAILURE ) { /* Server mode */ | ||
208 | |||
209 | IP_Port LOCAL_IP; /* since you need at least 1 recv-er */ | ||
210 | LOCAL_IP.ip.i = htonl(INADDR_ANY); | ||
211 | LOCAL_IP.port = RTP_PORT; | ||
212 | LOCAL_IP.padding = -1; | ||
213 | |||
214 | _m_session = rtp_init_session ( -1, -1 ); | ||
215 | Networking_Core* _networking = new_networking(LOCAL_IP.ip, RTP_PORT_LISTEN); | ||
216 | _socket = _networking->sock; | ||
217 | |||
218 | |||
219 | if ( !_networking ){ | ||
220 | pthread_mutex_destroy ( &_mutex ); | ||
221 | return FAILURE; | ||
222 | } | ||
223 | |||
224 | int _socket = _networking->sock; | ||
225 | |||
226 | if ( status < 0 ) { | ||
227 | pthread_mutex_destroy ( &_mutex ); | ||
228 | return FAILURE; | ||
229 | } | ||
230 | /* -- start in recv mode, get 1 message and then analyze it -- */ | ||
231 | pthread_t _tid; | ||
232 | RUN_IN_THREAD(receivepacket_callback, _tid, _m_session) | ||
233 | |||
234 | for ( ; ; ) { /* Recv for x seconds */ | ||
235 | _m_msg = rtp_recv_msg ( _m_session ); | ||
236 | |||
237 | /* _m_msg = rtp_session_get_message_queded ( _m_session ); DEPRECATED */ | ||
238 | if ( _m_msg ) { | ||
239 | /*rtp_free_msg(_m_session, _m_msg); | ||
240 | _m_msg = NULL;*/ | ||
241 | printf("Timestamp: %d\n", _m_msg->_header->_timestamp); | ||
242 | } | ||
243 | |||
244 | usleep ( 10000 ); | ||
245 | } | ||
246 | |||
247 | if ( _m_msg->_header ) { | ||
248 | rtp_header_print ( _m_msg->_header ); | ||
249 | } | ||
250 | if ( _m_msg->_ext_header ){ | ||
251 | print_ext_header_info(_m_msg->_ext_header); | ||
252 | } | ||
253 | |||
254 | //print_session_stats ( _m_session ); | ||
255 | |||
256 | |||
257 | //printf ( "Payload: ( %d ) \n%s\n", _m_msg->_length, _m_msg->_data ); | ||
258 | |||
259 | |||
260 | } else if ( find_arg_simple ( _list, "-s" ) != FAILURE ) { | ||
261 | ip = find_arg_duble ( _list, "-d" ); | ||
262 | |||
263 | if ( ip == NULL ) { | ||
264 | pthread_mutex_destroy ( &_mutex ); | ||
265 | return FAILURE; | ||
266 | } | ||
267 | |||
268 | const char* _port = find_arg_duble ( _list, "-p" ); | ||
269 | |||
270 | if ( _port != NULL ) { | ||
271 | port = atoi ( _port ); | ||
272 | } | ||
273 | |||
274 | t_setipport ( ip, port, &Ip_port ); | ||
275 | printf ( "Remote: %s:%d\n", ip, port ); | ||
276 | |||
277 | Networking_Core* _networking = new_networking(Ip_port.ip, RTP_PORT); | ||
278 | |||
279 | if ( !_networking ){ | ||
280 | pthread_mutex_destroy ( &_mutex ); | ||
281 | return FAILURE; | ||
282 | } | ||
283 | |||
284 | int _socket = _networking->sock; | ||
285 | |||
286 | _m_session = rtp_init_session ( -1, -1 ); | ||
287 | rtp_add_receiver( _m_session, &Ip_port ); | ||
288 | //rtp_add_resolution_marking(_m_session, 1920, 1080); | ||
289 | //rtp_add_framerate_marking(_m_session, 1000); | ||
290 | |||
291 | puts ( "Now sending payload!\n" ); | ||
292 | uint16_t _first_sequ = _m_session->_sequence_number; | ||
293 | |||
294 | /* use already defined buffer lenght */ | ||
295 | while ( 1 ){ | ||
296 | _m_msg = rtp_msg_new ( _m_session, test_bytes, 300 ); | ||
297 | rtp_send_msg ( _m_session, _m_msg, _socket ); | ||
298 | usleep(10000); | ||
299 | } | ||
300 | |||
301 | if ( _m_session->_last_error ) { | ||
302 | puts ( _m_session->_last_error ); | ||
303 | } | ||
304 | |||
305 | return rtp_terminate_session(_m_session); | ||
306 | |||
307 | } else { | ||
308 | pthread_mutex_destroy ( &_mutex ); | ||
309 | return FAILURE; | ||
310 | } | ||
311 | pthread_mutex_destroy ( &_mutex ); | ||
312 | |||
313 | return SUCCESS; | ||
314 | } | ||
315 | |||
316 | #endif /* _CT_HEADERS */ | ||