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_WRITER_H_ 18 19 #define A_RTP_WRITER_H_ 20 21 #include <media/stagefright/foundation/ABase.h> 22 #include <media/stagefright/foundation/AHandlerReflector.h> 23 #include <media/stagefright/foundation/AString.h> 24 #include <media/stagefright/foundation/base64.h> 25 #include <media/stagefright/MediaWriter.h> 26 27 #include <arpa/inet.h> 28 #include <sys/socket.h> 29 30 #include <android/multinetwork.h> 31 #include "TrafficRecorder.h" 32 33 #define LOG_TO_FILES 0 34 35 namespace android { 36 37 struct ABuffer; 38 class MediaBuffer; 39 40 struct ARTPWriter : public MediaWriter { 41 explicit ARTPWriter(int fd); 42 explicit ARTPWriter(int fd, String8& localIp, int localPort, 43 String8& remoteIp, int remotePort, 44 uint32_t seqNo); 45 46 virtual status_t addSource(const sp<MediaSource> &source); 47 virtual bool reachedEOS(); 48 virtual status_t start(MetaData *params); 49 virtual status_t stop(); 50 virtual status_t pause(); 51 void updateCVODegrees(int32_t cvoDegrees); 52 void updatePayloadType(int32_t payloadType); 53 void updateSocketOpt(); 54 void updateSocketNetwork(int64_t socketNetwork); 55 uint32_t getSequenceNum(); 56 virtual uint64_t getAccumulativeBytes() override; 57 58 virtual void onMessageReceived(const sp<AMessage> &msg); 59 virtual void setTMMBNInfo(uint32_t opponentID, uint32_t bitrate); 60 61 protected: 62 virtual ~ARTPWriter(); 63 64 private: 65 enum { 66 kWhatStart = 'strt', 67 kWhatStop = 'stop', 68 kWhatRead = 'read', 69 kWhatSendSR = 'sr ', 70 }; 71 72 enum { 73 kFlagStarted = 1, 74 kFlagEOS = 2, 75 }; 76 77 Mutex mLock; 78 Condition mCondition; 79 uint32_t mFlags; 80 81 int mFd; 82 83 #if LOG_TO_FILES 84 int mRTPFd; 85 int mRTCPFd; 86 #endif 87 88 sp<MediaSource> mSource; 89 sp<ALooper> mLooper; 90 sp<AHandlerReflector<ARTPWriter> > mReflector; 91 92 bool mIsIPv6; 93 int mRTPSocket, mRTCPSocket; 94 struct sockaddr_in mLocalAddr; 95 struct sockaddr_in mRTPAddr; 96 struct sockaddr_in mRTCPAddr; 97 struct sockaddr_in6 mLocalAddr6; 98 struct sockaddr_in6 mRTPAddr6; 99 struct sockaddr_in6 mRTCPAddr6; 100 int32_t mRtpLayer3Dscp; 101 int32_t mRtpSockOptEcn; 102 net_handle_t mRTPSockNetwork; 103 104 AString mProfileLevel; 105 AString mSeqParamSet; 106 AString mPicParamSet; 107 108 MediaBufferBase *mVPSBuf; 109 MediaBufferBase *mSPSBuf; 110 MediaBufferBase *mPPSBuf; 111 112 uint32_t mClockRate; 113 uint32_t mSourceID; 114 uint32_t mPayloadType; 115 uint32_t mSeqNo; 116 uint32_t mRTPTimeBase; 117 uint32_t mNumRTPSent; 118 uint32_t mNumRTPOctetsSent; 119 120 uint32_t mOpponentID; 121 uint32_t mBitrate; 122 typedef uint64_t Bytes; 123 sp<TrafficRecorder<uint32_t /* Time */, Bytes> > mTrafficRec; 124 125 int32_t mNumSRsSent; 126 int32_t mRTPCVOExtMap; 127 int32_t mRTPCVODegrees; 128 129 enum { 130 INVALID, 131 H265, 132 H264, 133 H263, 134 AMR_NB, 135 AMR_WB, 136 } mMode; 137 138 static uint64_t GetNowNTP(); 139 uint32_t getRtpTime(int64_t timeUs); 140 141 void initState(); 142 void onRead(const sp<AMessage> &msg); 143 void onSendSR(const sp<AMessage> &msg); 144 145 void addSR(const sp<ABuffer> &buffer); 146 void addSDES(const sp<ABuffer> &buffer); 147 void addTMMBN(const sp<ABuffer> &buffer); 148 149 void makeH264SPropParamSets(MediaBufferBase *buffer); 150 void dumpSessionDesc(); 151 152 void sendBye(); 153 void sendVPSSPSPPSIfIFrame(MediaBufferBase *mediaBuf, int64_t timeUs); 154 void sendSPSPPSIfIFrame(MediaBufferBase *mediaBuf, int64_t timeUs); 155 void sendHEVCData(MediaBufferBase *mediaBuf); 156 void sendAVCData(MediaBufferBase *mediaBuf); 157 void sendH263Data(MediaBufferBase *mediaBuf); 158 void sendAMRData(MediaBufferBase *mediaBuf); 159 160 void send(const sp<ABuffer> &buffer, bool isRTCP); 161 void makeSocketPairAndBind(String8& localIp, int localPort, String8& remoteIp, int remotePort); 162 163 void ModerateInstantTraffic(uint32_t samplePeriod, uint32_t limitBytes); 164 DISALLOW_EVIL_CONSTRUCTORS(ARTPWriter); 165 }; 166 167 } // namespace android 168 169 #endif // A_RTP_WRITER_H_ 170