comparison test-voice/main.c @ 0:35c0d9f03c0a

beginning with sipout-test-voice, a copy of sip-manual-out from themwi-system-sw
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 03 Mar 2024 23:20:19 -0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:35c0d9f03c0a
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
34 send_invite_req()
35 {
36 struct sip_msg_out msg;
37 struct sdp_gen sdp;
38 int rc;
39
40 rc = start_request_out_msg(&msg, "INVITE", to_uri);
41 if (rc < 0) {
42 msg_size_err: fprintf(stderr, "composing INVITE req: msg size error\n");
43 exit(1);
44 }
45 rc = add_req_boilerplate(&msg, "1 INVITE", 0);
46 if (rc < 0)
47 goto msg_size_err;
48 rc = add_contact_header(&msg);
49 if (rc < 0)
50 goto msg_size_err;
51 if (declare_100rel_supp) {
52 rc = out_msg_add_header(&msg, "Supported", "100rel");
53 if (rc < 0)
54 goto msg_size_err;
55 }
56 rc = out_msg_add_header(&msg, "Content-Type", "application/sdp");
57 if (rc < 0)
58 goto msg_size_err;
59 bzero(&sdp, sizeof sdp);
60 sdp.conn_ip = rtp_local_addr.sin_addr;
61 sdp.conn_port = ntohs(rtp_local_addr.sin_port);
62 if (pcma_codec_force)
63 sdp.codec_mask = SDP_CODEC_MASK_PCMA;
64 else {
65 sdp.codec_mask = SDP_CODEC_MASK_BOTH;
66 if (pcma_codec_pref)
67 sdp.codec_mask |= SDP_CODEC_MASK_PCMA_PREF;
68 }
69 sdp.session_id = sdp.conn_port << 16;
70 sdp.owner_ip = sip_bind_ip;
71 rc = out_msg_finish_sdp(&msg, &sdp);
72 if (rc < 0)
73 goto msg_size_err;
74 sip_tx_packet(&msg, &sip_dest_sin);
75 }
76
77 static void
78 preliminary_proc(argc, argv)
79 char **argv;
80 {
81 extern int optind;
82 extern char *optarg;
83 char *logfile;
84 int opt, rc;
85
86 logfile = 0;
87 while ((opt = getopt(argc, argv, "aAl:m:r")) != EOF) {
88 switch (opt) {
89 case 'a':
90 pcma_codec_pref = 1;
91 continue;
92 case 'A':
93 pcma_codec_force = 1;
94 continue;
95 case 'l':
96 logfile = optarg;
97 continue;
98 case 'm':
99 max_forwards = atoi(optarg);
100 continue;
101 case 'r':
102 declare_100rel_supp = 1;
103 continue;
104 default:
105 usage:
106 fprintf(stderr,
107 "usage: %s [options] dest-conf from-num to-num\n",
108 argv[0]);
109 exit(1);
110 }
111 }
112 if (argc != optind + 3)
113 goto usage;
114 read_config_file(argv[optind]);
115 open_sip_udp_socket();
116 obtain_rtp_endp();
117 sip_dest_sin.sin_family = AF_INET;
118 sip_dest_sin.sin_addr = sip_dest_ip;
119 sip_dest_sin.sin_port = htons(sip_dest_port);
120 sprintf(from_uri, "<sip:%s@%s>;tag=out%u", argv[optind+1],
121 inet_ntoa(sip_bind_ip), ntohs(rtp_local_addr.sin_port));
122 sprintf(to_uri, "sip:%s@%s", argv[optind+2], sip_dest_domain);
123 if (logfile) {
124 rc = open_sip_log_file(logfile);
125 if (rc < 0)
126 exit(1); /* error msg already printed */
127 }
128 }
129
130 main(argc, argv)
131 char **argv;
132 {
133 fd_set fds;
134 struct timeval next_rtp_out, timeout;
135 int rc, max_fd, rtp_out_running;
136
137 preliminary_proc(argc, argv);
138 gettimeofday(&cur_event_time, 0);
139 sprintf(call_id, "%08u_%u@%s",
140 (unsigned)(cur_event_time.tv_sec % 100000000),
141 ntohs(rtp_local_addr.sin_port), inet_ntoa(sip_bind_ip));
142 send_invite_req();
143 /* main select loop */
144 max_fd = sip_socket;
145 if (rtp_udp_fd > max_fd)
146 max_fd = rtp_udp_fd;
147 if (rtcp_udp_fd > max_fd)
148 max_fd = rtcp_udp_fd;
149 rtp_out_running = 0;
150 for (;;) {
151 FD_ZERO(&fds);
152 FD_SET(0, &fds);
153 FD_SET(sip_socket, &fds);
154 FD_SET(rtp_udp_fd, &fds);
155 FD_SET(rtcp_udp_fd, &fds);
156 if (rtp_out_enable) {
157 if (!rtp_out_running) {
158 printf("Starting RTP output\n");
159 bcopy(&cur_event_time, &next_rtp_out,
160 sizeof(struct timeval));
161 rtp_out_running = 1;
162 }
163 if (timercmp(&cur_event_time, &next_rtp_out, <))
164 timersub(&next_rtp_out, &cur_event_time,
165 &timeout);
166 else
167 timerclear(&timeout);
168 rc = select(max_fd+1, &fds, 0, 0, &timeout);
169 } else {
170 if (rtp_out_running) {
171 printf("Stopping RTP output\n");
172 rtp_out_running = 0;
173 }
174 rc = select(max_fd+1, &fds, 0, 0, 0);
175 }
176 if (rc < 0) {
177 if (errno == EINTR)
178 continue;
179 perror("select");
180 exit(1);
181 }
182 gettimeofday(&cur_event_time, 0);
183 if (FD_ISSET(0, &fds))
184 select_stdin();
185 if (FD_ISSET(sip_socket, &fds))
186 sip_socket_select();
187 if (FD_ISSET(rtp_udp_fd, &fds))
188 rtp_rx_select();
189 if (FD_ISSET(rtcp_udp_fd, &fds))
190 rtcp_rx_select();
191 if (rtp_out_running && (rc == 0)) {
192 generate_rtp_packet();
193 next_rtp_out.tv_usec += 20000;
194 if (next_rtp_out.tv_usec >= 1000000) {
195 next_rtp_out.tv_sec++;
196 next_rtp_out.tv_usec -= 1000000;
197 }
198 }
199 }
200 }