summaryrefslogtreecommitdiff
path: root/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/include/IRtpSession.h
blob: bb667867caea2293b7f90eec58b69dfde396b3dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IRTP_SESSION_H
#define IRTP_SESSION_H

#include <ImsMediaDefine.h>
#include <AudioConfig.h>
#include <RtpService.h>
#include <list>
#include <atomic>
#include <stdint.h>
#include <mutex>

/*!
 * @class       IRtpEncoderListener
 */
class IRtpEncoderListener
{
public:
    IRtpEncoderListener() {}
    virtual ~IRtpEncoderListener() {}
    virtual void OnRtpPacket(unsigned char* pData, uint32_t wLen) = 0;
};

/*!
 * @class        IRtcpEncoderListener
 */
class IRtcpEncoderListener
{
public:
    IRtcpEncoderListener() {}
    virtual ~IRtcpEncoderListener() {}
    virtual void OnRtcpPacket(unsigned char* pData, uint32_t wLen) = 0;
};

/*!
 * @class        IRtpDecoderListener
 */
class IRtpDecoderListener
{
public:
    IRtpDecoderListener() {}
    virtual ~IRtpDecoderListener() {}
    virtual void OnMediaDataInd(unsigned char* pData, uint32_t nDataSize, uint32_t nTimestamp,
            bool bMark, uint16_t nSeqNum, uint32_t nPayloadType, uint32_t nSSRC, bool bExtension,
            uint16_t nExtensionData) = 0;
    virtual void OnNumReceivedPacket(uint32_t nNumRtpPacket) = 0;
};

/*!
 * @class        IRtcpDecoderListener
 */
class IRtcpDecoderListener
{
public:
    IRtcpDecoderListener() {}
    virtual ~IRtcpDecoderListener() {}
    virtual void OnRtcpInd(tRtpSvc_IndicationFromStack eIndType, void* pMsg) = 0;
    virtual void OnNumReceivedPacket(uint32_t nNumRtcpSRPacket, uint32_t nNumRtcpRRPacket) = 0;
    virtual void OnEvent(uint32_t event, uint32_t param) = 0;
};

#define MAX_NUM_PAYLOAD_PARAM 4

/*!
 * @class        IRtpSession
 */
class IRtpSession : public RtpServiceListener
{
public:
    static IRtpSession* GetInstance(
            ImsMediaType type, const RtpAddress& localAddress, const RtpAddress& peerAddress);
    static void ReleaseInstance(IRtpSession* pSession);
    IRtpSession(
            ImsMediaType subtype, const RtpAddress& localAddress, const RtpAddress& peerAddress);
    virtual ~IRtpSession();
    bool operator==(const IRtpSession& obj2);
    bool isSameInstance(
            ImsMediaType subtype, const RtpAddress& localAddress, const RtpAddress& peerAddress);
    void SetRtpEncoderListener(IRtpEncoderListener* pRtpEncoderListener);
    void SetRtpDecoderListener(IRtpDecoderListener* pRtpDecoderListener);
    void SetRtcpEncoderListener(IRtcpEncoderListener* pRtcpEncoderListener);
    void SetRtcpDecoderListener(IRtcpDecoderListener* pRtcpDecoderListener);
    void SetRtpPayloadParam(int32_t payloadNumTx, int32_t payloadNumRx, int32_t samplingRate,
            int32_t subTxPayloadTypeNum = 0, int32_t subRxPayloadTypeNum = 0,
            int32_t subSamplingRate = 0);
    void SetRtcpInterval(int32_t nInterval);
    void StartRtp();
    void StopRtp();
    void StartRtcp(bool bSendRtcpBye = false);
    void StopRtcp();
    bool SendRtpPacket(uint32_t nPayloadType, uint8_t* pData, uint32_t nDataSize,
            uint32_t timestamp, bool mark, uint32_t nTimeDiff, bool bExtension = false,
            tRtpHeaderExtensionInfo* pExtensionInfo = nullptr);
    bool ProcRtpPacket(uint8_t* pData, uint32_t nDataSize);
    bool ProcRtcpPacket(uint8_t* pData, uint32_t nDataSize);
    void OnTimer();
    void SendRtcpXr(uint8_t* pPayload, uint32_t nSize);
    bool SendRtcpFeedback(int32_t type, uint8_t* pFic, uint32_t nFicSize);
    ImsMediaType getMediaType();
    void increaseRefCounter();
    void decreaseRefCounter();
    uint32_t getRefCounter();
    // receive Rtp packet, send it to rtp tx node
    virtual int OnRtpPacket(unsigned char* pData, RtpSvc_Length wLen);
    // receive Rtcp packet, send it to rtcp node
    virtual int OnRtcpPacket(unsigned char* pData, RtpSvc_Length wLen);
    // indication from the RtpStack
    virtual void OnPeerInd(tRtpSvc_IndicationFromStack eIndType, void* pMsg);
    // indication from the RtpStack
    virtual void OnPeerRtcpComponents(void* nMsg);

private:
    static std::list<IRtpSession*> mListRtpSession;
    ImsMediaType mMediaType;
    RTPSESSIONID mRtpSessionId;
    std::atomic<int32_t> mRefCount;
    RtpAddress mLocalAddress;
    RtpAddress mPeerAddress;
    // Listener
    IRtpEncoderListener* mRtpEncoderListener;
    IRtpDecoderListener* mRtpDecoderListener;
    IRtcpEncoderListener* mRtcpEncoderListener;
    IRtcpDecoderListener* mRtcpDecoderListener;
    // payload parameter
    tRtpSvc_SetPayloadParam mPayloadParam[MAX_NUM_PAYLOAD_PARAM];
    uint32_t mNumPayloadParam;
    // Rtp configure
    uint32_t mLocalRtpSsrc;
    uint32_t mPeerRtpSsrc;
    bool mEnableRtcpTx;
    bool mEnableDTMF;
    uint32_t mRtpDtmfPayloadType;
    // internal use
    uint32_t mPrevTimestamp;
    uint32_t mRtpStarted;
    uint32_t mRtcpStarted;
    uint32_t mNumRtpProcPacket;   // received packet
    uint32_t mNumRtcpProcPacket;  // received packet
    uint32_t mNumRtpPacket;       // received packet
    uint32_t mNumSRPacket;        // received packet
    uint32_t mNumRRPacket;        // received packet
    uint32_t mNumRtpDataToSend;
    uint32_t mNumRtpPacketSent;
    uint32_t mNumRtcpPacketSent;
    int32_t mRttd;
    std::mutex mutexDecoder;
    std::mutex mutexEncoder;
};

#endif