phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 1 | /* |
phoglund@webrtc.org | 244251a | 2013-02-04 13:23:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/rtp_rtcp/include/rtp_payload_registry.h" |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 12 | |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "modules/audio_coding/codecs/audio_format_conversion.h" |
| 16 | #include "rtc_base/checks.h" |
| 17 | #include "rtc_base/logging.h" |
| 18 | #include "rtc_base/stringutils.h" |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 22 | namespace { |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 23 | |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 24 | bool PayloadIsCompatible(const RtpUtility::Payload& payload, |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 25 | const SdpAudioFormat& audio_format) { |
| 26 | return payload.typeSpecific.is_audio() && |
| 27 | audio_format.Matches(payload.typeSpecific.audio_payload().format); |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | bool PayloadIsCompatible(const RtpUtility::Payload& payload, |
| 31 | const VideoCodec& video_codec) { |
Karl Wiberg | c856dc2 | 2017-09-28 20:13:59 +0200 | [diff] [blame] | 32 | if (!payload.typeSpecific.is_video() || |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 33 | _stricmp(payload.name, CodecTypeToPayloadString(video_codec.codecType)) != |
| 34 | 0) |
magjed | e69a1a9 | 2016-11-25 10:06:31 -0800 | [diff] [blame] | 35 | return false; |
| 36 | // For H264, profiles must match as well. |
| 37 | if (video_codec.codecType == kVideoCodecH264) { |
| 38 | return video_codec.H264().profile == |
Karl Wiberg | c856dc2 | 2017-09-28 20:13:59 +0200 | [diff] [blame] | 39 | payload.typeSpecific.video_payload().h264_profile; |
magjed | e69a1a9 | 2016-11-25 10:06:31 -0800 | [diff] [blame] | 40 | } |
| 41 | return true; |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 44 | RtpUtility::Payload CreatePayloadType(const SdpAudioFormat& audio_format) { |
| 45 | RTC_DCHECK_GE(audio_format.clockrate_hz, 1000); |
| 46 | return {audio_format.name.c_str(), |
| 47 | PayloadUnion(AudioPayload{audio_format, 0})}; |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 48 | } |
| 49 | |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 50 | RtpUtility::Payload CreatePayloadType(const VideoCodec& video_codec) { |
Karl Wiberg | 83d3ec1 | 2017-09-28 19:54:38 +0200 | [diff] [blame] | 51 | VideoPayload p; |
Kári Tristan Helgason | 01a8990 | 2018-08-27 13:26:52 +0200 | [diff] [blame] | 52 | p.videoCodecType = video_codec.codecType; |
magjed | e69a1a9 | 2016-11-25 10:06:31 -0800 | [diff] [blame] | 53 | if (video_codec.codecType == kVideoCodecH264) |
Karl Wiberg | 83d3ec1 | 2017-09-28 19:54:38 +0200 | [diff] [blame] | 54 | p.h264_profile = video_codec.H264().profile; |
Niels Möller | 2e1d784 | 2018-02-23 15:41:13 +0100 | [diff] [blame] | 55 | return {CodecTypeToPayloadString(video_codec.codecType), PayloadUnion(p)}; |
magjed | 56124bd | 2016-11-24 09:34:46 -0800 | [diff] [blame] | 56 | } |
| 57 | |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 58 | bool IsPayloadTypeValid(int8_t payload_type) { |
pbos@webrtc.org | b5bf54c | 2013-04-05 13:27:38 +0000 | [diff] [blame] | 59 | assert(payload_type >= 0); |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 60 | |
| 61 | // Sanity check. |
| 62 | switch (payload_type) { |
| 63 | // Reserved payload types to avoid RTCP conflicts when marker bit is set. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 64 | case 64: // 192 Full INTRA-frame request. |
| 65 | case 72: // 200 Sender report. |
| 66 | case 73: // 201 Receiver report. |
| 67 | case 74: // 202 Source description. |
| 68 | case 75: // 203 Goodbye. |
| 69 | case 76: // 204 Application-defined. |
| 70 | case 77: // 205 Transport layer FB message. |
| 71 | case 78: // 206 Payload-specific FB message. |
| 72 | case 79: // 207 Extended report. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 73 | RTC_LOG(LS_ERROR) << "Can't register invalid receiver payload type: " |
| 74 | << payload_type; |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 75 | return false; |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 76 | default: |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 77 | return true; |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 78 | } |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 79 | } |
pbos@webrtc.org | b5bf54c | 2013-04-05 13:27:38 +0000 | [diff] [blame] | 80 | |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 81 | } // namespace |
| 82 | |
Niels Möller | fd77b78 | 2018-08-06 12:40:58 +0200 | [diff] [blame] | 83 | RTPPayloadRegistry::RTPPayloadRegistry() = default; |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 84 | |
| 85 | RTPPayloadRegistry::~RTPPayloadRegistry() = default; |
| 86 | |
kwiberg | 1c07c70 | 2017-03-27 07:15:49 -0700 | [diff] [blame] | 87 | void RTPPayloadRegistry::SetAudioReceivePayloads( |
| 88 | std::map<int, SdpAudioFormat> codecs) { |
| 89 | rtc::CritScope cs(&crit_sect_); |
| 90 | |
| 91 | #if RTC_DCHECK_IS_ON |
| 92 | RTC_DCHECK(!used_for_video_); |
| 93 | used_for_audio_ = true; |
| 94 | #endif |
| 95 | |
| 96 | payload_type_map_.clear(); |
| 97 | for (const auto& kv : codecs) { |
| 98 | const int& rtp_payload_type = kv.first; |
| 99 | const SdpAudioFormat& audio_format = kv.second; |
kwiberg | 1c07c70 | 2017-03-27 07:15:49 -0700 | [diff] [blame] | 100 | RTC_DCHECK(IsPayloadTypeValid(rtp_payload_type)); |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 101 | payload_type_map_.emplace(rtp_payload_type, |
| 102 | CreatePayloadType(audio_format)); |
kwiberg | 1c07c70 | 2017-03-27 07:15:49 -0700 | [diff] [blame] | 103 | } |
kwiberg | 1c07c70 | 2017-03-27 07:15:49 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 106 | int32_t RTPPayloadRegistry::RegisterReceivePayload( |
| 107 | int payload_type, |
| 108 | const SdpAudioFormat& audio_format, |
| 109 | bool* created_new_payload) { |
kwiberg | b0bf93a | 2017-03-23 00:10:09 -0700 | [diff] [blame] | 110 | rtc::CritScope cs(&crit_sect_); |
| 111 | |
| 112 | #if RTC_DCHECK_IS_ON |
| 113 | RTC_DCHECK(!used_for_video_); |
| 114 | used_for_audio_ = true; |
| 115 | #endif |
| 116 | |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 117 | *created_new_payload = false; |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 118 | if (!IsPayloadTypeValid(payload_type)) |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 119 | return -1; |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 120 | |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 121 | const auto it = payload_type_map_.find(payload_type); |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 122 | if (it != payload_type_map_.end()) { |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 123 | // We already use this payload type. Check if it's the same as we already |
| 124 | // have. If same, ignore sending an error. |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 125 | if (PayloadIsCompatible(it->second, audio_format)) { |
Karl Wiberg | c856dc2 | 2017-09-28 20:13:59 +0200 | [diff] [blame] | 126 | it->second.typeSpecific.audio_payload().rate = 0; |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 127 | return 0; |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 128 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 129 | RTC_LOG(LS_ERROR) << "Payload type already registered: " << payload_type; |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 130 | return -1; |
| 131 | } |
| 132 | |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 133 | // Audio codecs must be unique. |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 134 | DeregisterAudioCodecOrRedTypeRegardlessOfPayloadType(audio_format); |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 135 | |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 136 | const auto insert_status = |
| 137 | payload_type_map_.emplace(payload_type, CreatePayloadType(audio_format)); |
Karl Wiberg | 83d3ec1 | 2017-09-28 19:54:38 +0200 | [diff] [blame] | 138 | RTC_DCHECK(insert_status.second); // Insertion succeeded. |
Minyue Li | 190c3ca | 2015-03-25 16:11:24 +0100 | [diff] [blame] | 139 | *created_new_payload = true; |
| 140 | |
Niels Möller | fd77b78 | 2018-08-06 12:40:58 +0200 | [diff] [blame] | 141 | // Successful set of payload type. |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | int32_t RTPPayloadRegistry::RegisterReceivePayload( |
| 146 | const VideoCodec& video_codec) { |
kwiberg | b0bf93a | 2017-03-23 00:10:09 -0700 | [diff] [blame] | 147 | rtc::CritScope cs(&crit_sect_); |
| 148 | |
| 149 | #if RTC_DCHECK_IS_ON |
| 150 | RTC_DCHECK(!used_for_audio_); |
| 151 | used_for_video_ = true; |
| 152 | #endif |
| 153 | |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 154 | if (!IsPayloadTypeValid(video_codec.plType)) |
| 155 | return -1; |
| 156 | |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 157 | auto it = payload_type_map_.find(video_codec.plType); |
| 158 | if (it != payload_type_map_.end()) { |
| 159 | // We already use this payload type. Check if it's the same as we already |
| 160 | // have. If same, ignore sending an error. |
| 161 | if (PayloadIsCompatible(it->second, video_codec)) |
| 162 | return 0; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 163 | RTC_LOG(LS_ERROR) << "Payload type already registered: " |
| 164 | << static_cast<int>(video_codec.plType); |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 165 | return -1; |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 166 | } |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 167 | |
Karl Wiberg | 83d3ec1 | 2017-09-28 19:54:38 +0200 | [diff] [blame] | 168 | const auto insert_status = payload_type_map_.emplace( |
| 169 | video_codec.plType, CreatePayloadType(video_codec)); |
| 170 | RTC_DCHECK(insert_status.second); // Insertion succeeded. |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 171 | |
Niels Möller | fd77b78 | 2018-08-06 12:40:58 +0200 | [diff] [blame] | 172 | // Successful set of payload type. |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 176 | int32_t RTPPayloadRegistry::DeRegisterReceivePayload( |
| 177 | const int8_t payload_type) { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 178 | rtc::CritScope cs(&crit_sect_); |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 179 | payload_type_map_.erase(payload_type); |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
phoglund@webrtc.org | 244251a | 2013-02-04 13:23:07 +0000 | [diff] [blame] | 183 | // There can't be several codecs with the same rate, frequency and channels |
| 184 | // for audio codecs, but there can for video. |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 185 | // Always called from within a critical section. |
phoglund@webrtc.org | 244251a | 2013-02-04 13:23:07 +0000 | [diff] [blame] | 186 | void RTPPayloadRegistry::DeregisterAudioCodecOrRedTypeRegardlessOfPayloadType( |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 187 | const SdpAudioFormat& audio_format) { |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 188 | for (auto iterator = payload_type_map_.begin(); |
| 189 | iterator != payload_type_map_.end(); ++iterator) { |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 190 | if (PayloadIsCompatible(iterator->second, audio_format)) { |
magjed | f3feeff | 2016-11-25 06:40:25 -0800 | [diff] [blame] | 191 | // Remove old setting. |
| 192 | payload_type_map_.erase(iterator); |
| 193 | break; |
phoglund@webrtc.org | 244251a | 2013-02-04 13:23:07 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 198 | int RTPPayloadRegistry::GetPayloadTypeFrequency(uint8_t payload_type) const { |
Karl Wiberg | 73b60b8 | 2017-09-21 15:00:58 +0200 | [diff] [blame] | 199 | const auto payload = PayloadTypeToPayload(payload_type); |
danilchap | 5c1def8 | 2015-12-10 09:51:54 -0800 | [diff] [blame] | 200 | if (!payload) { |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 201 | return -1; |
| 202 | } |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 203 | rtc::CritScope cs(&crit_sect_); |
Karl Wiberg | c856dc2 | 2017-09-28 20:13:59 +0200 | [diff] [blame] | 204 | return payload->typeSpecific.is_audio() |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 205 | ? payload->typeSpecific.audio_payload().format.clockrate_hz |
Karl Wiberg | c856dc2 | 2017-09-28 20:13:59 +0200 | [diff] [blame] | 206 | : kVideoPayloadTypeFrequency; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Danil Chapovalov | d264df5 | 2018-06-14 12:59:38 +0200 | [diff] [blame] | 209 | absl::optional<RtpUtility::Payload> RTPPayloadRegistry::PayloadTypeToPayload( |
danilchap | 5c1def8 | 2015-12-10 09:51:54 -0800 | [diff] [blame] | 210 | uint8_t payload_type) const { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 211 | rtc::CritScope cs(&crit_sect_); |
Karl Wiberg | 73b60b8 | 2017-09-21 15:00:58 +0200 | [diff] [blame] | 212 | const auto it = payload_type_map_.find(payload_type); |
| 213 | return it == payload_type_map_.end() |
Danil Chapovalov | d264df5 | 2018-06-14 12:59:38 +0200 | [diff] [blame] | 214 | ? absl::nullopt |
| 215 | : absl::optional<RtpUtility::Payload>(it->second); |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 216 | } |
| 217 | |
phoglund@webrtc.org | efae5d5 | 2013-01-17 16:10:45 +0000 | [diff] [blame] | 218 | } // namespace webrtc |