Unverified 提交 d4204c6e authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

OPUS: Fix DTX detection (#1357)

上级 f0a0a5b3
......@@ -3,6 +3,7 @@
### NEXT
- Update worker subprojects ([PR #1376](https://github.com/versatica/mediasoup/pull/1376)).
- OPUS: Fix DTX detection #1357 ([PR #1357](https://github.com/versatica/mediasoup/pull/1357)).
### 3.14.1
......
......@@ -20,6 +20,9 @@ namespace RTC
void Dump() const override;
// Mandatory fields.
uint8_t stereo : 1;
uint8_t code : 2;
// Parsed values.
bool isDtx{ false };
};
......
......@@ -18,6 +18,8 @@ namespace RTC
if (len < 2)
{
MS_WARN_DEV("ignoring payload with length < 2");
return nullptr;
}
......
......@@ -17,6 +17,8 @@ namespace RTC
if (len < 2)
{
MS_WARN_DEV("ignoring payload with length < 2");
return nullptr;
}
......@@ -54,12 +56,13 @@ namespace RTC
}
}
// NOTE: Unfortunately libwebrtc produces wrong Frame-Marking (without i=1 in
// keyframes) when it uses H264 hardware encoder (at least in Mac):
// NOTE: Unfortunately libwebrtc produces wrong Frame-Marking (without i=1
// in keyframes) when it uses H264 hardware encoder (at least in Mac):
// https://bugs.chromium.org/p/webrtc/issues/detail?id=10746
//
// As a temporal workaround, always do payload parsing to detect keyframes if
// there is no frame-marking or if there is but keyframe was not detected above.
// As a temporal workaround, always do payload parsing to detect keyframes
// if there is no frame-marking or if there is but keyframe was not
// detected above.
if (!frameMarking || !payloadDescriptor->isKeyFrame)
{
const uint8_t nal = *data & 0x1F;
......@@ -79,6 +82,8 @@ namespace RTC
if (payloadDescriptor == nullptr)
{
MS_WARN_DEV("ignoring invalid payload (1)");
return nullptr;
}
......@@ -106,6 +111,8 @@ namespace RTC
if (payloadDescriptor == nullptr)
{
MS_WARN_DEV("ignoring invalid payload (2)");
return nullptr;
}
......@@ -142,6 +149,8 @@ namespace RTC
if (payloadDescriptor == nullptr)
{
MS_WARN_DEV("ignoring invalid payload (3)");
return nullptr;
}
......
......@@ -14,12 +14,72 @@ namespace RTC
{
MS_TRACE();
if (len < 1)
{
MS_WARN_DEV("ignoring empty payload");
return nullptr;
}
std::unique_ptr<PayloadDescriptor> payloadDescriptor(new PayloadDescriptor());
// libopus generates a single byte payload (TOC, no frames) to generate DTX.
if (len == 1)
uint8_t byte = data[0];
payloadDescriptor->stereo = (byte >> 2) & 0x01;
payloadDescriptor->code = byte & 0x03;
switch (payloadDescriptor->code)
{
payloadDescriptor->isDtx = true;
case 0:
case 1:
{
// In code 0 and 1 packets, DTX is determined by total length = 1 (TOC
// byte only).
if (len == 1)
{
payloadDescriptor->isDtx = true;
}
break;
}
case 2:
{
// As per spec, a 1-byte code 2 packet is always invalid.
if (len == 1)
{
MS_WARN_DEV("ignoring invalid payload (1)");
return nullptr;
}
// In code 2 packets, DTX is determined by total length = 2 (TOC byte
// only). Per spec, the only valid 2-byte code 2 packet is one where
// the length of both frames is zero.
if (len == 2)
{
payloadDescriptor->isDtx = true;
}
break;
}
case 3:
{
// As per spec, a 1-byte code 3 packet is always invalid.
if (len == 1)
{
MS_WARN_DEV("ignoring invalid payload (2)");
return nullptr;
}
// A code 3 packet can never be DTX.
break;
}
default:;
}
return payloadDescriptor.release();
......@@ -34,6 +94,11 @@ namespace RTC
PayloadDescriptor* payloadDescriptor = Opus::Parse(data, len);
if (!payloadDescriptor)
{
return;
}
auto* payloadDescriptorHandler = new PayloadDescriptorHandler(payloadDescriptor);
packet->SetPayloadDescriptorHandler(payloadDescriptorHandler);
......@@ -46,6 +111,8 @@ namespace RTC
MS_TRACE();
MS_DUMP("<Opus::PayloadDescriptor>");
MS_DUMP(" stereo: %" PRIu8, this->stereo);
MS_DUMP(" code: %" PRIu8, this->code);
MS_DUMP(" isDtx: %s", this->isDtx ? "true" : "false");
MS_DUMP("</Opus::PayloadDescriptor>");
}
......
......@@ -21,6 +21,8 @@ namespace RTC
if (len < 1)
{
MS_WARN_DEV("ignoring empty payload");
return nullptr;
}
......@@ -36,12 +38,16 @@ namespace RTC
if (!payloadDescriptor->extended)
{
MS_WARN_DEV("ignoring invalid payload (1)");
return nullptr;
}
else
{
if (len < ++offset + 1)
{
MS_WARN_DEV("ignoring invalid payload (2)");
return nullptr;
}
......@@ -57,6 +63,8 @@ namespace RTC
{
if (len < ++offset + 1)
{
MS_WARN_DEV("ignoring invalid payload (3)");
return nullptr;
}
......@@ -66,6 +74,8 @@ namespace RTC
{
if (len < ++offset + 1)
{
MS_WARN_DEV("ignoring invalid payload (4)");
return nullptr;
}
......@@ -86,6 +96,8 @@ namespace RTC
{
if (len < ++offset + 1)
{
MS_WARN_DEV("ignoring invalid payload (5)");
return nullptr;
}
......@@ -97,6 +109,8 @@ namespace RTC
{
if (len < ++offset + 1)
{
MS_WARN_DEV("ignoring invalid payload (6)");
return nullptr;
}
......
......@@ -20,6 +20,8 @@ namespace RTC
if (len < 1)
{
MS_WARN_DEV("ignoring empty payload");
return nullptr;
}
......@@ -40,6 +42,8 @@ namespace RTC
{
if (len < ++offset + 1)
{
MS_WARN_DEV("ignoring invalid payload (1)");
return nullptr;
}
......@@ -49,6 +53,8 @@ namespace RTC
{
if (len < ++offset + 1)
{
MS_WARN_DEV("ignoring invalid payload (2)");
return nullptr;
}
......@@ -69,6 +75,8 @@ namespace RTC
{
if (len < ++offset + 1)
{
MS_WARN_DEV("ignoring invalid payload (3)");
return nullptr;
}
......@@ -83,6 +91,8 @@ namespace RTC
if (len < ++offset + 1)
{
MS_WARN_DEV("ignoring invalid payload (4)");
return nullptr;
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论