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 <cstring> | |
the sun
2016/12/08 10:53:52
The code base generally uses string.h
kwiberg-webrtc
2016/12/09 02:39:01
Yes, unfortunately. Fixed.
| |
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 MakeCI(int payload_type, | |
the sun
2016/12/08 10:53:52
MakeCodecInst
kwiberg-webrtc
2016/12/09 02:39:02
Done.
| |
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)); | |
the sun
2016/12/08 10:53:52
nice!
| |
33 ci.pltype = payload_type; | |
34 std::strncpy(ci.plname, name, sizeof(ci.plname)); | |
35 ci.plname[sizeof(ci.plname) - 1] = '\0'; | |
the sun
2016/12/08 10:53:52
don't you need to init the trailing zero as well?
kwiberg-webrtc
2016/12/09 02:39:01
No, strncpy copies the null terminator---unless th
the sun
2016/12/09 12:29:49
Ah, sorry. Read wrong.
| |
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 MakeCI(payload_type, "g722", 16000, audio_format.num_channels); | |
the sun
2016/12/08 10:53:52
no other parameters that would need to be copied?
kwiberg-webrtc
2016/12/09 02:39:01
Like what? CodecInst doesn't have the extra parame
the sun
2016/12/09 12:29:49
Acknowledged.
| |
60 } else if (STR_CASE_CMP(audio_format.name.c_str(), "opus") == 0 && | |
61 audio_format.clockrate_hz == 48000 && | |
62 audio_format.num_channels == 2) { | |
63 const rtc::Optional<int> num_channels = [&] { | |
64 const auto stereo = audio_format.parameters.find("stereo"); | |
65 if (stereo != audio_format.parameters.end()) { | |
66 if (stereo->second == "0") { | |
67 return rtc::Optional<int>(1); | |
68 } else if (stereo->second == "1") { | |
69 return rtc::Optional<int>(2); | |
70 } | |
71 } | |
72 return rtc::Optional<int>(); | |
73 }(); | |
74 if (num_channels) { | |
75 return MakeCI(payload_type, "opus", 48000, *num_channels); | |
76 } | |
77 } | |
78 | |
79 return MakeCI(payload_type, audio_format.name.c_str(), | |
80 audio_format.clockrate_hz, audio_format.num_channels); | |
81 } | |
82 | |
30 } // namespace webrtc | 83 } // namespace webrtc |
OLD | NEW |