ao_pulse: support native mute control
[mplayer.git] / stream / stream_file.c
blobe9bb3eb03a27bf06e8e9029d7d0c60f96f95d6b4
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 "osdep/io.h"
29 #include "mp_msg.h"
30 #include "stream.h"
31 #include "m_option.h"
32 #include "m_struct.h"
34 static struct stream_priv_s {
35 char* filename;
36 char *filename2;
37 } stream_priv_dflts = {
38 NULL, NULL
41 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
42 /// URL definition
43 static const m_option_t stream_opts_fields[] = {
44 {"string", ST_OFF(filename), CONF_TYPE_STRING, 0, 0 ,0, NULL},
45 {"filename", ST_OFF(filename2), CONF_TYPE_STRING, 0, 0 ,0, NULL},
46 { NULL, NULL, 0, 0, 0, 0, NULL }
48 static const struct m_struct_st stream_opts = {
49 "file",
50 sizeof(struct stream_priv_s),
51 &stream_priv_dflts,
52 stream_opts_fields
55 static int fill_buffer(stream_t *s, char* buffer, int max_len){
56 int r = read(s->fd,buffer,max_len);
57 return (r <= 0) ? -1 : r;
60 static int write_buffer(stream_t *s, char* buffer, int len) {
61 int r;
62 int wr = 0;
63 while (wr < len) {
64 r = write(s->fd,buffer,len);
65 if (r <= 0)
66 return -1;
67 wr += r;
68 buffer += r;
70 return len;
73 static int seek(stream_t *s,off_t newpos) {
74 s->pos = newpos;
75 if(lseek(s->fd,s->pos,SEEK_SET)<0) {
76 s->eof=1;
77 return 0;
79 return 1;
82 static int seek_forward(stream_t *s,off_t newpos) {
83 if(newpos<s->pos){
84 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
85 return 0;
87 while(s->pos<newpos){
88 int len=s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE);
89 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; break; } // EOF
90 s->buf_pos=0;
91 s->buf_len=len;
92 s->pos+=len;
94 return 1;
97 static int control(stream_t *s, int cmd, void *arg) {
98 switch(cmd) {
99 case STREAM_CTRL_GET_SIZE: {
100 off_t size;
102 size = lseek(s->fd, 0, SEEK_END);
103 lseek(s->fd, s->pos, SEEK_SET);
104 if(size != (off_t)-1) {
105 *((off_t*)arg) = size;
106 return 1;
110 return STREAM_UNSUPPORTED;
113 static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
114 int f;
115 mode_t m = 0;
116 off_t len;
117 unsigned char *filename;
118 struct stream_priv_s* p = (struct stream_priv_s*)opts;
120 if(mode == STREAM_READ)
121 m = O_RDONLY;
122 else if(mode == STREAM_WRITE)
123 m = O_RDWR|O_CREAT|O_TRUNC;
124 else {
125 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] Unknown open mode %d\n",mode);
126 m_struct_free(&stream_opts,opts);
127 return STREAM_UNSUPPORTED;
130 if(p->filename)
131 filename = p->filename;
132 else if(p->filename2)
133 filename = p->filename2;
134 else
135 filename = NULL;
136 if(!filename) {
137 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] No filename\n");
138 m_struct_free(&stream_opts,opts);
139 return STREAM_ERROR;
142 #if HAVE_DOS_PATHS
143 // extract '/' from '/x:/path'
144 if( filename[ 0 ] == '/' && filename[ 1 ] && filename[ 2 ] == ':' )
145 filename++;
146 #endif
148 m |= O_BINARY;
150 if(!strcmp(filename,"-")){
151 if(mode == STREAM_READ) {
152 // read from stdin
153 mp_tmsg(MSGT_OPEN,MSGL_INFO,"Reading from stdin...\n");
154 f=0; // 0=stdin
155 #if HAVE_SETMODE
156 setmode(fileno(stdin),O_BINARY);
157 #endif
158 } else {
159 mp_msg(MSGT_OPEN,MSGL_INFO,"Writing to stdout\n");
160 f=1;
161 #if HAVE_SETMODE
162 setmode(fileno(stdout),O_BINARY);
163 #endif
165 } else {
166 mode_t openmode = S_IRUSR|S_IWUSR;
167 #ifndef __MINGW32__
168 openmode |= S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
169 #endif
170 f=open(filename,m, openmode);
171 if(f<0) {
172 mp_tmsg(MSGT_OPEN,MSGL_ERR,"File not found: '%s'\n",filename);
173 m_struct_free(&stream_opts,opts);
174 return STREAM_ERROR;
178 len=lseek(f,0,SEEK_END); lseek(f,0,SEEK_SET);
179 #ifdef __MINGW32__
180 // seeks on stdin incorrectly succeed on MinGW
181 if(f==0)
182 len = -1;
183 #endif
184 if(len == -1) {
185 if(mode == STREAM_READ) stream->seek = seek_forward;
186 stream->type = STREAMTYPE_STREAM; // Must be move to STREAMTYPE_FILE
187 stream->flags |= MP_STREAM_SEEK_FW;
188 } else if(len >= 0) {
189 stream->seek = seek;
190 stream->end_pos = len;
191 stream->type = STREAMTYPE_FILE;
194 mp_msg(MSGT_OPEN,MSGL_V,"[file] File size is %"PRId64" bytes\n", (int64_t)len);
196 stream->fd = f;
197 stream->fill_buffer = fill_buffer;
198 stream->write_buffer = write_buffer;
199 stream->control = control;
200 stream->read_chunk = 64*1024;
202 m_struct_free(&stream_opts,opts);
203 return STREAM_OK;
206 const stream_info_t stream_info_file = {
207 "File",
208 "file",
209 "Albeu",
210 "based on the code from ??? (probably Arpi)",
211 open_f,
212 { "file", "", NULL },
213 &stream_opts,
214 1 // Urls are an option string