Fix two typos in stdin.c.
[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 return -ERRNO_TO_PARA_ERROR(EINVAL);
69 first_chunk = conf.begin_chunk_arg;
70
71 if (conf.end_chunk_given) {
72 if (conf.end_chunk_arg < 0)
73 return -ERRNO_TO_PARA_ERROR(EINVAL);
74 if (conf.end_chunk_arg >= afhi->chunks_total)
75 return -ERRNO_TO_PARA_ERROR(EINVAL);
76 last_chunk = conf.end_chunk_arg;
77 } else
78 last_chunk = afhi->chunks_total - 1;
79 if (!afhi->chunks_total)
80 return 1;
81 afh_get_header(afhi, audio_file_data, &buf, &size);
82 if (size && first_chunk && !conf.no_header_given) {
83 PARA_INFO_LOG("writing audio file header (%zu bytes)\n", size);
84 ret = write(STDOUT_FILENO, buf, size);
85 if (ret < 0)
86 return ret;
87 if (ret != size)
88 return -E_AFH_SHORT_WRITE;
89 }
90 PARA_NOTICE_LOG("writing chunks %lu - %lu\n", first_chunk, last_chunk);
91 gettimeofday(&stream_start, NULL);
92 for (i = first_chunk; i <= last_chunk; i++) {
93 struct timeval now, diff, next_chunk;
94 afh_get_chunk(i, afhi, audio_file_data, &buf, &size);
95 PARA_DEBUG_LOG("chunk %lu: size %zu\n", i, size);
96 if (conf.just_in_time_given) {
97 compute_chunk_time(i - first_chunk, &afhi->chunk_tv,
98 &stream_start, &next_chunk);
99 gettimeofday(&now, NULL);
100 ret = tv_diff(&next_chunk, &now, &diff);
101 if (ret > 0) {
102 ret = para_select(1, NULL, NULL, &diff);
103 if (ret < 0)
104 return ret;
105 }
106 }
107 PARA_INFO_LOG("writing chunk %lu\n", i);
108 ret = write_all(STDOUT_FILENO, buf, &size);
109 if (ret < 0)
110 return ret;
111 }
112 return 1;
113 }
114
115 int main(int argc, char **argv)
116 {
117 int ret, audio_format_num;
118 void *audio_file_data;
119 size_t audio_file_size;
120 struct afh_info afhi;
121
122 afh_cmdline_parser(argc, argv, &conf);
123 HANDLE_VERSION_FLAG("afh", conf);
124 ret = -E_AFH_SYNTAX;
125 if (conf.inputs_num != 1)
126 goto out;
127 afh_init();
128 ret = mmap_full_file(conf.inputs[0], O_RDONLY, &audio_file_data,
129 &audio_file_size, NULL);
130 if (ret < 0)
131 goto out;
132 ret = compute_afhi(conf.inputs[0], audio_file_data, audio_file_size, &afhi);
133 if (ret < 0)
134 goto out;
135 audio_format_num = ret;
136 if (conf.stream_given)
137 ret = cat_file(audio_file_data, &afhi);
138 else {
139 print_info(audio_format_num, &afhi);
140 if (conf.chunk_table_given)
141 print_chunk_table(&afhi);
142 ret = 1;
143 }
144 out:
145 if (ret < 0)
146 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
147 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
148 }