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.
22 #include <sys/types.h>
33 static struct stream_priv_s
{
36 } stream_priv_dflts
= {
40 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
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
= {
49 sizeof(struct stream_priv_s
),
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
) {
66 if(lseek(s
->fd
,s
->pos
,SEEK_SET
)<0) {
73 static int seek_forward(stream_t
*s
,off_t newpos
) {
75 mp_msg(MSGT_STREAM
,MSGL_INFO
,"Cannot seek backward in linear streams!\n");
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
88 static int control(stream_t
*s
, int cmd
, void *arg
) {
90 case STREAM_CTRL_GET_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
;
101 return STREAM_UNSUPPORTED
;
104 static int open_f(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
108 unsigned char *filename
;
109 struct stream_priv_s
* p
= (struct stream_priv_s
*)opts
;
111 if(mode
== STREAM_READ
)
113 else if(mode
== STREAM_WRITE
)
114 m
= O_RDWR
|O_CREAT
|O_TRUNC
;
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
;
122 filename
= p
->filename
;
123 else if(p
->filename2
)
124 filename
= p
->filename2
;
128 mp_msg(MSGT_OPEN
,MSGL_ERR
, "[file] No filename\n");
129 m_struct_free(&stream_opts
,opts
);
133 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
134 // extract '/' from '/x:/path'
135 if( filename
[ 0 ] == '/' && filename
[ 1 ] && filename
[ 2 ] == ':' )
139 #if defined(__CYGWIN__)|| defined(__MINGW32__) || defined(__OS2__)
143 if(!strcmp(filename
,"-")){
144 if(mode
== STREAM_READ
) {
146 mp_tmsg(MSGT_OPEN
,MSGL_INFO
,"Reading from stdin...\n");
148 #if defined(__MINGW32__) || defined(__OS2__)
149 setmode(fileno(stdin
),O_BINARY
);
152 mp_msg(MSGT_OPEN
,MSGL_INFO
,"Writing to stdout\n");
154 #if defined(__MINGW32__) || defined(__OS2__)
155 setmode(fileno(stdout
),O_BINARY
);
159 mode_t openmode
= S_IRUSR
|S_IWUSR
;
161 openmode
|= S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
;
163 f
=open(filename
,m
, openmode
);
165 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"File not found: '%s'\n",filename
);
166 m_struct_free(&stream_opts
,opts
);
171 len
=lseek(f
,0,SEEK_END
); lseek(f
,0,SEEK_SET
);
173 if(f
==0 || len
== -1) {
177 if(mode
== STREAM_READ
) stream
->seek
= seek_forward
;
178 stream
->type
= STREAMTYPE_STREAM
; // Must be move to STREAMTYPE_FILE
179 stream
->flags
|= MP_STREAM_SEEK_FW
;
180 } else if(len
>= 0) {
182 stream
->end_pos
= len
;
183 stream
->type
= STREAMTYPE_FILE
;
186 mp_msg(MSGT_OPEN
,MSGL_V
,"[file] File size is %"PRId64
" bytes\n", (int64_t)len
);
189 stream
->fill_buffer
= fill_buffer
;
190 stream
->write_buffer
= write_buffer
;
191 stream
->control
= control
;
193 m_struct_free(&stream_opts
,opts
);
197 const stream_info_t stream_info_file
= {
201 "based on the code from ??? (probably Arpi)",
203 { "file", "", NULL
},
205 1 // Urls are an option string