3
0
Fork 0

mc1n2: Opensource audio HAL from Replicant

Thanks to Paul Kocialkowski of the Replicant project for this work.

Original sources:
https://gitorious.org/replicant/hardware_tinyalsa-audio

http://git.paulk.fr/gitweb/?p=yamaha-mc1n2-audio.git;a=summary

Change-Id: Ia3d89c67d64decb56e3d6518c5f382d38e5a1fa9
replicant-6.0
Andrew Dodd 2012-11-10 14:23:08 -05:00 committed by codeworkx
parent fd9472ca4b
commit cdc1a96ac0
21 changed files with 7218 additions and 11 deletions

View File

@ -90,10 +90,8 @@ BOARD_USE_METADATABUFFERTYPE := true
BOARD_USES_MFC_FPS := true
# Audio
BOARD_USE_YAMAHAPLAYER := true
BOARD_USE_SAMSUNG_SEPARATEDSTREAM := true
BOARD_HAS_SAMSUNG_VOLUME_BUG := true
COMMON_GLOBAL_CFLAGS += -DICS_AUDIO_BLOB
BOARD_USE_TINYALSA_AUDIO := true
BOARD_USE_YAMAHA_MC1N2_AUDIO := true
# RIL
BOARD_MOBILEDATA_INTERFACE_NAME := "pdp0"

View File

@ -24,7 +24,8 @@ PRODUCT_COPY_FILES := \
# Audio
PRODUCT_COPY_FILES += \
device/samsung/galaxys2-common/configs/asound.conf:system/etc/asound.conf
device/samsung/galaxys2-common/configs/tinyalsa-audio.xml:system/etc/tinyalsa-audio.xml \
device/samsung/galaxys2-common/configs/audio_policy.conf:system/etc/audio_policy.conf
# Vold and Storage
PRODUCT_COPY_FILES += \
@ -52,17 +53,22 @@ PRODUCT_COPY_FILES += \
# Packages
PRODUCT_PACKAGES := \
audio.a2dp.default \
libaudiohw_legacy \
camera.exynos4 \
libsurfaceflinger_client \
com.android.future.usb.accessory \
SamsungServiceMode \
Torch \
TvOut
# Audio Packages
PRODUCT_PACKAGES += \
audio.primary.exynos4 \
audio.a2dp.default \
audio.usb.default \
libaudiohw_legacy
# HAL
PRODUCT_PACKAGES += \
camera.exynos4 \
gralloc.exynos4 \
hwcomposer.exynos4 \
libnetcmdiface \

82
configs/audio_policy.conf Normal file
View File

@ -0,0 +1,82 @@
# Global configuration section: lists input and output devices always present on the device
# as well as the output device selected by default.
# Devices are designated by a string that corresponds to the enum in audio.h
global_configuration {
attached_output_devices AUDIO_DEVICE_OUT_EARPIECE|AUDIO_DEVICE_OUT_SPEAKER
default_output_device AUDIO_DEVICE_OUT_SPEAKER
attached_input_devices AUDIO_DEVICE_IN_BUILTIN_MIC
}
# audio hardware module section: contains descriptors for all audio hw modules present on the
# device. Each hw module node is named after the corresponding hw module library base name.
# For instance, "primary" corresponds to audio.primary.<device>.so.
# The "primary" module is mandatory and must include at least one output with
# AUDIO_OUTPUT_FLAG_PRIMARY flag.
# Each module descriptor contains one or more output profile descriptors and zero or more
# input profile descriptors. Each profile lists all the parameters supported by a given output
# or input stream category.
# The "channel_masks", "formats", "devices" and "flags" are specified using strings corresponding
# to enums in audio.h and audio_policy.h. They are concatenated by use of "|" without space or "\n".
audio_hw_modules {
primary {
outputs {
primary {
sampling_rates 44100
channel_masks AUDIO_CHANNEL_OUT_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_OUT_EARPIECE|AUDIO_DEVICE_OUT_SPEAKER|AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_WIRED_HEADPHONE|AUDIO_DEVICE_OUT_ALL_SCO|AUDIO_DEVICE_OUT_AUX_DIGITAL|AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET
flags AUDIO_OUTPUT_FLAG_PRIMARY
}
# deep_buffer {
# sampling_rates 44100
# channel_masks AUDIO_CHANNEL_OUT_STEREO
# formats AUDIO_FORMAT_PCM_16_BIT
# devices AUDIO_DEVICE_OUT_SPEAKER|AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_WIRED_HEADPHONE
# flags AUDIO_OUTPUT_FLAG_DEEP_BUFFER
# }
# hdmi {
# sampling_rates 44100|48000
# channel_masks dynamic
# formats AUDIO_FORMAT_PCM_16_BIT
# devices AUDIO_DEVICE_OUT_AUX_DIGITAL
# flags AUDIO_OUTPUT_FLAG_DIRECT
# }
}
inputs {
primary {
sampling_rates 8000|11025|16000|22050|24000|32000|44100|48000
channel_masks AUDIO_CHANNEL_IN_MONO|AUDIO_CHANNEL_IN_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET|AUDIO_DEVICE_IN_WIRED_HEADSET
}
}
}
a2dp {
outputs {
a2dp {
sampling_rates 44100
channel_masks AUDIO_CHANNEL_OUT_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_OUT_ALL_A2DP
}
}
}
usb {
outputs {
usb_accessory {
sampling_rates 44100
channel_masks AUDIO_CHANNEL_OUT_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_OUT_USB_ACCESSORY
}
usb_device {
sampling_rates 44100
channel_masks AUDIO_CHANNEL_OUT_STEREO
formats AUDIO_FORMAT_PCM_16_BIT
devices AUDIO_DEVICE_OUT_USB_DEVICE
}
}
}
}

170
configs/tinyalsa-audio.xml Normal file
View File

