comparison libmpdemux/stream_file.c @ 9794:f67d87b2d3c7

Stream modularization, the first step. Only file stream are converted atm, other will come later.
author albeu
date Wed, 02 Apr 2003 16:25:07 +0000
parents
children d179dbc45935
comparison
equal deleted inserted replaced
9793:b2bf632d37b5 9794:f67d87b2d3c7
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
80 #if defined(__CYGWIN__)|| defined(__MINGW32__)
81 m |= O_BINARY;
82 #endif
83
84 if(!strcmp(p->filename,"-")){
85 if(mode == STREAM_READ) {
86 // read from stdin
87 mp_msg(MSGT_OPEN,MSGL_INFO,MSGTR_ReadSTDIN);
88 f=0; // 0=stdin
89 } else {
90 mp_msg(MSGT_OPEN,MSGL_INFO,"Writing to stdout\n");
91 f=1;
92 }
93 } else {
94 f=open(p->filename,m);
95 if(f<0) {
96 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_FileNotFound,p->filename);
97 m_struct_free(&stream_opts,opts);
98 return -1;
99 }
100 }
101
102 len=lseek(f,0,SEEK_END); lseek(f,0,SEEK_SET);
103 if(len == -1) {
104 stream->seek = seek_forward;
105 stream->type = STREAMTYPE_STREAM; // Must be move to STREAMTYPE_FILE
106 stream->flags |= STREAM_SEEK_FW;
107 } else if(len >= 0) {
108 stream->seek = seek;
109 stream->end_pos = len;
110 stream->type = STREAMTYPE_FILE;
111 }
112
113 #ifdef _LARGEFILE_SOURCE
114 mp_msg(MSGT_OPEN,MSGL_V,"[file] File size is %lld bytes\n", (long long)len);
115 #else
116 mp_msg(MSGT_OPEN,MSGL_V,"[file] File size is %u bytes\n", (unsigned int)len);
117 #endif
118
119 stream->fd = f;
120 stream->fill_buffer = fill_buffer;
121 stream->write_buffer = write_buffer;
122
123 m_struct_free(&stream_opts,opts);
124 return STREAM_OK;
125 }
126
127 stream_info_t stream_info_file = {
128 "File",
129 "file",
130 "Albeu",
131 "based on the code from ??? (probably Arpi)",
132 open_f,
133 { "file", "", NULL },
134 &stream_opts,
135 1 // Urls are an option string
136 };