2013-01-17 16:10:45 +00:00
|
|
|
/*
|
2013-02-04 13:23:07 +00:00
|
|
|
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
|
2013-01-17 16:10:45 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/rtp_rtcp/include/rtp_payload_registry.h"
|
2013-01-17 16:10:45 +00:00
|
|
|
|
2016-11-25 06:40:25 -08:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
2017-09-15 13:58:09 +02:00
|
|
|
#include "common_types.h" // NOLINT(build/include)
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_coding/codecs/audio_format_conversion.h"
|
|
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
|
#include "rtc_base/logging.h"
|
|
|
|
|
#include "rtc_base/stringutils.h"
|
2013-01-17 16:10:45 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2016-11-25 06:40:25 -08:00
|
|
|
namespace {
|
2016-11-24 09:34:46 -08:00
|
|
|
|
2016-11-25 06:40:25 -08:00
|
|
|
bool PayloadIsCompatible(const RtpUtility::Payload& payload,
|
2017-10-04 12:38:53 +02:00
|
|
|
const SdpAudioFormat& audio_format) {
|
|
|
|
|
return payload.typeSpecific.is_audio() &&
|
|
|
|
|
audio_format.Matches(payload.typeSpecific.audio_payload().format);
|
2016-11-24 09:34:46 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-25 06:40:25 -08:00
|
|
|
bool PayloadIsCompatible(const RtpUtility::Payload& payload,
|
|
|
|
|
const VideoCodec& video_codec) {
|
2017-09-28 20:13:59 +02:00
|
|
|
if (!payload.typeSpecific.is_video() ||
|
2018-02-23 15:41:13 +01:00
|
|
|
_stricmp(payload.name,
|
|
|
|
|
CodecTypeToPayloadString(video_codec.codecType)) != 0)
|
2016-11-25 10:06:31 -08:00
|
|
|
return false;
|
|
|
|
|
// For H264, profiles must match as well.
|
|
|
|
|
if (video_codec.codecType == kVideoCodecH264) {
|
|
|
|
|
return video_codec.H264().profile ==
|
2017-09-28 20:13:59 +02:00
|
|
|
payload.typeSpecific.video_payload().h264_profile;
|
2016-11-25 10:06:31 -08:00
|
|
|
}
|
|
|
|
|
return true;
|
2016-11-24 10:43:42 -08:00
|
|
|
}
|
|
|
|
|
|
2017-10-04 12:38:53 +02:00
|
|
|
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})};
|
2016-11-24 10:43:42 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-25 06:40:25 -08:00
|
|
|
RtpVideoCodecTypes ConvertToRtpVideoCodecType(VideoCodecType type) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case kVideoCodecVP8:
|
|
|
|
|
return kRtpVideoVp8;
|
|
|
|
|
case kVideoCodecVP9:
|
|
|
|
|
return kRtpVideoVp9;
|
|
|
|
|
case kVideoCodecH264:
|
|
|
|
|
return kRtpVideoH264;
|
|
|
|
|
case kVideoCodecRED:
|
|
|
|
|
case kVideoCodecULPFEC:
|
|
|
|
|
return kRtpVideoNone;
|
2018-01-23 15:53:06 -08:00
|
|
|
case kVideoCodecI420:
|
|
|
|
|
case kVideoCodecFlexfec:
|
|
|
|
|
case kVideoCodecGeneric:
|
2018-01-25 13:01:09 -08:00
|
|
|
case kVideoCodecMultiplex:
|
2018-01-23 15:53:06 -08:00
|
|
|
case kVideoCodecUnknown:
|
2016-11-25 06:40:25 -08:00
|
|
|
return kRtpVideoGeneric;
|
|
|
|
|
}
|
2018-01-23 15:53:06 -08:00
|
|
|
return kRtpVideoGeneric;
|
2016-11-25 06:40:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RtpUtility::Payload CreatePayloadType(const VideoCodec& video_codec) {
|
2017-09-28 19:54:38 +02:00
|
|
|
VideoPayload p;
|
|
|
|
|
p.videoCodecType = ConvertToRtpVideoCodecType(video_codec.codecType);
|
2016-11-25 10:06:31 -08:00
|
|
|
if (video_codec.codecType == kVideoCodecH264)
|
2017-09-28 19:54:38 +02:00
|
|
|
p.h264_profile = video_codec.H264().profile;
|
2018-02-23 15:41:13 +01:00
|
|
|
return {CodecTypeToPayloadString(video_codec.codecType), PayloadUnion(p)};
|
2016-11-25 06:40:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool IsPayloadTypeValid(int8_t payload_type) {
|
2013-04-05 13:27:38 +00:00
|
|
|
assert(payload_type >= 0);
|
2013-01-17 16:10:45 +00:00
|
|
|
|
|
|
|
|
// 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.
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "Can't register invalid receiver payload type: "
|
|
|
|
|
<< payload_type;
|
2016-11-25 06:40:25 -08:00
|
|
|
return false;
|
2013-01-17 16:10:45 +00:00
|
|
|
default:
|
2016-11-25 06:40:25 -08:00
|
|
|
return true;
|
2013-01-17 16:10:45 +00:00
|
|
|
}
|
2016-11-25 06:40:25 -08:00
|
|
|
}
|
2013-01-17 16:10:45 +00:00
|
|
|
|
2016-11-25 06:40:25 -08:00
|
|
|
} // namespace
|
2013-01-17 16:10:45 +00:00
|
|
|
|
2016-11-25 06:40:25 -08:00
|
|
|
RTPPayloadRegistry::RTPPayloadRegistry()
|
2018-03-23 15:56:57 +01:00
|
|
|
: last_received_payload_type_(-1) {}
|
2016-11-24 10:43:42 -08:00
|
|
|
|
2016-11-25 06:40:25 -08:00
|
|
|
RTPPayloadRegistry::~RTPPayloadRegistry() = default;
|
2017-03-27 07:15:49 -07:00
|
|
|
|
|
|
|
|
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));
|
2017-10-04 12:38:53 +02:00
|
|
|
payload_type_map_.emplace(rtp_payload_type,
|
|
|
|
|
CreatePayloadType(audio_format));
|
2017-03-27 07:15:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear the value of last received payload type since it might mean
|
|
|
|
|
// something else now.
|
|
|
|
|
last_received_payload_type_ = -1;
|
|
|
|
|
}
|
2016-11-25 06:40:25 -08:00
|
|
|
|
2017-10-04 12:38:53 +02:00
|
|
|
int32_t RTPPayloadRegistry::RegisterReceivePayload(
|
|
|
|
|
int payload_type,
|
|
|
|
|
const SdpAudioFormat& audio_format,
|
|
|
|
|
bool* created_new_payload) {
|
2017-03-23 00:10:09 -07:00
|
|
|
rtc::CritScope cs(&crit_sect_);
|
|
|
|
|
|
|
|
|
|
#if RTC_DCHECK_IS_ON
|
|
|
|
|
RTC_DCHECK(!used_for_video_);
|
|
|
|
|
used_for_audio_ = true;
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-11-25 06:40:25 -08:00
|
|
|
*created_new_payload = false;
|
2017-10-04 12:38:53 +02:00
|
|
|
if (!IsPayloadTypeValid(payload_type))
|
2016-11-25 06:40:25 -08:00
|
|
|
return -1;
|
2016-11-24 11:08:39 -08:00
|
|
|
|
2017-10-04 12:38:53 +02:00
|
|
|
const auto it = payload_type_map_.find(payload_type);
|
2013-01-17 16:10:45 +00:00
|
|
|
if (it != payload_type_map_.end()) {
|
2016-11-25 06:40:25 -08:00
|
|
|
// We already use this payload type. Check if it's the same as we already
|
|
|
|
|
// have. If same, ignore sending an error.
|
2017-10-04 12:38:53 +02:00
|
|
|
if (PayloadIsCompatible(it->second, audio_format)) {
|
2017-09-28 20:13:59 +02:00
|
|
|
it->second.typeSpecific.audio_payload().rate = 0;
|
2016-11-25 06:40:25 -08:00
|
|
|
return 0;
|
2013-01-17 16:10:45 +00:00
|
|
|
}
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "Payload type already registered: " << payload_type;
|
2013-01-17 16:10:45 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-25 06:40:25 -08:00
|
|
|
// Audio codecs must be unique.
|
2017-10-04 12:38:53 +02:00
|
|
|
DeregisterAudioCodecOrRedTypeRegardlessOfPayloadType(audio_format);
|
2016-11-24 10:43:42 -08:00
|
|
|
|
2017-10-04 12:38:53 +02:00
|
|
|
const auto insert_status =
|
|
|
|
|
payload_type_map_.emplace(payload_type, CreatePayloadType(audio_format));
|
2017-09-28 19:54:38 +02:00
|
|
|
RTC_DCHECK(insert_status.second); // Insertion succeeded.
|
2016-11-24 11:08:39 -08:00
|
|
|
*created_new_payload = true;
|
2016-11-24 10:43:42 -08:00
|
|
|
|
2016-11-25 06:40:25 -08:00
|
|
|
// Successful set of payload type, clear the value of last received payload
|
|
|
|
|
// type since it might mean something else.
|
|
|
|
|
last_received_payload_type_ = -1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t RTPPayloadRegistry::RegisterReceivePayload(
|
|
|
|
|
const VideoCodec& video_codec) {
|
2017-03-23 00:10:09 -07:00
|
|
|
rtc::CritScope cs(&crit_sect_);
|
|
|
|
|
|
|
|
|
|
#if RTC_DCHECK_IS_ON
|
|
|
|
|
RTC_DCHECK(!used_for_audio_);
|
|
|
|
|
used_for_video_ = true;
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-11-25 06:40:25 -08:00
|
|
|
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;
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "Payload type already registered: "
|
|
|
|
|
<< static_cast<int>(video_codec.plType);
|
2016-11-25 06:40:25 -08:00
|
|
|
return -1;
|
2013-01-17 16:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
2017-09-28 19:54:38 +02:00
|
|
|
const auto insert_status = payload_type_map_.emplace(
|
|
|
|
|
video_codec.plType, CreatePayloadType(video_codec));
|
|
|
|
|
RTC_DCHECK(insert_status.second); // Insertion succeeded.
|
2016-11-25 06:40:25 -08:00
|
|
|
|
2013-01-17 16:10:45 +00:00
|
|
|
// Successful set of payload type, clear the value of last received payload
|
|
|
|
|
// type since it might mean something else.
|
|
|
|
|
last_received_payload_type_ = -1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPPayloadRegistry::DeRegisterReceivePayload(
|
|
|
|
|
const int8_t payload_type) {
|
2016-04-14 03:05:31 -07:00
|
|
|
rtc::CritScope cs(&crit_sect_);
|
2016-11-25 06:40:25 -08:00
|
|
|
payload_type_map_.erase(payload_type);
|
2013-01-17 16:10:45 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-04 13:23:07 +00:00
|
|
|
// There can't be several codecs with the same rate, frequency and channels
|
|
|
|
|
// for audio codecs, but there can for video.
|
2013-09-06 13:40:11 +00:00
|
|
|
// Always called from within a critical section.
|
2013-02-04 13:23:07 +00:00
|
|
|
void RTPPayloadRegistry::DeregisterAudioCodecOrRedTypeRegardlessOfPayloadType(
|
2017-10-04 12:38:53 +02:00
|
|
|
const SdpAudioFormat& audio_format) {
|
2016-11-25 06:40:25 -08:00
|
|
|
for (auto iterator = payload_type_map_.begin();
|
|
|
|
|
iterator != payload_type_map_.end(); ++iterator) {
|
2017-10-04 12:38:53 +02:00
|
|
|
if (PayloadIsCompatible(iterator->second, audio_format)) {
|
2016-11-25 06:40:25 -08:00
|
|
|
// Remove old setting.
|
|
|
|
|
payload_type_map_.erase(iterator);
|
|
|
|
|
break;
|
2013-02-04 13:23:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-15 23:38:54 +00:00
|
|
|
int RTPPayloadRegistry::GetPayloadTypeFrequency(
|
|
|
|
|
uint8_t payload_type) const {
|
2017-09-21 15:00:58 +02:00
|
|
|
const auto payload = PayloadTypeToPayload(payload_type);
|
2015-12-10 09:51:54 -08:00
|
|
|
if (!payload) {
|
2013-08-15 23:38:54 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2016-04-14 03:05:31 -07:00
|
|
|
rtc::CritScope cs(&crit_sect_);
|
2017-09-28 20:13:59 +02:00
|
|
|
return payload->typeSpecific.is_audio()
|
2017-10-04 12:38:53 +02:00
|
|
|
? payload->typeSpecific.audio_payload().format.clockrate_hz
|
2017-09-28 20:13:59 +02:00
|
|
|
: kVideoPayloadTypeFrequency;
|
2013-08-15 23:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
2017-09-21 15:00:58 +02:00
|
|
|
rtc::Optional<RtpUtility::Payload> RTPPayloadRegistry::PayloadTypeToPayload(
|
2015-12-10 09:51:54 -08:00
|
|
|
uint8_t payload_type) const {
|
2016-04-14 03:05:31 -07:00
|
|
|
rtc::CritScope cs(&crit_sect_);
|
2017-09-21 15:00:58 +02:00
|
|
|
const auto it = payload_type_map_.find(payload_type);
|
|
|
|
|
return it == payload_type_map_.end()
|
2017-11-16 10:55:48 +01:00
|
|
|
? rtc::nullopt
|
2017-09-21 15:00:58 +02:00
|
|
|
: rtc::Optional<RtpUtility::Payload>(it->second);
|
2013-01-17 16:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|