comparison mtctest/rtp_play.c @ 10:395c56969bc4

mtctest: implement guts of RTP play mechanism
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 09 Jun 2024 04:24:53 +0000
parents
children aa2ba9b432af
comparison
equal deleted inserted replaced
9:0ec938ed530b 10:395c56969bc4
1 /*
2 * In this module we implement RTP play command, emitting a test sequence
3 * of encoded speech frames toward the MS.
4 */
5
6 #include <sys/types.h>
7 #include <sys/file.h>
8 #include <sys/stat.h>
9 #include <sys/socket.h>
10 #include <sys/time.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
13 #include <stdio.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <strings.h>
18 #include <unistd.h>
19 #include <themwi/rtp/rtp_defs.h>
20 #include <themwi/rtp/rtp_alloc_simple.h>
21
22 extern struct rtp_alloc_simple rtp_info;
23 extern int got_msc_rtp_info;
24 extern struct sockaddr_storage msc_rtp_addr;
25 extern uint8_t rtp_payload_type;
26 extern struct timeval cur_event_time;
27
28 int rtp_play_active;
29
30 static uint8_t play_buffer[33*50*30]; /* max 30 s of GSM-FR play */
31
32 static uint32_t rtp_ssrc;
33 static uint32_t rtp_out_ts;
34 static uint16_t rtp_out_seq;
35
36 static unsigned play_frame_len;
37 static unsigned play_buf_nframes, play_buf_ptr;
38 static int play_loop;
39
40 static void
41 assign_rtpout_ssrc()
42 {
43 rtp_ssrc = cur_event_time.tv_sec ^ cur_event_time.tv_usec ^ getpid();
44 }
45
46 static void
47 fill_with_play(outbuf)
48 uint8_t *outbuf;
49 {
50 memcpy(outbuf, play_buffer + play_buf_ptr * play_frame_len,
51 play_frame_len);
52 play_buf_ptr++;
53 if (play_buf_ptr < play_buf_nframes)
54 return;
55 play_buf_ptr = 0;
56 if (!play_loop)
57 rtp_play_active = 0;
58 }
59
60 void
61 generate_rtp_packet()
62 {
63 struct rtp_packet pkt;
64 socklen_t addrlen;
65
66 pkt.v_p_x_cc = 0x80;
67 pkt.m_pt = rtp_payload_type;
68 pkt.seq = htons(rtp_out_seq++);
69 pkt.tstamp = htonl(rtp_out_ts);
70 rtp_out_ts += 160;
71 pkt.ssrc = rtp_ssrc;
72 fill_with_play(pkt.payload);
73 addrlen = sizeof(struct sockaddr_in);
74 sendto(rtp_info.gsm_rtp_fd, &pkt, RTP_PACKET_HDR_SIZE + play_frame_len,
75 0, (struct sockaddr *) &msc_rtp_addr, addrlen);
76 }
77
78 void
79 play_file_cmdop(filename, frame_len, loop)
80 unsigned frame_len;
81 char *filename;
82 {
83 int fd;
84 struct stat st;
85
86 if (rtp_play_active) {
87 fprintf(stderr, "error: file play already in progress\n");
88 return;
89 }
90 if (!got_msc_rtp_info) {
91 fprintf(stderr, "error: no RTP info received from MSC\n");
92 return;
93 }
94 fd = open(filename, O_RDONLY);
95 if (fd < 0) {
96 perror(filename);
97 return;
98 }
99 fstat(fd, &st);
100 if (!S_ISREG(st.st_mode)) {
101 close(fd);
102 fprintf(stderr, "error: %s is not a regular file\n", filename);
103 return;
104 }
105 if (!st.st_size) {
106 close(fd);
107 fprintf(stderr, "error: %s is an empty file\n", filename);
108 return;
109 }
110 if (st.st_size % frame_len) {
111 close(fd);
112 fprintf(stderr,
113 "error: size of %s is not a multiple of %u bytes\n",
114 filename, frame_len);
115 return;
116 }
117 if (st.st_size > sizeof play_buffer) {
118 close(fd);
119 fprintf(stderr,
120 "error: size of %s exceeds compiled-in buffer size\n",
121 filename);
122 return;
123 }
124 read(fd, play_buffer, st.st_size);
125 close(fd);
126 play_buf_nframes = st.st_size / frame_len;
127 play_buf_ptr = 0;
128 play_frame_len = frame_len;
129 play_loop = loop;
130 assign_rtpout_ssrc();
131 rtp_out_ts = 0;
132 rtp_out_seq = 0;
133 rtp_play_active = 1;
134 }
135
136 void
137 play_file_stop()
138 {
139 rtp_play_active = 0;
140 }