3 * Copyright (c) 2016 Michael Niedermayer
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
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 GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avstring.h"
23 #include "libavutil/opt.h"
25 #include "avio_internal.h"
27 typedef struct ChildContext {
28 URLContext *url_context;
31 typedef struct TeeContext {
37 static const AVOption tee_options[] = {
41 static const AVClass tee_class = {
43 .item_name = av_default_item_name,
44 .option = tee_options,
45 .version = LIBAVUTIL_VERSION_INT,
48 static const char *const child_delim = "|";
50 static int tee_write(URLContext *h, const unsigned char *buf, int size)
52 TeeContext *c = h->priv_data;
56 for (i=0; i<c->child_count; i++) {
57 int ret = ffurl_write(c->child[i].url_context, buf, size);
64 static int tee_close(URLContext *h)
66 TeeContext *c = h->priv_data;
70 for (i=0; i<c->child_count; i++) {
71 int ret = ffurl_closep(&c->child[i].url_context);
81 static int tee_open(URLContext *h, const char *filename, int flags)
83 TeeContext *c = h->priv_data;
86 av_strstart(filename, "tee:", &filename);
88 if (flags & AVIO_FLAG_READ)
89 return AVERROR(ENOSYS);
92 char *child_name = av_get_token(&filename, child_delim);
95 ret = AVERROR(ENOMEM);
99 tmp = av_realloc_array(c->child, c->child_count + 1, sizeof(*c->child));
101 ret = AVERROR(ENOMEM);
105 memset(&c->child[c->child_count], 0, sizeof(c->child[c->child_count]));
107 ret = ffurl_open_whitelist(&c->child[c->child_count].url_context, child_name, flags,
108 &h->interrupt_callback, /*AVDictionary **options*/NULL,
109 h->protocol_whitelist, h->protocol_blacklist,
116 if (strspn(filename, child_delim))
121 for (i=0; i<c->child_count; i++) {
122 h->is_streamed |= c->child[i].url_context->is_streamed;
130 const URLProtocol ff_tee_protocol = {
132 .url_open = tee_open,
133 .url_write = tee_write,
134 .priv_data_size = sizeof(TeeContext),
135 .priv_data_class = &tee_class,
136 .default_whitelist = "crypto,file,http,https,httpproxy,rtmp,tcp,tls"