Merge branch 't/recv_fix'
[paraslash.git] / afh_recv.c
1 /*
2  * Copyright (C) 2011-2014 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file afh_recv.c Receiver for streaming local files. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <stdbool.h>
12
13 #include "para.h"
14 #include "error.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "ggo.h"
18 #include "buffer_tree.h"
19 #include "recv.h"
20 #include "afh_recv.cmdline.h"
21 #include "string.h"
22 #include "fd.h"
23 #include "afh.h"
24
25 struct private_afh_recv_data {
26         int fd;
27         void *map;
28         size_t map_size;
29         struct afh_info afhi;
30         int audio_format_num;
31         long unsigned first_chunk;
32         long unsigned last_chunk;
33         struct timeval stream_start;
34         uint32_t current_chunk;
35 };
36
37 static int afh_execute(struct btr_node *btrn, const char *cmd, char **result)
38 {
39         struct receiver_node *rn = btr_context(btrn);
40         struct private_afh_recv_data *pard = rn->private_data;
41
42         *result = NULL;
43         if (!strcmp(cmd, "seconds_total")) {
44                 *result = make_message("%lu", pard->afhi.seconds_total);
45                 return 1;
46         }
47         if (!strcmp(cmd, "chunks_total")) {
48                 *result = make_message("%lu", pard->afhi.chunks_total);
49                 return 1;
50         }
51         if (!strcmp(cmd, "afhi")) {
52                 afh_get_afhi_txt(pard->audio_format_num, &pard->afhi, result);
53                 return 1;
54         }
55         if (!strncmp(cmd, "repos", 5)) {
56                 int32_t x;
57                 int ret = para_atoi32(cmd + 5, &x);
58                 if (ret < 0)
59                         return ret;
60                 if (x >= pard->afhi.chunks_total)
61                         return -ERRNO_TO_PARA_ERROR(EINVAL);
62                 pard->first_chunk = pard->current_chunk = x;
63                 return 1;
64         }
65         return -E_BTR_NAVAIL;
66 }
67
68 static void *afh_recv_parse_config(int argc, char **argv)
69 {
70         struct afh_recv_args_info *tmp = para_calloc(sizeof(*tmp));
71
72         afh_recv_cmdline_parser(argc, argv, tmp);
73         return tmp;
74 }
75
76 static void afh_recv_free_config(void *conf)
77 {
78         if (!conf)
79                 return;
80         afh_recv_cmdline_parser_free(conf);
81         free(conf);
82 }
83
84 static int afh_recv_open(struct receiver_node *rn)
85 {
86         struct afh_recv_args_info *conf = rn->conf;
87         struct private_afh_recv_data *pard;
88         struct afh_info *afhi;
89         char *filename = conf->filename_arg;
90
91         int ret;
92
93         if (!filename || *filename == '\0')
94                 return -E_AFH_RECV_BAD_FILENAME;
95         rn->private_data = pard = para_calloc(sizeof(*pard));
96         afhi = &pard->afhi;
97         ret = mmap_full_file(filename, O_RDONLY, &pard->map,
98                 &pard->map_size, &pard->fd);
99         if (ret < 0)
100                 goto out;
101         ret = compute_afhi(filename, pard->map, pard->map_size,
102                 pard->fd, afhi);
103         if (ret < 0)
104                 goto out_unmap;
105         pard->audio_format_num = ret;
106         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
107         if (afhi->chunks_total == 0)
108                 goto out_clear_afhi;
109         if (PARA_ABS(conf->begin_chunk_arg) >= afhi->chunks_total)
110                 goto out_clear_afhi;
111         if (conf->begin_chunk_arg >= 0)
112                 pard->first_chunk = conf->begin_chunk_arg;
113         else
114                 pard->first_chunk = afhi->chunks_total + conf->begin_chunk_arg;
115         if (conf->end_chunk_given) {
116                 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
117                 if (PARA_ABS(conf->end_chunk_arg) > afhi->chunks_total)
118                         goto out_clear_afhi;
119                 if (conf->end_chunk_arg >= 0)
120                         pard->last_chunk = conf->end_chunk_arg;
121                 else
122                         pard->last_chunk = afhi->chunks_total + conf->end_chunk_arg;
123         } else
124                 pard->last_chunk = afhi->chunks_total - 1;
125         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
126         if (pard->first_chunk >= pard->last_chunk)
127                 goto out_clear_afhi;
128         pard->current_chunk = pard->first_chunk;
129         return pard->audio_format_num;
130 out_clear_afhi:
131         clear_afhi(afhi);
132 out_unmap:
133         para_munmap(pard->map, pard->map_size);
134         close(pard->fd);
135 out:
136         freep(&rn->private_data);
137         return ret;
138 }
139
140 static void afh_recv_close(struct receiver_node *rn)
141 {
142         struct private_afh_recv_data *pard;
143
144         if (!rn || !rn->private_data)
145                 return;
146         pard = rn->private_data;
147         clear_afhi(&pard->afhi);
148         para_munmap(pard->map, pard->map_size);
149         close(pard->fd);
150         freep(&rn->private_data);
151 }
152
153 static void afh_recv_pre_select(struct sched *s, void *context)
154 {
155         struct receiver_node *rn = context;
156         struct private_afh_recv_data *pard = rn->private_data;
157         struct afh_info *afhi = &pard->afhi;
158         struct afh_recv_args_info *conf = rn->conf;
159         struct timeval chunk_time;
160         int state = generic_recv_pre_select(s, rn);
161
162         if (state <= 0)
163                 return;
164         if (!conf->just_in_time_given) {
165                 sched_min_delay(s);
166                 return;
167         }
168         compute_chunk_time(pard->current_chunk - pard->first_chunk,
169                 &afhi->chunk_tv, &pard->stream_start, &chunk_time);
170         sched_request_barrier_or_min_delay(&chunk_time, s);
171 }
172
173 static int afh_recv_post_select(__a_unused struct sched *s, void *context)
174 {
175         struct receiver_node *rn = context;
176         struct afh_recv_args_info *conf = rn->conf;
177         struct private_afh_recv_data *pard = rn->private_data;
178         struct btr_node *btrn = rn->btrn;
179         struct afh_info *afhi = &pard->afhi;
180         int ret;
181         char *buf;
182         const char *start, *end;
183         size_t size;
184         struct timeval chunk_time;
185
186         ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
187         if (ret <= 0)
188                 goto out;
189         if (pard->first_chunk > 0 && !conf->no_header_given) {
190                 char *header;
191                 afh_get_header(afhi, pard->audio_format_num, pard->map,
192                         pard->map_size, &header, &size);
193                 if (size > 0) {
194                         PARA_INFO_LOG("writing header (%zu bytes)\n", size);
195                         buf = para_malloc(size);
196                         memcpy(buf, header, size);
197                         btr_add_output(buf, size, btrn);
198                         afh_free_header(header, pard->audio_format_num);
199                 }
200         }
201         if (!conf->just_in_time_given) {
202                 afh_get_chunk(pard->first_chunk, afhi, pard->map, &start, &size);
203                 afh_get_chunk(pard->last_chunk, afhi, pard->map, &end, &size);
204                 end += size;
205                 PARA_INFO_LOG("adding %zu bytes\n", end - start);
206                 btr_add_output_dont_free(start, end - start, btrn);
207                 ret = -E_RECV_EOF;
208                 goto out;
209         }
210         if (pard->current_chunk == pard->first_chunk)
211                 pard->stream_start = *now;
212         else {
213                 compute_chunk_time(pard->current_chunk - pard->first_chunk,
214                         &afhi->chunk_tv, &pard->stream_start, &chunk_time);
215                 ret = tv_diff(&chunk_time, now, NULL);
216                 if (ret > 0)
217                         goto out;
218         }
219         afh_get_chunk(pard->current_chunk, afhi, pard->map, &start, &size);
220         PARA_DEBUG_LOG("adding chunk %u\n", pard->current_chunk);
221         btr_add_output_dont_free(start, size, btrn);
222         if (pard->current_chunk >= pard->last_chunk) {
223                 ret = -E_RECV_EOF;
224                 goto out;
225         }
226         pard->current_chunk++;
227         ret = 1;
228 out:
229         if (ret < 0) {
230                 btr_remove_node(&rn->btrn);
231                 pard->current_chunk = pard->first_chunk;
232         }
233         return ret;
234 }
235
236 /**
237  * The init function of the afh receiver.
238  *
239  * \param r Pointer to the receiver struct to initialize.
240  *
241  * This initializes all function pointers of \a r.
242  */
243 void afh_recv_init(struct receiver *r)
244 {
245         struct afh_recv_args_info dummy;
246
247         afh_init();
248         afh_recv_cmdline_parser_init(&dummy);
249         r->open = afh_recv_open;
250         r->close = afh_recv_close;
251         r->pre_select = afh_recv_pre_select;
252         r->post_select = afh_recv_post_select;
253         r->parse_config = afh_recv_parse_config;
254         r->free_config = afh_recv_free_config;
255         r->execute = afh_execute;
256         r->help = (struct ggo_help)DEFINE_GGO_HELP(afh_recv);
257         afh_recv_cmdline_parser_free(&dummy);
258 }