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 #define MAX_NUM_PAYLOAD_PARAM 4 78 79 /*! 80 * @class IRtpSession 81 */ 82 class IRtpSession : public RtpServiceListener 83 { 84 public: 85 static IRtpSession* GetInstance( 86 ImsMediaType type, const RtpAddress& localAddress, const RtpAddress& peerAddress); 87 static void ReleaseInstance(IRtpSession* pSession); 88 IRtpSession( 89 ImsMediaType subtype, const RtpAddress& localAddress, const RtpAddress& peerAddress); 90 virtual ~IRtpSession(); 91 bool operator==(const IRtpSession& obj2); 92 bool isSameInstance( 93 ImsMediaType subtype, const RtpAddress& localAddress, const RtpAddress& peerAddress); 94 void SetRtpEncoderListener(IRtpEncoderListener* pRtpEncoderListener); 95 void SetRtpDecoderListener(IRtpDecoderListener* pRtpDecoderListener); 96 void SetRtcpEncoderListener(IRtcpEncoderListener* pRtcpEncoderListener); 97 void SetRtcpDecoderListener(IRtcpDecoderListener* pRtcpDecoderListener); 98 void SetRtpPayloadParam(int32_t payloadNumTx, int32_t payloadNumRx, int32_t samplingRate, 99 int32_t subTxPayloadTypeNum = 0, int32_t subRxPayloadTypeNum = 0, 100 int32_t subSamplingRate = 0); 101 void SetRtcpInterval(int32_t nInterval); 102 void StartRtp(bool bResetSsrc = false); 103 void StopRtp(); 104 void StartRtcp(bool bSendRtcpBye = false); 105 void StopRtcp(); 106 bool SendRtpPacket(uint32_t payloadType, uint8_t* data, uint32_t dataSize, uint32_t timestamp, 107 bool mark, uint32_t nTimeDiff, RtpHeaderExtensionInfo* extensionInfo = nullptr); 108 bool ProcRtpPacket(uint8_t* pData, uint32_t nDataSize); 109 bool ProcRtcpPacket(uint8_t* pData, uint32_t nDataSize); 110 void OnTimer(); 111 void SendRtcpXr(uint8_t* pPayload, uint32_t nSize); 112 bool SendRtcpFeedback(int32_t type, uint8_t* pFic, uint32_t nFicSize); 113 ImsMediaType getMediaType(); 114 void increaseRefCounter(); 115 void decreaseRefCounter(); 116 uint32_t getRefCounter(); 117 void SetRtpContext(uint32_t ssrc, uint32_t timestamp, uint16_t sequenceNumber); 118 void GetRtpContext(uint32_t& ssrc, uint32_t& timestamp, uint16_t& sequenceNumber); 119 // receive Rtp packet, send it to rtp tx node 120 virtual int OnRtpPacket(unsigned char* pData, RtpSvc_Length wLen); 121 // receive Rtcp packet, send it to rtcp node 122 virtual int OnRtcpPacket(unsigned char* pData, RtpSvc_Length wLen); 123 // indication from the RtpStack 124 virtual void OnPeerInd(tRtpSvc_IndicationFromStack eIndType, void* pMsg); 125 // indication from the RtpStack 126 virtual void OnPeerRtcpComponents(void* nMsg); 127 128 private: 129 static std::list<IRtpSession*> mListRtpSession; 130 ImsMediaType mMediaType; 131 RTPSESSIONID mRtpSessionId; 132 std::atomic<int32_t> mRefCount; 133 RtpAddress mLocalAddress; 134 RtpAddress mPeerAddress; 135 // Listener 136 IRtpEncoderListener* mRtpEncoderListener; 137 IRtpDecoderListener* mRtpDecoderListener; 138 IRtcpEncoderListener* mRtcpEncoderListener; 139 IRtcpDecoderListener* mRtcpDecoderListener; 140 // payload parameter 141 tRtpSvc_SetPayloadParam mPayloadParam[MAX_NUM_PAYLOAD_PARAM]; 142 uint32_t mNumPayloadParam; 143 // Rtp configure 144 uint32_t mLocalRtpSsrc; 145 uint32_t mPeerRtpSsrc; 146 bool mEnableRtcpTx; 147 bool mEnableDTMF; 148 uint32_t mRtpDtmfPayloadType; 149 // internal use 150 uint32_t mPrevTimestamp; 151 uint32_t mRtpStarted; 152 uint32_t mRtcpStarted; 153 uint32_t mNumRtpProcPacket; // received packet 154 uint32_t mNumRtcpProcPacket; // received packet 155 uint32_t mNumRtpPacket; // received packet 156 uint32_t mNumSRPacket; // received packet 157 uint32_t mNumRRPacket; // received packet 158 uint32_t mNumRtpDataToSend; 159 uint32_t mNumRtpPacketSent; 160 uint32_t mNumRtcpPacketSent; 161 int32_t mRttd; 162 std::mutex mutexDecoder; 163 std::mutex mutexEncoder; 164 }; 165 166 #endif 167