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 #ifndef MPLAYER_STREAM_H
19 #define MPLAYER_STREAM_H
20
21 #include "common/msg.h"
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <inttypes.h>
26 #include <sys/types.h>
27 #include <fcntl.h>
28
29 #include "misc/bstr.h"
30
31 // Minimum guaranteed buffer and seek-back size. For any reads <= of this size,
32 // it's guaranteed that you can seek back by <= of this size again.
33 #define STREAM_BUFFER_SIZE 2048
34
35 // flags for stream_open_ext (this includes STREAM_READ and STREAM_WRITE)
36
37 // stream->mode
38 #define STREAM_READ 0
39 #define STREAM_WRITE (1 << 0)
40
41 #define STREAM_SILENT (1 << 1)
42
43 // Origin value for "security". This is an integer within the flags bit-field.
44 #define STREAM_ORIGIN_DIRECT (1 << 2) // passed from cmdline or loadfile
45 #define STREAM_ORIGIN_FS (2 << 2) // referenced from playlist on unix FS
46 #define STREAM_ORIGIN_NET (3 << 2) // referenced from playlist on network
47 #define STREAM_ORIGIN_UNSAFE (4 << 2) // from a grotesque source
48
49 #define STREAM_ORIGIN_MASK (7 << 2) // for extracting origin value from flags
50
51 #define STREAM_LOCAL_FS_ONLY (1 << 5) // stream_file only, no URLs
52 #define STREAM_LESS_NOISE (1 << 6) // try to log errors only
53
54 // end flags for stream_open_ext (the naming convention sucks)
55
56 #define STREAM_UNSAFE -3
57 #define STREAM_NO_MATCH -2
58 #define STREAM_UNSUPPORTED -1
59 #define STREAM_ERROR 0
60 #define STREAM_OK 1
61
62 enum stream_ctrl {
63 // Certain network protocols
64 STREAM_CTRL_AVSEEK,
65 STREAM_CTRL_HAS_AVSEEK,
66 STREAM_CTRL_GET_METADATA,
67
68 // Optical discs (internal interface between streams and demux_disc)
69 STREAM_CTRL_GET_TIME_LENGTH,
70 STREAM_CTRL_GET_DVD_INFO,
71 STREAM_CTRL_GET_DISC_NAME,
72 STREAM_CTRL_GET_NUM_CHAPTERS,
73 STREAM_CTRL_GET_CURRENT_TIME,
74 STREAM_CTRL_GET_CHAPTER_TIME,
75 STREAM_CTRL_SEEK_TO_TIME,
76 STREAM_CTRL_GET_ASPECT_RATIO,
77 STREAM_CTRL_GET_NUM_ANGLES,
78 STREAM_CTRL_GET_ANGLE,
79 STREAM_CTRL_SET_ANGLE,
80 STREAM_CTRL_GET_NUM_TITLES,
81 STREAM_CTRL_GET_TITLE_LENGTH, // double* (in: title number, out: len)
82 STREAM_CTRL_GET_LANG,
83 STREAM_CTRL_GET_CURRENT_TITLE,
84 STREAM_CTRL_SET_CURRENT_TITLE,
85 };
86
87 struct stream_lang_req {
88 int type; // STREAM_AUDIO, STREAM_SUB
89 int id;
90 char name[50];
91 };
92
93 struct stream_dvd_info_req {
94 unsigned int palette[16];
95 int num_subs;
96 };
97
98 // for STREAM_CTRL_AVSEEK
99 struct stream_avseek {
100 int stream_index;
101 int64_t timestamp;
102 int flags;
103 };
104
105 struct stream;
106 struct stream_open_args;
107 typedef struct stream_info_st {
108 const char *name;
109 // opts is set from ->opts
110 int (*open)(struct stream *st);
111 // Alternative to open(). Only either open() or open2() can be set.
112 int (*open2)(struct stream *st, const struct stream_open_args *args);
113 const char *const *protocols;
114 bool can_write; // correctly checks for READ/WRITE modes
115 bool local_fs; // supports STREAM_LOCAL_FS_ONLY
116 int stream_origin; // 0 or set of STREAM_ORIGIN_*; if 0, the same origin
117 // is set, or the stream's open() function handles it
118 } stream_info_t;
119
120 typedef struct stream {
121 const struct stream_info_st *info;
122
123 // Read
124 int (*fill_buffer)(struct stream *s, void *buffer, int max_len);
125 // Write
126 int (*write_buffer)(struct stream *s, void *buffer, int len);
127 // Seek
128 int (*seek)(struct stream *s, int64_t pos);
129 // Total stream size in bytes (negative if unavailable)
130 int64_t (*get_size)(struct stream *s);
131 // Control
132 int (*control)(struct stream *s, int cmd, void *arg);
133 // Close
134 void (*close)(struct stream *s);
135
136 int64_t pos;
137 int eof; // valid only after read calls that returned a short result
138 int mode; //STREAM_READ or STREAM_WRITE
139 int stream_origin; // any STREAM_ORIGIN_*
140 void *priv; // used for DVD, TV, RTSP etc
141 char *url; // filename/url (possibly including protocol prefix)
142 char *path; // filename (url without protocol prefix)
143 char *mime_type; // when HTTP streaming is used
144 char *demuxer; // request demuxer to be used
145 char *lavf_type; // name of expected demuxer type for lavf
146 bool streaming : 1; // known to be a network stream if true
147 bool seekable : 1; // presence of general byte seeking support
148 bool fast_skip : 1; // consider stream fast enough to fw-seek by skipping
149 bool is_network : 1; // I really don't know what this is for
150 bool is_local_file : 1; // from the filesystem
151 bool is_directory : 1; // directory on the filesystem
152 bool access_references : 1; // open other streams
153 struct mp_log *log;
154 struct mpv_global *global;
155
156 struct mp_cancel *cancel; // cancellation notification
157
158 // Read statistic for fill_buffer calls. All bytes read by fill_buffer() are
159 // added to this. The user can reset this as needed.
160 uint64_t total_unbuffered_read_bytes;
161 // Seek statistics. The user can reset this as needed.
162 uint64_t total_stream_seeks;
163
164 // Buffer size requested by user; s->buffer may have a different size
165 int requested_buffer_size;
166
167 // This is a ring buffer. It is reset only on seeks (or when buffers are
168 // dropped). Otherwise old contents always stay valid.
169 // The valid buffer is from buf_start to buf_end; buf_end can be larger
170 // than the buffer size (requires wrap around). buf_cur is a value in the
171 // range [buf_start, buf_end].
172 // When reading more data from the stream, buf_start is advanced as old
173 // data is overwritten with new data.
174 // Example:
175 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
176 // +===========================+---------------------------+
177 // + 05 06 07 08 | 01 02 03 04 + 05 06 07 08 | 01 02 03 04 +
178 // +===========================+---------------------------+
179 // ^ buf_start (4) | |
180 // | ^ buf_end (12 % 8 => 4)
181 // ^ buf_cur (9 % 8 => 1)
182 // Here, the entire 8 byte buffer is filled, i.e. buf_end - buf_start = 8.
183 // buffer_mask == 7, so (x & buffer_mask) == (x % buffer_size)
184 unsigned int buf_start; // index of oldest byte in buffer (is <= buffer_mask)
185 unsigned int buf_cur; // current read pos (can be > buffer_mask)
186 unsigned int buf_end; // end position (can be > buffer_mask)
187
188 unsigned int buffer_mask; // buffer_size-1, where buffer_size == 2**n
189 uint8_t *buffer;
190 } stream_t;
191
192 // Non-inline version of stream_read_char().
193 int stream_read_char_fallback(stream_t *s);
194
195 int stream_write_buffer(stream_t *s, void *buf, int len);
196
stream_read_char(stream_t * s)197 inline static int stream_read_char(stream_t *s)
198 {
199 return s->buf_cur < s->buf_end
200 ? s->buffer[(s->buf_cur++) & s->buffer_mask]
201 : stream_read_char_fallback(s);
202 }
203
204 int stream_skip_bom(struct stream *s);
205
stream_tell(stream_t * s)206 inline static int64_t stream_tell(stream_t *s)
207 {
208 return s->pos + s->buf_cur - s->buf_end;
209 }
210
211 bool stream_seek_skip(stream_t *s, int64_t pos);
212 bool stream_seek(stream_t *s, int64_t pos);
213 int stream_read(stream_t *s, void *mem, int total);
214 int stream_read_partial(stream_t *s, void *buf, int buf_size);
215 int stream_peek(stream_t *s, int forward_size);
216 int stream_read_peek(stream_t *s, void *buf, int buf_size);
217 void stream_drop_buffers(stream_t *s);
218 int64_t stream_get_size(stream_t *s);
219
220 struct mpv_global;
221
222 struct bstr stream_read_complete(struct stream *s, void *talloc_ctx,
223 int max_size);
224 struct bstr stream_read_file(const char *filename, void *talloc_ctx,
225 struct mpv_global *global, int max_size);
226
227 int stream_control(stream_t *s, int cmd, void *arg);
228 void free_stream(stream_t *s);
229
230 struct stream_open_args {
231 struct mpv_global *global;
232 struct mp_cancel *cancel; // aborting stream access (used directly)
233 const char *url;
234 int flags; // STREAM_READ etc.
235 const stream_info_t *sinfo; // NULL = autoprobe, otherwise force stream impl.
236 void *special_arg; // specific to impl., use only with sinfo
237 };
238
239 int stream_create_with_args(struct stream_open_args *args, struct stream **ret);
240 struct stream *stream_create(const char *url, int flags,
241 struct mp_cancel *c, struct mpv_global *global);
242 stream_t *open_output_stream(const char *filename, struct mpv_global *global);
243
244 void mp_url_unescape_inplace(char *buf);
245 char *mp_url_escape(void *talloc_ctx, const char *s, const char *ok);
246
247 // stream_memory.c
248 struct stream *stream_memory_open(struct mpv_global *global, void *data, int len);
249
250 // stream_concat.c
251 struct stream *stream_concat_open(struct mpv_global *global, struct mp_cancel *c,
252 struct stream **streams, int num_streams);
253
254 // stream_file.c
255 char *mp_file_url_to_filename(void *talloc_ctx, bstr url);
256 char *mp_file_get_path(void *talloc_ctx, bstr url);
257
258 // stream_lavf.c
259 struct AVDictionary;
260 void mp_setup_av_network_options(struct AVDictionary **dict,
261 const char *target_fmt,
262 struct mpv_global *global,
263 struct mp_log *log);
264
265 void stream_print_proto_list(struct mp_log *log);
266 char **stream_get_proto_list(void);
267 bool stream_has_proto(const char *proto);
268
269 #endif /* MPLAYER_STREAM_H */
270