Merge commit 'fml/master' into next
[paraslash.git] / afh.c
1 /*
2 * Copyright (C) 2008-2009 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
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 printf("chunk_table: ");
66 for (i = 0; i <= afhi->chunks_total; i++)
67 printf("%u ", afhi->chunk_table[i]);
68 printf("\n");
69 }
70
71 static int cat_file(void *audio_file_data, struct afh_info *afhi)
72 {
73 int ret;
74 struct timeval stream_start;
75 long unsigned i, first_chunk, last_chunk;
76 const char *buf;
77 size_t size;
78
79
80 if (conf.begin_chunk_arg < 0) {
81 if (-conf.begin_chunk_arg > afhi->chunks_total)
82 return -ERRNO_TO_PARA_ERROR(EINVAL);
83 first_chunk = afhi->chunks_total + conf.begin_chunk_arg;
84 } else
85 first_chunk = conf.begin_chunk_arg;
86 if (conf.end_chunk_given) {
87 if (conf.end_chunk_arg < 0) {
88 if (-conf.end_chunk_arg > afhi->chunks_total)
89 return -ERRNO_TO_PARA_ERROR(EINVAL);
90 last_chunk = afhi->chunks_total + conf.end_chunk_arg;
91 } else {
92 if (conf.end_chunk_arg >= afhi->chunks_total)
93 return -ERRNO_TO_PARA_ERROR(EINVAL);
94 last_chunk = conf.end_chunk_arg;
95 }
96 } else
97 last_chunk = afhi->chunks_total - 1;
98 if (first_chunk >= last_chunk)
99 return -ERRNO_TO_PARA_ERROR(EINVAL);
100 if (!afhi->chunks_total)
101 return 1;
102 afh_get_header(afhi, audio_file_data, &buf, &size);
103 if (size && first_chunk && !conf.no_header_given) {
104 PARA_INFO_LOG("writing audio file header (%zu bytes)\n", size);
105 ret = write(STDOUT_FILENO, buf, size);
106 if (ret < 0)
107 return ret;
108 if (ret != size)
109 return -E_AFH_SHORT_WRITE;
110 }
111 PARA_NOTICE_LOG("writing chunks %lu - %lu\n", first_chunk, last_chunk);
112 gettimeofday(&stream_start, NULL);
113 for (i = first_chunk; i <= last_chunk; i++) {
114 struct timeval now, diff, next_chunk;
115 afh_get_chunk(i, afhi, audio_file_data, &buf, &size);
116 PARA_DEBUG_LOG("chunk %lu: size %zu\n", i, size);
117 if (conf.just_in_time_given) {
118 compute_chunk_time(i - first_chunk, &afhi->chunk_tv,
119 &stream_start, &next_chunk);
120 gettimeofday(&now, NULL);
121 ret = tv_diff(&next_chunk, &now, &diff);
122 if (ret > 0) {
123 ret = para_select(1, NULL, NULL, &diff);
124 if (ret < 0)
125 return ret;
126 }
127 }
128 if (!size)
129 continue;
130 PARA_INFO_LOG("writing chunk %lu\n", i);
131 ret = write_all(STDOUT_FILENO, buf, &size);
132 if (ret < 0)
133 return ret;
134 }
135 return 1;
136 }
137
138 /**
139 * The main function of para_afh.
140 *
141 * \param argc Usual argument count.
142 * \param argv Usual argument vector.
143 *
144 * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
145 */
146 int main(int argc, char **argv)
147 {
148 int i, ret, audio_format_num, fd;
149 void *audio_file_data;
150 size_t audio_file_size;
151 struct afh_info afhi;
152
153 afh_cmdline_parser(argc, argv, &conf);
154 HANDLE_VERSION_FLAG("afh", conf);
155 loglevel = get_loglevel_by_name(conf.loglevel_arg);
156 ret = -E_AFH_SYNTAX;
157 if (conf.inputs_num == 0)
158 goto out;
159 if (conf.stream_given && conf.inputs_num != 1)
160 goto out;
161 afh_init();
162 for (i = 0; i < conf.inputs_num; i++) {
163 int ret2;
164 ret = mmap_full_file(conf.inputs[i], O_RDONLY, &audio_file_data,
165 &audio_file_size, &fd);
166 if (ret < 0)
167 goto out;
168 ret = compute_afhi(conf.inputs[i], audio_file_data, audio_file_size,
169 fd, &afhi);
170 if (ret < 0)
171 goto out;
172 audio_format_num = ret;
173 if (conf.stream_given)
174 ret = cat_file(audio_file_data, &afhi);
175 else {
176 printf("File %d: %s\n", i + 1, conf.inputs[i]);
177 print_info(audio_format_num, &afhi);
178 if (conf.chunk_table_given)
179 print_chunk_table(&afhi);
180 free(afhi.techinfo);
181 free(afhi.tags.artist);
182 free(afhi.tags.title);
183 free(afhi.tags.year);
184 free(afhi.tags.album);
185 free(afhi.tags.comment);
186 free(afhi.chunk_table);
187 printf("\n");
188 }
189 ret2 = para_munmap(audio_file_data, audio_file_size);
190 if (ret2 < 0 && ret >= 0)
191 ret = ret2;
192 if (ret < 0)
193 break;
194 }
195 out:
196 if (ret < 0)
197 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
198 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
199 }