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