Add a missing check for a NULL return value.
[mplayer/glamo.git] / stream / stream.h
blob2bbe4437574a040187ddaef3a9154cc86f994a2c
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_st;
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* 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_st {
91 // Read
92 int (*fill_buffer)(struct stream_st *s, char* buffer, int max_len);
93 // Write
94 int (*write_buffer)(struct stream_st *s, char* buffer, int len);
95 // Seek
96 int (*seek)(struct stream_st *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_st *s,int cmd,void* arg);
101 // Close
102 void (*close)(struct stream_st *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 #ifdef CONFIG_NETWORK
117 streaming_ctrl_t *streaming_ctrl;
118 #endif
119 unsigned char buffer[STREAM_BUFFER_SIZE>VCD_SECTOR_SIZE?STREAM_BUFFER_SIZE:VCD_SECTOR_SIZE];
120 } stream_t;
122 #ifdef CONFIG_STREAM_CACHE
123 int stream_enable_cache(stream_t *stream,int size,int min,int prefill);
124 int cache_stream_fill_buffer(stream_t *s);
125 int cache_stream_seek_long(stream_t *s,off_t pos);
126 #else
127 // no cache, define wrappers:
128 int stream_fill_buffer(stream_t *s);
129 int stream_seek_long(stream_t *s,off_t pos);
130 #define cache_stream_fill_buffer(x) stream_fill_buffer(x)
131 #define cache_stream_seek_long(x,y) stream_seek_long(x,y)
132 #define stream_enable_cache(x,y,z,w) 1
133 #endif
134 void fixup_network_stream_cache(stream_t *stream);
135 int stream_write_buffer(stream_t *s, unsigned char *buf, int len);
137 inline static int stream_read_char(stream_t *s){
138 return (s->buf_pos<s->buf_len)?s->buffer[s->buf_pos++]:
139 (cache_stream_fill_buffer(s)?s->buffer[s->buf_pos++]:-256);
140 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
141 // stream_fill_buffer(s);
142 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
143 // return 0; // EOF
146 inline static unsigned int stream_read_word(stream_t *s){
147 int x,y;
148 x=stream_read_char(s);
149 y=stream_read_char(s);
150 return (x<<8)|y;
153 inline static unsigned int stream_read_dword(stream_t *s){
154 unsigned int y;
155 y=stream_read_char(s);
156 y=(y<<8)|stream_read_char(s);
157 y=(y<<8)|stream_read_char(s);
158 y=(y<<8)|stream_read_char(s);
159 return y;
162 #define stream_read_fourcc stream_read_dword_le
164 inline static unsigned int stream_read_word_le(stream_t *s){
165 int x,y;
166 x=stream_read_char(s);
167 y=stream_read_char(s);
168 return (y<<8)|x;
171 inline static unsigned int stream_read_dword_le(stream_t *s){
172 unsigned int y;
173 y=stream_read_char(s);
174 y|=stream_read_char(s)<<8;
175 y|=stream_read_char(s)<<16;
176 y|=stream_read_char(s)<<24;
177 return y;
180 inline static uint64_t stream_read_qword(stream_t *s){
181 uint64_t y;
182 y = stream_read_char(s);
183 y=(y<<8)|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 return y;
193 inline static uint64_t stream_read_qword_le(stream_t *s){
194 uint64_t y;
195 y = stream_read_dword_le(s);
196 y|=(uint64_t)stream_read_dword_le(s)<<32;
197 return y;
200 inline static unsigned int stream_read_int24(stream_t *s){
201 unsigned int y;
202 y = stream_read_char(s);
203 y=(y<<8)|stream_read_char(s);
204 y=(y<<8)|stream_read_char(s);
205 return y;
208 inline static int stream_read(stream_t *s,char* mem,int total){
209 int len=total;
210 while(len>0){
211 int x;
212 x=s->buf_len-s->buf_pos;
213 if(x==0){
214 if(!cache_stream_fill_buffer(s)) return total-len; // EOF
215 x=s->buf_len-s->buf_pos;
217 if(s->buf_pos>s->buf_len) mp_msg(MSGT_DEMUX, MSGL_WARN, "stream_read: WARNING! s->buf_pos>s->buf_len\n");
218 if(x>len) x=len;
219 memcpy(mem,&s->buffer[s->buf_pos],x);
220 s->buf_pos+=x; mem+=x; len-=x;
222 return total;
225 inline static unsigned char* stream_read_line(stream_t *s,unsigned char* mem, int max) {
226 int len;
227 unsigned char* end,*ptr = mem;
228 do {
229 len = s->buf_len-s->buf_pos;
230 // try to fill the buffer
231 if(len <= 0 &&
232 (!cache_stream_fill_buffer(s) ||
233 (len = s->buf_len-s->buf_pos) <= 0)) break;
234 end = (unsigned char*) memchr((void*)(s->buffer+s->buf_pos),'\n',len);
235 if(end) len = end - (s->buffer+s->buf_pos) + 1;
236 if(len > 0 && max > 1) {
237 int l = len > max-1 ? max-1 : len;
238 memcpy(ptr,s->buffer+s->buf_pos,l);
239 max -= l;
240 ptr += l;
242 s->buf_pos += len;
243 } while(!end);
244 if(s->eof && ptr == mem) return NULL;
245 if(max > 0) ptr[0] = 0;
246 return mem;
250 inline static int stream_eof(stream_t *s){
251 return s->eof;
254 inline static off_t stream_tell(stream_t *s){
255 return s->pos+s->buf_pos-s->buf_len;
258 inline static int stream_seek(stream_t *s,off_t pos){
260 mp_dbg(MSGT_DEMUX, MSGL_DBG3, "seek to 0x%qX\n",(long long)pos);
262 if(pos<s->pos){
263 off_t x=pos-(s->pos-s->buf_len);
264 if(x>=0){
265 s->buf_pos=x;
266 // putchar('*');fflush(stdout);
267 return 1;
271 return cache_stream_seek_long(s,pos);
274 inline static int stream_skip(stream_t *s,off_t len){
275 if( (len<0 && (s->flags & STREAM_SEEK_BW)) || (len>2*STREAM_BUFFER_SIZE && (s->flags & STREAM_SEEK_FW)) ) {
276 // negative or big skip!
277 return stream_seek(s,stream_tell(s)+len);
279 while(len>0){
280 int x=s->buf_len-s->buf_pos;
281 if(x==0){
282 if(!cache_stream_fill_buffer(s)) return 0; // EOF
283 x=s->buf_len-s->buf_pos;
285 if(x>len) x=len;
286 //memcpy(mem,&s->buf[s->buf_pos],x);
287 s->buf_pos+=x; len-=x;
289 return 1;
292 void stream_reset(stream_t *s);
293 int stream_control(stream_t *s, int cmd, void *arg);
294 stream_t* new_stream(int fd,int type);
295 void free_stream(stream_t *s);
296 stream_t* new_memory_stream(unsigned char* data,int len);
297 stream_t* open_stream(char* filename,char** options,int* file_format);
298 stream_t* open_stream_full(char* filename,int mode, char** options, int* file_format);
299 stream_t* open_output_stream(char* filename,char** options);
300 /// Set the callback to be used by libstream to check for user
301 /// interruption during long blocking operations (cache filling, etc).
302 void stream_set_interrupt_callback(int (*cb)(int));
303 /// Call the interrupt checking callback if there is one.
304 int stream_check_interrupt(int time);
306 extern int dvd_title;
307 extern int dvd_chapter;
308 extern int dvd_last_chapter;
309 extern int dvd_angle;
311 extern char * audio_stream;
313 typedef struct {
314 int id; // 0 - 31 mpeg; 128 - 159 ac3; 160 - 191 pcm
315 int language;
316 int type;
317 int channels;
318 } stream_language_t;
320 #endif /* MPLAYER_STREAM_H */