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