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.
- Loading branch information
Showing
9 changed files
with
186 additions
and
9 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
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
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,65 @@ | ||
/* | ||
* Copyright (c) 2023 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 "stream_stats_monitor.h" | ||
#include "roc_status/status_code.h" | ||
#include <math.h> | ||
|
||
namespace roc { | ||
namespace rtp { | ||
|
||
StreamStatsMonitor::StreamStatsMonitor(packet::IReader& reader, | ||
core::IArena& arena, | ||
const audio::SampleSpec& sample_spec, | ||
core::nanoseconds_t default_packet_length, | ||
const StreamStatsConfig &config) | ||
: reader_(reader) | ||
, arena_(arena) | ||
, config_(config) | ||
, packet_length_est_(default_packet_length) | ||
, sample_spec_(sample_spec) | ||
, npack_win_(ceil(config_.window_duration/default_packet_length)) | ||
, enq_ts_buff_(arena) | ||
, enq_ts_buff_i_(0) | ||
, enq_ts_accum_(0) | ||
, enq_ts_buff_ready_(false) | ||
{ | ||
resize_buffers_(); | ||
} | ||
|
||
status::StatusCode StreamStatsMonitor::read(packet::PacketPtr& packet) { | ||
status::StatusCode result = reader_.read(packet); | ||
if (result == status::StatusOK) { | ||
enq_ts_accum_ -= enq_ts_buff_[enq_ts_buff_i_]; | ||
enq_ts_buff_[enq_ts_buff_i_] = packet->udp()->enqueue_ts; | ||
enq_ts_accum_ += enq_ts_buff_[enq_ts_buff_i_]; | ||
|
||
if (++enq_ts_buff_i_ >= npack_win_) { | ||
enq_ts_buff_ready_ = true; | ||
enq_ts_buff_i_ = 0; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
void StreamStatsMonitor::resize_buffers_() { | ||
npack_win_ = ceil(config_.window_duration/packet_length_est_); | ||
enq_ts_buff_.resize(npack_win_); | ||
} | ||
|
||
core::nanoseconds_t StreamStatsMonitor::enq_ts_variance_(core::nanoseconds_t mean) const { | ||
core::nanoseconds_t var = 0; | ||
for (size_t i = 0; i < npack_win_; ++i) { | ||
var += enq_ts_buff_[i]-mean; | ||
} | ||
return 0; | ||
} | ||
|
||
} // namespace packet | ||
} // 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,72 @@ | ||
/* | ||
* Copyright (c) 2023 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_rtp/stream_stats_monitor.h | ||
//! @brief Calculates basic network stream statistics. | ||
|
||
|
||
#ifndef ROC_PACKET_STREAMSTATSMONITOR_H_ | ||
#define ROC_PACKET_STREAMSTATSMONITOR_H_ | ||
|
||
#include "roc_core/noncopyable.h" | ||
#include "roc_packet/ireader.h" | ||
#include "roc_core/time.h" | ||
#include "roc_audio/sample_spec.h" | ||
#include "roc_status/status_code.h" | ||
#include "roc_packet/ireader.h" | ||
#include "roc_core/array.h" | ||
|
||
namespace roc { | ||
namespace rtp { | ||
|
||
struct StreamStatsConfig { | ||
core::nanoseconds_t window_duration; | ||
core::nanoseconds_t window_overlap; | ||
}; | ||
|
||
struct StreamStats { | ||
core::nanoseconds_t windowed_max_jitter; | ||
|
||
size_t windowed_npackets; | ||
size_t windowed_lost_packets; | ||
size_t windowed_recovered_packets; | ||
|
||
float packet_loss_rate; | ||
}; | ||
|
||
class StreamStatsMonitor : public packet::IReader, public core::NonCopyable<> { | ||
public: | ||
StreamStatsMonitor(packet::IReader& reader, core::IArena& arena, | ||
const audio::SampleSpec& sample_spec, | ||
core::nanoseconds_t default_packet_length, | ||
const StreamStatsConfig &config); | ||
|
||
virtual ROC_ATTR_NODISCARD status::StatusCode read(packet::PacketPtr& packet); | ||
|
||
private: | ||
void resize_buffers_(); | ||
core::nanoseconds_t enq_ts_variance_(core::nanoseconds_t mean) const; | ||
|
||
packet::IReader& reader_; | ||
core::IArena& arena_; | ||
const StreamStatsConfig config_; | ||
const audio::SampleSpec sample_spec_; | ||
|
||
core::nanoseconds_t packet_length_est_; | ||
size_t npack_win_; | ||
|
||
core::Array<core::nanoseconds_t> enq_ts_buff_; | ||
size_t enq_ts_buff_i_; | ||
core::nanoseconds_t enq_ts_accum_; | ||
bool enq_ts_buff_ready_; | ||
}; | ||
|
||
} // namespace packet | ||
} // namespace roc | ||
|
||
#endif // ROC_PACKET_STREAMSTATSMONITOR_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