1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_TMMBR_HELP_H_
12#define WEBRTC_MODULES_RTP_RTCP_SOURCE_TMMBR_HELP_H_
13
14#include <vector>
15
16#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
17#include "webrtc/typedefs.h"
18
19namespace webrtc {
20class TMMBRSet
21{
22public:
23    TMMBRSet();
24    ~TMMBRSet();
25
26    void VerifyAndAllocateSet(uint32_t minimumSize);
27    void VerifyAndAllocateSetKeepingData(uint32_t minimumSize);
28    // Number of valid data items in set.
29    uint32_t lengthOfSet() const { return _lengthOfSet; }
30    // Presently allocated max size of set.
31    uint32_t sizeOfSet() const { return _sizeOfSet; }
32    void clearSet() {
33      _lengthOfSet = 0;
34    }
35    uint32_t Tmmbr(int i) const {
36      return _data.at(i).tmmbr;
37    }
38    uint32_t PacketOH(int i) const {
39      return _data.at(i).packet_oh;
40    }
41    uint32_t Ssrc(int i) const {
42      return _data.at(i).ssrc;
43    }
44    void SetEntry(unsigned int i,
45                  uint32_t tmmbrSet,
46                  uint32_t packetOHSet,
47                  uint32_t ssrcSet);
48
49    void AddEntry(uint32_t tmmbrSet,
50                  uint32_t packetOHSet,
51                  uint32_t ssrcSet);
52
53    // Remove one entry from table, and move all others down.
54    void RemoveEntry(uint32_t sourceIdx);
55
56    void SwapEntries(uint32_t firstIdx,
57                     uint32_t secondIdx);
58
59    // Set entry data to zero, but keep it in table.
60    void ClearEntry(uint32_t idx);
61
62 private:
63    class SetElement {
64      public:
65        SetElement() : tmmbr(0), packet_oh(0), ssrc(0) {}
66        uint32_t tmmbr;
67        uint32_t packet_oh;
68        uint32_t ssrc;
69    };
70
71    std::vector<SetElement> _data;
72    // Number of places allocated.
73    uint32_t    _sizeOfSet;
74    // NUmber of places currently in use.
75    uint32_t    _lengthOfSet;
76};
77
78class TMMBRHelp
79{
80public:
81    TMMBRHelp();
82    virtual ~TMMBRHelp();
83
84    TMMBRSet* BoundingSet(); // used for debuging
85    TMMBRSet* CandidateSet();
86    TMMBRSet* BoundingSetToSend();
87
88    TMMBRSet* VerifyAndAllocateCandidateSet(const uint32_t minimumSize);
89    int32_t FindTMMBRBoundingSet(TMMBRSet*& boundingSet);
90    int32_t SetTMMBRBoundingSetToSend(
91        const TMMBRSet* boundingSetToSend,
92        const uint32_t maxBitrateKbit);
93
94    bool IsOwner(const uint32_t ssrc, const uint32_t length) const;
95
96    bool CalcMinBitRate(uint32_t* minBitrateKbit) const;
97
98protected:
99    TMMBRSet*   VerifyAndAllocateBoundingSet(uint32_t minimumSize);
100    int32_t VerifyAndAllocateBoundingSetToSend(uint32_t minimumSize);
101
102    int32_t FindTMMBRBoundingSet(int32_t numCandidates, TMMBRSet& candidateSet);
103
104private:
105    CriticalSectionWrapper* _criticalSection;
106    TMMBRSet                _candidateSet;
107    TMMBRSet                _boundingSet;
108    TMMBRSet                _boundingSetToSend;
109
110    float*                  _ptrIntersectionBoundingSet;
111    float*                  _ptrMaxPRBoundingSet;
112};
113}  // namespace webrtc
114
115#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_TMMBR_HELP_H_
116