Skip to content

Commit

Permalink
[spo] roc-streaminggh-731: Implement PLC support
Browse files Browse the repository at this point in the history
Add PlcReader (pipeline element) and IPlc (PLC backend).
They are used to fill lost samples with something better
than silence, e.g. interpolated data.

Also add a dummy PLC backend "BeepPlc", that replaces
losses with a loud beep (useful for debugging).

Sponsored-by: waspd
  • Loading branch information
gavv committed Jul 17, 2024
1 parent 6611022 commit 4575698
Show file tree
Hide file tree
Showing 27 changed files with 2,729 additions and 118 deletions.
8 changes: 4 additions & 4 deletions docs/man/roc-recv.1
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ Resampler backend (possible values=\(dqdefault\(dq, \(dqbuiltin\(dq, \(dqspeex\
.BI \-\-resampler\-profile\fB= ENUM
Resampler profile (possible values=\(dqlow\(dq, \(dqmedium\(dq, \(dqhigh\(dq default=\(gamedium\(aq)
.TP
.BI \-\-plc\fB= ENUM
Which PLC algorithm to use (possible values=\(dqnone\(dq, \(dqbeep\(dq default=\(ganone\(aq)
.TP
.B \-1\fP,\fB \-\-oneshot
Exit when last connected client disconnects (default=off)
.TP
.B \-\-profiling
Enable self\-profiling (default=off)
.TP
.B \-\-beep
Enable beeping on packet loss (default=off)
.TP
.BI \-\-color\fB= ENUM
Set colored logging mode for stderr output (possible values=\(dqauto\(dq, \(dqalways\(dq, \(dqnever\(dq default=\(gaauto\(aq)
.UNINDENT
Expand Down Expand Up @@ -155,7 +155,7 @@ The list of supported protocols can be retrieved using \fB\-\-list\-supported\fP
.sp
The host field should be either FQDN (domain name), or IPv4 address, or IPv6 address in square brackets. It may be \fB0.0.0.0\fP (for IPv4) or \fB[::]\fP (for IPv6) to bind endpoint to all network interfaces.
.sp
The port field can be omitted if the protocol defines standard port. Otherwise, it is mandatory. It may be set to zero to bind endpoint to a radomly chosen ephemeral port.
The port field can be omitted if the protocol defines standard port. Otherwise, it is mandatory. It may be set to zero to bind endpoint to a randomly chosen ephemeral port.
.sp
The path and query fields are allowed only for protocols that support them, e.g. for RTSP.
.sp
Expand Down
4 changes: 2 additions & 2 deletions docs/sphinx/manuals/roc_recv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ Options
--latency-profile=ENUM Latency tuning profile (possible values="default", "responsive", "gradual", "intact" default=`default')
--resampler-backend=ENUM Resampler backend (possible values="default", "builtin", "speex", "speexdec" default=`default')
--resampler-profile=ENUM Resampler profile (possible values="low", "medium", "high" default=`medium')
--plc=ENUM Which PLC algorithm to use (possible values="none", "beep" default=`none')
-1, --oneshot Exit when last connected client disconnects (default=off)
--profiling Enable self-profiling (default=off)
--beep Enable beeping on packet loss (default=off)
--color=ENUM Set colored logging mode for stderr output (possible values="auto", "always", "never" default=`auto')

Endpoint URI
Expand All @@ -66,7 +66,7 @@ The list of supported protocols can be retrieved using ``--list-supported`` opti

The host field should be either FQDN (domain name), or IPv4 address, or IPv6 address in square brackets. It may be ``0.0.0.0`` (for IPv4) or ``[::]`` (for IPv6) to bind endpoint to all network interfaces.

The port field can be omitted if the protocol defines standard port. Otherwise, it is mandatory. It may be set to zero to bind endpoint to a radomly chosen ephemeral port.
The port field can be omitted if the protocol defines standard port. Otherwise, it is mandatory. It may be set to zero to bind endpoint to a randomly chosen ephemeral port.

The path and query fields are allowed only for protocols that support them, e.g. for RTSP.

Expand Down
71 changes: 71 additions & 0 deletions src/internal_modules/roc_audio/beep_plc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include "roc_audio/beep_plc.h"
#include "roc_audio/sample_spec_to_str.h"
#include "roc_core/panic.h"

namespace roc {
namespace audio {

BeepPlc::BeepPlc(core::IArena& arena,
FrameFactory& frame_factory,
const PlcConfig& config,
const SampleSpec& sample_spec)
: sample_spec_(sample_spec)
, signal_pos_(0) {
if (!sample_spec_.is_valid() || !sample_spec_.is_raw()) {
roc_panic("beep plc: required valid sample specs with raw format: spec=%s",
sample_spec_to_str(sample_spec_).c_str());
}
}

BeepPlc::~BeepPlc() {
}

status::StatusCode BeepPlc::init_status() const {
return status::StatusOK;
}

SampleSpec BeepPlc::sample_spec() const {
return sample_spec_;
}

packet::stream_timestamp_t BeepPlc::lookbehind_len() {
return 0;
}

packet::stream_timestamp_t BeepPlc::lookahead_len() {
return 0;
}

void BeepPlc::process_history(Frame& hist_frame) {
sample_spec_.validate_frame(hist_frame);

signal_pos_ += hist_frame.duration();
}

void BeepPlc::process_loss(Frame& lost_frame, Frame* prev_frame, Frame* next_frame) {
sample_spec_.validate_frame(lost_frame);

sample_t* lost_samples = lost_frame.raw_samples();
const size_t lost_samples_count =
lost_frame.num_raw_samples() / sample_spec_.num_channels();

for (size_t ns = 0; ns < lost_samples_count; ns++) {
const sample_t s = (sample_t)std::sin(2.0 * M_PI / sample_spec_.sample_rate()
* 880.0 * signal_pos_++);

for (size_t nc = 0; nc < sample_spec_.num_channels(); nc++) {
*lost_samples++ = s;
}
}
}

} // namespace audio
} // namespace roc
63 changes: 63 additions & 0 deletions src/internal_modules/roc_audio/beep_plc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2024 Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

//! @file roc_audio/beep_plc.h
//! @brief Beep PLC.

#ifndef ROC_AUDIO_BEEP_PLC_H_
#define ROC_AUDIO_BEEP_PLC_H_

#include "roc_audio/frame_factory.h"
#include "roc_audio/iplc.h"
#include "roc_audio/plc_config.h"
#include "roc_audio/sample_spec.h"
#include "roc_core/stddefs.h"

namespace roc {
namespace audio {

//! Beep "PLC".
//! Replaces lost samples with a loud beep.
//! Useful for debugging to distinguish losses easily.
class BeepPlc : public IPlc {
public:
//! Initialize.
BeepPlc(core::IArena& arena,
FrameFactory& frame_factory,
const PlcConfig& config,
const SampleSpec& sample_spec);

virtual ~BeepPlc();

//! Check if the object was successfully constructed.
virtual status::StatusCode init_status() const;

//! Sample specification expected by PLC.
virtual SampleSpec sample_spec() const;

//! How many samples before lost frame are needed for interpolation.
virtual packet::stream_timestamp_t lookbehind_len();

//! How many samples after lost frame are needed for interpolation.
virtual packet::stream_timestamp_t lookahead_len();

//! When next frame has no losses, PLC reader calls this method.
virtual void process_history(Frame& hist_frame);

//! When next frame is lost, PLC reader calls this method.
virtual void process_loss(Frame& lost_frame, Frame* prev_frame, Frame* next_frame);

private:
const SampleSpec sample_spec_;
uint32_t signal_pos_;
};

} // namespace audio
} // namespace roc

#endif // ROC_AUDIO_BEEP_PLC_H_
10 changes: 7 additions & 3 deletions src/internal_modules/roc_audio/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ unsigned Frame::flags() const {
return flags_;
}

bool Frame::has_flags(unsigned flags) const {
return (flags_ & flags) != 0;
}

void Frame::set_flags(unsigned flags) {
flags_ = (uint16_t)flags;
}
Expand Down Expand Up @@ -153,9 +157,9 @@ void Frame::set_capture_timestamp(core::nanoseconds_t capture_ts) {

void Frame::print() const {
char flags_str[] = {
!(flags_ & HasSignal) ? 'b' : '.', // b=blank
(flags_ & HasGaps) ? 'i' : '.', // i=incomplete
(flags_ & HasDrops) ? 'd' : '.', // d=drops
(flags_ & HasSignal) ? 's' : '-',
(flags_ & HasGaps) ? 'g' : '-',
(flags_ & HasDrops) ? 'd' : '-',
'\0',
};

Expand Down
3 changes: 3 additions & 0 deletions src/internal_modules/roc_audio/frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class Frame : public core::RefCounted<Frame, core::PoolAllocation> {
//! Get flags.
unsigned flags() const;

//! Check if frame has all of the given flags.
bool has_flags(unsigned flags) const;

//! Set flags.
void set_flags(unsigned flags);

Expand Down
18 changes: 18 additions & 0 deletions src/internal_modules/roc_audio/iplc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2024 Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include "roc_audio/iplc.h"

namespace roc {
namespace audio {

IPlc::~IPlc() {
}

} // namespace audio
} // namespace roc
91 changes: 91 additions & 0 deletions src/internal_modules/roc_audio/iplc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2024 Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

//! @file roc_audio/iplc.h
//! @brief PLC interface.

#ifndef ROC_AUDIO_IPLC_H_
#define ROC_AUDIO_IPLC_H_

#include "roc_audio/frame.h"
#include "roc_audio/sample_spec.h"
#include "roc_status/status_code.h"

namespace roc {
namespace audio {

//! Packet loss concealment (PLC) interface.
//!
//! Used to mask the effects of packet loss when lost packets were
//! not recovered using FEC.
//!
//! Unlike FEC, which recovers original packet bit-to-bit (but may fail),
//! PLC is lossy as it uses interpolation. However usually it's still
//! better than silence, because distortion becomes less audible.
//!
//! IPlc is invoked by PlcReader. IPlc implements interpolation algorithm,
//! and PlcReader integrates it into receiver pipeline.
//!
//! Each frame, PlcReader invokes either process_history() (so that PLC can
//! remember previously played samples), or process_loss() (to ask PLC to
//! generate interpolated samples), depending on whether there's a loss.
//!
//! PLC implementation is allowed to use arbitrary PCM format, specified
//! by its sample_spec() method.
class IPlc {
public:
//! Deinitialization.
virtual ~IPlc();

//! Check if the object was successfully constructed.
virtual status::StatusCode init_status() const = 0;

//! Sample specification expected by PLC.
virtual SampleSpec sample_spec() const = 0;

//! How many samples before lost frame are needed for interpolation.
//! @remarks
//! - If it returns N, PLC reader will remember last N samples before the
//! gap. It will provide them to process_loss() via prev_frame argument.
//! - If it returns 0, prev_frame argument will be NULL.
virtual packet::stream_timestamp_t lookbehind_len() = 0;

//! How many samples after lost frame are needed for interpolation.
//! @remarks
//! - If it returns N, PLC reader will try to read next N samples following
//! the gap. It will provide them to process_loss() via next_frame argument.
//! - If it returns 0, next_frame argument will be NULL.
virtual packet::stream_timestamp_t lookahead_len() = 0;

//! When next frame has no losses, PLC reader calls this method.
//! PLC may remember samples to use it later for interpolation.
virtual void process_history(Frame& hist_frame) = 0;

//! When next frame is lost, PLC reader calls this method.
//! PLC should fill the lost frame with the interpolated data.
//! @remarks
//! - @p lost_frame is the frame to be filled with the interpolated data
//! - @p prev_frame is non-null only if lookbehind_len() returns non-zero;
//! in this case, prev_frame contains last N samples before the loss,
//! where N <= lookbehind_len()
//! - @p next_frame is non-null only if lookahead_len() returns non-zero,
//! and packets following the loss have already arrived;
//! in this case, next_frame contains next N samples after the loss,
//! where N <= lookahead_len()
//! - @p prev_frame may be shorter only in the very beginning of the stream,
//! when there are not enough samples before the loss
//! - @p next_frame may be shorter or even empty quite frequently,
//! depending on whether packets next to the loss already arrived
virtual void
process_loss(Frame& lost_frame, Frame* prev_frame, Frame* next_frame) = 0;
};

} // namespace audio
} // namespace roc

#endif // ROC_AUDIO_IPLC_H_
36 changes: 36 additions & 0 deletions src/internal_modules/roc_audio/plc_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include "roc_audio/plc_config.h"

namespace roc {
namespace audio {

void PlcConfig::deduce_defaults() {
if (backend == PlcBackend_Default) {
backend = PlcBackend_None;
}
}

const char* plc_backend_to_str(PlcBackend backend) {
switch (backend) {
case PlcBackend_Default:
return "default";
case PlcBackend_None:
return "none";
case PlcBackend_Beep:
return "beep";
case PlcBackend_Max:
break;
}

return "unknown";
}

} // namespace audio
} // namespace roc
Loading

0 comments on commit 4575698

Please sign in to comment.