1 /*
2  * This file is part of mpv.
3  *
4  * mpv is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * mpv is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <libavutil/common.h>
19 #include <stdint.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 
24 #include "common/common.h"
25 #include "options/m_option.h"
26 #include "options/path.h"
27 #include "stream.h"
28 
29 struct priv {
30     int64_t slice_start;
31     int64_t slice_max_end; // 0 for no limit
32     struct stream *inner;
33 };
34 
fill_buffer(struct stream * s,void * buffer,int len)35 static int fill_buffer(struct stream *s, void *buffer, int len)
36 {
37     struct priv *p = s->priv;
38 
39     if (p->slice_max_end) {
40         // We don't simply use (s->pos >= size) to avoid early return
41         // if the file is still being appended to.
42         if (s->pos + p->slice_start >= p->slice_max_end)
43             return -1;
44         // Avoid rading beyond p->slice_max_end
45         len = MPMIN(len, p->slice_max_end - s->pos);
46     }
47 
48     return stream_read_partial(p->inner, buffer, len);
49 }
50 
seek(struct stream * s,int64_t newpos)51 static int seek(struct stream *s, int64_t newpos)
52 {
53     struct priv *p = s->priv;
54     return stream_seek(p->inner, newpos + p->slice_start);
55 }
56 
get_size(struct stream * s)57 static int64_t get_size(struct stream *s)
58 {
59     struct priv *p = s->priv;
60     int64_t size = stream_get_size(p->inner);
61     if (size <= 0)
62         return size;
63     if (size <= p->slice_start)
64       return 0;
65     if (p->slice_max_end)
66         size = MPMIN(size, p->slice_max_end);
67     return size - p->slice_start;
68 }
69 
s_close(struct stream * s)70 static void s_close(struct stream *s)
71 {
72     struct priv *p = s->priv;
73     free_stream(p->inner);
74 }
75 
parse_slice_range(stream_t * stream)76 static int parse_slice_range(stream_t *stream)
77 {
78     struct priv *p = stream->priv;
79 
80     struct bstr b_url = bstr0(stream->url);
81     struct bstr proto_with_range, inner_url;
82 
83     bool has_at = bstr_split_tok(b_url, "@", &proto_with_range, &inner_url);
84 
85     if (!has_at) {
86         MP_ERR(stream, "Expected slice://start[-end]@URL: '%s'\n", stream->url);
87         return STREAM_ERROR;
88     }
89 
90     if (!inner_url.len) {
91         MP_ERR(stream, "URL expected to follow 'slice://start[-end]@': '%s'.\n", stream->url);
92         return STREAM_ERROR;
93     }
94     stream->path = bstrto0(stream, inner_url);
95 
96     mp_split_proto(proto_with_range, &proto_with_range);
97     struct bstr range = proto_with_range;
98 
99     struct bstr start, end;
100     bool has_end = bstr_split_tok(range, "-", &start, &end);
101 
102     if (!start.len) {
103         MP_ERR(stream, "The byte range must have a start, and it can't be negative: '%s'\n", stream->url);
104         return STREAM_ERROR;
105     }
106 
107     if (has_end && !end.len) {
108         MP_ERR(stream, "The byte range end can be omitted, but it can't be empty: '%s'\n", stream->url);
109         return STREAM_ERROR;
110     }
111 
112     const struct m_option opt = {
113         .type = &m_option_type_byte_size,
114     };
115 
116     if (m_option_parse(stream->log, &opt, bstr0("slice_start"), start, &p->slice_start) < 0)
117         return STREAM_ERROR;
118 
119     bool max_end_is_offset = bstr_startswith0(end, "+");
120     if (has_end) {
121         if (m_option_parse(stream->log, &opt, bstr0("slice_max_end"), end, &p->slice_max_end) < 0)
122             return STREAM_ERROR;
123     }
124 
125     if (max_end_is_offset)
126       p->slice_max_end += p->slice_start;
127 
128     if (p->slice_max_end && p->slice_max_end < p->slice_start) {
129         MP_ERR(stream, "The byte range end (%"PRId64") can't be smaller than the start (%"PRId64"): '%s'\n",
130                 p->slice_max_end,
131                 p->slice_start,
132                 stream->url);
133         return STREAM_ERROR;
134     }
135 
136     return STREAM_OK;
137 }
138 
open2(struct stream * stream,const struct stream_open_args * args)139 static int open2(struct stream *stream, const struct stream_open_args *args)
140 {
141     struct priv *p = talloc_zero(stream, struct priv);
142     stream->priv = p;
143 
144     stream->fill_buffer = fill_buffer;
145     stream->seek = seek;
146     stream->get_size = get_size;
147     stream->close = s_close;
148 
149     int parse_ret = parse_slice_range(stream);
150     if (parse_ret != STREAM_OK) {
151         return parse_ret;
152     }
153 
154     struct stream_open_args args2 = *args;
155     args2.url = stream->path;
156     int inner_ret = stream_create_with_args(&args2, &p->inner);
157     if (inner_ret != STREAM_OK) {
158         return inner_ret;
159     }
160 
161     if (!p->inner->seekable) {
162         MP_FATAL(stream, "Non-seekable stream '%s' can't be used with 'slice://'\n", p->inner->url);
163         free_stream(p->inner);
164         return STREAM_ERROR;
165     }
166 
167     stream->seekable = 1;
168     stream->stream_origin = p->inner->stream_origin;
169 
170     if (p->slice_start)
171         seek(stream, 0);
172 
173     return STREAM_OK;
174 }
175 
176 const stream_info_t stream_info_slice = {
177     .name = "slice",
178     .open2 = open2,
179     .protocols = (const char*const[]){ "slice", NULL },
180     .can_write = false,
181 };
182