12024
|
1 /*
|
|
2 The mediastreamer library aims at providing modular media processing and I/O
|
|
3 for linphone, but also for any telephony application.
|
|
4 Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org
|
|
5
|
|
6 This library is free software; you can redistribute it and/or
|
|
7 modify it under the terms of the GNU Lesser General Public
|
|
8 License as published by the Free Software Foundation; either
|
|
9 version 2.1 of the License, or (at your option) any later version.
|
|
10
|
|
11 This library is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 Lesser General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU Lesser General Public
|
|
17 License along with this library; if not, write to the Free Software
|
|
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 */
|
|
20
|
|
21 #include "msread.h"
|
|
22 #include "mssync.h"
|
|
23 #include <unistd.h>
|
|
24 #include <fcntl.h>
|
|
25 #include <sys/types.h>
|
|
26 #include <string.h>
|
|
27 #include <errno.h>
|
|
28
|
|
29 static MSReadClass *ms_read_class=NULL;
|
|
30
|
|
31 MSFilter * ms_read_new(char *name)
|
|
32 {
|
|
33 MSRead *r;
|
|
34 int fd=-1;
|
|
35
|
|
36 r=g_new(MSRead,1);
|
|
37 ms_read_init(r);
|
|
38 if (ms_read_class==NULL)
|
|
39 {
|
|
40 ms_read_class=g_new(MSReadClass,1);
|
|
41 ms_read_class_init(ms_read_class);
|
|
42 }
|
|
43 MS_FILTER(r)->klass=MS_FILTER_CLASS(ms_read_class);
|
|
44 r->fd=-1;
|
|
45 if (name!=NULL) ms_read_open(r,name);
|
|
46 return(MS_FILTER(r));
|
|
47 }
|
|
48
|
|
49
|
|
50
|
|
51 gint ms_read_open(MSRead *r, gchar *name)
|
|
52 {
|
|
53 gint fd;
|
|
54 fd=open(name,O_RDONLY);
|
|
55 if (fd<0) {
|
|
56 r->fd=-1;
|
|
57 g_warning("ms_read_new: cannot open %s : %s",name,strerror(errno));
|
|
58 return -1;
|
|
59 }
|
|
60 r->fd=fd;
|
|
61 if (strstr(name,".wav")!=NULL){
|
|
62 /* skip the header */
|
|
63 lseek(fd,20,SEEK_SET);
|
|
64 #ifdef WORDS_BIGENDIAN
|
|
65 r->need_swap=1;
|
|
66 #else
|
|
67 r->need_swap=0;
|
|
68 #endif
|
|
69 }
|
|
70 r->state=MS_READ_STATE_STARTED;
|
|
71 return 0;
|
|
72 }
|
|
73
|
|
74 /* FOR INTERNAL USE*/
|
|
75 void ms_read_init(MSRead *r)
|
|
76 {
|
|
77 ms_filter_init(MS_FILTER(r));
|
|
78 MS_FILTER(r)->outfifos=r->foutputs;
|
|
79 MS_FILTER(r)->outqueues=r->qoutputs;
|
|
80 memset(r->foutputs,0,sizeof(MSFifo*)*MSREAD_MAX_OUTPUTS);
|
|
81 memset(r->qoutputs,0,sizeof(MSQueue*)*MSREAD_MAX_OUTPUTS);
|
|
82 r->fd=-1;
|
|
83 r->gran=320;
|
|
84 r->state=MS_READ_STATE_STOPPED;
|
|
85 r->need_swap=0;
|
|
86 r->rate=8000;
|
|
87 }
|
|
88
|
|
89 gint ms_read_set_property(MSRead *f,MSFilterProperty prop, void *value)
|
|
90 {
|
|
91 switch(prop){
|
|
92 case MS_FILTER_PROPERTY_FREQ:
|
|
93 f->rate=((gint*)value)[0];
|
|
94 break;
|
|
95 }
|
|
96 return 0;
|
|
97 }
|
|
98
|
|
99 void ms_read_class_init(MSReadClass *klass)
|
|
100 {
|
|
101 ms_filter_class_init(MS_FILTER_CLASS(klass));
|
|
102 ms_filter_class_set_name(MS_FILTER_CLASS(klass),"dskreader");
|
|
103 ms_filter_class_set_attr(MS_FILTER_CLASS(klass),FILTER_IS_SOURCE);
|
|
104 MS_FILTER_CLASS(klass)->max_foutputs=MSREAD_MAX_OUTPUTS;
|
|
105 MS_FILTER_CLASS(klass)->max_qoutputs=MSREAD_MAX_OUTPUTS;
|
|
106 MS_FILTER_CLASS(klass)->w_maxgran=MSREAD_DEF_GRAN;
|
|
107 MS_FILTER_CLASS(klass)->destroy=(MSFilterDestroyFunc)ms_read_destroy;
|
|
108 MS_FILTER_CLASS(klass)->setup=(MSFilterSetupFunc)ms_read_setup;
|
|
109 MS_FILTER_CLASS(klass)->process=(MSFilterProcessFunc)ms_read_process;
|
|
110 MS_FILTER_CLASS(klass)->set_property=(MSFilterPropertyFunc)ms_read_set_property;
|
|
111 }
|
|
112
|
|
113 void ms_read_process(MSRead *r)
|
|
114 {
|
|
115 MSFifo *f;
|
|
116 MSQueue *q;
|
|
117 MSMessage *msg=NULL;
|
|
118 int err;
|
|
119 gint gran=r->gran;
|
|
120 void *p;
|
|
121
|
|
122 f=r->foutputs[0];
|
|
123 if ((f!=NULL) && (r->state==MS_READ_STATE_STARTED))
|
|
124 {
|
|
125 ms_fifo_get_write_ptr(f,gran,&p);
|
|
126 if (p!=NULL)
|
|
127 {
|
|
128 err=read(r->fd,p,gran);
|
|
129 if (err<0)
|
|
130 {
|
|
131 /* temp: */
|
|
132 g_warning("ms_read_process: failed to read: %s.\n",strerror(errno));
|
|
133 }
|
|
134 else if (err<gran){
|
|
135 ms_trace("ms_read_process: end of file.");
|
|
136 ms_filter_notify_event(MS_FILTER(r),MS_READ_EVENT_EOF,NULL);
|
|
137 r->state=MS_READ_STATE_STOPPED;
|
|
138 close(r->fd);
|
|
139 r->fd=-1;
|
|
140 }
|
|
141 if (r->need_swap) swap_buffer(p,gran);
|
|
142 }
|
|
143 }
|
|
144 /* process output queues*/
|
|
145 q=r->qoutputs[0];
|
|
146 if ((q!=NULL) && (r->fd>0))
|
|
147 {
|
|
148 msg=ms_message_new(r->gran);
|
|
149 err=read(r->fd,msg->data,r->gran);
|
|
150 if (err>0){
|
|
151 msg->size=err;
|
|
152 ms_queue_put(q,msg);
|
|
153 if (r->need_swap) swap_buffer(msg->data,r->gran);
|
|
154 }else{
|
|
155 ms_filter_notify_event(MS_FILTER(r),MS_READ_EVENT_EOF,NULL);
|
|
156 ms_trace("End of file reached.");
|
|
157 r->state=MS_READ_STATE_STOPPED;
|
|
158 }
|
|
159 }
|
|
160 }
|
|
161
|
|
162 void ms_read_destroy( MSRead *obj)
|
|
163 {
|
|
164 if (obj->fd!=0) close(obj->fd);
|
|
165 g_free(obj);
|
|
166 }
|
|
167
|
|
168 gint ms_read_close(MSRead *obj)
|
|
169 {
|
|
170 if (obj->fd!=0) {
|
|
171 close(obj->fd);
|
|
172 obj->fd=-1;
|
|
173 obj->state=MS_READ_STATE_STOPPED;
|
|
174 }
|
|
175 }
|
|
176
|
|
177
|
|
178 void ms_read_setup(MSRead *r, MSSync *sync)
|
|
179 {
|
|
180 r->sync=sync;
|
|
181 r->gran=(r->rate*sync->interval/1000)*2;
|
|
182 }
|