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 updateSocketDscp(int32_t dscp); 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 net_handle_t mRTPSockNetwork; 102 103 AString mProfileLevel; 104 AString mSeqParamSet; 105 AString mPicParamSet; 106 107 MediaBufferBase *mVPSBuf; 108 MediaBufferBase *mSPSBuf; 109 MediaBufferBase *mPPSBuf; 110 111 uint32_t mClockRate; 112 uint32_t mSourceID; 113 uint32_t mPayloadType; 114 uint32_t mSeqNo; 115 uint32_t mRTPTimeBase; 116 uint32_t mNumRTPSent; 117 uint32_t mNumRTPOctetsSent; 118 119 uint32_t mOpponentID; 120 uint32_t mBitrate; 121 typedef uint64_t Bytes; 122 sp<TrafficRecorder<uint32_t /* Time */, Bytes> > mTrafficRec; 123 124 int32_t mNumSRsSent; 125 int32_t mRTPCVOExtMap; 126 int32_t mRTPCVODegrees; 127 128 enum { 129 INVALID, 130 H265, 131 H264, 132 H263, 133 AMR_NB, 134 AMR_WB, 135 } mMode; 136 137 static uint64_t GetNowNTP(); 138 uint32_t getRtpTime(int64_t timeUs); 139 140 void initState(); 141 void onRead(const sp<AMessage> &msg); 142 void onSendSR(const sp<AMessage> &msg); 143 144 void addSR(const sp<ABuffer> &buffer); 145 void addSDES(const sp<ABuffer> &buffer); 146 void addTMMBN(const sp<ABuffer> &buffer); 147 148 void makeH264SPropParamSets(MediaBufferBase *buffer); 149 void dumpSessionDesc(); 150 151 void sendBye(); 152 void sendVPSSPSPPSIfIFrame(MediaBufferBase *mediaBuf, int64_t timeUs); 153 void sendSPSPPSIfIFrame(MediaBufferBase *mediaBuf, int64_t timeUs); 154 void sendHEVCData(MediaBufferBase *mediaBuf); 155 void sendAVCData(MediaBufferBase *mediaBuf); 156 void sendH263Data(MediaBufferBase *mediaBuf); 157 void sendAMRData(MediaBufferBase *mediaBuf); 158 159 void send(const sp<ABuffer> &buffer, bool isRTCP); 160 void makeSocketPairAndBind(String8& localIp, int localPort, String8& remoteIp, int remotePort); 161 162 void ModerateInstantTraffic(uint32_t samplePeriod, uint32_t limitBytes); 163 DISALLOW_EVIL_CONSTRUCTORS(ARTPWriter); 164 }; 165 166 } // namespace android 167 168 #endif // A_RTP_WRITER_H_ 169