A lot of updates
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
This is a VERY SIMPLE Speex VoIP client. It is not a complete VoIP application,
|
||||
isn't compatible with anything else (including probably future versions of
|
||||
itself) and does not support any form of standard protocols. It is intended
|
||||
only as a way to show how to use Speex in a VoIP application.
|
||||
|
||||
To use it:
|
||||
|
||||
On Alices machine:
|
||||
% speexclient plughw:0,0 bob.somewhere.net alice_port bob_port
|
||||
|
||||
On Bob's machine:
|
||||
% speexclient plughw:0,0 alice.somewhere.net bob_port alice_port
|
||||
|
||||
where bob_port is the UDP port on which bob receives and alice_port is the
|
||||
UDP port on which alice receives. In most cases, the two ports can be the same.
|
||||
|
||||
Note that the clients do not even know whether they are connected or not. All
|
||||
they do is send/receive the audio to/from a specific port.
|
||||
@@ -1,431 +0,0 @@
|
||||
/*
|
||||
Copyright (C) 2004-2006 Jean-Marc Valin
|
||||
Copyright (C) 2006 Commonwealth Scientific and Industrial Research
|
||||
Organisation (CSIRO) Australia
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The name of the author may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "alsa_device.h"
|
||||
#include <stdlib.h>
|
||||
#include <alsa/asoundlib.h>
|
||||
|
||||
struct AlsaDevice_ {
|
||||
char *device_name;
|
||||
int channels;
|
||||
int period;
|
||||
snd_pcm_t *capture_handle;
|
||||
snd_pcm_t *playback_handle;
|
||||
int readN, writeN;
|
||||
struct pollfd *read_fd, *write_fd;
|
||||
};
|
||||
|
||||
AlsaDevice *alsa_device_open(char *device_name, unsigned int rate, int channels, int period)
|
||||
{
|
||||
int dir;
|
||||
int err;
|
||||
snd_pcm_hw_params_t *hw_params;
|
||||
snd_pcm_sw_params_t *sw_params;
|
||||
snd_pcm_uframes_t period_size = period;
|
||||
snd_pcm_uframes_t buffer_size = 2*period;
|
||||
static snd_output_t *jcd_out;
|
||||
AlsaDevice *dev = malloc(sizeof(*dev));
|
||||
if (!dev)
|
||||
return NULL;
|
||||
dev->device_name = malloc(1+strlen(device_name));
|
||||
if (!dev->device_name)
|
||||
{
|
||||
free(dev);
|
||||
return NULL;
|
||||
}
|
||||
strcpy(dev->device_name, device_name);
|
||||
dev->channels = channels;
|
||||
dev->period = period;
|
||||
err = snd_output_stdio_attach(&jcd_out, stdout, 0);
|
||||
|
||||
if ((err = snd_pcm_open (&dev->capture_handle, dev->device_name, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
|
||||
fprintf (stderr, "cannot open audio device %s (%s)\n",
|
||||
dev->device_name,
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
|
||||
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if ((err = snd_pcm_hw_params_any (dev->capture_handle, hw_params)) < 0) {
|
||||
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if ((err = snd_pcm_hw_params_set_access (dev->capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
|
||||
fprintf (stderr, "cannot set access type (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if ((err = snd_pcm_hw_params_set_format (dev->capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
|
||||
fprintf (stderr, "cannot set sample format (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if ((err = snd_pcm_hw_params_set_rate_near (dev->capture_handle, hw_params, &rate, 0)) < 0) {
|
||||
fprintf (stderr, "cannot set sample rate (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
/*fprintf (stderr, "rate = %d\n", rate);*/
|
||||
|
||||
if ((err = snd_pcm_hw_params_set_channels (dev->capture_handle, hw_params, channels)) < 0) {
|
||||
fprintf (stderr, "cannot set channel count (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
period_size = period;
|
||||
dir = 0;
|
||||
if ((err = snd_pcm_hw_params_set_period_size_near (dev->capture_handle, hw_params, &period_size, &dir)) < 0) {
|
||||
fprintf (stderr, "cannot set period size (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if ((err = snd_pcm_hw_params_set_periods (dev->capture_handle, hw_params, 2, 0)) < 0) {
|
||||
fprintf (stderr, "cannot set number of periods (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
buffer_size = period_size * 2;
|
||||
dir=0;
|
||||
if ((err = snd_pcm_hw_params_set_buffer_size_near (dev->capture_handle, hw_params, &buffer_size)) < 0) {
|
||||
fprintf (stderr, "cannot set buffer time (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if ((err = snd_pcm_hw_params (dev->capture_handle, hw_params)) < 0) {
|
||||
fprintf (stderr, "cannot set capture parameters (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
/*snd_pcm_dump_setup(dev->capture_handle, jcd_out);*/
|
||||
snd_pcm_hw_params_free (hw_params);
|
||||
|
||||
if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
|
||||
fprintf (stderr, "cannot allocate software parameters structure (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
if ((err = snd_pcm_sw_params_current (dev->capture_handle, sw_params)) < 0) {
|
||||
fprintf (stderr, "cannot initialize software parameters structure (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
if ((err = snd_pcm_sw_params_set_avail_min (dev->capture_handle, sw_params, period)) < 0) {
|
||||
fprintf (stderr, "cannot set minimum available count (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
if ((err = snd_pcm_sw_params (dev->capture_handle, sw_params)) < 0) {
|
||||
fprintf (stderr, "cannot set software parameters (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
|
||||
if ((err = snd_pcm_open (&dev->playback_handle, dev->device_name, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
|
||||
fprintf (stderr, "cannot open audio device %s (%s)\n",
|
||||
dev->device_name,
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
|
||||
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if ((err = snd_pcm_hw_params_any (dev->playback_handle, hw_params)) < 0) {
|
||||
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if ((err = snd_pcm_hw_params_set_access (dev->playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
|
||||
fprintf (stderr, "cannot set access type (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if ((err = snd_pcm_hw_params_set_format (dev->playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
|
||||
fprintf (stderr, "cannot set sample format (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if ((err = snd_pcm_hw_params_set_rate_near (dev->playback_handle, hw_params, &rate, 0)) < 0) {
|
||||
fprintf (stderr, "cannot set sample rate (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
/*fprintf (stderr, "rate = %d\n", rate);*/
|
||||
|
||||
if ((err = snd_pcm_hw_params_set_channels (dev->playback_handle, hw_params, channels)) < 0) {
|
||||
fprintf (stderr, "cannot set channel count (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
period_size = period;
|
||||
dir = 0;
|
||||
if ((err = snd_pcm_hw_params_set_period_size_near (dev->playback_handle, hw_params, &period_size, &dir)) < 0) {
|
||||
fprintf (stderr, "cannot set period size (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
if ((err = snd_pcm_hw_params_set_periods (dev->playback_handle, hw_params, 2, 0)) < 0) {
|
||||
fprintf (stderr, "cannot set number of periods (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
buffer_size = period_size * 2;
|
||||
dir=0;
|
||||
if ((err = snd_pcm_hw_params_set_buffer_size_near (dev->playback_handle, hw_params, &buffer_size)) < 0) {
|
||||
fprintf (stderr, "cannot set buffer time (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
|
||||
if ((err = snd_pcm_hw_params (dev->playback_handle, hw_params)) < 0) {
|
||||
fprintf (stderr, "cannot set playback parameters (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
/*snd_pcm_dump_setup(dev->playback_handle, jcd_out);*/
|
||||
snd_pcm_hw_params_free (hw_params);
|
||||
|
||||
|
||||
if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
|
||||
fprintf (stderr, "cannot allocate software parameters structure (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
if ((err = snd_pcm_sw_params_current (dev->playback_handle, sw_params)) < 0) {
|
||||
fprintf (stderr, "cannot initialize software parameters structure (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
if ((err = snd_pcm_sw_params_set_avail_min (dev->playback_handle, sw_params, period)) < 0) {
|
||||
fprintf (stderr, "cannot set minimum available count (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
if ((err = snd_pcm_sw_params_set_start_threshold (dev->playback_handle, sw_params, period)) < 0) {
|
||||
fprintf (stderr, "cannot set start mode (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
if ((err = snd_pcm_sw_params (dev->playback_handle, sw_params)) < 0) {
|
||||
fprintf (stderr, "cannot set software parameters (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
|
||||
snd_pcm_link(dev->capture_handle, dev->playback_handle);
|
||||
if ((err = snd_pcm_prepare (dev->capture_handle)) < 0) {
|
||||
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
if ((err = snd_pcm_prepare (dev->playback_handle)) < 0) {
|
||||
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
dev->readN = snd_pcm_poll_descriptors_count(dev->capture_handle);
|
||||
dev->writeN = snd_pcm_poll_descriptors_count(dev->playback_handle);
|
||||
|
||||
dev->read_fd = malloc(dev->readN*sizeof(*dev->read_fd));
|
||||
/*printf ("descriptors: %d %d\n", dev->readN, dev->writeN);*/
|
||||
if (snd_pcm_poll_descriptors(dev->capture_handle, dev->read_fd, dev->readN) != dev->readN)
|
||||
{
|
||||
fprintf (stderr, "cannot obtain capture file descriptors (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
|
||||
dev->write_fd = malloc(dev->writeN*sizeof(*dev->read_fd));
|
||||
if (snd_pcm_poll_descriptors(dev->playback_handle, dev->write_fd, dev->writeN) != dev->writeN)
|
||||
{
|
||||
fprintf (stderr, "cannot obtain playback file descriptors (%s)\n",
|
||||
snd_strerror (err));
|
||||
assert(0);
|
||||
}
|
||||
return dev;
|
||||
}
|
||||
|
||||
void alsa_device_close(AlsaDevice *dev)
|
||||
{
|
||||
snd_pcm_close(dev->capture_handle);
|
||||
snd_pcm_close(dev->playback_handle);
|
||||
free(dev->device_name);
|
||||
free(dev);
|
||||
}
|
||||
|
||||
int alsa_device_read(AlsaDevice *dev, short *pcm, int len)
|
||||
{
|
||||
int err;
|
||||
/*fprintf (stderr, "-");*/
|
||||
if ((err = snd_pcm_readi (dev->capture_handle, pcm, len)) != len)
|
||||
{
|
||||
if (err<0)
|
||||
{
|
||||
//fprintf(stderr, "error %d, EPIPE = %d\n", err, EPIPE);
|
||||
if (err == -EPIPE)
|
||||
{
|
||||
fprintf (stderr, "An overrun has occured, reseting capture\n");
|
||||
} else
|
||||
{
|
||||
fprintf (stderr, "read from audio interface failed (%s)\n",
|
||||
snd_strerror (err));
|
||||
}
|
||||
if ((err = snd_pcm_prepare (dev->capture_handle)) < 0)
|
||||
{
|
||||
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
|
||||
snd_strerror (err));
|
||||
}
|
||||
if ((err = snd_pcm_start (dev->capture_handle)) < 0)
|
||||
{
|
||||
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
|
||||
snd_strerror (err));
|
||||
}
|
||||
/*alsa_device_read(dev,pcm,len);*/
|
||||
} else {
|
||||
fprintf (stderr, "Couldn't read as many samples as I wanted (%d instead of %d)\n", err, len);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int alsa_device_write(AlsaDevice *dev, const short *pcm, int len)
|
||||
{
|
||||
int err;
|
||||
/*fprintf (stderr, "+");*/
|
||||
if ((err = snd_pcm_writei (dev->playback_handle, pcm, len)) != len)
|
||||
{
|
||||
if (err<0)
|
||||
{
|
||||
if (err == -EPIPE)
|
||||
{
|
||||
fprintf (stderr, "An underrun has occured, reseting playback, len=%d\n", len);
|
||||
} else
|
||||
{
|
||||
fprintf (stderr, "write to audio interface failed (%s)\n",
|
||||
snd_strerror (err));
|
||||
}
|
||||
if ((err = snd_pcm_prepare (dev->playback_handle)) < 0)
|
||||
{
|
||||
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
|
||||
snd_strerror (err));
|
||||
}
|
||||
} else {
|
||||
fprintf (stderr, "Couldn't write as many samples as I wanted (%d instead of %d)\n", err, len);
|
||||
}
|
||||
/*alsa_device_write(dev,pcm,len);*/
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int alsa_device_capture_ready(AlsaDevice *dev, struct pollfd *pfds, unsigned int nfds)
|
||||
{
|
||||
unsigned short revents=0;
|
||||
int err;
|
||||
if ((err = snd_pcm_poll_descriptors_revents(dev->capture_handle, pfds, dev->readN, &revents)) < 0)
|
||||
{
|
||||
//cerr << "error in snd_pcm_poll_descriptors_revents for capture: " << snd_strerror (err) << endl;
|
||||
//FIXME: This is a kludge
|
||||
fprintf (stderr, "error in alsa_device_capture_ready: %s\n", snd_strerror (err));
|
||||
return pfds[0].revents & POLLIN;
|
||||
}
|
||||
//cerr << (revents & POLLERR) << endl;
|
||||
return revents & POLLIN;
|
||||
}
|
||||
|
||||
int alsa_device_playback_ready(AlsaDevice *dev, struct pollfd *pfds, unsigned int nfds)
|
||||
{
|
||||
unsigned short revents=0;
|
||||
int err;
|
||||
if ((err = snd_pcm_poll_descriptors_revents(dev->playback_handle, pfds+dev->readN, dev->writeN, &revents)) < 0)
|
||||
{
|
||||
//cerr << "error in snd_pcm_poll_descriptors_revents for playback: " << snd_strerror (err) << endl;
|
||||
//FIXME: This is a kludge
|
||||
fprintf (stderr, "error in alsa_device_playback_ready: %s\n", snd_strerror (err));
|
||||
return pfds[1].revents & POLLOUT;
|
||||
}
|
||||
//cerr << (revents & POLLERR) << endl;
|
||||
return revents & POLLOUT;
|
||||
}
|
||||
|
||||
void alsa_device_start(AlsaDevice *dev)
|
||||
{
|
||||
int i;
|
||||
short pcm[dev->period*dev->channels];
|
||||
for (i=0;i<dev->period*dev->channels;i++)
|
||||
pcm[i] = 0;
|
||||
alsa_device_write(dev, pcm, dev->period);
|
||||
alsa_device_write(dev, pcm, dev->period);
|
||||
snd_pcm_start(dev->capture_handle);
|
||||
snd_pcm_start(dev->playback_handle);
|
||||
}
|
||||
|
||||
int alsa_device_nfds(AlsaDevice *dev)
|
||||
{
|
||||
return dev->writeN+dev->readN;
|
||||
}
|
||||
|
||||
void alsa_device_getfds(AlsaDevice *dev, struct pollfd *pfds, unsigned int nfds)
|
||||
{
|
||||
int i;
|
||||
assert (nfds >= dev->writeN+dev->readN);
|
||||
for (i=0;i<dev->readN;i++)
|
||||
pfds[i] = dev->read_fd[i];
|
||||
for (i=0;i<dev->writeN;i++)
|
||||
pfds[i+dev->readN] = dev->write_fd[i];
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
Copyright (C) 2004-2006 Jean-Marc Valin
|
||||
Copyright (C) 2006 Commonwealth Scientific and Industrial Research
|
||||
Organisation (CSIRO) Australia
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The name of the author may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef ALSA_DEVICE_H
|
||||
#define ALSA_DEVICE_H
|
||||
|
||||
#include <sys/poll.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct AlsaDevice_;
|
||||
|
||||
typedef struct AlsaDevice_ AlsaDevice;
|
||||
|
||||
AlsaDevice *alsa_device_open(char *device_name, unsigned int rate, int channels, int period);
|
||||
|
||||
void alsa_device_close(AlsaDevice *dev);
|
||||
|
||||
int alsa_device_read(AlsaDevice *dev, short *pcm, int len);
|
||||
|
||||
int alsa_device_write(AlsaDevice *dev, const short *pcm, int len);
|
||||
|
||||
int alsa_device_capture_ready(AlsaDevice *dev, struct pollfd *pfds, unsigned int nfds);
|
||||
|
||||
int alsa_device_playback_ready(AlsaDevice *dev, struct pollfd *pfds, unsigned int nfds);
|
||||
|
||||
void alsa_device_start(AlsaDevice *dev);
|
||||
|
||||
int alsa_device_nfds(AlsaDevice *dev);
|
||||
|
||||
void alsa_device_getfds(AlsaDevice *dev, struct pollfd *pfds, unsigned int nfds);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,3 +0,0 @@
|
||||
#!/bin/sh
|
||||
gcc -Wall -I../include speex_jitter_buffer.c speexclient.c alsa_device.c `pkg-config --cflags speexdsp` -o speexclient -lspeex -lspeexdsp -lasound -lm `pkg-config --libs speexdsp`
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
#include <speex/speex_jitter.h>
|
||||
#include "speex_jitter_buffer.h"
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
|
||||
void speex_jitter_init(SpeexJitter *jitter, void *decoder, int sampling_rate)
|
||||
{
|
||||
jitter->dec = decoder;
|
||||
speex_decoder_ctl(decoder, SPEEX_GET_FRAME_SIZE, &jitter->frame_size);
|
||||
|
||||
jitter->packets = jitter_buffer_init(jitter->frame_size);
|
||||
|
||||
speex_bits_init(&jitter->current_packet);
|
||||
jitter->valid_bits = 0;
|
||||
|
||||
}
|
||||
|
||||
void speex_jitter_destroy(SpeexJitter *jitter)
|
||||
{
|
||||
jitter_buffer_destroy(jitter->packets);
|
||||
speex_bits_destroy(&jitter->current_packet);
|
||||
}
|
||||
|
||||
void speex_jitter_put(SpeexJitter *jitter, char *packet, int len, int timestamp)
|
||||
{
|
||||
JitterBufferPacket p;
|
||||
p.data = packet;
|
||||
p.len = len;
|
||||
p.timestamp = timestamp;
|
||||
p.span = jitter->frame_size;
|
||||
jitter_buffer_put(jitter->packets, &p);
|
||||
}
|
||||
|
||||
void speex_jitter_get(SpeexJitter *jitter, spx_int16_t *out, int *current_timestamp)
|
||||
{
|
||||
int i;
|
||||
int ret;
|
||||
spx_int32_t activity;
|
||||
char data[2048];
|
||||
JitterBufferPacket packet;
|
||||
packet.data = data;
|
||||
packet.len = 2048;
|
||||
|
||||
if (jitter->valid_bits)
|
||||
{
|
||||
/* Try decoding last received packet */
|
||||
ret = speex_decode_int(jitter->dec, &jitter->current_packet, out);
|
||||
if (ret == 0)
|
||||
{
|
||||
jitter_buffer_tick(jitter->packets);
|
||||
return;
|
||||
} else {
|
||||
jitter->valid_bits = 0;
|
||||
}
|
||||
}
|
||||
|
||||
ret = jitter_buffer_get(jitter->packets, &packet, jitter->frame_size, NULL);
|
||||
|
||||
if (ret != JITTER_BUFFER_OK)
|
||||
{
|
||||
/* No packet found */
|
||||
|
||||
/*fprintf (stderr, "lost/late frame\n");*/
|
||||
/*Packet is late or lost*/
|
||||
speex_decode_int(jitter->dec, NULL, out);
|
||||
} else {
|
||||
speex_bits_read_from(&jitter->current_packet, packet.data, packet.len);
|
||||
/* Decode packet */
|
||||
ret = speex_decode_int(jitter->dec, &jitter->current_packet, out);
|
||||
if (ret == 0)
|
||||
{
|
||||
jitter->valid_bits = 1;
|
||||
} else {
|
||||
/* Error while decoding */
|
||||
for (i=0;i<jitter->frame_size;i++)
|
||||
out[i]=0;
|
||||
}
|
||||
}
|
||||
speex_decoder_ctl(jitter->dec, SPEEX_GET_ACTIVITY, &activity);
|
||||
if (activity < 30)
|
||||
jitter_buffer_update_delay(jitter->packets, &packet, NULL);
|
||||
jitter_buffer_tick(jitter->packets);
|
||||
}
|
||||
|
||||
int speex_jitter_get_pointer_timestamp(SpeexJitter *jitter)
|
||||
{
|
||||
return jitter_buffer_get_pointer_timestamp(jitter->packets);
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/* Copyright (C) 2002 Jean-Marc Valin */
|
||||
/**
|
||||
@file speex_jitter_buffer.h
|
||||
@brief Adaptive jitter buffer for Speex packets only
|
||||
*/
|
||||
|
||||
#include <speex/speex_jitter.h>
|
||||
#include <speex/speex.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** @defgroup SpeexJitter SpeexJitter: Adaptive jitter buffer specifically for Speex
|
||||
* This is the jitter buffer that reorders UDP/RTP packets and adjusts the buffer size
|
||||
* to maintain good quality and low latency. This is a simplified version that works only
|
||||
* with Speex, but is much easier to use.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** Speex jitter-buffer state. Never use it directly! */
|
||||
typedef struct SpeexJitter {
|
||||
SpeexBits current_packet; /**< Current Speex packet */
|
||||
int valid_bits; /**< True if Speex bits are valid */
|
||||
JitterBuffer *packets; /**< Generic jitter buffer state */
|
||||
void *dec; /**< Pointer to Speex decoder */
|
||||
spx_int32_t frame_size; /**< Frame size of Speex decoder */
|
||||
} SpeexJitter;
|
||||
|
||||
/** Initialise jitter buffer
|
||||
*
|
||||
* @param jitter State of the Speex jitter buffer
|
||||
* @param decoder Speex decoder to call
|
||||
* @param sampling_rate Sampling rate used by the decoder
|
||||
*/
|
||||
void speex_jitter_init(SpeexJitter *jitter, void *decoder, int sampling_rate);
|
||||
|
||||
/** Destroy jitter buffer */
|
||||
void speex_jitter_destroy(SpeexJitter *jitter);
|
||||
|
||||
/** Put one packet into the jitter buffer */
|
||||
void speex_jitter_put(SpeexJitter *jitter, char *packet, int len, int timestamp);
|
||||
|
||||
/** Get one packet from the jitter buffer */
|
||||
void speex_jitter_get(SpeexJitter *jitter, spx_int16_t *out, int *start_offset);
|
||||
|
||||
/** Get pointer timestamp of jitter buffer */
|
||||
int speex_jitter_get_pointer_timestamp(SpeexJitter *jitter);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/* @} */
|
||||
@@ -1,250 +0,0 @@
|
||||
/***************************************************************************
|
||||
Copyright (C) 2004-2006 by Jean-Marc Valin
|
||||
Copyright (C) 2006 Commonwealth Scientific and Industrial Research
|
||||
Organisation (CSIRO) Australia
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
|
||||
- Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
- Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
- Neither the name of the Xiph.org Foundation nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
|
||||
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h> /* close() */
|
||||
#include <string.h> /* memset() */
|
||||
|
||||
#include "alsa_device.h"
|
||||
#include <speex/speex.h>
|
||||
#include <speex/speex_jitter.h>
|
||||
#include <speex/speex_preprocess.h>
|
||||
#include <speex/speex_echo.h>
|
||||
#include "speex_jitter_buffer.h"
|
||||
|
||||
#include <sched.h>
|
||||
|
||||
#define MAX_MSG 1500
|
||||
|
||||
#define SAMPLING_RATE 16000
|
||||
#define FRAME_SIZE 320
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
int sd, rc, n;
|
||||
int i;
|
||||
struct sockaddr_in cliAddr, remoteAddr;
|
||||
char msg[MAX_MSG];
|
||||
struct hostent *h;
|
||||
int local_port, remote_port;
|
||||
int nfds;
|
||||
struct pollfd *pfds;
|
||||
SpeexPreprocessState *preprocess;
|
||||
AlsaDevice *audio_dev;
|
||||
int tmp;
|
||||
|
||||
if (argc != 5)
|
||||
{
|
||||
fprintf(stderr, "wrong options\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
h = gethostbyname(argv[2]);
|
||||
if(h==NULL) {
|
||||
fprintf(stderr, "%s: unknown host '%s' \n", argv[0], argv[1]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
local_port = atoi(argv[3]);
|
||||
remote_port = atoi(argv[4]);
|
||||
|
||||
printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name,
|
||||
inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
|
||||
|
||||
{
|
||||
remoteAddr.sin_family = h->h_addrtype;
|
||||
memcpy((char *) &remoteAddr.sin_addr.s_addr,
|
||||
h->h_addr_list[0], h->h_length);
|
||||
remoteAddr.sin_port = htons(remote_port);
|
||||
}
|
||||
/* socket creation */
|
||||
sd=socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if(sd<0) {
|
||||
printf("%s: cannot open socket \n",argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* bind any port */
|
||||
cliAddr.sin_family = AF_INET;
|
||||
cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
cliAddr.sin_port = htons(local_port);
|
||||
|
||||
rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
|
||||
if(rc<0) {
|
||||
printf("%s: cannot bind port\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Setup audio device */
|
||||
audio_dev = alsa_device_open(argv[1], SAMPLING_RATE, 1, FRAME_SIZE);
|
||||
|
||||
/* Setup the encoder and decoder in wideband */
|
||||
void *enc_state, *dec_state;
|
||||
enc_state = speex_encoder_init(&speex_wb_mode);
|
||||
tmp = 8;
|
||||
speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &tmp);
|
||||
tmp = 2;
|
||||
speex_encoder_ctl(enc_state, SPEEX_SET_COMPLEXITY, &tmp);
|
||||
dec_state = speex_decoder_init(&speex_wb_mode);
|
||||
tmp = 1;
|
||||
speex_decoder_ctl(dec_state, SPEEX_SET_ENH, &tmp);
|
||||
SpeexBits enc_bits, dec_bits;
|
||||
speex_bits_init(&enc_bits);
|
||||
speex_bits_init(&dec_bits);
|
||||
|
||||
|
||||
struct sched_param param;
|
||||
/*param.sched_priority = 40; */
|
||||
param.sched_priority = sched_get_priority_min(SCHED_FIFO);
|
||||
if (sched_setscheduler(0,SCHED_FIFO,¶m))
|
||||
perror("sched_setscheduler");
|
||||
|
||||
int send_timestamp = 0;
|
||||
int recv_started=0;
|
||||
|
||||
/* Setup all file descriptors for poll()ing */
|
||||
nfds = alsa_device_nfds(audio_dev);
|
||||
pfds = malloc(sizeof(*pfds)*(nfds+1));
|
||||
alsa_device_getfds(audio_dev, pfds, nfds);
|
||||
pfds[nfds].fd = sd;
|
||||
pfds[nfds].events = POLLIN;
|
||||
|
||||
/* Setup jitter buffer using decoder */
|
||||
SpeexJitter jitter;
|
||||
speex_jitter_init(&jitter, dec_state, SAMPLING_RATE);
|
||||
|
||||
/* Echo canceller with 200 ms tail length */
|
||||
SpeexEchoState *echo_state = speex_echo_state_init(FRAME_SIZE, 10*FRAME_SIZE);
|
||||
tmp = SAMPLING_RATE;
|
||||
speex_echo_ctl(echo_state, SPEEX_ECHO_SET_SAMPLING_RATE, &tmp);
|
||||
|
||||
/* Setup preprocessor and associate with echo canceller for residual echo suppression */
|
||||
preprocess = speex_preprocess_state_init(FRAME_SIZE, SAMPLING_RATE);
|
||||
speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_ECHO_STATE, echo_state);
|
||||
|
||||
alsa_device_start(audio_dev);
|
||||
|
||||
/* Infinite loop on capture, playback and receiving packets */
|
||||
while (1)
|
||||
{
|
||||
/* Wait for either 1) capture 2) playback 3) socket data */
|
||||
poll(pfds, nfds+1, -1);
|
||||
/* Received packets */
|
||||
if (pfds[nfds].revents & POLLIN)
|
||||
{
|
||||
/*fprintf (stderr, "x");*/
|
||||
n = recv(sd, msg, MAX_MSG, 0);
|
||||
int recv_timestamp = ((int*)msg)[1];
|
||||
int payload = ((int*)msg)[0];
|
||||
|
||||
if ((payload & 0x80000000) == 0)
|
||||
{
|
||||
/* Put content of the packet into the jitter buffer, except for the pseudo-header */
|
||||
speex_jitter_put(&jitter, msg+8, n-8, recv_timestamp);
|
||||
recv_started = 1;
|
||||
}
|
||||
|
||||
}
|
||||
/* Ready to play a frame (playback) */
|
||||
if (alsa_device_playback_ready(audio_dev, pfds, nfds))
|
||||
{
|
||||
short pcm[FRAME_SIZE];
|
||||
if (recv_started)
|
||||
{
|
||||
/* Get audio from the jitter buffer */
|
||||
speex_jitter_get(&jitter, pcm, NULL);
|
||||
} else {
|
||||
for (i=0;i<FRAME_SIZE;i++)
|
||||
pcm[i] = 0;
|
||||
}
|
||||
/* Playback the audio and reset the echo canceller if we got an underrun */
|
||||
if (alsa_device_write(audio_dev, pcm, FRAME_SIZE))
|
||||
speex_echo_state_reset(echo_state);
|
||||
/* Put frame into playback buffer */
|
||||
speex_echo_playback(echo_state, pcm);
|
||||
}
|
||||
/* Audio available from the soundcard (capture) */
|
||||
if (alsa_device_capture_ready(audio_dev, pfds, nfds))
|
||||
{
|
||||
short pcm[FRAME_SIZE], pcm2[FRAME_SIZE];
|
||||
char outpacket[MAX_MSG];
|
||||
/* Get audio from the soundcard */
|
||||
alsa_device_read(audio_dev, pcm, FRAME_SIZE);
|
||||
|
||||
/* Perform echo cancellation */
|
||||
speex_echo_capture(echo_state, pcm, pcm2);
|
||||
for (i=0;i<FRAME_SIZE;i++)
|
||||
pcm[i] = pcm2[i];
|
||||
|
||||
speex_bits_reset(&enc_bits);
|
||||
|
||||
/* Apply noise/echo suppression */
|
||||
speex_preprocess_run(preprocess, pcm);
|
||||
|
||||
/* Encode */
|
||||
speex_encode_int(enc_state, pcm, &enc_bits);
|
||||
int packetSize = speex_bits_write(&enc_bits, outpacket+8, MAX_MSG);
|
||||
|
||||
/* Pseudo header: four null bytes and a 32-bit timestamp */
|
||||
((int*)outpacket)[0] = htonl(0);
|
||||
((int*)outpacket)[1] = send_timestamp;
|
||||
send_timestamp += FRAME_SIZE;
|
||||
rc = sendto(sd, outpacket, packetSize+8, 0,
|
||||
(struct sockaddr *) &remoteAddr,
|
||||
sizeof(remoteAddr));
|
||||
|
||||
if(rc<0) {
|
||||
printf("cannot send audio data\n");
|
||||
close(sd);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user