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 "mswrite.h"
|
|
22 #include <unistd.h>
|
|
23 #include <fcntl.h>
|
|
24 #include <sys/types.h>
|
|
25 #include <sys/stat.h>
|
|
26 #include <string.h>
|
|
27 #include <errno.h>
|
|
28
|
|
29 static MSWriteClass *ms_write_class=NULL;
|
|
30
|
|
31 MSFilter * ms_write_new(char *name)
|
|
32 {
|
|
33 MSWrite *r;
|
|
34 int fd=-1;
|
|
35
|
|
36 r=g_new(MSWrite,1);
|
|
37 ms_write_init(r);
|
|
38 if (ms_write_class==NULL)
|
|
39 {
|
|
40 ms_write_class=g_new(MSWriteClass,1);
|
|
41 ms_write_class_init(ms_write_class);
|
|
42 }
|
|
43 MS_FILTER(r)->klass=MS_FILTER_CLASS(ms_write_class);
|
|
44 if ((name!=NULL) && (strlen(name)!=0))
|
|
45 {
|
|
46 fd=open(name,O_WRONLY | O_CREAT | O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
|
|
47 if (fd<0) g_error("ms_write_new: failed to open %s.\n",name);
|
|
48 }
|
|
49 r->fd=fd;
|
|
50 return(MS_FILTER(r));
|
|
51 }
|
|
52
|
|
53
|
|
54 /* FOR INTERNAL USE*/
|
|
55 void ms_write_init(MSWrite *r)
|
|
56 {
|
|
57 ms_filter_init(MS_FILTER(r));
|
|
58 MS_FILTER(r)->infifos=r->f_inputs;
|
|
59 MS_FILTER(r)->inqueues=r->q_inputs;
|
|
60 MS_FILTER(r)->r_mingran=MSWRITE_MIN_GRAN;
|
|
61 memset(r->f_inputs,0,sizeof(MSFifo*)*MSWRITE_MAX_INPUTS);
|
|
62 memset(r->q_inputs,0,sizeof(MSQueue*)*MSWRITE_MAX_INPUTS);
|
|
63 r->fd=-1;
|
|
64 }
|
|
65
|
|
66 void ms_write_class_init(MSWriteClass *klass)
|
|
67 {
|
|
68 ms_filter_class_init(MS_FILTER_CLASS(klass));
|
|
69 ms_filter_class_set_name(MS_FILTER_CLASS(klass),"dskwriter");
|
|
70 MS_FILTER_CLASS(klass)->max_finputs=MSWRITE_MAX_INPUTS;
|
|
71 MS_FILTER_CLASS(klass)->max_qinputs=MSWRITE_MAX_INPUTS;
|
|
72 MS_FILTER_CLASS(klass)->r_maxgran=MSWRITE_DEF_GRAN;
|
|
73 MS_FILTER_CLASS(klass)->destroy=(MSFilterDestroyFunc)ms_write_destroy;
|
|
74 MS_FILTER_CLASS(klass)->process=(MSFilterProcessFunc)ms_write_process;
|
|
75 }
|
|
76
|
|
77 void ms_write_process(MSWrite *r)
|
|
78 {
|
|
79 MSFifo *f;
|
|
80 MSQueue *q;
|
|
81 MSMessage *buf=NULL;
|
|
82 int i,j,err1,err2;
|
|
83 gint gran=ms_filter_get_mingran(MS_FILTER(r));
|
|
84 void *p;
|
|
85
|
|
86 /* process output fifos*/
|
|
87 for (i=0,j=0;(i<MS_FILTER(r)->klass->max_finputs)&&(j<MS_FILTER(r)->finputs);i++)
|
|
88 {
|
|
89 f=r->f_inputs[i];
|
|
90 if (f!=NULL)
|
|
91 {
|
|
92 if ( (err1=ms_fifo_get_read_ptr(f,gran,&p))>0 )
|
|
93 {
|
|
94
|
|
95 err2=write(r->fd,p,gran);
|
|
96 if (err2<0) g_warning("ms_write_process: failed to write: %s.\n",strerror(errno));
|
|
97 }
|
|
98 j++;
|
|
99 }
|
|
100 }
|
|
101 /* process output queues*/
|
|
102 for (i=0,j=0;(i<MS_FILTER(r)->klass->max_qinputs)&&(j<MS_FILTER(r)->qinputs);i++)
|
|
103 {
|
|
104 q=r->q_inputs[i];
|
|
105 if (q!=NULL)
|
|
106 {
|
|
107 while ( (buf=ms_queue_get(q))!=NULL ){
|
|
108 write(r->fd,buf->data,buf->size);
|
|
109 j++;
|
|
110 ms_message_destroy(buf);
|
|
111 }
|
|
112 }
|
|
113 }
|
|
114 }
|
|
115
|
|
116 void ms_write_destroy( MSWrite *obj)
|
|
117 {
|
|
118 if (obj->fd!=0) close(obj->fd);
|
|
119 g_free(obj);
|
|
120 }
|
|
121
|