@ -0,0 +1,170 @@
<tinyalsa-audio device="Galaxy S2">
<output card="0" device="0"
rate="44100" channels="2" format="PCM_16"
period_size="1024" period_count="4">
<device type="default">
<path type="enable">
<ctrl name="AD Digital Volume" value="75" />
<ctrl name="AD Digital Switch" value="on" />
<ctrl name="AENG6 Volume" value="75" />
<ctrl name="AENG6 Switch" value="on" />
<ctrl name="DIR#0 Volume" value="75" />
<ctrl name="DIR#0 Switch" value="on" />
<ctrl name="DIR#1 Volume" value="75" />
<ctrl name="DIR#1 Switch" value="on" />
<ctrl name="DIR#2 Volume" value="75" />
<ctrl name="DIR#2 Switch" value="on" />
<ctrl name="DIR#0 ATT Volume" value="75" />
<ctrl name="DIR#0 ATT Switch" value="on" />
<ctrl name="DIR#1 ATT Volume" value="75" />
<ctrl name="DIR#1 ATT Switch" value="on" />
<ctrl name="DIR#2 ATT Volume" value="75" />
<ctrl name="DIR#2 ATT Switch" value="on" />
<ctrl name="Master Playback Volume" value="75" />
<ctrl name="Master Playback Switch" value="on" />
<ctrl name="DAC Playback Volume" value="75" />
<ctrl name="DAC Playback Switch" value="on" />
<ctrl name="DIT#0 Capture Volume" value="75" />
<ctrl name="DIT#0 Capture Switch" value="on" />
<ctrl name="DIT#1 Capture Volume" value="75" />
<ctrl name="DIT#1 Capture Switch" value="on" />
<ctrl name="DIT#2 Capture Volume" value="75" />
<ctrl name="DIT#2 Capture Switch" value="on" />
<ctrl name="AD Analog Volume" value="19" />
<ctrl name="AD Analog Switch" value="on" />
</path>
</device>
<device type="earpiece">
<path type="enable">
<ctrl name="Receiver Playback Volume" value="31" />
<ctrl name="Receiver Playback Switch" value="on" />
</path>
<path type="disable">
<ctrl name="Receiver Playback Volume" value="0" />
<ctrl name="Receiver Playback Switch" value="off" />
</path>
</device>
<device type="speaker">
<path type="enable">
<ctrl name="Speaker Playback Volume" value="26" />
<ctrl name="Speaker Playback Switch" value="on" />
</path>
<path type="disable">
<ctrl name="Speaker Playback Volume" value="0" />
<ctrl name="Speaker Playback Switch" value="off" />
</path>
</device>
<device type="wired-headphone">
<path type="enable">
<ctrl name="Headphone Playback Volume" value="15" />
<ctrl name="Headphone Playback Switch" value="on" />
</path>
<path type="disable">
<ctrl name="Headphone Playback Volume" value="0" />
<ctrl name="Headphone Playback Switch" value="off" />
</path>
</device>
<device type="wired-headset">
<path type="enable">
<ctrl name="Headphone Playback Volume" value="15" />
<ctrl name="Headphone Playback Switch" value="on" />
</path>
<path type="disable">
<ctrl name="Headphone Playback Volume" value="0" />
<ctrl name="Headphone Playback Switch" value="off" />
</path>
</device>
</output>
<input card="0" device="0"
rate="44100" channels="2" format="PCM_16"
period_size="1024" period_count="4">
<device type="builtin-mic">
<path type="enable">
<ctrl name="AD Analog Volume" value="26" />
<ctrl name="Mic 1 Gain Volume" value="2" />
</path>
<path type="disable">
<ctrl name="AD Analog Volume" value="19" />
<ctrl name="Mic 1 Gain Volume" value="0" />
</path>
</device>
<device type="wired-headset">
<path type="enable">
<ctrl name="AD Analog Volume" value="26" />
<ctrl name="Mic 2 Gain Volume" value="2" />
</path>
<path type="disable">
<ctrl name="AD Analog Volume" value="19" />
<ctrl name="Mic 2 Gain Volume" value="0" />
</path>
</device>
</input>
<modem card="0" device="0">
<device type="earpiece">
<path type="enable">
<ctrl name="AD Digital Volume" value="84" />
<ctrl name="DIR#1 Volume" value="80" />
<ctrl name="Master Playback Volume" value="76" />
<ctrl name="AD Analog Volume" value="22" />
</path>
<path type="disable">
<ctrl name="AD Digital Volume" value="75" />
<ctrl name="DIR#1 Volume" value="75" />
<ctrl name="Master Playback Volume" value="75" />
<ctrl name="AD Analog Volume" value="19" />
</path>
</device>
<device type="speaker">
<path type="enable">
<ctrl name="AD Digital Volume" value="80" />
<ctrl name="Speaker Playback Volume" value="31" />
<!-- <ctrl name="Mic 3 Gain Volume" value="3" /> -->
</path>
<path type="disable">
<ctrl name="AD Digital Volume" value="75" />
<ctrl name="Speaker Playback Volume" value="26" />
<!-- <ctrl name="Mic 3 Gain Volume" value="0" /> -->
</path>
</device>
<device type="wired-headphone">
<path type="enable">
<ctrl name="AD Digital Volume" value="84" />
<ctrl name="Master Playback Volume" value="69" />
<ctrl name="AD Analog Volume" value="22" />
<ctrl name="Headphone Playback Volume" value="31" />
</path>
<path type="disable">
<ctrl name="AD Digital Volume" value="75" />
<ctrl name="Master Playback Volume" value="75" />
<ctrl name="AD Analog Volume" value="19" />
<ctrl name="Headphone Playback Volume" value="15" />
</path>
</device>
<device type="wired-headset">
<path type="enable">
<ctrl name="Master Playback Volume" value="69" />
<ctrl name="AD Analog Volume" value="26" />
<ctrl name="Headphone Playback Volume" value="31" />
<ctrl name="Mic 2 Gain Volume" value="2" />
</path>
<path type="disable">
<ctrl name="Master Playback Volume" value="75" />
<ctrl name="AD Analog Volume" value="19" />
<ctrl name="Headphone Playback Volume" value="15" />
<ctrl name="Mic 2 Gain Volume" value="0" />
</path>
</device>
<device type="bt-sco-headset">
<path type="enable">
<ctrl name="DIR#2 Volume" value="81" />
</path>
<path type="disable">
<ctrl name="DIR#2 Volume" value="75" />
</path>
</device>
</modem>
</tinyalsa-audio>

