Skip to content

Commit

Permalink
Add option janus_playout_delay_support
Browse files Browse the repository at this point in the history
  • Loading branch information
danstiner committed Dec 7, 2021
1 parent bd2d503 commit 91170b7
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 32 deletions.
5 changes: 5 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ sources = files([
'src/VideoDecoders/H264VideoDecoder.cpp',
# RTP Utilities
'src/Rtp/ExtendedSequenceCounter.cpp',
'src/Rtp/JanusRtpPacketBuilder.cpp',
'src/Rtp/RtpPacket.cpp',
# Service Connections
'src/ServiceConnections/DummyServiceConnection.cpp',
Expand Down Expand Up @@ -69,6 +70,10 @@ if systemd_dep.found()
add_project_arguments('-DSYSTEMD_WATCHDOG_SUPPORT', language: 'cpp')
endif

if get_option('janus_playout_delay_support')
add_project_arguments('-DJANUS_PLAYOUT_DELAY_SUPPORT', language: 'cpp')
endif

deps = [
dependency('glib-2.0'),
dependency('libsrtp2'),
Expand Down
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
option('systemd_watchdog_support', type : 'feature', value : 'auto') # http://0pointer.de/blog/projects/watchdog.html
option('janus_playout_delay_support', type : 'boolean', value: false)
3 changes: 2 additions & 1 deletion src/JanusFtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,8 @@ std::string JanusFtl::generateSdpOffer(const ActiveSession& session, const Janus
"a=rtcp-fb:" << videoPayloadType << " nack pli\r\n" << // Send us picture-loss-indicators
// "a=rtcp-fb:96 nack goog-remb\r\n" << // Send some congestion indicator thing
"a=sendonly\r\n" <<
"a=extmap:1 urn:ietf:params:rtp-hdrext:sdes:mid\r\n";
"a=extmap:1 urn:ietf:params:rtp-hdrext:sdes:mid\r\n" <<
"a=extmap:6 http://www.webrtc.org/experiments/rtp-hdrext/playout-delay\r\n";
}
return offerStream.str();
}
Expand Down
29 changes: 11 additions & 18 deletions src/JanusSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

#include "JanusSession.h"
#include "Rtp/JanusRtpPacketBuilder.h"

extern "C"
{
Expand All @@ -28,29 +29,21 @@ JanusSession::JanusSession(janus_plugin_session* handle, janus_callbacks* janusC
#pragma region Public methods
void JanusSession::SendRtpPacket(const RtpPacket& packet, const MediaMetadata& mediaMetadata)
{
if (!isStarted)
if (!isStarted || handle->gateway_handle == nullptr)
{
return;
}

bool isVideoPacket = packet.Header()->Type == mediaMetadata.VideoPayloadType;

// Sadly, we can't avoid a copy here because the janus_plugin_rtp struct doesn't take a
// const buffer. So allocate some storage to copy.
std::byte packetBuffer[2048] { std::byte(0) };
std::copy(packet.Bytes.begin(), packet.Bytes.end(), packetBuffer);
auto builder = JanusRtpPacketBuilder(packet.Bytes);

janus_plugin_rtp janusRtp =
{
.video = isVideoPacket,
.buffer = reinterpret_cast<char*>(&packetBuffer[0]),
.length = static_cast<uint16_t>(packet.Bytes.size())
};
janus_plugin_rtp_extensions_reset(&janusRtp.extensions);
if (handle->gateway_handle != nullptr)
{
janusCore->relay_rtp(handle, &janusRtp);
}
#if defined(JANUS_PLAYOUT_DELAY_SUPPORT)
// TODO don't add extension to every packet, that wastes bytes and not every viewer needs it
builder.PlayoutDelay(20, 1000);
#endif

janus_plugin_rtp janusRtp = builder.Build(mediaMetadata.VideoPayloadType);

janusCore->relay_rtp(handle, &janusRtp);
}
#pragma endregion

Expand Down
4 changes: 2 additions & 2 deletions src/Rtp/ExtendedSequenceCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* @copyright Copyright (c) 2021 Daniel Stiner
*/

#include <iostream>

#include "ExtendedSequenceCounter.h"

#include <iostream>

bool ExtendedSequenceCounter::Extend(rtp_sequence_num_t seq, rtp_extended_sequence_num_t* extendedSeq)
{
bool valid = UpdateState(seq);
Expand Down
27 changes: 27 additions & 0 deletions src/Rtp/JanusRtpPacketBuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @file JanusRtpPacketBuilder.cpp
* @author Daniel Stiner ([email protected])
* @date 2021-12-06
* @copyright Copyright (c) 2021 Daniel Stiner
*/

#include "JanusRtpPacketBuilder.h"

#include "RtpPacket.h"

#include <assert.h>

janus_plugin_rtp JanusRtpPacketBuilder::Build(rtp_payload_type_t videoPayloadType)
{
bool isVideoPacket = RtpPacket::GetRtpHeader(this->buffer)->Type == videoPayloadType;

janus_plugin_rtp janusRtp =
{
.video = isVideoPacket,
.buffer = reinterpret_cast<char *>(this->buffer.data()),
.length = static_cast<uint16_t>(this->buffer.size()),
.extensions = this->extensions,
};

return janusRtp;
}
50 changes: 50 additions & 0 deletions src/Rtp/JanusRtpPacketBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @file JanusRtpPacketBuilder.h
* @author Daniel Stiner ([email protected])
* @date 2021-12-06
* @copyright Copyright (c) 2021 Daniel Stiner
*/

#pragma once

#include "Types.h"

#include <assert.h>
#include <span>

extern "C"
{
#include <plugins/plugin.h>
}

class JanusRtpPacketBuilder
{
private:
/* Private fields */
std::vector<std::byte> buffer;
janus_plugin_rtp_extensions extensions;

public:
/* Constructor/Destructor */
JanusRtpPacketBuilder(std::span<const std::byte> packet) : buffer(packet.begin(), packet.end())
{
janus_plugin_rtp_extensions_reset(&this->extensions);
}

/* Public methods */
janus_plugin_rtp Build(rtp_payload_type_t videoPayloadType);

#if defined(JANUS_PLAYOUT_DELAY_SUPPORT)
JanusRtpPacketBuilder &PlayoutDelay(int16_t min, int16_t max)
{
assert(min > 0);
assert(max > 0);
assert(max >= min);
assert(min <= 0x0FFF);
assert(max <= 0x0FFF);
this->extensions.playout_delay_min = min;
this->extensions.playout_delay_max = max;
return *this;
}
#endif
};
12 changes: 6 additions & 6 deletions src/Rtp/RtpPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
#include <netinet/in.h>

#pragma region Static utility methods
const RtpHeader* RtpPacket::GetRtpHeader(const std::vector<std::byte>& rtpPacket)
const RtpHeader* RtpPacket::GetRtpHeader(std::span<const std::byte> rtpPacket)
{
return reinterpret_cast<const RtpHeader*>(rtpPacket.data());
}

const rtp_sequence_num_t RtpPacket::GetRtpSequence(const std::vector<std::byte>& rtpPacket)
const rtp_sequence_num_t RtpPacket::GetRtpSequence(std::span<const std::byte> rtpPacket)
{
return ntohs(GetRtpHeader(rtpPacket)->SequenceNumber);
}

const std::span<const std::byte> RtpPacket::GetRtpPayload(const std::vector<std::byte>& rtpPacket)
const std::span<const std::byte> RtpPacket::GetRtpPayload(std::span<const std::byte> rtpPacket)
{
if (rtpPacket.size() < 12)
{
Expand Down Expand Up @@ -63,10 +63,10 @@ const std::span<const std::byte> RtpPacket::GetRtpPayload(const std::vector<std:

#pragma region Constructor/Destructor
RtpPacket::RtpPacket(
const std::vector<std::byte>& bytes,
const rtp_extended_sequence_num_t extendedSequenceNum)
std::span<const std::byte> bytes,
rtp_extended_sequence_num_t extendedSequenceNum)
:
Bytes(bytes),
Bytes(bytes.begin(), bytes.end()),
ExtendedSequenceNum(extendedSequenceNum)
{
}
Expand Down
10 changes: 5 additions & 5 deletions src/Rtp/RtpPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class RtpPacket
{
public:
/* Static utility methods */
static const RtpHeader* GetRtpHeader(const std::vector<std::byte>& rtpPacket);
static const rtp_sequence_num_t GetRtpSequence(const std::vector<std::byte>& rtpPacket);
static const std::span<const std::byte> GetRtpPayload(const std::vector<std::byte>& rtpPacket);
static const RtpHeader* GetRtpHeader(std::span<const std::byte> rtpPacket);
static const rtp_sequence_num_t GetRtpSequence(std::span<const std::byte> rtpPacket);
static const std::span<const std::byte> GetRtpPayload(std::span<const std::byte> rtpPacket);

/* Constructor/Destructor */
RtpPacket(
const std::vector<std::byte>& bytes,
const rtp_extended_sequence_num_t extendedSequenceNum);
std::span<const std::byte> bytes,
rtp_extended_sequence_num_t extendedSequenceNum);

/* Public fields */
const std::vector<std::byte> Bytes;
Expand Down

0 comments on commit 91170b7

Please sign in to comment.