gl_common: minor cleanup/refactor
[mplayer.git] / stream / stream.h
blob7d0ed771c0de1d9b4eeee5cca68638c7a6832bb4
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer 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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #ifndef MPLAYER_STREAM_H
20 #define MPLAYER_STREAM_H
22 #include "config.h"
23 #include "mp_msg.h"
24 #include "url.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <inttypes.h>
28 #include <sys/types.h>
29 #include <fcntl.h>
31 #include "bstr.h"
33 #ifndef O_BINARY
34 #define O_BINARY 0
35 #endif
37 #define STREAMTYPE_DUMMY -1 // for placeholders, when the actual reading is handled in the demuxer
38 #define STREAMTYPE_FILE 0 // read from seekable file
39 #define STREAMTYPE_VCD 1 // raw mode-2 CDROM reading, 2324 bytes/sector
40 #define STREAMTYPE_STREAM 2 // same as FILE but no seeking (for net/stdin)
41 #define STREAMTYPE_DVD 3 // libdvdread
42 #define STREAMTYPE_MEMORY 4 // read data from memory area
43 #define STREAMTYPE_PLAYLIST 6 // FIXME!!! same as STREAMTYPE_FILE now
44 #define STREAMTYPE_DS 8 // read from a demuxer stream
45 #define STREAMTYPE_DVDNAV 9 // we cannot safely "seek" in this...
46 #define STREAMTYPE_CDDA 10 // raw audio CD reader
47 #define STREAMTYPE_SMB 11 // smb:// url, using libsmbclient (samba)
48 #define STREAMTYPE_VCDBINCUE 12 // vcd directly from bin/cue files
49 #define STREAMTYPE_DVB 13
50 #define STREAMTYPE_VSTREAM 14
51 #define STREAMTYPE_SDP 15
52 #define STREAMTYPE_PVR 16
53 #define STREAMTYPE_TV 17
54 #define STREAMTYPE_MF 18
55 #define STREAMTYPE_RADIO 19
56 #define STREAMTYPE_BLURAY 20
58 #define STREAM_BUFFER_SIZE 2048
59 #define STREAM_MAX_SECTOR_SIZE (8*1024)
61 #define VCD_SECTOR_SIZE 2352
62 #define VCD_SECTOR_OFFS 24
63 #define VCD_SECTOR_DATA 2324
65 /// atm it will always use mode == STREAM_READ
66 /// streams that use the new api should check the mode at open
67 #define STREAM_READ 0
68 #define STREAM_WRITE 1
69 /// Seek flags, if not mannualy set and s->seek isn't NULL
70 /// MP_STREAM_SEEK is automaticly set
71 #define MP_STREAM_SEEK_BW 2
72 #define MP_STREAM_SEEK_FW 4
73 #define MP_STREAM_SEEK (MP_STREAM_SEEK_BW|MP_STREAM_SEEK_FW)
75 //////////// Open return code
76 #define STREAM_REDIRECTED -2
77 /// This can't open the requested protocol (used by stream wich have a
78 /// * protocol when they don't know the requested protocol)
79 #define STREAM_UNSUPPORTED -1
80 #define STREAM_ERROR 0
81 #define STREAM_OK 1
83 #define MAX_STREAM_PROTOCOLS 10
85 #define STREAM_CTRL_RESET 0
86 #define STREAM_CTRL_GET_TIME_LENGTH 1
87 #define STREAM_CTRL_SEEK_TO_CHAPTER 2
88 #define STREAM_CTRL_GET_CURRENT_CHAPTER 3
89 #define STREAM_CTRL_GET_NUM_CHAPTERS 4
90 #define STREAM_CTRL_GET_CURRENT_TIME 5
91 #define STREAM_CTRL_SEEK_TO_TIME 6
92 #define STREAM_CTRL_GET_SIZE 7
93 #define STREAM_CTRL_GET_ASPECT_RATIO 8
94 #define STREAM_CTRL_GET_NUM_ANGLES 9
95 #define STREAM_CTRL_GET_ANGLE 10
96 #define STREAM_CTRL_SET_ANGLE 11
99 typedef enum {
100 streaming_stopped_e,
101 streaming_playing_e
102 } streaming_status;
104 typedef struct streaming_control {
105 URL_t *url;
106 streaming_status status;
107 int buffering; // boolean
108 unsigned int prebuffer_size;
109 char *buffer;
110 unsigned int buffer_size;
111 unsigned int buffer_pos;
112 unsigned int bandwidth; // The downstream available
113 int (*streaming_read)( int fd, char *buffer, int buffer_size, struct streaming_control *stream_ctrl );
114 int (*streaming_seek)( int fd, off_t pos, struct streaming_control *stream_ctrl );
115 void *data;
116 // hacks for asf
117 int *audio_id_ptr;
118 int *video_id_ptr;
119 } streaming_ctrl_t;
121 struct stream;
122 typedef struct stream_info_st {
123 const char *info;
124 const char *name;
125 const char *author;
126 const char *comment;
127 /// mode isn't used atm (ie always READ) but it shouldn't be ignored
128 /// opts is at least in it's defaults settings and may have been
129 /// altered by url parsing if enabled and the options string parsing.
130 int (*open)(struct stream* st, int mode, void* opts, int* file_format);
131 const char* protocols[MAX_STREAM_PROTOCOLS];
132 const void* opts;
133 int opts_url; /* If this is 1 we will parse the url as an option string
134 * too. Otherwise options are only parsed from the
135 * options string given to open_stream_plugin */
136 } stream_info_t;
138 typedef struct stream {
139 // Read
140 int (*fill_buffer)(struct stream *s, char* buffer, int max_len);
141 // Write
142 int (*write_buffer)(struct stream *s, char* buffer, int len);
143 // Seek
144 int (*seek)(struct stream *s,off_t pos);
145 // Control
146 // Will be later used to let streams like dvd and cdda report
147 // their structure (ie tracks, chapters, etc)
148 int (*control)(struct stream *s,int cmd,void* arg);
149 // Close
150 void (*close)(struct stream *s);
152 int fd; // file descriptor, see man open(2)
153 int type; // see STREAMTYPE_*
154 int flags;
155 int sector_size; // sector size (seek will be aligned on this size if non 0)
156 int read_chunk; // maximum amount of data to read at once to limit latency (0 for default)
157 unsigned int buf_pos,buf_len;
158 off_t pos,start_pos,end_pos;
159 int eof;
160 int mode; //STREAM_READ or STREAM_WRITE
161 unsigned int cache_pid;
162 void* cache_data;
163 void* priv; // used for DVD, TV, RTSP etc
164 char* url; // strdup() of filename/url
165 char *lavf_type; // name of expected demuxer type for lavf
166 struct MPOpts *opts;
167 #ifdef CONFIG_NETWORKING
168 streaming_ctrl_t *streaming_ctrl;
169 #endif
170 unsigned char buffer[STREAM_BUFFER_SIZE>STREAM_MAX_SECTOR_SIZE?STREAM_BUFFER_SIZE:STREAM_MAX_SECTOR_SIZE];
171 FILE *capture_file;
172 } stream_t;
174 #ifdef CONFIG_NETWORKING
175 #include "network.h"
176 #endif
178 int stream_fill_buffer(stream_t *s);
179 int stream_seek_long(stream_t *s, off_t pos);
180 void stream_capture_do(stream_t *s);
182 #ifdef CONFIG_STREAM_CACHE
183 int stream_enable_cache_percent(stream_t *stream, int stream_cache_size,
184 float stream_cache_min_percent, float stream_cache_seek_min_percent);
185 int stream_enable_cache(stream_t *stream,int size,int min,int prefill);
186 int cache_stream_fill_buffer(stream_t *s);
187 int cache_stream_seek_long(stream_t *s,off_t pos);
188 #else
189 // no cache, define wrappers:
190 #define cache_stream_fill_buffer(x) stream_fill_buffer(x)
191 #define cache_stream_seek_long(x,y) stream_seek_long(x,y)
192 #define stream_enable_cache(x,y,z,w) 1
193 #define stream_enable_cache_percent(x,y,z,w) 1
194 #endif
195 int stream_write_buffer(stream_t *s, unsigned char *buf, int len);
197 inline static int stream_read_char(stream_t *s){
198 return (s->buf_pos<s->buf_len)?s->buffer[s->buf_pos++]:
199 (cache_stream_fill_buffer(s)?s->buffer[s->buf_pos++]:-256);
200 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
201 // stream_fill_buffer(s);
202 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
203 // return 0; // EOF
206 inline static unsigned int stream_read_word(stream_t *s){
207 int x,y;
208 x=stream_read_char(s);
209 y=stream_read_char(s);
210 return (x<<8)|y;
213 inline static unsigned int stream_read_dword(stream_t *s){
214 unsigned int y;
215 y=stream_read_char(s);
216 y=(y<<8)|stream_read_char(s);
217 y=(y<<8)|stream_read_char(s);
218 y=(y<<8)|stream_read_char(s);
219 return y;
222 #define stream_read_fourcc stream_read_dword_le
224 inline static unsigned int stream_read_word_le(stream_t *s){
225 int x,y;
226 x=stream_read_char(s);
227 y=stream_read_char(s);
228 return (y<<8)|x;
231 inline static uint32_t stream_read_dword_le(stream_t *s)
233 unsigned int y;
234 y=stream_read_char(s);
235 y|=stream_read_char(s)<<8;
236 y|=stream_read_char(s)<<16;
237 y|=stream_read_char(s)<<24;
238 return y;
241 inline static uint64_t stream_read_qword(stream_t *s){
242 uint64_t y;
243 y = stream_read_char(s);
244 y=(y<<8)|stream_read_char(s);
245 y=(y<<8)|stream_read_char(s);
246 y=(y<<8)|stream_read_char(s);
247 y=(y<<8)|stream_read_char(s);
248 y=(y<<8)|stream_read_char(s);
249 y=(y<<8)|stream_read_char(s);
250 y=(y<<8)|stream_read_char(s);
251 return y;
254 inline static uint64_t stream_read_qword_le(stream_t *s){
255 uint64_t y;
256 y = stream_read_dword_le(s);
257 y|=(uint64_t)stream_read_dword_le(s)<<32;
258 return y;
261 inline static unsigned int stream_read_int24(stream_t *s){
262 unsigned int y;
263 y = stream_read_char(s);
264 y=(y<<8)|stream_read_char(s);
265 y=(y<<8)|stream_read_char(s);
266 return y;
269 inline static int stream_read(stream_t *s,char* mem,int total){
270 int len=total;
271 while(len>0){
272 int x;
273 x=s->buf_len-s->buf_pos;
274 if(x==0){
275 if(!cache_stream_fill_buffer(s)) return total-len; // EOF
276 x=s->buf_len-s->buf_pos;
278 if(s->buf_pos>s->buf_len) mp_msg(MSGT_DEMUX, MSGL_WARN, "stream_read: WARNING! s->buf_pos>s->buf_len\n");
279 if(x>len) x=len;
280 memcpy(mem,&s->buffer[s->buf_pos],x);
281 s->buf_pos+=x; mem+=x; len-=x;
283 return total;
286 unsigned char* stream_read_line(stream_t *s,unsigned char* mem, int max, int utf16);
288 inline static int stream_eof(stream_t *s){
289 return s->eof;
292 inline static off_t stream_tell(stream_t *s){
293 return s->pos+s->buf_pos-s->buf_len;
296 inline static int stream_seek(stream_t *s,off_t pos){
298 mp_dbg(MSGT_DEMUX, MSGL_DBG3, "seek to 0x%qX\n",(long long)pos);
300 if (pos < 0) {
301 mp_msg(MSGT_DEMUX, MSGL_ERR, "Invalid seek to negative position %llx!\n",
302 (long long)pos);
303 pos = 0;
305 if(pos<s->pos){
306 off_t x=pos-(s->pos-s->buf_len);
307 if(x>=0){
308 s->buf_pos=x;
309 s->eof = 0;
310 // putchar('*');fflush(stdout);
311 return 1;
315 return cache_stream_seek_long(s,pos);
318 inline static int stream_skip(stream_t *s,off_t len){
319 if( len<0 || (len>2*STREAM_BUFFER_SIZE && (s->flags & MP_STREAM_SEEK_FW)) ) {
320 // negative or big skip!
321 return stream_seek(s,stream_tell(s)+len);
323 while(len>0){
324 int x=s->buf_len-s->buf_pos;
325 if(x==0){
326 if(!cache_stream_fill_buffer(s)) return 0; // EOF
327 x=s->buf_len-s->buf_pos;
329 if(x>len) x=len;
330 //memcpy(mem,&s->buf[s->buf_pos],x);
331 s->buf_pos+=x; len-=x;
333 return 1;
336 struct MPOpts;
338 * Return allocated buffer for all data until EOF.
339 * If amount of data would be more than max_size return NULL as data ptr.
340 * Make the allocated buffer padding_bytes larger than the data read.
341 * Write number of bytes read at *amount_read.
343 struct bstr stream_read_complete(struct stream *s, void *talloc_ctx,
344 int max_size, int padding_bytes);
345 void stream_reset(stream_t *s);
346 int stream_control(stream_t *s, int cmd, void *arg);
347 stream_t* new_stream(int fd,int type);
348 void free_stream(stream_t *s);
349 stream_t* new_memory_stream(unsigned char* data,int len);
350 stream_t *open_stream(const char *filename, struct MPOpts *options,
351 int *file_format);
352 stream_t *open_stream_full(const char *filename,int mode,
353 struct MPOpts *options, int *file_format);
354 stream_t *open_output_stream(const char *filename, struct MPOpts *options);
355 struct demux_stream;
356 struct stream *new_ds_stream(struct demux_stream *ds);
358 /// Set the callback to be used by libstream to check for user
359 /// interruption during long blocking operations (cache filling, etc).
360 struct input_ctx;
361 void stream_set_interrupt_callback(int (*cb)(struct input_ctx*, int),
362 struct input_ctx *ctx);
363 /// Call the interrupt checking callback if there is one and
364 /// wait for time milliseconds
365 int stream_check_interrupt(int time);
366 /// Internal read function bypassing the stream buffer
367 int stream_read_internal(stream_t *s, void *buf, int len);
368 /// Internal seek function bypassing the stream buffer
369 int stream_seek_internal(stream_t *s, off_t newpos);
371 extern int bluray_angle;
372 extern int bluray_chapter;
373 extern int dvd_title;
374 extern int dvd_angle;
376 extern char *bluray_device;
378 typedef struct {
379 int id; // 0 - 31 mpeg; 128 - 159 ac3; 160 - 191 pcm
380 int language;
381 int type;
382 int channels;
383 } stream_language_t;
385 #endif /* MPLAYER_STREAM_H */