1 /*
2 * Copyright 2019 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 #define LOG_TAG "BTAudioHalUtils"
18
19 #include "utils.h"
20
21 #include <android-base/logging.h>
22 #include <android-base/strings.h>
23 #include <stdlib.h>
24
25 #include <sstream>
26 #include <vector>
27
28 namespace android {
29 namespace bluetooth {
30 namespace audio {
31 namespace utils {
32
ParseAudioParams(const std::string & params)33 std::unordered_map<std::string, std::string> ParseAudioParams(const std::string& params) {
34 std::vector<std::string> segments = android::base::Split(params, ";");
35 std::unordered_map<std::string, std::string> params_map;
36 for (const auto& segment : segments) {
37 if (segment.length() == 0) {
38 continue;
39 }
40 std::vector<std::string> kv = android::base::Split(segment, "=");
41 if (kv[0].empty()) {
42 LOG(WARNING) << __func__ << ": Invalid audio parameter " << segment;
43 continue;
44 }
45 params_map[kv[0]] = (kv.size() > 1 ? kv[1] : "");
46 }
47 return params_map;
48 }
49
GetAudioParamString(std::unordered_map<std::string,std::string> & params_map)50 std::string GetAudioParamString(std::unordered_map<std::string, std::string>& params_map) {
51 std::ostringstream sout;
52 for (const auto& ptr : params_map) {
53 sout << "key: '" << ptr.first << "' value: '" << ptr.second << "'\n";
54 }
55 return sout.str();
56 }
57
FrameCount(uint64_t microseconds,uint32_t sample_rate)58 size_t FrameCount(uint64_t microseconds, uint32_t sample_rate) {
59 return (microseconds * sample_rate) / 1000000;
60 }
61
62 } // namespace utils
63 } // namespace audio
64 } // namespace bluetooth
65 } // namespace android
66