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

OPUS: Fix DTX detection (#1357)

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