Fix compilation after FFmpeg r21353.
[mplayer/glamo.git] / stream / stream.h
blob1270553a00f7ea7150f1cdb69de6bc2cd17f3fd3
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_st;
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* 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_st {
96 // Read
97 int (*fill_buffer)(struct stream_st *s, char* buffer, int max_len);
98 // Write
99 int (*write_buffer)(struct stream_st *s, char* buffer, int len);
100 // Seek
101 int (*seek)(struct stream_st *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_st *s,int cmd,void* arg);
106 // Close
107 void (*close)(struct stream_st *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 #ifdef CONFIG_NETWORK
122 streaming_ctrl_t *streaming_ctrl;
123 #endif
124 unsigned char buffer[STREAM_BUFFER_SIZE>VCD_SECTOR_SIZE?STREAM_BUFFER_SIZE:VCD_SECTOR_SIZE];
125 } stream_t;
127 #ifdef CONFIG_STREAM_CACHE
128 int stream_enable_cache(stream_t *stream,int size,int min,int prefill);
129 int cache_stream_fill_buffer(stream_t *s);
130 int cache_stream_seek_long(stream_t *s,off_t pos);
131 #else
132 // no cache, define wrappers:
133 int stream_fill_buffer(stream_t *s);
134 int stream_seek_long(stream_t *s,off_t pos);
135 #define cache_stream_fill_buffer(x) stream_fill_buffer(x)
136 #define cache_stream_seek_long(x,y) stream_seek_long(x,y)
137 #define stream_enable_cache(x,y,z,w) 1
138 #endif
139 void fixup_network_stream_cache(stream_t *stream);
140 int stream_write_buffer(stream_t *s, unsigned char *buf, int len);
142 inline static int stream_read_char(stream_t *s){
143 return (s->buf_pos<s->buf_len)?s->buffer[s->buf_pos++]:
144 (cache_stream_fill_buffer(s)?s->buffer[s->buf_pos++]:-256);
145 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
146 // stream_fill_buffer(s);
147 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
148 // return 0; // EOF
151 inline static unsigned int stream_read_word(stream_t *s){
152 int x,y;
153 x=stream_read_char(s);
154 y=stream_read_char(s);
155 return (x<<8)|y;
158 inline static unsigned int stream_read_dword(stream_t *s){
159 unsigned int y;
160 y=stream_read_char(s);
161 y=(y<<8)|stream_read_char(s);
162 y=(y<<8)|stream_read_char(s);
163 y=(y<<8)|stream_read_char(s);
164 return y;
167 #define stream_read_fourcc stream_read_dword_le
169 inline static unsigned int stream_read_word_le(stream_t *s){
170 int x,y;
171 x=stream_read_char(s);
172 y=stream_read_char(s);
173 return (y<<8)|x;
176 inline static unsigned int stream_read_dword_le(stream_t *s){
177 unsigned int y;
178 y=stream_read_char(s);
179 y|=stream_read_char(s)<<8;
180 y|=stream_read_char(s)<<16;
181 y|=stream_read_char(s)<<24;
182 return y;
185 inline static uint64_t stream_read_qword(stream_t *s){
186 uint64_t y;
187 y = 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 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 return y;
198 inline static uint64_t stream_read_qword_le(stream_t *s){
199 uint64_t y;
200 y = stream_read_dword_le(s);
201 y|=(uint64_t)stream_read_dword_le(s)<<32;
202 return y;
205 inline static unsigned int stream_read_int24(stream_t *s){
206 unsigned int y;
207 y = stream_read_char(s);
208 y=(y<<8)|stream_read_char(s);
209 y=(y<<8)|stream_read_char(s);
210 return y;
213 inline static int stream_read(stream_t *s,char* mem,int total){
214 int len=total;
215 while(len>0){
216 int x;
217 x=s->buf_len-s->buf_pos;
218 if(x==0){
219 if(!cache_stream_fill_buffer(s)) return total-len; // EOF
220 x=s->buf_len-s->buf_pos;
222 if(s->buf_pos>s->buf_len) mp_msg(MSGT_DEMUX, MSGL_WARN, "stream_read: WARNING! s->buf_pos>s->buf_len\n");
223 if(x>len) x=len;
224 memcpy(mem,&s->buffer[s->buf_pos],x);
225 s->buf_pos+=x; mem+=x; len-=x;
227 return total;
230 inline static unsigned char* stream_read_line(stream_t *s,unsigned char* mem, int max) {
231 int len;
232 unsigned char* end,*ptr = mem;
233 do {
234 len = s->buf_len-s->buf_pos;
235 // try to fill the buffer
236 if(len <= 0 &&
237 (!cache_stream_fill_buffer(s) ||
238 (len = s->buf_len-s->buf_pos) <= 0)) break;
239 end = (unsigned char*) memchr((void*)(s->buffer+s->buf_pos),'\n',len);
240 if(end) len = end - (s->buffer+s->buf_pos) + 1;
241 if(len > 0 && max > 1) {
242 int l = len > max-1 ? max-1 : len;
243 memcpy(ptr,s->buffer+s->buf_pos,l);
244 max -= l;
245 ptr += l;
247 s->buf_pos += len;
248 } while(!end);
249 if(s->eof && ptr == mem) return NULL;
250 if(max > 0) ptr[0] = 0;
251 return mem;
255 inline static int stream_eof(stream_t *s){
256 return s->eof;
259 inline static off_t stream_tell(stream_t *s){
260 return s->pos+s->buf_pos-s->buf_len;
263 inline static int stream_seek(stream_t *s,off_t pos){
265 mp_dbg(MSGT_DEMUX, MSGL_DBG3, "seek to 0x%qX\n",(long long)pos);
267 if(pos<s->pos){
268 off_t x=pos-(s->pos-s->buf_len);
269 if(x>=0){
270 s->buf_pos=x;
271 // putchar('*');fflush(stdout);
272 return 1;
276 return cache_stream_seek_long(s,pos);
279 inline static int stream_skip(stream_t *s,off_t len){
280 if( (len<0 && (s->flags & MP_STREAM_SEEK_BW)) || (len>2*STREAM_BUFFER_SIZE && (s->flags & MP_STREAM_SEEK_FW)) ) {
281 // negative or big skip!
282 return stream_seek(s,stream_tell(s)+len);
284 while(len>0){
285 int x=s->buf_len-s->buf_pos;
286 if(x==0){
287 if(!cache_stream_fill_buffer(s)) return 0; // EOF
288 x=s->buf_len-s->buf_pos;
290 if(x>len) x=len;
291 //memcpy(mem,&s->buf[s->buf_pos],x);
292 s->buf_pos+=x; len-=x;
294 return 1;
297 void stream_reset(stream_t *s);
298 int stream_control(stream_t *s, int cmd, void *arg);
299 stream_t* new_stream(int fd,int type);
300 void free_stream(stream_t *s);
301 stream_t* new_memory_stream(unsigned char* data,int len);
302 stream_t* open_stream(char* filename,char** options,int* file_format);
303 stream_t* open_stream_full(char* filename,int mode, char** options, int* file_format);
304 stream_t* open_output_stream(char* filename,char** options);
305 /// Set the callback to be used by libstream to check for user
306 /// interruption during long blocking operations (cache filling, etc).
307 void stream_set_interrupt_callback(int (*cb)(int));
308 /// Call the interrupt checking callback if there is one.
309 int stream_check_interrupt(int time);
311 extern int dvd_title;
312 extern int dvd_chapter;
313 extern int dvd_last_chapter;
314 extern int dvd_angle;
316 extern char * audio_stream;
318 typedef struct {
319 int id; // 0 - 31 mpeg; 128 - 159 ac3; 160 - 191 pcm
320 int language;
321 int type;
322 int channels;
323 } stream_language_t;
325 #endif /* MPLAYER_STREAM_H */