| 1 | /* |
| 2 | * Copyright (c) 2015, 2020 Moritz Grimm <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #ifdef HAVE_CONFIG_H |
| 18 | # include "config.h" |
| 19 | #endif /* HAVE_CONFIG_H */ |
| 20 | |
| 21 | #include <sys/queue.h> |
| 22 | |
| 23 | #include <assert.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "cfg_private.h" |
| 27 | #include "cfg_encoder.h" |
| 28 | #include "xalloc.h" |
| 29 | |
| 30 | struct cfg_encoder { |
| 31 | TAILQ_ENTRY(cfg_encoder) entry; |
| 32 | char *name; |
| 33 | enum cfg_stream_format format; |
| 34 | char *program; |
| 35 | }; |
| 36 | |
| 37 | TAILQ_HEAD(cfg_encoder_list, cfg_encoder); |
| 38 | |
| 39 | struct cfg_encoder_list * |
| 40 | cfg_encoder_list_create(void) |
| 41 | { |
| 42 | struct cfg_encoder_list *el; |
| 43 | |
| 44 | el = xcalloc(1UL, sizeof(*el)); |
| 45 | TAILQ_INIT(el); |
| 46 | |
| 47 | return (el); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | cfg_encoder_list_destroy(struct cfg_encoder_list **el_p) |
| 52 | { |
| 53 | struct cfg_encoder_list *el = *el_p; |
| 54 | struct cfg_encoder *e; |
| 55 | |
| 56 | if (!el) |
| 57 | return; |
| 58 | |
| 59 | while (NULL != (e = TAILQ_FIRST(el))) |
| 60 | cfg_encoder_list_remove(el, &e); |
| 61 | |
| 62 | xfree(el); |
| 63 | *el_p = NULL; |
| 64 | } |
| 65 | |
| 66 | unsigned int |
| 67 | cfg_encoder_list_nentries(struct cfg_encoder_list *el) |
| 68 | { |
| 69 | struct cfg_encoder *e; |
| 70 | unsigned int n = 0; |
| 71 | |
| 72 | TAILQ_FOREACH(e, el, entry) { |
| 73 | n++; |
| 74 | } |
| 75 | |
| 76 | return (n); |
| 77 | } |
| 78 | |
| 79 | struct cfg_encoder * |
| 80 | cfg_encoder_list_find(struct cfg_encoder_list *el, const char *name) |
| 81 | { |
| 82 | struct cfg_encoder *e; |
| 83 | |
| 84 | TAILQ_FOREACH(e, el, entry) { |
| 85 | if (0 == strcasecmp(e->name, name)) |
| 86 | return (e); |
| 87 | } |
| 88 | |
| 89 | return (NULL); |
| 90 | } |
| 91 | |
| 92 | struct cfg_encoder * |
| 93 | cfg_encoder_list_get(struct cfg_encoder_list *el, const char *name) |
| 94 | { |
| 95 | struct cfg_encoder *e; |
| 96 | |
| 97 | e = cfg_encoder_list_find(el, name); |
| 98 | if (e) |
| 99 | return (e); |
| 100 | e = cfg_encoder_create(name); |
| 101 | if (!e) |
| 102 | return (NULL); |
| 103 | |
| 104 | TAILQ_INSERT_TAIL(el, e, entry); |
| 105 | |
| 106 | return (e); |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | cfg_encoder_list_foreach(struct cfg_encoder_list *el, |
| 111 | void (*cb)(cfg_encoder_t, void *), void *cb_arg) |
| 112 | { |
| 113 | struct cfg_encoder *e; |
| 114 | |
| 115 | TAILQ_FOREACH(e, el, entry) { |
| 116 | cb(e, cb_arg); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | cfg_encoder_list_remove(struct cfg_encoder_list *el, struct cfg_encoder **e_p) |
| 122 | { |
| 123 | TAILQ_REMOVE(el, *e_p, entry); |
| 124 | cfg_encoder_destroy(e_p); |
| 125 | } |
| 126 | |
| 127 | struct cfg_encoder * |
| 128 | cfg_encoder_create(const char *name) |
| 129 | { |
| 130 | struct cfg_encoder *e; |
| 131 | |
| 132 | if (!name || !name[0]) |
| 133 | return (NULL); |
| 134 | |
| 135 | e = xcalloc(1UL, sizeof(*e)); |
| 136 | e->name = xstrdup(name); |
| 137 | |
| 138 | return (e); |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | cfg_encoder_destroy(struct cfg_encoder **e_p) |
| 143 | { |
| 144 | struct cfg_encoder *e = *e_p; |
| 145 | |
| 146 | xfree(e->name); |
| 147 | xfree(e->program); |
| 148 | xfree(e); |
| 149 | *e_p = NULL; |
| 150 | } |
| 151 | |
| 152 | int |
| 153 | cfg_encoder_set_format(struct cfg_encoder *e, enum cfg_stream_format fmt) |
| 154 | { |
| 155 | assert(CFG_STREAM_MIN <= fmt && CFG_STREAM_MAX >= fmt); |
| 156 | e->format = fmt; |
| 157 | return (0); |
| 158 | } |
| 159 | |
| 160 | int |
| 161 | cfg_encoder_set_name(struct cfg_encoder *e, struct cfg_encoder_list *el, |
| 162 | const char *name, const char **errstrp) |
| 163 | { |
| 164 | struct cfg_encoder *e2; |
| 165 | |
| 166 | if (!name || !name[0]) { |
| 167 | if (errstrp) |
| 168 | *errstrp = "empty"; |
| 169 | return (-1); |
| 170 | } |
| 171 | |
| 172 | e2 = cfg_encoder_list_find(el, name); |
| 173 | if (e2 && e2 != e) { |
| 174 | if (errstrp) |
| 175 | *errstrp = "already exists"; |
| 176 | return (-1); |
| 177 | } |
| 178 | |
| 179 | SET_XSTRDUP(e->name, name, errstrp); |
| 180 | |
| 181 | return (0); |
| 182 | } |
| 183 | |
| 184 | int |
| 185 | cfg_encoder_set_format_str(struct cfg_encoder *e, |
| 186 | struct cfg_encoder_list *not_used, const char *fmt_str, |
| 187 | const char **errstrp) |
| 188 | { |
| 189 | enum cfg_stream_format fmt; |
| 190 | |
| 191 | (void)not_used; |
| 192 | |
| 193 | if (!fmt_str || !fmt_str[0]) { |
| 194 | if (errstrp) |
| 195 | *errstrp = "empty"; |
| 196 | return (-1); |
| 197 | } |
| 198 | |
| 199 | if (0 > cfg_stream_str2fmt(fmt_str, &fmt)) { |
| 200 | if (errstrp) |
| 201 | *errstrp = "unsupported stream format"; |
| 202 | return (-1); |
| 203 | } |
| 204 | |
| 205 | cfg_encoder_set_format(e, fmt); |
| 206 | |
| 207 | return (0); |
| 208 | } |
| 209 | |
| 210 | int |
| 211 | cfg_encoder_set_program(struct cfg_encoder *e, |
| 212 | struct cfg_encoder_list *not_used, const char *program, |
| 213 | const char **errstrp) |
| 214 | { |
| 215 | (void)not_used; |
| 216 | |
| 217 | if (!program || !program[0]) { |
| 218 | if (errstrp) |
| 219 | *errstrp = "empty"; |
| 220 | return (-1); |
| 221 | } |
| 222 | |
| 223 | xfree(e->program); |
| 224 | e->program = xstrdup(program); |
| 225 | |
| 226 | return (0); |
| 227 | } |
| 228 | |
| 229 | int |
| 230 | cfg_encoder_validate(struct cfg_encoder *e, const char **errstrp) |
| 231 | { |
| 232 | if (CFG_STREAM_INVALID == e->format) { |
| 233 | if (errstrp) |
| 234 | *errstrp = "format not set"; |
| 235 | return (-1); |
| 236 | } |
| 237 | |
| 238 | if (!e->program) { |
| 239 | if (errstrp) |
| 240 | *errstrp = "program not set"; |
| 241 | return (-1); |
| 242 | } |
| 243 | |
| 244 | CHECKPH_PROHIBITED(e->program, PLACEHOLDER_TRACK); |
| 245 | CHECKPH_PROHIBITED(e->program, PLACEHOLDER_STRING); |
| 246 | CHECKPH_DUPLICATE(e->program, PLACEHOLDER_METADATA); |
| 247 | CHECKPH_DUPLICATE(e->program, PLACEHOLDER_ARTIST); |
| 248 | CHECKPH_DUPLICATE(e->program, PLACEHOLDER_TITLE); |
| 249 | |
| 250 | return (0); |
| 251 | } |
| 252 | |
| 253 | const char * |
| 254 | cfg_encoder_get_name(struct cfg_encoder *e) |
| 255 | { |
| 256 | return (e->name); |
| 257 | } |
| 258 | |
| 259 | enum cfg_stream_format |
| 260 | cfg_encoder_get_format(struct cfg_encoder *e) |
| 261 | { |
| 262 | return (e->format); |
| 263 | } |
| 264 | |
| 265 | const char * |
| 266 | cfg_encoder_get_program(struct cfg_encoder *e) |
| 267 | { |
| 268 | return (e->program); |
| 269 | } |