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 namespace webrtc {
14 
GetFormatParameter(const SdpAudioFormat & format,const std::string & param)15 absl::optional<std::string> GetFormatParameter(const SdpAudioFormat& format,
16                                                const std::string& param) {
17   auto it = format.parameters.find(param);
18   if (it == format.parameters.end())
19     return absl::nullopt;
20 
21   return it->second;
22 }
23 
24 // Parses a comma-separated string "1,2,0,6" into a std::vector<unsigned char>.
25 template <>
GetFormatParameter(const SdpAudioFormat & format,const std::string & param)26 absl::optional<std::vector<unsigned char>> GetFormatParameter(
27     const SdpAudioFormat& format,
28     const std::string& param) {
29   std::vector<unsigned char> result;
30   const std::string comma_separated_list =
31       GetFormatParameter(format, param).value_or("");
32   size_t pos = 0;
33   while (pos < comma_separated_list.size()) {
34     const size_t next_comma = comma_separated_list.find(',', pos);
35     const size_t distance_to_next_comma = next_comma == std::string::npos
36                                               ? std::string::npos
37                                               : (next_comma - pos);
38     auto substring_with_number =
39         comma_separated_list.substr(pos, distance_to_next_comma);
40     auto conv = rtc::StringToNumber<int>(substring_with_number);
41     if (!conv.has_value()) {
42       return absl::nullopt;
43     }
44     result.push_back(*conv);
45     pos += substring_with_number.size() + 1;
46   }
47   return result;
48 }
49 
50 }  // namespace webrtc
51