OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h" | 11 #include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h" |
12 | 12 |
| 13 #include <string.h> |
| 14 |
| 15 #include "webrtc/base/array_view.h" |
13 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
| 17 #include "webrtc/base/optional.h" |
14 #include "webrtc/base/safe_conversions.h" | 18 #include "webrtc/base/safe_conversions.h" |
| 19 #include "webrtc/base/sanitizer.h" |
15 | 20 |
16 namespace webrtc { | 21 namespace webrtc { |
17 | 22 |
| 23 namespace { |
| 24 |
| 25 CodecInst MakeCodecInst(int payload_type, |
| 26 const char* name, |
| 27 int sample_rate, |
| 28 int num_channels) { |
| 29 // Create a CodecInst with some fields set. The remaining fields are zeroed, |
| 30 // but we tell MSan to consider them uninitialized. |
| 31 CodecInst ci = {0}; |
| 32 rtc::MsanMarkUninitialized(rtc::MakeArrayView(&ci, 1)); |
| 33 ci.pltype = payload_type; |
| 34 strncpy(ci.plname, name, sizeof(ci.plname)); |
| 35 ci.plname[sizeof(ci.plname) - 1] = '\0'; |
| 36 ci.plfreq = sample_rate; |
| 37 ci.channels = rtc::checked_cast<size_t>(num_channels); |
| 38 return ci; |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
18 SdpAudioFormat CodecInstToSdp(const CodecInst& ci) { | 43 SdpAudioFormat CodecInstToSdp(const CodecInst& ci) { |
19 if (STR_CASE_CMP(ci.plname, "g722") == 0 && ci.plfreq == 16000) { | 44 if (STR_CASE_CMP(ci.plname, "g722") == 0 && ci.plfreq == 16000) { |
20 RTC_CHECK(ci.channels == 1 || ci.channels == 2); | 45 RTC_CHECK(ci.channels == 1 || ci.channels == 2); |
21 return {"g722", 8000, static_cast<int>(ci.channels)}; | 46 return {"g722", 8000, static_cast<int>(ci.channels)}; |
22 } else if (STR_CASE_CMP(ci.plname, "opus") == 0 && ci.plfreq == 48000) { | 47 } else if (STR_CASE_CMP(ci.plname, "opus") == 0 && ci.plfreq == 48000) { |
23 RTC_CHECK(ci.channels == 1 || ci.channels == 2); | 48 RTC_CHECK(ci.channels == 1 || ci.channels == 2); |
24 return {"opus", 48000, 2, {{"stereo", ci.channels == 1 ? "0" : "1"}}}; | 49 return {"opus", 48000, 2, {{"stereo", ci.channels == 1 ? "0" : "1"}}}; |
25 } else { | 50 } else { |
26 return {ci.plname, ci.plfreq, rtc::checked_cast<int>(ci.channels)}; | 51 return {ci.plname, ci.plfreq, rtc::checked_cast<int>(ci.channels)}; |
27 } | 52 } |
28 } | 53 } |
29 | 54 |
| 55 CodecInst SdpToCodecInst(int payload_type, const SdpAudioFormat& audio_format) { |
| 56 if (STR_CASE_CMP(audio_format.name.c_str(), "g722") == 0 && |
| 57 audio_format.clockrate_hz == 8000 && |
| 58 (audio_format.num_channels == 1 || audio_format.num_channels == 2)) { |
| 59 return MakeCodecInst(payload_type, "g722", 16000, |
| 60 audio_format.num_channels); |
| 61 } else if (STR_CASE_CMP(audio_format.name.c_str(), "opus") == 0 && |
| 62 audio_format.clockrate_hz == 48000 && |
| 63 audio_format.num_channels == 2) { |
| 64 const rtc::Optional<int> num_channels = [&] { |
| 65 const auto stereo = audio_format.parameters.find("stereo"); |
| 66 if (stereo != audio_format.parameters.end()) { |
| 67 if (stereo->second == "0") { |
| 68 return rtc::Optional<int>(1); |
| 69 } else if (stereo->second == "1") { |
| 70 return rtc::Optional<int>(2); |
| 71 } |
| 72 } |
| 73 return rtc::Optional<int>(); |
| 74 }(); |
| 75 if (num_channels) { |
| 76 return MakeCodecInst(payload_type, "opus", 48000, *num_channels); |
| 77 } |
| 78 } |
| 79 |
| 80 return MakeCodecInst(payload_type, audio_format.name.c_str(), |
| 81 audio_format.clockrate_hz, audio_format.num_channels); |
| 82 } |
| 83 |
30 } // namespace webrtc | 84 } // namespace webrtc |
OLD | NEW |