add blox binary codec to codecs.conf
[mplayer/glamo.git] / libaf / af_export.c
blob949f3f18caf20f1e61864528d49380e65da0998f
1 /*
2 * This audio filter exports the incoming signal to other processes
3 * using memory mapping. The memory mapped area contains a header:
4 * int nch,
5 * int size,
6 * unsigned long long counter (updated every time the contents of
7 * the area changes),
8 * the rest is payload (non-interleaved).
10 * This file is part of MPlayer.
12 * MPlayer is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * MPlayer is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <inttypes.h>
31 #include <unistd.h>
32 #include "config.h"
34 #include <sys/types.h>
35 #include <sys/mman.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
40 #include "af.h"
41 #include "get_path.h"
43 #define DEF_SZ 512 // default buffer size (in samples)
44 #define SHARED_FILE "mplayer-af_export" /* default file name
45 (relative to ~/.mplayer/ */
47 #define SIZE_HEADER (2 * sizeof(int) + sizeof(unsigned long long))
49 // Data for specific instances of this filter
50 typedef struct af_export_s
52 unsigned long long count; // Used for sync
53 void* buf[AF_NCH]; // Buffers for storing the data before it is exported
54 int sz; // Size of buffer in samples
55 int wi; // Write index
56 int fd; // File descriptor to shared memory area
57 char* filename; // File to export data
58 void* mmap_area; // MMap shared area
59 } af_export_t;
62 /* Initialization and runtime control
63 af audio filter instance
64 cmd control command
65 arg argument
67 static int control(struct af_instance_s* af, int cmd, void* arg)
69 af_export_t* s = af->setup;
70 switch (cmd){
71 case AF_CONTROL_REINIT:{
72 int i=0;
73 int mapsize;
75 // Free previous buffers
76 if (s->buf && s->buf[0])
77 free(s->buf[0]);
79 // unmap previous area
80 if(s->mmap_area)
81 munmap(s->mmap_area, SIZE_HEADER + (af->data->bps*s->sz*af->data->nch));
82 // close previous file descriptor
83 if(s->fd)
84 close(s->fd);
86 // Accept only int16_t as input format (which sucks)
87 af->data->rate = ((af_data_t*)arg)->rate;
88 af->data->nch = ((af_data_t*)arg)->nch;
89 af->data->format = AF_FORMAT_S16_NE;
90 af->data->bps = 2;
92 // If buffer length isn't set, set it to the default value
93 if(s->sz == 0)
94 s->sz = DEF_SZ;
96 // Allocate new buffers (as one continuous block)
97 s->buf[0] = calloc(s->sz*af->data->nch, af->data->bps);
98 if(NULL == s->buf[0])
99 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[export] Out of memory\n");
100 for(i = 1; i < af->data->nch; i++)
101 s->buf[i] = s->buf[0] + i*s->sz*af->data->bps;
103 // Init memory mapping
104 s->fd = open(s->filename, O_RDWR | O_CREAT | O_TRUNC, 0640);
105 mp_msg(MSGT_AFILTER, MSGL_INFO, "[export] Exporting to file: %s\n", s->filename);
106 if(s->fd < 0)
107 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[export] Could not open/create file: %s\n",
108 s->filename);
110 // header + buffer
111 mapsize = (SIZE_HEADER + (af->data->bps * s->sz * af->data->nch));
113 // grow file to needed size
114 for(i = 0; i < mapsize; i++){
115 char null = 0;
116 write(s->fd, (void*) &null, 1);
119 // mmap size
120 s->mmap_area = mmap(0, mapsize, PROT_READ|PROT_WRITE,MAP_SHARED, s->fd, 0);
121 if(s->mmap_area == NULL)
122 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[export] Could not mmap file %s\n", s->filename);
123 mp_msg(MSGT_AFILTER, MSGL_INFO, "[export] Memory mapped to file: %s (%p)\n",
124 s->filename, s->mmap_area);
126 // Initialize header
127 *((int*)s->mmap_area) = af->data->nch;
128 *((int*)s->mmap_area + 1) = s->sz * af->data->bps * af->data->nch;
129 msync(s->mmap_area, mapsize, MS_ASYNC);
131 // Use test_output to return FALSE if necessary
132 return af_test_output(af, (af_data_t*)arg);
134 case AF_CONTROL_COMMAND_LINE:{
135 int i=0;
136 char *str = arg;
138 if (!str){
139 if(s->filename)
140 free(s->filename);
142 s->filename = get_path(SHARED_FILE);
143 return AF_OK;
146 while((str[i]) && (str[i] != ':'))
147 i++;
149 if(s->filename)
150 free(s->filename);
152 s->filename = calloc(i + 1, 1);
153 memcpy(s->filename, str, i);
154 s->filename[i] = 0;
156 sscanf(str + i + 1, "%d", &(s->sz));
158 return af->control(af, AF_CONTROL_EXPORT_SZ | AF_CONTROL_SET, &s->sz);
160 case AF_CONTROL_EXPORT_SZ | AF_CONTROL_SET:
161 s->sz = * (int *) arg;
162 if((s->sz <= 0) || (s->sz > 2048))
163 mp_msg(MSGT_AFILTER, MSGL_ERR, "[export] Buffer size must be between"
164 " 1 and 2048\n" );
166 return AF_OK;
167 case AF_CONTROL_EXPORT_SZ | AF_CONTROL_GET:
168 *(int*) arg = s->sz;
169 return AF_OK;
172 return AF_UNKNOWN;
175 /* Free allocated memory and clean up other stuff too.
176 af audio filter instance
178 static void uninit( struct af_instance_s* af )
180 if (af->data){
181 free(af->data);
182 af->data = NULL;
185 if(af->setup){
186 af_export_t* s = af->setup;
187 if (s->buf && s->buf[0])
188 free(s->buf[0]);
190 // Free mmaped area
191 if(s->mmap_area)
192 munmap(s->mmap_area, sizeof(af_export_t));
194 if(s->fd > -1)
195 close(s->fd);
197 if(s->filename)
198 free(s->filename);
200 free(af->setup);
201 af->setup = NULL;
205 /* Filter data through filter
206 af audio filter instance
207 data audio data
209 static af_data_t* play( struct af_instance_s* af, af_data_t* data )
211 af_data_t* c = data; // Current working data
212 af_export_t* s = af->setup; // Setup for this instance
213 int16_t* a = c->audio; // Incomming sound
214 int nch = c->nch; // Number of channels
215 int len = c->len/c->bps; // Number of sample in data chunk
216 int sz = s->sz; // buffer size (in samples)
217 int flag = 0; // Set to 1 if buffer is filled
219 int ch, i;
221 // Fill all buffers
222 for(ch = 0; ch < nch; ch++){
223 int wi = s->wi; // Reset write index
224 int16_t* b = s->buf[ch]; // Current buffer
226 // Copy data to export buffers
227 for(i = ch; i < len; i += nch){
228 b[wi++] = a[i];
229 if(wi >= sz){ // Don't write outside the end of the buffer
230 flag = 1;
231 break;
234 s->wi = wi % s->sz;
237 // Export buffer to mmaped area
238 if(flag){
239 // update buffer in mapped area
240 memcpy(s->mmap_area + SIZE_HEADER, s->buf[0], sz * c->bps * nch);
241 s->count++; // increment counter (to sync)
242 memcpy(s->mmap_area + SIZE_HEADER - sizeof(s->count),
243 &(s->count), sizeof(s->count));
246 // We don't modify data, just export it
247 return data;
250 /* Allocate memory and set function pointers
251 af audio filter instance
252 returns AF_OK or AF_ERROR
254 static int af_open( af_instance_t* af )
256 af->control = control;
257 af->uninit = uninit;
258 af->play = play;
259 af->mul=1;
260 af->data = calloc(1, sizeof(af_data_t));
261 af->setup = calloc(1, sizeof(af_export_t));
262 if((af->data == NULL) || (af->setup == NULL))
263 return AF_ERROR;
265 ((af_export_t *)af->setup)->filename = get_path(SHARED_FILE);
267 return AF_OK;
270 // Description of this filter
271 af_info_t af_info_export = {
272 "Sound export filter",
273 "export",
274 "Anders; Gustavo Sverzut Barbieri <gustavo.barbieri@ic.unicamp.br>",
276 AF_FLAGS_REENTRANT,
277 af_open