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) { |
| 45 RTC_CHECK_EQ(16000, ci.plfreq); |
20 RTC_CHECK(ci.channels == 1 || ci.channels == 2); | 46 RTC_CHECK(ci.channels == 1 || ci.channels == 2); |
21 return {"g722", 8000, static_cast<int>(ci.channels)}; | 47 return {"g722", 8000, static_cast<int>(ci.channels)}; |
22 } else if (STR_CASE_CMP(ci.plname, "opus") == 0 && ci.plfreq == 48000) { | 48 } else if (STR_CASE_CMP(ci.plname, "opus") == 0) { |
| 49 RTC_CHECK_EQ(48000, ci.plfreq); |
23 RTC_CHECK(ci.channels == 1 || ci.channels == 2); | 50 RTC_CHECK(ci.channels == 1 || ci.channels == 2); |
24 return {"opus", 48000, 2, {{"stereo", ci.channels == 1 ? "0" : "1"}}}; | 51 return ci.channels == 1 |
| 52 ? SdpAudioFormat("opus", 48000, 2) |
| 53 : SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}}); |
25 } else { | 54 } else { |
26 return {ci.plname, ci.plfreq, rtc::checked_cast<int>(ci.channels)}; | 55 return {ci.plname, ci.plfreq, rtc::checked_cast<int>(ci.channels)}; |
27 } | 56 } |
28 } | 57 } |
29 | 58 |
| 59 CodecInst SdpToCodecInst(int payload_type, const SdpAudioFormat& audio_format) { |
| 60 if (STR_CASE_CMP(audio_format.name.c_str(), "g722") == 0) { |
| 61 RTC_CHECK_EQ(8000, audio_format.clockrate_hz); |
| 62 RTC_CHECK(audio_format.num_channels == 1 || audio_format.num_channels == 2); |
| 63 return MakeCodecInst(payload_type, "g722", 16000, |
| 64 audio_format.num_channels); |
| 65 } else if (STR_CASE_CMP(audio_format.name.c_str(), "opus") == 0) { |
| 66 RTC_CHECK_EQ(48000, audio_format.clockrate_hz); |
| 67 RTC_CHECK_EQ(2, audio_format.num_channels); |
| 68 const int num_channels = [&] { |
| 69 auto stereo = audio_format.parameters.find("stereo"); |
| 70 if (stereo != audio_format.parameters.end()) { |
| 71 if (stereo->second == "0") { |
| 72 return 1; |
| 73 } else if (stereo->second == "1") { |
| 74 return 2; |
| 75 } else { |
| 76 RTC_CHECK(false); // Bad stereo parameter. |
| 77 } |
| 78 } |
| 79 return 1; // Default to mono. |
| 80 }(); |
| 81 return MakeCodecInst(payload_type, "opus", 48000, num_channels); |
| 82 } else { |
| 83 return MakeCodecInst(payload_type, audio_format.name.c_str(), |
| 84 audio_format.clockrate_hz, audio_format.num_channels); |
| 85 } |
| 86 } |
| 87 |
30 } // namespace webrtc | 88 } // namespace webrtc |
OLD | NEW |