Raise LIBASS_VERSION, forgotten in r31293.
[mplayer/glamo.git] / stream / stream_file.c
blob37bbee4cfbfeb750cc164694807af5e96d5f605f
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
27 #include "mp_msg.h"
28 #include "stream.h"
29 #include "help_mp.h"
30 #include "m_option.h"
31 #include "m_struct.h"
33 static struct stream_priv_s {
34 char* filename;
35 char *filename2;
36 } stream_priv_dflts = {
37 NULL, NULL
40 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
41 /// URL definition
42 static const m_option_t stream_opts_fields[] = {
43 {"string", ST_OFF(filename), CONF_TYPE_STRING, 0, 0 ,0, NULL},
44 {"filename", ST_OFF(filename2), CONF_TYPE_STRING, 0, 0 ,0, NULL},
45 { NULL, NULL, 0, 0, 0, 0, NULL }
47 static const struct m_struct_st stream_opts = {
48 "file",
49 sizeof(struct stream_priv_s),
50 &stream_priv_dflts,
51 stream_opts_fields
54 static int fill_buffer(stream_t *s, char* buffer, int max_len){
55 int r = read(s->fd,buffer,max_len);
56 return (r <= 0) ? -1 : r;
59 static int write_buffer(stream_t *s, char* buffer, int len) {
60 int r = write(s->fd,buffer,len);
61 return (r <= 0) ? -1 : r;
64 static int seek(stream_t *s,off_t newpos) {
65 s->pos = newpos;
66 if(lseek(s->fd,s->pos,SEEK_SET)<0) {
67 s->eof=1;
68 return 0;
70 return 1;
73 static int seek_forward(stream_t *s,off_t newpos) {
74 if(newpos<s->pos){
75 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
76 return 0;
78 while(s->pos<newpos){
79 int len=s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE);
80 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; break; } // EOF
81 s->buf_pos=0;
82 s->buf_len=len;
83 s->pos+=len;
85 return 1;
88 static int control(stream_t *s, int cmd, void *arg) {
89 switch(cmd) {
90 case STREAM_CTRL_GET_SIZE: {
91 off_t size;
93 size = lseek(s->fd, 0, SEEK_END);
94 lseek(s->fd, s->pos, SEEK_SET);
95 if(size != (off_t)-1) {
96 *((off_t*)arg) = size;
97 return 1;
101 return STREAM_UNSUPPORTED;
104 static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
105 int f;
106 mode_t m = 0;
107 off_t len;
108 unsigned char *filename;
109 struct stream_priv_s* p = (struct stream_priv_s*)opts;
111 if(mode == STREAM_READ)
112 m = O_RDONLY;
113 else if(mode == STREAM_WRITE)
114 m = O_RDWR|O_CREAT|O_TRUNC;
115 else {
116 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] Unknown open mode %d\n",mode);
117 m_struct_free(&stream_opts,opts);
118 return STREAM_UNSUPPORTED;
121 if(p->filename)
122 filename = p->filename;
123 else if(p->filename2)
124 filename = p->filename2;
125 else
126 filename = NULL;
127 if(!filename) {
128 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] No filename\n");
129 m_struct_free(&stream_opts,opts);
130 return STREAM_ERROR;
133 #if HAVE_DOS_PATHS
134 // extract '/' from '/x:/path'
135 if( filename[ 0 ] == '/' && filename[ 1 ] && filename[ 2 ] == ':' )
136 filename++;
137 #endif
139 m |= O_BINARY;
141 if(!strcmp(filename,"-")){
142 if(mode == STREAM_READ) {
143 // read from stdin
144 mp_msg(MSGT_OPEN,MSGL_INFO,MSGTR_ReadSTDIN);
145 f=0; // 0=stdin
146 #if HAVE_SETMODE
147 setmode(fileno(stdin),O_BINARY);
148 #endif
149 } else {
150 mp_msg(MSGT_OPEN,MSGL_INFO,"Writing to stdout\n");
151 f=1;
152 #if HAVE_SETMODE
153 setmode(fileno(stdout),O_BINARY);
154 #endif
156 } else {
157 mode_t openmode = S_IRUSR|S_IWUSR;
158 #ifndef __MINGW32__
159 openmode |= S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
160 #endif
161 f=open(filename,m, openmode);
162 if(f<0) {
163 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_FileNotFound,filename);
164 m_struct_free(&stream_opts,opts);
165 return STREAM_ERROR;
169 len=lseek(f,0,SEEK_END); lseek(f,0,SEEK_SET);
170 #ifdef __MINGW32__
171 if(f==0 || len == -1) {
172 #else
173 if(len == -1) {
174 #endif
175 if(mode == STREAM_READ) stream->seek = seek_forward;
176 stream->type = STREAMTYPE_STREAM; // Must be move to STREAMTYPE_FILE
177 stream->flags |= MP_STREAM_SEEK_FW;
178 } else if(len >= 0) {
179 stream->seek = seek;
180 stream->end_pos = len;
181 stream->type = STREAMTYPE_FILE;
184 mp_msg(MSGT_OPEN,MSGL_V,"[file] File size is %"PRId64" bytes\n", (int64_t)len);
186 stream->fd = f;
187 stream->fill_buffer = fill_buffer;
188 stream->write_buffer = write_buffer;
189 stream->control = control;
191 m_struct_free(&stream_opts,opts);
192 return STREAM_OK;
195 const stream_info_t stream_info_file = {
196 "File",
197 "file",
198 "Albeu",
199 "based on the code from ??? (probably Arpi)",
200 open_f,
201 { "file", "", NULL },
202 &stream_opts,
203 1 // Urls are an option string