View File

@ -166,9 +166,6 @@
<!-- Workaround for devices with broken keyboards -->
<bool name="config_forceDisableHardwareKeyboard">true</bool>
<!-- If true, adds support for no delay A2DP in Samsung devices -->
<bool name="config_noDelayInATwoDP">true</bool>
<!-- Boolean to enable stk functionality on Samsung phones -->
<bool name="config_samsung_stk">true</bool>
</resources>

73
tinyalsa_audio/Android.mk Normal file
View File

@ -0,0 +1,73 @@
# Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
LOCAL_PATH := $(call my-dir)
ifeq ($(strip $(BOARD_USE_TINYALSA_AUDIO)),true)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
audio_hw.c \
audio_out.c \
audio_in.c \
audio_ril_interface.c \
mixer.c
LOCAL_C_INCLUDES += \
external/tinyalsa/include \
external/expat/lib \
system/media/audio_utils/include \
system/media/audio_effects/include \
hardware/tinyalsa-audio/include
LOCAL_SHARED_LIBRARIES := \
libc \
libcutils \
libutils \
libexpat \
libtinyalsa \
libaudioutils \
libdl
ifeq ($(strip $(BOARD_USE_YAMAHA_MC1N2_AUDIO)),true)
LOCAL_CFLAGS += -DYAMAHA_MC1N2_AUDIO
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../yamaha-mc1n2-audio/include
LOCAL_SHARED_LIBRARIES += libyamaha-mc1n2-audio
#this really needs to get cleaned up to properly inherit from -common
ifeq ($(TARGET_DEVICE),n7000)
LOCAL_CFLAGS += -DYAMAHA_MC1N2_AUDIO_DEVICE=\"galaxys2\"
endif
ifeq ($(TARGET_DEVICE),i9100)
LOCAL_CFLAGS += -DYAMAHA_MC1N2_AUDIO_DEVICE=\"galaxys2\"
endif
ifeq ($(TARGET_DEVICE),i777)
LOCAL_CFLAGS += -DYAMAHA_MC1N2_AUDIO_DEVICE=\"galaxys2\"
endif
endif
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := audio.primary.$(TARGET_BOARD_PLATFORM)
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
include $(BUILD_SHARED_LIBRARY)
endif

534
tinyalsa_audio/audio_hw.c Normal file
View File

