1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
/*
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/rtp_rtcp/include/rtp_payload_registry.h"
#include <algorithm>
#include "common_types.h" // NOLINT(build/include)
#include "modules/audio_coding/codecs/audio_format_conversion.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "rtc_base/stringutils.h"
namespace webrtc {
namespace {
bool PayloadIsCompatible(const RtpUtility::Payload& payload,
const SdpAudioFormat& audio_format) {
return payload.typeSpecific.is_audio() &&
audio_format.Matches(payload.typeSpecific.audio_payload().format);
}
bool PayloadIsCompatible(const RtpUtility::Payload& payload,
const VideoCodec& video_codec) {
if (!payload.typeSpecific.is_video() ||
_stricmp(payload.name, video_codec.plName) != 0)
return false;
// For H264, profiles must match as well.
if (video_codec.codecType == kVideoCodecH264) {
return video_codec.H264().profile ==
payload.typeSpecific.video_payload().h264_profile;
}
return true;
}
RtpUtility::Payload CreatePayloadType(const SdpAudioFormat& audio_format) {
RTC_DCHECK_GE(audio_format.clockrate_hz, 1000);
return {audio_format.name.c_str(),
PayloadUnion(AudioPayload{audio_format, 0})};
}
RtpVideoCodecTypes ConvertToRtpVideoCodecType(VideoCodecType type) {
switch (type) {
case kVideoCodecVP8:
return kRtpVideoVp8;
case kVideoCodecVP9:
return kRtpVideoVp9;
case kVideoCodecH264:
return kRtpVideoH264;
case kVideoCodecRED:
case kVideoCodecULPFEC:
return kRtpVideoNone;
default:
return kRtpVideoGeneric;
}
}
RtpUtility::Payload CreatePayloadType(const VideoCodec& video_codec) {
VideoPayload p;
p.videoCodecType = ConvertToRtpVideoCodecType(video_codec.codecType);
if (video_codec.codecType == kVideoCodecH264)
p.h264_profile = video_codec.H264().profile;
return {video_codec.plName, PayloadUnion(p)};
}
bool IsPayloadTypeValid(int8_t payload_type) {
assert(payload_type >= 0);
// Sanity check.
switch (payload_type) {
// Reserved payload types to avoid RTCP conflicts when marker bit is set.
case 64: // 192 Full INTRA-frame request.
case 72: // 200 Sender report.
case 73: // 201 Receiver report.
case 74: // 202 Source description.
case 75: // 203 Goodbye.
case 76: // 204 Application-defined.
case 77: // 205 Transport layer FB message.
case 78: // 206 Payload-specific FB message.
case 79: // 207 Extended report.
RTC_LOG(LS_ERROR) << "Can't register invalid receiver payload type: "
<< payload_type;
return false;
default:
return true;
}
}
} // namespace
RTPPayloadRegistry::RTPPayloadRegistry()
: incoming_payload_type_(-1),
last_received_payload_type_(-1),
last_received_media_payload_type_(-1),
rtx_(false),
ssrc_rtx_(0) {}
RTPPayloadRegistry::~RTPPayloadRegistry() = default;
void RTPPayloadRegistry::SetAudioReceivePayloads(
std::map<int, SdpAudioFormat> codecs) {
rtc::CritScope cs(&crit_sect_);
#if RTC_DCHECK_IS_ON
RTC_DCHECK(!used_for_video_);
used_for_audio_ = true;
#endif
payload_type_map_.clear();
for (const auto& kv : codecs) {
const int& rtp_payload_type = kv.first;
const SdpAudioFormat& audio_format = kv.second;
RTC_DCHECK(IsPayloadTypeValid(rtp_payload_type));
payload_type_map_.emplace(rtp_payload_type,
CreatePayloadType(audio_format));
}
// Clear the value of last received payload type since it might mean
// something else now.
last_received_payload_type_ = -1;
last_received_media_payload_type_ = -1;
}
int32_t RTPPayloadRegistry::RegisterReceivePayload(
int payload_type,
const SdpAudioFormat& audio_format,
bool* created_new_payload) {
rtc::CritScope cs(&crit_sect_);
#if RTC_DCHECK_IS_ON
RTC_DCHECK(!used_for_video_);
used_for_audio_ = true;
#endif
*created_new_payload = false;
if (!IsPayloadTypeValid(payload_type))
return -1;
const auto it = payload_type_map_.find(payload_type);
if (it != payload_type_map_.end()) {
// We already use this payload type. Check if it's the same as we already
// have. If same, ignore sending an error.
if (PayloadIsCompatible(it->second, audio_format)) {
it->second.typeSpecific.audio_payload().rate = 0;
return 0;
}
RTC_LOG(LS_ERROR) << "Payload type already registered: " << payload_type;
return -1;
}
// Audio codecs must be unique.
DeregisterAudioCodecOrRedTypeRegardlessOfPayloadType(audio_format);
const auto insert_status =
payload_type_map_.emplace(payload_type, CreatePayloadType(audio_format));
RTC_DCHECK(insert_status.second); // Insertion succeeded.
*created_new_payload = true;
// Successful set of payload type, clear the value of last received payload
// type since it might mean something else.
last_received_payload_type_ = -1;
last_received_media_payload_type_ = -1;
return 0;
}
int32_t RTPPayloadRegistry::RegisterReceivePayload(
const VideoCodec& video_codec) {
rtc::CritScope cs(&crit_sect_);
#if RTC_DCHECK_IS_ON
RTC_DCHECK(!used_for_audio_);
used_for_video_ = true;
#endif
if (!IsPayloadTypeValid(video_codec.plType))
return -1;
auto it = payload_type_map_.find(video_codec.plType);
if (it != payload_type_map_.end()) {
// We already use this payload type. Check if it's the same as we already
// have. If same, ignore sending an error.
if (PayloadIsCompatible(it->second, video_codec))
return 0;
RTC_LOG(LS_ERROR) << "Payload type already registered: "
<< static_cast<int>(video_codec.plType);
return -1;
}
const auto insert_status = payload_type_map_.emplace(
video_codec.plType, CreatePayloadType(video_codec));
RTC_DCHECK(insert_status.second); // Insertion succeeded.
// Successful set of payload type, clear the value of last received payload
// type since it might mean something else.
last_received_payload_type_ = -1;
last_received_media_payload_type_ = -1;
return 0;
}
int32_t RTPPayloadRegistry::DeRegisterReceivePayload(
const int8_t payload_type) {
rtc::CritScope cs(&crit_sect_);
payload_type_map_.erase(payload_type);
return 0;
}
// There can't be several codecs with the same rate, frequency and channels
// for audio codecs, but there can for video.
// Always called from within a critical section.
void RTPPayloadRegistry::DeregisterAudioCodecOrRedTypeRegardlessOfPayloadType(
const SdpAudioFormat& audio_format) {
for (auto iterator = payload_type_map_.begin();
iterator != payload_type_map_.end(); ++iterator) {
if (PayloadIsCompatible(iterator->second, audio_format)) {
// Remove old setting.
payload_type_map_.erase(iterator);
break;
}
}
}
int32_t RTPPayloadRegistry::ReceivePayloadType(
const SdpAudioFormat& audio_format,
int8_t* payload_type) const {
assert(payload_type);
rtc::CritScope cs(&crit_sect_);
for (const auto& it : payload_type_map_) {
if (PayloadIsCompatible(it.second, audio_format)) {
*payload_type = it.first;
return 0;
}
}
return -1;
}
int32_t RTPPayloadRegistry::ReceivePayloadType(const VideoCodec& video_codec,
int8_t* payload_type) const {
assert(payload_type);
rtc::CritScope cs(&crit_sect_);
for (const auto& it : payload_type_map_) {
if (PayloadIsCompatible(it.second, video_codec)) {
*payload_type = it.first;
return 0;
}
}
return -1;
}
bool RTPPayloadRegistry::RtxEnabled() const {
rtc::CritScope cs(&crit_sect_);
return rtx_;
}
bool RTPPayloadRegistry::IsRtxInternal(const RTPHeader& header) const {
return rtx_ && ssrc_rtx_ == header.ssrc;
}
void RTPPayloadRegistry::SetRtxSsrc(uint32_t ssrc) {
rtc::CritScope cs(&crit_sect_);
ssrc_rtx_ = ssrc;
rtx_ = true;
}
bool RTPPayloadRegistry::GetRtxSsrc(uint32_t* ssrc) const {
rtc::CritScope cs(&crit_sect_);
*ssrc = ssrc_rtx_;
return rtx_;
}
void RTPPayloadRegistry::SetRtxPayloadType(int payload_type,
int associated_payload_type) {
rtc::CritScope cs(&crit_sect_);
if (payload_type < 0) {
RTC_LOG(LS_ERROR) << "Invalid RTX payload type: " << payload_type;
return;
}
rtx_payload_type_map_[payload_type] = associated_payload_type;
rtx_ = true;
}
bool RTPPayloadRegistry::IsRed(const RTPHeader& header) const {
rtc::CritScope cs(&crit_sect_);
auto it = payload_type_map_.find(header.payloadType);
return it != payload_type_map_.end() && _stricmp(it->second.name, "red") == 0;
}
int RTPPayloadRegistry::GetPayloadTypeFrequency(
uint8_t payload_type) const {
const auto payload = PayloadTypeToPayload(payload_type);
if (!payload) {
return -1;
}
rtc::CritScope cs(&crit_sect_);
return payload->typeSpecific.is_audio()
? payload->typeSpecific.audio_payload().format.clockrate_hz
: kVideoPayloadTypeFrequency;
}
rtc::Optional<RtpUtility::Payload> RTPPayloadRegistry::PayloadTypeToPayload(
uint8_t payload_type) const {
rtc::CritScope cs(&crit_sect_);
const auto it = payload_type_map_.find(payload_type);
return it == payload_type_map_.end()
? rtc::nullopt
: rtc::Optional<RtpUtility::Payload>(it->second);
}
void RTPPayloadRegistry::SetIncomingPayloadType(const RTPHeader& header) {
rtc::CritScope cs(&crit_sect_);
if (!IsRtxInternal(header))
incoming_payload_type_ = header.payloadType;
}
bool RTPPayloadRegistry::ReportMediaPayloadType(uint8_t media_payload_type) {
rtc::CritScope cs(&crit_sect_);
if (last_received_media_payload_type_ == media_payload_type) {
// Media type unchanged.
return true;
}
last_received_media_payload_type_ = media_payload_type;
return false;
}
// Returns -1 if a payload with name |payload_name| is not registered.
int8_t RTPPayloadRegistry::GetPayloadTypeWithName(
const char* payload_name) const {
rtc::CritScope cs(&crit_sect_);
for (const auto& it : payload_type_map_) {
if (_stricmp(it.second.name, payload_name) == 0)
return it.first;
}
return -1;
}
} // namespace webrtc
|