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