@ -0,0 +1,534 @@
/*
* Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr>
*
* This is based on Galaxy Nexus audio.primary.tuna implementation:
* Copyright 2011, The Android Open-Source Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define LOG_TAG "TinyALSA-Audio Hardware"
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <sys/time.h>
#include <cutils/str_parms.h>
#include <cutils/log.h>
#ifdef YAMAHA_MC1N2_AUDIO
#include <yamaha-mc1n2-audio.h>
#endif
#include "audio_hw.h"
#include "mixer.h"
/*
* Functions
*/
static uint32_t audio_hw_get_supported_devices(const struct audio_hw_device *dev)
{
struct tinyalsa_audio_device *device;
ALOGD("%s(%p)", __func__, dev);
if(dev == NULL)
return -1;
device = (struct tinyalsa_audio_device *) dev;
if(device->mixer == NULL)
return -1;
return (uint32_t) tinyalsa_mixer_get_supported_devices(device->mixer);
}
static int audio_hw_init_check(const struct audio_hw_device *dev)
{
struct tinyalsa_audio_device *device;
ALOGD("%s(%p)", __func__, dev);
if(dev == NULL)
return -1;
device = (struct tinyalsa_audio_device *) dev;
if(device->mixer == NULL)
return -1;
return 0;
}
static int audio_hw_set_voice_volume(struct audio_hw_device *dev, float volume)
{
struct tinyalsa_audio_device *device;
audio_devices_t device_modem;
ALOGD("%s(%p, %f)++", __func__, dev, volume);
if(dev == NULL)
return -1;
device = (struct tinyalsa_audio_device *) dev;
if(device->mixer == NULL)
return -1;
if(volume != device->voice_volume) {
pthread_mutex_lock(&device->lock);
if(device->mode == AUDIO_MODE_IN_CALL) {
if(device->ril_interface != NULL)
device_modem = device->ril_interface->device_current;
else if(device->stream_out != NULL)
device_modem = device->stream_out->device_current;
else
device_modem = AUDIO_DEVICE_OUT_EARPIECE;
tinyalsa_mixer_set_voice_volume(device->mixer,
device_modem, volume);
if(device->ril_interface != NULL)
audio_ril_interface_set_voice_volume(device->ril_interface, device_modem, volume);
}
device->voice_volume = volume;
pthread_mutex_unlock(&device->lock);
}
ALOGD("%s(%p, %f)--", __func__, dev, volume);
return 0;
}
static int audio_hw_set_master_volume(struct audio_hw_device *dev, float volume)
{
struct tinyalsa_audio_device *device;
ALOGD("%s(%p, %f)++", __func__, dev, volume);
if(dev == NULL)
return -1;
device = (struct tinyalsa_audio_device *) dev;
if(device->mixer == NULL)
return -1;
pthread_mutex_lock(&device->lock);
tinyalsa_mixer_set_master_volume(device->mixer, volume);
pthread_mutex_unlock(&device->lock);
ALOGD("%s(%p, %f)--", __func__, dev, volume);
return 0;
}
static int audio_hw_set_mode(struct audio_hw_device *dev, int mode)
{
struct tinyalsa_audio_ril_interface *ril_interface;
struct tinyalsa_audio_device *device;
audio_devices_t device_modem;
int rc;
ALOGD("%s(%p, %d)++", __func__, dev, mode);
if(dev == NULL)
return -1;
device = (struct tinyalsa_audio_device *) dev;
if(mode != device->mode) {
pthread_mutex_lock(&device->lock);
if(mode == AUDIO_MODE_IN_CALL) {
tinyalsa_mixer_set_modem_state(device->mixer, 1);
if(device->stream_out != NULL)
device_modem = device->stream_out->device_current;
else
device_modem = AUDIO_DEVICE_OUT_EARPIECE;
tinyalsa_mixer_set_device(device->mixer, device_modem);
#ifdef YAMAHA_MC1N2_AUDIO
rc = yamaha_mc1n2_audio_modem_start(device->mc1n2_pdata);
if(rc < 0) {
ALOGE("Failed to set Yamaha-MC1N2-Audio route");
}
#endif
rc = audio_ril_interface_open((struct audio_hw_device *) device, device_modem, &ril_interface);
if(rc < 0 || ril_interface == NULL) {
ALOGE("Failed to open RIL interface");
device->ril_interface = NULL;
} else {
device->ril_interface = ril_interface;
//Only enable dualmic for earpiece.
if(device_modem == AUDIO_DEVICE_OUT_EARPIECE)
audio_ril_interface_set_twomic(ril_interface,TWO_MIC_SOLUTION_ON);
if(device->voice_volume)
audio_ril_interface_set_voice_volume(ril_interface, device_modem, device->voice_volume);
}
} else if(device->mode == AUDIO_MODE_IN_CALL) {
tinyalsa_mixer_set_modem_state(device->mixer, 0);
/*
* Should be safe to ALWAYS disable it on exit
* But we should instrument secril-client to be sure
* when this is/isn't controlled - FIXME
*/
if(device->ril_interface != NULL) {
audio_ril_interface_set_twomic(device->ril_interface,TWO_MIC_SOLUTION_OFF);
}
#ifdef YAMAHA_MC1N2_AUDIO
rc = yamaha_mc1n2_audio_modem_stop(device->mc1n2_pdata);
if(rc < 0) {
ALOGE("Failed to set Yamaha-MC1N2-Audio route");
}
#endif
if(device->ril_interface != NULL) {
audio_ril_interface_close((struct audio_hw_device *) device, device->ril_interface);
}
}
device->mode = mode;
pthread_mutex_unlock(&device->lock);
}
ALOGD("%s(%p, %d)--", __func__, dev, mode);
return 0;
}
static int audio_hw_set_mic_mute(struct audio_hw_device *dev, bool state)
{
struct tinyalsa_audio_device *device;
audio_devices_t device_modem;
ALOGD("%s(%p, %d)++", __func__, dev, state);
if(dev == NULL)
return -1;
device = (struct tinyalsa_audio_device *) dev;
if(device->mixer == NULL)
return -1;
if(device->mic_mute != state) {
pthread_mutex_lock(&device->lock);
if(device->mode == AUDIO_MODE_IN_CALL) {
if(device->ril_interface != NULL)
device_modem = device->ril_interface->device_current;
else if(device->stream_out != NULL)
device_modem = device->stream_out->device_current;
else
device_modem = AUDIO_DEVICE_OUT_EARPIECE;
tinyalsa_mixer_set_mic_mute(device->mixer,
device_modem, state);
if(device->ril_interface != NULL)
audio_ril_interface_set_mic_mute(device->ril_interface, state);
} else {
if(device->stream_in != NULL) {
tinyalsa_mixer_set_mic_mute(device->mixer,
device->stream_in->device_current, state);
}
}
device->mic_mute = state;
pthread_mutex_unlock(&device->lock);
}
ALOGD("%s(%p, %d)--", __func__, dev, state);
return 0;
}
static int audio_hw_get_mic_mute(const struct audio_hw_device *dev, bool *state)
{
struct tinyalsa_audio_device *device;
ALOGD("%s(%p, %p)", __func__, dev, state);
if(dev == NULL)
return -1;
device = (struct tinyalsa_audio_device *) dev;
*state = device->mic_mute;
return 0;
}
static int audio_hw_set_parameters(struct audio_hw_device *dev,
const char *kvpairs)
{
struct tinyalsa_audio_device *device;
struct str_parms *parms;
char value_string[32] = { 0 };
int value;
int rc;
ALOGD("%s(%p, %s)++", __func__, dev, kvpairs);
if(dev == NULL || kvpairs == NULL)
return -1;
device = (struct tinyalsa_audio_device *) dev;
if(device->mixer == NULL)
return -1;
parms = str_parms_create_str(kvpairs);
if(parms == NULL)
return -1;
rc = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value_string, sizeof(value_string));
if(rc < 0)
goto error_params;
value = atoi(value_string);
pthread_mutex_lock(&device->lock);
if(audio_is_output_device((audio_devices_t) value)) {
if(device->stream_out != NULL && device->stream_out->device_current != (audio_devices_t) value) {
pthread_mutex_lock(&device->stream_out->lock);
audio_out_set_route(device->stream_out, (audio_devices_t) value);
pthread_mutex_unlock(&device->stream_out->lock);
}
if(device->ril_interface != NULL && device->ril_interface->device_current != (audio_devices_t) value) {
audio_ril_interface_set_route(device->ril_interface, (audio_devices_t) value);
}
} else if(audio_is_input_device((audio_devices_t) value)) {
if(device->stream_in != NULL && device->stream_in->device_current != (audio_devices_t) value) {
pthread_mutex_lock(&device->stream_in->lock);
audio_in_set_route(device->stream_in, (audio_devices_t) value);
pthread_mutex_unlock(&device->stream_in->lock);
}
}
pthread_mutex_unlock(&device->lock);
str_parms_destroy(parms);
ALOGD("%s(%p, %s)--", __func__, dev, kvpairs);
return 0;
error_params:
str_parms_destroy(parms);
ALOGD("%s(%p, %s)-- (PARAMETER ERROR)", __func__, dev, kvpairs);
return -1;
}
static char *audio_hw_get_parameters(const struct audio_hw_device *dev,
const char *keys)
{
ALOGD("%s(%p, %s)", __func__, dev, keys);
return strdup("");
}
static size_t audio_hw_get_input_buffer_size(const struct audio_hw_device *dev,
const struct audio_config *config)
{
struct tinyalsa_audio_device *device;
struct tinyalsa_mixer_io_props *mixer_props;
size_t size;
int channel_count = popcount(config->channel_mask);
ALOGD("%s(%p, %d, %d, %d)++", __func__, dev, config->sample_rate, config->format, channel_count);
if(dev == NULL)
return -1;
device = (struct tinyalsa_audio_device *) dev;
if(device->mixer == NULL)
return -1;
mixer_props = tinyalsa_mixer_get_input_props(device->mixer);
if(mixer_props == NULL)
return -1;
// Default value
if(mixer_props->rate == 0)
mixer_props->rate = 44100;
size = (mixer_props->period_size * config->sample_rate) / mixer_props->rate;
size = ((size + 15) / 16) * 16;
size = size * channel_count * audio_bytes_per_sample(config->format);
ALOGD("%s(%p, %d, %d, %d)--", __func__, dev, config->sample_rate, config->format, channel_count);
return size;
}
static int audio_hw_dump(const audio_hw_device_t *device, int fd)
{
ALOGD("%s(%p, %d)", __func__, device, fd);
return 0;
}
/*
* Interface
*/
int audio_hw_close(hw_device_t *device)
{
struct tinyalsa_audio_device *tinyalsa_audio_device;
ALOGD("%s(%p)++", __func__, device);
if(device != NULL) {
tinyalsa_audio_device = (struct tinyalsa_audio_device *) device;
if(tinyalsa_audio_device->mixer != NULL) {
tinyalsa_mixer_close(tinyalsa_audio_device->mixer);
tinyalsa_audio_device->mixer = NULL;
}
#ifdef YAMAHA_MC1N2_AUDIO
if(tinyalsa_audio_device->mc1n2_pdata != NULL) {
yamaha_mc1n2_audio_stop(tinyalsa_audio_device->mc1n2_pdata);
tinyalsa_audio_device->mc1n2_pdata = NULL;
}
#endif
free(device);
}
ALOGD("%s(%p)--", __func__, device);
return 0;
}
int audio_hw_open(const hw_module_t *module, const char *name,
hw_device_t **device)
{
struct tinyalsa_audio_device *tinyalsa_audio_device = NULL;
struct tinyalsa_mixer *tinyalsa_mixer = NULL;
struct audio_hw_device *dev;
int rc;
ALOGD("%s(%p, %s, %p)++", __func__, module, name, device);
if(device == NULL)
return -EINVAL;
if(strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
return -EINVAL;
tinyalsa_audio_device = calloc(1, sizeof(struct tinyalsa_audio_device));
if(tinyalsa_audio_device == NULL)
return -ENOMEM;
dev = &(tinyalsa_audio_device->device);
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = AUDIO_DEVICE_API_VERSION_1_0;
dev->common.module = (struct hw_module_t *) module;
dev->common.close = audio_hw_close;
dev->get_supported_devices = audio_hw_get_supported_devices;
dev->init_check = audio_hw_init_check;
dev->set_voice_volume = audio_hw_set_voice_volume;
dev->set_master_volume = audio_hw_set_master_volume;
dev->set_mode = audio_hw_set_mode;
dev->set_mic_mute = audio_hw_set_mic_mute;
dev->get_mic_mute = audio_hw_get_mic_mute;
dev->set_parameters = audio_hw_set_parameters;
dev->get_parameters = audio_hw_get_parameters;
dev->get_input_buffer_size = audio_hw_get_input_buffer_size;
dev->open_output_stream = audio_hw_open_output_stream;
dev->close_output_stream = audio_hw_close_output_stream;
dev->open_input_stream = audio_hw_open_input_stream;
dev->close_input_stream = audio_hw_close_input_stream;
dev->dump = audio_hw_dump;
#ifdef YAMAHA_MC1N2_AUDIO
rc = yamaha_mc1n2_audio_start(&tinyalsa_audio_device->mc1n2_pdata,
YAMAHA_MC1N2_AUDIO_DEVICE);
if(rc < 0) {
ALOGE("Failed to open Yamaha-MC1N2-Audio");
goto error_device;
}
rc = yamaha_mc1n2_audio_init(tinyalsa_audio_device->mc1n2_pdata);
if(rc < 0) {
ALOGE("Failed to init Yamaha-MC1N2-Audio");
}
#endif
rc = tinyalsa_mixer_open(&tinyalsa_mixer, TINYALSA_MIXER_CONFIG_FILE);
if(rc < 0 || tinyalsa_mixer == NULL) {
ALOGE("Failed to open mixer!");
goto error_device;
}
tinyalsa_audio_device->mixer = tinyalsa_mixer;
*device = &(dev->common);
ALOGD("%s(%p, %s, %p)--", __func__, module, name, device);
return 0;
error_device:
*device = NULL;
free(tinyalsa_audio_device);
ALOGD("%s(%p, %s, %p)-- (DEVICE ERROR)", __func__, module, name, device);
return -1;
}
struct hw_module_methods_t audio_hw_module_methods = {
.open = audio_hw_open,
};
struct audio_module HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.module_api_version = AUDIO_MODULE_API_VERSION_0_1,
.hal_api_version = HARDWARE_HAL_API_VERSION,
.id = AUDIO_HARDWARE_MODULE_ID,
.name = "TinyALSA-Audio",
.author = "Paul Kocialkowski",
.methods = &audio_hw_module_methods,
},
};

