changed: update version strings for beta4
[xbmc.git] / xbmc / utils / BitstreamStats.cpp
blobdcc131a5a7900af4470e893289f967a86fa82d07
1 /*
2 * Copyright (C) 2005-2008 Team XBMC
3 * http://www.xbmc.org
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 #include "BitstreamStats.h"
23 #include "utils/TimeUtils.h"
25 int64_t BitstreamStats::m_tmFreq;
27 BitstreamStats::BitstreamStats(unsigned int nEstimatedBitrate)
29 m_dBitrate = 0.0;
30 m_dMaxBitrate = 0.0;
31 m_dMinBitrate = -1.0;
33 m_nBitCount = 0;
34 m_nEstimatedBitrate = nEstimatedBitrate;
35 m_tmStart = 0LL;
37 if (m_tmFreq == 0LL)
38 m_tmFreq = CurrentHostFrequency();
41 BitstreamStats::~BitstreamStats()
45 void BitstreamStats::AddSampleBytes(unsigned int nBytes)
47 AddSampleBits(nBytes*8);
50 void BitstreamStats::AddSampleBits(unsigned int nBits)
52 m_nBitCount += nBits;
53 if (m_nBitCount >= m_nEstimatedBitrate)
54 CalculateBitrate();
57 void BitstreamStats::Start()
59 m_nBitCount = 0;
60 m_tmStart = CurrentHostCounter();
63 void BitstreamStats::CalculateBitrate()
65 int64_t tmNow;
66 tmNow = CurrentHostCounter();
68 double elapsed = (double)(tmNow - m_tmStart) / (double)m_tmFreq;
69 // only update once every 2 seconds
70 if (elapsed >= 2)
72 m_dBitrate = (double)m_nBitCount / elapsed;
74 if (m_dBitrate > m_dMaxBitrate)
75 m_dMaxBitrate = m_dBitrate;
77 if (m_dBitrate < m_dMinBitrate || m_dMinBitrate == -1)
78 m_dMinBitrate = m_dBitrate;
80 Start();