audioc: Make all fds non-blocking.
[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 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 int main(int argc, char **argv)
124 {
125 int ret, audio_format_num;
126 void *audio_file_data;
127 size_t audio_file_size;
128 struct afh_info afhi;
129
130 afh_cmdline_parser(argc, argv, &conf);
131 HANDLE_VERSION_FLAG("afh", conf);
132 ret = -E_AFH_SYNTAX;
133 if (conf.inputs_num != 1)
134 goto out;
135 afh_init();
136 ret = mmap_full_file(conf.inputs[0], O_RDONLY, &audio_file_data,
137 &audio_file_size, NULL);
138 if (ret < 0)
139 goto out;
140 ret = compute_afhi(conf.inputs[0], audio_file_data, audio_file_size, &afhi);
141 if (ret < 0)
142 goto out;
143 audio_format_num = ret;
144 if (conf.stream_given)
145 ret = cat_file(audio_file_data, &afhi);
146 else {
147 print_info(audio_format_num, &afhi);
148 if (conf.chunk_table_given)
149 print_chunk_table(&afhi);
150 ret = 1;
151 }
152 out:
153 if (ret < 0)
154 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
155 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
156 }