Use symbolic names for loglevels and clean up the ggo mess.
[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" /* tag info */
36 "%s: %lu: %lu\n" /* chunk time */
37 "%s: %lu\n", /* num chunks */
38 status_item_list[SI_BITRATE], afhi->bitrate,
39 status_item_list[SI_FORMAT], audio_format_name(audio_format_num),
40 status_item_list[SI_FREQUENCY], afhi->frequency,
41 status_item_list[SI_CHANNELS], afhi->channels,
42 status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
43 afhi->info_string,
44 status_item_list[SI_CHUNK_TIME], (long unsigned)afhi->chunk_tv.tv_sec,
45 (long unsigned)afhi->chunk_tv.tv_usec,
46 status_item_list[SI_NUM_CHUNKS], afhi->chunks_total
47 );
48 }
49
50 static void print_chunk_table(struct afh_info *afhi)
51 {
52 int i;
53
54 printf("chunk_table: ");
55 for (i = 0; i <= afhi->chunks_total; i++)
56 printf("%u ", afhi->chunk_table[i]);
57 printf("\n");
58 }
59
60 static int cat_file(void *audio_file_data, struct afh_info *afhi)
61 {
62 int ret;
63 struct timeval stream_start;
64 long unsigned i, first_chunk, last_chunk;
65 const char *buf;
66 size_t size;
67
68
69 if (conf.begin_chunk_arg < 0) {
70 if (-conf.begin_chunk_arg > afhi->chunks_total)
71 return -ERRNO_TO_PARA_ERROR(EINVAL);
72 first_chunk = afhi->chunks_total + conf.begin_chunk_arg;
73 } else
74 first_chunk = conf.begin_chunk_arg;
75 if (conf.end_chunk_given) {
76 if (conf.end_chunk_arg < 0) {
77 if (-conf.end_chunk_arg > afhi->chunks_total)
78 return -ERRNO_TO_PARA_ERROR(EINVAL);
79 last_chunk = afhi->chunks_total + conf.end_chunk_arg;
80 } else {
81 if (conf.end_chunk_arg >= afhi->chunks_total)
82 return -ERRNO_TO_PARA_ERROR(EINVAL);
83 last_chunk = conf.end_chunk_arg;
84 }
85 } else
86 last_chunk = afhi->chunks_total - 1;
87 if (first_chunk >= last_chunk)
88 return -ERRNO_TO_PARA_ERROR(EINVAL);
89 if (!afhi->chunks_total)
90 return 1;
91 afh_get_header(afhi, audio_file_data, &buf, &size);
92 if (size && first_chunk && !conf.no_header_given) {
93 PARA_INFO_LOG("writing audio file header (%zu bytes)\n", size);
94 ret = write(STDOUT_FILENO, buf, size);
95 if (ret < 0)
96 return ret;
97 if (ret != size)
98 return -E_AFH_SHORT_WRITE;
99 }
100 PARA_NOTICE_LOG("writing chunks %lu - %lu\n", first_chunk, last_chunk);
101 gettimeofday(&stream_start, NULL);
102 for (i = first_chunk; i <= last_chunk; i++) {
103 struct timeval now, diff, next_chunk;
104 afh_get_chunk(i, afhi, audio_file_data, &buf, &size);
105 PARA_DEBUG_LOG("chunk %lu: size %zu\n", i, size);
106 if (conf.just_in_time_given) {
107 compute_chunk_time(i - first_chunk, &afhi->chunk_tv,
108 &stream_start, &next_chunk);
109 gettimeofday(&now, NULL);
110 ret = tv_diff(&next_chunk, &now, &diff);
111 if (ret > 0) {
112 ret = para_select(1, NULL, NULL, &diff);
113 if (ret < 0)
114 return ret;
115 }
116 }
117 if (!size)
118 continue;
119 PARA_INFO_LOG("writing chunk %lu\n", i);
120 ret = write_all(STDOUT_FILENO, buf, &size);
121 if (ret < 0)
122 return ret;
123 }
124 return 1;
125 }
126
127 /**
128 * The main function of para_afh.
129 *
130 * \param argc Usual argument count.
131 * \param argv Usual argument vector.
132 *
133 * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
134 */
135 int main(int argc, char **argv)
136 {
137 int i, ret, audio_format_num, fd;
138 void *audio_file_data;
139 size_t audio_file_size;
140 struct afh_info afhi;
141
142 afh_cmdline_parser(argc, argv, &conf);
143 HANDLE_VERSION_FLAG("afh", conf);
144 loglevel = get_loglevel_by_name(conf.loglevel_arg);
145 ret = -E_AFH_SYNTAX;
146 if (conf.inputs_num == 0)
147 goto out;
148 if (conf.stream_given && conf.inputs_num != 1)
149 goto out;
150 afh_init();
151 for (i = 0; i < conf.inputs_num; i++) {
152 int ret2;
153 ret = mmap_full_file(conf.inputs[i], O_RDONLY, &audio_file_data,
154 &audio_file_size, &fd);
155 if (ret < 0)
156 goto out;
157 ret = compute_afhi(conf.inputs[i], audio_file_data, audio_file_size,
158 fd, &afhi);
159 if (ret < 0)
160 goto out;
161 audio_format_num = ret;
162 if (conf.stream_given)
163 ret = cat_file(audio_file_data, &afhi);
164 else {
165 printf("File %d: %s\n", i + 1, conf.inputs[i]);
166 print_info(audio_format_num, &afhi);
167 if (conf.chunk_table_given)
168 print_chunk_table(&afhi);
169 printf("\n");
170 }
171 ret2 = para_munmap(audio_file_data, audio_file_size);
172 if (ret2 < 0 && ret >= 0)
173 ret = ret2;
174 if (ret < 0)
175 break;
176 }
177 out:
178 if (ret < 0)
179 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
180 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
181 }