From 62bffb6008aad930652d21e9a14b3a790514c5d0 Mon Sep 17 00:00:00 2001 From: Paul Kocialkowski Date: Sun, 28 Oct 2012 21:39:04 +0100 Subject: Audio RIL Interface: Initial commit, implements RIL interface Signed-off-by: Paul Kocialkowski --- Android.mk | 1 + audio_hw.c | 122 ++++++++++++++++++++++--- audio_hw.h | 19 ++-- audio_in.c | 14 ++- audio_out.c | 8 +- audio_ril_interface.c | 205 ++++++++++++++++++++++++++++++++++++++++++ audio_ril_interface.h | 42 +++++++++ include/audio_ril_interface.h | 30 +++++++ 8 files changed, 419 insertions(+), 22 deletions(-) create mode 100644 audio_ril_interface.c create mode 100644 audio_ril_interface.h create mode 100644 include/audio_ril_interface.h diff --git a/Android.mk b/Android.mk index a510621..4a5ac4d 100644 --- a/Android.mk +++ b/Android.mk @@ -23,6 +23,7 @@ LOCAL_SRC_FILES := \ audio_hw.c \ audio_out.c \ audio_in.c \ + audio_ril_interface.c \ mixer.c LOCAL_C_INCLUDES += \ diff --git a/audio_hw.c b/audio_hw.c index b0bda46..0292216 100644 --- a/audio_hw.c +++ b/audio_hw.c @@ -1,6 +1,6 @@ /* * Copyright (C) 2012 Paul Kocialkowski - * + * * This is based on Galaxy Nexus audio.primary.tuna implementation: * Copyright 2011, The Android Open-Source Project * @@ -20,11 +20,13 @@ #define LOG_TAG "TinyALSA-Audio Hardware" +#include #include #include #include #include +#include #include #ifdef YAMAHA_MC1N2_AUDIO @@ -75,6 +77,7 @@ static int audio_hw_init_check(const struct audio_hw_device *dev) static int audio_hw_set_voice_volume(struct audio_hw_device *dev, float volume) { struct tinyalsa_audio_device *device; + audio_devices_t device_modem; LOGD("%s(%p, %f)", __func__, dev, volume); @@ -86,10 +89,23 @@ static int audio_hw_set_voice_volume(struct audio_hw_device *dev, float volume) if(device->mixer == NULL) return -1; - if(device->mode == AUDIO_MODE_IN_CALL) { - // FIXME: Select the device from ril interface - tinyalsa_mixer_set_voice_volume(device->mixer, - AUDIO_DEVICE_OUT_DEFAULT, volume); + if(volume != device->voice_volume) { + 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; } return 0; @@ -116,7 +132,9 @@ static int audio_hw_set_master_volume(struct audio_hw_device *dev, float volume) 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; LOGD("%s(%p, %d)", __func__, dev, mode); @@ -131,7 +149,11 @@ static int audio_hw_set_mode(struct audio_hw_device *dev, int mode) tinyalsa_mixer_set_modem_state(device->mixer, 1); if(device->stream_out != NULL) - tinyalsa_mixer_set_device(device->mixer, device->stream_out->device_current); + 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); @@ -139,6 +161,17 @@ static int audio_hw_set_mode(struct audio_hw_device *dev, int mode) LOGE("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) { + LOGE("Failed to open RIL interface"); + device->ril_interface = NULL; + } else { + device->ril_interface = ril_interface; + + 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); @@ -148,6 +181,9 @@ static int audio_hw_set_mode(struct audio_hw_device *dev, int mode) LOGE("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; @@ -159,6 +195,7 @@ static int audio_hw_set_mode(struct audio_hw_device *dev, int mode) static int audio_hw_set_mic_mute(struct audio_hw_device *dev, bool state) { struct tinyalsa_audio_device *device; + audio_devices_t device_modem; LOGD("%s(%p, %d)", __func__, dev, state); @@ -171,12 +208,27 @@ static int audio_hw_set_mic_mute(struct audio_hw_device *dev, bool state) return -1; if(device->mic_mute != state) { - device->mic_mute = state; + 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); + } + } - // FIXME: Select the device from input if mode isn't in-call, - // select it from ril interface if mode is in-call - tinyalsa_mixer_set_mic_mute(device->mixer, - AUDIO_DEVICE_IN_DEFAULT, state); + device->mic_mute = state; } return 0; @@ -201,9 +253,50 @@ static int audio_hw_get_mic_mute(const struct audio_hw_device *dev, bool *state) 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; + LOGD("%s(%p, %s)", __func__, dev, kvpairs); - return -ENOSYS; + 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); + + if(audio_is_output_device((audio_devices_t) value)) { + if(device->stream_out != NULL && device->stream_out->device_current != (audio_devices_t) value) + audio_out_set_route(device->stream_out, (audio_devices_t) value); + 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) + audio_in_set_route(device->stream_in, (audio_devices_t) value); + } + + str_parms_destroy(parms); + + return 0; + +error_params: + str_parms_destroy(parms); + + return -1; } static char *audio_hw_get_parameters(const struct audio_hw_device *dev, @@ -211,7 +304,7 @@ static char *audio_hw_get_parameters(const struct audio_hw_device *dev, { LOGD("%s(%p, %s)", __func__, dev, keys); - return NULL; + return strdup(""); } static size_t audio_hw_get_input_buffer_size(const struct audio_hw_device *dev, @@ -270,6 +363,9 @@ int audio_hw_open(const hw_module_t *module, const char *name, LOGD("%s(%p, %s, %p)", __func__, module, name, device); + if(device == NULL) + return -EINVAL; + if(strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; diff --git a/audio_hw.h b/audio_hw.h index dc5d395..c7e6d05 100644 --- a/audio_hw.h +++ b/audio_hw.h @@ -26,6 +26,9 @@ #include #endif +#include "mixer.h" +#include "audio_ril_interface.h" + struct tinyalsa_audio_stream_out { struct audio_stream_out stream; struct tinyalsa_audio_device *device; @@ -63,6 +66,7 @@ struct tinyalsa_audio_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; @@ -70,22 +74,27 @@ struct tinyalsa_audio_device { struct tinyalsa_mixer *mixer; audio_mode_t mode; + + float voice_volume; int mic_mute; }; +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, uint32_t devices, int *format, uint32_t *channels, uint32_t *sample_rate, struct audio_stream_out **stream_out); -void audio_hw_close_output_stream(struct audio_hw_device *dev, - struct audio_stream_out *stream); -int audio_out_set_route(struct tinyalsa_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, uint32_t devices, int *format, uint32_t *channels, uint32_t *sample_rate, audio_in_acoustics_t acoustics, struct audio_stream_in **stream_in); -void audio_hw_close_input_stream(struct audio_hw_device *dev, - struct audio_stream_in *stream); #endif diff --git a/audio_in.c b/audio_in.c index a27ab98..560aa25 100644 --- a/audio_in.c +++ b/audio_in.c @@ -247,7 +247,7 @@ static int audio_in_set_parameters(struct audio_stream *stream, const char *kvpa stream_in = (struct tinyalsa_audio_stream_in *) stream; - if(stream_in->device->mixer == NULL) + if(stream_in->device == NULL || stream_in->device->mixer == NULL) return -1; parms = str_parms_create_str(kvpairs); @@ -372,10 +372,18 @@ static int audio_in_remove_audio_effect(const struct audio_stream *stream, effec void audio_hw_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream) { + struct tinyalsa_audio_stream_in *stream_in; struct tinyalsa_audio_device *tinyalsa_audio_device; LOGD("%s(%p)", __func__, stream); + stream_in = (struct tinyalsa_audio_stream_in *) stream; + +#ifdef YAMAHA_MC1N2_AUDIO + if(stream_in != NULL && !stream_in->standby) + yamaha_mc1n2_audio_input_stop(stream_in->device->mc1n2_pdata); +#endif + if(stream != NULL) free(stream); @@ -401,7 +409,7 @@ int audio_hw_open_input_stream(struct audio_hw_device *dev, LOGD("%s(%p, %d, %p, %p, %p, %d, %p)", __func__, dev, devices, format, channels, sample_rate, acoustics, stream_in); - if(dev == NULL) + if(dev == NULL || stream_in == NULL) return -EINVAL; tinyalsa_audio_device = (struct tinyalsa_audio_device *) dev; @@ -474,6 +482,8 @@ int audio_hw_open_input_stream(struct audio_hw_device *dev, error_stream: *stream_in = NULL; + free(tinyalsa_audio_stream_in); + tinyalsa_audio_device->stream_in = NULL; return -1; } diff --git a/audio_out.c b/audio_out.c index 573dd4e..9b814a6 100644 --- a/audio_out.c +++ b/audio_out.c @@ -250,7 +250,7 @@ static int audio_out_set_parameters(struct audio_stream *stream, const char *kvp stream_out = (struct tinyalsa_audio_stream_out *) stream; - if(stream_out->device->mixer == NULL) + if(stream_out->device == NULL || stream_out->device->mixer == NULL) return -1; parms = str_parms_create_str(kvpairs); @@ -265,6 +265,8 @@ static int audio_out_set_parameters(struct audio_stream *stream, const char *kvp if(stream_out->device_current != (audio_devices_t) value) audio_out_set_route(stream_out, (audio_devices_t) value); + if(stream_out->device->ril_interface != NULL && stream_out->device->ril_interface->device_current != (audio_devices_t) value) + audio_ril_interface_set_route(stream_out->device->ril_interface, (audio_devices_t) value); str_parms_destroy(parms); @@ -422,7 +424,7 @@ int audio_hw_open_output_stream(struct audio_hw_device *dev, LOGD("%s(%p, %d, %p, %p, %p, %p)", __func__, dev, devices, format, channels, sample_rate, stream_out); - if(dev == NULL) + if(dev == NULL || stream_out == NULL) return -EINVAL; tinyalsa_audio_device = (struct tinyalsa_audio_device *) dev; @@ -496,6 +498,8 @@ int audio_hw_open_output_stream(struct audio_hw_device *dev, error_stream: *stream_out = NULL; + free(tinyalsa_audio_stream_out); + tinyalsa_audio_device->stream_out = NULL; return -1; } diff --git a/audio_ril_interface.c b/audio_ril_interface.c new file mode 100644 index 0000000..d3a8658 --- /dev/null +++ b/audio_ril_interface.c @@ -0,0 +1,205 @@ +/* + * Copyright (C) 2012 Paul Kocialkowski + * + * 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 . + */ + +#define LOG_TAG "TinyALSA-Audio RIL Interface" + +#include +#include +#include +#include +#include +#include + +#include + +#define EFFECT_UUID_NULL EFFECT_UUID_NULL_RIL +#define EFFECT_UUID_NULL_STR EFFECT_UUID_NULL_STR_RIL +#include "audio_hw.h" + +#include "include/audio_ril_interface.h" +#include "audio_ril_interface.h" + +int audio_ril_interface_set_mic_mute(struct tinyalsa_audio_ril_interface *ril_interface, bool state) +{ + int rc; + + if(ril_interface == NULL) + return -1; + + LOGD("%s(%d)", __func__, state); + + if(ril_interface->interface->mic_mute == NULL) + return -1; + + rc = ril_interface->interface->mic_mute(ril_interface->interface->pdata, (int) state); + if(rc < 0) { + LOGE("Failed to set RIL interface mic mute"); + return -1; + } + + return 0; +} + +int audio_ril_interface_set_voice_volume(struct tinyalsa_audio_ril_interface *ril_interface, + audio_devices_t device, float volume) +{ + int rc; + + if(ril_interface == NULL) + return -1; + + LOGD("%s(%d, %f)", __func__, device, volume); + + if(ril_interface->interface->voice_volume == NULL) + return -1; + + rc = ril_interface->interface->voice_volume(ril_interface->interface->pdata, device, volume); + if(rc < 0) { + LOGE("Failed to set RIL interface voice volume"); + return -1; + } + + return 0; +} + +int audio_ril_interface_set_route(struct tinyalsa_audio_ril_interface *ril_interface, audio_devices_t device) +{ + int rc; + + if(ril_interface == NULL) + return -1; + + LOGD("%s(%d)", __func__, device); + + ril_interface->device_current = device; + + if(ril_interface->interface->route == NULL) + return -1; + + rc = ril_interface->interface->route(ril_interface->interface->pdata, device); + if(rc < 0) { + LOGE("Failed to set RIL interface route"); + return -1; + } + + return 0; +} + +/* + * Interface + */ + +void audio_ril_interface_close(struct audio_hw_device *dev, + struct tinyalsa_audio_ril_interface *ril_interface) +{ + void (*audio_ril_interface_close)(struct audio_ril_interface *interface); + + struct tinyalsa_audio_device *tinyalsa_audio_device; + + LOGD("%s(%p)", __func__, ril_interface); + + if(ril_interface->dl_handle != NULL) { + audio_ril_interface_close = (void (*)(struct audio_ril_interface *interface)) + dlsym(ril_interface->dl_handle, "audio_ril_interface_close"); + + if(audio_ril_interface_close == NULL) { + LOGE("Unable to get close function from: %s", AUDIO_RIL_INTERFACE_LIB); + } else { + audio_ril_interface_close(ril_interface->interface); + } + + dlclose(ril_interface->dl_handle); + ril_interface->dl_handle = NULL; + } + + if(ril_interface != NULL) + free(ril_interface); + + if(dev == NULL) + return; + + tinyalsa_audio_device = (struct tinyalsa_audio_device *) dev; + + tinyalsa_audio_device->ril_interface = NULL; +} + +int audio_ril_interface_open(struct audio_hw_device *dev, audio_devices_t device, + struct tinyalsa_audio_ril_interface **ril_interface) +{ + struct audio_ril_interface *(*audio_ril_interface_open)(void); + + struct tinyalsa_audio_device *tinyalsa_audio_device; + struct tinyalsa_audio_ril_interface *tinyalsa_audio_ril_interface; + struct audio_ril_interface *interface; + void *dl_handle; + int rc; + + LOGD("%s(%p, %d, %p)", __func__, dev, device, ril_interface); + + if(dev == NULL || ril_interface == NULL) + return -EINVAL; + + tinyalsa_audio_device = (struct tinyalsa_audio_device *) dev; + tinyalsa_audio_ril_interface = calloc(1, sizeof(struct tinyalsa_audio_ril_interface)); + + if(tinyalsa_audio_ril_interface == NULL) + return -ENOMEM; + + tinyalsa_audio_ril_interface->device = tinyalsa_audio_device; + tinyalsa_audio_device->ril_interface = tinyalsa_audio_ril_interface; + + dl_handle = dlopen(AUDIO_RIL_INTERFACE_LIB, RTLD_NOW); + if(dl_handle == NULL) { + LOGE("Unable to dlopen lib: %s", AUDIO_RIL_INTERFACE_LIB); + goto error_interface; + } + + audio_ril_interface_open = (struct audio_ril_interface *(*)(void)) + dlsym(dl_handle, "audio_ril_interface_open"); + if(audio_ril_interface_open == NULL) { + LOGE("Unable to get open function from: %s", AUDIO_RIL_INTERFACE_LIB); + goto error_interface; + } + + interface = audio_ril_interface_open(); + if(interface == NULL) { + LOGE("Unable to open audio ril interface"); + goto error_interface; + } + + tinyalsa_audio_ril_interface->interface = interface; + tinyalsa_audio_ril_interface->dl_handle = dl_handle; + + if(device) { + tinyalsa_audio_ril_interface->device_current = device; + audio_ril_interface_set_route(tinyalsa_audio_ril_interface, device); + } + + *ril_interface = tinyalsa_audio_ril_interface; + + return 0; + +error_interface: + *ril_interface = NULL; + free(tinyalsa_audio_ril_interface); + tinyalsa_audio_device->ril_interface = NULL; + + if(dl_handle != NULL) + dlclose(dl_handle); + + return -1; +} diff --git a/audio_ril_interface.h b/audio_ril_interface.h new file mode 100644 index 0000000..c1befae --- /dev/null +++ b/audio_ril_interface.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2012 Paul Kocialkowski + * + * 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 . + */ + +#include "include/audio_ril_interface.h" +#include "audio_hw.h" + +#ifndef TINYALSA_AUDIO_RIL_INTERFACE_H +#define TINYALSA_AUDIO_RIL_INTERFACE_H + +struct tinyalsa_audio_ril_interface { + struct audio_ril_interface *interface; + struct tinyalsa_audio_device *device; + + void *dl_handle; + + audio_devices_t device_current; +}; + +int audio_ril_interface_set_mic_mute(struct tinyalsa_audio_ril_interface *ril_interface, bool state); +int audio_ril_interface_set_voice_volume(struct tinyalsa_audio_ril_interface *ril_interface, audio_devices_t device, float volume); +int audio_ril_interface_set_route(struct tinyalsa_audio_ril_interface *ril_interface, audio_devices_t device); + +void audio_ril_interface_close(struct audio_hw_device *dev, + struct tinyalsa_audio_ril_interface *interface); +int audio_ril_interface_open(struct audio_hw_device *dev, audio_devices_t device, + struct tinyalsa_audio_ril_interface **ril_interface); + +#endif diff --git a/include/audio_ril_interface.h b/include/audio_ril_interface.h new file mode 100644 index 0000000..eab77c1 --- /dev/null +++ b/include/audio_ril_interface.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2012 Paul Kocialkowski + * + * 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 . + */ + +#ifndef TINYALSA_AUDIO_RIL_INTERFACE +#define TINYALSA_AUDIO_RIL_INTERFACE + +#define AUDIO_RIL_INTERFACE_LIB "libaudio-ril-interface.so" + +struct audio_ril_interface { + void *pdata; + int (*mic_mute)(void *pdata, int mute); + int (*voice_volume)(void *pdata, audio_devices_t device, float volume); + int (*route)(void *pdata, audio_devices_t device); +}; + +#endif -- cgit v1.2.3