Merge svn changes up to r29412
[mplayer.git] / stream / stream.h
blob51dbbc925c9d3783aefc893b6c252910b1ba1f9b
1 #ifndef MPLAYER_STREAM_H
2 #define MPLAYER_STREAM_H
4 #include "mp_msg.h"
5 #include <string.h>
6 #include <inttypes.h>
7 #include <sys/types.h>
9 #define STREAMTYPE_DUMMY -1 // for placeholders, when the actual reading is handled in the demuxer
10 #define STREAMTYPE_FILE 0 // read from seekable file
11 #define STREAMTYPE_VCD 1 // raw mode-2 CDROM reading, 2324 bytes/sector
12 #define STREAMTYPE_STREAM 2 // same as FILE but no seeking (for net/stdin)
13 #define STREAMTYPE_DVD 3 // libdvdread
14 #define STREAMTYPE_MEMORY 4 // read data from memory area
15 #define STREAMTYPE_PLAYLIST 6 // FIXME!!! same as STREAMTYPE_FILE now
16 #define STREAMTYPE_DS 8 // read from a demuxer stream
17 #define STREAMTYPE_DVDNAV 9 // we cannot safely "seek" in this...
18 #define STREAMTYPE_CDDA 10 // raw audio CD reader
19 #define STREAMTYPE_SMB 11 // smb:// url, using libsmbclient (samba)
20 #define STREAMTYPE_VCDBINCUE 12 // vcd directly from bin/cue files
21 #define STREAMTYPE_DVB 13
22 #define STREAMTYPE_VSTREAM 14
23 #define STREAMTYPE_SDP 15
24 #define STREAMTYPE_PVR 16
25 #define STREAMTYPE_TV 17
26 #define STREAMTYPE_MF 18
27 #define STREAMTYPE_RADIO 19
29 #define STREAM_BUFFER_SIZE 2048
31 #define VCD_SECTOR_SIZE 2352
32 #define VCD_SECTOR_OFFS 24
33 #define VCD_SECTOR_DATA 2324
35 /// atm it will always use mode == STREAM_READ
36 /// streams that use the new api should check the mode at open
37 #define STREAM_READ 0
38 #define STREAM_WRITE 1
39 /// Seek flags, if not mannualy set and s->seek isn't NULL
40 /// STREAM_SEEK is automaticly set
41 #define STREAM_SEEK_BW 2
42 #define STREAM_SEEK_FW 4
43 #define STREAM_SEEK (STREAM_SEEK_BW|STREAM_SEEK_FW)
45 //////////// Open return code
46 #define STREAM_REDIRECTED -2
47 /// This can't open the requested protocol (used by stream wich have a
48 /// * protocol when they don't know the requested protocol)
49 #define STREAM_UNSUPPORTED -1
50 #define STREAM_ERROR 0
51 #define STREAM_OK 1
53 #define MAX_STREAM_PROTOCOLS 10
55 #define STREAM_CTRL_RESET 0
56 #define STREAM_CTRL_GET_TIME_LENGTH 1
57 #define STREAM_CTRL_SEEK_TO_CHAPTER 2
58 #define STREAM_CTRL_GET_CURRENT_CHAPTER 3
59 #define STREAM_CTRL_GET_NUM_CHAPTERS 4
60 #define STREAM_CTRL_GET_CURRENT_TIME 5
61 #define STREAM_CTRL_SEEK_TO_TIME 6
62 #define STREAM_CTRL_GET_SIZE 7
63 #define STREAM_CTRL_GET_ASPECT_RATIO 8
64 #define STREAM_CTRL_GET_NUM_ANGLES 9
65 #define STREAM_CTRL_GET_ANGLE 10
66 #define STREAM_CTRL_SET_ANGLE 11
69 #ifdef CONFIG_NETWORK
70 #include "network.h"
71 #endif
73 struct stream;
74 typedef struct stream_info_st {
75 const char *info;
76 const char *name;
77 const char *author;
78 const char *comment;
79 /// mode isn't used atm (ie always READ) but it shouldn't be ignored
80 /// opts is at least in it's defaults settings and may have been
81 /// altered by url parsing if enabled and the options string parsing.
82 int (*open)(struct stream* st, int mode, void* opts, int* file_format);
83 const char* protocols[MAX_STREAM_PROTOCOLS];
84 const void* opts;
85 int opts_url; /* If this is 1 we will parse the url as an option string
86 * too. Otherwise options are only parsed from the
87 * options string given to open_stream_plugin */
88 } stream_info_t;
90 typedef struct stream {
91 // Read
92 int (*fill_buffer)(struct stream *s, char* buffer, int max_len);
93 // Write
94 int (*write_buffer)(struct stream *s, char* buffer, int len);
95 // Seek
96 int (*seek)(struct stream *s,off_t pos);
97 // Control
98 // Will be later used to let streams like dvd and cdda report
99 // their structure (ie tracks, chapters, etc)
100 int (*control)(struct stream *s,int cmd,void* arg);
101 // Close
102 void (*close)(struct stream *s);
104 int fd; // file descriptor, see man open(2)
105 int type; // see STREAMTYPE_*
106 int flags;
107 int sector_size; // sector size (seek will be aligned on this size if non 0)
108 unsigned int buf_pos,buf_len;
109 off_t pos,start_pos,end_pos;
110 int eof;
111 int mode; //STREAM_READ or STREAM_WRITE
112 unsigned int cache_pid;
113 void* cache_data;
114 void* priv; // used for DVD, TV, RTSP etc
115 char* url; // strdup() of filename/url
116 struct MPOpts *opts;
117 #ifdef CONFIG_NETWORK
118 streaming_ctrl_t *streaming_ctrl;
119 #endif
120 unsigned char buffer[STREAM_BUFFER_SIZE>VCD_SECTOR_SIZE?STREAM_BUFFER_SIZE:VCD_SECTOR_SIZE];
121 } stream_t;
123 int stream_fill_buffer(stream_t *s);
124 int stream_seek_long(stream_t *s,off_t pos);
125 #ifdef CONFIG_STREAM_CACHE
126 int stream_enable_cache(stream_t *stream,int size,int min,int prefill);
127 int cache_stream_fill_buffer(stream_t *s);
128 int cache_stream_seek_long(stream_t *s,off_t pos);
129 #else
130 // no cache, define wrappers:
131 #define cache_stream_fill_buffer(x) stream_fill_buffer(x)
132 #define cache_stream_seek_long(x,y) stream_seek_long(x,y)
133 #define stream_enable_cache(x,y,z,w) 1
134 #endif
135 void fixup_network_stream_cache(stream_t *stream);
136 int stream_write_buffer(stream_t *s, unsigned char *buf, int len);
138 inline static int stream_read_char(stream_t *s){
139 return (s->buf_pos<s->buf_len)?s->buffer[s->buf_pos++]:
140 (cache_stream_fill_buffer(s)?s->buffer[s->buf_pos++]:-256);
141 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
142 // stream_fill_buffer(s);
143 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
144 // return 0; // EOF
147 inline static unsigned int stream_read_word(stream_t *s){
148 int x,y;
149 x=stream_read_char(s);
150 y=stream_read_char(s);
151 return (x<<8)|y;
154 inline static unsigned int stream_read_dword(stream_t *s){
155 unsigned int y;
156 y=stream_read_char(s);
157 y=(y<<8)|stream_read_char(s);
158 y=(y<<8)|stream_read_char(s);
159 y=(y<<8)|stream_read_char(s);
160 return y;
163 #define stream_read_fourcc stream_read_dword_le
165 inline static unsigned int stream_read_word_le(stream_t *s){
166 int x,y;
167 x=stream_read_char(s);
168 y=stream_read_char(s);
169 return (y<<8)|x;
172 inline static unsigned int stream_read_dword_le(stream_t *s){
173 unsigned int y;
174 y=stream_read_char(s);
175 y|=stream_read_char(s)<<8;
176 y|=stream_read_char(s)<<16;
177 y|=stream_read_char(s)<<24;
178 return y;
181 inline static uint64_t stream_read_qword(stream_t *s){
182 uint64_t y;
183 y = stream_read_char(s);
184 y=(y<<8)|stream_read_char(s);
185 y=(y<<8)|stream_read_char(s);
186 y=(y<<8)|stream_read_char(s);
187 y=(y<<8)|stream_read_char(s);
188 y=(y<<8)|stream_read_char(s);
189 y=(y<<8)|stream_read_char(s);
190 y=(y<<8)|stream_read_char(s);
191 return y;
194 inline static uint64_t stream_read_qword_le(stream_t *s){
195 uint64_t y;
196 y = stream_read_dword_le(s);
197 y|=(uint64_t)stream_read_dword_le(s)<<32;
198 return y;
201 inline static unsigned int stream_read_int24(stream_t *s){
202 unsigned int y;
203 y = stream_read_char(s);
204 y=(y<<8)|stream_read_char(s);
205 y=(y<<8)|stream_read_char(s);
206 return y;
209 inline static int stream_read(stream_t *s,char* mem,int total){
210 int len=total;
211 while(len>0){
212 int x;
213 x=s->buf_len-s->buf_pos;
214 if(x==0){
215 if(!cache_stream_fill_buffer(s)) return total-len; // EOF
216 x=s->buf_len-s->buf_pos;
218 if(s->buf_pos>s->buf_len) mp_msg(MSGT_DEMUX, MSGL_WARN, "stream_read: WARNING! s->buf_pos>s->buf_len\n");
219 if(x>len) x=len;
220 memcpy(mem,&s->buffer[s->buf_pos],x);
221 s->buf_pos+=x; mem+=x; len-=x;
223 return total;
226 inline static unsigned char* stream_read_line(stream_t *s,unsigned char* mem, int max) {
227 int len;
228 unsigned char* end,*ptr = mem;
229 do {
230 len = s->buf_len-s->buf_pos;
231 // try to fill the buffer
232 if(len <= 0 &&
233 (!cache_stream_fill_buffer(s) ||
234 (len = s->buf_len-s->buf_pos) <= 0)) break;
235 end = (unsigned char*) memchr((void*)(s->buffer+s->buf_pos),'\n',len);
236 if(end) len = end - (s->buffer+s->buf_pos) + 1;
237 if(len > 0 && max > 1) {
238 int l = len > max-1 ? max-1 : len;
239 memcpy(ptr,s->buffer+s->buf_pos,l);
240 max -= l;
241 ptr += l;
243 s->buf_pos += len;
244 } while(!end);
245 if(s->eof && ptr == mem) return NULL;
246 if(max > 0) ptr[0] = 0;
247 return mem;
251 inline static int stream_eof(stream_t *s){
252 return s->eof;
255 inline static off_t stream_tell(stream_t *s){
256 return s->pos+s->buf_pos-s->buf_len;
259 inline static int stream_seek(stream_t *s,off_t pos){
261 mp_dbg(MSGT_DEMUX, MSGL_DBG3, "seek to 0x%qX\n",(long long)pos);
263 if(pos<s->pos){
264 off_t x=pos-(s->pos-s->buf_len);
265 if(x>=0){
266 s->buf_pos=x;
267 // putchar('*');fflush(stdout);
268 return 1;
272 return cache_stream_seek_long(s,pos);
275 inline static int stream_skip(stream_t *s,off_t len){
276 if( (len<0 && (s->flags & STREAM_SEEK_BW)) || (len>2*STREAM_BUFFER_SIZE && (s->flags & STREAM_SEEK_FW)) ) {
277 // negative or big skip!
278 return stream_seek(s,stream_tell(s)+len);
280 while(len>0){
281 int x=s->buf_len-s->buf_pos;
282 if(x==0){
283 if(!cache_stream_fill_buffer(s)) return 0; // EOF
284 x=s->buf_len-s->buf_pos;
286 if(x>len) x=len;
287 //memcpy(mem,&s->buf[s->buf_pos],x);
288 s->buf_pos+=x; len-=x;
290 return 1;
293 struct MPOpts;
294 void stream_reset(stream_t *s);
295 int stream_control(stream_t *s, int cmd, void *arg);
296 stream_t* new_stream(int fd,int type);
297 void free_stream(stream_t *s);
298 stream_t* new_memory_stream(unsigned char* data,int len);
299 stream_t* open_stream(char* filename, struct MPOpts *options,int* file_format);
300 stream_t* open_stream_full(char* filename,int mode, struct MPOpts *options, int* file_format);
301 stream_t* open_output_stream(char* filename,struct MPOpts *options);
302 /// Set the callback to be used by libstream to check for user
303 /// interruption during long blocking operations (cache filling, etc).
304 struct input_ctx;
305 void stream_set_interrupt_callback(int (*cb)(struct input_ctx*, int),
306 struct input_ctx *ctx);
307 /// Call the interrupt checking callback if there is one.
308 int stream_check_interrupt(int time);
310 extern int dvd_title;
311 extern int dvd_chapter;
312 extern int dvd_last_chapter;
313 extern int dvd_angle;
315 extern char * audio_stream;
317 typedef struct {
318 int id; // 0 - 31 mpeg; 128 - 159 ac3; 160 - 191 pcm
319 int language;
320 int type;
321 int channels;
322 } stream_language_t;
324 #endif /* MPLAYER_STREAM_H */