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_CONNECTION_H_
18 
19 #define A_RTP_CONNECTION_H_
20 
21 #include <media/stagefright/foundation/AHandler.h>
22 #include <utils/List.h>
23 #include <sys/socket.h>
24 
25 namespace android {
26 
27 struct ABuffer;
28 struct ARTPSource;
29 struct ASessionDescription;
30 
31 struct ARTPConnection : public AHandler {
32     enum Flags {
33         kRegularlyRequestFIR = 2,
34         kViLTEConnection = 4,
35     };
36 
37     explicit ARTPConnection(uint32_t flags = 0);
38 
39     void addStream(
40             int rtpSocket, int rtcpSocket,
41             const sp<ASessionDescription> &sessionDesc, size_t index,
42             const sp<AMessage> &notify,
43             bool injected);
44     void seekStream();
45     void removeStream(int rtpSocket, int rtcpSocket);
46 
47     void injectPacket(int index, const sp<ABuffer> &buffer);
48 
49     void setSelfID(const uint32_t selfID);
50     void setStaticJitterTimeMs(const uint32_t jbTimeMs);
51     void setTargetBitrate(int32_t targetBitrate);
52     void setRtpSockOptEcn(int32_t sockOptEcn);
53     void setIsIPv6(const char *localIp);
54 
55     // Creates a pair of UDP datagram sockets bound to adjacent ports
56     // (the rtpSocket is bound to an even port, the rtcpSocket to the
57     // next higher port).
58     static void MakePortPair(
59             int *rtpSocket, int *rtcpSocket, unsigned *rtpPort);
60     // Creates a pair of UDP datagram sockets bound to assigned ip and
61     // ports (the rtpSocket is bound to an even port, the rtcpSocket
62     // to the next higher port).
63     static void MakeRTPSocketPair(
64             int *rtpSocket, int *rtcpSocket,
65             const char *localIp, const char *remoteIp,
66             unsigned localPort, unsigned remotePort, int64_t socketNetwork = 0,
67             int32_t sockOptEcn = 0);
68 
69 protected:
70     virtual ~ARTPConnection();
71     virtual void onMessageReceived(const sp<AMessage> &msg);
72 
73 private:
74     enum {
75         kWhatAddStream,
76         kWhatSeekStream,
77         kWhatRemoveStream,
78         kWhatPollStreams,
79         kWhatInjectPacket,
80         kWhatAlarmStream,
81     };
82 
83     static const int64_t kSelectTimeoutUs;
84     static const int64_t kMinOneSecondNotifyDelayUs;
85 
86     uint32_t mFlags;
87 
88     struct StreamInfo;
89     List<StreamInfo> mStreams;
90 
91     bool mPollEventPending;
92     int64_t mLastReceiverReportTimeUs;
93     int64_t mLastBitrateReportTimeUs;
94     int64_t mLastEarlyNotifyTimeUs;
95     int64_t mLastCongestionNotifyTimeUs;
96 
97     int32_t mSelfID;
98     int32_t mTargetBitrate;
99     int32_t mRtpSockOptEcn;
100     bool mIsIPv6;
101 
102     uint32_t mStaticJitterTimeMs;
103 
104     int32_t mCumulativeBytes;
105 
106     void onAddStream(const sp<AMessage> &msg);
107     void onSeekStream(const sp<AMessage> &msg);
108     void onRemoveStream(const sp<AMessage> &msg);
109     void onPollStreams();
110     void onAlarmStream(const sp<AMessage> msg);
111     void onInjectPacket(const sp<AMessage> &msg);
112     void onSendReceiverReports();
113     void checkRxBitrate(int64_t nowUs);
114     void notifyCongestionToUpperLayerIfNeeded(StreamInfo *s);
115     void handleIpHeadersIfReceived(StreamInfo *s, struct msghdr sMsg);
116 
117     status_t receive(StreamInfo *info, bool receiveRTP);
118     ssize_t send(const StreamInfo *info, const sp<ABuffer> buffer);
119 
120     status_t parseRTP(StreamInfo *info, const sp<ABuffer> &buffer);
121     status_t parseRTPExt(StreamInfo *s, const uint8_t *extData, size_t extLen, int32_t *cvoDegrees);
122     status_t parseRTCP(StreamInfo *info, const sp<ABuffer> &buffer);
123     status_t parseSenderReport(StreamInfo *info, const uint8_t *data, size_t size);
124     status_t parseReceiverReport(StreamInfo *info, const uint8_t *data, size_t size);
125     status_t parseReceptionReportBlock(StreamInfo *info,
126             int64_t recvTimeUs, uint32_t senderId, const uint8_t *data, size_t size);
127     status_t parseTSFB(StreamInfo *info, const uint8_t *data, size_t size);
128     status_t parsePSFB(StreamInfo *info, const uint8_t *data, size_t size);
129     status_t parseBYE(StreamInfo *info, const uint8_t *data, size_t size);
130 
131     sp<ARTPSource> findSource(StreamInfo *info, uint32_t id);
132 
133     void postPollEvent();
134 
135     DISALLOW_EVIL_CONSTRUCTORS(ARTPConnection);
136 };
137 
138 }  // namespace android
139 
140 #endif  // A_RTP_CONNECTION_H_
141