1 /** 2 * Copyright (C) 2022 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 IRTP_SESSION_H 18 #define IRTP_SESSION_H 19 20 #include <ImsMediaDefine.h> 21 #include <AudioConfig.h> 22 #include <RtpService.h> 23 #include <list> 24 #include <atomic> 25 #include <stdint.h> 26 #include <mutex> 27 28 /*! 29 * @class IRtpEncoderListener 30 */ 31 class IRtpEncoderListener 32 { 33 public: IRtpEncoderListener()34 IRtpEncoderListener() {} ~IRtpEncoderListener()35 virtual ~IRtpEncoderListener() {} 36 virtual void OnRtpPacket(unsigned char* pData, uint32_t wLen) = 0; 37 }; 38 39 /*! 40 * @class IRtcpEncoderListener 41 */ 42 class IRtcpEncoderListener 43 { 44 public: IRtcpEncoderListener()45 IRtcpEncoderListener() {} ~IRtcpEncoderListener()46 virtual ~IRtcpEncoderListener() {} 47 virtual void OnRtcpPacket(unsigned char* pData, uint32_t wLen) = 0; 48 }; 49 50 /*! 51 * @class IRtpDecoderListener 52 */ 53 class IRtpDecoderListener 54 { 55 public: IRtpDecoderListener()56 IRtpDecoderListener() {} ~IRtpDecoderListener()57 virtual ~IRtpDecoderListener() {} 58 virtual void OnMediaDataInd(unsigned char* data, uint32_t dataSize, uint32_t timestamp, 59 bool mark, uint16_t seqNum, uint32_t payloadType, uint32_t ssrc, 60 const RtpHeaderExtensionInfo& extensionInfo) = 0; 61 virtual void OnNumReceivedPacket(uint32_t nNumRtpPacket) = 0; 62 }; 63 64 /*! 65 * @class IRtcpDecoderListener 66 */ 67 class IRtcpDecoderListener 68 { 69 public: IRtcpDecoderListener()70 IRtcpDecoderListener() {} ~IRtcpDecoderListener()71 virtual ~IRtcpDecoderListener() {} 72 virtual void OnRtcpInd(tRtpSvc_IndicationFromStack eIndType, void* pMsg) = 0; 73 virtual void OnNumReceivedPacket(uint32_t nNumRtcpSRPacket, uint32_t nNumRtcpRRPacket) = 0; 74 virtual void OnEvent(uint32_t event, uint32_t param) = 0; 75 }; 76 77 /*! 78 * @class IRtpSession 79 */ 80 class IRtpSession : public RtpServiceListener 81 { 82 public: 83 static IRtpSession* GetInstance( 84 ImsMediaType type, const RtpAddress& localAddress, const RtpAddress& peerAddress); 85 static void ReleaseInstance(IRtpSession* pSession); 86 IRtpSession( 87 ImsMediaType subtype, const RtpAddress& localAddress, const RtpAddress& peerAddress); 88 virtual ~IRtpSession(); 89 bool operator==(const IRtpSession& obj2); 90 bool isSameInstance( 91 ImsMediaType subtype, const RtpAddress& localAddress, const RtpAddress& peerAddress); 92 void SetRtpEncoderListener(IRtpEncoderListener* pRtpEncoderListener); 93 void SetRtpDecoderListener(IRtpDecoderListener* pRtpDecoderListener); 94 void SetRtcpEncoderListener(IRtcpEncoderListener* pRtcpEncoderListener); 95 void SetRtcpDecoderListener(IRtcpDecoderListener* pRtcpDecoderListener); 96 void SetRtpPayloadParam(int32_t payloadNumTx, int32_t payloadNumRx, int32_t samplingRate, 97 int32_t subTxPayloadTypeNum = 0, int32_t subRxPayloadTypeNum = 0, 98 int32_t subSamplingRate = 0); 99 void SetRtcpInterval(int32_t nInterval); 100 void StartRtp(bool bResetSsrc = false); 101 void StopRtp(); 102 void StartRtcp(bool bSendRtcpBye = false); 103 void StopRtcp(); 104 bool SendRtpPacket(uint32_t payloadType, uint8_t* data, uint32_t dataSize, uint32_t timestamp, 105 bool mark, uint32_t nTimeDiff, RtpHeaderExtensionInfo* extensionInfo = nullptr); 106 bool ProcRtpPacket(uint8_t* pData, uint32_t nDataSize); 107 bool ProcRtcpPacket(uint8_t* pData, uint32_t nDataSize); 108 // Sends RTP and RTCP statistics to listeners. 109 void OnRtpStatsTimerExpired(); 110 void SendRtcpXr(uint8_t* pPayload, uint32_t nSize); 111 bool SendRtcpFeedback(int32_t type, uint8_t* pFic, uint32_t nFicSize); 112 void SetRtpContext(uint32_t ssrc, uint32_t timestamp, uint16_t sequenceNumber); 113 void GetRtpContext(uint32_t& ssrc, uint32_t& timestamp, uint16_t& sequenceNumber); 114 // receive Rtp packet, send it to rtp tx node 115 virtual int OnRtpPacket(unsigned char* pData, RtpSvc_Length wLen); 116 // receive Rtcp packet, send it to rtcp node 117 virtual int OnRtcpPacket(unsigned char* pData, RtpSvc_Length wLen); 118 // indication from the RtpStack 119 virtual void OnPeerInd(tRtpSvc_IndicationFromStack eIndType, void* pMsg); 120 // indication from the RtpStack 121 virtual void OnPeerRtcpComponents(void* nMsg); 122 123 private: 124 static std::list<IRtpSession*> mListRtpSession; 125 ImsMediaType mMediaType; 126 RTPSESSIONID mRtpSessionId; 127 std::atomic<int32_t> mRefCount; 128 RtpAddress mLocalAddress; 129 RtpAddress mPeerAddress; 130 // Listener 131 IRtpEncoderListener* mRtpEncoderListener; 132 IRtpDecoderListener* mRtpDecoderListener; 133 IRtcpEncoderListener* mRtcpEncoderListener; 134 IRtcpDecoderListener* mRtcpDecoderListener; 135 // payload parameter 136 tRtpSvc_SetPayloadParam mPayloadParam[RTP_MAX_PAYLOAD_TYPE]; 137 uint32_t mNumPayloadParam; 138 // Rtp configure 139 uint32_t mLocalRtpSsrc; 140 uint32_t mPeerRtpSsrc; 141 bool mEnableRtcpTx; 142 bool mEnableDTMF; 143 uint32_t mRtpDtmfPayloadType; 144 // internal use 145 uint32_t mPrevTimestamp; 146 uint32_t mRtpStarted; 147 uint32_t mRtcpStarted; 148 uint32_t mNumRtpProcPacket; // received packet 149 uint32_t mNumRtcpProcPacket; // received packet 150 uint32_t mNumRtpPacket; // received packet 151 uint32_t mNumSRPacket; // received packet 152 uint32_t mNumRRPacket; // received packet 153 uint32_t mNumRtpDataToSend; 154 uint32_t mNumRtpPacketSent; 155 uint32_t mNumRtcpPacketSent; 156 int32_t mRttd; 157 std::mutex mutexDecoder; 158 std::mutex mutexEncoder; 159 160 ImsMediaType getMediaType(); 161 void increaseRefCounter(); 162 void decreaseRefCounter(); 163 uint32_t getRefCounter(); 164 void addPayloadType(RtpDt_UInt32 payloadType, RtpDt_UInt32 samplingRate); 165 }; 166 167 #endif 168