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