2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file write_common.c common functions of para_audiod and para_write */
17 /** the array containing the names of all supported writers */
18 const char *writer_names
[] ={WRITER_NAMES
};
20 /** the array of supported writers */
21 struct writer writers
[NUM_SUPPORTED_WRITERS
] = {WRITER_ARRAY
};
23 static void wng_pre_select(struct sched
*s
, struct task
*t
)
25 struct writer_node_group
*g
= container_of(t
, struct writer_node_group
, task
);
28 FOR_EACH_WRITER_NODE(i
, g
) {
29 struct writer_node
*wn
= &g
->writer_nodes
[i
];
30 struct writer
*w
= writers
+ wn
->writer_num
;
33 t
->error
= w
->pre_select(s
, wn
);
38 * Force a minimal delay if something was written during the previous
39 * call to wng_post_select(). This is necessary because the filter
40 * chain might still have data for us which it couldn't convert during
41 * the previous run due to its buffer size constraints. In this case we
42 * do not want to wait until the next input data arrives as this could
43 * lead to buffer underruns.
45 if (g
->last_written
== 0)
47 s
->timeout
.tv_sec
= 0;
48 s
->timeout
.tv_usec
= 1;
51 static void wng_post_select(struct sched
*s
, struct task
*t
)
53 struct writer_node_group
*g
= container_of(t
, struct writer_node_group
, task
);
55 size_t min_written
= 0, max_written
= 0;
57 FOR_EACH_WRITER_NODE(i
, g
) {
58 struct writer_node
*wn
= &g
->writer_nodes
[i
];
59 struct writer
*w
= writers
+ wn
->writer_num
;
60 t
->error
= w
->post_select(s
, wn
);
64 min_written
= wn
->written
;
66 min_written
= PARA_MIN(min_written
, wn
->written
);
67 max_written
= PARA_MAX(max_written
, wn
->written
);
69 g
->last_written
= max_written
;
70 //PARA_INFO_LOG("loaded: %zd, min_written: %zd bytes\n", *g->loaded, min_written);
72 *g
->loaded
-= min_written
;
73 FOR_EACH_WRITER_NODE(i
, g
)
74 g
->writer_nodes
[i
].written
-= min_written
;
76 if (!*g
->loaded
&& *g
->input_error
) {
77 t
->error
= *g
->input_error
;
80 if (*g
->loaded
&& min_written
) {
81 // PARA_INFO_LOG("moving %zd bytes\n", *g->loaded);
82 memmove(*g
->bufp
, *g
->bufp
+ min_written
, *g
->loaded
);
87 * call the open function of each writer in the group
89 * \param g the writer node group
91 * \return If at least one open function returned an error, all successful
92 * writer notes get closed and this error value is returned. Upon success, a
93 * task associated with \a g is registered to the scheduler and the function
94 * returns a positive value.
96 int wng_open(struct writer_node_group
*g
)
100 PARA_NOTICE_LOG("opening wng %p with %d writer(s)\n", g
, g
->num_writers
);
101 FOR_EACH_WRITER_NODE(i
, g
) {
102 struct writer_node
*wn
= &g
->writer_nodes
[i
];
103 struct writer
*w
= writers
+ wn
->writer_num
;
109 sprintf(g
->task
.status
, "%s", "writer node group");
110 register_task(&g
->task
);
114 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
116 struct writer_node
*wn
= &g
->writer_nodes
[--i
];
117 struct writer
*w
= writers
+ wn
->writer_num
;
120 free(g
->writer_nodes
);
122 g
->task
.error
= -E_TASK_UNREGISTERED
;
127 * call the close function of each writer in the given group
129 * \param g the writer node group to close
131 * This function also frees all resources of the given group.
133 void wng_close(struct writer_node_group
*g
)
139 PARA_NOTICE_LOG("closing wng with %d writer(s)\n", g
->num_writers
);
140 FOR_EACH_WRITER_NODE(i
, g
) {
141 struct writer_node
*wn
= &g
->writer_nodes
[i
];
142 struct writer
*w
= writers
+ wn
->writer_num
;
145 free(g
->writer_nodes
);
150 * allocate and initialize a new writer_node_group struct
152 * \param num_writers the number of writer nodes for the new group
154 * \return Pointer to the new writer node group
156 struct writer_node_group
*wng_new(unsigned num_writers
)
158 struct writer_node_group
*g
= para_calloc(sizeof(struct writer_node_group
));
159 g
->num_writers
= num_writers
;
160 g
->writer_nodes
= para_calloc(num_writers
161 * sizeof(struct writer_node
));
162 g
->task
.post_select
= wng_post_select
;
163 g
->task
.pre_select
= wng_pre_select
;
168 * Call the init function of each supported paraslash writer.
170 void writer_init(void)
175 writers
[i
].init(&writers
[i
]);
178 * check if given string is a valid command line for any writer
180 * \param \wa string of the form writer_name:options
181 * \param writer_num contains the number of the writer upon success
183 * This function checks whether \a wa starts with the name of a supported
184 * paraslash writer, optionally followed by a colon and any options for that
185 * writer. If a valid writer name was found and further are present, the
186 * remaining part of \a wa is passed to that writer's config parser.
188 * \return On success, a pointer to the gengetopt args info struct is returned
189 * and \a writer_num contains the number of the writer. Otherwise this function
192 void *check_writer_arg(const char *wa
, int *writer_num
)
196 *writer_num
= -E_WRITE_COMMON_SYNTAX
;
197 PARA_INFO_LOG("checking %s\n", wa
);
199 const char *name
= writer_names
[i
];
200 size_t len
= strlen(name
);
202 if (strlen(wa
) < len
)
204 if (strncmp(name
, wa
, len
))
209 if (c
&& !writers
[i
].parse_config
)
212 return writers
[i
].parse_config(c
? wa
+ len
+ 1 : "");
214 PARA_ERROR_LOG("writer not found\n");
219 * setup a writer node group with only one writer, the default writer
221 * The writer which is set up depends on the OS. It defaults to alsa for Linux,
222 * osx_write for OS X, file writer if neither of these is supported.
224 * \return pointer to the allocated writer node group
226 struct writer_node_group
*setup_default_wng(void)
228 struct writer_node_group
*wng
= wng_new(1);
229 wng
->writer_nodes
[0].writer_num
= DEFAULT_WRITER
;
230 PARA_INFO_LOG("using default writer: %s %p\n",
231 writer_names
[DEFAULT_WRITER
], writers
[DEFAULT_WRITER
].parse_config
);
232 wng
->writer_nodes
[0].conf
= writers
[DEFAULT_WRITER
].parse_config("");
236 * Print the help text of all writers to stdout.
238 * \param detailed Whether to print the detailed help text.
240 void print_writer_helps(int detailed
)
244 printf_or_die("\nAvailable writers: \n\t");
246 printf_or_die("%s%s", i
? " " : "", writer_names
[i
]);
247 printf_or_die("\n\n");
249 struct writer
*w
= writers
+ i
;
251 if (!w
->help
.short_help
)
253 printf_or_die("Options for %s:\n", writer_names
[i
]);
254 ggo_print_help(&w
->help
, detailed
);