2014-03-31 07:10:18 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Espen Jürgensen <espenjurgensen@gmail.com>
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include "pipe.h"
|
|
|
|
#include "logger.h"
|
|
|
|
|
|
|
|
#define PIPE_BUFFER_SIZE 8192
|
|
|
|
|
|
|
|
static int g_fd = -1;
|
2014-04-01 15:43:30 -04:00
|
|
|
static uint16_t *g_buf = NULL;
|
2014-03-31 07:10:18 -04:00
|
|
|
|
|
|
|
int
|
2016-10-26 13:40:15 -04:00
|
|
|
pipe_setup(const char *path)
|
2014-03-31 07:10:18 -04:00
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
2016-10-26 13:40:15 -04:00
|
|
|
if (!path)
|
2014-03-31 07:10:18 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_PLAYER, "Path to pipe is NULL\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-10-26 13:40:15 -04:00
|
|
|
DPRINTF(E_DBG, L_PLAYER, "Setting up pipe: %s\n", path);
|
2014-03-31 07:10:18 -04:00
|
|
|
|
2016-10-26 13:40:15 -04:00
|
|
|
if (lstat(path, &sb) < 0)
|
2014-03-31 07:10:18 -04:00
|
|
|
{
|
2016-10-26 13:40:15 -04:00
|
|
|
DPRINTF(E_LOG, L_PLAYER, "Could not lstat() '%s': %s\n", path, strerror(errno));
|
2014-03-31 07:10:18 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISFIFO(sb.st_mode))
|
|
|
|
{
|
2016-10-26 13:40:15 -04:00
|
|
|
DPRINTF(E_LOG, L_PLAYER, "Source type is pipe, but path is not a fifo: %s\n", path);
|
2014-03-31 07:10:18 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-31 07:13:24 -04:00
|
|
|
pipe_cleanup();
|
|
|
|
|
2016-10-26 13:40:15 -04:00
|
|
|
g_fd = open(path, O_RDONLY | O_NONBLOCK);
|
2014-03-31 07:10:18 -04:00
|
|
|
if (g_fd < 0)
|
|
|
|
{
|
2016-10-26 13:40:15 -04:00
|
|
|
DPRINTF(E_LOG, L_PLAYER, "Could not open pipe for reading '%s': %s\n", path, strerror(errno));
|
2014-03-31 07:10:18 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-04-01 15:43:30 -04:00
|
|
|
g_buf = (uint16_t *)malloc(PIPE_BUFFER_SIZE);
|
2014-03-31 07:10:18 -04:00
|
|
|
if (!g_buf)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_PLAYER, "Out of memory for buffer\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-04 16:33:32 -04:00
|
|
|
return 0;
|
2014-03-31 07:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pipe_cleanup(void)
|
|
|
|
{
|
|
|
|
if (g_fd >= 0)
|
|
|
|
close(g_fd);
|
|
|
|
g_fd = -1;
|
|
|
|
|
|
|
|
if (g_buf)
|
|
|
|
free(g_buf);
|
|
|
|
g_buf = NULL;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pipe_audio_get(struct evbuffer *evbuf, int wanted)
|
|
|
|
{
|
|
|
|
int got;
|
|
|
|
|
|
|
|
if (wanted > PIPE_BUFFER_SIZE)
|
|
|
|
wanted = PIPE_BUFFER_SIZE;
|
|
|
|
|
|
|
|
got = read(g_fd, g_buf, wanted);
|
|
|
|
|
2014-10-07 13:55:28 -04:00
|
|
|
if ((got < 0) && (errno != EAGAIN))
|
2014-03-31 07:10:18 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_PLAYER, "Could not read from pipe: %s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-10-07 13:55:28 -04:00
|
|
|
// If the other end of the pipe is not writing or the read was blocked,
|
|
|
|
// we just return silence
|
|
|
|
if (got <= 0)
|
2014-03-31 07:10:18 -04:00
|
|
|
{
|
|
|
|
memset(g_buf, 0, wanted);
|
|
|
|
got = wanted;
|
|
|
|
}
|
|
|
|
|
|
|
|
evbuffer_add(evbuf, g_buf, got);
|
|
|
|
|
|
|
|
return got;
|
|
|
|
}
|
|
|
|
|