119
tinyalsa_audio/audio_hw.h Normal file
View File

@ -0,0 +1,119 @@
/*
* Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TINYALSA_AUDIO_HW_H
#define TINYALSA_AUDIO_HW_H
#include <hardware/hardware.h>
#include <hardware/audio.h>
#include <system/audio.h>
#include <audio_utils/resampler.h>
#ifdef YAMAHA_MC1N2_AUDIO
#include <yamaha-mc1n2-audio.h>
#endif
#include "mixer.h"
#include "audio_ril_interface.h"
struct tinyalsa_audio_stream_out {
struct audio_stream_out stream;
struct tinyalsa_audio_device *device;
struct tinyalsa_mixer_io_props *mixer_props;
int rate;
audio_channel_mask_t channel_mask;
audio_format_t format;
audio_devices_t device_current;
struct resampler_itfe *resampler;
struct pcm *pcm;
int standby;
pthread_mutex_t lock;
};
struct tinyalsa_audio_stream_in {
struct audio_stream_in stream;
struct tinyalsa_audio_device *device;
struct tinyalsa_mixer_io_props *mixer_props;
int rate;
audio_channel_mask_t channel_mask;
audio_format_t format;
audio_devices_t device_current;
struct resampler_itfe *resampler;
struct resampler_buffer_provider buffer_provider;
void *buffer;
int frames_left;
struct pcm *pcm;
int standby;
pthread_mutex_t lock;
};
struct tinyalsa_audio_device {
struct audio_hw_device device;
struct tinyalsa_audio_stream_out *stream_out;
struct tinyalsa_audio_stream_in *stream_in;
struct tinyalsa_audio_ril_interface *ril_interface;
#ifdef YAMAHA_MC1N2_AUDIO
struct yamaha_mc1n2_audio_pdata *mc1n2_pdata;
#endif
struct tinyalsa_mixer *mixer;
audio_mode_t mode;
float voice_volume;
int mic_mute;
pthread_mutex_t lock;
};
int audio_out_set_route(struct tinyalsa_audio_stream_out *stream_out,
audio_devices_t device);
void audio_hw_close_output_stream(struct audio_hw_device *dev,
struct audio_stream_out *stream);
int audio_hw_open_output_stream(struct audio_hw_device *dev,
audio_io_handle_t handle,
audio_devices_t devices,
audio_output_flags_t flags,
struct audio_config *config,
struct audio_stream_out **stream_out);
int audio_in_set_route(struct tinyalsa_audio_stream_in *stream_in,
audio_devices_t device);
void audio_hw_close_input_stream(struct audio_hw_device *dev,
struct audio_stream_in *stream);
int audio_hw_open_input_stream(struct audio_hw_device *dev,
audio_io_handle_t handle,
audio_devices_t devices,
struct audio_config *config,
struct audio_stream_in **stream_in);
#endif

812
tinyalsa_audio/audio_in.c Normal file
View File

@ -0,0 +1,812 @@
/*
* Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr>
*
* This is based on Galaxy Nexus audio.primary.tuna implementation:
* Copyright 2011, The Android Open-Source Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define LOG_TAG "TinyALSA-Audio Input"
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <sys/time.h>
#include <cutils/str_parms.h>
#include <cutils/log.h>
#define EFFECT_UUID_NULL EFFECT_UUID_NULL_IN
#define EFFECT_UUID_NULL_STR EFFECT_UUID_NULL_STR_IN
#include <audio_utils/resampler.h>
#include "audio_hw.h"
#include "mixer.h"
/*
* Functions
*/
int audio_in_pcm_open(struct tinyalsa_audio_stream_in *stream_in)
{
struct pcm *pcm = NULL;
struct pcm_config pcm_config;
if(stream_in == NULL)
return -1;
memset(&pcm_config, 0, sizeof(pcm_config));
pcm_config.channels = popcount(stream_in->mixer_props->channel_mask);
pcm_config.rate = stream_in->mixer_props->rate;
switch(stream_in->mixer_props->format) {
case AUDIO_FORMAT_PCM_16_BIT:
pcm_config.format = PCM_FORMAT_S16_LE;
break;
case AUDIO_FORMAT_PCM_32_BIT:
pcm_config.format = PCM_FORMAT_S32_LE;
break;
default:
ALOGE("Invalid format: 0x%x", stream_in->mixer_props->format);
return -1;
}
pcm_config.period_size = stream_in->mixer_props->period_size;
pcm_config.period_count = stream_in->mixer_props->period_count;
pcm = pcm_open(stream_in->mixer_props->card,
stream_in->mixer_props->device, PCM_IN, &pcm_config);
if(pcm == NULL || !pcm_is_ready(pcm)) {
ALOGE("Unable to open pcm device: %s", pcm_get_error(pcm));
return -1;
}
stream_in->pcm = pcm;
if(stream_in->resampler != NULL)
stream_in->resampler->reset(stream_in->resampler);
return 0;
}
void audio_in_pcm_close(struct tinyalsa_audio_stream_in *stream_in)
{
if(stream_in->pcm == NULL)
return;
pcm_close(stream_in->pcm);
stream_in->pcm = NULL;
}
int audio_in_set_route(struct tinyalsa_audio_stream_in *stream_in,
audio_devices_t device)
{
int rc;
if(stream_in == NULL)
return -1;
stream_in->device_current = device;
if(device == 0) {
pthread_mutex_unlock(&stream_in->lock);
return stream_in->stream.common.standby((struct audio_stream *) stream_in);
}
tinyalsa_mixer_set_device(stream_in->device->mixer, stream_in->device_current);
#ifdef YAMAHA_MC1N2_AUDIO
yamaha_mc1n2_audio_set_route(stream_in->device->mc1n2_pdata, device);
#endif
return 0;
}
int audio_in_resampler_open(struct tinyalsa_audio_stream_in *stream_in)
{
int rc;
if(stream_in == NULL)
return -1;
rc = create_resampler(stream_in->mixer_props->rate,
stream_in->rate,
popcount(stream_in->mixer_props->channel_mask),
RESAMPLER_QUALITY_DEFAULT,
&stream_in->buffer_provider,
&stream_in->resampler);
if(rc < 0 || stream_in->resampler == NULL) {
ALOGE("Failed to create resampler");
return -1;
}
stream_in->buffer = malloc(stream_in->mixer_props->period_size *
popcount(stream_in->mixer_props->channel_mask) *
audio_bytes_per_sample(stream_in->mixer_props->format));
return 0;
}
void audio_in_resampler_close(struct tinyalsa_audio_stream_in *stream_in)
{
if(stream_in == NULL)
return;
if(stream_in->resampler != NULL) {
release_resampler(stream_in->resampler);
stream_in->resampler = NULL;
}
if(stream_in->buffer != NULL) {
free(stream_in->buffer);
stream_in->buffer = NULL;
}
}
int audio_in_get_next_buffer(struct resampler_buffer_provider *buffer_provider,
struct resampler_buffer *buffer)
{
struct tinyalsa_audio_stream_in *stream_in;
int rc;
if(buffer_provider == NULL || buffer == NULL)
return -1;
stream_in = (struct tinyalsa_audio_stream_in *) ((void *) buffer_provider -
offsetof(struct tinyalsa_audio_stream_in, buffer_provider));
if(stream_in->frames_left == 0) {
if(stream_in->pcm == NULL || !pcm_is_ready(stream_in->pcm)) {
ALOGE("pcm device is not ready");
goto error_pcm;
}
rc = pcm_read(stream_in->pcm, stream_in->buffer,
stream_in->mixer_props->period_size *
popcount(stream_in->mixer_props->channel_mask) *
audio_bytes_per_sample(stream_in->mixer_props->format));
if(rc != 0) {
ALOGE("pcm read failed!");
goto error_pcm;
}
stream_in->frames_left = stream_in->mixer_props->period_size;
}
buffer->frame_count = (buffer->frame_count > stream_in->frames_left) ?
stream_in->frames_left : buffer->frame_count;
buffer->raw = stream_in->buffer +
(stream_in->mixer_props->period_size - stream_in->frames_left) *
popcount(stream_in->mixer_props->channel_mask) *
audio_bytes_per_sample(stream_in->mixer_props->format);
return 0;
error_pcm:
buffer->raw = NULL;
buffer->frame_count = 0;
return -1;
}
void audio_in_release_buffer(struct resampler_buffer_provider *buffer_provider,
struct resampler_buffer *buffer)
{
struct tinyalsa_audio_stream_in *stream_in;
if(buffer_provider == NULL || buffer == NULL)
return;
stream_in = (struct tinyalsa_audio_stream_in *) ((void *) buffer_provider -
offsetof(struct tinyalsa_audio_stream_in, buffer_provider));
stream_in->frames_left -= buffer->frame_count;
}
int audio_in_read_process(struct tinyalsa_audio_stream_in *stream_in, void *buffer, int size)
{
size_t frames_out;
size_t frames_in;
int size_in;
void *buffer_in = NULL;
int frames_out_resampler;
int size_out_resampler;
void *buffer_out_resampler = NULL;
int frames_out_read;
int size_out_read;
void *buffer_out_read = NULL;
int frames_out_channels;
int size_out_channels;
void *buffer_out_channels = NULL;
int i, j;
int rc;
if(stream_in == NULL || buffer == NULL || size <= 0)
return -1;
frames_in = size / audio_stream_frame_size((struct audio_stream *) stream_in);
if(stream_in->resampler != NULL) {
frames_out_resampler = frames_in;
size_out_resampler = frames_out_resampler * popcount(stream_in->mixer_props->channel_mask) * audio_bytes_per_sample(stream_in->mixer_props->format);
buffer_out_resampler = calloc(1, size_out_resampler);
frames_out = 0;
while(frames_out < frames_out_resampler) {
frames_in = frames_out_resampler - frames_out;
stream_in->resampler->resample_from_provider(stream_in->resampler,
buffer_out_resampler + (size_out_resampler / frames_out_resampler) * frames_out,
&frames_in);
frames_out += frames_in;
}
frames_in = frames_out_resampler;
size_in = size_out_resampler;
buffer_in = buffer_out_resampler;
} else {
frames_out_read = frames_in;
size_out_read = frames_out_read * popcount(stream_in->mixer_props->channel_mask) * audio_bytes_per_sample(stream_in->mixer_props->format);
buffer_out_read = calloc(1, size_out_read);
if(stream_in->pcm == NULL || !pcm_is_ready(stream_in->pcm)) {
ALOGE("pcm device is not ready");
goto error;
}
rc = pcm_read(stream_in->pcm, buffer_out_read, size_out_read);
if(rc != 0) {
ALOGE("pcm read failed!");
goto error;
}
frames_in = frames_out_read;
size_in = size_out_read;
buffer_in = buffer_out_read;
}
if(buffer_in == NULL)
goto error;
//FIXME: This is only for PCM 16
if(popcount(stream_in->channel_mask) < popcount(stream_in->mixer_props->channel_mask)) {
frames_out_channels = frames_in;
size_out_channels = frames_out_channels * audio_stream_frame_size((struct audio_stream *) stream_in);
buffer_out_channels = calloc(1, size_out_channels);
int channels_count_in = popcount(stream_in->mixer_props->channel_mask);
int channels_count_out = popcount(stream_in->channel_mask);
int ratio = channels_count_in / channels_count_out;
int16_t *byte_in = (int16_t *) buffer_in;
int16_t *byte_out = (int16_t *) buffer_out_channels;
int16_t byte;
for(i=0 ; i < frames_out_channels * channels_count_out; i++) {
byte = 0;
for(j=0 ; j < ratio ; j++) {
byte += *byte_in / ratio;
byte_in++;
}
*byte_out = byte;
byte_out++;
}
frames_in = frames_out_channels;
size_in = size_out_channels;
buffer_in = buffer_out_channels;
} else if(popcount(stream_in->channel_mask) != popcount(stream_in->mixer_props->channel_mask)) {
ALOGE("Asked for more channels than hardware can provide!");
goto error;
}
if(buffer_in != NULL)
memcpy(buffer, buffer_in, size);
if(buffer_out_resampler != NULL)
free(buffer_out_resampler);
if(buffer_out_read != NULL)
free(buffer_out_read);
if(buffer_out_channels != NULL)
free(buffer_out_channels);
return 0;
error:
if(buffer_out_resampler != NULL)
free(buffer_out_resampler);
if(buffer_out_read != NULL)
free(buffer_out_read);
if(buffer_out_channels != NULL)
free(buffer_out_channels);
return -1;
}
static uint32_t audio_in_get_sample_rate(const struct audio_stream *stream)
{
struct tinyalsa_audio_stream_in *stream_in;
if(stream == NULL)
return 0;
stream_in = (struct tinyalsa_audio_stream_in *) stream;
return stream_in->rate;
}
static int audio_in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
{
struct tinyalsa_audio_stream_in *stream_in;
ALOGD("%s(%p, %d)", __func__, stream, rate);
if(stream == NULL)
return -1;
stream_in = (struct tinyalsa_audio_stream_in *) stream;
if(stream_in->rate != (int) rate) {
pthread_mutex_lock(&stream_in->lock);
stream_in->rate = rate;
if(stream_in->rate != stream_in->mixer_props->rate) {
audio_in_resampler_close(stream_in);
audio_in_resampler_open(stream_in);
stream_in->standby = 1;
}
pthread_mutex_unlock(&stream_in->lock);
}
return 0;
}
static size_t audio_in_get_buffer_size(const struct audio_stream *stream)
{
struct tinyalsa_audio_stream_in *stream_in;
size_t size;
if(stream == NULL)
return -1;
stream_in = (struct tinyalsa_audio_stream_in *) stream;
size = (stream_in->mixer_props->period_size * stream_in->rate) /
stream_in->mixer_props->rate;
size = ((size + 15) / 16) * 16;
size = size * audio_stream_frame_size((struct audio_stream *) stream);
return size;
}
static uint32_t audio_in_get_channels(const struct audio_stream *stream)
{
struct tinyalsa_audio_stream_in *stream_in;
if(stream == NULL)
return -1;
stream_in = (struct tinyalsa_audio_stream_in *) stream;
return stream_in->channel_mask;
}
static audio_format_t audio_in_get_format(const struct audio_stream *stream)
{
struct tinyalsa_audio_stream_in *stream_in;
if(stream == NULL)
return -1;
stream_in = (struct tinyalsa_audio_stream_in *) stream;
return stream_in->format;
}
static int audio_in_set_format(struct audio_stream *stream, audio_format_t format)
{
struct tinyalsa_audio_stream_in *stream_in;
ALOGD("%s(%p, %d)", __func__, stream, format);
if(stream == NULL)
return -1;
stream_in = (struct tinyalsa_audio_stream_in *) stream;
if(stream_in->format != (audio_format_t) format) {
pthread_mutex_lock(&stream_in->lock);
stream_in->format = format;
if(stream_in->format != stream_in->mixer_props->format)
stream_in->standby = 1;
pthread_mutex_unlock(&stream_in->lock);
}
return 0;
}
static int audio_in_standby(struct audio_stream *stream)
{
struct tinyalsa_audio_stream_in *stream_in;
int rc;
ALOGD("%s(%p)", __func__, stream);
if(stream == NULL)
return -1;
stream_in = (struct tinyalsa_audio_stream_in *) stream;
pthread_mutex_lock(&stream_in->lock);
if(stream_in->pcm != NULL)
audio_in_pcm_close(stream_in);
#ifdef YAMAHA_MC1N2_AUDIO
if(!stream_in->standby) {
rc = yamaha_mc1n2_audio_input_stop(stream_in->device->mc1n2_pdata);
if(rc < 0) {
ALOGE("Failed to set Yamaha-MC1N2-Audio route");
}
}
#endif
stream_in->standby = 1;
pthread_mutex_unlock(&stream_in->lock);
return 0;
}
static int audio_in_dump(const struct audio_stream *stream, int fd)
{
ALOGD("%s(%p, %d)", __func__, stream, fd);
return 0;
}
static int audio_in_set_parameters(struct audio_stream *stream, const char *kvpairs)
{
struct tinyalsa_audio_stream_in *stream_in;
struct str_parms *parms;
char value_string[32] = { 0 };
int value;
int rc;
ALOGD("%s(%p, %s)", __func__, stream, kvpairs);
if(stream == NULL || kvpairs == NULL)
return -1;
stream_in = (struct tinyalsa_audio_stream_in *) stream;
if(stream_in->device == NULL || stream_in->device->mixer == NULL)
return -1;
parms = str_parms_create_str(kvpairs);
if(parms == NULL)
return -1;