forked from roc-streaming/roc-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[spo] roc-streaminggh-731: Implement PLC support
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
Showing
27 changed files
with
2,729 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.