2011-12-16 14:31:37 +00:00
|
|
|
/*
|
2012-08-13 17:13:27 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-12-16 14:31:37 +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.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-11-08 13:36:37 -08:00
|
|
|
#include <assert.h>
|
2011-12-16 14:31:37 +00:00
|
|
|
|
2016-10-31 03:34:40 -07:00
|
|
|
#include "webrtc/base/checks.h"
|
2016-11-08 13:36:37 -08:00
|
|
|
#include "webrtc/common_types.h"
|
|
|
|
|
#include "webrtc/modules/rtp_rtcp/source/rtp_header_extension.h"
|
2015-03-17 14:33:12 +00:00
|
|
|
#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
|
2011-12-16 14:31:37 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2016-11-08 12:20:43 -08:00
|
|
|
|
|
|
|
|
constexpr uint8_t RtpHeaderExtensionMap::kInvalidId;
|
|
|
|
|
|
|
|
|
|
RtpHeaderExtensionMap::RtpHeaderExtensionMap() {
|
2011-12-16 14:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
2016-11-08 13:36:37 -08:00
|
|
|
RtpHeaderExtensionMap::~RtpHeaderExtensionMap() {
|
|
|
|
|
Erase();
|
2011-12-16 14:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
2016-11-08 13:36:37 -08:00
|
|
|
void RtpHeaderExtensionMap::Erase() {
|
|
|
|
|
while (!extensionMap_.empty()) {
|
|
|
|
|
std::map<uint8_t, HeaderExtension*>::iterator it =
|
|
|
|
|
extensionMap_.begin();
|
|
|
|
|
delete it->second;
|
|
|
|
|
extensionMap_.erase(it);
|
|
|
|
|
}
|
2011-12-16 14:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
2016-11-08 13:36:37 -08:00
|
|
|
int32_t RtpHeaderExtensionMap::Register(RTPExtensionType type, uint8_t id) {
|
|
|
|
|
if (id < 1 || id > 14) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
std::map<uint8_t, HeaderExtension*>::iterator it =
|
|
|
|
|
extensionMap_.find(id);
|
|
|
|
|
if (it != extensionMap_.end()) {
|
|
|
|
|
if (it->second->type != type) {
|
|
|
|
|
// An extension is already registered with the same id
|
|
|
|
|
// but a different type, so return failure.
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
// This extension type is already registered with this id,
|
|
|
|
|
// so return success.
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
RTC_DCHECK_EQ(kInvalidId, GetId(type));
|
|
|
|
|
extensionMap_[id] = new HeaderExtension(type);
|
|
|
|
|
return 0;
|
2014-07-17 16:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
2016-11-08 13:36:37 -08:00
|
|
|
int32_t RtpHeaderExtensionMap::Deregister(const RTPExtensionType type) {
|
|
|
|
|
uint8_t id;
|
|
|
|
|
if (GetId(type, &id) != 0) {
|
2016-11-08 12:20:43 -08:00
|
|
|
return 0;
|
2016-11-08 13:36:37 -08:00
|
|
|
}
|
|
|
|
|
std::map<uint8_t, HeaderExtension*>::iterator it =
|
|
|
|
|
extensionMap_.find(id);
|
|
|
|
|
assert(it != extensionMap_.end());
|
|
|
|
|
delete it->second;
|
|
|
|
|
extensionMap_.erase(it);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RtpHeaderExtensionMap::IsRegistered(RTPExtensionType type) const {
|
|
|
|
|
std::map<uint8_t, HeaderExtension*>::const_iterator it =
|
|
|
|
|
extensionMap_.begin();
|
|
|
|
|
for (; it != extensionMap_.end(); ++it) {
|
|
|
|
|
if (it->second->type == type)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2011-12-16 14:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
2016-11-08 13:36:37 -08:00
|
|
|
int32_t RtpHeaderExtensionMap::GetType(const uint8_t id,
|
|
|
|
|
RTPExtensionType* type) const {
|
|
|
|
|
assert(type);
|
|
|
|
|
std::map<uint8_t, HeaderExtension*>::const_iterator it =
|
|
|
|
|
extensionMap_.find(id);
|
|
|
|
|
if (it == extensionMap_.end()) {
|
|
|
|
|
return -1;
|
2016-04-20 05:25:10 -07:00
|
|
|
}
|
2016-11-08 13:36:37 -08:00
|
|
|
HeaderExtension* extension = it->second;
|
|
|
|
|
*type = extension->type;
|
2016-11-08 12:20:43 -08:00
|
|
|
return 0;
|
2016-04-20 05:25:10 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-08 13:36:37 -08:00
|
|
|
RTPExtensionType RtpHeaderExtensionMap::GetType(uint8_t id) const {
|
|
|
|
|
auto it = extensionMap_.find(id);
|
|
|
|
|
if (it == extensionMap_.end()) {
|
|
|
|
|
return kInvalidType;
|
2011-12-16 14:31:37 +00:00
|
|
|
}
|
2016-11-08 13:36:37 -08:00
|
|
|
return it->second->type;
|
|
|
|
|
}
|
2011-12-16 14:31:37 +00:00
|
|
|
|
2016-11-08 13:36:37 -08:00
|
|
|
int32_t RtpHeaderExtensionMap::GetId(const RTPExtensionType type,
|
|
|
|
|
uint8_t* id) const {
|
|
|
|
|
assert(id);
|
|
|
|
|
std::map<uint8_t, HeaderExtension*>::const_iterator it =
|
|
|
|
|
extensionMap_.begin();
|
|
|
|
|
|
|
|
|
|
while (it != extensionMap_.end()) {
|
|
|
|
|
HeaderExtension* extension = it->second;
|
|
|
|
|
if (extension->type == type) {
|
|
|
|
|
*id = it->first;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
it++;
|
2016-04-20 05:25:10 -07:00
|
|
|
}
|
2016-11-08 13:36:37 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2016-04-20 05:25:10 -07:00
|
|
|
|
2016-11-08 13:36:37 -08:00
|
|
|
uint8_t RtpHeaderExtensionMap::GetId(RTPExtensionType type) const {
|
|
|
|
|
for (auto kv : extensionMap_) {
|
|
|
|
|
if (kv.second->type == type)
|
|
|
|
|
return kv.first;
|
2016-11-08 12:20:43 -08:00
|
|
|
}
|
2016-11-08 13:36:37 -08:00
|
|
|
return kInvalidId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t RtpHeaderExtensionMap::GetTotalLengthInBytes() const {
|
|
|
|
|
// Get length for each extension block.
|
|
|
|
|
size_t length = 0;
|
|
|
|
|
for (const auto& kv : extensionMap_)
|
|
|
|
|
length += kv.second->length;
|
|
|
|
|
// Add RTP extension header length.
|
|
|
|
|
if (length > 0)
|
|
|
|
|
length += kRtpOneByteHeaderLength;
|
|
|
|
|
// Pad up to nearest 32bit word.
|
|
|
|
|
length = RtpUtility::Word32Align(length);
|
|
|
|
|
return length;
|
|
|
|
|
}
|
2011-12-16 14:31:37 +00:00
|
|
|
|
2016-11-08 13:36:37 -08:00
|
|
|
int32_t RtpHeaderExtensionMap::Size() const {
|
|
|
|
|
return extensionMap_.size();
|
2011-12-16 14:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
2016-11-08 13:36:37 -08:00
|
|
|
void RtpHeaderExtensionMap::GetCopy(RtpHeaderExtensionMap* map) const {
|
|
|
|
|
assert(map);
|
|
|
|
|
std::map<uint8_t, HeaderExtension*>::const_iterator it =
|
|
|
|
|
extensionMap_.begin();
|
|
|
|
|
while (it != extensionMap_.end()) {
|
|
|
|
|
HeaderExtension* extension = it->second;
|
|
|
|
|
map->Register(extension->type, it->first);
|
|
|
|
|
it++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-03 15:12:26 +00:00
|
|
|
} // namespace webrtc
|