Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2016 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 #ifndef ANDROID_TYPE_CONVERTER_H_
     18 #define ANDROID_TYPE_CONVERTER_H_
     19 
     20 #include <string>
     21 #include <string.h>
     22 
     23 #include <system/audio.h>
     24 #include <utils/Log.h>
     25 #include <utils/Vector.h>
     26 #include <utils/SortedVector.h>
     27 
     28 #include <media/AudioParameter.h>
     29 #include "convert.h"
     30 
     31 namespace android {
     32 
     33 struct SampleRateTraits
     34 {
     35     typedef uint32_t Type;
     36     typedef SortedVector<Type> Collection;
     37 };
     38 struct DeviceTraits
     39 {
     40     typedef audio_devices_t Type;
     41     typedef Vector<Type> Collection;
     42 };
     43 struct OutputDeviceTraits : public DeviceTraits {};
     44 struct InputDeviceTraits : public DeviceTraits {};
     45 struct OutputFlagTraits
     46 {
     47     typedef audio_output_flags_t Type;
     48     typedef Vector<Type> Collection;
     49 };
     50 struct InputFlagTraits
     51 {
     52     typedef audio_input_flags_t Type;
     53     typedef Vector<Type> Collection;
     54 };
     55 struct FormatTraits
     56 {
     57     typedef audio_format_t Type;
     58     typedef Vector<Type> Collection;
     59 };
     60 struct ChannelTraits
     61 {
     62     typedef audio_channel_mask_t Type;
     63     typedef SortedVector<Type> Collection;
     64 };
     65 struct OutputChannelTraits : public ChannelTraits {};
     66 struct InputChannelTraits : public ChannelTraits {};
     67 struct ChannelIndexTraits : public ChannelTraits {};
     68 struct GainModeTraits
     69 {
     70     typedef audio_gain_mode_t Type;
     71     typedef Vector<Type> Collection;
     72 };
     73 struct StreamTraits
     74 {
     75     typedef audio_stream_type_t Type;
     76     typedef Vector<Type> Collection;
     77 };
     78 struct AudioModeTraits
     79 {
     80     typedef audio_mode_t Type;
     81     typedef Vector<Type> Collection;
     82 };
     83 struct AudioContentTraits
     84 {
     85     typedef audio_content_type_t Type;
     86     typedef Vector<Type> Collection;
     87 };
     88 struct UsageTraits
     89 {
     90     typedef audio_usage_t Type;
     91     typedef Vector<Type> Collection;
     92 };
     93 struct SourceTraits
     94 {
     95     typedef audio_source_t Type;
     96     typedef Vector<Type> Collection;
     97 };
     98 template <typename T>
     99 struct DefaultTraits
    100 {
    101     typedef T Type;
    102     typedef Vector<Type> Collection;
    103 };
    104 
    105 template <class Traits>
    106 static void collectionFromString(const std::string &str, typename Traits::Collection &collection,
    107                                  const char *del = AudioParameter::valueListSeparator)
    108 {
    109     char *literal = strdup(str.c_str());
    110     for (const char *cstr = strtok(literal, del); cstr != NULL; cstr = strtok(NULL, del)) {
    111         typename Traits::Type value;
    112         if (utilities::convertTo<std::string, typename Traits::Type >(cstr, value)) {
    113             collection.add(value);
    114         }
    115     }
    116     free(literal);
    117 }
    118 
    119 template <class Traits>
    120 class TypeConverter
    121 {
    122 public:
    123     static bool toString(const typename Traits::Type &value, std::string &str);
    124 
    125     static bool fromString(const std::string &str, typename Traits::Type &result);
    126 
    127     static void collectionFromString(const std::string &str,
    128                                      typename Traits::Collection &collection,
    129                                      const char *del = AudioParameter::valueListSeparator);
    130 
    131     static uint32_t maskFromString(
    132             const std::string &str, const char *del = AudioParameter::valueListSeparator);
    133 
    134     static void maskToString(
    135             uint32_t mask, std::string &str, const char *del = AudioParameter::valueListSeparator);
    136 
    137 protected:
    138     struct Table {
    139         const char *literal;
    140         typename Traits::Type value;
    141     };
    142 
    143     static const Table mTable[];
    144 };
    145 
    146 template <class Traits>
    147 inline bool TypeConverter<Traits>::toString(const typename Traits::Type &value, std::string &str)
    148 {
    149     for (size_t i = 0; mTable[i].literal; i++) {
    150         if (mTable[i].value == value) {
    151             str = mTable[i].literal;
    152             return true;
    153         }
    154     }
    155     char result[64];
    156     snprintf(result, sizeof(result), "Unknown enum value %d", value);
    157     str = result;
    158     return false;
    159 }
    160 
    161 template <class Traits>
    162 inline bool TypeConverter<Traits>::fromString(const std::string &str, typename Traits::Type &result)
    163 {
    164     for (size_t i = 0; mTable[i].literal; i++) {
    165         if (strcmp(mTable[i].literal, str.c_str()) == 0) {
    166             ALOGV("stringToEnum() found %s", mTable[i].literal);
    167             result = mTable[i].value;
    168             return true;
    169         }
    170     }
    171     return false;
    172 }
    173 
    174 template <class Traits>
    175 inline void TypeConverter<Traits>::collectionFromString(const std::string &str,
    176         typename Traits::Collection &collection,
    177         const char *del)
    178 {
    179     char *literal = strdup(str.c_str());
    180 
    181     for (const char *cstr = strtok(literal, del); cstr != NULL; cstr = strtok(NULL, del)) {
    182         typename Traits::Type value;
    183         if (fromString(cstr, value)) {
    184             collection.add(value);
    185         }
    186     }
    187     free(literal);
    188 }
    189 
    190 template <class Traits>
    191 inline uint32_t TypeConverter<Traits>::maskFromString(const std::string &str, const char *del)
    192 {
    193     char *literal = strdup(str.c_str());
    194     uint32_t value = 0;
    195     for (const char *cstr = strtok(literal, del); cstr != NULL; cstr = strtok(NULL, del)) {
    196         typename Traits::Type type;
    197         if (fromString(cstr, type)) {
    198             value |= static_cast<uint32_t>(type);
    199         }
    200     }
    201     free(literal);
    202     return value;
    203 }
    204 
    205 template <class Traits>
    206 inline void TypeConverter<Traits>::maskToString(uint32_t mask, std::string &str, const char *del)
    207 {
    208     if (mask != 0) {
    209         bool first_flag = true;
    210         for (size_t i = 0; mTable[i].literal; i++) {
    211             uint32_t value = static_cast<uint32_t>(mTable[i].value);
    212             if (mTable[i].value != 0 && ((mask & value) == value)) {
    213                 if (!first_flag) str += del;
    214                 first_flag = false;
    215                 str += mTable[i].literal;
    216             }
    217         }
    218     } else {
    219         toString(static_cast<typename Traits::Type>(0), str);
    220     }
    221 }
    222 
    223 typedef TypeConverter<OutputDeviceTraits> OutputDeviceConverter;
    224 typedef TypeConverter<InputDeviceTraits> InputDeviceConverter;
    225 typedef TypeConverter<OutputFlagTraits> OutputFlagConverter;
    226 typedef TypeConverter<InputFlagTraits> InputFlagConverter;
    227 typedef TypeConverter<FormatTraits> FormatConverter;
    228 typedef TypeConverter<OutputChannelTraits> OutputChannelConverter;
    229 typedef TypeConverter<InputChannelTraits> InputChannelConverter;
    230 typedef TypeConverter<ChannelIndexTraits> ChannelIndexConverter;
    231 typedef TypeConverter<GainModeTraits> GainModeConverter;
    232 typedef TypeConverter<StreamTraits> StreamTypeConverter;
    233 typedef TypeConverter<AudioModeTraits> AudioModeConverter;
    234 typedef TypeConverter<AudioContentTraits> AudioContentTypeConverter;
    235 typedef TypeConverter<UsageTraits> UsageTypeConverter;
    236 typedef TypeConverter<SourceTraits> SourceTypeConverter;
    237 
    238 template<> const OutputDeviceConverter::Table OutputDeviceConverter::mTable[];
    239 template<> const InputDeviceConverter::Table InputDeviceConverter::mTable[];
    240 template<> const OutputFlagConverter::Table OutputFlagConverter::mTable[];
    241 template<> const InputFlagConverter::Table InputFlagConverter::mTable[];
    242 template<> const FormatConverter::Table FormatConverter::mTable[];
    243 template<> const OutputChannelConverter::Table OutputChannelConverter::mTable[];
    244 template<> const InputChannelConverter::Table InputChannelConverter::mTable[];
    245 template<> const ChannelIndexConverter::Table ChannelIndexConverter::mTable[];
    246 template<> const GainModeConverter::Table GainModeConverter::mTable[];
    247 template<> const StreamTypeConverter::Table StreamTypeConverter::mTable[];
    248 template<> const AudioModeConverter::Table AudioModeConverter::mTable[];
    249 template<> const AudioContentTypeConverter::Table AudioContentTypeConverter::mTable[];
    250 template<> const UsageTypeConverter::Table UsageTypeConverter::mTable[];
    251 template<> const SourceTypeConverter::Table SourceTypeConverter::mTable[];
    252 
    253 bool deviceFromString(const std::string& literalDevice, audio_devices_t& device);
    254 
    255 bool deviceToString(audio_devices_t device, std::string& literalDevice);
    256 
    257 SampleRateTraits::Collection samplingRatesFromString(
    258         const std::string &samplingRates, const char *del = AudioParameter::valueListSeparator);
    259 
    260 FormatTraits::Collection formatsFromString(
    261         const std::string &formats, const char *del = AudioParameter::valueListSeparator);
    262 
    263 audio_format_t formatFromString(
    264         const std::string &literalFormat, audio_format_t defaultFormat = AUDIO_FORMAT_DEFAULT);
    265 
    266 audio_channel_mask_t channelMaskFromString(const std::string &literalChannels);
    267 
    268 ChannelTraits::Collection channelMasksFromString(
    269         const std::string &channels, const char *del = AudioParameter::valueListSeparator);
    270 
    271 InputChannelTraits::Collection inputChannelMasksFromString(
    272         const std::string &inChannels, const char *del = AudioParameter::valueListSeparator);
    273 
    274 OutputChannelTraits::Collection outputChannelMasksFromString(
    275         const std::string &outChannels, const char *del = AudioParameter::valueListSeparator);
    276 
    277 }; // namespace android
    278 
    279 #endif  /*ANDROID_TYPE_CONVERTER_H_*/
    280