1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Mobility Components.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "frequencymonitor.h"
52#include <QDebug>
53#include <QElapsedTimer>
54#include <QString>
55#include <QTime>
56#include <QTimer>
57
58//#define VERBOSE_TRACE
59
60inline QDebug qtTrace() { return qDebug() << "[frequencymonitor]"; }
61#ifdef VERBOSE_TRACE
62inline QDebug qtVerboseTrace() { return qtTrace(); }
63#else
64inline QNoDebug qtVerboseTrace() { return QNoDebug(); }
65#endif
66
67static const int DefaultSamplingInterval = 100;
68static const int DefaultTraceInterval = 0;
69
70class FrequencyMonitorPrivate : public QObject
71{
72 Q_OBJECT
73
74public:
75 FrequencyMonitorPrivate(FrequencyMonitor *parent);
76 void calculateInstantaneousFrequency();
77
78private slots:
79 void calculateAverageFrequency();
80 void stalled();
81
82public:
83 FrequencyMonitor *const q_ptr;
84 QString m_label;
85 bool m_active;
86 qreal m_instantaneousFrequency;
87 QElapsedTimer m_instantaneousElapsed;
88 QTimer *m_averageTimer;
89 QElapsedTimer m_averageElapsed;
90 int m_count;
91 qreal m_averageFrequency;
92 QTimer *m_traceTimer;
93 QTimer *m_stalledTimer;
94};
95
96FrequencyMonitorPrivate::FrequencyMonitorPrivate(FrequencyMonitor *parent)
97: QObject(parent)
98, q_ptr(parent)
99, m_active(false)
100, m_instantaneousFrequency(0)
101, m_averageTimer(new QTimer(this))
102, m_count(0)
103, m_averageFrequency(0)
104, m_traceTimer(new QTimer(this))
105, m_stalledTimer(new QTimer(this))
106{
107 m_instantaneousElapsed.start();
108 connect(sender: m_averageTimer, signal: &QTimer::timeout,
109 receiver: this, slot: &FrequencyMonitorPrivate::calculateAverageFrequency);
110 if (DefaultSamplingInterval)
111 m_averageTimer->start(msec: DefaultSamplingInterval);
112 m_averageElapsed.start();
113 connect(sender: m_traceTimer, signal: &QTimer::timeout,
114 receiver: q_ptr, slot: &FrequencyMonitor::trace);
115 if (DefaultTraceInterval)
116 m_traceTimer->start(msec: DefaultTraceInterval);
117 m_stalledTimer->setSingleShot(true);
118 connect(sender: m_stalledTimer, signal: &QTimer::timeout,
119 receiver: this, slot: &FrequencyMonitorPrivate::stalled);
120}
121
122void FrequencyMonitorPrivate::calculateInstantaneousFrequency()
123{
124 const qint64 ms = m_instantaneousElapsed.restart();
125 m_instantaneousFrequency = ms ? qreal(1000) / ms : 0;
126 m_stalledTimer->start(msec: 3 * ms);
127 if (m_instantaneousFrequency)
128 q_ptr->setActive(true);
129 emit q_ptr->instantaneousFrequencyChanged(value: m_instantaneousFrequency);
130 emit q_ptr->frequencyChanged();
131}
132
133void FrequencyMonitorPrivate::calculateAverageFrequency()
134{
135 const qint64 ms = m_averageElapsed.restart();
136 m_averageFrequency = qreal(m_count * 1000) / ms;
137 emit q_ptr->averageFrequencyChanged(value: m_averageFrequency);
138 emit q_ptr->frequencyChanged();
139 m_count = 0;
140}
141
142void FrequencyMonitorPrivate::stalled()
143{
144 if (m_instantaneousFrequency) {
145 m_instantaneousFrequency = 0;
146 emit q_ptr->instantaneousFrequencyChanged(value: m_instantaneousFrequency);
147 emit q_ptr->frequencyChanged();
148 }
149}
150
151FrequencyMonitor::FrequencyMonitor(QObject *parent)
152: QObject(parent)
153{
154 d_ptr = new FrequencyMonitorPrivate(this);
155}
156
157FrequencyMonitor::~FrequencyMonitor()
158{
159
160}
161
162QString FrequencyMonitor::label() const
163{
164 return d_func()->m_label;
165}
166
167bool FrequencyMonitor::active() const
168{
169 return d_func()->m_active;
170}
171
172int FrequencyMonitor::samplingInterval() const
173{
174 return d_ptr->m_averageTimer->isActive() ? d_ptr->m_averageTimer->interval() : 0;
175}
176
177int FrequencyMonitor::traceInterval() const
178{
179 return d_ptr->m_traceTimer->isActive() ? d_ptr->m_traceTimer->interval() : 0;
180}
181
182qreal FrequencyMonitor::instantaneousFrequency() const
183{
184 return d_func()->m_instantaneousFrequency;
185}
186
187qreal FrequencyMonitor::averageFrequency() const
188{
189 return d_func()->m_averageFrequency;
190}
191
192void FrequencyMonitor::notify()
193{
194 Q_D(FrequencyMonitor);
195 ++(d->m_count);
196 d->calculateInstantaneousFrequency();
197}
198
199void FrequencyMonitor::trace()
200{
201 Q_D(FrequencyMonitor);
202 const QString value = QString::fromLatin1(str: "instant %1 average %2")
203 .arg(a: d->m_instantaneousFrequency, fieldWidth: 0, fmt: 'f', prec: 2)
204 .arg(a: d->m_averageFrequency, fieldWidth: 0, fmt: 'f', prec: 2);
205 if (d->m_label.isEmpty())
206 qtTrace() << "FrequencyMonitor::trace" << value;
207 else
208 qtTrace() << "FrequencyMonitor::trace" << "label" << d->m_label << value;
209}
210
211void FrequencyMonitor::setLabel(const QString &value)
212{
213 Q_D(FrequencyMonitor);
214 if (d->m_label != value) {
215 d->m_label = value;
216 emit labelChanged(value: d->m_label);
217 }
218}
219
220void FrequencyMonitor::setActive(bool value)
221{
222 Q_D(FrequencyMonitor);
223 if (d->m_active != value) {
224 d->m_active = value;
225 emit activeChanged(d->m_active);
226 }
227}
228
229void FrequencyMonitor::setSamplingInterval(int value)
230{
231 Q_D(FrequencyMonitor);
232 if (samplingInterval() != value) {
233 if (value) {
234 d->m_averageTimer->setInterval(value);
235 d->m_averageTimer->start();
236 } else {
237 d->m_averageTimer->stop();
238 }
239 emit samplingIntervalChanged(value);
240 }
241}
242
243void FrequencyMonitor::setTraceInterval(int value)
244{
245 Q_D(FrequencyMonitor);
246 if (traceInterval() != value) {
247 if (value) {
248 d->m_traceTimer->setInterval(value);
249 d->m_traceTimer->start();
250 } else {
251 d->m_traceTimer->stop();
252 }
253 emit traceIntervalChanged(value);
254 }
255}
256
257#include "frequencymonitor.moc"
258

source code of qtmultimedia/examples/multimedia/video/snippets/frequencymonitor/frequencymonitor.cpp