b684661b4aa04ccdfd736b6319bf857c74080498
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(__a_unused
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 if (!wn
->writer
->pre_select
)
32 t
->error
= wn
->writer
->pre_select(s
, wn
);
38 static void wng_post_select(struct sched
*s
, struct task
*t
)
40 struct writer_node_group
*g
= container_of(t
, struct writer_node_group
, task
);
42 size_t min_written
= 0;
44 FOR_EACH_WRITER_NODE(i
, g
) {
45 struct writer_node
*wn
= &g
->writer_nodes
[i
];
46 t
->error
= wn
->writer
->post_select(s
, wn
);
50 min_written
= wn
->written
;
52 min_written
= PARA_MIN(min_written
, wn
->written
);
54 //PARA_INFO_LOG("loaded: %zd, min_written: %zd bytes\n", *g->loaded, min_written);
56 *g
->loaded
-= min_written
;
57 FOR_EACH_WRITER_NODE(i
, g
)
58 g
->writer_nodes
[i
].written
-= min_written
;
60 if (!*g
->loaded
&& *g
->input_error
) {
61 t
->error
= *g
->input_error
;
64 if (*g
->loaded
&& min_written
) {
65 // PARA_INFO_LOG("moving %zd bytes\n", *g->loaded);
66 memmove(*g
->bufp
, *g
->bufp
+ min_written
, *g
->loaded
);
71 * call the open function of each writer in the group
73 * \param g the writer node group
75 * \return If at least one open function returned an error, all successful
76 * writer notes get closed and this error value is returned. Upon success, a
77 * task associated with \a g is registered to the scheduler and the function
78 * returns a positive value.
80 int wng_open(struct writer_node_group
*g
)
84 PARA_NOTICE_LOG("opening wng %p with %d writer(s)\n", g
, g
->num_writers
);
85 FOR_EACH_WRITER_NODE(i
, g
) {
86 struct writer_node
*wn
= &g
->writer_nodes
[i
];
88 ret
= wn
->writer
->open(wn
);
92 sprintf(g
->task
.status
, "%s", "writer node group");
93 register_task(&g
->task
);
97 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
99 struct writer_node
*wn
= &g
->writer_nodes
[--i
];
100 wn
->writer
->close(wn
);
107 * call the close function of each writer in the given group
109 * \param g the writer node group to close
111 * This function also frees all resources of the given group.
113 void wng_close(struct writer_node_group
*g
)
119 PARA_NOTICE_LOG("closing wng with %d writer(s)\n", g
->num_writers
);
120 FOR_EACH_WRITER_NODE(i
, g
) {
121 struct writer_node
*wn
= &g
->writer_nodes
[i
];
122 wn
->writer
->close(wn
);
124 free(g
->writer_nodes
);
129 * allocate and initialize a new writer_node_group struct
131 * \param num_writers the number of writer nodes for the new group
133 * \return Pointer to the new writer node group
135 struct writer_node_group
*wng_new(unsigned num_writers
)
137 struct writer_node_group
*g
= para_calloc(sizeof(struct writer_node_group
));
138 g
->num_writers
= num_writers
;
139 g
->writer_nodes
= para_calloc(num_writers
140 * sizeof(struct writer_node
));
141 g
->task
.post_select
= wng_post_select
;
142 g
->task
.pre_select
= wng_pre_select
;
147 * Call the init function of each supported paraslash writer.
149 void writer_init(void)
154 writers
[i
].init(&writers
[i
]);
157 * check if given string is a valid command line for any writer
159 * \param \wa string of the form writer_name:options
160 * \param writer_num contains the number of the writer upon success
162 * This function checks whether \a wa starts with the name of a supported
163 * paraslash writer, optionally followed by a colon and any options for that
164 * writer. If a valid writer name was found and further are present, the
165 * remaining part of \a wa is passed to that writer's config parser.
167 * \return On success, a pointer to the gengetopt args info struct is returned
168 * and \a writer_num contains the number of the writer. Otherwise this function
171 void *check_writer_arg(const char *wa
, int *writer_num
)
175 *writer_num
= -E_WRITE_COMMON_SYNTAX
;
176 PARA_INFO_LOG("checking %s\n", wa
);
178 const char *name
= writer_names
[i
];
179 size_t len
= strlen(name
);
181 if (strlen(wa
) < len
)
183 if (strncmp(name
, wa
, len
))
188 if (c
&& !writers
[i
].parse_config
)
191 return writers
[i
].parse_config(c
? wa
+ len
+ 1 : "");
193 PARA_ERROR_LOG("writer not found\n");
198 * setup a writer node group with only one writer, the default writer
200 * The writer which is set up depends on the OS. It defaults to alsa for Linux,
201 * osx_write for OS X, file writer if neither of these is supported.
203 * \return pointer to the allocated writer node group
205 struct writer_node_group
*setup_default_wng(void)
207 struct writer_node_group
*wng
= wng_new(1);
208 wng
->writer_nodes
[0].writer
= &writers
[DEFAULT_WRITER
];
209 PARA_INFO_LOG("using default writer: %s %p\n",
210 writer_names
[DEFAULT_WRITER
], writers
[DEFAULT_WRITER
].parse_config
);
211 wng
->writer_nodes
[0].conf
= writers
[DEFAULT_WRITER
].parse_config("");
215 void print_writer_helps(int detailed
)
219 printf_or_die("\nAvailable writers: \n\t");
221 printf_or_die("%s%s", i
? " " : "", writer_names
[i
]);
222 printf_or_die("\n\n");
224 struct writer
*w
= writers
+ i
;
226 if (!w
->help
.short_help
)
228 printf_or_die("Options for %s:\n", writer_names
[i
]);
229 ggo_print_help(&w
->help
, detailed
);