Revert "Makefile: Fix compilation after header removal."
[paraslash.git] / afh_recv.c
1 /* Copyright (C) 2011 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file afh_recv.c Receiver for streaming local files. */
4
5 #include <regex.h>
6 #include <sys/types.h>
7 #include <lopsub.h>
8
9 #include "recv_cmd.lsg.h"
10 #include "para.h"
11 #include "error.h"
12 #include "list.h"
13 #include "sched.h"
14 #include "buffer_tree.h"
15 #include "recv.h"
16 #include "string.h"
17 #include "fd.h"
18 #include "afh.h"
19
20 struct private_afh_recv_data {
21         int fd;
22         void *map;
23         size_t map_size;
24         struct afh_info afhi;
25         int audio_format_num;
26         long unsigned first_chunk;
27         long unsigned last_chunk;
28         struct timeval stream_start;
29         uint32_t current_chunk;
30         void *afh_context;
31 };
32
33 static int afh_execute(struct btr_node *btrn, const char *cmd, char **result)
34 {
35         struct receiver_node *rn = btr_context(btrn);
36         struct private_afh_recv_data *pard = rn->private_data;
37
38         *result = NULL;
39         if (!strcmp(cmd, "seconds_total")) {
40                 *result = make_message("%" PRIu32, pard->afhi.seconds_total);
41                 return 1;
42         }
43         if (!strcmp(cmd, "chunks_total")) {
44                 *result = make_message("%" PRIu32, pard->afhi.chunks_total);
45                 return 1;
46         }
47         if (!strcmp(cmd, "afhi")) {
48                 afh_get_afhi_txt(pard->audio_format_num, &pard->afhi, result);
49                 return 1;
50         }
51         if (!strncmp(cmd, "repos", 5)) {
52                 int32_t x;
53                 int ret = para_atoi32(cmd + 5, &x);
54                 if (ret < 0)
55                         return ret;
56                 if (x >= pard->afhi.chunks_total)
57                         return -ERRNO_TO_PARA_ERROR(EINVAL);
58                 pard->first_chunk = afh_get_start_chunk(x, &pard->afhi,
59                         pard->audio_format_num);
60                 pard->current_chunk = pard->first_chunk;
61                 return 1;
62         }
63         return -E_BTR_NAVAIL;
64 }
65
66 static int afh_recv_open(struct receiver_node *rn)
67 {
68         struct lls_parse_result *lpr = rn->lpr;
69         struct private_afh_recv_data *pard;
70         struct afh_info *afhi;
71         const char *fn = RECV_CMD_OPT_STRING_VAL(AFH, FILENAME, lpr), *msg;
72         int32_t bc = RECV_CMD_OPT_INT32_VAL(AFH, BEGIN_CHUNK, lpr);
73         const struct lls_opt_result *r_e = RECV_CMD_OPT_RESULT(AFH, END_CHUNK, lpr);
74         int ret;
75
76         if (!fn || *fn == '\0')
77                 return -E_AFH_RECV_BAD_FILENAME;
78         rn->private_data = pard = para_calloc(sizeof(*pard));
79         afhi = &pard->afhi;
80         ret = mmap_full_file(fn, O_RDONLY, &pard->map,
81                 &pard->map_size, &pard->fd);
82         if (ret < 0)
83                 goto out;
84         ret = compute_afhi(fn, pard->map, pard->map_size,
85                 pard->fd, afhi);
86         if (ret < 0)
87                 goto out_unmap;
88         pard->audio_format_num = ret;
89         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
90         msg = "no data chunks";
91         if (afhi->chunks_total == 0)
92                 goto out_clear_afhi;
93         msg = "invalid begin chunk";
94         if (PARA_ABS(bc) >= afhi->chunks_total)
95                 goto out_clear_afhi;
96         if (bc >= 0)
97                 pard->first_chunk = afh_get_start_chunk(bc, &pard->afhi,
98                         pard->audio_format_num);
99         else
100                 pard->first_chunk = afh_get_start_chunk(afhi->chunks_total + bc,
101                         &pard->afhi, pard->audio_format_num);
102         if (lls_opt_given(r_e)) {
103                 int32_t ec = lls_int32_val(0, r_e);
104                 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
105                 msg = "invalid end chunk";
106                 if (PARA_ABS(ec) > afhi->chunks_total)
107                         goto out_clear_afhi;
108                 if (ec >= 0)
109                         pard->last_chunk = ec;
110                 else
111                         pard->last_chunk = afhi->chunks_total + ec;
112         } else
113                 pard->last_chunk = afhi->chunks_total - 1;
114         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
115         msg = "begin chunk >= end chunk!?";
116         if (pard->first_chunk >= pard->last_chunk)
117                 goto out_clear_afhi;
118         pard->current_chunk = pard->first_chunk;
119         return pard->audio_format_num;
120 out_clear_afhi:
121         clear_afhi(afhi);
122         PARA_ERROR_LOG("%s: %s\n", fn, msg);
123 out_unmap:
124         para_munmap(pard->map, pard->map_size);
125         close(pard->fd);
126 out:
127         freep(&rn->private_data);
128         return ret;
129 }
130
131 static void afh_recv_close(struct receiver_node *rn)
132 {
133         struct private_afh_recv_data *pard;
134
135         if (!rn || !rn->private_data)
136                 return;
137         pard = rn->private_data;
138         clear_afhi(&pard->afhi);
139         para_munmap(pard->map, pard->map_size);
140         close(pard->fd);
141         afh_close(pard->afh_context, pard->audio_format_num);
142         freep(&rn->private_data);
143 }
144
145 static void afh_recv_pre_select(struct sched *s, void *context)
146 {
147         struct receiver_node *rn = context;
148         struct private_afh_recv_data *pard = rn->private_data;
149         struct afh_info *afhi = &pard->afhi;
150         struct lls_parse_result *lpr = rn->lpr;
151         struct timeval chunk_time;
152         int state = generic_recv_pre_select(s, rn);
153         unsigned j_given = RECV_CMD_OPT_GIVEN(AFH, JUST_IN_TIME, lpr);
154
155         if (state <= 0)
156                 return;
157         if (!j_given) {
158                 sched_min_delay(s);
159                 return;
160         }
161         compute_chunk_time(pard->current_chunk - pard->first_chunk,
162                 &afhi->chunk_tv, &pard->stream_start, &chunk_time);
163         sched_request_barrier_or_min_delay(&chunk_time, s);
164 }
165
166 static int afh_recv_post_select(__a_unused struct sched *s, void *context)
167 {
168         struct receiver_node *rn = context;
169         struct lls_parse_result *lpr = rn->lpr;
170         struct private_afh_recv_data *pard = rn->private_data;
171         struct btr_node *btrn = rn->btrn;
172         struct afh_info *afhi = &pard->afhi;
173         int ret;
174         char *buf;
175         const char *start;
176         size_t size;
177         uint32_t len;
178         struct timeval chunk_time;
179         unsigned j_given = RECV_CMD_OPT_GIVEN(AFH, JUST_IN_TIME, lpr);
180         unsigned H_given = RECV_CMD_OPT_GIVEN(AFH, NO_HEADER, lpr);
181
182         ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
183         if (ret <= 0)
184                 goto out;
185         if (pard->first_chunk > 0 && !H_given) {
186                 char *header;
187                 afh_get_header(afhi, pard->audio_format_num, pard->map,
188                         pard->map_size, &header, &size);
189                 if (size > 0) {
190                         PARA_INFO_LOG("writing header (%zu bytes)\n", size);
191                         buf = para_malloc(size);
192                         memcpy(buf, header, size);
193                         btr_add_output(buf, size, btrn);
194                         afh_free_header(header, pard->audio_format_num);
195                 }
196         }
197         if (!j_given) {
198                 long unsigned n;
199                 for (n = pard->first_chunk; n < pard->last_chunk; n++) {
200                         ret = afh_get_chunk(n, afhi, pard->audio_format_num,
201                                 pard->map, pard->map_size, &start, &len,
202                                 &pard->afh_context);
203                         if (ret < 0)
204                                 goto out;
205                         PARA_DEBUG_LOG("adding %u bytes\n", len);
206                         btr_add_output_dont_free(start, len, btrn);
207                 }
208                 ret = -E_RECV_EOF;
209                 goto out;
210         }
211         if (pard->current_chunk == pard->first_chunk)
212                 pard->stream_start = *now;
213         else {
214                 compute_chunk_time(pard->current_chunk - pard->first_chunk,
215                         &afhi->chunk_tv, &pard->stream_start, &chunk_time);
216                 ret = tv_diff(&chunk_time, now, NULL);
217                 if (ret > 0)
218                         goto out;
219         }
220         ret = afh_get_chunk(pard->current_chunk, afhi,
221                 pard->audio_format_num, pard->map,
222                 pard->map_size, &start, &len,
223                 &pard->afh_context);
224         if (ret < 0)
225                 goto out;
226         PARA_DEBUG_LOG("adding chunk %u\n", pard->current_chunk);
227         btr_add_output_dont_free(start, len, btrn);
228         if (pard->current_chunk >= pard->last_chunk) {
229                 ret = -E_RECV_EOF;
230                 goto out;
231         }
232         pard->current_chunk++;
233         ret = 1;
234 out:
235         if (ret < 0) {
236                 btr_remove_node(&rn->btrn);
237                 pard->current_chunk = pard->first_chunk;
238         }
239         return ret;
240 }
241
242 const struct receiver lsg_recv_cmd_com_afh_user_data = {
243         .open = afh_recv_open,
244         .close = afh_recv_close,
245         .pre_select = afh_recv_pre_select,
246         .post_select = afh_recv_post_select,
247         .execute = afh_execute,
248 };