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){
|
|
59 if(s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE)<=0) break; // EOF
|
|
60 }
|
|
61 return 1;
|
|
62 }
|
|
63
|
|
64 static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
|
|
65 int f;
|
|
66 mode_t m = 0;
|
|
67 off_t len;
|
|
68 struct stream_priv_s* p = (struct stream_priv_s*)opts;
|
|
69
|
|
70 if(mode == STREAM_READ)
|
|
71 m = O_RDONLY;
|
|
72 else if(mode == STREAM_WRITE)
|
|
73 m = O_WRONLY;
|
|
74 else {
|
|
75 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] Unknow open mode %d\n",mode);
|
|
76 m_struct_free(&stream_opts,opts);
|
|
77 return STREAM_UNSUPORTED;
|
|
78 }
|
|
79
|
9849
|
80 if(!p->filename) {
|
|
81 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] No filename\n");
|
|
82 m_struct_free(&stream_opts,opts);
|
|
83 return STREAM_ERROR;
|
|
84 }
|
|
85
|
9794
|
86 #if defined(__CYGWIN__)|| defined(__MINGW32__)
|
|
87 m |= O_BINARY;
|
|
88 #endif
|
|
89
|
|
90 if(!strcmp(p->filename,"-")){
|
|
91 if(mode == STREAM_READ) {
|
|
92 // read from stdin
|
|
93 mp_msg(MSGT_OPEN,MSGL_INFO,MSGTR_ReadSTDIN);
|
|
94 f=0; // 0=stdin
|
|
95 } else {
|
|
96 mp_msg(MSGT_OPEN,MSGL_INFO,"Writing to stdout\n");
|
|
97 f=1;
|
|
98 }
|
|
99 } else {
|
|
100 f=open(p->filename,m);
|
|
101 if(f<0) {
|
|
102 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_FileNotFound,p->filename);
|
|
103 m_struct_free(&stream_opts,opts);
|
9827
|
104 return STREAM_ERROR;
|
9794
|
105 }
|
|
106 }
|
|
107
|
|
108 len=lseek(f,0,SEEK_END); lseek(f,0,SEEK_SET);
|
|
109 if(len == -1) {
|
|
110 stream->seek = seek_forward;
|
|
111 stream->type = STREAMTYPE_STREAM; // Must be move to STREAMTYPE_FILE
|
|
112 stream->flags |= STREAM_SEEK_FW;
|
|
113 } else if(len >= 0) {
|
|
114 stream->seek = seek;
|
|
115 stream->end_pos = len;
|
|
116 stream->type = STREAMTYPE_FILE;
|
|
117 }
|
|
118
|
|
119 #ifdef _LARGEFILE_SOURCE
|
|
120 mp_msg(MSGT_OPEN,MSGL_V,"[file] File size is %lld bytes\n", (long long)len);
|
|
121 #else
|
|
122 mp_msg(MSGT_OPEN,MSGL_V,"[file] File size is %u bytes\n", (unsigned int)len);
|
|
123 #endif
|
|
124
|
|
125 stream->fd = f;
|
|
126 stream->fill_buffer = fill_buffer;
|
|
127 stream->write_buffer = write_buffer;
|
|
128
|
|
129 m_struct_free(&stream_opts,opts);
|
|
130 return STREAM_OK;
|
|
131 }
|
|
132
|
|
133 stream_info_t stream_info_file = {
|
|
134 "File",
|
|
135 "file",
|
|
136 "Albeu",
|
|
137 "based on the code from ??? (probably Arpi)",
|
|
138 open_f,
|
|
139 { "file", "", NULL },
|
|
140 &stream_opts,
|
|
141 1 // Urls are an option string
|
|
142 };
|