/src/mpv/stream/stream_slice.c
Line | Count | Source |
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 | | |
23 | | #include "common/common.h" |
24 | | #include "options/m_option.h" |
25 | | #include "options/path.h" |
26 | | #include "stream.h" |
27 | | |
28 | | struct priv { |
29 | | int64_t slice_start; |
30 | | int64_t slice_max_end; // 0 for no limit |
31 | | struct stream *inner; |
32 | | }; |
33 | | |
34 | | static int fill_buffer(struct stream *s, void *buffer, int len) |
35 | 0 | { |
36 | 0 | struct priv *p = s->priv; |
37 | |
|
38 | 0 | if (p->slice_max_end) { |
39 | | // We don't simply use (s->pos >= size) to avoid early return |
40 | | // if the file is still being appended to. |
41 | 0 | if (s->pos + p->slice_start >= p->slice_max_end) |
42 | 0 | return -1; |
43 | | // Avoid rading beyond p->slice_max_end |
44 | 0 | len = MPMIN(len, p->slice_max_end - p->slice_start - s->pos); |
45 | 0 | } |
46 | | |
47 | 0 | return stream_read_partial(p->inner, buffer, len); |
48 | 0 | } |
49 | | |
50 | | static int seek(struct stream *s, int64_t newpos) |
51 | 0 | { |
52 | 0 | struct priv *p = s->priv; |
53 | 0 | if (newpos > INT64_MAX - p->slice_start) |
54 | 0 | return 0; |
55 | 0 | return stream_seek(p->inner, newpos + p->slice_start); |
56 | 0 | } |
57 | | |
58 | | static int64_t get_size(struct stream *s) |
59 | 0 | { |
60 | 0 | struct priv *p = s->priv; |
61 | 0 | int64_t size = stream_get_size(p->inner); |
62 | 0 | if (size <= 0) |
63 | 0 | return size; |
64 | 0 | if (size <= p->slice_start) |
65 | 0 | return 0; |
66 | 0 | if (p->slice_max_end) |
67 | 0 | size = MPMIN(size, p->slice_max_end); |
68 | 0 | return size - p->slice_start; |
69 | 0 | } |
70 | | |
71 | | static void s_close(struct stream *s) |
72 | 0 | { |
73 | 0 | struct priv *p = s->priv; |
74 | 0 | free_stream(p->inner); |
75 | 0 | } |
76 | | |
77 | | static int parse_slice_range(stream_t *stream) |
78 | 87 | { |
79 | 87 | struct priv *p = stream->priv; |
80 | | |
81 | 87 | struct bstr b_url = bstr0(stream->url); |
82 | 87 | struct bstr proto_with_range, inner_url; |
83 | | |
84 | 87 | bool has_at = bstr_split_tok(b_url, "@", &proto_with_range, &inner_url); |
85 | | |
86 | 87 | if (!has_at) { |
87 | 26 | MP_ERR(stream, "Expected slice://start[-end]@URL: '%s'\n", stream->url); |
88 | 26 | return STREAM_ERROR; |
89 | 26 | } |
90 | | |
91 | 61 | if (!inner_url.len) { |
92 | 1 | MP_ERR(stream, "URL expected to follow 'slice://start[-end]@': '%s'.\n", stream->url); |
93 | 1 | return STREAM_ERROR; |
94 | 1 | } |
95 | 60 | stream->path = bstrto0(stream, inner_url); |
96 | | |
97 | 60 | mp_split_proto(proto_with_range, &proto_with_range); |
98 | 60 | struct bstr range = proto_with_range; |
99 | | |
100 | 60 | struct bstr start, end; |
101 | 60 | bool has_end = bstr_split_tok(range, "-", &start, &end); |
102 | | |
103 | 60 | if (!start.len) { |
104 | 17 | MP_ERR(stream, "The byte range must have a start, and it can't be negative: '%s'\n", stream->url); |
105 | 17 | return STREAM_ERROR; |
106 | 17 | } |
107 | | |
108 | 43 | if (has_end && !end.len) { |
109 | 12 | MP_ERR(stream, "The byte range end can be omitted, but it can't be empty: '%s'\n", stream->url); |
110 | 12 | return STREAM_ERROR; |
111 | 12 | } |
112 | | |
113 | 31 | const struct m_option opt = { |
114 | 31 | .type = &m_option_type_byte_size, |
115 | 31 | }; |
116 | | |
117 | 31 | if (m_option_parse(stream->log, &opt, bstr0("slice_start"), start, &p->slice_start) < 0) |
118 | 31 | return STREAM_ERROR; |
119 | | |
120 | 31 | bool max_end_is_offset = bstr_startswith0(end, "+"); |
121 | 0 | if (has_end) { |
122 | 0 | if (m_option_parse(stream->log, &opt, bstr0("slice_max_end"), end, &p->slice_max_end) < 0) |
123 | 0 | return STREAM_ERROR; |
124 | 0 | } |
125 | | |
126 | 0 | if (max_end_is_offset) { |
127 | 0 | if (p->slice_max_end > INT64_MAX - p->slice_start) { |
128 | 0 | MP_ERR(stream, "The byte range end overflows: '%s'\n", stream->url); |
129 | 0 | return STREAM_ERROR; |
130 | 0 | } |
131 | 0 | p->slice_max_end += p->slice_start; |
132 | 0 | } |
133 | | |
134 | 0 | if (p->slice_max_end && p->slice_max_end < p->slice_start) { |
135 | 0 | MP_ERR(stream, "The byte range end (%"PRId64") can't be smaller than the start (%"PRId64"): '%s'\n", |
136 | 0 | p->slice_max_end, |
137 | 0 | p->slice_start, |
138 | 0 | stream->url); |
139 | 0 | return STREAM_ERROR; |
140 | 0 | } |
141 | | |
142 | 0 | return STREAM_OK; |
143 | 0 | } |
144 | | |
145 | | static int open2(struct stream *stream, const struct stream_open_args *args) |
146 | 87 | { |
147 | 87 | struct priv *p = talloc_zero(stream, struct priv); |
148 | 87 | stream->priv = p; |
149 | | |
150 | 87 | stream->fill_buffer = fill_buffer; |
151 | 87 | stream->seek = seek; |
152 | 87 | stream->get_size = get_size; |
153 | 87 | stream->close = s_close; |
154 | | |
155 | 87 | int parse_ret = parse_slice_range(stream); |
156 | 87 | if (parse_ret != STREAM_OK) { |
157 | 87 | return parse_ret; |
158 | 87 | } |
159 | | |
160 | 0 | struct stream_open_args args2 = *args; |
161 | 0 | args2.url = stream->path; |
162 | 0 | int inner_ret = stream_create_with_args(&args2, &p->inner); |
163 | 0 | if (inner_ret != STREAM_OK) { |
164 | 0 | return inner_ret; |
165 | 0 | } |
166 | | |
167 | 0 | if (p->inner->is_directory) { |
168 | 0 | MP_FATAL(stream, "Inner stream '%s' is a directory\n", p->inner->url); |
169 | 0 | free_stream(p->inner); |
170 | 0 | return STREAM_ERROR; |
171 | 0 | } else if (!p->inner->seekable) { |
172 | 0 | MP_FATAL(stream, "Non-seekable stream '%s' can't be used with 'slice://'\n", p->inner->url); |
173 | 0 | free_stream(p->inner); |
174 | 0 | return STREAM_ERROR; |
175 | 0 | } |
176 | | |
177 | 0 | stream->seekable = 1; |
178 | 0 | stream->stream_origin = p->inner->stream_origin; |
179 | |
|
180 | 0 | if (p->slice_start) |
181 | 0 | seek(stream, 0); |
182 | |
|
183 | 0 | return STREAM_OK; |
184 | 0 | } |
185 | | |
186 | | const stream_info_t stream_info_slice = { |
187 | | .name = "slice", |
188 | | .open2 = open2, |
189 | | .protocols = (const char*const[]){ "slice", NULL }, |
190 | | .can_write = false, |
191 | | }; |