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