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 #include <JitterBufferControlNode.h>
18 #include <AudioJitterBuffer.h>
19 #include <VideoJitterBuffer.h>
20 #include <TextJitterBuffer.h>
21 #include <ImsMediaTimer.h>
22 
JitterBufferControlNode(BaseSessionCallback * callback,ImsMediaType type)23 JitterBufferControlNode::JitterBufferControlNode(BaseSessionCallback* callback, ImsMediaType type) :
24         BaseNode(callback),
25         mMediaType(type)
26 {
27     if (mMediaType == IMS_MEDIA_AUDIO)
28     {
29         mJitterBuffer = new AudioJitterBuffer();
30     }
31     else if (mMediaType == IMS_MEDIA_VIDEO)
32     {
33         mJitterBuffer = new VideoJitterBuffer();
34     }
35     else if (mMediaType == IMS_MEDIA_TEXT)
36     {
37         mJitterBuffer = new TextJitterBuffer();
38     }
39 
40     mJitterBuffer->SetSessionCallback(mCallback);
41 }
42 
~JitterBufferControlNode()43 JitterBufferControlNode::~JitterBufferControlNode()
44 {
45     if (mJitterBuffer != nullptr)
46     {
47         delete mJitterBuffer;
48     }
49 }
50 
SetJitterBufferSize(uint32_t nInit,uint32_t nMin,uint32_t nMax)51 void JitterBufferControlNode::SetJitterBufferSize(uint32_t nInit, uint32_t nMin, uint32_t nMax)
52 {
53     if (mJitterBuffer)
54     {
55         mJitterBuffer->SetJitterBufferSize(nInit, nMin, nMax);
56     }
57 }
58 
Reset()59 void JitterBufferControlNode::Reset()
60 {
61     if (mJitterBuffer)
62     {
63         mJitterBuffer->ClearBuffer();
64         mJitterBuffer->Reset();
65     }
66 }
67 
GetDataCount()68 uint32_t JitterBufferControlNode::GetDataCount()
69 {
70     if (mJitterBuffer)
71     {
72         return mJitterBuffer->GetCount();
73     }
74 
75     return 0;
76 }
77 
OnDataFromFrontNode(ImsMediaSubType subtype,uint8_t * pData,uint32_t nDataSize,uint32_t nTimestamp,bool bMark,uint32_t nSeqNum,ImsMediaSubType nDataType,uint32_t arrivalTime)78 void JitterBufferControlNode::OnDataFromFrontNode(ImsMediaSubType subtype, uint8_t* pData,
79         uint32_t nDataSize, uint32_t nTimestamp, bool bMark, uint32_t nSeqNum,
80         ImsMediaSubType nDataType, uint32_t arrivalTime)
81 {
82     if (mJitterBuffer)
83     {
84         mJitterBuffer->Add(
85                 subtype, pData, nDataSize, nTimestamp, bMark, nSeqNum, nDataType, arrivalTime);
86     }
87 }
88 
GetData(ImsMediaSubType * pSubtype,uint8_t ** ppData,uint32_t * pnDataSize,uint32_t * pnTimestamp,bool * pbMark,uint32_t * pnSeqNum,ImsMediaSubType * pnDataType,uint32_t * arrivalTime)89 bool JitterBufferControlNode::GetData(ImsMediaSubType* pSubtype, uint8_t** ppData,
90         uint32_t* pnDataSize, uint32_t* pnTimestamp, bool* pbMark, uint32_t* pnSeqNum,
91         ImsMediaSubType* pnDataType, uint32_t* arrivalTime)
92 {
93     if (arrivalTime)
94     {
95         *arrivalTime = 0;
96     }
97 
98     if (pnDataType)
99     {
100         *pnDataType = MEDIASUBTYPE_UNDEFINED;
101     }
102 
103     if (mJitterBuffer)
104     {
105         return mJitterBuffer->Get(pSubtype, ppData, pnDataSize, pnTimestamp, pbMark, pnSeqNum,
106                 ImsMediaTimer::GetTimeInMilliSeconds(), pnDataType);
107     }
108 
109     return false;
110 }
111 
GetRedundantFrame(uint32_t lostSeq,uint8_t ** ppData,uint32_t * pnDataSize,bool * hasNextFrame,uint8_t * nextFrameFirstByte)112 bool JitterBufferControlNode::GetRedundantFrame(uint32_t lostSeq, uint8_t** ppData,
113         uint32_t* pnDataSize, bool* hasNextFrame, uint8_t* nextFrameFirstByte)
114 {
115     if (mJitterBuffer)
116     {
117         return mJitterBuffer->GetRedundantFrame(
118                 lostSeq, ppData, pnDataSize, hasNextFrame, nextFrameFirstByte);
119     }
120 
121     return false;
122 }
123 
DeleteData()124 void JitterBufferControlNode::DeleteData()
125 {
126     if (mJitterBuffer)
127     {
128         mJitterBuffer->Delete();
129     }
130 }