Teach command_util to print also completions.
[paraslash.git] / afh.c
1 /*
2  * Copyright (C) 2008-2011 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 /** The list of all status items */
22 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
23
24 INIT_AFH_ERRLISTS;
25
26 static int loglevel;
27 INIT_STDERR_LOGGING(loglevel)
28
29 static void print_info(int audio_format_num, struct afh_info *afhi)
30 {
31         printf("%s: %dkbit/s\n" /* bitrate */
32                 "%s: %s\n" /* format */
33                 "%s: %dHz\n" /* frequency */
34                 "%s: %d\n" /* channels */
35                 "%s: %lu\n" /* seconds total */
36                 "%s: %lu: %lu\n" /* chunk time */
37                 "%s: %lu\n" /* num chunks */
38                 "%s: %s\n" /* techinfo */
39                 "%s: %s\n" /* artist */
40                 "%s: %s\n" /* title */
41                 "%s: %s\n" /* year */
42                 "%s: %s\n" /* album */
43                 "%s: %s\n", /* comment */
44                 status_item_list[SI_BITRATE], afhi->bitrate,
45                 status_item_list[SI_FORMAT], audio_format_name(audio_format_num),
46                 status_item_list[SI_FREQUENCY], afhi->frequency,
47                 status_item_list[SI_CHANNELS], afhi->channels,
48                 status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
49                 status_item_list[SI_CHUNK_TIME], (long unsigned)afhi->chunk_tv.tv_sec,
50                         (long unsigned)afhi->chunk_tv.tv_usec,
51                 status_item_list[SI_NUM_CHUNKS], afhi->chunks_total,
52                 status_item_list[SI_TECHINFO], afhi->techinfo? afhi->techinfo : "",
53                 status_item_list[SI_ARTIST], afhi->tags.artist? afhi->tags.artist : "",
54                 status_item_list[SI_TITLE], afhi->tags.title? afhi->tags.title : "",
55                 status_item_list[SI_YEAR], afhi->tags.year? afhi->tags.year : "",
56                 status_item_list[SI_ALBUM], afhi->tags.album? afhi->tags.album : "",
57                 status_item_list[SI_COMMENT], afhi->tags.comment? afhi->tags.comment : ""
58         );
59 }
60
61 static void print_chunk_table(struct afh_info *afhi)
62 {
63         int i;
64
65         if (!conf.human_given) {
66                 printf("chunk_table: ");
67                 for (i = 0; i <= afhi->chunks_total; i++)
68                         printf("%u ", afhi->chunk_table[i]);
69                 printf("\n");
70                 return;
71         }
72         for (i = 1; i <= afhi->chunks_total; i++) {
73                 struct timeval tv;
74                 long unsigned from, to;
75                 tv_scale(i - 1, &afhi->chunk_tv, &tv);
76                 from = tv2ms(&tv);
77                 tv_scale(i, &afhi->chunk_tv, &tv);
78                 to = tv2ms(&tv);
79                 printf("%d [%lu.%03lu - %lu.%03lu] %u - %u (%u)\n", i - 1,
80                         from / 1000, from % 1000, to / 1000, to % 1000,
81                         afhi->chunk_table[i - 1], afhi->chunk_table[i],
82                         afhi->chunk_table[i] - afhi->chunk_table[i - 1]);
83         }
84 }
85
86 static int cat_file(struct afh_info *afhi, int audio_format_id,
87                 void *audio_file_data, size_t audio_file_size)
88 {
89         int ret;
90         struct timeval stream_start;
91         long unsigned i, first_chunk, last_chunk;
92         const char *buf;
93         char *header;
94         size_t size;
95
96         if (conf.begin_chunk_arg < 0) {
97                 if (-conf.begin_chunk_arg > afhi->chunks_total)
98                         return -ERRNO_TO_PARA_ERROR(EINVAL);
99                 first_chunk = afhi->chunks_total + conf.begin_chunk_arg;
100         } else
101                 first_chunk = conf.begin_chunk_arg;
102         if (conf.end_chunk_given) {
103                 if (conf.end_chunk_arg < 0) {
104                         if (-conf.end_chunk_arg > afhi->chunks_total)
105                                 return -ERRNO_TO_PARA_ERROR(EINVAL);
106                         last_chunk = afhi->chunks_total + conf.end_chunk_arg;
107                 } else {
108                         if (conf.end_chunk_arg >= afhi->chunks_total)
109                                 return -ERRNO_TO_PARA_ERROR(EINVAL);
110                         last_chunk = conf.end_chunk_arg;
111                 }
112         } else
113                 last_chunk = afhi->chunks_total - 1;
114         if (first_chunk >= last_chunk)
115                 return -ERRNO_TO_PARA_ERROR(EINVAL);
116         if (!afhi->chunks_total)
117                 return 1;
118         if (first_chunk > 0 && !conf.no_header_given) {
119                 afh_get_header(afhi, audio_format_id, audio_file_data, audio_file_size,
120                         &header, &size);
121                 if (size > 0) {
122                         PARA_INFO_LOG("writing header (%zu bytes)\n", size);
123                         ret = write(STDOUT_FILENO, header, size); /* FIXME */
124                         afh_free_header(header, audio_format_id);
125                         if (ret < 0)
126                                 return ret;
127                         if (ret != size)
128                                 return -E_AFH_SHORT_WRITE;
129                 }
130         }
131         PARA_NOTICE_LOG("writing chunks %lu - %lu\n", first_chunk, last_chunk);
132         gettimeofday(&stream_start, NULL);
133         for (i = first_chunk; i <= last_chunk; i++) {
134                 struct timeval now, diff, next_chunk;
135                 afh_get_chunk(i, afhi, audio_file_data, &buf, &size);
136                 PARA_DEBUG_LOG("chunk %lu: size %zu\n", i, size);
137                 if (conf.just_in_time_given) {
138                         compute_chunk_time(i - first_chunk, &afhi->chunk_tv,
139                                 &stream_start, &next_chunk);
140                         gettimeofday(&now, NULL);
141                         ret = tv_diff(&next_chunk, &now, &diff);
142                         if (ret > 0) {
143                                 ret = para_select(1, NULL, NULL, &diff);
144                                 if (ret < 0)
145                                         return ret;
146                         }
147                 }
148                 if (!size)
149                         continue;
150                 PARA_INFO_LOG("writing chunk %lu\n", i);
151                 ret = write_all(STDOUT_FILENO, buf, &size);
152                 if (ret < 0)
153                         return ret;
154         }
155         return 1;
156 }
157
158 /**
159  * The main function of para_afh.
160  *
161  * \param argc Usual argument count.
162  * \param argv Usual argument vector.
163  *
164  * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
165  */
166 int main(int argc, char **argv)
167 {
168         int i, ret, audio_format_num, fd;
169         void *audio_file_data;
170         size_t audio_file_size;
171         struct afh_info afhi;
172
173         afh_cmdline_parser(argc, argv, &conf);
174         HANDLE_VERSION_FLAG("afh", conf);
175         loglevel = get_loglevel_by_name(conf.loglevel_arg);
176         ret = -E_AFH_SYNTAX;
177         if (conf.inputs_num == 0)
178                 goto out;
179         if (conf.stream_given && conf.inputs_num != 1)
180                 goto out;
181         afh_init();
182         for (i = 0; i < conf.inputs_num; i++) {
183                 int ret2;
184                 ret = mmap_full_file(conf.inputs[i], O_RDONLY, &audio_file_data,
185                         &audio_file_size, &fd);
186                 if (ret < 0)
187                         goto out;
188                 ret = compute_afhi(conf.inputs[i], audio_file_data, audio_file_size,
189                         fd, &afhi);
190                 if (ret < 0)
191                         goto out;
192
193                 audio_format_num = ret;
194                 if (conf.stream_given)
195                         ret = cat_file(&afhi, audio_format_num,
196                                 audio_file_data, audio_file_size);
197                 else {
198                         printf("File %d: %s\n", i + 1, conf.inputs[i]);
199                         print_info(audio_format_num, &afhi);
200                         if (conf.chunk_table_given)
201                                 print_chunk_table(&afhi);
202                         printf("\n");
203                 }
204                 free(afhi.techinfo);
205                 free(afhi.tags.artist);
206                 free(afhi.tags.title);
207                 free(afhi.tags.year);
208                 free(afhi.tags.album);
209                 free(afhi.tags.comment);
210                 free(afhi.chunk_table);
211                 ret2 = para_munmap(audio_file_data, audio_file_size);
212                 if (ret2 < 0 && ret >= 0)
213                         ret = ret2;
214                 if (ret < 0)
215                         break;
216         }
217 out:
218         if (ret < 0)
219                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
220         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
221 }