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 struct ggo_help h
= DEFINE_GGO_HELP(write
);
39 bool d
= conf
.detailed_help_given
;
41 ggo_print_help(&h
, d
? GPH_STANDARD_FLAGS_DETAILED
: GPH_STANDARD_FLAGS
);
42 print_writer_helps(d
? GPH_MODULE_FLAGS_DETAILED
: GPH_MODULE_FLAGS
);
47 * Parse config and register a task for a writer node.
49 * \param arg Command line arguments.
50 * \param parent The new node will be a child of \a parent.
51 * \param wn The writer node.
53 * If arg is \p NULL, the OS-dependent default writer is used with no
54 * arguments. The default writers are alsa for Linux, osx for OS X, oss for
55 * *BSD, and the file writer if the default writer is not supported.
57 * Once the writer configuration has been retrieved from the ->parse_config
58 * callback a writer node is created, its buffer tree node is added to the
59 * buffer tree as a child of the given parent.
61 * Finally, the new writer node's task structure is initialized and registered
62 * to the paraslash scheduler.
66 static void setup_writer_node(const char *arg
, struct btr_node
*parent
,
67 struct writer_node
*wn
, struct sched
*s
)
69 wn
->conf
= check_writer_arg_or_die(arg
, &wn
->writer_num
);
70 register_writer_node(wn
, parent
, s
);
75 struct check_wav_context
*cwc
;
78 static void write_pre_select(struct sched
*s
, struct task
*t
)
80 struct write_task
*wt
= container_of(t
, struct write_task
, task
);
81 check_wav_pre_select(s
, wt
->cwc
);
84 static int write_post_select(__a_unused
struct sched
*s
, struct task
*t
)
86 struct write_task
*wt
= container_of(t
, struct write_task
, task
);
87 return check_wav_post_select(wt
->cwc
);
90 static int setup_and_schedule(void)
93 struct btr_node
*cw_btrn
;
94 struct writer_node
*wns
;
95 static struct sched s
;
97 struct write_task wt
= {
99 .pre_select
= write_pre_select
,
100 .post_select
= write_post_select
,
101 .status
= "write task",
105 sit
.btrn
= btr_new_node(&(struct btr_node_description
)
106 EMBRACE(.name
= "stdin"));
107 stdin_set_defaults(&sit
);
108 register_task(&s
, &sit
.task
);
110 COPY_WAV_PARMS(&wp
, &conf
);
111 wt
.cwc
= check_wav_init(sit
.btrn
, NULL
, &wp
, &cw_btrn
);
112 register_task(&s
, &wt
.task
);
113 if (!conf
.writer_given
) {
114 wns
= para_calloc(sizeof(*wns
));
115 setup_writer_node(NULL
, cw_btrn
, wns
, &s
);
118 wns
= para_calloc(conf
.writer_given
* sizeof(*wns
));
119 for (i
= 0; i
< conf
.writer_given
; i
++)
120 setup_writer_node(conf
.writer_arg
[i
], cw_btrn
,
124 s
.default_timeout
.tv_sec
= 10;
125 s
.default_timeout
.tv_usec
= 50000;
129 for (j
= 0; j
< i
; j
++) {
130 struct task
*t
= &wns
[j
].task
;
131 assert(t
->error
< 0);
132 if (t
->error
!= -E_WRITE_COMMON_EOF
133 && t
->error
!= -E_BTR_EOF
) {
134 PARA_ERROR_LOG("%s: %s\n", t
->status
,
135 para_strerror(-t
->error
));
141 for (i
--; i
>= 0; i
--) {
142 struct writer_node
*wn
= wns
+ i
;
143 struct writer
*w
= writers
+ wn
->writer_num
;
146 btr_remove_node(&wn
->btrn
);
147 w
->free_config(wn
->conf
);
151 check_wav_shutdown(wt
.cwc
);
156 * Para_write's main function.
158 * \param argc The usual argument counter.
159 * \param argv The usual argument vector.
161 * It sets up and starts the tasks and the buffer tree nodes determined by
162 * command line options.
164 * \return \p EXIT_SUCCESS or EXIT_FAILURE
166 int main(int argc
, char *argv
[])
170 write_cmdline_parser(argc
, argv
, &conf
);
171 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
173 version_handle_flag("write", conf
.version_given
);
174 if (conf
.help_given
|| conf
.detailed_help_given
)
175 print_help_and_die();
177 ret
= setup_and_schedule();
179 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));