Sync with latest FFmpeg changes.
[mplayer/glamo.git] / stream / stream_file.c
blob1abc2e1c9e8f7cb05ebe3388f9b782a6969ad9c5
2 #include "config.h"
4 #include <stdio.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
10 #include "mp_msg.h"
11 #include "stream.h"
12 #include "help_mp.h"
13 #include "m_option.h"
14 #include "m_struct.h"
16 static struct stream_priv_s {
17 char* filename;
18 char *filename2;
19 } stream_priv_dflts = {
20 NULL, NULL
23 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
24 /// URL definition
25 static const m_option_t stream_opts_fields[] = {
26 {"string", ST_OFF(filename), CONF_TYPE_STRING, 0, 0 ,0, NULL},
27 {"filename", ST_OFF(filename2), CONF_TYPE_STRING, 0, 0 ,0, NULL},
28 { NULL, NULL, 0, 0, 0, 0, NULL }
30 static const struct m_struct_st stream_opts = {
31 "file",
32 sizeof(struct stream_priv_s),
33 &stream_priv_dflts,
34 stream_opts_fields
35 };
37 static int fill_buffer(stream_t *s, char* buffer, int max_len){
38 int r = read(s->fd,buffer,max_len);
39 return (r <= 0) ? -1 : r;
42 static int write_buffer(stream_t *s, char* buffer, int len) {
43 int r = write(s->fd,buffer,len);
44 return (r <= 0) ? -1 : r;
47 static int seek(stream_t *s,off_t newpos) {
48 s->pos = newpos;
49 if(lseek(s->fd,s->pos,SEEK_SET)<0) {
50 s->eof=1;
51 return 0;
53 return 1;
56 static int seek_forward(stream_t *s,off_t newpos) {
57 if(newpos<s->pos){
58 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
59 return 0;
61 while(s->pos<newpos){
62 int len=s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE);
63 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; break; } // EOF
64 s->buf_pos=0;
65 s->buf_len=len;
66 s->pos+=len;
68 return 1;
71 static int control(stream_t *s, int cmd, void *arg) {
72 switch(cmd) {
73 case STREAM_CTRL_GET_SIZE: {
74 off_t size;
76 size = lseek(s->fd, 0, SEEK_END);
77 lseek(s->fd, s->pos, SEEK_SET);
78 if(size != (off_t)-1) {
79 *((off_t*)arg) = size;
80 return 1;
84 return STREAM_UNSUPPORTED;
87 static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
88 int f;
89 mode_t m = 0;
90 off_t len;
91 unsigned char *filename;
92 struct stream_priv_s* p = (struct stream_priv_s*)opts;
94 if(mode == STREAM_READ)
95 m = O_RDONLY;
96 else if(mode == STREAM_WRITE)
97 m = O_RDWR|O_CREAT|O_TRUNC;
98 else {
99 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] Unknown open mode %d\n",mode);
100 m_struct_free(&stream_opts,opts);
101 return STREAM_UNSUPPORTED;
104 if(p->filename)
105 filename = p->filename;
106 else if(p->filename2)
107 filename = p->filename2;
108 else
109 filename = NULL;
110 if(!filename) {
111 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] No filename\n");
112 m_struct_free(&stream_opts,opts);
113 return STREAM_ERROR;
116 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
117 // extract '/' from '/x:/path'
118 if( filename[ 0 ] == '/' && filename[ 1 ] && filename[ 2 ] == ':' )
119 filename++;
120 #endif
122 #if defined(__CYGWIN__)|| defined(__MINGW32__)
123 m |= O_BINARY;
124 #endif
126 if(!strcmp(filename,"-")){
127 if(mode == STREAM_READ) {
128 // read from stdin
129 mp_msg(MSGT_OPEN,MSGL_INFO,MSGTR_ReadSTDIN);
130 f=0; // 0=stdin
131 #ifdef __MINGW32__
132 setmode(fileno(stdin),O_BINARY);
133 #endif
134 } else {
135 mp_msg(MSGT_OPEN,MSGL_INFO,"Writing to stdout\n");
136 f=1;
137 #ifdef __MINGW32__
138 setmode(fileno(stdout),O_BINARY);
139 #endif
141 } else {
142 mode_t openmode = S_IRUSR|S_IWUSR;
143 #ifndef __MINGW32__
144 openmode |= S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
145 #endif
146 f=open(filename,m, openmode);
147 if(f<0) {
148 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_FileNotFound,filename);
149 m_struct_free(&stream_opts,opts);
150 return STREAM_ERROR;
154 len=lseek(f,0,SEEK_END); lseek(f,0,SEEK_SET);
155 #ifdef __MINGW32__
156 if(f==0 || len == -1) {
157 #else
158 if(len == -1) {
159 #endif
160 if(mode == STREAM_READ) stream->seek = seek_forward;
161 stream->type = STREAMTYPE_STREAM; // Must be move to STREAMTYPE_FILE
162 stream->flags |= STREAM_SEEK_FW;
163 } else if(len >= 0) {
164 stream->seek = seek;
165 stream->end_pos = len;
166 stream->type = STREAMTYPE_FILE;
169 mp_msg(MSGT_OPEN,MSGL_V,"[file] File size is %"PRId64" bytes\n", (int64_t)len);
171 stream->fd = f;
172 stream->fill_buffer = fill_buffer;
173 stream->write_buffer = write_buffer;
174 stream->control = control;
176 m_struct_free(&stream_opts,opts);
177 return STREAM_OK;
180 const stream_info_t stream_info_file = {
181 "File",
182 "file",
183 "Albeu",
184 "based on the code from ??? (probably Arpi)",
185 open_f,
186 { "file", "", NULL },
187 &stream_opts,
188 1 // Urls are an option string