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 #include <media/TypeConverter.h>
18 
19 namespace android {
20 
21 #define MAKE_STRING_FROM_ENUM(enumval) { #enumval, enumval }
22 #define TERMINATOR { .literal = nullptr }
23 
24 template<>
25 const AudioModeConverter::Table AudioModeConverter::mTable[] = {
26     MAKE_STRING_FROM_ENUM(AUDIO_MODE_INVALID),
27     MAKE_STRING_FROM_ENUM(AUDIO_MODE_CURRENT),
28     MAKE_STRING_FROM_ENUM(AUDIO_MODE_NORMAL),
29     MAKE_STRING_FROM_ENUM(AUDIO_MODE_RINGTONE),
30     MAKE_STRING_FROM_ENUM(AUDIO_MODE_IN_CALL),
31     MAKE_STRING_FROM_ENUM(AUDIO_MODE_IN_COMMUNICATION),
32     MAKE_STRING_FROM_ENUM(AUDIO_MODE_CALL_SCREEN),
33     TERMINATOR
34 };
35 
36 template <>
37 const AudioFlagConverter::Table AudioFlagConverter::mTable[] = {
38     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_NONE),
39     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_AUDIBILITY_ENFORCED),
40     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_SECURE),
41     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_SCO),
42     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_BEACON),
43     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_HW_AV_SYNC),
44     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_HW_HOTWORD),
45     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY),
46     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_BYPASS_MUTE),
47     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_LOW_LATENCY),
48     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_DEEP_BUFFER),
49     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_NO_MEDIA_PROJECTION),
50     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_MUTE_HAPTIC),
51     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_NO_SYSTEM_CAPTURE),
52     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_CAPTURE_PRIVATE),
53     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_CONTENT_SPATIALIZED),
54     MAKE_STRING_FROM_ENUM(AUDIO_FLAG_NEVER_SPATIALIZE),
55     TERMINATOR
56 };
57 
58 template class TypeConverter<OutputDeviceTraits>;
59 template class TypeConverter<InputDeviceTraits>;
60 template class TypeConverter<DeviceTraits>;
61 template class TypeConverter<OutputFlagTraits>;
62 template class TypeConverter<InputFlagTraits>;
63 template class TypeConverter<FormatTraits>;
64 template class TypeConverter<OutputChannelTraits>;
65 template class TypeConverter<InputChannelTraits>;
66 template class TypeConverter<ChannelIndexTraits>;
67 template class TypeConverter<GainModeTraits>;
68 template class TypeConverter<StreamTraits>;
69 template class TypeConverter<AudioModeTraits>;
70 template class TypeConverter<UsageTraits>;
71 template class TypeConverter<SourceTraits>;
72 template class TypeConverter<AudioFlagTraits>;
73 
samplingRatesFromString(const std::string & samplingRates,const char * del)74 SampleRateTraits::Collection samplingRatesFromString(
75         const std::string &samplingRates, const char *del)
76 {
77     SampleRateTraits::Collection samplingRateCollection;
78     collectionFromString<SampleRateTraits>(samplingRates, samplingRateCollection, del);
79     return samplingRateCollection;
80 }
81 
formatsFromString(const std::string & formats,const char * del)82 FormatTraits::Collection formatsFromString(
83         const std::string &formats, const char *del)
84 {
85     FormatTraits::Collection formatCollection;
86     FormatConverter::collectionFromString(formats, formatCollection, del);
87     return formatCollection;
88 }
89 
formatFromString(const std::string & literalFormat,audio_format_t defaultFormat)90 audio_format_t formatFromString(const std::string &literalFormat, audio_format_t defaultFormat)
91 {
92     audio_format_t format;
93     if (!literalFormat.empty() && FormatConverter::fromString(literalFormat, format)) {
94         return format;
95     }
96     return defaultFormat;
97 }
98 
channelMaskFromString(const std::string & literalChannels)99 audio_channel_mask_t channelMaskFromString(const std::string &literalChannels)
100 {
101     audio_channel_mask_t channels;
102     if (!literalChannels.empty() &&
103             audio_channel_mask_from_string(literalChannels.c_str(), &channels)) {
104         return channels;
105     }
106     return AUDIO_CHANNEL_INVALID;
107 }
108 
channelMasksFromString(const std::string & channels,const char * del)109 ChannelTraits::Collection channelMasksFromString(
110         const std::string &channels, const char *del)
111 {
112     ChannelTraits::Collection channelMaskCollection;
113     OutputChannelConverter::collectionFromString(channels, channelMaskCollection, del);
114     InputChannelConverter::collectionFromString(channels, channelMaskCollection, del);
115     ChannelIndexConverter::collectionFromString(channels, channelMaskCollection, del);
116     return channelMaskCollection;
117 }
118 
inputChannelMasksFromString(const std::string & inChannels,const char * del)119 InputChannelTraits::Collection inputChannelMasksFromString(
120         const std::string &inChannels, const char *del)
121 {
122     InputChannelTraits::Collection inputChannelMaskCollection;
123     InputChannelConverter::collectionFromString(inChannels, inputChannelMaskCollection, del);
124     ChannelIndexConverter::collectionFromString(inChannels, inputChannelMaskCollection, del);
125     return inputChannelMaskCollection;
126 }
127 
outputChannelMasksFromString(const std::string & outChannels,const char * del)128 OutputChannelTraits::Collection outputChannelMasksFromString(
129         const std::string &outChannels, const char *del)
130 {
131     OutputChannelTraits::Collection outputChannelMaskCollection;
132     OutputChannelConverter::collectionFromString(outChannels, outputChannelMaskCollection, del);
133     ChannelIndexConverter::collectionFromString(outChannels, outputChannelMaskCollection, del);
134     return outputChannelMaskCollection;
135 }
136 
137 }; // namespace android
138