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 AUDIO_JITTER_BUFFER_INCLUDED
18 #define AUDIO_JITTER_BUFFER_INCLUDED
19 
20 #include <BaseJitterBuffer.h>
21 #include <JitterNetworkAnalyser.h>
22 
23 class AudioJitterBuffer : public BaseJitterBuffer
24 {
25 public:
26     AudioJitterBuffer();
27     virtual ~AudioJitterBuffer();
28     virtual void Reset();
29     virtual void ClearBuffer();
30     virtual void SetJitterBufferSize(uint32_t nInit, uint32_t nMin, uint32_t nMax);
31     virtual void Add(ImsMediaSubType subtype, uint8_t* pbBuffer, uint32_t nBufferSize,
32             uint32_t nTimestamp, bool bMark, uint32_t nSeqNum,
33             ImsMediaSubType nDataType = ImsMediaSubType::MEDIASUBTYPE_UNDEFINED,
34             uint32_t arrivalTime = 0);
35     virtual bool Get(ImsMediaSubType* psubtype, uint8_t** ppData, uint32_t* pnDataSize,
36             uint32_t* pnTimestamp, bool* pbMark, uint32_t* pnSeqNum, uint32_t currentTime,
37             ImsMediaSubType* pDataType = nullptr);
38 
39     /**
40      * @brief Set the jitter network analyzer option parameters
41      *
42      * @param incThreshold The threshold of time difference to increase the jitter buffer size
43      * @param decThreshold The threshold of time difference to decrease the jitter buffer size
44      * @param stepSize The size how many steps to decrease the jitter buffer size
45      * @param weight The weight to calculate margin to the jitter buffer size
46      */
47     void SetJitterOptions(
48             uint32_t incThreshold, uint32_t decThreshold, uint32_t stepSize, double weight);
49 
50     /**
51      * @brief Set the offset of the sequence number for extracting the redundant frame for EVS
52      *
53      * @param offset The offset value of sequence number to find redundant frame
54      */
55     void SetEvsRedundantFrameOffset(const int32_t offset);
56     /* set the start time in ms unit */
SetStartTime(uint32_t time)57     void SetStartTime(uint32_t time) { mTimeStarted = time; }
GetCurrentSize()58     uint32_t GetCurrentSize() { return mCurrJitterBufferSize; }
59     double GetMeanBufferSize();
60     /**
61      * @brief For EVS, get the redundant frame for lost packet
62      *
63      * @param lostSeq The sequence number of lost Rtp packet
64      * @param ppData full payload of redundant frame if found
65      * @param pnDataSize payload size in bytes of redundant frame if found
66      * @param hasNextFrame set to true if next to lost frame is found only if
67               redundant frame is found
68      * @param nextFrameFirstByte first byte of next to lost frame only if
69               redundant frame is found
70      * @return true when redundant frame is found
71      * @return false when redundant frame is not found
72      */
73     bool GetRedundantFrame(uint32_t lostSeq, uint8_t** ppData, uint32_t* pnDataSize,
74             bool* hasNextFrame, uint8_t* nextFrameFirstByte);
75 
76     /**
77      * @brief Adjust buffering delay by adding the additional delay to synchronize the audio with
78      *        the video frames
79      *
80      * @param delayMs The delay to add
81      */
82     void SetAdditionalDelay(const int32_t delayMs);
83 
84 private:
85     void Resync(uint32_t spareFrames);
86     void CountLostFrames(int32_t currentSeq, int32_t lastSeq);
87     uint32_t GetDropVoiceRateInDuration(uint32_t duration, uint32_t currentTime);
88     void CollectRxRtpStatus(int32_t seq, kRtpPacketStatus status);
89     void CollectJitterBufferStatus(int32_t currSize, int32_t maxSize);
90     bool GetPartialRedundancyFrame(const uint32_t currentSeq, uint32_t currentTimestamp,
91             const uint32_t offset, DataEntry** entry);
92     bool GetNextFrameFirstByte(uint32_t nextSeq, uint8_t* nextFrameFirstByte);
93 
94     JitterNetworkAnalyser mJitterAnalyzer;
95     bool mDtxPlayed;
96     bool mDtxReceived;
97     bool mWaiting;
98     int32_t mUpdatedDelay;
99     uint32_t mCurrPlayingTS;
100     uint32_t mCheckUpdateJitterPacketCnt;
101     uint32_t mCurrJitterBufferSize;
102     uint32_t mNextJitterBufferSize;
103     uint32_t mTimeStarted;
104     std::list<uint32_t> mListJitterBufferSize;
105     std::list<uint32_t> mListVoiceFrames;
106     std::list<uint32_t> mListDropVoiceFrames;
107     DataEntry* mPreservedDtx;
108     int32_t mEvsRedundantFrameOffset;
109     uint32_t mPrevGetTime;
110     uint32_t mAdditionalDelay;
111 };
112 
113 #endif