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