1
0
mirror of https://github.com/rofafor/vdr-plugin-iptv.git synced 2023-10-10 13:37:03 +02:00
vdr-plugin-iptv/statistics.c

220 lines
6.7 KiB
C
Raw Normal View History

2007-10-05 21:00:44 +02:00
/*
* statistics.c: IPTV plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#include <limits.h>
2007-10-05 21:00:44 +02:00
#include "common.h"
#include "statistics.h"
#include "config.h"
2007-10-05 21:00:44 +02:00
2013-02-23 16:12:46 +01:00
// Section statistics class
2007-10-05 21:00:44 +02:00
cIptvSectionStatistics::cIptvSectionStatistics()
2013-02-23 14:31:11 +01:00
: filteredDataM(0),
numberOfCallsM(0),
timerM(),
mutexM()
2007-10-05 21:00:44 +02:00
{
2013-02-23 16:12:46 +01:00
//debug("cIptvSectionStatistics::%s()", __FUNCTION__);
2007-10-05 21:00:44 +02:00
}
cIptvSectionStatistics::~cIptvSectionStatistics()
{
2013-02-23 16:12:46 +01:00
//debug("cIptvSectionStatistics::%s()", __FUNCTION__);
2007-10-05 21:00:44 +02:00
}
cString cIptvSectionStatistics::GetSectionStatistic()
2007-10-05 21:00:44 +02:00
{
2013-02-23 16:12:46 +01:00
//debug("cIptvSectionStatistics::%s()", __FUNCTION__);
2013-02-23 14:31:11 +01:00
cMutexLock MutexLock(&mutexM);
uint64_t elapsed = timerM.Elapsed(); /* in milliseconds */
timerM.Set();
long bitrate = elapsed ? (long)(1000.0L * filteredDataM / KILOBYTE(1) / elapsed) : 0L;
if (!IptvConfig.GetUseBytes())
bitrate *= 8;
// no trailing linefeed here!
2013-02-23 14:31:11 +01:00
cString s = cString::sprintf("%4ld (%4ld k%s/s)", numberOfCallsM, bitrate,
IptvConfig.GetUseBytes() ? "B" : "bit");
2013-02-23 14:31:11 +01:00
filteredDataM = numberOfCallsM = 0;
return s;
2007-10-05 21:00:44 +02:00
}
2013-02-23 14:31:11 +01:00
void cIptvSectionStatistics::AddSectionStatistic(long bytesP, long callsP)
2007-10-08 18:24:48 +02:00
{
//debug("cIptvSectionStatistics::%s(%ld, %ld)", __FUNCTION__, bytesP, callsP);
2013-02-23 14:31:11 +01:00
cMutexLock MutexLock(&mutexM);
filteredDataM += bytesP;
numberOfCallsM += callsP;
2007-10-08 18:24:48 +02:00
}
2007-10-10 00:12:17 +02:00
// --- cIptvPidStatistics ----------------------------------------------------
2013-02-23 16:12:46 +01:00
// Device statistics class
2007-10-10 00:12:17 +02:00
cIptvPidStatistics::cIptvPidStatistics()
2013-02-23 14:31:11 +01:00
: timerM(),
mutexM()
2007-10-05 21:00:44 +02:00
{
2013-02-23 16:12:46 +01:00
debug("cIptvPidStatistics::%s()", __FUNCTION__);
2014-02-19 20:38:11 +01:00
const int numberOfElements = sizeof(mostActivePidsM) / sizeof(pidStruct);
for (int i = 0; i < numberOfElements; ++i) {
mostActivePidsM[i].pid = -1;
mostActivePidsM[i].dataAmount = 0L;
}
2007-10-05 21:00:44 +02:00
}
2007-10-10 00:12:17 +02:00
cIptvPidStatistics::~cIptvPidStatistics()
2007-10-05 21:00:44 +02:00
{
2013-02-23 16:12:46 +01:00
debug("cIptvPidStatistics::%s()", __FUNCTION__);
2007-10-05 21:00:44 +02:00
}
cString cIptvPidStatistics::GetPidStatistic()
2007-10-05 21:00:44 +02:00
{
2013-02-23 16:12:46 +01:00
//debug("cIptvPidStatistics::%s()", __FUNCTION__);
2013-02-23 14:31:11 +01:00
cMutexLock MutexLock(&mutexM);
2014-02-19 20:38:11 +01:00
const int numberOfElements = sizeof(mostActivePidsM) / sizeof(pidStruct);
2013-02-23 14:31:11 +01:00
uint64_t elapsed = timerM.Elapsed(); /* in milliseconds */
timerM.Set();
cString s("Active pids:\n");
2014-02-19 20:38:11 +01:00
for (int i = 0; i < numberOfElements; ++i) {
if (mostActivePidsM[i].pid >= 0) {
long bitrate = elapsed ? (long)(1000.0L * mostActivePidsM[i].dataAmount / KILOBYTE(1) / elapsed) : 0L;
if (!IptvConfig.GetUseBytes())
bitrate *= 8;
s = cString::sprintf("%sPid %d: %4d (%4ld k%s/s)\n", *s, i,
2013-02-23 14:31:11 +01:00
mostActivePidsM[i].pid, bitrate,
IptvConfig.GetUseBytes() ? "B" : "bit");
}
2007-10-08 00:54:09 +02:00
}
2014-02-19 20:38:11 +01:00
for (int i = 0; i < numberOfElements; ++i) {
mostActivePidsM[i].pid = -1;
mostActivePidsM[i].dataAmount = 0L;
}
return s;
2007-10-05 21:00:44 +02:00
}
2013-02-23 14:31:11 +01:00
int cIptvPidStatistics::SortPids(const void* data1P, const void* data2P)
2007-10-05 21:00:44 +02:00
{
2013-02-23 16:12:46 +01:00
//debug("cIptvPidStatistics::%s()", __FUNCTION__);
2013-02-23 14:31:11 +01:00
const pidStruct *comp1 = reinterpret_cast<const pidStruct*>(data1P);
const pidStruct *comp2 = reinterpret_cast<const pidStruct*>(data2P);
2014-02-19 20:38:11 +01:00
if (comp1->dataAmount > comp2->dataAmount)
2007-10-05 21:00:44 +02:00
return -1;
2014-02-19 20:38:11 +01:00
if (comp1->dataAmount < comp2->dataAmount)
2007-10-07 10:46:34 +02:00
return 1;
2007-10-05 21:00:44 +02:00
return 0;
}
2014-02-19 20:38:11 +01:00
void cIptvPidStatistics::AddPidStatistic(int pidP, long payloadP)
2007-10-05 21:00:44 +02:00
{
2013-02-23 16:12:46 +01:00
//debug("cIptvPidStatistics::%s(%ld, %ld)", __FUNCTION__, pidP, payloadP);
2013-02-23 14:31:11 +01:00
cMutexLock MutexLock(&mutexM);
const int numberOfElements = sizeof(mostActivePidsM) / sizeof(pidStruct);
2007-10-05 21:00:44 +02:00
// If our statistic already is in the array, update it and quit
for (int i = 0; i < numberOfElements; ++i) {
2013-02-23 14:31:11 +01:00
if (mostActivePidsM[i].pid == pidP) {
2014-02-19 20:38:11 +01:00
mostActivePidsM[i].dataAmount += payloadP;
// Now re-sort the array and quit
2013-02-23 14:31:11 +01:00
qsort(mostActivePidsM, numberOfElements, sizeof(pidStruct), SortPids);
return;
}
}
2007-10-05 21:00:44 +02:00
// Apparently our pid isn't in the array. Replace the last element with this
// one if new payload is greater
2014-02-19 20:38:11 +01:00
if (mostActivePidsM[numberOfElements - 1].dataAmount < payloadP) {
2013-02-23 14:31:11 +01:00
mostActivePidsM[numberOfElements - 1].pid = pidP;
2014-02-19 20:38:11 +01:00
mostActivePidsM[numberOfElements - 1].dataAmount = payloadP;
// Re-sort
2013-02-23 14:31:11 +01:00
qsort(mostActivePidsM, numberOfElements, sizeof(pidStruct), SortPids);
}
2007-10-05 21:00:44 +02:00
}
// --- cIptvStreamerStatistics -----------------------------------------------
2007-10-05 21:00:44 +02:00
2013-02-23 16:12:46 +01:00
// Streamer statistics class
2007-10-05 21:00:44 +02:00
cIptvStreamerStatistics::cIptvStreamerStatistics()
2013-02-23 14:31:11 +01:00
: dataBytesM(0),
timerM(),
mutexM()
2007-10-05 21:00:44 +02:00
{
2013-02-23 16:12:46 +01:00
debug("cIptvStreamerStatistics::%s()", __FUNCTION__);
2007-10-05 21:00:44 +02:00
}
cIptvStreamerStatistics::~cIptvStreamerStatistics()
{
2013-02-23 16:12:46 +01:00
debug("cIptvStreamerStatistics::%s()", __FUNCTION__);
2007-10-05 21:00:44 +02:00
}
cString cIptvStreamerStatistics::GetStreamerStatistic()
2007-10-05 21:00:44 +02:00
{
2013-02-23 16:12:46 +01:00
//debug("cIptvStreamerStatistics::%s()", __FUNCTION__);
2013-02-23 14:31:11 +01:00
cMutexLock MutexLock(&mutexM);
uint64_t elapsed = timerM.Elapsed(); /* in milliseconds */
timerM.Set();
long bitrate = elapsed ? (long)(1000.0L * dataBytesM / KILOBYTE(1) / elapsed) : 0L;
if (!IptvConfig.GetUseBytes())
bitrate *= 8;
cString s = cString::sprintf("%ld k%s/s", bitrate, IptvConfig.GetUseBytes() ? "B" : "bit");
2013-02-23 14:31:11 +01:00
dataBytesM = 0;
return s;
2007-10-08 18:24:48 +02:00
}
2013-02-23 14:31:11 +01:00
void cIptvStreamerStatistics::AddStreamerStatistic(long bytesP)
2007-10-08 18:24:48 +02:00
{
2013-02-23 16:12:46 +01:00
//debug("cIptvStreamerStatistics::%s(%ld)", __FUNCTION__, bytesP);
2013-02-23 14:31:11 +01:00
cMutexLock MutexLock(&mutexM);
dataBytesM += bytesP;
2007-10-05 21:00:44 +02:00
}
2007-10-09 19:58:17 +02:00
2013-02-23 16:12:46 +01:00
// Buffer statistics class
2007-10-09 19:58:17 +02:00
cIptvBufferStatistics::cIptvBufferStatistics()
2013-02-23 14:31:11 +01:00
: dataBytesM(0),
freeSpaceM(0),
usedSpaceM(0),
timerM(),
mutexM()
2007-10-09 19:58:17 +02:00
{
2013-02-23 16:12:46 +01:00
debug("cIptvBufferStatistics::%s()", __FUNCTION__);
2007-10-09 19:58:17 +02:00
}
cIptvBufferStatistics::~cIptvBufferStatistics()
{
2013-02-23 16:12:46 +01:00
debug("cIptvBufferStatistics::%s()", __FUNCTION__);
2007-10-09 19:58:17 +02:00
}
cString cIptvBufferStatistics::GetBufferStatistic()
2007-10-09 19:58:17 +02:00
{
2013-02-23 16:12:46 +01:00
//debug("cIptvBufferStatistics::%s()", __FUNCTION__);
2013-02-23 14:31:11 +01:00
cMutexLock MutexLock(&mutexM);
uint64_t elapsed = timerM.Elapsed(); /* in milliseconds */
timerM.Set();
long bitrate = elapsed ? (long)(1000.0L * dataBytesM / KILOBYTE(1) / elapsed) : 0L;
long totalSpace = IPTV_BUFFER_SIZE;
2013-02-23 14:31:11 +01:00
float percentage = (float)((float)usedSpaceM / (float)totalSpace * 100.0);
long totalKilos = totalSpace / KILOBYTE(1);
2013-02-23 14:31:11 +01:00
long usedKilos = usedSpaceM / KILOBYTE(1);
2007-10-09 19:58:17 +02:00
if (!IptvConfig.GetUseBytes()) {
2007-10-10 00:12:17 +02:00
bitrate *= 8;
totalKilos *= 8;
2007-10-09 19:58:17 +02:00
usedKilos *= 8;
}
cString s = cString::sprintf("Buffer bitrate: %ld k%s/s\nBuffer usage: %ld/%ld k%s (%2.1f%%)\n", bitrate,
IptvConfig.GetUseBytes() ? "B" : "bit", usedKilos, totalKilos,
IptvConfig.GetUseBytes() ? "B" : "bit", percentage);
2013-02-23 14:31:11 +01:00
dataBytesM = 0;
usedSpaceM = 0;
return s;
2007-10-09 19:58:17 +02:00
}
2013-02-23 14:31:11 +01:00
void cIptvBufferStatistics::AddBufferStatistic(long bytesP, long usedP)
2007-10-09 19:58:17 +02:00
{
2013-02-23 16:12:46 +01:00
//debug("cIptvBufferStatistics::%s(%ld, %ld)", __FUNCTION__, bytesP, usedP);
2013-02-23 14:31:11 +01:00
cMutexLock MutexLock(&mutexM);
dataBytesM += bytesP;
if (usedP > usedSpaceM)
usedSpaceM = usedP;
2007-10-09 19:58:17 +02:00
}