1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#pragma once 18 19#include "policy.h" 20#include <Volume.h> 21#include <system/audio.h> 22#include <convert/convert.h> 23#include <utils/Log.h> 24#include <string> 25#include <utils/Vector.h> 26#include <utils/SortedVector.h> 27 28namespace android { 29 30struct SampleRateTraits 31{ 32 typedef uint32_t Type; 33 typedef SortedVector<Type> Collection; 34}; 35struct DeviceTraits 36{ 37 typedef audio_devices_t Type; 38 typedef Vector<Type> Collection; 39}; 40struct OutputFlagTraits 41{ 42 typedef audio_output_flags_t Type; 43 typedef Vector<Type> Collection; 44}; 45struct InputFlagTraits 46{ 47 typedef audio_input_flags_t Type; 48 typedef Vector<Type> Collection; 49}; 50struct FormatTraits 51{ 52 typedef audio_format_t Type; 53 typedef Vector<Type> Collection; 54}; 55struct ChannelTraits 56{ 57 typedef audio_channel_mask_t Type; 58 typedef SortedVector<Type> Collection; 59}; 60struct OutputChannelTraits : public ChannelTraits {}; 61struct InputChannelTraits : public ChannelTraits {}; 62struct ChannelIndexTraits : public ChannelTraits {}; 63struct GainModeTraits 64{ 65 typedef audio_gain_mode_t Type; 66 typedef Vector<Type> Collection; 67}; 68struct StreamTraits 69{ 70 typedef audio_stream_type_t Type; 71 typedef Vector<Type> Collection; 72}; 73struct DeviceCategoryTraits 74{ 75 typedef device_category Type; 76 typedef Vector<Type> Collection; 77}; 78template <typename T> 79struct DefaultTraits 80{ 81 typedef T Type; 82 typedef Vector<Type> Collection; 83}; 84 85template <class Traits> 86static void collectionFromString(const std::string &str, typename Traits::Collection &collection, 87 const char *del = "|") 88{ 89 char *literal = strdup(str.c_str()); 90 for (const char *cstr = strtok(literal, del); cstr != NULL; cstr = strtok(NULL, del)) { 91 typename Traits::Type value; 92 if (utilities::convertTo<std::string, typename Traits::Type >(cstr, value)) { 93 collection.add(value); 94 } 95 } 96 free(literal); 97} 98 99template <class Traits> 100class TypeConverter 101{ 102public: 103 static bool toString(const typename Traits::Type &value, std::string &str); 104 105 static bool fromString(const std::string &str, typename Traits::Type &result); 106 107 static void collectionFromString(const std::string &str, 108 typename Traits::Collection &collection, 109 const char *del = "|"); 110 111 static uint32_t maskFromString(const std::string &str, const char *del = "|"); 112 113protected: 114 struct Table { 115 const char *literal; 116 typename Traits::Type value; 117 }; 118 119 static const Table mTable[]; 120 static const size_t mSize; 121}; 122 123typedef TypeConverter<DeviceTraits> DeviceConverter; 124typedef TypeConverter<OutputFlagTraits> OutputFlagConverter; 125typedef TypeConverter<InputFlagTraits> InputFlagConverter; 126typedef TypeConverter<FormatTraits> FormatConverter; 127typedef TypeConverter<OutputChannelTraits> OutputChannelConverter; 128typedef TypeConverter<InputChannelTraits> InputChannelConverter; 129typedef TypeConverter<ChannelIndexTraits> ChannelIndexConverter; 130typedef TypeConverter<GainModeTraits> GainModeConverter; 131typedef TypeConverter<StreamTraits> StreamTypeConverter; 132typedef TypeConverter<DeviceCategoryTraits> DeviceCategoryConverter; 133 134inline 135static SampleRateTraits::Collection samplingRatesFromString(const std::string &samplingRates, 136 const char *del = "|") 137{ 138 SampleRateTraits::Collection samplingRateCollection; 139 collectionFromString<SampleRateTraits>(samplingRates, samplingRateCollection, del); 140 return samplingRateCollection; 141} 142 143inline 144static FormatTraits::Collection formatsFromString(const std::string &formats, const char *del = "|") 145{ 146 FormatTraits::Collection formatCollection; 147 FormatConverter::collectionFromString(formats, formatCollection, del); 148 return formatCollection; 149} 150 151inline 152static audio_format_t formatFromString(const std::string &literalFormat) 153{ 154 audio_format_t format; 155 if (literalFormat.empty()) { 156 return gDynamicFormat; 157 } 158 FormatConverter::fromString(literalFormat, format); 159 return format; 160} 161 162inline 163static audio_channel_mask_t channelMaskFromString(const std::string &literalChannels) 164{ 165 audio_channel_mask_t channels; 166 if (!OutputChannelConverter::fromString(literalChannels, channels) || 167 !InputChannelConverter::fromString(literalChannels, channels)) { 168 return AUDIO_CHANNEL_INVALID; 169 } 170 return channels; 171} 172 173inline 174static ChannelTraits::Collection channelMasksFromString(const std::string &channels, 175 const char *del = "|") 176{ 177 ChannelTraits::Collection channelMaskCollection; 178 OutputChannelConverter::collectionFromString(channels, channelMaskCollection, del); 179 InputChannelConverter::collectionFromString(channels, channelMaskCollection, del); 180 ChannelIndexConverter::collectionFromString(channels, channelMaskCollection, del); 181 return channelMaskCollection; 182} 183 184inline 185static InputChannelTraits::Collection inputChannelMasksFromString(const std::string &inChannels, 186 const char *del = "|") 187{ 188 InputChannelTraits::Collection inputChannelMaskCollection; 189 InputChannelConverter::collectionFromString(inChannels, inputChannelMaskCollection, del); 190 ChannelIndexConverter::collectionFromString(inChannels, inputChannelMaskCollection, del); 191 return inputChannelMaskCollection; 192} 193 194inline 195static OutputChannelTraits::Collection outputChannelMasksFromString(const std::string &outChannels, 196 const char *del = "|") 197{ 198 OutputChannelTraits::Collection outputChannelMaskCollection; 199 OutputChannelConverter::collectionFromString(outChannels, outputChannelMaskCollection, del); 200 ChannelIndexConverter::collectionFromString(outChannels, outputChannelMaskCollection, del); 201 return outputChannelMaskCollection; 202} 203 204}; // namespace android 205 206