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