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

Frame marking and H264 temporal layers (#305)

Fixes #298 
上级 163df732
......@@ -257,24 +257,39 @@ const supportedRtpCapabilities =
preferredEncrypt : false,
direction : 'inactive' // TODO: Enable when done.
},
// NOTE: Remove this once framemarking draft becomes RFC.
{
kind : 'video',
uri : 'http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07',
preferredId : 6,
preferredEncrypt : false,
direction : 'sendrecv'
},
{
kind : 'video',
uri : 'urn:ietf:params:rtp-hdrext:framemarking',
preferredId : 7,
preferredEncrypt : false,
direction : 'sendrecv'
},
{
kind : 'audio',
uri : 'urn:ietf:params:rtp-hdrext:ssrc-audio-level',
preferredId : 8,
preferredId : 10,
preferredEncrypt : false,
direction : 'sendrecv'
},
{
kind : 'video',
uri : 'urn:3gpp:video-orientation',
preferredId : 9,
preferredId : 11,
preferredEncrypt : false,
direction : 'sendrecv'
},
{
kind : 'video',
uri : 'urn:ietf:params:rtp-hdrext:toffset',
preferredId : 10,
preferredId : 12,
preferredEncrypt : false,
direction : 'sendrecv'
}
......
......@@ -221,19 +221,19 @@ const consumerDeviceCapabilities =
{
kind : 'audio',
uri : 'urn:ietf:params:rtp-hdrext:ssrc-audio-level',
preferredId : 8,
preferredId : 10,
preferredEncrypt : false
},
{
kind : 'video',
uri : 'urn:3gpp:video-orientation',
preferredId : 9,
preferredId : 11,
preferredEncrypt : false
},
{
kind : 'video',
uri : 'urn:ietf:params:rtp-hdrext:toffset',
preferredId : 10,
preferredId : 12,
preferredEncrypt : false
}
],
......@@ -503,7 +503,7 @@ test('consumer.dump() succeeds', async () =>
},
{
uri : 'urn:ietf:params:rtp-hdrext:ssrc-audio-level',
id : 8,
id : 10,
parameters : {},
encrypt : false
}
......@@ -564,13 +564,13 @@ test('consumer.dump() succeeds', async () =>
},
{
uri : 'urn:3gpp:video-orientation',
id : 9,
id : 11,
parameters : {},
encrypt : false
},
{
uri : 'urn:ietf:params:rtp-hdrext:toffset',
id : 10,
id : 12,
parameters : {},
encrypt : false
}
......
......@@ -173,7 +173,7 @@ const consumerDeviceCapabilities =
{
kind : 'audio',
uri : 'urn:ietf:params:rtp-hdrext:ssrc-audio-level',
preferredId : 8,
preferredId : 10,
preferredEncrypt : false
}
]
......@@ -250,7 +250,7 @@ test('router.pipeToRouter() succeeds with audio', async () =>
[
{
uri : 'urn:ietf:params:rtp-hdrext:ssrc-audio-level',
id : 8
id : 10
}
]);
expect(pipeConsumer.rtpParameters.encodings).toEqual(
......@@ -287,7 +287,7 @@ test('router.pipeToRouter() succeeds with audio', async () =>
[
{
uri : 'urn:ietf:params:rtp-hdrext:ssrc-audio-level',
id : 8
id : 10
}
]);
expect(pipeProducer.rtpParameters.encodings).toEqual(
......@@ -337,13 +337,22 @@ test('router.pipeToRouter() succeeds with video', async () =>
]);
expect(pipeConsumer.rtpParameters.headerExtensions).toEqual(
[
// NOTE: Remove this once framemarking draft becomes RFC.
{
uri : 'http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07',
id : 6
},
{
uri : 'urn:ietf:params:rtp-hdrext:framemarking',
id : 7
},
{
uri : 'urn:3gpp:video-orientation',
id : 9
id : 11
},
{
uri : 'urn:ietf:params:rtp-hdrext:toffset',
id : 10
id : 12
}
]);
expect(pipeConsumer.rtpParameters.encodings).toEqual(
......@@ -379,13 +388,22 @@ test('router.pipeToRouter() succeeds with video', async () =>
]);
expect(pipeProducer.rtpParameters.headerExtensions).toEqual(
[
// NOTE: Remove this once framemarking draft becomes RFC.
{
uri : 'http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07',
id : 6
},
{
uri : 'urn:ietf:params:rtp-hdrext:framemarking',
id : 7
},
{
uri : 'urn:3gpp:video-orientation',
id : 9
id : 11
},
{
uri : 'urn:ietf:params:rtp-hdrext:toffset',
id : 10
id : 12
}
]);
expect(pipeProducer.paused).toBe(true);
......
......@@ -423,13 +423,13 @@ test('getProducerRtpParametersMapping(), getConsumableRtpParameters(), getConsum
{
kind : 'video',
uri : 'urn:3gpp:video-orientation',
preferredId : 9,
preferredId : 11,
preferredEncrypt : false
},
{
kind : 'video',
uri : 'urn:ietf:params:rtp-hdrext:toffset',
preferredId : 10,
preferredId : 12,
preferredEncrypt : false
}
]
......@@ -479,11 +479,11 @@ test('getProducerRtpParametersMapping(), getConsumableRtpParameters(), getConsum
[
{
uri : 'urn:3gpp:video-orientation',
id : 9
id : 11
},
{
uri : 'urn:ietf:params:rtp-hdrext:toffset',
id : 10
id : 12
}
]);
......
......@@ -16,14 +16,29 @@ namespace RTC
{
/* Pure virtual methods inherited from RTC::Codecs::PayloadDescriptor. */
~PayloadDescriptor() = default;
void Dump() const;
void Dump() const override;
// Fields in frame-marking extension.
uint8_t s : 1; // Start of Frame.
uint8_t e : 1; // End of Frame.
uint8_t i : 1; // Independent Frame.
uint8_t d : 1; // Discardable Frame.
uint8_t b : 1; // Base Layer Sync.
uint8_t tid{ 0 }; // Temporal layer id.
uint8_t lid{ 0 }; // Spatial layer id.
uint8_t tl0picidx{ 0 }; // TL0PICIDX
// Parsed values.
bool hasLid{ false };
bool hasTl0picidx{ false };
bool isKeyFrame{ false };
};
public:
static H264::PayloadDescriptor* Parse(uint8_t* data, size_t len);
static H264::PayloadDescriptor* Parse(
const uint8_t* data,
size_t len,
RTC::RtpPacket::FrameMarking* frameMarking = nullptr,
uint8_t frameMarkingLen = 0);
static void ProcessRtpPacket(RTC::RtpPacket* packet);
public:
......@@ -35,6 +50,9 @@ namespace RTC
/* Pure virtual methods inherited from RTC::Codecs::EncodingContext. */
public:
void SyncRequired() override;
public:
uint8_t currentTemporalLayer{ std::numeric_limits<uint8_t>::max() };
};
class PayloadDescriptorHandler : public RTC::Codecs::PayloadDescriptorHandler
......@@ -44,12 +62,12 @@ namespace RTC
~PayloadDescriptorHandler() = default;
public:
void Dump() const;
bool Encode(RTC::Codecs::EncodingContext* context, uint8_t* data);
void Restore(uint8_t* data);
uint8_t GetSpatialLayer() const;
uint8_t GetTemporalLayer() const;
bool IsKeyFrame() const;
void Dump() const override;
bool Encode(RTC::Codecs::EncodingContext* context, uint8_t* data) override;
void Restore(uint8_t* data) override;
uint8_t GetSpatialLayer() const override;
uint8_t GetTemporalLayer() const override;
bool IsKeyFrame() const override;
private:
std::unique_ptr<PayloadDescriptor> payloadDescriptor;
......@@ -58,34 +76,26 @@ namespace RTC
/* Inline EncondingContext methods */
inline void H264::EncodingContext::SyncRequired(){};
/* Inline PayloadDescriptorHandler methods */
inline bool H264::PayloadDescriptorHandler::Encode(
RTC::Codecs::EncodingContext* /*encodingContext*/, uint8_t* /*data*/)
{
return true;
};
inline void H264::PayloadDescriptorHandler::Restore(uint8_t* /*data*/)
inline void H264::EncodingContext::SyncRequired()
{
}
/* Inline PayloadDescriptorHandler methods */
inline uint8_t H264::PayloadDescriptorHandler::GetSpatialLayer() const
{
return 0u;
};
}
inline uint8_t H264::PayloadDescriptorHandler::GetTemporalLayer() const
{
return 0u;
};
return this->payloadDescriptor->tid;
}
inline bool H264::PayloadDescriptorHandler::IsKeyFrame() const
{
return this->payloadDescriptor->isKeyFrame;
};
}
inline void H264::PayloadDescriptorHandler::Dump() const
{
......
......@@ -40,7 +40,7 @@ namespace RTC
{
/* Pure virtual methods inherited from RTC::Codecs::PayloadDescriptor. */
~PayloadDescriptor() = default;
void Dump() const;
void Dump() const override;
// Rewrite the buffer with the given pictureId and tl0PictureIndex values.
void Encode(uint8_t* data, uint16_t pictureId, uint8_t tl0PictureIndex) const;
......@@ -72,7 +72,11 @@ namespace RTC
};
public:
static VP8::PayloadDescriptor* Parse(const uint8_t* data, size_t len);
static VP8::PayloadDescriptor* Parse(
const uint8_t* data,
size_t len,
RTC::RtpPacket::FrameMarking* frameMarking = nullptr,
uint8_t frameMarkingLen = 0);
static void ProcessRtpPacket(RTC::RtpPacket* packet);
public:
......@@ -99,12 +103,12 @@ namespace RTC
~PayloadDescriptorHandler() = default;
public:
void Dump() const;
bool Encode(RTC::Codecs::EncodingContext* encodingContext, uint8_t* data);
void Restore(uint8_t* data);
uint8_t GetSpatialLayer() const;
uint8_t GetTemporalLayer() const;
bool IsKeyFrame() const;
void Dump() const override;
bool Encode(RTC::Codecs::EncodingContext* encodingContext, uint8_t* data) override;
void Restore(uint8_t* data) override;
uint8_t GetSpatialLayer() const override;
uint8_t GetTemporalLayer() const override;
bool IsKeyFrame() const override;
private:
std::unique_ptr<PayloadDescriptor> payloadDescriptor;
......@@ -116,27 +120,24 @@ namespace RTC
inline void VP8::EncodingContext::SyncRequired()
{
this->syncRequired = true;
};
}
/* Inline PayloadDescriptorHandler methods */
inline uint8_t VP8::PayloadDescriptorHandler::GetSpatialLayer() const
{
return 0u;
};
}
inline uint8_t VP8::PayloadDescriptorHandler::GetTemporalLayer() const
{
if (this->payloadDescriptor->hasTlIndex)
return this->payloadDescriptor->tlIndex;
else
return 0u;
};
return this->payloadDescriptor->hasTlIndex ? this->payloadDescriptor->tlIndex : 0u;
}
inline bool VP8::PayloadDescriptorHandler::IsKeyFrame() const
{
return this->payloadDescriptor->isKeyFrame;
};
}
inline void VP8::PayloadDescriptorHandler::Dump() const
{
......
......@@ -85,6 +85,7 @@ namespace RTC
RTC::RtpStreamRecv* CreateRtpStream(
RTC::RtpPacket* packet, const RTC::RtpCodecParameters& mediaCodec, size_t encodingIdx);
void NotifyNewRtpStream(RTC::RtpStreamRecv* rtpStream);
void PreProcessRtpPacket(RTC::RtpPacket* packet);
bool MangleRtpPacket(RTC::RtpPacket* packet, RTC::RtpStreamRecv* rtpStream) const;
void PostProcessRtpPacket(RTC::RtpPacket* packet);
void EmitScore() const;
......
......@@ -132,9 +132,11 @@ namespace RTC
REPAIRED_RTP_STREAM_ID = 3,
ABS_SEND_TIME = 4,
TRANSPORT_CC_01 = 5,
SSRC_AUDIO_LEVEL = 8,
VIDEO_ORIENTATION = 9,
TOFFSET = 10
FRAME_MARKING_07 = 6, // NOTE: Remove once RFC.
FRAME_MARKING = 7,
SSRC_AUDIO_LEVEL = 10,
VIDEO_ORIENTATION = 11,
TOFFSET = 12
};
private:
......
......@@ -7,13 +7,17 @@ namespace RTC
// the same Transport. Others are different for each Producer.
struct RtpHeaderExtensionIds
{
uint8_t mid{ 0u }; // 0 means no MID id.
uint8_t rid{ 0u }; // 0 means no RID id.
uint8_t rrid{ 0u }; // 0 means no RRID id.
uint8_t absSendTime{ 0u }; // 0 means no abs-send-time id.
uint8_t ssrcAudioLevel{ 0u }; // 0 means no ssrc-audio-level id.
uint8_t videoOrientation{ 0u }; // 0 means no video-orientation id.
uint8_t toffset{ 0u }; // 0 means no toffset id.
// 0 means no id.
uint8_t mid{ 0u };
uint8_t rid{ 0u };
uint8_t rrid{ 0u };
uint8_t absSendTime{ 0u };
uint8_t transportWideCC01{ 0u };
uint8_t frameMarking07{ 0u }; // NOTE: Remove once RFC.
uint8_t frameMarking{ 0u };
uint8_t ssrcAudioLevel{ 0u };
uint8_t videoOrientation{ 0u };
uint8_t toffset{ 0u };
};
} // namespace RTC
......
......@@ -84,6 +84,29 @@ namespace RTC
uint8_t* value;
};
public:
/* Struct with frame-marking information. */
struct FrameMarking
{
#if defined(MS_LITTLE_ENDIAN)
uint8_t tid : 3;
uint8_t base : 1;
uint8_t discardable : 1;
uint8_t independent : 1;
uint8_t end : 1;
uint8_t start : 1;
#elif defined(MS_BIG_ENDIAN)
uint8_t start : 1;
uint8_t end : 1;
uint8_t independent : 1;
uint8_t discardable : 1;
uint8_t base : 1;
uint8_t tid : 3;
#endif
uint8_t lid;
uint8_t tl0picidx;
};
public:
static bool IsRtp(const uint8_t* data, size_t len);
static RtpPacket* Parse(const uint8_t* data, size_t len);
......@@ -126,11 +149,14 @@ namespace RTC
void SetRidExtensionId(uint8_t id);
void SetRepairedRidExtensionId(uint8_t id);
void SetAbsSendTimeExtensionId(uint8_t id);
void SetFrameMarking07ExtensionId(uint8_t id); // NOTE: Remove once RFC.
void SetFrameMarkingExtensionId(uint8_t id);
void SetSsrcAudioLevelExtensionId(uint8_t id);
void SetVideoOrientationExtensionId(uint8_t id);
bool ReadMid(std::string& mid) const;
bool ReadRid(std::string& rid) const;
bool ReadAbsSendTime(uint32_t& time) const;
bool ReadFrameMarking(RtpPacket::FrameMarking** frameMarking, uint8_t& length) const;
bool ReadSsrcAudioLevel(uint8_t& volume, bool& voice) const;
bool ReadVideoOrientation(bool& camera, bool& flip, uint16_t& rotation) const;
uint8_t* GetExtension(uint8_t id, uint8_t& len) const;
......@@ -162,6 +188,8 @@ namespace RTC
uint8_t ridExtensionId{ 0 };
uint8_t rridExtensionId{ 0 };
uint8_t absSendTimeExtensionId{ 0 };
uint8_t frameMarking07ExtensionId{ 0 }; // NOTE: Remove once RFC.
uint8_t frameMarkingExtensionId{ 0 };
uint8_t ssrcAudioLevelExtensionId{ 0 };
uint8_t videoOrientationExtensionId{ 0 };
uint8_t* payload{ nullptr };
......@@ -326,6 +354,23 @@ namespace RTC
this->absSendTimeExtensionId = id;
}
inline void RtpPacket::SetFrameMarkingExtensionId(uint8_t id)
{
if (id == 0u)
return;
this->frameMarkingExtensionId = id;
}
// NOTE: Remove once RFC.
inline void RtpPacket::SetFrameMarking07ExtensionId(uint8_t id)
{
if (id == 0u)
return;
this->frameMarking07ExtensionId = id;
}
inline void RtpPacket::SetSsrcAudioLevelExtensionId(uint8_t id)
{
if (id == 0u)
......@@ -393,6 +438,24 @@ namespace RTC
return true;
}
inline bool RtpPacket::ReadFrameMarking(RtpPacket::FrameMarking** frameMarking, uint8_t& length) const
{
uint8_t extenLen;
uint8_t* extenValue = GetExtension(this->frameMarkingExtensionId, extenLen);
// NOTE: Remove this once framemarking draft becomes RFC.
if (!extenValue)
extenValue = GetExtension(this->frameMarking07ExtensionId, extenLen);
if (!extenValue || extenLen > 3)
return false;
*frameMarking = reinterpret_cast<RtpPacket::FrameMarking*>(extenValue);
length = extenLen;
return true;
}
inline bool RtpPacket::ReadSsrcAudioLevel(uint8_t& volume, bool& voice) const
{
uint8_t extenLen;
......
......@@ -9,107 +9,204 @@ namespace RTC
{
namespace Codecs
{
H264::PayloadDescriptor* H264::Parse(uint8_t* data, size_t len)
/* Class methods. */
H264::PayloadDescriptor* H264::Parse(
const uint8_t* data, size_t len, RTC::RtpPacket::FrameMarking* frameMarking, uint8_t frameMarkingLen)
{
MS_TRACE();
std::unique_ptr<PayloadDescriptor> payloadDescriptor(new PayloadDescriptor());
if (len < 2)
return nullptr;
uint8_t nal = *data & 0x1F;
std::unique_ptr<PayloadDescriptor> payloadDescriptor(new PayloadDescriptor());
switch (nal)
// Use frame-marking.
if (frameMarking)
{
// Single NAL unit packet.
// IDR (instantaneous decoding picture).
case 7:
// Read fields.
payloadDescriptor->s = frameMarking->start;
payloadDescriptor->e = frameMarking->end;
payloadDescriptor->i = frameMarking->independent;
payloadDescriptor->d = frameMarking->discardable;
payloadDescriptor->b = frameMarking->base;
payloadDescriptor->tid = frameMarking->tid;
if (frameMarkingLen >= 2)
{
payloadDescriptor->isKeyFrame = true;
payloadDescriptor->hasLid = true;
payloadDescriptor->lid = frameMarking->lid;
}
break;
if (frameMarkingLen == 3)
{
payloadDescriptor->hasTl0picidx = true;
payloadDescriptor->tl0picidx = frameMarking->tl0picidx;
}
// Aggreation packet.
// STAP-A.
case 24:
// Detect key frame.
if (frameMarking->start && frameMarking->independent)
payloadDescriptor->isKeyFrame = true;
}
else
// Parse payload otherwise.
{
uint8_t nal = *data & 0x1F;
switch (nal)
{
size_t offset{ 1 };
// Single NAL unit packet.
// IDR (instantaneous decoding picture).
case 7:
{
payloadDescriptor->isKeyFrame = true;
len -= 1;
break;
}
// Iterate NAL units.
while (len >= 3)
// Aggreation packet.
// STAP-A.
case 24:
{
auto naluSize = Utils::Byte::Get2Bytes(data, offset);
uint8_t subnal = *(data + offset + sizeof(naluSize)) & 0x1F;
size_t offset{ 1 };
if (subnal == 7)
len -= 1;
// Iterate NAL units.
while (len >= 3)
{
payloadDescriptor->isKeyFrame = true;
auto naluSize = Utils::Byte::Get2Bytes(data, offset);
uint8_t subnal = *(data + offset + sizeof(naluSize)) & 0x1F;
break;
}
if (subnal == 7)
{
payloadDescriptor->isKeyFrame = true;
// Check if there is room for the indicated NAL unit size.
if (len < (naluSize + sizeof(naluSize)))
break;
break;
}
offset += naluSize + sizeof(naluSize);
len -= naluSize + sizeof(naluSize);
}
// Check if there is room for the indicated NAL unit size.
if (len < (naluSize + sizeof(naluSize)))
break;
break;
}
offset += naluSize + sizeof(naluSize);
len -= naluSize + sizeof(naluSize);
}
// Aggreation packet.
// FU-A, FU-B.
case 28:
case 29:
{
uint8_t subnal = *(data + 1) & 0x1F;
uint8_t startBit = *(data + 1) & 0x80;
break;
}
if (subnal == 7 && startBit == 128)
payloadDescriptor->isKeyFrame = true;
// Aggreation packet.
// FU-A, FU-B.
case 28:
case 29:
{
uint8_t subnal = *(data + 1) & 0x1F;
uint8_t startBit = *(data + 1) & 0x80;
break;
if (subnal == 7 && startBit == 128)
payloadDescriptor->isKeyFrame = true;
break;
}
}
}
return payloadDescriptor.release();
}
void H264::ProcessRtpPacket(RTC::RtpPacket* packet)
{
MS_TRACE();
auto* data = packet->GetPayload();
auto len = packet->GetPayloadLength();
RtpPacket::FrameMarking* frameMarking{ nullptr };
uint8_t frameMarkingLen{ 0 };
// Read frame-marking.
packet->ReadFrameMarking(&frameMarking, frameMarkingLen);
PayloadDescriptor* payloadDescriptor = H264::Parse(data, len, frameMarking, frameMarkingLen);
if (!payloadDescriptor)
return;
auto* payloadDescriptorHandler = new PayloadDescriptorHandler(payloadDescriptor);
packet->SetPayloadDescriptorHandler(payloadDescriptorHandler);
}
/* Instance methods. */
void H264::PayloadDescriptor::Dump() const
{
MS_TRACE();
MS_DEBUG_DEV("<PayloadDescriptor>");
MS_DEBUG_DEV(" s : %s", this->s ? "true" : "false");
MS_DEBUG_DEV(" e : %s", this->e ? "true" : "false");
MS_DEBUG_DEV(" i : %s", this->i ? "true" : "false");
MS_DEBUG_DEV(" d : %s", this->d ? "true" : "false");
MS_DEBUG_DEV(" b : %s", this->b ? "true" : "false");
MS_DEBUG_DEV(" tid : %" PRIu8, this->tid);
if (this->hasLid)
MS_DEBUG_DEV(" lid : %" PRIu8, this->lid);
if (this->hasTl0picidx)
MS_DEBUG_DEV(" tl0picidx : %" PRIu8, this->tl0picidx);
MS_DEBUG_DEV(" isKeyFrame : %s", this->isKeyFrame ? "true" : "false");
MS_DEBUG_DEV("</PayloadDescriptor>");
}
H264::PayloadDescriptorHandler::PayloadDescriptorHandler(H264::PayloadDescriptor* payloadDescriptor)
{
MS_TRACE();
this->payloadDescriptor.reset(payloadDescriptor);
}
void H264::ProcessRtpPacket(RTC::RtpPacket* packet)
bool H264::PayloadDescriptorHandler::Encode(
RTC::Codecs::EncodingContext* encodingContext, uint8_t* /*data*/)
{
MS_TRACE();
auto data = packet->GetPayload();
auto len = packet->GetPayloadLength();
auto* context = static_cast<RTC::Codecs::H264::EncodingContext*>(encodingContext);
PayloadDescriptor* payloadDescriptor = Parse(data, len);
// Check temporal layer.
// TODO: We should check incremental picture id here somehow and let it pass
// if so.
if (this->payloadDescriptor->tid > context->preferences.temporalLayer)
{
return false;
}
else if (context->currentTemporalLayer != context->preferences.temporalLayer)
{
// Payload descriptor contains the target temporal layer.
if (this->payloadDescriptor->tid == context->preferences.temporalLayer)
{
// Upgrade required. Drop current packet if base flag is not set.
// NOTE: It seems that Base Layer Sync flag is not set when in tid 0.
// clang-format off
if (
this->payloadDescriptor->tid > context->currentTemporalLayer &&
!this->payloadDescriptor->b &&
this->payloadDescriptor->tid == 0
)
// clang-format on
{
return false;
}
if (!payloadDescriptor)
return;
context->currentTemporalLayer = this->payloadDescriptor->tid;
}
}
auto* payloadDescriptorHandler = new PayloadDescriptorHandler(payloadDescriptor);
return true;
}
packet->SetPayloadDescriptorHandler(payloadDescriptorHandler);
void H264::PayloadDescriptorHandler::Restore(uint8_t* /*data*/)
{
MS_TRACE();
}
} // namespace Codecs
} // namespace RTC
......@@ -8,15 +8,21 @@ namespace RTC
{
namespace Codecs
{
VP8::PayloadDescriptor* VP8::Parse(const uint8_t* data, size_t len)
/* Class methods. */
VP8::PayloadDescriptor* VP8::Parse(
const uint8_t* data,
size_t len,
RTC::RtpPacket::FrameMarking* /*frameMarking*/,
uint8_t /*frameMarkingLen*/)
{
MS_TRACE();
std::unique_ptr<PayloadDescriptor> payloadDescriptor(new PayloadDescriptor());
if (len < 1)
return nullptr;
std::unique_ptr<PayloadDescriptor> payloadDescriptor(new PayloadDescriptor());
size_t offset{ 0 };
uint8_t byte = data[offset];
......@@ -99,6 +105,44 @@ namespace RTC
return payloadDescriptor.release();
}
void VP8::ProcessRtpPacket(RTC::RtpPacket* packet)
{
MS_TRACE();
auto* data = packet->GetPayload();
auto len = packet->GetPayloadLength();
RtpPacket::FrameMarking* frameMarking{ nullptr };
uint8_t frameMarkingLen{ 0 };
// Read frame-marking.
packet->ReadFrameMarking(&frameMarking, frameMarkingLen);
PayloadDescriptor* payloadDescriptor = VP8::Parse(data, len, frameMarking, frameMarkingLen);
if (!payloadDescriptor)
return;
auto* payloadDescriptorHandler = new PayloadDescriptorHandler(payloadDescriptor);
packet->SetPayloadDescriptorHandler(payloadDescriptorHandler);
// Modify the RtpPacket payload in order to always have two byte pictureId.
if (payloadDescriptor->hasOneBytePictureId)
{
// Shift the RTP payload one byte from the begining of the pictureId field.
packet->ShiftPayload(2, 1, true /*expand*/);
// Set the two byte pictureId marker bit.
data[2] = 0x80;
// Update the payloadDescriptor.
payloadDescriptor->hasOneBytePictureId = false;
payloadDescriptor->hasTwoBytesPictureId = true;
}
}
/* Instance methods. */
void VP8::PayloadDescriptor::Encode(uint8_t* data, uint16_t pictureId, uint8_t tl0PictureIndex) const
{
MS_TRACE();
......@@ -135,7 +179,9 @@ namespace RTC
void VP8::PayloadDescriptor::Restore(uint8_t* data) const
{
this->Encode(data, this->pictureId, this->tl0PictureIndex);
MS_TRACE();
Encode(data, this->pictureId, this->tl0PictureIndex);
}
void VP8::PayloadDescriptor::Dump() const
......@@ -153,13 +199,13 @@ namespace RTC
this->l,
this->t,
this->k);
MS_DEBUG_DEV(" pictureId : %" PRIu16, this->pictureId);
MS_DEBUG_DEV(" tl0PictureIndex : %" PRIu8, this->tl0PictureIndex);
MS_DEBUG_DEV(" tlIndex : %" PRIu8, this->tlIndex);
MS_DEBUG_DEV(" y : %" PRIu8, this->y);
MS_DEBUG_DEV(" keyIndex : %" PRIu8, this->keyIndex);
MS_DEBUG_DEV(" isKeyFrame : %s", this->isKeyFrame ? "true" : "false");
MS_DEBUG_DEV(" hasPictureId : %s", this->hasPictureId ? "true" : "false");
MS_DEBUG_DEV(" pictureId : %" PRIu16, this->pictureId);
MS_DEBUG_DEV(" tl0PictureIndex : %" PRIu8, this->tl0PictureIndex);
MS_DEBUG_DEV(" tlIndex : %" PRIu8, this->tlIndex);
MS_DEBUG_DEV(" y : %" PRIu8, this->y);
MS_DEBUG_DEV(" keyIndex : %" PRIu8, this->keyIndex);
MS_DEBUG_DEV(" isKeyFrame : %s", this->isKeyFrame ? "true" : "false");
MS_DEBUG_DEV(" hasPictureId : %s", this->hasPictureId ? "true" : "false");
MS_DEBUG_DEV(" hasOneBytePictureId : %s", this->hasOneBytePictureId ? "true" : "false");
MS_DEBUG_DEV(" hasTwoBytesPictureId : %s", this->hasTwoBytesPictureId ? "true" : "false");
MS_DEBUG_DEV(" hasTl0PictureIndex : %s", this->hasTl0PictureIndex ? "true" : "false");
......@@ -169,13 +215,17 @@ namespace RTC
VP8::PayloadDescriptorHandler::PayloadDescriptorHandler(VP8::PayloadDescriptor* payloadDescriptor)
{
MS_TRACE();
this->payloadDescriptor.reset(payloadDescriptor);
}
bool VP8::PayloadDescriptorHandler::Encode(
RTC::Codecs::EncodingContext* encodingContext, uint8_t* data)
{
auto* context = static_cast<EncodingContext*>(encodingContext);
MS_TRACE();
auto* context = static_cast<RTC::Codecs::VP8::EncodingContext*>(encodingContext);
// Check whether pictureId and tl0PictureIndex sync is required.
if (context->syncRequired)
......@@ -186,40 +236,44 @@ namespace RTC
}
// Incremental pictureId. Check the temporal layer.
// clang-format off
if (
this->payloadDescriptor->hasPictureId && this->payloadDescriptor->hasTlIndex &&
this->payloadDescriptor->hasTl0PictureIndex)
this->payloadDescriptor->hasPictureId &&
this->payloadDescriptor->hasTlIndex &&
this->payloadDescriptor->hasTl0PictureIndex &&
RTC::SeqManager<uint16_t>::IsSeqHigherThan(
this->payloadDescriptor->pictureId,
context->pictureIdManager.GetMaxInput())
)
// clang-format on
{
if (RTC::SeqManager<uint16_t>::IsSeqHigherThan(
this->payloadDescriptor->pictureId, context->pictureIdManager.GetMaxInput()))
if (this->payloadDescriptor->tlIndex > context->preferences.temporalLayer)
{
if (this->payloadDescriptor->tlIndex > context->preferences.temporalLayer)
{
context->pictureIdManager.Drop(this->payloadDescriptor->pictureId);
context->tl0PictureIndexManager.Drop(this->payloadDescriptor->tl0PictureIndex);
context->pictureIdManager.Drop(this->payloadDescriptor->pictureId);
context->tl0PictureIndexManager.Drop(this->payloadDescriptor->tl0PictureIndex);
return false;
}
else if (context->currentTemporalLayer != context->preferences.temporalLayer)
return false;
}
else if (context->currentTemporalLayer != context->preferences.temporalLayer)
{
// Payload descriptor contains the target temporal layer.
if (this->payloadDescriptor->tlIndex == context->preferences.temporalLayer)
{
// Payload descriptor contains the target temporal layer.
if (this->payloadDescriptor->tlIndex == context->preferences.temporalLayer)
// Upgrade required. Drop current packet if sync flag is not set.
// clang-format off
if (
this->payloadDescriptor->tlIndex > context->currentTemporalLayer &&
!this->payloadDescriptor->y
)
// clang-format on
{
// Upgrade required.
if (context->currentTemporalLayer < this->payloadDescriptor->tlIndex)
{
// Drop current packet if sync flag is not set.
if (this->payloadDescriptor->y != 1u)
{
context->pictureIdManager.Drop(this->payloadDescriptor->pictureId);
context->tl0PictureIndexManager.Drop(this->payloadDescriptor->tl0PictureIndex);
return false;
}
}
context->currentTemporalLayer = this->payloadDescriptor->tlIndex;
context->pictureIdManager.Drop(this->payloadDescriptor->pictureId);
context->tl0PictureIndexManager.Drop(this->payloadDescriptor->tl0PictureIndex);
return false;
}
context->currentTemporalLayer = this->payloadDescriptor->tlIndex;
}
}
}
......@@ -245,39 +299,10 @@ namespace RTC
};
void VP8::PayloadDescriptorHandler::Restore(uint8_t* data)
{
this->payloadDescriptor->Restore(data);
}
void VP8::ProcessRtpPacket(RTC::RtpPacket* packet)
{
MS_TRACE();
auto data = packet->GetPayload();
auto len = packet->GetPayloadLength();
PayloadDescriptor* payloadDescriptor = Parse(data, len);
if (!payloadDescriptor)
return;
auto* payloadDescriptorHandler = new PayloadDescriptorHandler(payloadDescriptor);
packet->SetPayloadDescriptorHandler(payloadDescriptorHandler);
// Modify the RtpPacket payload in order to always have two byte pictureId.
if (payloadDescriptor->hasOneBytePictureId)
{
// Shift the RTP payload one byte from the begining of the pictureId field.
packet->ShiftPayload(2, 1, true /*expand*/);
// Set the two byte pictureId marker bit.
data[2] = 0x80;
// Update the payloadDescriptor.
payloadDescriptor->hasOneBytePictureId = false;
payloadDescriptor->hasTwoBytesPictureId = true;
}
this->payloadDescriptor->Restore(data);
}
} // namespace Codecs
} // namespace RTC
......@@ -171,6 +171,17 @@ namespace RTC
this->rtpHeaderExtensionIds.absSendTime = exten.id;
}
// NOTE: Remove this once framemarking draft becomes RFC.
if (this->rtpHeaderExtensionIds.frameMarking07 == 0u && exten.type == RTC::RtpHeaderExtensionUri::Type::FRAME_MARKING_07)
{
this->rtpHeaderExtensionIds.frameMarking07 = exten.id;
}
if (this->rtpHeaderExtensionIds.frameMarking == 0u && exten.type == RTC::RtpHeaderExtensionUri::Type::FRAME_MARKING)
{
this->rtpHeaderExtensionIds.frameMarking = exten.id;
}
if (this->rtpHeaderExtensionIds.ssrcAudioLevel == 0u && exten.type == RTC::RtpHeaderExtensionUri::Type::SSRC_AUDIO_LEVEL)
{
this->rtpHeaderExtensionIds.ssrcAudioLevel = exten.id;
......@@ -449,6 +460,9 @@ namespace RTC
return;
}
// Pre-process the packet.
PreProcessRtpPacket(packet);
// Media packet.
if (packet->GetSsrc() == rtpStream->GetSsrc())
{
......@@ -929,6 +943,18 @@ namespace RTC
this->listener->OnProducerNewRtpStream(this, static_cast<RTC::RtpStream*>(rtpStream), mappedSsrc);
}
inline void Producer::PreProcessRtpPacket(RTC::RtpPacket* packet)
{
MS_TRACE();
if (this->kind == RTC::Media::Kind::VIDEO)
{
// NOTE: Remove this once framemarking draft becomes RFC.
packet->SetFrameMarking07ExtensionId(this->rtpHeaderExtensionIds.frameMarking07);
packet->SetFrameMarkingExtensionId(this->rtpHeaderExtensionIds.frameMarking);
}
}
inline bool Producer::MangleRtpPacket(RTC::RtpPacket* packet, RTC::RtpStreamRecv* rtpStream) const
{
MS_TRACE();
......@@ -989,47 +1015,76 @@ namespace RTC
}
else if (this->kind == RTC::Media::Kind::VIDEO)
{
// Proxy urn:3gpp:video-orientation.
extenValue = packet->GetExtension(this->rtpHeaderExtensionIds.videoOrientation, extenLen);
// Add http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time.
// NOTE: Just if this is simulcast or SVC.
if (this->type == RTC::RtpParameters::Type::SIMULCAST || this->type == RTC::RtpParameters::Type::SVC)
{
extenLen = 3u;
auto now = DepLibUV::GetTime();
auto absSendTime = static_cast<uint32_t>(((now << 18) + 500) / 1000) & 0x00FFFFFF;
Utils::Byte::Set3Bytes(bufferPtr, 0, absSendTime);
extensions.emplace_back(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::ABS_SEND_TIME), extenLen, bufferPtr);
}
// NOTE: Remove this once framemarking draft becomes RFC.
// Proxy urn:ietf:params:rtp-hdrext:framemarking.
extenValue = packet->GetExtension(this->rtpHeaderExtensionIds.frameMarking07, extenLen);
if (extenValue)
{
std::memcpy(bufferPtr, extenValue, extenLen);
extensions.emplace_back(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::VIDEO_ORIENTATION),
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::FRAME_MARKING_07),
extenLen,
bufferPtr);
bufferPtr += extenLen;
}
// Proxy urn:ietf:params:rtp-hdrext:toffset.
extenValue = packet->GetExtension(this->rtpHeaderExtensionIds.toffset, extenLen);
// Proxy urn:ietf:params:rtp-hdrext:framemarking.
extenValue = packet->GetExtension(this->rtpHeaderExtensionIds.frameMarking, extenLen);
if (extenValue)
{
std::memcpy(bufferPtr, extenValue, extenLen);
extensions.emplace_back(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::TOFFSET), extenLen, bufferPtr);
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::FRAME_MARKING), extenLen, bufferPtr);
bufferPtr += extenLen;
}
// Add http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time.
// NOTE: Just if this is simulcast or SVC.
if (this->type == RTC::RtpParameters::Type::SIMULCAST || this->type == RTC::RtpParameters::Type::SVC)
// Proxy urn:3gpp:video-orientation.
extenValue = packet->GetExtension(this->rtpHeaderExtensionIds.videoOrientation, extenLen);
if (extenValue)
{
extenLen = 3u;
std::memcpy(bufferPtr, extenValue, extenLen);
auto now = DepLibUV::GetTime();
auto absSendTime = static_cast<uint32_t>(((now << 18) + 500) / 1000) & 0x00FFFFFF;
extensions.emplace_back(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::VIDEO_ORIENTATION),
extenLen,
bufferPtr);
Utils::Byte::Set3Bytes(bufferPtr, 0, absSendTime);
bufferPtr += extenLen;
}
// Proxy urn:ietf:params:rtp-hdrext:toffset.
extenValue = packet->GetExtension(this->rtpHeaderExtensionIds.toffset, extenLen);
if (extenValue)
{
std::memcpy(bufferPtr, extenValue, extenLen);
extensions.emplace_back(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::ABS_SEND_TIME), extenLen, bufferPtr);
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::TOFFSET), extenLen, bufferPtr);
bufferPtr += extenLen;
}
}
......@@ -1040,6 +1095,11 @@ namespace RTC
// be interested in after passing it to the Router).
packet->SetAbsSendTimeExtensionId(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::ABS_SEND_TIME));
// NOTE: Remove this once framemarking draft becomes RFC.
packet->SetFrameMarking07ExtensionId(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::FRAME_MARKING_07));
packet->SetFrameMarkingExtensionId(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::FRAME_MARKING));
packet->SetSsrcAudioLevelExtensionId(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::SSRC_AUDIO_LEVEL));
packet->SetVideoOrientationExtensionId(
......
......@@ -17,6 +17,9 @@ namespace RTC
{ "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id", RtpHeaderExtensionUri::Type::REPAIRED_RTP_STREAM_ID },
{ "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time", RtpHeaderExtensionUri::Type::ABS_SEND_TIME },
{ "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01", RtpHeaderExtensionUri::Type::TRANSPORT_CC_01 },
// NOTE: Remove this once framemarking draft becomes RFC.
{ "http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07", RtpHeaderExtensionUri::Type::FRAME_MARKING_07 },
{ "urn:ietf:params:rtp-hdrext:framemarking", RtpHeaderExtensionUri::Type::FRAME_MARKING },
{ "urn:ietf:params:rtp-hdrext:ssrc-audio-level", RtpHeaderExtensionUri::Type::SSRC_AUDIO_LEVEL },
{ "urn:3gpp:video-orientation", RtpHeaderExtensionUri::Type::VIDEO_ORIENTATION },
{ "urn:ietf:params:rtp-hdrext:toffset", RtpHeaderExtensionUri::Type::TOFFSET }
......
......@@ -222,6 +222,7 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(packet->GetHeaderExtensionLength() == 12);
REQUIRE(packet->HasOneByteExtensions());
REQUIRE(packet->HasTwoBytesExtensions() == false);
REQUIRE(packet->GetPayloadLength() == 0);
delete packet;
}
......@@ -259,6 +260,7 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(packet->GetHeaderExtensionLength() == 16);
REQUIRE(packet->HasOneByteExtensions() == false);
REQUIRE(packet->HasTwoBytesExtensions());
REQUIRE(packet->GetPayloadLength() == 0);
extenValue = packet->GetExtension(1, extenLen);
REQUIRE(extenValue == nullptr);
......@@ -724,4 +726,54 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
delete packet;
}
SECTION("read frame-marking extension")
{
// clang-format off
uint8_t buffer[] =
{
0b10010000, 0b00000001, 0, 8,
0, 0, 0, 4,
0, 0, 0, 5,
0xBE, 0xDE, 0, 1, // Header Extension
0b00110010, 0b10101011, 1, 5,
1, 2, 3, 4
};
// clang-format on
RtpPacket* packet = RtpPacket::Parse(buffer, sizeof(buffer));
if (!packet)
FAIL("not a RTP packet");
REQUIRE(packet->HasMarker() == false);
REQUIRE(packet->HasHeaderExtension() == true);
REQUIRE(packet->GetPayloadType() == 1);
REQUIRE(packet->GetSequenceNumber() == 8);
REQUIRE(packet->GetTimestamp() == 4);
REQUIRE(packet->GetSsrc() == 5);
REQUIRE(packet->GetHeaderExtensionId() == 0xBEDE);
REQUIRE(packet->GetHeaderExtensionLength() == 4);
REQUIRE(packet->HasOneByteExtensions());
REQUIRE(packet->HasTwoBytesExtensions() == false);
REQUIRE(packet->GetPayloadLength() == 4);
packet->SetFrameMarkingExtensionId(3);
RtpPacket::FrameMarking* frameMarking;
uint8_t frameMarkingLen;
REQUIRE(packet->ReadFrameMarking(&frameMarking, frameMarkingLen) == true);
REQUIRE(frameMarkingLen == 3);
REQUIRE(frameMarking->start == 1);
REQUIRE(frameMarking->end == 0);
REQUIRE(frameMarking->independent == 1);
REQUIRE(frameMarking->discardable == 0);
REQUIRE(frameMarking->base == 1);
REQUIRE(frameMarking->tid == 3);
REQUIRE(frameMarking->lid == 1);
REQUIRE(frameMarking->tl0picidx == 5);
delete packet;
}
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论