2 * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file write.c Paraslash's standalone wav/raw player. */
10 #include <sys/types.h>
14 #include "write.cmdline.h"
19 #include "buffer_tree.h"
21 #include "write_common.h"
25 #include "check_wav.h"
29 static struct write_args_info conf
;
31 static struct stdin_task sit
;
34 INIT_STDERR_LOGGING(loglevel
)
36 __noreturn
static void print_help_and_die(void)
38 int d
= conf
.detailed_help_given
;
39 const char **p
= d
? write_args_info_detailed_help
40 : write_args_info_help
;
42 printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE
"-"
43 WRITE_CMDLINE_PARSER_VERSION
);
44 printf_or_die("%s\n\n", write_args_info_usage
);
46 printf_or_die("%s\n", *p
);
47 print_writer_helps(d
);
52 * Parse config and register a task for a writer node.
54 * \param arg Command line arguments.
55 * \param parent The new node will be a child of \a parent.
56 * \param wn The writer node.
58 * If arg is \p NULL, the OS-dependent default writer is used with no
59 * arguments. The default writers are alsa for Linux, osx for OS X, oss for
60 * *BSD, and the file writer if the default writer is not supported.
62 * Once the writer configuration has been retrieved from the ->parse_config
63 * callback a writer node is created, its buffer tree node is added to the
64 * buffer tree as a child of the given parent.
66 * Finally, the new writer node's task structure is initialized and registered
67 * to the paraslash scheduler.
71 static void setup_writer_node(const char *arg
, struct btr_node
*parent
,
72 struct writer_node
*wn
, struct sched
*s
)
74 wn
->conf
= check_writer_arg_or_die(arg
, &wn
->writer_num
);
75 register_writer_node(wn
, parent
, s
);
80 struct check_wav_context
*cwc
;
83 static void write_pre_select(struct sched
*s
, struct task
*t
)
85 struct write_task
*wt
= container_of(t
, struct write_task
, task
);
86 check_wav_pre_select(s
, wt
->cwc
);
89 static int write_post_select(__a_unused
struct sched
*s
, struct task
*t
)
91 struct write_task
*wt
= container_of(t
, struct write_task
, task
);
92 return check_wav_post_select(wt
->cwc
);
95 static int setup_and_schedule(void)
98 struct btr_node
*cw_btrn
;
99 struct writer_node
*wns
;
100 static struct sched s
;
101 struct wav_params wp
;
102 struct write_task wt
= {
104 .pre_select
= write_pre_select
,
105 .new_post_select
= write_post_select
,
106 .status
= "write task",
110 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
111 sit
.btrn
= btr_new_node(&(struct btr_node_description
)
112 EMBRACE(.name
= "stdin"));
113 stdin_set_defaults(&sit
);
114 register_task(&s
, &sit
.task
);
116 COPY_WAV_PARMS(&wp
, &conf
);
117 wt
.cwc
= check_wav_init(sit
.btrn
, NULL
, &wp
, &cw_btrn
);
118 register_task(&s
, &wt
.task
);
119 if (!conf
.writer_given
) {
120 wns
= para_calloc(sizeof(*wns
));
121 setup_writer_node(NULL
, cw_btrn
, wns
, &s
);
124 wns
= para_calloc(conf
.writer_given
* sizeof(*wns
));
125 for (i
= 0; i
< conf
.writer_given
; i
++)
126 setup_writer_node(conf
.writer_arg
[i
], cw_btrn
,
130 s
.default_timeout
.tv_sec
= 10;
131 s
.default_timeout
.tv_usec
= 50000;
135 for (j
= 0; j
< i
; j
++) {
136 struct task
*t
= &wns
[j
].task
;
137 assert(t
->error
< 0);
138 if (t
->error
!= -E_WRITE_COMMON_EOF
139 && t
->error
!= -E_BTR_EOF
) {
140 PARA_ERROR_LOG("%s: %s\n", t
->status
,
141 para_strerror(-t
->error
));
147 for (i
--; i
>= 0; i
--) {
148 struct writer_node
*wn
= wns
+ i
;
149 struct writer
*w
= writers
+ wn
->writer_num
;
152 btr_remove_node(&wn
->btrn
);
153 w
->free_config(wn
->conf
);
157 check_wav_shutdown(wt
.cwc
);
162 * Para_write's main function.
164 * \param argc The usual argument counter.
165 * \param argv The usual argument vector.
167 * It sets up and starts the tasks and the buffer tree nodes determined by
168 * command line options.
170 * \return \p EXIT_SUCCESS or EXIT_FAILURE
172 int main(int argc
, char *argv
[])
177 write_cmdline_parser(argc
, argv
, &conf
);
178 HANDLE_VERSION_FLAG("write", conf
);
179 if (conf
.help_given
|| conf
.detailed_help_given
)
180 print_help_and_die();
182 ret
= setup_and_schedule();
184 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));