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