1 /*
2 * Copyright (c) 2022-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "audio_type_translate.h"
17
18 #include <map>
19 #include <utility>
20
21 #include "audio_errors.h"
22 #include "errors.h"
23
24 namespace {
25 using namespace OHOS;
26 const std::pair<AudioStandard::AudioSamplingRate, uint32_t> g_auSampleRateMap[] = {
27 {AudioStandard::SAMPLE_RATE_8000, 8000},
28 {AudioStandard::SAMPLE_RATE_11025, 11025},
29 {AudioStandard::SAMPLE_RATE_12000, 12000},
30 {AudioStandard::SAMPLE_RATE_16000, 16000},
31 {AudioStandard::SAMPLE_RATE_22050, 22050},
32 {AudioStandard::SAMPLE_RATE_24000, 24000},
33 {AudioStandard::SAMPLE_RATE_32000, 32000},
34 {AudioStandard::SAMPLE_RATE_44100, 44100},
35 {AudioStandard::SAMPLE_RATE_48000, 48000},
36 {AudioStandard::SAMPLE_RATE_64000, 64000},
37 {AudioStandard::SAMPLE_RATE_96000, 96000},
38 };
39 const std::pair<AudioStandard::AudioSampleFormat, Media::Plugin::AudioSampleFormat> g_aduFmtMap[] = {
40 {AudioStandard::SAMPLE_U8, Media::Plugin::AudioSampleFormat::U8},
41 {AudioStandard::SAMPLE_S16LE, Media::Plugin::AudioSampleFormat::S16},
42 {AudioStandard::SAMPLE_S24LE, Media::Plugin::AudioSampleFormat::S24},
43 {AudioStandard::SAMPLE_S32LE, Media::Plugin::AudioSampleFormat::S32}
44 };
45 const std::pair<AudioStandard::AudioChannel, uint32_t> g_auChannelsMap[] = {
46 {AudioStandard::MONO, 1},
47 {AudioStandard::STEREO, 2},
48 };
49 }
50
51 namespace OHOS {
52 namespace Media {
53 namespace AuCapturePlugin {
SampleRateEnum2Num(OHOS::AudioStandard::AudioSamplingRate enumVal,uint32_t & numVal)54 bool SampleRateEnum2Num (OHOS::AudioStandard::AudioSamplingRate enumVal, uint32_t& numVal)
55 {
56 for (const auto& item : g_auSampleRateMap) {
57 if (item.first == enumVal) {
58 numVal = item.second;
59 return true;
60 }
61 }
62 numVal = 0;
63 return false;
64 }
SampleRateNum2Enum(uint32_t numVal,OHOS::AudioStandard::AudioSamplingRate & enumVal)65 bool SampleRateNum2Enum (uint32_t numVal, OHOS::AudioStandard::AudioSamplingRate& enumVal)
66 {
67 for (const auto& item : g_auSampleRateMap) {
68 if (item.second == numVal) {
69 enumVal = item.first;
70 return true;
71 }
72 }
73 return false;
74 }
75
SampleFmt2PluginFmt(OHOS::AudioStandard::AudioSampleFormat aFmt,OHOS::Media::Plugin::AudioSampleFormat & pFmt)76 bool SampleFmt2PluginFmt(OHOS::AudioStandard::AudioSampleFormat aFmt, OHOS::Media::Plugin::AudioSampleFormat& pFmt)
77 {
78 for (const auto& item : g_aduFmtMap) {
79 if (item.first == aFmt) {
80 pFmt = item.second;
81 return true;
82 }
83 }
84 return false;
85 }
PluginFmt2SampleFmt(OHOS::Media::Plugin::AudioSampleFormat pFmt,OHOS::AudioStandard::AudioSampleFormat & aFmt)86 bool PluginFmt2SampleFmt(OHOS::Media::Plugin::AudioSampleFormat pFmt, OHOS::AudioStandard::AudioSampleFormat& aFmt)
87 {
88 for (const auto& item : g_aduFmtMap) {
89 if (item.second == pFmt) {
90 aFmt = item.first;
91 return true;
92 }
93 }
94 return false;
95 }
96
ChannelNumEnum2Num(OHOS::AudioStandard::AudioChannel enumVal,uint32_t & numVal)97 bool ChannelNumEnum2Num(OHOS::AudioStandard::AudioChannel enumVal, uint32_t& numVal)
98 {
99 for (const auto& item : g_auChannelsMap) {
100 if (item.first == enumVal) {
101 numVal = item.second;
102 return true;
103 }
104 }
105 return false;
106 }
ChannelNumNum2Enum(uint32_t numVal,OHOS::AudioStandard::AudioChannel & enumVal)107 bool ChannelNumNum2Enum(uint32_t numVal, OHOS::AudioStandard::AudioChannel& enumVal)
108 {
109 for (const auto& item : g_auChannelsMap) {
110 if (item.second == numVal) {
111 enumVal = item.first;
112 return true;
113 }
114 }
115 return false;
116 }
Error2Status(int32_t err)117 Plugin::Status Error2Status(int32_t err)
118 {
119 const static std::map<int32_t, Plugin::Status> transMap = {
120 {OHOS::AudioStandard::SUCCESS, Plugin::Status::OK},
121 {OHOS::ERR_OK, Plugin::Status::OK},
122 {OHOS::ERR_INVALID_OPERATION, Plugin::Status::ERROR_WRONG_STATE},
123 {OHOS::ERR_NO_MEMORY, Plugin::Status::ERROR_NO_MEMORY},
124 {OHOS::ERR_INVALID_VALUE, Plugin::Status::ERROR_INVALID_PARAMETER},
125 {OHOS::ERR_NAME_NOT_FOUND, Plugin::Status::ERROR_NOT_EXISTED},
126 {OHOS::ERR_PERMISSION_DENIED, Plugin::Status::ERROR_PERMISSION_DENIED},
127 {OHOS::ERR_ENOUGH_DATA, Plugin::Status::ERROR_NOT_ENOUGH_DATA},
128 {OHOS::ERR_WOULD_BLOCK, Plugin::Status::ERROR_AGAIN},
129 {OHOS::ERR_TIMED_OUT, Plugin::Status::ERROR_TIMED_OUT},
130 {OHOS::ERR_ALREADY_EXISTS, Plugin::Status::ERROR_UNKNOWN},
131 {OHOS::ERR_DEAD_OBJECT, Plugin::Status::ERROR_UNKNOWN},
132 {OHOS::ERR_NO_INIT, Plugin::Status::ERROR_UNKNOWN},
133 {OHOS::ERR_OVERFLOW, Plugin::Status::ERROR_UNKNOWN},
134 };
135 if (transMap.count(err) != 0) {
136 return transMap.at(err);
137 }
138 return Plugin::Status::ERROR_UNKNOWN;
139 }
140 } // AuCapturePlugin
141 } // Media
142 } // OHOS