b2563726943b99c77489e3141b929e7f83fad93d
[paraslash.git] / udp_recv.c
1 /*
2 * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6 /** \file udp_recv.c Paraslash's udp receiver */
7
8 #include <dirent.h>
9
10 #include "para.h"
11 #include "portable_io.h"
12 #include "udp_header.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "recv.h"
17 #include "udp_recv.cmdline.h"
18 #include "error.h"
19 #include "audiod.h"
20 #include "string.h"
21 #include "net.h"
22 #include "fd.h"
23
24 /** The size of the receiver node buffer. */
25 #define UDP_RECV_CHUNK_SIZE (128 * 1024)
26
27 /**
28 * Data specific to the udp receiver.
29 *
30 * \sa \ref receiver, \ref receiver_node.
31 */
32 struct private_udp_recv_data {
33 /**
34 * Whether a header was received.
35 *
36 * A flag indicating whether this receiver already received a packet
37 * which contains the audio file header.
38 *
39 * This flag has no effect if the audio stream indicates that no extra
40 * headers will be sent (mp3, aac). Otherwise, all data packets are
41 * dropped until the header is received.
42 */
43 int have_header;
44 /** The socket file descriptor. */
45 int fd;
46 };
47
48 static void udp_recv_pre_select(struct sched *s, struct task *t)
49 {
50 struct receiver_node *rn = container_of(t, struct receiver_node, task);
51 struct private_udp_recv_data *purd = rn->private_data;
52
53 para_fd_set(purd->fd, &s->rfds, &s->max_fileno);
54 }
55
56 static int enough_space(size_t packet_size, size_t loaded)
57 {
58 return packet_size + loaded < UDP_RECV_CHUNK_SIZE + UDP_AUDIO_HEADER_LEN;
59 }
60
61 static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
62 {
63 struct receiver_node *rn = container_of(t, struct receiver_node, task);
64 struct private_udp_recv_data *purd = rn->private_data;
65 int ret;
66 char tmpbuf[UDP_RECV_CHUNK_SIZE];
67 size_t packet_size;
68 uint8_t stream_type, packet_type;
69
70 if (rn->output_error && *rn->output_error < 0) {
71 t->error = *rn->output_error;
72 return;
73 }
74 if (!FD_ISSET(purd->fd, &s->rfds))
75 return;
76 ret = recv_bin_buffer(purd->fd, tmpbuf, UDP_RECV_CHUNK_SIZE);
77 if (ret < 0) {
78 t->error = 0;
79 if (errno != EINTR && errno != EAGAIN)
80 t->error = -ERRNO_TO_PARA_ERROR(errno);
81 return;
82 }
83 packet_size = ret;
84 if (packet_size < UDP_AUDIO_HEADER_LEN) {
85 t->error = -E_UDP_SHORT_PACKET; /* FIXME: We shouldn't fail here */
86 return;
87 }
88 if (udp_check_magic(tmpbuf, packet_size) < 0) {
89 t->error = -E_UDP_NO_MAGIC;
90 return;
91 }
92 stream_type = udp_read_stream_type(tmpbuf);
93 packet_type = udp_read_packet_type(tmpbuf);
94 // PARA_INFO_LOG("packet type: %d, stream type: %d,"
95 // " loaded: %u\n", packet_type,
96 // (unsigned) stream_type, rn->loaded);
97 switch (packet_type) {
98 uint16_t header_len, payload_len;
99 case UDP_EOF_PACKET:
100 t->error = -E_RECV_EOF;
101 return;
102 case UDP_BOF_PACKET:
103 PARA_INFO_LOG("bof (%zu)\n", packet_size);
104 purd->have_header = 1;
105 /* fall through */
106 case UDP_DATA_PACKET:
107 if (!purd->have_header && stream_type == UDP_HEADER_STREAM)
108 /* can't use the data, wait for header */
109 goto success;
110 if (!enough_space(packet_size, rn->loaded)) {
111 t->error = -E_OVERRUN;
112 return;
113 }
114 if (packet_size > UDP_AUDIO_HEADER_LEN) {
115 memcpy(rn->buf + rn->loaded, tmpbuf + UDP_AUDIO_HEADER_LEN,
116 packet_size - UDP_AUDIO_HEADER_LEN);
117 rn->loaded += packet_size - UDP_AUDIO_HEADER_LEN;
118 }
119 goto success;
120 case UDP_HEADER_PACKET:
121 if (!enough_space(packet_size, rn->loaded)) {
122 t->error = -E_OVERRUN;
123 return;
124 }
125 header_len = udp_read_header_len(tmpbuf);
126 if (header_len + UDP_AUDIO_HEADER_LEN > packet_size) {
127 t->error = -E_INVALID_HEADER;
128 return;
129 }
130 // PARA_DEBUG_LOG("header packet (%zu bytes), header len: %d\n",
131 // packet_size, header_len);
132 if (!purd->have_header) {
133 purd->have_header = 1;
134 rn->loaded = header_len;
135 memcpy(rn->buf, tmpbuf + UDP_AUDIO_HEADER_LEN,
136 rn->loaded);
137 // sleep(1);
138 goto success;
139 }
140 payload_len = packet_size - UDP_AUDIO_HEADER_LEN - header_len;
141 if (rn->loaded + payload_len > UDP_RECV_CHUNK_SIZE) {
142 t->error = -E_OVERRUN;
143 return;
144 }
145 if (payload_len)
146 memcpy(rn->buf + rn->loaded, tmpbuf
147 + (packet_size - payload_len), payload_len);
148 rn->loaded += payload_len;
149 }
150 success:
151 t->error = 0;
152 }
153
154 static void udp_shutdown(void)
155 {
156 return;
157 }
158
159 static void udp_recv_close(struct receiver_node *rn)
160 {
161 struct private_udp_recv_data *purd = rn->private_data;
162
163 if (purd->fd >= 0)
164 close(purd->fd);
165 free(rn->private_data);
166 free(rn->buf);
167 }
168
169 static void *udp_recv_parse_config(int argc, char **argv)
170 {
171 int ret;
172 struct udp_recv_args_info *tmp =
173 para_calloc(sizeof(struct udp_recv_args_info));
174
175 ret = udp_recv_cmdline_parser(argc, argv, tmp)? -E_UDP_SYNTAX : 1;
176 if (ret >= 0)
177 return tmp;
178 free(tmp);
179 return NULL;
180 }
181
182 static int udp_recv_open(struct receiver_node *rn)
183 {
184 struct private_udp_recv_data *purd;
185 struct udp_recv_args_info *c = rn->conf;
186 int ret;
187
188 rn->buf = para_calloc(UDP_RECV_CHUNK_SIZE);
189 rn->private_data = para_calloc(sizeof(struct private_udp_recv_data));
190 purd = rn->private_data;
191 ret = create_udp_recv_socket(c->host_arg, c->port_arg);
192 if (ret < 0)
193 return ret;
194 purd->fd = ret;
195 ret = mark_fd_nonblocking(purd->fd);
196 if (ret < 0)
197 return ret;
198 PARA_NOTICE_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
199 c->port_arg, purd->fd);
200 return purd->fd;
201 }
202
203 /**
204 * The init function of the udp receiver.
205 *
206 * \param r Pointer to the receiver struct to initialize.
207 *
208 * Initialize all function pointers of \a r.
209 */
210 void udp_recv_init(struct receiver *r)
211 {
212 struct udp_recv_args_info dummy;
213
214 udp_recv_cmdline_parser_init(&dummy);
215 r->shutdown = udp_shutdown;
216 r->open = udp_recv_open;
217 r->close = udp_recv_close;
218 r->pre_select = udp_recv_pre_select;
219 r->post_select = udp_recv_post_select;
220 r->parse_config = udp_recv_parse_config;
221 r->help = (struct ggo_help) {
222 .short_help = udp_recv_args_info_help,
223 .detailed_help = udp_recv_args_info_detailed_help
224 };
225 }