para_gui ggo: Do not pass --no-handle-error.
[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         printf("%s: %dkbit/s\n" /* bitrate */
29                 "%s: %s\n" /* format */
30                 "%s: %dHz\n" /* frequency */
31                 "%s: %d\n" /* channels */
32                 "%s: %lu\n" /* seconds total */
33                 "%s: %lu: %lu\n" /* chunk time */
34                 "%s: %lu\n" /* num chunks */
35                 "%s: %s\n" /* techinfo */
36                 "%s: %s\n" /* artist */
37                 "%s: %s\n" /* title */
38                 "%s: %s\n" /* year */
39                 "%s: %s\n" /* album */
40                 "%s: %s\n", /* comment */
41                 status_item_list[SI_BITRATE], afhi->bitrate,
42                 status_item_list[SI_FORMAT], audio_format_name(audio_format_num),
43                 status_item_list[SI_FREQUENCY], afhi->frequency,
44                 status_item_list[SI_CHANNELS], afhi->channels,
45                 status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
46                 status_item_list[SI_CHUNK_TIME], (long unsigned)afhi->chunk_tv.tv_sec,
47                         (long unsigned)afhi->chunk_tv.tv_usec,
48                 status_item_list[SI_NUM_CHUNKS], afhi->chunks_total,
49                 status_item_list[SI_TECHINFO], afhi->techinfo? afhi->techinfo : "",
50                 status_item_list[SI_ARTIST], afhi->tags.artist? afhi->tags.artist : "",
51                 status_item_list[SI_TITLE], afhi->tags.title? afhi->tags.title : "",
52                 status_item_list[SI_YEAR], afhi->tags.year? afhi->tags.year : "",
53                 status_item_list[SI_ALBUM], afhi->tags.album? afhi->tags.album : "",
54                 status_item_list[SI_COMMENT], afhi->tags.comment? afhi->tags.comment : ""
55         );
56 }
57
58 static void print_chunk_table(struct afh_info *afhi)
59 {
60         int i;
61
62         if (!conf.human_given) {
63                 printf("chunk_table: ");
64                 for (i = 0; i <= afhi->chunks_total; i++)
65                         printf("%u ", afhi->chunk_table[i]);
66                 printf("\n");
67                 return;
68         }
69         for (i = 1; i <= afhi->chunks_total; i++) {
70                 struct timeval tv;
71                 long unsigned from, to;
72                 tv_scale(i - 1, &afhi->chunk_tv, &tv);
73                 from = tv2ms(&tv);
74                 tv_scale(i, &afhi->chunk_tv, &tv);
75                 to = tv2ms(&tv);
76                 printf("%d [%lu.%03lu - %lu.%03lu] %u - %u (%u)\n", i - 1,
77                         from / 1000, from % 1000, to / 1000, to % 1000,
78                         afhi->chunk_table[i - 1], afhi->chunk_table[i],
79                         afhi->chunk_table[i] - afhi->chunk_table[i - 1]);
80         }
81 }
82
83 static int cat_file(struct afh_info *afhi, int audio_format_id,
84                 void *audio_file_data, size_t audio_file_size)
85 {
86         int ret;
87         struct timeval stream_start;
88         long unsigned i, first_chunk, last_chunk;
89         const char *buf;
90         char *header;
91         size_t size;
92
93         if (conf.begin_chunk_arg < 0) {
94                 if (-conf.begin_chunk_arg > afhi->chunks_total)
95                         return -ERRNO_TO_PARA_ERROR(EINVAL);
96                 first_chunk = afhi->chunks_total + conf.begin_chunk_arg;
97         } else
98                 first_chunk = conf.begin_chunk_arg;
99         if (conf.end_chunk_given) {
100                 if (conf.end_chunk_arg < 0) {
101                         if (-conf.end_chunk_arg > afhi->chunks_total)
102                                 return -ERRNO_TO_PARA_ERROR(EINVAL);
103                         last_chunk = afhi->chunks_total + conf.end_chunk_arg;
104                 } else {
105                         if (conf.end_chunk_arg >= afhi->chunks_total)
106                                 return -ERRNO_TO_PARA_ERROR(EINVAL);
107                         last_chunk = conf.end_chunk_arg;
108                 }
109         } else
110                 last_chunk = afhi->chunks_total - 1;
111         if (first_chunk >= last_chunk)
112                 return -ERRNO_TO_PARA_ERROR(EINVAL);
113         if (!afhi->chunks_total)
114                 return 1;
115         /* eliminate the possibility of short writes */
116         ret = mark_fd_blocking(STDOUT_FILENO);
117         if (ret < 0)
118                 return ret;
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_all(STDOUT_FILENO, header, size);
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                         PARA_ERROR_LOG("failed to mmap \"%s\"\n", conf.inputs[i]);
189                         goto out;
190                 }
191                 ret = compute_afhi(conf.inputs[i], audio_file_data, audio_file_size,
192                         fd, &afhi);
193                 if (ret < 0)
194                         goto out;
195
196                 audio_format_num = ret;
197                 if (conf.stream_given)
198                         ret = cat_file(&afhi, audio_format_num,
199                                 audio_file_data, audio_file_size);
200                 else {
201                         printf("File %d: %s\n", i + 1, conf.inputs[i]);
202                         print_info(audio_format_num, &afhi);
203                         if (conf.chunk_table_given)
204                                 print_chunk_table(&afhi);
205                         printf("\n");
206                 }
207                 clear_afhi(&afhi);
208                 ret2 = para_munmap(audio_file_data, audio_file_size);
209                 if (ret2 < 0 && ret >= 0)
210                         ret = ret2;
211                 if (ret < 0)
212                         break;
213         }
214 out:
215         if (ret < 0)
216                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
217         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
218 }