UltraSPARC T1 (Niagara) support, patch by Derek E. Lewis /dlewis (gobble) solnetworks...
[mplayer/glamo.git] / libaf / af_export.c
blobc2eff534768feddc1abff4c25c41240f30e39aef
1 /* This audio filter exports the incomming signal to other processes
2 using memory mapping. Memory mapped area contains a header:
3 int nch,
4 int size,
5 unsigned long long counter (updated every time the contents of
6 the area changes),
7 the rest is payload (non-interleaved).
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <inttypes.h>
14 #include <unistd.h>
15 #include "config.h"
17 #ifdef HAVE_SYS_MMAN_H
18 #include <sys/types.h>
19 #include <sys/mman.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
24 #include "af.h"
26 extern char * get_path( char * filename );
29 #define DEF_SZ 512 // default buffer size (in samples)
30 #define SHARED_FILE "mplayer-af_export" /* default file name
31 (relative to ~/.mplayer/ */
33 #define SIZE_HEADER (2 * sizeof(int) + sizeof(unsigned long long))
35 // Data for specific instances of this filter
36 typedef struct af_export_s
38 unsigned long long count; // Used for sync
39 void* buf[AF_NCH]; // Buffers for storing the data before it is exported
40 int sz; // Size of buffer in samples
41 int wi; // Write index
42 int fd; // File descriptor to shared memory area
43 char* filename; // File to export data
44 void* mmap_area; // MMap shared area
45 } af_export_t;
48 /* Initialization and runtime control
49 af audio filter instance
50 cmd control command
51 arg argument
53 static int control(struct af_instance_s* af, int cmd, void* arg)
55 af_export_t* s = af->setup;
56 switch (cmd){
57 case AF_CONTROL_REINIT:{
58 int i=0;
59 int mapsize;
61 // Free previous buffers
62 if (s->buf && s->buf[0])
63 free(s->buf[0]);
65 // unmap previous area
66 if(s->mmap_area)
67 munmap(s->mmap_area, SIZE_HEADER + (af->data->bps*s->sz*af->data->nch));
68 // close previous file descriptor
69 if(s->fd)
70 close(s->fd);
72 // Accept only int16_t as input format (which sucks)
73 af->data->rate = ((af_data_t*)arg)->rate;
74 af->data->nch = ((af_data_t*)arg)->nch;
75 af->data->format = AF_FORMAT_S16_NE;
76 af->data->bps = 2;
78 // If buffer length isn't set, set it to the default value
79 if(s->sz == 0)
80 s->sz = DEF_SZ;
82 // Allocate new buffers (as one continuous block)
83 s->buf[0] = calloc(s->sz*af->data->nch, af->data->bps);
84 if(NULL == s->buf[0])
85 af_msg(AF_MSG_FATAL, "[export] Out of memory\n");
86 for(i = 1; i < af->data->nch; i++)
87 s->buf[i] = s->buf[0] + i*s->sz*af->data->bps;
89 // Init memory mapping
90 s->fd = open(s->filename, O_RDWR | O_CREAT | O_TRUNC, 0640);
91 af_msg(AF_MSG_INFO, "[export] Exporting to file: %s\n", s->filename);
92 if(s->fd < 0)
93 af_msg(AF_MSG_FATAL, "[export] Could not open/create file: %s\n",
94 s->filename);
96 // header + buffer
97 mapsize = (SIZE_HEADER + (af->data->bps * s->sz * af->data->nch));
99 // grow file to needed size
100 for(i = 0; i < mapsize; i++){
101 char null = 0;
102 write(s->fd, (void*) &null, 1);
105 // mmap size
106 s->mmap_area = mmap(0, mapsize, PROT_READ|PROT_WRITE,MAP_SHARED, s->fd, 0);
107 if(s->mmap_area == NULL)
108 af_msg(AF_MSG_FATAL, "[export] Could not mmap file %s\n", s->filename);
109 af_msg(AF_MSG_INFO, "[export] Memory mapped to file: %s (%p)\n",
110 s->filename, s->mmap_area);
112 // Initialize header
113 *((int*)s->mmap_area) = af->data->nch;
114 *((int*)s->mmap_area + 1) = s->sz * af->data->bps * af->data->nch;
115 msync(s->mmap_area, mapsize, MS_ASYNC);
117 // Use test_output to return FALSE if necessary
118 return af_test_output(af, (af_data_t*)arg);
120 case AF_CONTROL_COMMAND_LINE:{
121 int i=0;
122 char *str = arg;
124 if (!str){
125 if(s->filename)
126 free(s->filename);
128 s->filename = get_path(SHARED_FILE);
129 return AF_OK;
132 while((str[i]) && (str[i] != ':'))
133 i++;
135 if(s->filename)
136 free(s->filename);
138 s->filename = calloc(i + 1, sizeof(char));
139 memcpy(s->filename, str, i);
140 s->filename[i] = 0;
142 sscanf(str + i + 1, "%d", &(s->sz));
144 return af->control(af, AF_CONTROL_EXPORT_SZ | AF_CONTROL_SET, &s->sz);
146 case AF_CONTROL_EXPORT_SZ | AF_CONTROL_SET:
147 s->sz = * (int *) arg;
148 if((s->sz <= 0) || (s->sz > 2048))
149 af_msg( AF_MSG_ERROR, "[export] Buffer size must be between"
150 " 1 and 2048\n" );
152 return AF_OK;
153 case AF_CONTROL_EXPORT_SZ | AF_CONTROL_GET:
154 *(int*) arg = s->sz;
155 return AF_OK;
158 return AF_UNKNOWN;
161 /* Free allocated memory and clean up other stuff too.
162 af audio filter instance
164 static void uninit( struct af_instance_s* af )
166 if (af->data){
167 free(af->data);
168 af->data = NULL;
171 if(af->setup){
172 af_export_t* s = af->setup;
173 if (s->buf && s->buf[0])
174 free(s->buf[0]);
176 // Free mmaped area
177 if(s->mmap_area)
178 munmap(s->mmap_area, sizeof(af_export_t));
180 if(s->fd > -1)
181 close(s->fd);
183 if(s->filename)
184 free(s->filename);
186 free(af->setup);
187 af->setup = NULL;
191 /* Filter data through filter
192 af audio filter instance
193 data audio data
195 static af_data_t* play( struct af_instance_s* af, af_data_t* data )
197 af_data_t* c = data; // Current working data
198 af_export_t* s = af->setup; // Setup for this instance
199 int16_t* a = c->audio; // Incomming sound
200 int nch = c->nch; // Number of channels
201 int len = c->len/c->bps; // Number of sample in data chunk
202 int sz = s->sz; // buffer size (in samples)
203 int flag = 0; // Set to 1 if buffer is filled
205 int ch, i;
207 // Fill all buffers
208 for(ch = 0; ch < nch; ch++){
209 int wi = s->wi; // Reset write index
210 int16_t* b = s->buf[ch]; // Current buffer
212 // Copy data to export buffers
213 for(i = ch; i < len; i += nch){
214 b[wi++] = a[i];
215 if(wi >= sz){ // Don't write outside the end of the buffer
216 flag = 1;
217 break;
220 s->wi = wi % s->sz;
223 // Export buffer to mmaped area
224 if(flag){
225 // update buffer in mapped area
226 memcpy(s->mmap_area + SIZE_HEADER, s->buf[0], sz * c->bps * nch);
227 s->count++; // increment counter (to sync)
228 memcpy(s->mmap_area + SIZE_HEADER - sizeof(s->count),
229 &(s->count), sizeof(s->count));
232 // We don't modify data, just export it
233 return data;
236 /* Allocate memory and set function pointers
237 af audio filter instance
238 returns AF_OK or AF_ERROR
240 static int af_open( af_instance_t* af )
242 af->control = control;
243 af->uninit = uninit;
244 af->play = play;
245 af->mul.n = 1;
246 af->mul.d = 1;
247 af->data = calloc(1, sizeof(af_data_t));
248 af->setup = calloc(1, sizeof(af_export_t));
249 if((af->data == NULL) || (af->setup == NULL))
250 return AF_ERROR;
252 ((af_export_t *)af->setup)->filename = get_path(SHARED_FILE);
254 return AF_OK;
257 // Description of this filter
258 af_info_t af_info_export = {
259 "Sound export filter",
260 "export",
261 "Anders; Gustavo Sverzut Barbieri <gustavo.barbieri@ic.unicamp.br>",
263 AF_FLAGS_REENTRANT,
264 af_open
267 #endif /*HAVE_SYS_MMAN_H*/