1 /**
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef VIDEO_JITTER_BUFFER_H_INCLUDEED
18 #define VIDEO_JITTER_BUFFER_H_INCLUDEED
19 
20 #include <ImsMediaDefine.h>
21 #include <BaseJitterBuffer.h>
22 #include <ImsMediaVideoUtil.h>
23 #include <ImsMediaTimer.h>
24 #include <mutex>
25 #include <list>
26 
27 class VideoJitterBuffer : public BaseJitterBuffer
28 {
29 public:
30     VideoJitterBuffer();
31     virtual ~VideoJitterBuffer();
32 
33     /**
34      * @brief Set the Jitter Buffer Size
35      *
36      * @param nInit initial size of jitter buffer in milliseconds unit
37      * @param nMin minimum size of jitter buffer in milliseconds unit
38      * @param nMax maximum size of jitter buffer in milliseconds unit
39      */
40     virtual void SetJitterBufferSize(uint32_t nInit, uint32_t nMin, uint32_t nMax);
41     virtual uint32_t GetCount();
42     virtual void Reset();
43     virtual void Delete();
44     virtual void Add(ImsMediaSubType subtype, uint8_t* pbBuffer, uint32_t nBufferSize,
45             uint32_t nTimeStamp, bool mark, uint32_t nSeqNum, ImsMediaSubType nDataType,
46             uint32_t arrivalTime);
47     virtual bool Get(ImsMediaSubType* psubtype, uint8_t** ppData, uint32_t* pnDataSize,
48             uint32_t* ptimestamp, bool* pmark, uint32_t* pnSeqNum, uint32_t currentTime,
49             ImsMediaSubType* pDataType = nullptr);
50 
51     /**
52      * @brief Set the video codec type
53      *
54      * @param type The video codec type defined in ImsMediaDefine.h
55      */
56     void SetCodecType(uint32_t type);
57 
58     /**
59      * @brief Set the video framerate
60      */
61     void SetFramerate(uint32_t framerate);
62 
63     /**
64      * @brief Set the response wait time. A sender should ignore FIR messages that arrive within
65      * Response Wait Time (RWT) duration after responding to a previous FIR message. Response Wait
66      * Time (RWT) is defined as RTP-level round-trip time, estimated by RTCP or some other means,
67      * plus twice the frame duration.
68      *
69      * @param time The response wait time in milliseconds unit
70      */
71     void SetResponseWaitTime(const uint32_t time);
72 
73     /**
74      * @brief Start the packet loss monitoring timer to check the packet loss rate
75      *
76      * @param time The time duration of seconds unit to monitor the packet loss
77      * @param rate The packet loss rate in the monitoring duration range
78      */
79     void StartTimer(uint32_t time, uint32_t rate);
80 
81     /**
82      * @brief Stop the packet loss monitoring timer
83      */
84     void StopTimer();
85 
86 private:
87     bool CheckHeader(uint8_t* pbBuffer);
88     void CheckValidIDR(DataEntry* pIDREntry);
89     void InitLostPktList();
90     void RemovePacketFromLostList(uint16_t seqNum, bool bRemOldPkt = false);
91     void CheckPacketLoss(uint16_t seqNum, uint16_t nLastRecvPkt);
92     bool UpdateLostPacketList(uint16_t mLossRateThreshold, uint16_t* countSecondNack,
93             uint16_t* nPLIPkt, bool* bPLIPkt);
94     bool UpdateNackStatus(LostPacket* pTempEntry, uint16_t mLossRateThreshold,
95             uint16_t* countSecondNack, uint16_t* nPLIPkt, bool* bPLIPkt);
96     void RequestSendNack(
97             uint16_t nLossGap, uint16_t PID, uint16_t countSecondNack, bool bNACK = true);
98     void RequestToSendPictureLost(uint32_t eType);
99     void RequestToSendTmmbr(uint32_t bitrate);
100     static void OnTimer(hTimerHandler hTimer, void* pUserData);
101     void ProcessTimer();
102     void CheckBitrateAdaptation(double lossRate);
103 
104 private:
105     bool mNewInputData;
106     uint32_t mFramerate;
107     uint32_t mFrameInterval;
108     uint32_t mMaxSaveFrameNum;
109     uint32_t mSavedFrameNum;
110     uint32_t mMarkedFrameNum;
111     uint32_t mLastPlayedTime;
112     uint32_t mNumAddedPacket;
113     uint32_t mNumLossPacket;
114     uint64_t mAccumulatedPacketSize;
115     uint32_t mLastAddedTimestamp;
116     uint32_t mLastAddedSeqNum;
117     uint32_t mResponseWaitTime;
118     std::list<LostPacket*> mLostPktList;
119     uint32_t mIDRCheckCnt;
120     uint32_t mFirTimeStamp;
121     uint32_t mMaxBitrate;
122     uint32_t mRequestedBitrate;
123     uint32_t mIncomingBitrate;
124     uint32_t mLossDuration;
125     uint32_t mLossRateThreshold;
126     uint32_t mCountTimerExpired;
127     hTimerHandler mTimer;
128     std::mutex mMutexTimer;
129 };
130 #endif
131