xref: /aosp_15_r20/external/webrtc/modules/audio_coding/codecs/opus/audio_coder_opus_common.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/audio_coding/codecs/opus/audio_coder_opus_common.h"
12 
13 #include "absl/strings/string_view.h"
14 
15 namespace webrtc {
16 
GetFormatParameter(const SdpAudioFormat & format,absl::string_view param)17 absl::optional<std::string> GetFormatParameter(const SdpAudioFormat& format,
18                                                absl::string_view param) {
19   auto it = format.parameters.find(std::string(param));
20   if (it == format.parameters.end())
21     return absl::nullopt;
22 
23   return it->second;
24 }
25 
26 // Parses a comma-separated string "1,2,0,6" into a std::vector<unsigned char>.
27 template <>
GetFormatParameter(const SdpAudioFormat & format,absl::string_view param)28 absl::optional<std::vector<unsigned char>> GetFormatParameter(
29     const SdpAudioFormat& format,
30     absl::string_view param) {
31   std::vector<unsigned char> result;
32   const std::string comma_separated_list =
33       GetFormatParameter(format, param).value_or("");
34   size_t pos = 0;
35   while (pos < comma_separated_list.size()) {
36     const size_t next_comma = comma_separated_list.find(',', pos);
37     const size_t distance_to_next_comma = next_comma == std::string::npos
38                                               ? std::string::npos
39                                               : (next_comma - pos);
40     auto substring_with_number =
41         comma_separated_list.substr(pos, distance_to_next_comma);
42     auto conv = rtc::StringToNumber<int>(substring_with_number);
43     if (!conv.has_value()) {
44       return absl::nullopt;
45     }
46     result.push_back(*conv);
47     pos += substring_with_number.size() + 1;
48   }
49   return result;
50 }
51 
52 }  // namespace webrtc
53