annotate libmpdemux/stream_file.c @ 15461:7c272bfba96f

fixed file:// syntax using newly introduced -string- urlpart
author nicodvb
date Sat, 14 May 2005 12:50:59 +0000
parents de27bb6ff3ec
children ec02252fbaa6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
1
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
2 #include "config.h"
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
3
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
4 #include <sys/types.h>
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
5 #include <sys/stat.h>
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
6 #include <fcntl.h>
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
7 #include <unistd.h>
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
8
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
9 #include "mp_msg.h"
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
10 #include "stream.h"
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
11 #include "help_mp.h"
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
12 #include "../m_option.h"
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
13 #include "../m_struct.h"
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
14
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
15 static struct stream_priv_s {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
16 char* filename;
15461
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
17 char *filename2;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
18 } stream_priv_dflts = {
15452
de27bb6ff3ec make file:// prefix work
nicodvb
parents: 12921
diff changeset
19 NULL, NULL
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
20 };
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
21
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
22 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
23 /// URL definition
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
24 static m_option_t stream_opts_fields[] = {
15461
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
25 {"string", ST_OFF(filename), CONF_TYPE_STRING, 0, 0 ,0, NULL},
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
26 {"filename", ST_OFF(filename2), CONF_TYPE_STRING, 0, 0 ,0, NULL},
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
27 { NULL, NULL, 0, 0, 0, 0, NULL }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
28 };
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
29 static struct m_struct_st stream_opts = {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
30 "file",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
31 sizeof(struct stream_priv_s),
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
32 &stream_priv_dflts,
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
33 stream_opts_fields
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
34 };
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
35
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
36 static int fill_buffer(stream_t *s, char* buffer, int max_len){
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
37 int r = read(s->fd,buffer,max_len);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
38 return (r <= 0) ? -1 : r;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
39 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
40
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
41 static int write_buffer(stream_t *s, char* buffer, int len) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
42 int r = write(s->fd,buffer,len);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
43 return (r <= 0) ? -1 : r;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
44 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
45
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
46 static int seek(stream_t *s,off_t newpos) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
47 s->pos = newpos;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
48 if(lseek(s->fd,s->pos,SEEK_SET)<0) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
49 s->eof=1;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
50 return 0;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
51 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
52 return 1;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
53 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
54
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
55 static int seek_forward(stream_t *s,off_t newpos) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
56 if(newpos<s->pos){
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
57 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
58 return 0;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
59 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
60 while(s->pos<newpos){
12018
2c2fb4457982 Seek in HTTP streams and stdin seek fixes
rtognimp
parents: 10608
diff changeset
61 int len=s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE);
2c2fb4457982 Seek in HTTP streams and stdin seek fixes
rtognimp
parents: 10608
diff changeset
62 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; break; } // EOF
2c2fb4457982 Seek in HTTP streams and stdin seek fixes
rtognimp
parents: 10608
diff changeset
63 s->buf_pos=0;
2c2fb4457982 Seek in HTTP streams and stdin seek fixes
rtognimp
parents: 10608
diff changeset
64 s->buf_len=len;
2c2fb4457982 Seek in HTTP streams and stdin seek fixes
rtognimp
parents: 10608
diff changeset
65 s->pos+=len;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
66 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
67 return 1;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
68 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
69
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
70 static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
71 int f;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
72 mode_t m = 0;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
73 off_t len;
15461
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
74 unsigned char *filename;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
75 struct stream_priv_s* p = (struct stream_priv_s*)opts;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
76
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
77 if(mode == STREAM_READ)
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
78 m = O_RDONLY;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
79 else if(mode == STREAM_WRITE)
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
80 m = O_WRONLY;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
81 else {
10608
2cae82f2ab02 Spelling police:
diego
parents: 9849
diff changeset
82 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] Unknown open mode %d\n",mode);
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
83 m_struct_free(&stream_opts,opts);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
84 return STREAM_UNSUPORTED;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
85 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
86
15461
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
87 if(p->filename)
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
88 filename = p->filename;
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
89 else if(p->filename2)
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
90 filename = p->filename2;
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
91 else
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
92 filename = NULL;
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
93 if(!filename) {
9849
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
94 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] No filename\n");
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
95 m_struct_free(&stream_opts,opts);
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
96 return STREAM_ERROR;
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
97 }
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
98
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
99 #if defined(__CYGWIN__)|| defined(__MINGW32__)
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
100 m |= O_BINARY;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
101 #endif
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
102
15461
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
103 if(!strcmp(filename,"-")){
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
104 if(mode == STREAM_READ) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
105 // read from stdin
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
106 mp_msg(MSGT_OPEN,MSGL_INFO,MSGTR_ReadSTDIN);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
107 f=0; // 0=stdin
12921
1e42749917c8 mingw stdin fixes
faust3
parents: 12018
diff changeset
108 #ifdef __MINGW32__
1e42749917c8 mingw stdin fixes
faust3
parents: 12018
diff changeset
109 setmode(fileno(stdin),O_BINARY);
1e42749917c8 mingw stdin fixes
faust3
parents: 12018
diff changeset
110 #endif
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
111 } else {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
112 mp_msg(MSGT_OPEN,MSGL_INFO,"Writing to stdout\n");
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
113 f=1;
12921
1e42749917c8 mingw stdin fixes
faust3
parents: 12018
diff changeset
114 #ifdef __MINGW32__
1e42749917c8 mingw stdin fixes
faust3
parents: 12018
diff changeset
115 setmode(fileno(stdout),O_BINARY);
1e42749917c8 mingw stdin fixes
faust3
parents: 12018
diff changeset
116 #endif
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
117 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
118 } else {
15461
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
119 f=open(filename,m);
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
120 if(f<0) {
15461
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
121 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_FileNotFound,filename);
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
122 m_struct_free(&stream_opts,opts);
9827
d179dbc45935 Little fix.
albeu
parents: 9794
diff changeset
123 return STREAM_ERROR;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
124 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
125 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
126
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
127 len=lseek(f,0,SEEK_END); lseek(f,0,SEEK_SET);
12921
1e42749917c8 mingw stdin fixes
faust3
parents: 12018
diff changeset
128 #ifdef __MINGW32__
1e42749917c8 mingw stdin fixes
faust3
parents: 12018
diff changeset
129 if(f==0 || len == -1) {
1e42749917c8 mingw stdin fixes
faust3
parents: 12018
diff changeset
130 #else
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
131 if(len == -1) {
12921
1e42749917c8 mingw stdin fixes
faust3
parents: 12018
diff changeset
132 #endif
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
133 stream->seek = seek_forward;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
134 stream->type = STREAMTYPE_STREAM; // Must be move to STREAMTYPE_FILE
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
135 stream->flags |= STREAM_SEEK_FW;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
136 } else if(len >= 0) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
137 stream->seek = seek;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
138 stream->end_pos = len;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
139 stream->type = STREAMTYPE_FILE;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
140 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
141
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
142 #ifdef _LARGEFILE_SOURCE
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
143 mp_msg(MSGT_OPEN,MSGL_V,"[file] File size is %lld bytes\n", (long long)len);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
144 #else
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
145 mp_msg(MSGT_OPEN,MSGL_V,"[file] File size is %u bytes\n", (unsigned int)len);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
146 #endif
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
147
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
148 stream->fd = f;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
149 stream->fill_buffer = fill_buffer;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
150 stream->write_buffer = write_buffer;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
151
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
152 m_struct_free(&stream_opts,opts);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
153 return STREAM_OK;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
154 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
155
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
156 stream_info_t stream_info_file = {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
157 "File",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
158 "file",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
159 "Albeu",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
160 "based on the code from ??? (probably Arpi)",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
161 open_f,
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
162 { "file", "", NULL },
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
163 &stream_opts,
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
164 1 // Urls are an option string
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
165 };