annotate libmpdemux/stream_file.c @ 10493:40df234f2d30

translation by Juan Martin <nauj27@teleline.es> and Andoni Zubimendi
author diego
date Mon, 28 Jul 2003 02:24:05 +0000
parents 5b649442fe72
children 2cae82f2ab02
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;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
17 } stream_priv_dflts = {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
18 NULL
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
19 };
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
20
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
21 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
22 /// URL definition
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
23 static m_option_t stream_opts_fields[] = {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
24 {"filename", ST_OFF(filename), CONF_TYPE_STRING, 0, 0 ,0, NULL},
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
25 { NULL, NULL, 0, 0, 0, 0, NULL }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
26 };
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
27 static struct m_struct_st stream_opts = {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
28 "file",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
29 sizeof(struct stream_priv_s),
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
30 &stream_priv_dflts,
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
31 stream_opts_fields
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
32 };
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
33
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
34 static int fill_buffer(stream_t *s, char* buffer, int max_len){
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
35 int r = read(s->fd,buffer,max_len);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
36 return (r <= 0) ? -1 : r;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
37 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
38
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
39 static int write_buffer(stream_t *s, char* buffer, int len) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
40 int r = write(s->fd,buffer,len);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
41 return (r <= 0) ? -1 : r;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
42 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
43
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
44 static int seek(stream_t *s,off_t newpos) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
45 s->pos = newpos;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
46 if(lseek(s->fd,s->pos,SEEK_SET)<0) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
47 s->eof=1;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
48 return 0;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
49 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
50 return 1;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
51 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
52
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
53 static int seek_forward(stream_t *s,off_t newpos) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
54 if(newpos<s->pos){
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
55 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
56 return 0;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
57 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
58 while(s->pos<newpos){
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
59 if(s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE)<=0) break; // EOF
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
60 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
61 return 1;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
62 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
63
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
64 static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
65 int f;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
66 mode_t m = 0;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
67 off_t len;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
68 struct stream_priv_s* p = (struct stream_priv_s*)opts;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
69
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
70 if(mode == STREAM_READ)
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
71 m = O_RDONLY;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
72 else if(mode == STREAM_WRITE)
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
73 m = O_WRONLY;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
74 else {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
75 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] Unknow open mode %d\n",mode);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
76 m_struct_free(&stream_opts,opts);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
77 return STREAM_UNSUPORTED;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
78 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
79
9849
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
80 if(!p->filename) {
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
81 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] No filename\n");
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
82 m_struct_free(&stream_opts,opts);
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
83 return STREAM_ERROR;
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
84 }
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
85
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
86 #if defined(__CYGWIN__)|| defined(__MINGW32__)
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
87 m |= O_BINARY;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
88 #endif
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
89
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
90 if(!strcmp(p->filename,"-")){
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
91 if(mode == STREAM_READ) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
92 // read from stdin
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
93 mp_msg(MSGT_OPEN,MSGL_INFO,MSGTR_ReadSTDIN);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
94 f=0; // 0=stdin
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
95 } else {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
96 mp_msg(MSGT_OPEN,MSGL_INFO,"Writing to stdout\n");
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
97 f=1;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
98 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
99 } else {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
100 f=open(p->filename,m);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
101 if(f<0) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
102 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_FileNotFound,p->filename);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
103 m_struct_free(&stream_opts,opts);
9827
d179dbc45935 Little fix.
albeu
parents: 9794
diff changeset
104 return STREAM_ERROR;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
105 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
106 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
107
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
108 len=lseek(f,0,SEEK_END); lseek(f,0,SEEK_SET);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
109 if(len == -1) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
110 stream->seek = seek_forward;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
111 stream->type = STREAMTYPE_STREAM; // Must be move to STREAMTYPE_FILE
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
112 stream->flags |= STREAM_SEEK_FW;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
113 } else if(len >= 0) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
114 stream->seek = seek;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
115 stream->end_pos = len;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
116 stream->type = STREAMTYPE_FILE;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
117 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
118
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
119 #ifdef _LARGEFILE_SOURCE
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
120 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
121 #else
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
122 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
123 #endif
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
124
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
125 stream->fd = f;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
126 stream->fill_buffer = fill_buffer;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
127 stream->write_buffer = write_buffer;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
128
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
129 m_struct_free(&stream_opts,opts);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
130 return STREAM_OK;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
131 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
132
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
133 stream_info_t stream_info_file = {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
134 "File",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
135 "file",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
136 "Albeu",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
137 "based on the code from ??? (probably Arpi)",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
138 open_f,
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
139 { "file", "", NULL },
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
140 &stream_opts,
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
141 1 // Urls are an option string
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
142 };