Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Toggle main menu visibility
Loading...
Searching...
No Matches
profiler.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2019 Roc Streaming authors
3
*
4
* This Source Code Form is subject to the terms of the Mozilla Public
5
* License, v. 2.0. If a copy of the MPL was not distributed with this
6
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
*/
8
9
//! @file roc_audio/profiler.h
10
//! @brief Profiler.
11
12
#ifndef ROC_AUDIO_PROFILER_H_
13
#define ROC_AUDIO_PROFILER_H_
14
15
#include "
roc_audio/frame.h
"
16
#include "
roc_audio/sample_spec.h
"
17
#include "
roc_core/array.h
"
18
#include "
roc_core/iarena.h
"
19
#include "
roc_core/list.h
"
20
#include "
roc_core/noncopyable.h
"
21
#include "
roc_core/rate_limiter.h
"
22
#include "
roc_core/time.h
"
23
#include "
roc_packet/units.h
"
24
25
namespace
roc
{
26
namespace
audio
{
27
28
//! Profiler Configuration Parameters.
29
//! Controls profiling interval and duration of each circular buffer chunk.
30
struct
ProfilerConfig
{
31
//! Default Initialization.
32
ProfilerConfig
()
33
:
profiling_interval
(
core
::Second)
34
,
chunk_duration
(10 *
core
::Millisecond) {
35
}
36
37
//! Override Initialization.
38
ProfilerConfig
(
core::nanoseconds_t
interval,
core::nanoseconds_t
duration)
39
:
profiling_interval
(interval)
40
,
chunk_duration
(duration) {
41
}
42
43
//! Rolling window duration and reporting interval.
44
core::nanoseconds_t
profiling_interval
;
45
46
//! Duration of samples each chunk can hold in the circular buffer.
47
core::nanoseconds_t
chunk_duration
;
48
};
49
50
//! Profiler
51
//! The role of the profiler is to report the average processing speed (# of samples
52
//! processed per time unit) during the last N seconds. We want to calculate the average
53
//! processing speed efficiently (with O(1) complexity, without allocations, and as
54
//! lightweight as possible). The problems with this are that we have variable-sized
55
//! frames and SMA requires fixed-size chunks. To efficiently perform this calculation a
56
//! ring buffer is employed. The idea behind the ring buffer is that each chunk of the
57
//! buffer is the average speed of 10ms worth of samples. The ring buffer is initialized
58
//! with fixed size (N * 1000)ms / (10ms) chunks. Within each chunk a weighted mean is
59
//! used to calculate the average speed during those 10ms. Each frame will contribute a
60
//! different number of samples to each chunk, the chunk speed is then weighted based on
61
//! how many samples are contributed at what frame speed. As the chunks get populated the
62
//! moving average is calculated. When the buffer is not entirely full the cumulative
63
//! moving average algorithm is used and once the buffer is full the simple moving average
64
//! algorithm is used.
65
class
Profiler
:
public
core::NonCopyable
<> {
66
public
:
67
//! Initialization.
68
Profiler
(
core::IArena
& arena,
69
const
SampleSpec
& sample_spec,
70
ProfilerConfig
profiler_config);
71
72
//! Check if the profiler was succefully constructed.
73
bool
is_valid
()
const
;
74
75
//! Profile frame speed.
76
void
add_frame
(
packet::stream_timestamp_t
frame_duration,
77
core::nanoseconds_t
elapsed);
78
79
//! Get computed average.
80
float
get_moving_avg
();
81
82
private
:
83
void
update_moving_avg_(
packet::stream_timestamp_t
frame_duration,
84
core::nanoseconds_t
elapsed);
85
86
core::RateLimiter
rate_limiter_;
87
88
core::nanoseconds_t
interval_;
89
90
const
size_t
chunk_length_;
91
const
size_t
num_chunks_;
92
core::Array<float>
chunks_;
93
size_t
first_chunk_num_;
94
size_t
last_chunk_num_;
95
size_t
last_chunk_samples_;
96
97
float
moving_avg_;
98
99
const
SampleSpec
sample_spec_;
100
101
bool
valid_;
102
bool
buffer_full_;
103
};
104
105
}
// namespace audio
106
}
// namespace roc
107
108
#endif
// ROC_AUDIO_PROFILER_H_
array.h
Dynamic array.
roc::audio::Profiler::add_frame
void add_frame(packet::stream_timestamp_t frame_duration, core::nanoseconds_t elapsed)
Profile frame speed.
roc::audio::Profiler::Profiler
Profiler(core::IArena &arena, const SampleSpec &sample_spec, ProfilerConfig profiler_config)
Initialization.
roc::audio::Profiler::is_valid
bool is_valid() const
Check if the profiler was succefully constructed.
roc::audio::Profiler::get_moving_avg
float get_moving_avg()
Get computed average.
roc::audio::SampleSpec
Sample specification. Describes sample rate and channels.
Definition
sample_spec.h:30
roc::core::Array
Dynamic array.
Definition
array.h:40
roc::core::IArena
Memory arena interface.
Definition
iarena.h:23
roc::core::NonCopyable
Base class for non-copyable objects.
Definition
noncopyable.h:23
roc::core::RateLimiter
Rate limiter.
Definition
rate_limiter.h:22
frame.h
Audio frame.
iarena.h
Memory arena interface.
list.h
Intrusive doubly-linked list.
roc::audio
Audio frames and audio processing.
roc::core
General-purpose building blocks and platform abstraction layer.
roc::core::nanoseconds_t
int64_t nanoseconds_t
Nanoseconds.
Definition
time.h:58
roc::packet::stream_timestamp_t
uint32_t stream_timestamp_t
Packet stream timestamp.
Definition
units.h:36
roc
Root namespace.
noncopyable.h
Non-copyable object.
rate_limiter.h
Rate limiter.
sample_spec.h
Sample specifications.
roc::audio::ProfilerConfig
Profiler Configuration Parameters. Controls profiling interval and duration of each circular buffer c...
Definition
profiler.h:30
roc::audio::ProfilerConfig::chunk_duration
core::nanoseconds_t chunk_duration
Duration of samples each chunk can hold in the circular buffer.
Definition
profiler.h:47
roc::audio::ProfilerConfig::ProfilerConfig
ProfilerConfig()
Default Initialization.
Definition
profiler.h:32
roc::audio::ProfilerConfig::ProfilerConfig
ProfilerConfig(core::nanoseconds_t interval, core::nanoseconds_t duration)
Override Initialization.
Definition
profiler.h:38
roc::audio::ProfilerConfig::profiling_interval
core::nanoseconds_t profiling_interval
Rolling window duration and reporting interval.
Definition
profiler.h:44
time.h
Time definitions.
units.h
Various units used in packets.
roc_audio
profiler.h
Generated by
1.17.0