-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rtp header padding #1079
Rtp header padding #1079
Changes from 4 commits
e50bfad
3794264
8f04206
aa2ebe8
f4feadf
b3136a3
d5aa6eb
250280b
d411a31
34cf193
b35933b
0d50648
627a95a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -566,16 +566,14 @@ namespace RTC | |
return; | ||
} | ||
|
||
std::memcpy(extenValue, mid.c_str(), midLen); | ||
|
||
SetExtensionLength(this->midExtensionId, midLen); | ||
SetExtensionValue(this->midExtensionId, midLen, mid); | ||
} | ||
|
||
/** | ||
* The caller is responsible of not setting a length higher than the | ||
* available one (taking into account existing padding bytes). | ||
*/ | ||
bool RtpPacket::SetExtensionLength(uint8_t id, uint8_t len) | ||
bool RtpPacket::SetExtensionValue(uint8_t id, uint8_t len, const std::string& value) | ||
{ | ||
MS_TRACE(); | ||
|
||
|
@@ -600,12 +598,79 @@ namespace RTC | |
|
||
auto currentLen = extension->len + 1; | ||
|
||
// Fill with 0's if new length is minor. | ||
if (len < currentLen) | ||
std::memset(extension->value + len, 0, currentLen - len); | ||
MS_DEBUG_DEV("set extension id: %" PRIu8 ", length:%" PRIu8 ", currentLen:%" PRIu8 " value:%s", id, len, currentLen, value.c_str()); | ||
if(len != currentLen) | ||
{ | ||
// need shift | ||
uint8_t* extensionStart = reinterpret_cast<uint8_t*>(this->headerExtension) + 4; | ||
uint8_t* extensionEnd = extensionStart + GetHeaderExtensionLength(); | ||
uint8_t* ptr = extensionStart; | ||
size_t extensionsTotalSize = static_cast<size_t>(len+1); | ||
uint8_t* ptr1 = ptr; | ||
//clear current extension | ||
std::memset((uint8_t*)extension, 0, currentLen+1); | ||
while(ptr < extensionEnd && ptr1<extensionEnd) | ||
{ | ||
if(ptr >= extensionEnd) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In which case are we entering this condition? |
||
ptr1++; | ||
*ptr1 = 0u; | ||
continue; | ||
} | ||
const uint8_t tempId = (*ptr & 0xF0) >> 4; | ||
const size_t tempLen = static_cast<size_t>(*ptr & 0x0F) + 1; | ||
jmillan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// id=15 in One-Byte extensions means "stop parsing here". | ||
if (tempId == 15u) | ||
break; | ||
if(tempId != 0u && tempId != id) | ||
{ | ||
if(ptr1< ptr) | ||
{ | ||
std::memmove(ptr1, ptr, tempLen+1); | ||
this->oneByteExtensions[tempId - 1] = reinterpret_cast<OneByteExtension*>(ptr1); | ||
} | ||
extensionsTotalSize += tempLen + 1; | ||
// move forward templen+1 | ||
ptr += tempLen+1; | ||
ptr1 += tempLen+1; | ||
MS_DEBUG_DEV("tempId: %" PRIu8 " tempLen:%zd, offset:%ld", tempId, tempLen, ptr-extensionStart); | ||
} | ||
else | ||
{ | ||
ptr++; | ||
} | ||
} | ||
MS_DEBUG_DEV("extensionsTotalSize: %zd headerLength:%zd", extensionsTotalSize, GetHeaderExtensionLength()); | ||
auto paddedExtensionsTotalSize = | ||
static_cast<size_t>(Utils::Byte::PadTo4Bytes(static_cast<uint16_t>(extensionsTotalSize))); | ||
extensionsTotalSize = paddedExtensionsTotalSize; | ||
int16_t shift = static_cast<int16_t>(extensionsTotalSize - GetHeaderExtensionLength()); | ||
MS_DEBUG_DEV("shift:%d paddedExtensionsTotalSize: %zd", shift, paddedExtensionsTotalSize); | ||
//move payload | ||
std::memmove(this->payload + shift, this->payload, this->payloadLength + this->payloadPadding); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
// clear the shift place, if shift > currentLen, may create some strange extension | ||
if(shift>0) | ||
{ | ||
std::memset(this->payload, 0, shift); | ||
} | ||
//begin to set current extension | ||
ptr = extensionStart + extensionsTotalSize - (len + 1); | ||
this->oneByteExtensions[id - 1] = reinterpret_cast<OneByteExtension*>(ptr); | ||
extension = this->oneByteExtensions[id - 1]; | ||
*ptr = (id << 4) | ((len - 1) & 0x0F); | ||
ptr++; | ||
std::memcpy(ptr, value.c_str(), len); | ||
|
||
this->payload += shift; | ||
this->size += shift; | ||
this->headerExtension->length = htons(extensionsTotalSize / 4); | ||
}else { | ||
// In One-Byte extensions value length 0 means 1. | ||
extension->len = len - 1; | ||
std::memcpy(extension->value, value.c_str(), len); | ||
} | ||
|
||
// In One-Byte extensions value length 0 means 1. | ||
extension->len = len - 1; | ||
|
||
return true; | ||
} | ||
|
@@ -618,13 +683,70 @@ namespace RTC | |
|
||
auto* extension = it->second; | ||
auto currentLen = extension->len; | ||
|
||
// Fill with 0's if new length is minor. | ||
if (len < currentLen) | ||
std::memset(extension->value + len, 0, currentLen - len); | ||
|
||
extension->len = len; | ||
|
||
if(len != currentLen) | ||
{ | ||
// need shift | ||
uint8_t* extensionStart = reinterpret_cast<uint8_t*>(this->headerExtension) + 4; | ||
uint8_t* extensionEnd = extensionStart + GetHeaderExtensionLength(); | ||
uint8_t* ptr = extensionStart; | ||
size_t extensionsTotalSize = static_cast<size_t>(len + 2); | ||
uint8_t* ptr1 = ptr; | ||
//clear current extension valueLen + 2 byteheader | ||
std::memset((void *)extension, 0, currentLen + 2); | ||
while(ptr + 1 < extensionEnd && ptr1 < extensionEnd) | ||
{ | ||
if(ptr + 1 >= extensionEnd) { | ||
ptr1++; | ||
*ptr1 = 0u; | ||
continue; | ||
} | ||
const uint8_t tempId = *ptr; | ||
const uint8_t tempLen = *(ptr + 1); | ||
|
||
if(tempId != 0u && tempId != id) | ||
{ | ||
if(ptr1< ptr) | ||
{ | ||
std::memmove(ptr1, ptr, tempLen+2); | ||
this->mapTwoBytesExtensions[tempId] = reinterpret_cast<TwoBytesExtension*>(ptr1); | ||
} | ||
extensionsTotalSize += tempLen + 2; | ||
// move forward len+1 | ||
ptr += tempLen+2; | ||
ptr1 += tempLen+2; | ||
} else | ||
{ | ||
ptr++; | ||
} | ||
} | ||
auto paddedExtensionsTotalSize = | ||
static_cast<size_t>(Utils::Byte::PadTo4Bytes(static_cast<uint16_t>(extensionsTotalSize))); | ||
extensionsTotalSize = paddedExtensionsTotalSize; | ||
int16_t shift = static_cast<int16_t>(extensionsTotalSize - GetHeaderExtensionLength()); | ||
|
||
//move payload | ||
std::memmove(this->payload + shift, this->payload, this->payloadLength + this->payloadPadding); | ||
if(shift>0) | ||
{ | ||
std::memset(this->payload, 0, shift); | ||
} | ||
//begin to set current extension | ||
ptr = extensionStart + extensionsTotalSize - (len + 2); | ||
this->mapTwoBytesExtensions[id] = reinterpret_cast<TwoBytesExtension*>(ptr); | ||
extension = this->mapTwoBytesExtensions[id]; | ||
*ptr = id; | ||
ptr++; | ||
*ptr = len; | ||
ptr++; | ||
std::memcpy(ptr, value.c_str(), len); | ||
|
||
this->payload += shift; | ||
this->size += shift; | ||
this->headerExtension->length = htons(extensionsTotalSize / 4); | ||
}else { | ||
extension->len = len; | ||
std::memcpy(extension->value, value.c_str(), len); | ||
} | ||
return true; | ||
} | ||
else | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are those tests commented? We cannot leave commented tests. Do they no longer work or what?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this two line can be replace by setExtensionValue(). should I just delete the 2 line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should test as much as possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extremely agree. This is my first time writing C++, and I have been writing PHP before. So the more tests, the better