2 * Copyright (C) 2008-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file afh.c Paraslash's standalone audio format handler tool. */
14 #include "afh.cmdline.h"
19 static struct afh_args_info conf;
20 /** The list of all status items */
21 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
26 INIT_STDERR_LOGGING(loglevel)
28 static void print_info(int audio_format_num, struct afh_info *afhi)
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 */
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,
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
50 static void print_chunk_table(struct afh_info *afhi)
54 printf("chunk_table: ");
55 for (i = 0; i <= afhi->chunks_total; i++)
56 printf("%u ", afhi->chunk_table[i]);
60 static int cat_file(void *audio_file_data, struct afh_info *afhi)
63 struct timeval stream_start;
64 long unsigned i, first_chunk, last_chunk;
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;
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;
81 if (conf.end_chunk_arg >= afhi->chunks_total)
82 return -ERRNO_TO_PARA_ERROR(EINVAL);
83 last_chunk = conf.end_chunk_arg;
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)
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);
98 return -E_AFH_SHORT_WRITE;
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);
112 ret = para_select(1, NULL, NULL, &diff);
119 PARA_INFO_LOG("writing chunk %lu\n", i);
120 ret = write_all(STDOUT_FILENO, buf, &size);
128 * The main function of para_afh.
130 * \param argc Usual argument count.
131 * \param argv Usual argument vector.
133 * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
135 int main(int argc, char **argv)
137 int i, ret, audio_format_num, fd;
138 void *audio_file_data;
139 size_t audio_file_size;
140 struct afh_info afhi;
142 afh_cmdline_parser(argc, argv, &conf);
143 HANDLE_VERSION_FLAG("afh", conf);
144 loglevel = get_loglevel_by_name(conf.loglevel_arg);
146 if (conf.inputs_num == 0)
148 if (conf.stream_given && conf.inputs_num != 1)
151 for (i = 0; i < conf.inputs_num; i++) {
153 ret = mmap_full_file(conf.inputs[i], O_RDONLY, &audio_file_data,
154 &audio_file_size, &fd);
157 ret = compute_afhi(conf.inputs[i], audio_file_data, audio_file_size,
161 audio_format_num = ret;
162 if (conf.stream_given)
163 ret = cat_file(audio_file_data, &afhi);
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);
171 ret2 = para_munmap(audio_file_data, audio_file_size);
172 if (ret2 < 0 && ret >= 0)
179 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
180 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;