]> git.sesse.net Git - ffmpeg/blob - libavformat/teeproto.c
Merge commit 'bc7f4268514624e1286ea76d27a89a56b4ee18e1'
[ffmpeg] / libavformat / teeproto.c
1 /*
2  * Tee output protocol
3  * Copyright (c) 2016 Michael Niedermayer
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
9  * License 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 GNU
15  * Lesser General Public License for more details.
16  *
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
20  */
21
22 #include "libavutil/avstring.h"
23 #include "libavutil/opt.h"
24 #include "avformat.h"
25 #include "avio_internal.h"
26
27 typedef struct ChildContext {
28     URLContext *url_context;
29 } ChildContext;
30
31 typedef struct TeeContext {
32     const AVClass *class;
33     int child_count;
34     ChildContext *child;
35 } TeeContext;
36
37 static const AVOption tee_options[] = {
38     { NULL }
39 };
40
41 static const AVClass tee_class = {
42     .class_name = "tee",
43     .item_name  = av_default_item_name,
44     .option     = tee_options,
45     .version    = LIBAVUTIL_VERSION_INT,
46 };
47
48 static const char *const child_delim = "|";
49
50 static int tee_write(URLContext *h, const unsigned char *buf, int size)
51 {
52     TeeContext *c = h->priv_data;
53     int i;
54     int main_ret = size;
55
56     for (i=0; i<c->child_count; i++) {
57         int ret = ffurl_write(c->child[i].url_context, buf, size);
58         if (ret < 0)
59             main_ret = ret;
60     }
61     return main_ret;
62 }
63
64 static int tee_close(URLContext *h)
65 {
66     TeeContext *c = h->priv_data;
67     int i;
68     int main_ret = 0;
69
70     for (i=0; i<c->child_count; i++) {
71         int ret = ffurl_closep(&c->child[i].url_context);
72         if (ret < 0)
73             main_ret = ret;
74     }
75
76     av_freep(&c->child);
77     c->child_count = 0;
78     return main_ret;
79 }
80
81 static int tee_open(URLContext *h, const char *filename, int flags)
82 {
83     TeeContext *c = h->priv_data;
84     int ret, i;
85
86     av_strstart(filename, "tee:", &filename);
87
88     if (flags & AVIO_FLAG_READ)
89         return AVERROR(ENOSYS);
90
91     while (*filename) {
92         char *child_name = av_get_token(&filename, child_delim);
93         void *tmp;
94         if (!child_name) {
95             ret = AVERROR(ENOMEM);
96             goto fail;
97         }
98
99         tmp = av_realloc_array(c->child, c->child_count + 1, sizeof(*c->child));
100         if (!tmp) {
101             ret = AVERROR(ENOMEM);
102             goto fail;
103         }
104         c->child = tmp;
105         memset(&c->child[c->child_count], 0, sizeof(c->child[c->child_count]));
106
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,
110                                    h);
111         av_free(child_name);
112         if (ret < 0)
113             goto fail;
114         c->child_count++;
115
116         if (strspn(filename, child_delim))
117             filename++;
118     }
119
120     h->is_streamed = 0;
121     for (i=0; i<c->child_count; i++) {
122         h->is_streamed |= c->child[i].url_context->is_streamed;
123     }
124
125     return 0;
126 fail:
127     tee_close(h);
128     return ret;
129 }
130 const URLProtocol ff_tee_protocol = {
131     .name                = "tee",
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"
137 };