1 /*
2  * Copyright (C) 2010 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 A_RTP_SOURCE_H_
18 
19 #define A_RTP_SOURCE_H_
20 
21 #include <stdint.h>
22 
23 #include <media/stagefright/foundation/ABase.h>
24 #include <utils/List.h>
25 #include <utils/RefBase.h>
26 #include <utils/Thread.h>
27 
28 #include <map>
29 
30 #include "JitterCalculator.h"
31 
32 namespace android {
33 
34 const uint32_t kStaticJitterTimeMs = 100;   // 100ms
35 
36 struct ABuffer;
37 struct AMessage;
38 struct ARTPAssembler;
39 struct ReceptionReportBlock;
40 struct ASessionDescription;
41 
42 struct ARTPSource : public RefBase {
43     ARTPSource(
44             uint32_t id,
45             const sp<ASessionDescription> &sessionDesc, size_t index,
46             const sp<AMessage> &notify);
47 
48     enum {
49         RTP_FIRST_PACKET = 100,
50         RTCP_FIRST_PACKET = 101,
51         RTP_QUALITY = 102,
52         RTP_QUALITY_EMC = 103,
53         RTP_QUALITY_CD = 104,
54         RTCP_SR = 200,
55         RTCP_RR = 201,
56         RTCP_TSFB = 205,
57         RTCP_PSFB = 206,
58         RTP_CVO = 300,
59         RTP_AUTODOWN = 400,
60     };
61 
62     void processRTPPacket(const sp<ABuffer> &buffer);
63     void processRTPPacket();
64     void processReceptionReportBlock(
65             int64_t recvTimeUs, uint32_t senderId, sp<ReceptionReportBlock> rrb);
66     void timeReset();
67     void timeUpdate(int64_t recvTimeUs, uint32_t rtpTime, uint64_t ntpTime);
68     void byeReceived();
69 
queueARTPSource70     List<sp<ABuffer> > *queue() { return &mQueue; }
71 
72     void addReceiverReport(const sp<ABuffer> &buffer);
73     void addFIR(const sp<ABuffer> &buffer);
74     void addTMMBR(const sp<ABuffer> &buffer, int32_t targetBitrate);
75     int addNACK(const sp<ABuffer> &buffer);
76     void setSeqNumToNACK(uint16_t seqNum, uint16_t mask, uint16_t nowJitterHeadSeqNum);
77     uint32_t getSelfID();
78     void setSelfID(const uint32_t selfID);
79     void setPeriodicFIR(bool enable);
80 
81     int32_t getStaticJitterTimeMs();
82     int32_t getBaseJitterTimeMs();
83     int32_t getInterArrivalJitterTimeMs();
84     void setStaticJitterTimeMs(const uint32_t jbTimeMs);
85     void setJbTimer(const sp<AMessage> timer);
86     void setJbAlarmTime(int64_t nowTimeUs, int64_t alarmAfterUs);
87 
88     bool isNeedToEarlyNotify();
89     void notifyPktInfo(int32_t bitrate, int64_t nowUs, bool isRegular);
90     // FIR needs to be sent by missing packet or broken video image.
91     void onIssueFIRByAssembler();
92 
93     void noticeAbandonBuffer(int cnt=1);
94 
95     uint32_t mFirstRtpTime;
96     int64_t mFirstSysTime;
97     int32_t mClockRate;
98 
99     int64_t mSysAnchorTime;
100     int64_t mLastSysAnchorTimeUpdatedUs;
101 
102     int32_t mFirstSsrc;
103     int32_t mHighestNackNumber;
104 
105 private:
106 
107     uint32_t mID;
108     uint32_t mHighestSeqNumber;
109     uint32_t mPrevExpected;
110     uint32_t mBaseSeqNumber;
111     int32_t mNumBuffersReceived;
112     int32_t mPrevNumBuffersReceived;
113     uint32_t mPrevExpectedForRR;
114     int32_t mPrevNumBuffersReceivedForRR;
115 
116     uint32_t mLatestRtpTime;
117 
118     List<sp<ABuffer> > mQueue;
119     sp<ARTPAssembler> mAssembler;
120 
121     int32_t mStaticJbTimeMs;
122     sp<JitterCalc> mJitterCalc;
123     sp<AMessage> mJbTimer;
124 
125     typedef struct infoNACK {
126         uint16_t seqNum;
127         uint16_t mask;
128         uint16_t nowJitterHeadSeqNum;
129         bool    needToNACK;
130     } infoNACK;
131 
132     Mutex mMapLock;
133     std::map<uint16_t, infoNACK> mNACKMap;
134     int getSeqNumToNACK(List<int>& list, int size);
135 
136     uint32_t mLastSrRtpTime;
137     uint64_t mLastSrNtpTime;
138     int64_t mLastSrUpdateTimeUs;
139 
140     int64_t mLastRrUpdateTimeUs;
141 
142     bool mIsFirstRtpRtcpGap;
143     double mAvgRtpRtcpGapMs;
144     double mAvgUnderlineDelayMs;
145     int64_t mLastJbAlarmTimeUs;
146 
147     bool mIssueFIRRequests;
148     bool mIssueFIRByAssembler;
149     int64_t mLastFIRRequestUs;
150     uint8_t mNextFIRSeqNo;
151 
152     sp<AMessage> mNotify;
153 
154     void calcTimeGapRtpRtcp(const sp<ABuffer> &buffer, int64_t nowUs);
155     void calcUnderlineDelay(const sp<ABuffer> &buffer, int64_t nowUs);
156     void adjustAnchorTimeIfRequired(int64_t nowUs);
157 
158     bool queuePacket(const sp<ABuffer> &buffer);
159 
160     DISALLOW_EVIL_CONSTRUCTORS(ARTPSource);
161 };
162 
163 }  // namespace android
164 
165 #endif  // A_RTP_SOURCE_H_
166