Home | History | Annotate | Download | only in include
      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 
     28 namespace android {
     29 
     30 struct SampleRateTraits
     31 {
     32     typedef uint32_t Type;
     33     typedef SortedVector<Type> Collection;
     34 };
     35 struct DeviceTraits
     36 {
     37     typedef audio_devices_t Type;
     38     typedef Vector<Type> Collection;
     39 };
     40 struct OutputFlagTraits
     41 {
     42     typedef audio_output_flags_t Type;
     43     typedef Vector<Type> Collection;
     44 };
     45 struct InputFlagTraits
     46 {
     47     typedef audio_input_flags_t Type;
     48     typedef Vector<Type> Collection;
     49 };
     50 struct FormatTraits
     51 {
     52     typedef audio_format_t Type;
     53     typedef Vector<Type> Collection;
     54 };
     55 struct ChannelTraits
     56 {
     57     typedef audio_channel_mask_t Type;
     58     typedef SortedVector<Type> Collection;
     59 };
     60 struct OutputChannelTraits : public ChannelTraits {};
     61 struct InputChannelTraits : public ChannelTraits {};
     62 struct ChannelIndexTraits : public ChannelTraits {};
     63 struct GainModeTraits
     64 {
     65     typedef audio_gain_mode_t Type;
     66     typedef Vector<Type> Collection;
     67 };
     68 struct StreamTraits
     69 {
     70   typedef audio_stream_type_t Type;
     71   typedef Vector<Type> Collection;
     72 };
     73 struct DeviceCategoryTraits
     74 {
     75   typedef device_category Type;
     76   typedef Vector<Type> Collection;
     77 };
     78 template <typename T>
     79 struct DefaultTraits
     80 {
     81   typedef T Type;
     82   typedef Vector<Type> Collection;
     83 };
     84 
     85 template <class Traits>
     86 static 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 
     99 template <class Traits>
    100 class TypeConverter
    101 {
    102 public:
    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 
    113 protected:
    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 
    123 typedef TypeConverter<DeviceTraits> DeviceConverter;
    124 typedef TypeConverter<OutputFlagTraits> OutputFlagConverter;
    125 typedef TypeConverter<InputFlagTraits> InputFlagConverter;
    126 typedef TypeConverter<FormatTraits> FormatConverter;
    127 typedef TypeConverter<OutputChannelTraits> OutputChannelConverter;
    128 typedef TypeConverter<InputChannelTraits> InputChannelConverter;
    129 typedef TypeConverter<ChannelIndexTraits> ChannelIndexConverter;
    130 typedef TypeConverter<GainModeTraits> GainModeConverter;
    131 typedef TypeConverter<StreamTraits> StreamTypeConverter;
    132 typedef TypeConverter<DeviceCategoryTraits> DeviceCategoryConverter;
    133 
    134 inline
    135 static 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 
    143 inline
    144 static 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 
    151 inline
    152 static 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 
    162 inline
    163 static 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 
    173 inline
    174 static 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 
    184 inline
    185 static 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 
    194 inline
    195 static 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