2 * Copyright (C) 2005-2008 Team XBMC
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)
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
)
34 m_nEstimatedBitrate
= nEstimatedBitrate
;
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
)
53 if (m_nBitCount
>= m_nEstimatedBitrate
)
57 void BitstreamStats::Start()
60 m_tmStart
= CurrentHostCounter();
63 void BitstreamStats::CalculateBitrate()
66 tmNow
= CurrentHostCounter();
68 double elapsed
= (double)(tmNow
- m_tmStart
) / (double)m_tmFreq
;
69 // only update once every 2 seconds
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
;