2009-01-05 12:41:40 +00:00
|
|
|
/*
|
|
|
|
* This audio filter exports the incoming signal to other processes
|
|
|
|
* using memory mapping. The memory mapped area contains a header:
|
|
|
|
* int nch,
|
|
|
|
* int size,
|
|
|
|
* unsigned long long counter (updated every time the contents of
|
|
|
|
* the area changes),
|
|
|
|
* the rest is payload (non-interleaved).
|
|
|
|
*
|
2013-10-23 17:05:47 +00:00
|
|
|
* Authors: Anders; Gustavo Sverzut Barbieri <gustavo.barbieri@ic.unicamp.br>
|
|
|
|
*
|
2009-01-05 12:41:40 +00:00
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2003-09-20 13:42:26 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <unistd.h>
|
2005-11-13 18:28:44 +00:00
|
|
|
#include "config.h"
|
2003-09-21 12:16:16 +00:00
|
|
|
|
2003-09-20 13:42:26 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2013-11-30 21:40:51 +00:00
|
|
|
#include "osdep/io.h"
|
|
|
|
|
2012-12-09 14:05:21 +00:00
|
|
|
#include "talloc.h"
|
2003-09-20 13:42:26 +00:00
|
|
|
#include "af.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/path.h"
|
2003-09-20 13:42:26 +00:00
|
|
|
|
|
|
|
#define DEF_SZ 512 // default buffer size (in samples)
|
2012-10-11 00:04:08 +00:00
|
|
|
#define SHARED_FILE "mpv-af_export" /* default file name
|
2014-04-13 16:00:51 +00:00
|
|
|
(relative to ~/.mpv/ */
|
2003-09-20 13:42:26 +00:00
|
|
|
|
|
|
|
#define SIZE_HEADER (2 * sizeof(int) + sizeof(unsigned long long))
|
|
|
|
|
|
|
|
// Data for specific instances of this filter
|
|
|
|
typedef struct af_export_s
|
|
|
|
{
|
|
|
|
unsigned long long count; // Used for sync
|
2014-04-13 16:00:51 +00:00
|
|
|
void* buf[AF_NCH]; // Buffers for storing the data before it is exported
|
|
|
|
int sz; // Size of buffer in samples
|
|
|
|
int wi; // Write index
|
|
|
|
int fd; // File descriptor to shared memory area
|
|
|
|
char* filename; // File to export data
|
|
|
|
uint8_t *mmap_area; // MMap shared area
|
2003-09-20 13:42:26 +00:00
|
|
|
} af_export_t;
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialization and runtime control
|
|
|
|
af audio filter instance
|
|
|
|
cmd control command
|
|
|
|
arg argument
|
|
|
|
*/
|
2012-11-01 11:23:48 +00:00
|
|
|
static int control(struct af_instance* af, int cmd, void* arg)
|
2003-09-20 13:42:26 +00:00
|
|
|
{
|
2013-12-04 20:15:27 +00:00
|
|
|
af_export_t* s = af->priv;
|
2003-09-20 13:42:26 +00:00
|
|
|
switch (cmd){
|
|
|
|
case AF_CONTROL_REINIT:{
|
|
|
|
int i=0;
|
|
|
|
int mapsize;
|
|
|
|
|
|
|
|
// Free previous buffers
|
2010-11-14 09:12:34 +00:00
|
|
|
if (s->buf)
|
2003-09-20 13:42:26 +00:00
|
|
|
free(s->buf[0]);
|
|
|
|
|
|
|
|
// unmap previous area
|
|
|
|
if(s->mmap_area)
|
|
|
|
munmap(s->mmap_area, SIZE_HEADER + (af->data->bps*s->sz*af->data->nch));
|
|
|
|
// close previous file descriptor
|
|
|
|
if(s->fd)
|
2009-05-13 02:58:57 +00:00
|
|
|
close(s->fd);
|
2003-09-20 13:42:26 +00:00
|
|
|
|
2003-11-28 20:43:00 +00:00
|
|
|
// Accept only int16_t as input format (which sucks)
|
2013-04-05 17:47:51 +00:00
|
|
|
mp_audio_copy_config(af->data, (struct mp_audio*)arg);
|
2013-11-15 20:25:05 +00:00
|
|
|
mp_audio_set_format(af->data, AF_FORMAT_S16);
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2003-11-28 20:43:00 +00:00
|
|
|
// Allocate new buffers (as one continuous block)
|
|
|
|
s->buf[0] = calloc(s->sz*af->data->nch, af->data->bps);
|
2003-09-20 13:42:26 +00:00
|
|
|
if(NULL == s->buf[0])
|
2014-01-24 20:30:15 +00:00
|
|
|
MP_FATAL(af, "Out of memory\n");
|
2003-09-20 13:42:26 +00:00
|
|
|
for(i = 1; i < af->data->nch; i++)
|
2010-07-01 20:49:35 +00:00
|
|
|
s->buf[i] = (uint8_t *)s->buf[0] + i*s->sz*af->data->bps;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2013-09-18 17:56:15 +00:00
|
|
|
if (!s->filename) {
|
2014-01-24 20:30:15 +00:00
|
|
|
MP_FATAL(af, "No filename set.\n");
|
2013-09-18 17:56:15 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-09-20 13:42:26 +00:00
|
|
|
// Init memory mapping
|
2013-11-30 21:40:51 +00:00
|
|
|
s->fd = open(s->filename, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);
|
2014-01-24 20:30:15 +00:00
|
|
|
MP_INFO(af, "Exporting to file: %s\n", s->filename);
|
2012-10-30 17:28:34 +00:00
|
|
|
if(s->fd < 0) {
|
2014-01-24 20:30:15 +00:00
|
|
|
MP_FATAL(af, "Could not open/create file: %s\n",
|
2014-04-13 16:00:51 +00:00
|
|
|
s->filename);
|
2012-10-30 17:28:34 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2003-09-20 13:42:26 +00:00
|
|
|
// header + buffer
|
|
|
|
mapsize = (SIZE_HEADER + (af->data->bps * s->sz * af->data->nch));
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2003-09-20 13:42:26 +00:00
|
|
|
// grow file to needed size
|
|
|
|
for(i = 0; i < mapsize; i++){
|
|
|
|
char null = 0;
|
|
|
|
write(s->fd, (void*) &null, 1);
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2003-09-20 13:42:26 +00:00
|
|
|
// mmap size
|
|
|
|
s->mmap_area = mmap(0, mapsize, PROT_READ|PROT_WRITE,MAP_SHARED, s->fd, 0);
|
|
|
|
if(s->mmap_area == NULL)
|
2014-01-24 20:30:15 +00:00
|
|
|
MP_FATAL(af, "Could not mmap file %s\n", s->filename);
|
|
|
|
MP_INFO(af, "Memory mapped to file: %s (%p)\n",
|
2014-04-13 16:00:51 +00:00
|
|
|
s->filename, s->mmap_area);
|
2003-09-20 13:42:26 +00:00
|
|
|
|
|
|
|
// Initialize header
|
|
|
|
*((int*)s->mmap_area) = af->data->nch;
|
|
|
|
*((int*)s->mmap_area + 1) = s->sz * af->data->bps * af->data->nch;
|
|
|
|
msync(s->mmap_area, mapsize, MS_ASYNC);
|
|
|
|
|
|
|
|
// Use test_output to return FALSE if necessary
|
2012-11-01 11:00:00 +00:00
|
|
|
return af_test_output(af, (struct mp_audio*)arg);
|
2003-09-20 13:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return AF_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free allocated memory and clean up other stuff too.
|
|
|
|
af audio filter instance
|
|
|
|
*/
|
2012-11-01 11:23:48 +00:00
|
|
|
static void uninit( struct af_instance* af )
|
2003-09-20 13:42:26 +00:00
|
|
|
{
|
2013-12-04 20:15:27 +00:00
|
|
|
af_export_t* s = af->priv;
|
2010-11-14 09:12:34 +00:00
|
|
|
if (s->buf)
|
2003-09-20 13:42:26 +00:00
|
|
|
free(s->buf[0]);
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2003-09-20 13:42:26 +00:00
|
|
|
// Free mmaped area
|
|
|
|
if(s->mmap_area)
|
2009-05-13 02:58:57 +00:00
|
|
|
munmap(s->mmap_area, sizeof(af_export_t));
|
2003-09-20 13:42:26 +00:00
|
|
|
|
|
|
|
if(s->fd > -1)
|
|
|
|
close(s->fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Filter data through filter
|
|
|
|
af audio filter instance
|
|
|
|
data audio data
|
|
|
|
*/
|
2013-12-04 23:01:46 +00:00
|
|
|
static int filter( struct af_instance* af, struct mp_audio* data, int flags)
|
2003-09-20 13:42:26 +00:00
|
|
|
{
|
2014-04-13 16:00:51 +00:00
|
|
|
struct mp_audio* c = data; // Current working data
|
|
|
|
af_export_t* s = af->priv; // Setup for this instance
|
|
|
|
int16_t* a = c->planes[0]; // Incomming sound
|
|
|
|
int nch = c->nch; // Number of channels
|
|
|
|
int len = c->samples*c->nch; // Number of sample in data chunk
|
|
|
|
int sz = s->sz; // buffer size (in samples)
|
|
|
|
int flag = 0; // Set to 1 if buffer is filled
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2014-04-13 16:00:51 +00:00
|
|
|
int ch, i;
|
2003-09-20 13:42:26 +00:00
|
|
|
|
|
|
|
// Fill all buffers
|
|
|
|
for(ch = 0; ch < nch; ch++){
|
2014-04-13 16:00:51 +00:00
|
|
|
int wi = s->wi; // Reset write index
|
|
|
|
int16_t* b = s->buf[ch]; // Current buffer
|
2003-09-20 13:42:26 +00:00
|
|
|
|
2009-05-13 02:58:57 +00:00
|
|
|
// Copy data to export buffers
|
2003-09-20 13:42:26 +00:00
|
|
|
for(i = ch; i < len; i += nch){
|
|
|
|
b[wi++] = a[i];
|
|
|
|
if(wi >= sz){ // Don't write outside the end of the buffer
|
2014-04-13 16:00:51 +00:00
|
|
|
flag = 1;
|
|
|
|
break;
|
2003-09-20 13:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s->wi = wi % s->sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Export buffer to mmaped area
|
|
|
|
if(flag){
|
|
|
|
// update buffer in mapped area
|
|
|
|
memcpy(s->mmap_area + SIZE_HEADER, s->buf[0], sz * c->bps * nch);
|
|
|
|
s->count++; // increment counter (to sync)
|
2009-05-13 02:58:57 +00:00
|
|
|
memcpy(s->mmap_area + SIZE_HEADER - sizeof(s->count),
|
2014-04-13 16:00:51 +00:00
|
|
|
&(s->count), sizeof(s->count));
|
2003-09-20 13:42:26 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 23:01:46 +00:00
|
|
|
return 0;
|
2003-09-20 13:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate memory and set function pointers
|
2009-05-13 02:58:57 +00:00
|
|
|
af audio filter instance
|
2003-09-20 13:42:26 +00:00
|
|
|
returns AF_OK or AF_ERROR
|
|
|
|
*/
|
2012-11-01 11:23:48 +00:00
|
|
|
static int af_open( struct af_instance* af )
|
2003-09-20 13:42:26 +00:00
|
|
|
{
|
|
|
|
af->control = control;
|
|
|
|
af->uninit = uninit;
|
2013-12-04 23:01:46 +00:00
|
|
|
af->filter = filter;
|
2013-12-04 20:15:27 +00:00
|
|
|
af_export_t *priv = af->priv;
|
2003-09-20 13:42:26 +00:00
|
|
|
|
2013-12-21 19:40:26 +00:00
|
|
|
if (!priv->filename || !priv->filename[0]) {
|
|
|
|
MP_FATAL(af, "no export filename given");
|
2013-12-04 20:15:27 +00:00
|
|
|
return AF_ERROR;
|
2013-12-21 19:40:26 +00:00
|
|
|
}
|
2003-09-20 13:42:26 +00:00
|
|
|
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-04 20:15:27 +00:00
|
|
|
#define OPT_BASE_STRUCT af_export_t
|
2014-06-10 21:56:05 +00:00
|
|
|
const struct af_info af_info_export = {
|
2013-10-23 17:05:47 +00:00
|
|
|
.info = "Sound export filter",
|
|
|
|
.name = "export",
|
|
|
|
.open = af_open,
|
2013-12-04 20:15:27 +00:00
|
|
|
.priv_size = sizeof(af_export_t),
|
|
|
|
.options = (const struct m_option[]) {
|
|
|
|
OPT_STRING("filename", filename, 0),
|
|
|
|
OPT_INTRANGE("buffersamples", sz, 0, 1, 2048, OPTDEF_INT(DEF_SZ)),
|
|
|
|
{0}
|
|
|
|
},
|
2003-09-20 13:42:26 +00:00
|
|
|
};
|