-- cgit v1.2.3 From c902d7fb4e0f2b086030f384ce217679775ca2d4 Mon Sep 17 00:00:00 2001 From: Eric Laurent Date: Fri, 8 Mar 2013 14:50:45 -0800 Subject: Initial version of tinycompress From git://git.alsa-project.org/tinycompress.git 0765f97a Change-Id: I34599092e8c764ecb3475883d1d46cd9c9b5c439 --- Android.mk | 22 ++ MODULE_LICENSE_BSD | 0 NOTICE | 209 ++++++++++++++ README | 30 ++ compress.c | 539 ++++++++++++++++++++++++++++++++++++ cplay.c | 368 ++++++++++++++++++++++++ include/tinycompress/tinycompress.h | 270 ++++++++++++++++++ include/tinycompress/tinymp3.h | 106 +++++++ include/tinycompress/version.h | 70 +++++ 9 files changed, 1614 insertions(+) create mode 100644 Android.mk create mode 100644 MODULE_LICENSE_BSD create mode 100644 NOTICE create mode 100644 README create mode 100644 compress.c create mode 100644 cplay.c create mode 100644 include/tinycompress/tinycompress.h create mode 100644 include/tinycompress/tinymp3.h create mode 100644 include/tinycompress/version.h diff --git a/Android.mk b/Android.mk new file mode 100644 index 0000000..b9c52e2 --- /dev/null +++ b/Android.mk @@ -0,0 +1,22 @@ +LOCAL_PATH:= $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_C_INCLUDES:= $(LOCAL_PATH)/include +LOCAL_SRC_FILES:= compress.c +LOCAL_MODULE := libtinycompress +LOCAL_SHARED_LIBRARIES:= libcutils libutils +LOCAL_MODULE_TAGS := optional +LOCAL_PRELINK_MODULE := false + +include $(BUILD_SHARED_LIBRARY) + +include $(CLEAR_VARS) + +LOCAL_C_INCLUDES:= $(LOCAL_PATH)/include +LOCAL_SRC_FILES:= cplay.c +LOCAL_MODULE := cplay +LOCAL_SHARED_LIBRARIES:= libcutils libutils libtinycompress +LOCAL_MODULE_TAGS := optional + +include $(BUILD_EXECUTABLE) + diff --git a/MODULE_LICENSE_BSD b/MODULE_LICENSE_BSD new file mode 100644 index 0000000..e69de29 diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..ba3e45d --- /dev/null +++ b/NOTICE @@ -0,0 +1,209 @@ +BSD LICENSE + +tinycompress library for compress audio offload in alsa +Copyright (c) 2011-2012, Intel Corporation +All rights reserved. + +Author: Vinod Koul + +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 Intel Corporation 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 COPYRIGHT OWNER 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. + +LGPL LICENSE + +tinycompress library for compress audio offload in alsa +Copyright (c) 2011-2012, Intel Corporation. + + +This program is free software; you can redistribute it and/or modify it +under the terms and conditions of the GNU Lesser General Public License, +version 2.1, as published by the Free Software Foundation. + +This program is distributed in the hope it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program; if not, write to +the Free Software Foundation, Inc., +51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + +=============================================================================== + +BSD LICENSE + +tinyplay command line player for compress audio offload in alsa +Copyright (c) 2011-2012, Intel Corporation +All rights reserved. + +Author: Vinod Koul + +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 Intel Corporation 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 COPYRIGHT OWNER 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. + +LGPL LICENSE + +tinyplay command line player for compress audio offload in alsa +Copyright (c) 2011-2012, Intel Corporation. + +This program is free software; you can redistribute it and/or modify it +under the terms and conditions of the GNU Lesser General Public License, +version 2.1, as published by the Free Software Foundation. + +This program is distributed in the hope it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program; if not, write to +the Free Software Foundation, Inc., +51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + +=============================================================================== + +BSD LICENSE + +mp3 header and prasing +Copyright (c) 2011-2012, Intel Corporation +All rights reserved. + +Author: Vinod Koul + +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 Intel Corporation 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 COPYRIGHT OWNER 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. + +LGPL LICENSE + +mp3 header and parsing +Copyright (c) 2011-2012, Intel Corporation. + + +This program is free software; you can redistribute it and/or modify it +under the terms and conditions of the GNU Lesser General Public License, +version 2.1, as published by the Free Software Foundation. + +This program is distributed in the hope it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program; if not, write to +the Free Software Foundation, Inc., +51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + +=============================================================================== + +BSD LICENSE + +Copyright (c) 2011-2012, Intel Corporation +All rights reserved. + +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 Intel Corporation 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 COPYRIGHT OWNER 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. + +LGPL LICENSE + +tinycompress library for compress audio offload in alsa +Copyright (c) 2011-2012, Intel Corporation. + + +This program is free software; you can redistribute it and/or modify it +under the terms and conditions of the GNU Lesser General Public License, +version 2.1, as published by the Free Software Foundation. + +This program is distributed in the hope it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program; if not, write to +the Free Software Foundation, Inc., +51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. diff --git a/README b/README new file mode 100644 index 0000000..73c19a6 --- /dev/null +++ b/README @@ -0,0 +1,30 @@ + README for tinycompress + ======================= + vinod.koul@linux.intel.com + ========================== + +1. WHAT + tinycompress is a userspace library for anyone who wants to use the ALSA +compressed APIs introduced in Linux 3.3 +This library provides the APIs to open a ALSA compressed device and read/write +compressed data like MP3 etc to it. + + This also includes a utility command line player (cplay) which demonstrates +the usage of this API. Currently this contains support for playing the mp3 format + +2. WHERE + The library can found in alsa-project.org +Git: git clone git://git.alsa-project.org/tinycompress.git +Http: http://git.alsa-project.org/?p=tinycompress.git + +3. PATCHES + Please send any enhancements/fixes to alsa developer mailing list at: +alsa-devel@alsa-project.org. + +4. LICENSE + tinycompress is provided under LGPL and BSD dual license + +5. CREDITS +- Pierre-Louis Bossart for library design +- Navjot Singh for writing the mp3 parser code + diff --git a/compress.c b/compress.c new file mode 100644 index 0000000..5398e10 --- /dev/null +++ b/compress.c @@ -0,0 +1,539 @@ +/* + * BSD LICENSE + * + * tinycompress library for compress audio offload in alsa + * Copyright (c) 2011-2012, Intel Corporation + * All rights reserved. + * + * Author: Vinod Koul + * + * 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 Intel Corporation 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 COPYRIGHT OWNER 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. + * + * LGPL LICENSE + * + * tinycompress library for compress audio offload in alsa + * Copyright (c) 2011-2012, Intel Corporation. + * + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to + * the Free Software Foundation, Inc., + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#define __force +#define __bitwise +#define __user +#include +#include +#include +#include + +#define COMPR_ERR_MAX 128 + +/* Default maximum time we will wait in a poll() - 20 seconds */ +#define DEFAULT_MAX_POLL_WAIT_MS 20000 + +struct compress { + int fd; + unsigned int flags; + char error[COMPR_ERR_MAX]; + struct compr_config *config; + int running; + int max_poll_wait_ms; + unsigned int gapless_metadata; + unsigned int next_track; +}; + +static int oops(struct compress *compress, int e, const char *fmt, ...) +{ + va_list ap; + int sz; + + va_start(ap, fmt); + vsnprintf(compress->error, COMPR_ERR_MAX, fmt, ap); + va_end(ap); + sz = strlen(compress->error); + + if (errno) + snprintf(compress->error + sz, COMPR_ERR_MAX - sz, + ": %s", strerror(e)); + return e; +} + +const char *compress_get_error(struct compress *compress) +{ + return compress->error; +} +static struct compress bad_compress = { + .fd = -1, +}; + +int is_compress_running(struct compress *compress) +{ + return ((compress->fd > 0) && compress->running) ? 1 : 0; +} + +int is_compress_ready(struct compress *compress) +{ + return (compress->fd > 0) ? 1 : 0; +} + +static int get_compress_version(struct compress *compress) +{ + int version = 0; + + if (ioctl(compress->fd, SNDRV_COMPRESS_IOCTL_VERSION, &version)) { + oops(compress, errno, "cant read version"); + return -1; + } + return version; +} + +static bool _is_codec_supported(struct compress *compress, struct compr_config *config, + const struct snd_compr_caps *caps) +{ + bool codec = false; + unsigned int i; + + for (i = 0; i < caps->num_codecs; i++) { + if (caps->codecs[i] == config->codec->id) { + /* found the codec */ + codec = true; + break; + } + } + if (codec == false) { + oops(compress, -ENXIO, "this codec is not supported"); + return false; + } + + if (config->fragment_size < caps->min_fragment_size) { + oops(compress, -EINVAL, "requested fragment size %d is below min supported %d", + config->fragment_size, caps->min_fragment_size); + return false; + } + if (config->fragment_size > caps->max_fragment_size) { + oops(compress, -EINVAL, "requested fragment size %d is above max supported %d", + config->fragment_size, caps->max_fragment_size); + return false; + } + if (config->fragments < caps->min_fragments) { + oops(compress, -EINVAL, "requested fragments %d are below min supported %d", + config->fragments, caps->min_fragments); + return false; + } + if (config->fragments > caps->max_fragments) { + oops(compress, -EINVAL, "requested fragments %d are above max supported %d", + config->fragments, caps->max_fragments); + return false; + } + + /* TODO: match the codec properties */ + return true; +} + +static bool _is_codec_type_supported(int fd, struct snd_codec *codec) +{ + struct snd_compr_caps caps; + bool found = false; + unsigned int i; + + if (ioctl(fd, SNDRV_COMPRESS_GET_CAPS, &caps)) { + oops(&bad_compress, errno, "cannot get device caps"); + return false; + } + + for (i = 0; i < caps.num_codecs; i++) { + if (caps.codecs[i] == codec->id) { + /* found the codec */ + found = true; + break; + } + } + /* TODO: match the codec properties */ + return found; +} + +static inline void +fill_compress_params(struct compr_config *config, struct snd_compr_params *params) +{ + params->buffer.fragment_size = config->fragment_size; + params->buffer.fragments = config->fragments; + memcpy(¶ms->codec, config->codec, sizeof(params->codec)); +} + +struct compress *compress_open(unsigned int card, unsigned int device, + unsigned int flags, struct compr_config *config) +{ + struct compress *compress; + struct snd_compr_params params; + struct snd_compr_caps caps; + char fn[256]; + + compress = calloc(1, sizeof(struct compress)); + if (!compress || !config) { + oops(&bad_compress, errno, "cannot allocate compress object"); + return &bad_compress; + } + + compress->next_track = 0; + compress->gapless_metadata = 0; + compress->config = calloc(1, sizeof(*config)); + if (!compress->config) + goto input_fail; + memcpy(compress->config, config, sizeof(*compress->config)); + + snprintf(fn, sizeof(fn), "/dev/snd/comprC%uD%u", card, device); + + compress->max_poll_wait_ms = DEFAULT_MAX_POLL_WAIT_MS; + + compress->flags = flags; + if (!((flags & COMPRESS_OUT) || (flags & COMPRESS_IN))) { + oops(&bad_compress, -EINVAL, "can't deduce device direction from given flags"); + goto config_fail; + } + if (flags & COMPRESS_OUT) { + /* this should be removed once we have capture tested */ + oops(&bad_compress, -EINVAL, "this version doesnt support capture"); + goto config_fail; + } + + compress->fd = open(fn, O_WRONLY); + if (compress->fd < 0) { + oops(&bad_compress, errno, "cannot open device '%s'", fn); + goto config_fail; + } + + if (ioctl(compress->fd, SNDRV_COMPRESS_GET_CAPS, &caps)) { + oops(compress, errno, "cannot get device caps"); + goto codec_fail; + } + + /* If caller passed "don't care" fill in default values */ + if ((config->fragment_size == 0) || (config->fragments == 0)) { + config->fragment_size = caps.min_fragment_size; + config->fragments = caps.max_fragments; + } + +#if 0 + /* FIXME need to turn this On when DSP supports + * and treat in no support case + */ + if (_is_codec_supported(compress, config, &caps) == false) { + oops(compress, errno, "codec not supported\n"); + goto codec_fail; + } +#endif + fill_compress_params(config, ¶ms); + + if (ioctl(compress->fd, SNDRV_COMPRESS_SET_PARAMS, ¶ms)) { + oops(&bad_compress, errno, "cannot set device"); + goto codec_fail; + } + + return compress; + +codec_fail: + close(compress->fd); + compress->fd = -1; +config_fail: + free(compress->config); +input_fail: + free(compress); + return &bad_compress; +} + +void compress_close(struct compress *compress) +{ + if (compress == &bad_compress) + return; + + if (compress->fd >= 0) + close(compress->fd); + compress->running = 0; + compress->fd = -1; + free(compress->config); + free(compress); +} + +int compress_get_hpointer(struct compress *compress, + unsigned int *avail, struct timespec *tstamp) +{ + struct snd_compr_avail kavail; + __u64 time; + + if (!is_compress_ready(compress)) + return oops(compress, -ENODEV, "device not ready"); + + if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &kavail)) + return oops(compress, errno, "cannot get avail"); + if (0 == kavail.tstamp.sampling_rate) + return oops(compress, errno, "invalid paramter"); + *avail = (unsigned int)kavail.avail; + time = kavail.tstamp.pcm_io_frames / kavail.tstamp.sampling_rate; + tstamp->tv_sec = time; + time = kavail.tstamp.pcm_io_frames % kavail.tstamp.sampling_rate; + tstamp->tv_nsec = time * 1000000000 / kavail.tstamp.sampling_rate; + return 0; +} + +int compress_get_tstamp(struct compress *compress, + unsigned long *samples, unsigned int *sampling_rate) +{ + struct snd_compr_tstamp ktstamp; + + if (!is_compress_ready(compress)) + return oops(compress, -ENODEV, "device not ready"); + + if (ioctl(compress->fd, SNDRV_COMPRESS_TSTAMP, &ktstamp)) + return oops(compress, errno, "cannot get tstamp"); + + *samples = ktstamp.pcm_io_frames; + *sampling_rate = ktstamp.sampling_rate; + return 0; +} + +int compress_write(struct compress *compress, const void *buf, unsigned int size) +{ + struct snd_compr_avail avail; + struct pollfd fds; + int to_write = 0; /* zero indicates we haven't written yet */ + int written, total = 0, ret; + const char* cbuf = buf; + const unsigned int frag_size = compress->config->fragment_size; + + if (!(compress->flags & COMPRESS_IN)) + return oops(compress, -EINVAL, "Invalid flag set"); + if (!is_compress_ready(compress)) + return oops(compress, -ENODEV, "device not ready"); + fds.fd = compress->fd; + fds.events = POLLOUT; + + /*TODO: treat auto start here first */ + while (size) { + if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &avail)) + return oops(compress, errno, "cannot get avail"); + + if ( (avail.avail < frag_size) + || ((to_write != 0) && (avail.avail < size)) ) { + /* not enough space for one fragment, or we have done + * a short write and there isn't enough space for all + * the remaining data + */ + ret = poll(&fds, 1, compress->max_poll_wait_ms); + /* A pause will cause -EBADFD or zero. + * This is not an error, just stop writing */ + if ((ret == 0) || (ret == -EBADFD)) + break; + if (ret < 0) + return oops(compress, errno, "poll error"); + if (fds.revents & POLLOUT) { + continue; + } + if (fds.revents & POLLERR) { + return oops(compress, -EIO, "poll returned error!"); + } + } + /* write avail bytes */ + if (size > avail.avail) + to_write = avail.avail; + else + to_write = size; + written = write(compress->fd, cbuf, to_write); + /* If play was paused the write returns -EBADFD */ + if (written == -EBADFD) + break; + if (written < 0) + return oops(compress, errno, "write failed!"); + + size -= written; + cbuf += written; + total += written; + } + return total; +} + +int compress_read(struct compress *compress, void *buf, unsigned int size) +{ + return oops(compress, -ENOTTY, "Not supported yet in lib"); +} + +int compress_start(struct compress *compress) +{ + if (!is_compress_ready(compress)) + return oops(compress, -ENODEV, "device not ready"); + if (ioctl(compress->fd, SNDRV_COMPRESS_START)) + return oops(compress, errno, "cannot start the stream"); + compress->running = 1; + return 0; + +} + +int compress_stop(struct compress *compress) +{ + if (!is_compress_running(compress)) + return oops(compress, -ENODEV, "device not ready"); + if (ioctl(compress->fd, SNDRV_COMPRESS_STOP)) + return oops(compress, errno, "cannot stop the stream"); + return 0; +} + +int compress_pause(struct compress *compress) +{ + if (!is_compress_running(compress)) + return oops(compress, -ENODEV, "device not ready"); + if (ioctl(compress->fd, SNDRV_COMPRESS_PAUSE)) + return oops(compress, errno, "cannot pause the stream"); + return 0; +} + +int compress_resume(struct compress *compress) +{ + if (ioctl(compress->fd, SNDRV_COMPRESS_RESUME)) + return oops(compress, errno, "cannot resume the stream"); + return 0; +} + +int compress_drain(struct compress *compress) +{ + if (!is_compress_running(compress)) + return oops(compress, -ENODEV, "device not ready"); + if (ioctl(compress->fd, SNDRV_COMPRESS_DRAIN)) + return oops(compress, errno, "cannot drain the stream"); + return 0; +} + +int compress_partial_drain(struct compress *compress) +{ + if (!is_compress_running(compress)) + return oops(compress, -ENODEV, "device not ready"); + + if (!compress->next_track) + return oops(compress, -EPERM, "next track not signalled"); + if (ioctl(compress->fd, SNDRV_COMPRESS_PARTIAL_DRAIN)) + return oops(compress, errno, "cannot drain the stream\n"); + compress->next_track = 0; + return 0; +} + +int compress_next_track(struct compress *compress) +{ + if (!is_compress_running(compress)) + return oops(compress, -ENODEV, "device not ready"); + + if (!compress->gapless_metadata) + return oops(compress, -EPERM, "metadata not set"); + if (ioctl(compress->fd, SNDRV_COMPRESS_NEXT_TRACK)) + return oops(compress, errno, "cannot set next track\n"); + compress->next_track = 1; + compress->gapless_metadata = 0; + return 0; +} + +int compress_set_gapless_metadata(struct compress *compress, + struct compr_gapless_mdata *mdata) +{ + struct snd_compr_metadata metadata; + int version; + + if (!is_compress_ready(compress)) + return oops(compress, -ENODEV, "device not ready"); + + version = get_compress_version(compress); + if (version <= 0) + return -1; + + if (version < SNDRV_PROTOCOL_VERSION(0, 1, 1)) + return oops(compress, -ENXIO, "gapless apis not supported in kernel"); + + metadata.key = SNDRV_COMPRESS_ENCODER_PADDING; + metadata.value[0] = mdata->encoder_padding; + if (ioctl(compress->fd, SNDRV_COMPRESS_SET_METADATA, &metadata)) + return oops(compress, errno, "can't set metadata for stream\n"); + + metadata.key = SNDRV_COMPRESS_ENCODER_DELAY; + metadata.value[0] = mdata->encoder_delay; + if (ioctl(compress->fd, SNDRV_COMPRESS_SET_METADATA, &metadata)) + return oops(compress, errno, "can't set metadata for stream\n"); + compress->gapless_metadata = 1; + return 0; +} + +bool is_codec_supported(unsigned int card, unsigned int device, + unsigned int flags, struct snd_codec *codec) +{ + unsigned int dev_flag; + bool ret; + int fd; + char fn[256]; + + snprintf(fn, sizeof(fn), "/dev/snd/comprC%uD%u", card, device); + + if (flags & COMPRESS_OUT) + dev_flag = O_RDONLY; + else + dev_flag = O_WRONLY; + + fd = open(fn, dev_flag); + if (fd < 0) + return oops(&bad_compress, errno, "cannot open device '%s'", fn); + + ret = _is_codec_type_supported(fd, codec); + + close(fd); + return ret; +} + +void compress_set_max_poll_wait(struct compress *compress, int milliseconds) +{ + compress->max_poll_wait_ms = milliseconds; +} + diff --git a/cplay.c b/cplay.c new file mode 100644 index 0000000..b11d676 --- /dev/null +++ b/cplay.c @@ -0,0 +1,368 @@ +/* + * BSD LICENSE + * + * tinyplay command line player for compress audio offload in alsa + * Copyright (c) 2011-2012, Intel Corporation + * All rights reserved. + * + * Author: Vinod Koul + * + * 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 Intel Corporation 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 COPYRIGHT OWNER 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. + * + * LGPL LICENSE + * + * tinyplay command line player for compress audio offload in alsa + * Copyright (c) 2011-2012, Intel Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to + * the Free Software Foundation, Inc., + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define __force +#define __bitwise +#define __user +#include +#include +#include + +static int verbose; + +static void usage(void) +{ + fprintf(stderr, "usage: cplay [OPTIONS] filename\n" + "-c\tcard number\n" + "-d\tdevice node\n" + "-b\tbuffer size\n" + "-f\tfragments\n\n" + "-v\tverbose mode\n" + "-h\tPrints this help list\n\n" + "Example:\n" + "\tcplay -c 1 -d 2 test.mp3\n" + "\tcplay -f 5 test.mp3\n"); + + exit(EXIT_FAILURE); +} + +void play_samples(char *name, unsigned int card, unsigned int device, + unsigned long buffer_size, unsigned int frag); + +struct mp3_header { + uint16_t sync; + uint8_t format1; + uint8_t format2; +}; + +int parse_mp3_header(struct mp3_header *header, unsigned int *num_channels, + unsigned int *sample_rate, unsigned int *bit_rate) +{ + int ver_idx, mp3_version, layer, bit_rate_idx, sample_rate_idx, channel_idx; + + /* check sync bits */ + if ((header->sync & MP3_SYNC) != MP3_SYNC) { + fprintf(stderr, "Error: Can't find sync word\n"); + return -1; + } + ver_idx = (header->sync >> 11) & 0x03; + mp3_version = ver_idx == 0 ? MPEG25 : ((ver_idx & 0x1) ? MPEG1 : MPEG2); + layer = 4 - ((header->sync >> 9) & 0x03); + bit_rate_idx = ((header->format1 >> 4) & 0x0f); + sample_rate_idx = ((header->format1 >> 2) & 0x03); + channel_idx = ((header->format2 >> 6) & 0x03); + + if (sample_rate_idx == 3 || layer == 4 || bit_rate_idx == 15) { + fprintf(stderr, "Error: Can't find valid header\n"); + return -1; + } + *num_channels = (channel_idx == MONO ? 1 : 2); + *sample_rate = mp3_sample_rates[mp3_version][sample_rate_idx]; + *bit_rate = (mp3_bit_rates[mp3_version][layer - 1][bit_rate_idx]) * 1000; + if (verbose) + printf("%s: exit\n", __func__); + return 0; +} + +int check_codec_format_supported(unsigned int card, unsigned int device, struct snd_codec *codec) +{ + if (is_codec_supported(card, device, COMPRESS_IN, codec) == false) { + fprintf(stderr, "Error: This codec or format is not supported by DSP\n"); + return -1; + } + return 0; +} + +static int print_time(struct compress *compress) +{ + unsigned int avail; + struct timespec tstamp; + + if (compress_get_hpointer(compress, &avail, &tstamp) != 0) { + fprintf(stderr, "Error querying timestamp\n"); + fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); + return -1; + } else + fprintf(stderr, "DSP played %jd.%jd\n", (intmax_t)tstamp.tv_sec, (intmax_t)tstamp.tv_nsec*1000); + return 0; +} + +int main(int argc, char **argv) +{ + char *file; + unsigned long buffer_size = 0; + int c; + unsigned int card = 0, device = 0, frag = 0; + + + if (argc < 2) + usage(); + + verbose = 0; + while ((c = getopt(argc, argv, "hvb:f:c:d:")) != -1) { + switch (c) { + case 'h': + usage(); + break; + case 'b': + buffer_size = strtol(optarg, NULL, 0); + break; + case 'f': + frag = strtol(optarg, NULL, 10); + break; + case 'c': + card = strtol(optarg, NULL, 10); + break; + case 'd': + device = strtol(optarg, NULL, 10); + break; + case 'v': + verbose = 1; + break; + default: + exit(EXIT_FAILURE); + } + } + if (optind >= argc) + usage(); + + file = argv[optind]; + + play_samples(file, card, device, buffer_size, frag); + + fprintf(stderr, "Finish Playing.... Close Normally\n"); + exit(EXIT_SUCCESS); +} + +void play_samples(char *name, unsigned int card, unsigned int device, + unsigned long buffer_size, unsigned int frag) +{ + struct compr_config config; + struct snd_codec codec; + struct compress *compress; + struct mp3_header header; + FILE *file; + char *buffer; + int size, num_read, wrote; + unsigned int channels, rate, bits; + + if (verbose) + printf("%s: entry\n", __func__); + file = fopen(name, "rb"); + if (!file) { + fprintf(stderr, "Unable to open file '%s'\n", name); + exit(EXIT_FAILURE); + } + + fread(&header, sizeof(header), 1, file); + + if (parse_mp3_header(&header, &channels, &rate, &bits) == -1) { + fclose(file); + exit(EXIT_FAILURE); + } + + codec.id = SND_AUDIOCODEC_MP3; + codec.ch_in = channels; + codec.ch_out = channels; + switch (rate) { + case 5512: + codec.sample_rate = SNDRV_PCM_RATE_5512; + break; + case 8000: + codec.sample_rate = SNDRV_PCM_RATE_8000; + break; + case 11025: + codec.sample_rate = SNDRV_PCM_RATE_11025; + break; + case 16000: + codec.sample_rate = SNDRV_PCM_RATE_16000; + break; + case 220500: + codec.sample_rate = SNDRV_PCM_RATE_22050; + break; + case 32000: + codec.sample_rate = SNDRV_PCM_RATE_32000; + break; + case 44100: + codec.sample_rate = SNDRV_PCM_RATE_44100; + break; + case 48000: + codec.sample_rate = SNDRV_PCM_RATE_48000; + break; + case 64000: + codec.sample_rate = SNDRV_PCM_RATE_64000; + break; + case 88200: + codec.sample_rate = SNDRV_PCM_RATE_88200; + break; + case 96000: + codec.sample_rate = SNDRV_PCM_RATE_96000; + break; + case 176400: + codec.sample_rate = SNDRV_PCM_RATE_176400; + break; + case 192000: + codec.sample_rate = SNDRV_PCM_RATE_192000; + break; + default: + fprintf(stderr, "unknown sample rate %d\n", rate); + goto FILE_EXIT; + } + codec.bit_rate = bits; + codec.rate_control = 0; + codec.profile = 0; + codec.level = 0; + codec.ch_mode = 0; + codec.format = 0; + if ((buffer_size != 0) && (frag != 0)) { + config.fragment_size = buffer_size/frag; + config.fragments = frag; + } else { + /* use driver defaults */ + config.fragment_size = 0; + config.fragments = 0; + } + config.codec = &codec; + + compress = compress_open(card, device, COMPRESS_IN, &config); + if (!compress || !is_compress_ready(compress)) { + fprintf(stderr, "Unable to open Compress device %d:%d\n", + card, device); + fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); + goto FILE_EXIT; + }; + if (verbose) + printf("%s: Opened compress device\n", __func__); + size = config.fragment_size; + buffer = malloc(size * config.fragments); + if (!buffer) { + fprintf(stderr, "Unable to allocate %d bytes\n", size); + goto COMP_EXIT; + } + + /* we will write frag fragment_size and then start */ + num_read = fread(buffer, 1, size * config.fragments, file); + if (num_read > 0) { + if (verbose) + printf("%s: Doing first buffer write of %d\n", __func__, num_read); + wrote = compress_write(compress, buffer, num_read); + if (wrote < 0) { + fprintf(stderr, "Error %d playing sample\n", wrote); + fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); + goto BUF_EXIT; + } + if (wrote != num_read) { + /* TODO: Buufer pointer needs to be set here */ + fprintf(stderr, "We wrote %d, DSP accepted %d\n", num_read, wrote); + } + } + printf("Playing file %s On Card %u device %u, with buffer of %lu bytes\n", + name, card, device, buffer_size); + printf("Format %u Channels %u, %u Hz, Bit Rate %d\n", + SND_AUDIOCODEC_MP3, channels, rate, bits); + + compress_start(compress); + if (verbose) + printf("%s: You should hear audio NOW!!!\n", __func__); + + do { + num_read = fread(buffer, 1, size, file); + if (num_read > 0) { + wrote = compress_write(compress, buffer, num_read); + if (wrote < 0) { + fprintf(stderr, "Error playing sample\n"); + fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); + goto BUF_EXIT; + } + if (wrote != num_read) { + /* TODO: Buufer pointer needs to be set here */ + fprintf(stderr, "We wrote %d, DSP accepted %d\n", num_read, wrote); + } + if (verbose) { + print_time(compress); + printf("%s: wrote %d\n", __func__, wrote); + } + } + } while (num_read > 0); + + if (verbose) + printf("%s: exit sucess\n", __func__); + /* issue drain if it supports */ + compress_drain(compress); + free(buffer); + fclose(file); + compress_close(compress); + return; +BUF_EXIT: + free(buffer); +COMP_EXIT: + compress_close(compress); +FILE_EXIT: + fclose(file); + if (verbose) + printf("%s: exit failure\n", __func__); + exit(EXIT_FAILURE); +} + diff --git a/include/tinycompress/tinycompress.h b/include/tinycompress/tinycompress.h new file mode 100644 index 0000000..ba9f3b6 --- /dev/null +++ b/include/tinycompress/tinycompress.h @@ -0,0 +1,270 @@ +/* + * BSD LICENSE + * + * Copyright (c) 2011-2012, Intel Corporation + * All rights reserved. + * + * 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 Intel Corporation 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 COPYRIGHT OWNER 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. + * + * LGPL LICENSE + * + * tinycompress library for compress audio offload in alsa + * Copyright (c) 2011-2012, Intel Corporation. + * + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to + * the Free Software Foundation, Inc., + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#ifndef __TINYCOMPRESS_H +#define __TINYCOMPRESS_H + +#if defined(__cplusplus) +extern "C" { +#endif +/* + * struct compr_config: config structure, needs to be filled by app + * If fragment_size or fragments are zero, this means "don't care" + * and tinycompress will choose values that the driver supports + * + * @fragment_size: size of fragment requested, in bytes + * @fragments: number of fragments + * @codec: codec type and parameters requested + */ +struct compr_config { + __u32 fragment_size; + __u32 fragments; + struct snd_codec *codec; +}; + +struct compr_gapless_mdata { + __u32 encoder_delay; + __u32 encoder_padding; +}; + +#define COMPRESS_OUT 0x00000000 +#define COMPRESS_IN 0x10000000 + +struct compress; +struct snd_compr_tstamp; + +/* + * compress_open: open a new compress stream + * returns the valid struct compress on success, NULL on failure + * If config does not specify a requested fragment size, on return + * it will be updated with the size and number of fragments that + * were configured + * + * @card: sound card number + * @device: device number + * @flags: device flags can be COMPRESS_OUT or COMPRESS_IN + * @config: stream config requested. Returns actual fragment config + */ +struct compress *compress_open(unsigned int card, unsigned int device, + unsigned int flags, struct compr_config *config); + +/* + * compress_close: close the compress stream + * + * @compress: compress stream to be closed + */ +void compress_close(struct compress *compress); + +/* + * compress_get_hpointer: get the hw timestamp + * return 0 on success, negative on error + * + * @compress: compress stream on which query is made + * @avail: buffer availble for write/read, in bytes + * @tstamp: hw time + */ +int compress_get_hpointer(struct compress *compress, + unsigned int *avail, struct timespec *tstamp); + + +/* + * compress_get_tstamp: get the raw hw timestamp + * return 0 on success, negative on error + * + * @compress: compress stream on which query is made + * @samples: number of decoded samples played + * @sampling_rate: sampling rate of decoded samples + */ +int compress_get_tstamp(struct compress *compress, + unsigned long *samples, unsigned int *sampling_rate); + +/* + * compress_write: write data to the compress stream + * return bytes written on success, negative on error + * this is a blocking call + * + * @compress: compress stream to be written to + * @buf: pointer to data + * @size: number of bytes to be written + */ +int compress_write(struct compress *compress, const void *buf, unsigned int size); + +/* + * compress_read: read data from the compress stream + * return bytes read on success, negative on error + * + * @compress: compress stream from where data is to be read + * @buf: pointer to data buffer + * @size: size of given buffer + */ +int compress_read(struct compress *compress, void *buf, unsigned int size); + +/* + * compress_start: start the compress stream + * return 0 on success, negative on error + * + * @compress: compress stream to be started + */ +int compress_start(struct compress *compress); + +/* + * compress_stop: stop the compress stream + * return 0 on success, negative on error + * + * @compress: compress stream to be stopped + */ +int compress_stop(struct compress *compress); + +/* + * compress_pause: pause the compress stream + * return 0 on success, negative on error + * + * @compress: compress stream to be paused + */ +int compress_pause(struct compress *compress); + +/* + * compress_resume: resume the compress stream + * return 0 on success, negative on error + * + * @compress: compress stream to be resumed + */ +int compress_resume(struct compress *compress); + +/* + * compress_drain: drain the compress stream + * return 0 on success, negative on error + * + * @compress: compress stream to be drain + */ +int compress_drain(struct compress *compress); + +/* + * compress_next_track: set the next track for stream + * + * return 0 on success, negative on error + * + * @compress: compress stream to be transistioned to next track + */ +int compress_next_track(struct compress *compress); + +/* + * compress_partial_drain: drain will return after the last frame is decoded + * by DSP and will play the , All the data written into compressed + * ring buffer is decoded + * + * return 0 on success, negative on error + * + * @compress: compress stream to be drain + */ +int compress_partial_drain(struct compress *compress); + +/* + * compress_set_gapless_metadata: set gapless metadata of a compress strem + * + * return 0 on success, negative on error + * + * @compress: compress stream for which metadata has to set + * @mdata: metadata encoder delay and padding + */ + +int compress_set_gapless_metadata(struct compress *compress, + struct compr_gapless_mdata *mdata); + +/* + * is_codec_supported:check if the given codec is supported + * returns true when supported, false if not + * + * @card: sound card number + * @device: device number + * @flags: stream flags + * @codec: codec type and parameters to be checked + */ +bool is_codec_supported(unsigned int card, unsigned int device, + unsigned int flags, struct snd_codec *codec); + +/* + * compress_set_max_poll_wait: set the maximum time tinycompress + * will wait for driver to signal a poll(). Interval is in + * milliseconds. + * Pass interval of -1 to disable timeout and make poll() wait + * until driver signals. + * If this function is not used the timeout defaults to 20 seconds. + */ +void compress_set_max_poll_wait(struct compress *compress, int milliseconds); + +int is_compress_running(struct compress *compress); + +int is_compress_ready(struct compress *compress); + +/* Returns a human readable reason for the last error */ +const char *compress_get_error(struct compress *compress); +/* + * since the SNDRV_PCM_RATE_* is not availble anywhere in userspace + * and we have used these to define the sampling rate, we need to define + * then here + */ +#define SNDRV_PCM_RATE_5512 (1<<0) /* 5512Hz */ +#define SNDRV_PCM_RATE_8000 (1<<1) /* 8000Hz */ +#define SNDRV_PCM_RATE_11025 (1<<2) /* 11025Hz */ +#define SNDRV_PCM_RATE_16000 (1<<3) /* 16000Hz */ +#define SNDRV_PCM_RATE_22050 (1<<4) /* 22050Hz */ +#define SNDRV_PCM_RATE_32000 (1<<5) /* 32000Hz */ +#define SNDRV_PCM_RATE_44100 (1<<6) /* 44100Hz */ +#define SNDRV_PCM_RATE_48000 (1<<7) /* 48000Hz */ +#define SNDRV_PCM_RATE_64000 (1<<8) /* 64000Hz */ +#define SNDRV_PCM_RATE_88200 (1<<9) /* 88200Hz */ +#define SNDRV_PCM_RATE_96000 (1<<10) /* 96000Hz */ +#define SNDRV_PCM_RATE_176400 (1<<11) /* 176400Hz */ +#define SNDRV_PCM_RATE_192000 (1<<12) /* 192000Hz */ + +#endif diff --git a/include/tinycompress/tinymp3.h b/include/tinycompress/tinymp3.h new file mode 100644 index 0000000..b7b45e3 --- /dev/null +++ b/include/tinycompress/tinymp3.h @@ -0,0 +1,106 @@ +/* + * BSD LICENSE + * + * mp3 header and prasing + * Copyright (c) 2011-2012, Intel Corporation + * All rights reserved. + * + * Author: Vinod Koul + * + * 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 Intel Corporation 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 COPYRIGHT OWNER 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. + * + * LGPL LICENSE + * + * mp3 header and parsing + * Copyright (c) 2011-2012, Intel Corporation. + * + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to + * the Free Software Foundation, Inc., + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#ifndef __TINYMP3_H +#define __TINYMP3_H + +#if defined(__cplusplus) +extern "C" { +#endif + + +#define MP3_SYNC 0xe0ff + +const int mp3_sample_rates[3][3] = { + {44100, 48000, 32000}, /* MPEG-1 */ + {22050, 24000, 16000}, /* MPEG-2 */ + {11025, 12000, 8000}, /* MPEG-2.5 */ +}; + +const int mp3_bit_rates[3][3][15] = { + { + /* MPEG-1 */ + { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448}, /* Layer 1 */ + { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384}, /* Layer 2 */ + { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}, /* Layer 3 */ + }, + { + /* MPEG-2 */ + { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256}, /* Layer 1 */ + { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}, /* Layer 2 */ + { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}, /* Layer 3 */ + }, + { + /* MPEG-2.5 */ + { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256}, /* Layer 1 */ + { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}, /* Layer 2 */ + { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}, /* Layer 3 */ + }, +}; + +enum mpeg_version { + MPEG1 = 0, + MPEG2 = 1, + MPEG25 = 2 +}; + +enum mp3_stereo_mode { + STEREO = 0x00, + JOINT = 0x01, + DUAL = 0x02, + MONO = 0x03 +}; + +#endif diff --git a/include/tinycompress/version.h b/include/tinycompress/version.h new file mode 100644 index 0000000..795fe77 --- /dev/null +++ b/include/tinycompress/version.h @@ -0,0 +1,70 @@ +/* + * BSD LICENSE + * + * Copyright (c) 2011-2012, Intel Corporation + * All rights reserved. + * + * 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 Intel Corporation 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 COPYRIGHT OWNER 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. + * + * LGPL LICENSE + * + * tinycompress library for compress audio offload in alsa + * Copyright (c) 2011-2012, Intel Corporation. + * + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to + * the Free Software Foundation, Inc., + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#ifndef __VERSION_H +#define __VERSION_H + + +#define TINYCOMPRESS_LIB_MAJOR 0 /* major number of library version */ +#define TINYCOMPRESS_LIB_MINOR 1 /* minor number of library version */ +#define TINYCOMPRESS_LIB_SUBMINOR 0 /* subminor number of library version */ + +/** library version */ +#define TINYCOMPRESS_LIB_VERSION \ + ((TINYCOMPRESS_LIB_MAJOR<<16)|\ + (TINYCOMPRESS_LIB_MINOR<<8)|\ + TINYCOMPRESS_LIB_SUBMINOR) + +/** library version (string) */ +#define TINYCOMPRESS_LIB_VERSION_STR "0.1.0" + +#endif -- cgit v1.2.3 From f0c407825741bcb94952e4264efce66dee378411 Mon Sep 17 00:00:00 2001 From: Eric Laurent Date: Thu, 2 May 2013 18:10:20 -0700 Subject: Update to latest tinycompress 01ff678 compress: Add support for capture streams 8c9d5da tinycompress: cplay: Correct a couple of small message typos Change-Id: Ieee7cce63930eb2f846a7a86a92806f1db45656b --- compress.c | 64 +++++++++++++++++++++++++++++++++---- cplay.c | 4 +-- include/tinycompress/tinycompress.h | 2 +- 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/compress.c b/compress.c index 5398e10..d5115b6 100644 --- a/compress.c +++ b/compress.c @@ -240,13 +240,12 @@ struct compress *compress_open(unsigned int card, unsigned int device, oops(&bad_compress, -EINVAL, "can't deduce device direction from given flags"); goto config_fail; } + if (flags & COMPRESS_OUT) { - /* this should be removed once we have capture tested */ - oops(&bad_compress, -EINVAL, "this version doesnt support capture"); - goto config_fail; + compress->fd = open(fn, O_RDONLY); + } else { + compress->fd = open(fn, O_WRONLY); } - - compress->fd = open(fn, O_WRONLY); if (compress->fd < 0) { oops(&bad_compress, errno, "cannot open device '%s'", fn); goto config_fail; @@ -403,7 +402,60 @@ int compress_write(struct compress *compress, const void *buf, unsigned int size int compress_read(struct compress *compress, void *buf, unsigned int size) { - return oops(compress, -ENOTTY, "Not supported yet in lib"); + struct snd_compr_avail avail; + struct pollfd fds; + int to_read = 0; + int num_read, total = 0, ret; + char* cbuf = buf; + const unsigned int frag_size = compress->config->fragment_size; + + if (!(compress->flags & COMPRESS_OUT)) + return oops(compress, -EINVAL, "Invalid flag set"); + if (!is_compress_ready(compress)) + return oops(compress, -ENODEV, "device not ready"); + fds.fd = compress->fd; + fds.events = POLLIN; + + while (size) { + if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &avail)) + return oops(compress, errno, "cannot get avail"); + + if ( (avail.avail < frag_size) && (avail.avail < size) ) { + /* Less than one fragment available and not at the + * end of the read, so poll + */ + ret = poll(&fds, 1, compress->max_poll_wait_ms); + /* A pause will cause -EBADFD or zero. + * This is not an error, just stop reading */ + if ((ret == 0) || (ret == -EBADFD)) + break; + if (ret < 0) + return oops(compress, errno, "poll error"); + if (fds.revents & POLLIN) { + continue; + } + if (fds.revents & POLLERR) { + return oops(compress, -EIO, "poll returned error!"); + } + } + /* read avail bytes */ + if (size > avail.avail) + to_read = avail.avail; + else + to_read = size; + num_read = read(compress->fd, cbuf, to_read); + /* If play was paused the read returns -EBADFD */ + if (num_read == -EBADFD) + break; + if (num_read < 0) + return oops(compress, errno, "read failed!"); + + size -= num_read; + cbuf += num_read; + total += num_read; + } + + return total; } int compress_start(struct compress *compress) diff --git a/cplay.c b/cplay.c index b11d676..21e64a4 100644 --- a/cplay.c +++ b/cplay.c @@ -337,7 +337,7 @@ void play_samples(char *name, unsigned int card, unsigned int device, goto BUF_EXIT; } if (wrote != num_read) { - /* TODO: Buufer pointer needs to be set here */ + /* TODO: Buffer pointer needs to be set here */ fprintf(stderr, "We wrote %d, DSP accepted %d\n", num_read, wrote); } if (verbose) { @@ -348,7 +348,7 @@ void play_samples(char *name, unsigned int card, unsigned int device, } while (num_read > 0); if (verbose) - printf("%s: exit sucess\n", __func__); + printf("%s: exit success\n", __func__); /* issue drain if it supports */ compress_drain(compress); free(buffer); diff --git a/include/tinycompress/tinycompress.h b/include/tinycompress/tinycompress.h index ba9f3b6..9867afc 100644 --- a/include/tinycompress/tinycompress.h +++ b/include/tinycompress/tinycompress.h @@ -76,7 +76,7 @@ struct compr_gapless_mdata { __u32 encoder_padding; }; -#define COMPRESS_OUT 0x00000000 +#define COMPRESS_OUT 0x20000000 #define COMPRESS_IN 0x10000000 struct compress; -- cgit v1.2.3 From 79c370788bfeac8e2951cc35844fb96f15f28159 Mon Sep 17 00:00:00 2001 From: Eric Laurent Date: Fri, 7 Jun 2013 10:50:05 -0700 Subject: Update to tinycompress 2.0 0f25782 Tinycompress version 0.2.0 6b27cf7 compress: Add non-blocking I/O 0e0c39e compress: do not poll if enough space to write remaining data 78672ce compress: copy final version of config struct 5777ab6 cplay: use get_alsa_rate to convert sample rate 1bb4a13 add utils file with rate conversion helper 38145d7 fix error reporting in tinycompress aad6a2a compress: fix hpointer error when no sample rate 31d610d compress: check for config first Change-Id: I51037eacdab73dfd4f3b8e58cda79f536503f826 --- Android.mk | 2 +- NOTICE | 53 ++++++++++++++++ compress.c | 119 ++++++++++++++++++++++++------------ cplay.c | 54 +++------------- include/tinycompress/tinycompress.h | 25 +++++++- include/tinycompress/version.h | 4 +- utils.c | 96 +++++++++++++++++++++++++++++ 7 files changed, 263 insertions(+), 90 deletions(-) create mode 100644 utils.c diff --git a/Android.mk b/Android.mk index b9c52e2..104dfef 100644 --- a/Android.mk +++ b/Android.mk @@ -2,7 +2,7 @@ LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_C_INCLUDES:= $(LOCAL_PATH)/include -LOCAL_SRC_FILES:= compress.c +LOCAL_SRC_FILES:= compress.c utils.c LOCAL_MODULE := libtinycompress LOCAL_SHARED_LIBRARIES:= libcutils libutils LOCAL_MODULE_TAGS := optional diff --git a/NOTICE b/NOTICE index ba3e45d..5af5078 100644 --- a/NOTICE +++ b/NOTICE @@ -194,6 +194,59 @@ tinycompress library for compress audio offload in alsa Copyright (c) 2011-2012, Intel Corporation. +This program is free software; you can redistribute it and/or modify it +under the terms and conditions of the GNU Lesser General Public License, +version 2.1, as published by the Free Software Foundation. + +This program is distributed in the hope it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program; if not, write to +the Free Software Foundation, Inc., +51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + +=============================================================================== + +BSD LICENSE + +tinycompress utility functions +Copyright (c) 2011-2013, Intel Corporation +All rights reserved. + +Author: Vinod Koul + +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 Intel Corporation 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 COPYRIGHT OWNER 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. + +LGPL LICENSE + +tinycompress utility functions +Copyright (c) 2011-2013, Intel Corporation + This program is free software; you can redistribute it and/or modify it under the terms and conditions of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation. diff --git a/compress.c b/compress.c index d5115b6..734fbbf 100644 --- a/compress.c +++ b/compress.c @@ -72,9 +72,9 @@ #define __bitwise #define __user #include -#include -#include -#include +#include "sound/compress_params.h" +#include "sound/compress_offload.h" +#include "tinycompress/tinycompress.h" #define COMPR_ERR_MAX 128 @@ -88,6 +88,7 @@ struct compress { struct compr_config *config; int running; int max_poll_wait_ms; + int nonblocking; unsigned int gapless_metadata; unsigned int next_track; }; @@ -102,10 +103,11 @@ static int oops(struct compress *compress, int e, const char *fmt, ...) va_end(ap); sz = strlen(compress->error); - if (errno) - snprintf(compress->error + sz, COMPR_ERR_MAX - sz, - ": %s", strerror(e)); - return e; + snprintf(compress->error + sz, COMPR_ERR_MAX - sz, + ": %s", strerror(e)); + errno = e; + + return -1; } const char *compress_get_error(struct compress *compress) @@ -151,27 +153,27 @@ static bool _is_codec_supported(struct compress *compress, struct compr_config * } } if (codec == false) { - oops(compress, -ENXIO, "this codec is not supported"); + oops(compress, ENXIO, "this codec is not supported"); return false; } if (config->fragment_size < caps->min_fragment_size) { - oops(compress, -EINVAL, "requested fragment size %d is below min supported %d", + oops(compress, EINVAL, "requested fragment size %d is below min supported %d", config->fragment_size, caps->min_fragment_size); return false; } if (config->fragment_size > caps->max_fragment_size) { - oops(compress, -EINVAL, "requested fragment size %d is above max supported %d", + oops(compress, EINVAL, "requested fragment size %d is above max supported %d", config->fragment_size, caps->max_fragment_size); return false; } if (config->fragments < caps->min_fragments) { - oops(compress, -EINVAL, "requested fragments %d are below min supported %d", + oops(compress, EINVAL, "requested fragments %d are below min supported %d", config->fragments, caps->min_fragments); return false; } if (config->fragments > caps->max_fragments) { - oops(compress, -EINVAL, "requested fragments %d are above max supported %d", + oops(compress, EINVAL, "requested fragments %d are above max supported %d", config->fragments, caps->max_fragments); return false; } @@ -218,8 +220,13 @@ struct compress *compress_open(unsigned int card, unsigned int device, struct snd_compr_caps caps; char fn[256]; + if (!config) { + oops(&bad_compress, EINVAL, "passed bad config"); + return &bad_compress; + } + compress = calloc(1, sizeof(struct compress)); - if (!compress || !config) { + if (!compress) { oops(&bad_compress, errno, "cannot allocate compress object"); return &bad_compress; } @@ -229,7 +236,6 @@ struct compress *compress_open(unsigned int card, unsigned int device, compress->config = calloc(1, sizeof(*config)); if (!compress->config) goto input_fail; - memcpy(compress->config, config, sizeof(*compress->config)); snprintf(fn, sizeof(fn), "/dev/snd/comprC%uD%u", card, device); @@ -237,7 +243,7 @@ struct compress *compress_open(unsigned int card, unsigned int device, compress->flags = flags; if (!((flags & COMPRESS_OUT) || (flags & COMPRESS_IN))) { - oops(&bad_compress, -EINVAL, "can't deduce device direction from given flags"); + oops(&bad_compress, EINVAL, "can't deduce device direction from given flags"); goto config_fail; } @@ -271,6 +277,8 @@ struct compress *compress_open(unsigned int card, unsigned int device, goto codec_fail; } #endif + + memcpy(compress->config, config, sizeof(*compress->config)); fill_compress_params(config, ¶ms); if (ioctl(compress->fd, SNDRV_COMPRESS_SET_PARAMS, ¶ms)) { @@ -310,12 +318,12 @@ int compress_get_hpointer(struct compress *compress, __u64 time; if (!is_compress_ready(compress)) - return oops(compress, -ENODEV, "device not ready"); + return oops(compress, ENODEV, "device not ready"); if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &kavail)) return oops(compress, errno, "cannot get avail"); if (0 == kavail.tstamp.sampling_rate) - return oops(compress, errno, "invalid paramter"); + return oops(compress, ENODATA, "sample rate unknown"); *avail = (unsigned int)kavail.avail; time = kavail.tstamp.pcm_io_frames / kavail.tstamp.sampling_rate; tstamp->tv_sec = time; @@ -330,7 +338,7 @@ int compress_get_tstamp(struct compress *compress, struct snd_compr_tstamp ktstamp; if (!is_compress_ready(compress)) - return oops(compress, -ENODEV, "device not ready"); + return oops(compress, ENODEV, "device not ready"); if (ioctl(compress->fd, SNDRV_COMPRESS_TSTAMP, &ktstamp)) return oops(compress, errno, "cannot get tstamp"); @@ -350,9 +358,9 @@ int compress_write(struct compress *compress, const void *buf, unsigned int size const unsigned int frag_size = compress->config->fragment_size; if (!(compress->flags & COMPRESS_IN)) - return oops(compress, -EINVAL, "Invalid flag set"); + return oops(compress, EINVAL, "Invalid flag set"); if (!is_compress_ready(compress)) - return oops(compress, -ENODEV, "device not ready"); + return oops(compress, ENODEV, "device not ready"); fds.fd = compress->fd; fds.events = POLLOUT; @@ -361,12 +369,14 @@ int compress_write(struct compress *compress, const void *buf, unsigned int size if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &avail)) return oops(compress, errno, "cannot get avail"); - if ( (avail.avail < frag_size) - || ((to_write != 0) && (avail.avail < size)) ) { - /* not enough space for one fragment, or we have done - * a short write and there isn't enough space for all - * the remaining data - */ + /* We can write if we have at least one fragment available + * or there is enough space for all remaining data + */ + if ((avail.avail < frag_size) && (avail.avail < size)) { + + if (compress->nonblocking) + return total; + ret = poll(&fds, 1, compress->max_poll_wait_ms); /* A pause will cause -EBADFD or zero. * This is not an error, just stop writing */ @@ -378,7 +388,7 @@ int compress_write(struct compress *compress, const void *buf, unsigned int size continue; } if (fds.revents & POLLERR) { - return oops(compress, -EIO, "poll returned error!"); + return oops(compress, EIO, "poll returned error!"); } } /* write avail bytes */ @@ -410,9 +420,9 @@ int compress_read(struct compress *compress, void *buf, unsigned int size) const unsigned int frag_size = compress->config->fragment_size; if (!(compress->flags & COMPRESS_OUT)) - return oops(compress, -EINVAL, "Invalid flag set"); + return oops(compress, EINVAL, "Invalid flag set"); if (!is_compress_ready(compress)) - return oops(compress, -ENODEV, "device not ready"); + return oops(compress, ENODEV, "device not ready"); fds.fd = compress->fd; fds.events = POLLIN; @@ -424,6 +434,9 @@ int compress_read(struct compress *compress, void *buf, unsigned int size) /* Less than one fragment available and not at the * end of the read, so poll */ + if (compress->nonblocking) + return total; + ret = poll(&fds, 1, compress->max_poll_wait_ms); /* A pause will cause -EBADFD or zero. * This is not an error, just stop reading */ @@ -435,7 +448,7 @@ int compress_read(struct compress *compress, void *buf, unsigned int size) continue; } if (fds.revents & POLLERR) { - return oops(compress, -EIO, "poll returned error!"); + return oops(compress, EIO, "poll returned error!"); } } /* read avail bytes */ @@ -461,7 +474,7 @@ int compress_read(struct compress *compress, void *buf, unsigned int size) int compress_start(struct compress *compress) { if (!is_compress_ready(compress)) - return oops(compress, -ENODEV, "device not ready"); + return oops(compress, ENODEV, "device not ready"); if (ioctl(compress->fd, SNDRV_COMPRESS_START)) return oops(compress, errno, "cannot start the stream"); compress->running = 1; @@ -472,7 +485,7 @@ int compress_start(struct compress *compress) int compress_stop(struct compress *compress) { if (!is_compress_running(compress)) - return oops(compress, -ENODEV, "device not ready"); + return oops(compress, ENODEV, "device not ready"); if (ioctl(compress->fd, SNDRV_COMPRESS_STOP)) return oops(compress, errno, "cannot stop the stream"); return 0; @@ -481,7 +494,7 @@ int compress_stop(struct compress *compress) int compress_pause(struct compress *compress) { if (!is_compress_running(compress)) - return oops(compress, -ENODEV, "device not ready"); + return oops(compress, ENODEV, "device not ready"); if (ioctl(compress->fd, SNDRV_COMPRESS_PAUSE)) return oops(compress, errno, "cannot pause the stream"); return 0; @@ -497,7 +510,7 @@ int compress_resume(struct compress *compress) int compress_drain(struct compress *compress) { if (!is_compress_running(compress)) - return oops(compress, -ENODEV, "device not ready"); + return oops(compress, ENODEV, "device not ready"); if (ioctl(compress->fd, SNDRV_COMPRESS_DRAIN)) return oops(compress, errno, "cannot drain the stream"); return 0; @@ -506,10 +519,10 @@ int compress_drain(struct compress *compress) int compress_partial_drain(struct compress *compress) { if (!is_compress_running(compress)) - return oops(compress, -ENODEV, "device not ready"); + return oops(compress, ENODEV, "device not ready"); if (!compress->next_track) - return oops(compress, -EPERM, "next track not signalled"); + return oops(compress, EPERM, "next track not signalled"); if (ioctl(compress->fd, SNDRV_COMPRESS_PARTIAL_DRAIN)) return oops(compress, errno, "cannot drain the stream\n"); compress->next_track = 0; @@ -519,10 +532,10 @@ int compress_partial_drain(struct compress *compress) int compress_next_track(struct compress *compress) { if (!is_compress_running(compress)) - return oops(compress, -ENODEV, "device not ready"); + return oops(compress, ENODEV, "device not ready"); if (!compress->gapless_metadata) - return oops(compress, -EPERM, "metadata not set"); + return oops(compress, EPERM, "metadata not set"); if (ioctl(compress->fd, SNDRV_COMPRESS_NEXT_TRACK)) return oops(compress, errno, "cannot set next track\n"); compress->next_track = 1; @@ -537,14 +550,14 @@ int compress_set_gapless_metadata(struct compress *compress, int version; if (!is_compress_ready(compress)) - return oops(compress, -ENODEV, "device not ready"); + return oops(compress, ENODEV, "device not ready"); version = get_compress_version(compress); if (version <= 0) return -1; if (version < SNDRV_PROTOCOL_VERSION(0, 1, 1)) - return oops(compress, -ENXIO, "gapless apis not supported in kernel"); + return oops(compress, ENXIO, "gapless apis not supported in kernel"); metadata.key = SNDRV_COMPRESS_ENCODER_PADDING; metadata.value[0] = mdata->encoder_padding; @@ -589,3 +602,29 @@ void compress_set_max_poll_wait(struct compress *compress, int milliseconds) compress->max_poll_wait_ms = milliseconds; } +void compress_nonblock(struct compress *compress, int nonblock) +{ + compress->nonblocking = !!nonblock; +} + +int compress_wait(struct compress *compress, int timeout_ms) +{ + struct pollfd fds; + int ret; + + fds.fd = compress->fd; + fds.events = POLLOUT | POLLIN; + + ret = poll(&fds, 1, timeout_ms); + /* A pause will cause -EBADFD or zero. */ + if ((ret < 0) && (ret != -EBADFD)) + return oops(compress, errno, "poll error"); + if (fds.revents & (POLLOUT | POLLIN)) { + return 0; + } + if (fds.revents & POLLERR) { + return oops(compress, -EIO, "poll returned error!"); + } + return ret; +} + diff --git a/cplay.c b/cplay.c index 21e64a4..10c5424 100644 --- a/cplay.c +++ b/cplay.c @@ -66,9 +66,9 @@ #define __force #define __bitwise #define __user -#include -#include -#include +#include "sound/compress_params.h" +#include "tinycompress/tinycompress.h" +#include "tinycompress/tinymp3.h" static int verbose; @@ -226,49 +226,11 @@ void play_samples(char *name, unsigned int card, unsigned int device, codec.id = SND_AUDIOCODEC_MP3; codec.ch_in = channels; codec.ch_out = channels; - switch (rate) { - case 5512: - codec.sample_rate = SNDRV_PCM_RATE_5512; - break; - case 8000: - codec.sample_rate = SNDRV_PCM_RATE_8000; - break; - case 11025: - codec.sample_rate = SNDRV_PCM_RATE_11025; - break; - case 16000: - codec.sample_rate = SNDRV_PCM_RATE_16000; - break; - case 220500: - codec.sample_rate = SNDRV_PCM_RATE_22050; - break; - case 32000: - codec.sample_rate = SNDRV_PCM_RATE_32000; - break; - case 44100: - codec.sample_rate = SNDRV_PCM_RATE_44100; - break; - case 48000: - codec.sample_rate = SNDRV_PCM_RATE_48000; - break; - case 64000: - codec.sample_rate = SNDRV_PCM_RATE_64000; - break; - case 88200: - codec.sample_rate = SNDRV_PCM_RATE_88200; - break; - case 96000: - codec.sample_rate = SNDRV_PCM_RATE_96000; - break; - case 176400: - codec.sample_rate = SNDRV_PCM_RATE_176400; - break; - case 192000: - codec.sample_rate = SNDRV_PCM_RATE_192000; - break; - default: - fprintf(stderr, "unknown sample rate %d\n", rate); - goto FILE_EXIT; + codec.sample_rate = compress_get_alsa_rate(rate); + if (!codec.sample_rate) { + fprintf(stderr, "invalid sample rate %d\n", rate); + fclose(file); + exit(EXIT_FAILURE); } codec.bit_rate = bits; codec.rate_control = 0; diff --git a/include/tinycompress/tinycompress.h b/include/tinycompress/tinycompress.h index 9867afc..40de69a 100644 --- a/include/tinycompress/tinycompress.h +++ b/include/tinycompress/tinycompress.h @@ -130,7 +130,15 @@ int compress_get_tstamp(struct compress *compress, /* * compress_write: write data to the compress stream * return bytes written on success, negative on error - * this is a blocking call + * By default this is a blocking call and will not return + * until all bytes have been written or there was a + * write error. + * If non-blocking mode has been enabled with compress_nonblock(), + * this function will write all bytes that can be written without + * blocking and will then return the number of bytes successfully + * written. If the return value is not an error and is < size + * the caller can use compress_wait() to block until the driver + * is ready for more data. * * @compress: compress stream to be written to * @buf: pointer to data @@ -141,6 +149,13 @@ int compress_write(struct compress *compress, const void *buf, unsigned int size /* * compress_read: read data from the compress stream * return bytes read on success, negative on error + * By default this is a blocking call and will block until + * size bytes have been written or there was a read error. + * If non-blocking mode was enabled using compress_nonblock() + * the behaviour will change to read only as many bytes as + * are currently available (if no bytes are available it + * will return immediately). The caller can then use + * compress_wait() to block until more bytes are available. * * @compress: compress stream from where data is to be read * @buf: pointer to data buffer @@ -242,6 +257,12 @@ bool is_codec_supported(unsigned int card, unsigned int device, */ void compress_set_max_poll_wait(struct compress *compress, int milliseconds); +/* Enable or disable non-blocking mode for write and read */ +void compress_nonblock(struct compress *compress, int nonblock); + +/* Wait for ring buffer to ready for next read or write */ +int compress_wait(struct compress *compress, int timeout_ms); + int is_compress_running(struct compress *compress); int is_compress_ready(struct compress *compress); @@ -267,4 +288,6 @@ const char *compress_get_error(struct compress *compress); #define SNDRV_PCM_RATE_176400 (1<<11) /* 176400Hz */ #define SNDRV_PCM_RATE_192000 (1<<12) /* 192000Hz */ +/* utility functions */ +unsigned int compress_get_alsa_rate(unsigned int rate); #endif diff --git a/include/tinycompress/version.h b/include/tinycompress/version.h index 795fe77..000f6a2 100644 --- a/include/tinycompress/version.h +++ b/include/tinycompress/version.h @@ -55,7 +55,7 @@ #define TINYCOMPRESS_LIB_MAJOR 0 /* major number of library version */ -#define TINYCOMPRESS_LIB_MINOR 1 /* minor number of library version */ +#define TINYCOMPRESS_LIB_MINOR 2 /* minor number of library version */ #define TINYCOMPRESS_LIB_SUBMINOR 0 /* subminor number of library version */ /** library version */ @@ -65,6 +65,6 @@ TINYCOMPRESS_LIB_SUBMINOR) /** library version (string) */ -#define TINYCOMPRESS_LIB_VERSION_STR "0.1.0" +#define TINYCOMPRESS_LIB_VERSION_STR "0.2.0" #endif diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..fa37f23 --- /dev/null +++ b/utils.c @@ -0,0 +1,96 @@ +/* + * BSD LICENSE + * + * tinycompress utility functions + * Copyright (c) 2011-2013, Intel Corporation + * All rights reserved. + * + * Author: Vinod Koul + * + * 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 Intel Corporation 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 COPYRIGHT OWNER 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. + * + * LGPL LICENSE + * + * tinycompress utility functions + * Copyright (c) 2011-2013, Intel Corporation + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to + * the Free Software Foundation, Inc., + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ +#include +#include +#include +#include +#include +#define __force +#define __bitwise +#define __user +#include "tinycompress/tinycompress.h" + + +unsigned int compress_get_alsa_rate(unsigned int rate) +{ + switch (rate) { + case 5512: + return SNDRV_PCM_RATE_5512; + case 8000: + return SNDRV_PCM_RATE_8000; + case 11025: + return SNDRV_PCM_RATE_11025; + case 16000: + return SNDRV_PCM_RATE_16000; + case 220500: + return SNDRV_PCM_RATE_22050; + case 32000: + return SNDRV_PCM_RATE_32000; + case 44100: + return SNDRV_PCM_RATE_44100; + case 48000: + return SNDRV_PCM_RATE_48000; + case 64000: + return SNDRV_PCM_RATE_64000; + case 88200: + return SNDRV_PCM_RATE_88200; + case 96000: + return SNDRV_PCM_RATE_96000; + case 176400: + return SNDRV_PCM_RATE_176400; + case 192000: + return SNDRV_PCM_RATE_192000; + default: + return 0; + } +} -- cgit v1.2.3 From adad134c906042742a801fb66eb4271a7130af83 Mon Sep 17 00:00:00 2001 From: Vinod Koul Date: Sat, 20 Jul 2013 12:00:38 +0530 Subject: Update to latest tinycompress 22406d2 fix error returned by compress_wait() 7d7965c compress: fix the typo in compress_get_alsa_rate() --- compress.c | 2 +- utils.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compress.c b/compress.c index 734fbbf..0e71c28 100644 --- a/compress.c +++ b/compress.c @@ -623,7 +623,7 @@ int compress_wait(struct compress *compress, int timeout_ms) return 0; } if (fds.revents & POLLERR) { - return oops(compress, -EIO, "poll returned error!"); + return oops(compress, EIO, "poll returned error!"); } return ret; } diff --git a/utils.c b/utils.c index fa37f23..a835bef 100644 --- a/utils.c +++ b/utils.c @@ -72,7 +72,7 @@ unsigned int compress_get_alsa_rate(unsigned int rate) return SNDRV_PCM_RATE_11025; case 16000: return SNDRV_PCM_RATE_16000; - case 220500: + case 22050: return SNDRV_PCM_RATE_22050; case 32000: return SNDRV_PCM_RATE_32000; -- cgit v1.2.3 From a85e245a09c028d36cbf04f233be10bc583691f5 Mon Sep 17 00:00:00 2001 From: Richard Fitzgerald Date: Thu, 29 Aug 2013 09:32:02 +0100 Subject: Update to latest tinycompress 2b0410a compress: Must check for POLLERR before POLLOUT/POLLIN Bug: 8174034. Change-Id: I26a7bd4bf3999afbe99a2278a59304834980375e --- compress.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/compress.c b/compress.c index 0e71c28..5cd0966 100644 --- a/compress.c +++ b/compress.c @@ -378,6 +378,9 @@ int compress_write(struct compress *compress, const void *buf, unsigned int size return total; ret = poll(&fds, 1, compress->max_poll_wait_ms); + if (fds.revents & POLLERR) { + return oops(compress, EIO, "poll returned error!"); + } /* A pause will cause -EBADFD or zero. * This is not an error, just stop writing */ if ((ret == 0) || (ret == -EBADFD)) @@ -387,9 +390,6 @@ int compress_write(struct compress *compress, const void *buf, unsigned int size if (fds.revents & POLLOUT) { continue; } - if (fds.revents & POLLERR) { - return oops(compress, EIO, "poll returned error!"); - } } /* write avail bytes */ if (size > avail.avail) @@ -438,6 +438,9 @@ int compress_read(struct compress *compress, void *buf, unsigned int size) return total; ret = poll(&fds, 1, compress->max_poll_wait_ms); + if (fds.revents & POLLERR) { + return oops(compress, EIO, "poll returned error!"); + } /* A pause will cause -EBADFD or zero. * This is not an error, just stop reading */ if ((ret == 0) || (ret == -EBADFD)) @@ -447,9 +450,6 @@ int compress_read(struct compress *compress, void *buf, unsigned int size) if (fds.revents & POLLIN) { continue; } - if (fds.revents & POLLERR) { - return oops(compress, EIO, "poll returned error!"); - } } /* read avail bytes */ if (size > avail.avail) @@ -616,15 +616,15 @@ int compress_wait(struct compress *compress, int timeout_ms) fds.events = POLLOUT | POLLIN; ret = poll(&fds, 1, timeout_ms); + if (fds.revents & POLLERR) { + return oops(compress, EIO, "poll returned error!"); + } /* A pause will cause -EBADFD or zero. */ if ((ret < 0) && (ret != -EBADFD)) return oops(compress, errno, "poll error"); if (fds.revents & (POLLOUT | POLLIN)) { return 0; } - if (fds.revents & POLLERR) { - return oops(compress, EIO, "poll returned error!"); - } return ret; } -- cgit v1.2.3