]> git.tuebingen.mpg.de Git - paraslash.git/blob - afh.c
ee55cf0e603d1ff0cfa8b4f1bf55eeb8aba10098
[paraslash.git] / afh.c
1 /*
2  * Copyright (C) 2008-2012 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file afh.c Paraslash's standalone audio format handler tool. */
8
9 #include <regex.h>
10 #include <sys/time.h>
11
12 #include "para.h"
13 #include "string.h"
14 #include "afh.cmdline.h"
15 #include "fd.h"
16 #include "afh.h"
17 #include "error.h"
18 #include "version.h"
19
20 static struct afh_args_info conf;
21 INIT_AFH_ERRLISTS;
22
23 static int loglevel;
24 INIT_STDERR_LOGGING(loglevel)
25
26 static void print_info(int audio_format_num, struct afh_info *afhi)
27 {
28         char *msg;
29
30         afh_get_afhi_txt(audio_format_num, afhi, &msg);
31         printf("%s", msg);
32         free(msg);
33 }
34
35 static void print_chunk_table(struct afh_info *afhi)
36 {
37         int i;
38
39         if (!conf.human_given) {
40                 printf("chunk_table: ");
41                 for (i = 0; i <= afhi->chunks_total; i++)
42                         printf("%u ", afhi->chunk_table[i]);
43                 printf("\n");
44                 return;
45         }
46         for (i = 1; i <= afhi->chunks_total; i++) {
47                 struct timeval tv;
48                 long unsigned from, to;
49                 tv_scale(i - 1, &afhi->chunk_tv, &tv);
50                 from = tv2ms(&tv);
51                 tv_scale(i, &afhi->chunk_tv, &tv);
52                 to = tv2ms(&tv);
53                 printf("%d [%lu.%03lu - %lu.%03lu] %u - %u (%u)\n", i - 1,
54                         from / 1000, from % 1000, to / 1000, to % 1000,
55                         afhi->chunk_table[i - 1], afhi->chunk_table[i],
56                         afhi->chunk_table[i] - afhi->chunk_table[i - 1]);
57         }
58 }
59
60 static int cat_file(struct afh_info *afhi, int audio_format_id,
61                 void *audio_file_data, size_t audio_file_size)
62 {
63         int ret;
64         struct timeval stream_start;
65         long unsigned i, first_chunk, last_chunk;
66         const char *buf;
67         char *header;
68         size_t size;
69
70         if (conf.begin_chunk_arg < 0) {
71                 if (-conf.begin_chunk_arg > afhi->chunks_total)
72                         return -ERRNO_TO_PARA_ERROR(EINVAL);
73                 first_chunk = afhi->chunks_total + conf.begin_chunk_arg;
74         } else
75                 first_chunk = conf.begin_chunk_arg;
76         if (conf.end_chunk_given) {
77                 if (conf.end_chunk_arg < 0) {
78                         if (-conf.end_chunk_arg > afhi->chunks_total)
79                                 return -ERRNO_TO_PARA_ERROR(EINVAL);
80                         last_chunk = afhi->chunks_total + conf.end_chunk_arg;
81                 } else {
82                         if (conf.end_chunk_arg >= afhi->chunks_total)
83                                 return -ERRNO_TO_PARA_ERROR(EINVAL);
84                         last_chunk = conf.end_chunk_arg;
85                 }
86         } else
87                 last_chunk = afhi->chunks_total - 1;
88         if (first_chunk >= last_chunk)
89                 return -ERRNO_TO_PARA_ERROR(EINVAL);
90         if (!afhi->chunks_total)
91                 return 1;
92         /* eliminate the possibility of short writes */
93         ret = mark_fd_blocking(STDOUT_FILENO);
94         if (ret < 0)
95                 return ret;
96         if (first_chunk > 0 && !conf.no_header_given) {
97                 afh_get_header(afhi, audio_format_id, audio_file_data, audio_file_size,
98                         &header, &size);
99                 if (size > 0) {
100                         PARA_INFO_LOG("writing header (%zu bytes)\n", size);
101                         ret = write_all(STDOUT_FILENO, header, size);
102                         afh_free_header(header, audio_format_id);
103                         if (ret < 0)
104                                 return ret;
105                         if (ret != size)
106                                 return -E_AFH_SHORT_WRITE;
107                 }
108         }
109         PARA_NOTICE_LOG("writing chunks %lu - %lu\n", first_chunk, last_chunk);
110         gettimeofday(&stream_start, NULL);
111         for (i = first_chunk; i <= last_chunk; i++) {
112                 struct timeval now, diff, next_chunk;
113                 afh_get_chunk(i, afhi, audio_file_data, &buf, &size);
114                 PARA_DEBUG_LOG("chunk %lu: size %zu\n", i, size);
115                 if (conf.just_in_time_given) {
116                         compute_chunk_time(i - first_chunk, &afhi->chunk_tv,
117                                 &stream_start, &next_chunk);
118                         gettimeofday(&now, NULL);
119                         ret = tv_diff(&next_chunk, &now, &diff);
120                         if (ret > 0) {
121                                 ret = para_select(1, NULL, NULL, &diff);
122                                 if (ret < 0)
123                                         return ret;
124                         }
125                 }
126                 if (!size)
127                         continue;
128                 PARA_INFO_LOG("writing chunk %lu\n", i);
129                 ret = write_all(STDOUT_FILENO, buf, size);
130                 if (ret < 0)
131                         return ret;
132         }
133         return 1;
134 }
135
136 /**
137  * The main function of para_afh.
138  *
139  * \param argc Usual argument count.
140  * \param argv Usual argument vector.
141  *
142  * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
143  */
144 int main(int argc, char **argv)
145 {
146         int i, ret, audio_format_num, fd;
147         void *audio_file_data;
148         size_t audio_file_size;
149         struct afh_info afhi;
150
151         afh_cmdline_parser(argc, argv, &conf);
152         HANDLE_VERSION_FLAG("afh", conf);
153         loglevel = get_loglevel_by_name(conf.loglevel_arg);
154         ret = -E_AFH_SYNTAX;
155         if (conf.inputs_num == 0)
156                 goto out;
157         if (conf.stream_given && conf.inputs_num != 1)
158                 goto out;
159         afh_init();
160         for (i = 0; i < conf.inputs_num; i++) {
161                 int ret2;
162                 ret = mmap_full_file(conf.inputs[i], O_RDONLY, &audio_file_data,
163                         &audio_file_size, &fd);
164                 if (ret < 0) {
165                         PARA_ERROR_LOG("failed to mmap \"%s\"\n", conf.inputs[i]);
166                         goto out;
167                 }
168                 ret = compute_afhi(conf.inputs[i], audio_file_data, audio_file_size,
169                         fd, &afhi);
170                 if (ret < 0)
171                         goto out;
172
173                 audio_format_num = ret;
174                 if (conf.stream_given)
175                         ret = cat_file(&afhi, audio_format_num,
176                                 audio_file_data, audio_file_size);
177                 else {
178                         printf("File %d: %s\n", i + 1, conf.inputs[i]);
179                         print_info(audio_format_num, &afhi);
180                         if (conf.chunk_table_given)
181                                 print_chunk_table(&afhi);
182                         printf("\n");
183                 }
184                 clear_afhi(&afhi);
185                 ret2 = para_munmap(audio_file_data, audio_file_size);
186                 if (ret2 < 0 && ret >= 0)
187                         ret = ret2;
188                 if (ret < 0)
189                         break;
190         }
191 out:
192         if (ret < 0)
193                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
194         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
195 }