1 /*
2 * Tee pseudo-muxer
3 * Copyright (c) 2012 Nicolas George
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with FFmpeg; if not, write to the Free Software * Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #include "libavutil/avutil.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/opt.h"
26 #include "internal.h"
27 #include "avformat.h"
28 #include "avio_internal.h"
29 #include "tee_common.h"
30
31 typedef enum {
32 ON_SLAVE_FAILURE_ABORT = 1,
33 ON_SLAVE_FAILURE_IGNORE = 2
34 } SlaveFailurePolicy;
35
36 #define DEFAULT_SLAVE_FAILURE_POLICY ON_SLAVE_FAILURE_ABORT
37
38 typedef struct {
39 AVFormatContext *avf;
40 AVBSFContext **bsfs; ///< bitstream filters per stream
41
42 SlaveFailurePolicy on_fail;
43 int use_fifo;
44 AVDictionary *fifo_options;
45
46 /** map from input to output streams indexes,
47 * disabled output streams are set to -1 */
48 int *stream_map;
49 int header_written;
50 } TeeSlave;
51
52 typedef struct TeeContext {
53 const AVClass *class;
54 unsigned nb_slaves;
55 unsigned nb_alive;
56 TeeSlave *slaves;
57 int use_fifo;
58 AVDictionary *fifo_options;
59 } TeeContext;
60
61 static const char *const slave_delim = "|";
62 static const char *const slave_bsfs_spec_sep = "/";
63 static const char *const slave_select_sep = ",";
64
65 #define OFFSET(x) offsetof(TeeContext, x)
66 static const AVOption options[] = {
67 {"use_fifo", "Use fifo pseudo-muxer to separate actual muxers from encoder",
68 OFFSET(use_fifo), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
69 {"fifo_options", "fifo pseudo-muxer options", OFFSET(fifo_options),
70 AV_OPT_TYPE_DICT, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM},
71 {NULL}
72 };
73
74 static const AVClass tee_muxer_class = {
75 .class_name = "Tee muxer",
76 .item_name = av_default_item_name,
77 .option = options,
78 .version = LIBAVUTIL_VERSION_INT,
79 };
80
parse_slave_failure_policy_option(const char * opt,TeeSlave * tee_slave)81 static inline int parse_slave_failure_policy_option(const char *opt, TeeSlave *tee_slave)
82 {
83 if (!opt) {
84 tee_slave->on_fail = DEFAULT_SLAVE_FAILURE_POLICY;
85 return 0;
86 } else if (!av_strcasecmp("abort", opt)) {
87 tee_slave->on_fail = ON_SLAVE_FAILURE_ABORT;
88 return 0;
89 } else if (!av_strcasecmp("ignore", opt)) {
90 tee_slave->on_fail = ON_SLAVE_FAILURE_IGNORE;
91 return 0;
92 }
93 /* Set failure behaviour to abort, so invalid option error will not be ignored */
94 tee_slave->on_fail = ON_SLAVE_FAILURE_ABORT;
95 return AVERROR(EINVAL);
96 }
97
parse_slave_fifo_options(const char * use_fifo,const char * fifo_options,TeeSlave * tee_slave)98 static int parse_slave_fifo_options(const char *use_fifo,
99 const char *fifo_options, TeeSlave *tee_slave)
100 {
101 int ret = 0;
102
103 if (use_fifo) {
104 /*TODO - change this to use proper function for parsing boolean
105 * options when there is one */
106 if (av_match_name(use_fifo, "true,y,yes,enable,enabled,on,1")) {
107 tee_slave->use_fifo = 1;
108 } else if (av_match_name(use_fifo, "false,n,no,disable,disabled,off,0")) {
109 tee_slave->use_fifo = 0;
110 } else {
111 return AVERROR(EINVAL);
112 }
113 }
114
115 if (fifo_options)
116 ret = av_dict_parse_string(&tee_slave->fifo_options, fifo_options, "=", ":", 0);
117
118 return ret;
119 }
120
close_slave(TeeSlave * tee_slave)121 static int close_slave(TeeSlave *tee_slave)
122 {
123 AVFormatContext *avf;
124 unsigned i;
125 int ret = 0;
126
127 avf = tee_slave->avf;
128 if (!avf)
129 return 0;
130
131 if (tee_slave->header_written)
132 ret = av_write_trailer(avf);
133
134 if (tee_slave->bsfs) {
135 for (i = 0; i < avf->nb_streams; ++i)
136 av_bsf_free(&tee_slave->bsfs[i]);
137 }
138 av_freep(&tee_slave->stream_map);
139 av_freep(&tee_slave->bsfs);
140
141 ff_format_io_close(avf, &avf->pb);
142 avformat_free_context(avf);
143 tee_slave->avf = NULL;
144 return ret;
145 }
146
close_slaves(AVFormatContext * avf)147 static void close_slaves(AVFormatContext *avf)
148 {
149 TeeContext *tee = avf->priv_data;
150 unsigned i;
151
152 for (i = 0; i < tee->nb_slaves; i++) {
153 close_slave(&tee->slaves[i]);
154 }
155 av_freep(&tee->slaves);
156 }
157
open_slave(AVFormatContext * avf,char * slave,TeeSlave * tee_slave)158 static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave)
159 {
160 int i, ret;
161 AVDictionary *options = NULL, *bsf_options = NULL;
162 AVDictionaryEntry *entry;
163 char *filename;
164 char *format = NULL, *select = NULL, *on_fail = NULL;
165 char *use_fifo = NULL, *fifo_options_str = NULL;
166 AVFormatContext *avf2 = NULL;
167 AVStream *st, *st2;
168 int stream_count;
169 int fullret;
170 char *subselect = NULL, *next_subselect = NULL, *first_subselect = NULL, *tmp_select = NULL;
171
172 if ((ret = ff_tee_parse_slave_options(avf, slave, &options, &filename)) < 0)
173 return ret;
174
175 #define STEAL_OPTION(option, field) do { \
176 if ((entry = av_dict_get(options, option, NULL, 0))) { \
177 field = entry->value; \
178 entry->value = NULL; /* prevent it from being freed */ \
179 av_dict_set(&options, option, NULL, 0); \
180 } \
181 } while (0)
182
183 STEAL_OPTION("f", format);
184 STEAL_OPTION("select", select);
185 STEAL_OPTION("onfail", on_fail);
186 STEAL_OPTION("use_fifo", use_fifo);
187 STEAL_OPTION("fifo_options", fifo_options_str);
188 entry = NULL;
189 while ((entry = av_dict_get(options, "bsfs", entry, AV_DICT_IGNORE_SUFFIX))) {
190 /* trim out strlen("bsfs") characters from key */
191 av_dict_set(&bsf_options, entry->key + 4, entry->value, 0);
192 av_dict_set(&options, entry->key, NULL, 0);
193 }
194
195 ret = parse_slave_failure_policy_option(on_fail, tee_slave);
196 if (ret < 0) {
197 av_log(avf, AV_LOG_ERROR,
198 "Invalid onfail option value, valid options are 'abort' and 'ignore'\n");
199 goto end;
200 }
201
202 ret = parse_slave_fifo_options(use_fifo, fifo_options_str, tee_slave);
203 if (ret < 0) {
204 av_log(avf, AV_LOG_ERROR, "Error parsing fifo options: %s\n", av_err2str(ret));
205 goto end;
206 }
207
208 if (tee_slave->use_fifo) {
209
210 if (options) {
211 char *format_options_str = NULL;
212 ret = av_dict_get_string(options, &format_options_str, '=', ':');
213 if (ret < 0)
214 goto end;
215
216 ret = av_dict_set(&tee_slave->fifo_options, "format_opts", format_options_str,
217 AV_DICT_DONT_STRDUP_VAL);
218 if (ret < 0)
219 goto end;
220 }
221
222 if (format) {
223 ret = av_dict_set(&tee_slave->fifo_options, "fifo_format", format,
224 AV_DICT_DONT_STRDUP_VAL);
225 format = NULL;
226 if (ret < 0)
227 goto end;
228 }
229
230 av_dict_free(&options);
231 options = tee_slave->fifo_options;
232 }
233 ret = avformat_alloc_output_context2(&avf2, NULL,
234 tee_slave->use_fifo ? "fifo" :format, filename);
235 if (ret < 0)
236 goto end;
237 tee_slave->avf = avf2;
238 av_dict_copy(&avf2->metadata, avf->metadata, 0);
239 avf2->opaque = avf->opaque;
240 avf2->io_open = avf->io_open;
241 avf2->io_close = avf->io_close;
242 avf2->interrupt_callback = avf->interrupt_callback;
243 avf2->flags = avf->flags;
244 avf2->strict_std_compliance = avf->strict_std_compliance;
245
246 tee_slave->stream_map = av_calloc(avf->nb_streams, sizeof(*tee_slave->stream_map));
247 if (!tee_slave->stream_map) {
248 ret = AVERROR(ENOMEM);
249 goto end;
250 }
251
252 stream_count = 0;
253 for (i = 0; i < avf->nb_streams; i++) {
254 st = avf->streams[i];
255 if (select) {
256 tmp_select = av_strdup(select); // av_strtok is destructive so we regenerate it in each loop
257 if (!tmp_select) {
258 ret = AVERROR(ENOMEM);
259 goto end;
260 }
261 fullret = 0;
262 first_subselect = tmp_select;
263 next_subselect = NULL;
264 while (subselect = av_strtok(first_subselect, slave_select_sep, &next_subselect)) {
265 first_subselect = NULL;
266
267 ret = avformat_match_stream_specifier(avf, avf->streams[i], subselect);
268 if (ret < 0) {
269 av_log(avf, AV_LOG_ERROR,
270 "Invalid stream specifier '%s' for output '%s'\n",
271 subselect, slave);
272 goto end;
273 }
274 if (ret != 0) {
275 fullret = 1; // match
276 break;
277 }
278 }
279 av_freep(&tmp_select);
280
281 if (fullret == 0) { /* no match */
282 tee_slave->stream_map[i] = -1;
283 continue;
284 }
285 }
286 tee_slave->stream_map[i] = stream_count++;
287
288 if (!(st2 = avformat_new_stream(avf2, NULL))) {
289 ret = AVERROR(ENOMEM);
290 goto end;
291 }
292
293 ret = ff_stream_encode_params_copy(st2, st);
294 if (ret < 0)
295 goto end;
296 }
297
298 ret = ff_format_output_open(avf2, filename, &options);
299 if (ret < 0) {
300 av_log(avf, AV_LOG_ERROR, "Slave '%s': error opening: %s\n", slave,
301 av_err2str(ret));
302 goto end;
303 }
304
305 if ((ret = avformat_write_header(avf2, &options)) < 0) {
306 av_log(avf, AV_LOG_ERROR, "Slave '%s': error writing header: %s\n",
307 slave, av_err2str(ret));
308 goto end;
309 }
310 tee_slave->header_written = 1;
311
312 tee_slave->bsfs = av_calloc(avf2->nb_streams, sizeof(*tee_slave->bsfs));
313 if (!tee_slave->bsfs) {
314 ret = AVERROR(ENOMEM);
315 goto end;
316 }
317
318 entry = NULL;
319 while (entry = av_dict_get(bsf_options, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
320 const char *spec = entry->key;
321 if (*spec) {
322 if (strspn(spec, slave_bsfs_spec_sep) != 1) {
323 av_log(avf, AV_LOG_ERROR,
324 "Specifier separator in '%s' is '%c', but only characters '%s' "
325 "are allowed\n", entry->key, *spec, slave_bsfs_spec_sep);
326 ret = AVERROR(EINVAL);
327 goto end;
328 }
329 spec++; /* consume separator */
330 }
331
332 for (i = 0; i < avf2->nb_streams; i++) {
333 ret = avformat_match_stream_specifier(avf2, avf2->streams[i], spec);
334 if (ret < 0) {
335 av_log(avf, AV_LOG_ERROR,
336 "Invalid stream specifier '%s' in bsfs option '%s' for slave "
337 "output '%s'\n", spec, entry->key, filename);
338 goto end;
339 }
340
341 if (ret > 0) {
342 av_log(avf, AV_LOG_DEBUG, "spec:%s bsfs:%s matches stream %d of slave "
343 "output '%s'\n", spec, entry->value, i, filename);
344 if (tee_slave->bsfs[i]) {
345 av_log(avf, AV_LOG_WARNING,
346 "Duplicate bsfs specification associated to stream %d of slave "
347 "output '%s', filters will be ignored\n", i, filename);
348 continue;
349 }
350 ret = av_bsf_list_parse_str(entry->value, &tee_slave->bsfs[i]);
351 if (ret < 0) {
352 av_log(avf, AV_LOG_ERROR,
353 "Error parsing bitstream filter sequence '%s' associated to "
354 "stream %d of slave output '%s'\n", entry->value, i, filename);
355 goto end;
356 }
357 }
358 }
359
360 av_dict_set(&bsf_options, entry->key, NULL, 0);
361 }
362
363 for (i = 0; i < avf->nb_streams; i++){
364 int target_stream = tee_slave->stream_map[i];
365 if (target_stream < 0)
366 continue;
367
368 if (!tee_slave->bsfs[target_stream]) {
369 /* Add pass-through bitstream filter */
370 ret = av_bsf_get_null_filter(&tee_slave->bsfs[target_stream]);
371 if (ret < 0) {
372 av_log(avf, AV_LOG_ERROR,
373 "Failed to create pass-through bitstream filter: %s\n",
374 av_err2str(ret));
375 goto end;
376 }
377 }
378
379 tee_slave->bsfs[target_stream]->time_base_in = avf->streams[i]->time_base;
380 ret = avcodec_parameters_copy(tee_slave->bsfs[target_stream]->par_in,
381 avf->streams[i]->codecpar);
382 if (ret < 0)
383 goto end;
384
385 ret = av_bsf_init(tee_slave->bsfs[target_stream]);
386 if (ret < 0) {
387 av_log(avf, AV_LOG_ERROR,
388 "Failed to initialize bitstream filter(s): %s\n",
389 av_err2str(ret));
390 goto end;
391 }
392 }
393
394 if (options) {
395 entry = NULL;
396 while ((entry = av_dict_get(options, "", entry, AV_DICT_IGNORE_SUFFIX)))
397 av_log(avf2, AV_LOG_ERROR, "Unknown option '%s'\n", entry->key);
398 ret = AVERROR_OPTION_NOT_FOUND;
399 goto end;
400 }
401
402 end:
403 av_free(format);
404 av_free(select);
405 av_free(on_fail);
406 av_dict_free(&options);
407 av_dict_free(&bsf_options);
408 av_freep(&tmp_select);
409 return ret;
410 }
411
log_slave(TeeSlave * slave,void * log_ctx,int log_level)412 static void log_slave(TeeSlave *slave, void *log_ctx, int log_level)
413 {
414 int i;
415 av_log(log_ctx, log_level, "filename:'%s' format:%s\n",
416 slave->avf->url, slave->avf->oformat->name);
417 for (i = 0; i < slave->avf->nb_streams; i++) {
418 AVStream *st = slave->avf->streams[i];
419 AVBSFContext *bsf = slave->bsfs[i];
420 const char *bsf_name;
421
422 av_log(log_ctx, log_level, " stream:%d codec:%s type:%s",
423 i, avcodec_get_name(st->codecpar->codec_id),
424 av_get_media_type_string(st->codecpar->codec_type));
425
426 bsf_name = bsf->filter->priv_class ?
427 bsf->filter->priv_class->item_name(bsf) : bsf->filter->name;
428 av_log(log_ctx, log_level, " bsfs: %s\n", bsf_name);
429 }
430 }
431
tee_process_slave_failure(AVFormatContext * avf,unsigned slave_idx,int err_n)432 static int tee_process_slave_failure(AVFormatContext *avf, unsigned slave_idx, int err_n)
433 {
434 TeeContext *tee = avf->priv_data;
435 TeeSlave *tee_slave = &tee->slaves[slave_idx];
436
437 tee->nb_alive--;
438
439 close_slave(tee_slave);
440
441 if (!tee->nb_alive) {
442 av_log(avf, AV_LOG_ERROR, "All tee outputs failed.\n");
443 return err_n;
444 } else if (tee_slave->on_fail == ON_SLAVE_FAILURE_ABORT) {
445 av_log(avf, AV_LOG_ERROR, "Slave muxer #%u failed, aborting.\n", slave_idx);
446 return err_n;
447 } else {
448 av_log(avf, AV_LOG_ERROR, "Slave muxer #%u failed: %s, continuing with %u/%u slaves.\n",
449 slave_idx, av_err2str(err_n), tee->nb_alive, tee->nb_slaves);
450 return 0;
451 }
452 }
453
tee_write_header(AVFormatContext * avf)454 static int tee_write_header(AVFormatContext *avf)
455 {
456 TeeContext *tee = avf->priv_data;
457 unsigned nb_slaves = 0, i;
458 const char *filename = avf->url;
459 char **slaves = NULL;
460 int ret;
461
462 while (*filename) {
463 char *slave = av_get_token(&filename, slave_delim);
464 if (!slave) {
465 ret = AVERROR(ENOMEM);
466 goto fail;
467 }
468 ret = av_dynarray_add_nofree(&slaves, &nb_slaves, slave);
469 if (ret < 0) {
470 av_free(slave);
471 goto fail;
472 }
473 if (strspn(filename, slave_delim))
474 filename++;
475 }
476
477 if (!(tee->slaves = av_mallocz_array(nb_slaves, sizeof(*tee->slaves)))) {
478 ret = AVERROR(ENOMEM);
479 goto fail;
480 }
481 tee->nb_slaves = tee->nb_alive = nb_slaves;
482
483 for (i = 0; i < nb_slaves; i++) {
484
485 tee->slaves[i].use_fifo = tee->use_fifo;
486 ret = av_dict_copy(&tee->slaves[i].fifo_options, tee->fifo_options, 0);
487 if (ret < 0)
488 goto fail;
489
490 if ((ret = open_slave(avf, slaves[i], &tee->slaves[i])) < 0) {
491 ret = tee_process_slave_failure(avf, i, ret);
492 if (ret < 0)
493 goto fail;
494 } else {
495 log_slave(&tee->slaves[i], avf, AV_LOG_VERBOSE);
496 }
497 av_freep(&slaves[i]);
498 }
499
500 for (i = 0; i < avf->nb_streams; i++) {
501 int j, mapped = 0;
502 for (j = 0; j < tee->nb_slaves; j++)
503 if (tee->slaves[j].avf)
504 mapped += tee->slaves[j].stream_map[i] >= 0;
505 if (!mapped)
506 av_log(avf, AV_LOG_WARNING, "Input stream #%d is not mapped "
507 "to any slave.\n", i);
508 }
509 av_free(slaves);
510 return 0;
511
512 fail:
513 for (i = 0; i < nb_slaves; i++)
514 av_freep(&slaves[i]);
515 close_slaves(avf);
516 av_free(slaves);
517 return ret;
518 }
519
tee_write_trailer(AVFormatContext * avf)520 static int tee_write_trailer(AVFormatContext *avf)
521 {
522 TeeContext *tee = avf->priv_data;
523 int ret_all = 0, ret;
524 unsigned i;
525
526 for (i = 0; i < tee->nb_slaves; i++) {
527 if ((ret = close_slave(&tee->slaves[i])) < 0) {
528 ret = tee_process_slave_failure(avf, i, ret);
529 if (!ret_all && ret < 0)
530 ret_all = ret;
531 }
532 }
533 av_freep(&tee->slaves);
534 return ret_all;
535 }
536
tee_write_packet(AVFormatContext * avf,AVPacket * pkt)537 static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt)
538 {
539 TeeContext *tee = avf->priv_data;
540 AVFormatContext *avf2;
541 AVBSFContext *bsfs;
542 AVPacket pkt2;
543 int ret_all = 0, ret;
544 unsigned i, s;
545 int s2;
546
547 for (i = 0; i < tee->nb_slaves; i++) {
548 if (!(avf2 = tee->slaves[i].avf))
549 continue;
550
551 /* Flush slave if pkt is NULL*/
552 if (!pkt) {
553 ret = av_interleaved_write_frame(avf2, NULL);
554 if (ret < 0) {
555 ret = tee_process_slave_failure(avf, i, ret);
556 if (!ret_all && ret < 0)
557 ret_all = ret;
558 }
559 continue;
560 }
561
562 s = pkt->stream_index;
563 s2 = tee->slaves[i].stream_map[s];
564 if (s2 < 0)
565 continue;
566
567 if ((ret = av_packet_ref(&pkt2, pkt)) < 0)
568 if (!ret_all) {
569 ret_all = ret;
570 continue;
571 }
572 bsfs = tee->slaves[i].bsfs[s2];
573 pkt2.stream_index = s2;
574
575 ret = av_bsf_send_packet(bsfs, &pkt2);
576 if (ret < 0) {
577 av_log(avf, AV_LOG_ERROR, "Error while sending packet to bitstream filter: %s\n",
578 av_err2str(ret));
579 ret = tee_process_slave_failure(avf, i, ret);
580 if (!ret_all && ret < 0)
581 ret_all = ret;
582 }
583
584 while(1) {
585 ret = av_bsf_receive_packet(bsfs, &pkt2);
586 if (ret == AVERROR(EAGAIN)) {
587 ret = 0;
588 break;
589 } else if (ret < 0) {
590 break;
591 }
592
593 av_packet_rescale_ts(&pkt2, bsfs->time_base_out,
594 avf2->streams[s2]->time_base);
595 ret = av_interleaved_write_frame(avf2, &pkt2);
596 if (ret < 0)
597 break;
598 };
599
600 if (ret < 0) {
601 ret = tee_process_slave_failure(avf, i, ret);
602 if (!ret_all && ret < 0)
603 ret_all = ret;
604 }
605 }
606 return ret_all;
607 }
608
609 AVOutputFormat ff_tee_muxer = {
610 .name = "tee",
611 .long_name = NULL_IF_CONFIG_SMALL("Multiple muxer tee"),
612 .priv_data_size = sizeof(TeeContext),
613 .write_header = tee_write_header,
614 .write_trailer = tee_write_trailer,
615 .write_packet = tee_write_packet,
616 .priv_class = &tee_muxer_class,
617 .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
618 };
619