FreeCalypso > hg > sipout-test-utils
comparison test-v22/main.c @ 10:3c5734b88c20
sipout-test-v22 put together
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 07 Mar 2024 02:33:49 -0800 |
parents | test-fsk/main.c@ba66d297fe57 |
children |
comparison
equal
deleted
inserted
replaced
9:ff535725e01f | 10:3c5734b88c20 |
---|---|
1 /* | |
2 * This is the main module for sip-manual-out test program. | |
3 */ | |
4 | |
5 #include <sys/types.h> | |
6 #include <sys/time.h> | |
7 #include <sys/socket.h> | |
8 #include <sys/errno.h> | |
9 #include <netinet/in.h> | |
10 #include <arpa/inet.h> | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <string.h> | |
14 #include <strings.h> | |
15 #include <unistd.h> | |
16 #include "../libsip/out_msg.h" | |
17 #include "../libsip/sdp.h" | |
18 | |
19 extern int sip_socket; | |
20 extern struct in_addr sip_bind_ip, sip_dest_ip; | |
21 extern unsigned sip_bind_port, sip_dest_port; | |
22 extern char sip_dest_domain[]; | |
23 extern struct sockaddr_in rtp_local_addr; | |
24 extern int rtp_udp_fd, rtcp_udp_fd; | |
25 extern int rtp_out_enable; | |
26 | |
27 struct sockaddr_in sip_dest_sin; | |
28 char from_uri[128], to_uri[128], call_id[128]; | |
29 struct timeval cur_event_time; | |
30 unsigned max_forwards = 70; | |
31 int declare_100rel_supp; | |
32 int pcma_codec_pref, pcma_codec_force; | |
33 int v22_bitrate; | |
34 | |
35 send_invite_req() | |
36 { | |
37 struct sip_msg_out msg; | |
38 struct sdp_gen sdp; | |
39 int rc; | |
40 | |
41 rc = start_request_out_msg(&msg, "INVITE", to_uri); | |
42 if (rc < 0) { | |
43 msg_size_err: fprintf(stderr, "composing INVITE req: msg size error\n"); | |
44 exit(1); | |
45 } | |
46 rc = add_req_boilerplate(&msg, "1 INVITE", 0); | |
47 if (rc < 0) | |
48 goto msg_size_err; | |
49 rc = add_contact_header(&msg); | |
50 if (rc < 0) | |
51 goto msg_size_err; | |
52 if (declare_100rel_supp) { | |
53 rc = out_msg_add_header(&msg, "Supported", "100rel"); | |
54 if (rc < 0) | |
55 goto msg_size_err; | |
56 } | |
57 rc = out_msg_add_header(&msg, "Content-Type", "application/sdp"); | |
58 if (rc < 0) | |
59 goto msg_size_err; | |
60 bzero(&sdp, sizeof sdp); | |
61 sdp.conn_ip = rtp_local_addr.sin_addr; | |
62 sdp.conn_port = ntohs(rtp_local_addr.sin_port); | |
63 if (pcma_codec_force) | |
64 sdp.codec_mask = SDP_CODEC_MASK_PCMA; | |
65 else { | |
66 sdp.codec_mask = SDP_CODEC_MASK_BOTH; | |
67 if (pcma_codec_pref) | |
68 sdp.codec_mask |= SDP_CODEC_MASK_PCMA_PREF; | |
69 } | |
70 sdp.session_id = sdp.conn_port << 16; | |
71 sdp.owner_ip = sip_bind_ip; | |
72 rc = out_msg_finish_sdp(&msg, &sdp); | |
73 if (rc < 0) | |
74 goto msg_size_err; | |
75 sip_tx_packet(&msg, &sip_dest_sin); | |
76 } | |
77 | |
78 static void | |
79 preliminary_proc(argc, argv) | |
80 char **argv; | |
81 { | |
82 extern int optind; | |
83 extern char *optarg; | |
84 char *logfile; | |
85 int opt, rc; | |
86 | |
87 logfile = 0; | |
88 while ((opt = getopt(argc, argv, "aAl:m:r")) != EOF) { | |
89 switch (opt) { | |
90 case 'a': | |
91 pcma_codec_pref = 1; | |
92 continue; | |
93 case 'A': | |
94 pcma_codec_force = 1; | |
95 continue; | |
96 case 'l': | |
97 logfile = optarg; | |
98 continue; | |
99 case 'm': | |
100 max_forwards = atoi(optarg); | |
101 continue; | |
102 case 'r': | |
103 declare_100rel_supp = 1; | |
104 continue; | |
105 default: | |
106 usage: | |
107 fprintf(stderr, | |
108 "usage: %s [options] dest-conf from-num to-num bps\n", | |
109 argv[0]); | |
110 exit(1); | |
111 } | |
112 } | |
113 if (argc != optind + 4) | |
114 goto usage; | |
115 read_config_file(argv[optind]); | |
116 open_sip_udp_socket(); | |
117 obtain_rtp_endp(); | |
118 sip_dest_sin.sin_family = AF_INET; | |
119 sip_dest_sin.sin_addr = sip_dest_ip; | |
120 sip_dest_sin.sin_port = htons(sip_dest_port); | |
121 sprintf(from_uri, "<sip:%s@%s>;tag=out%u", argv[optind+1], | |
122 inet_ntoa(sip_bind_ip), ntohs(rtp_local_addr.sin_port)); | |
123 sprintf(to_uri, "sip:%s@%s", argv[optind+2], sip_dest_domain); | |
124 v22_bitrate = atoi(argv[optind+3]); | |
125 if (v22_bitrate != 1200 && v22_bitrate != 2400) { | |
126 fprintf(stderr, "error: bps argument must be 1200 or 2400\n"); | |
127 exit(1); | |
128 } | |
129 init_modem_func(); | |
130 if (logfile) { | |
131 rc = open_sip_log_file(logfile); | |
132 if (rc < 0) | |
133 exit(1); /* error msg already printed */ | |
134 } | |
135 } | |
136 | |
137 main(argc, argv) | |
138 char **argv; | |
139 { | |
140 fd_set fds; | |
141 struct timeval next_rtp_out, timeout; | |
142 int rc, max_fd, rtp_out_running; | |
143 | |
144 preliminary_proc(argc, argv); | |
145 gettimeofday(&cur_event_time, 0); | |
146 sprintf(call_id, "%08u_%u@%s", | |
147 (unsigned)(cur_event_time.tv_sec % 100000000), | |
148 ntohs(rtp_local_addr.sin_port), inet_ntoa(sip_bind_ip)); | |
149 send_invite_req(); | |
150 /* main select loop */ | |
151 max_fd = sip_socket; | |
152 if (rtp_udp_fd > max_fd) | |
153 max_fd = rtp_udp_fd; | |
154 if (rtcp_udp_fd > max_fd) | |
155 max_fd = rtcp_udp_fd; | |
156 rtp_out_running = 0; | |
157 for (;;) { | |
158 FD_ZERO(&fds); | |
159 FD_SET(0, &fds); | |
160 FD_SET(sip_socket, &fds); | |
161 FD_SET(rtp_udp_fd, &fds); | |
162 FD_SET(rtcp_udp_fd, &fds); | |
163 if (rtp_out_enable) { | |
164 if (!rtp_out_running) { | |
165 printf("Starting RTP output\n"); | |
166 bcopy(&cur_event_time, &next_rtp_out, | |
167 sizeof(struct timeval)); | |
168 rtp_out_running = 1; | |
169 } | |
170 if (timercmp(&cur_event_time, &next_rtp_out, <)) | |
171 timersub(&next_rtp_out, &cur_event_time, | |
172 &timeout); | |
173 else | |
174 timerclear(&timeout); | |
175 rc = select(max_fd+1, &fds, 0, 0, &timeout); | |
176 } else { | |
177 if (rtp_out_running) { | |
178 printf("Stopping RTP output\n"); | |
179 rtp_out_running = 0; | |
180 } | |
181 rc = select(max_fd+1, &fds, 0, 0, 0); | |
182 } | |
183 if (rc < 0) { | |
184 if (errno == EINTR) | |
185 continue; | |
186 perror("select"); | |
187 exit(1); | |
188 } | |
189 gettimeofday(&cur_event_time, 0); | |
190 if (FD_ISSET(0, &fds)) | |
191 select_stdin(); | |
192 if (FD_ISSET(sip_socket, &fds)) | |
193 sip_socket_select(); | |
194 if (FD_ISSET(rtp_udp_fd, &fds)) | |
195 rtp_rx_select(); | |
196 if (FD_ISSET(rtcp_udp_fd, &fds)) | |
197 rtcp_rx_select(); | |
198 if (rtp_out_running && (rc == 0)) { | |
199 generate_rtp_packet(); | |
200 next_rtp_out.tv_usec += 20000; | |
201 if (next_rtp_out.tv_usec >= 1000000) { | |
202 next_rtp_out.tv_sec++; | |
203 next_rtp_out.tv_usec -= 1000000; | |
204 } | |
205 } | |
206 } | |
207 } |