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 RTP_ENCODER_NODE_H
18 #define RTP_ENCODER_NODE_H
19 
20 #include <BaseNode.h>
21 #include <IRtpSession.h>
22 #include <RtpContextParams.h>
23 #include <RtpHeaderExtension.h>
24 #include <mutex>
25 
26 class RtpEncoderNode : public BaseNode, public IRtpEncoderListener
27 {
28 public:
29     RtpEncoderNode(BaseSessionCallback* callback = nullptr);
30     virtual ~RtpEncoderNode();
31     virtual kBaseNodeId GetNodeId();
32     virtual ImsMediaResult Start();
33     virtual void Stop();
34     virtual void OnDataFromFrontNode(ImsMediaSubType subtype, uint8_t* data, uint32_t size,
35             uint32_t timestamp, bool mark, uint32_t seq,
36             ImsMediaSubType dataType = ImsMediaSubType::MEDIASUBTYPE_UNDEFINED,
37             uint32_t arrivalTime = 0);
38     virtual bool IsSourceNode();
39     virtual void SetConfig(void* config);
40     virtual bool IsSameConfig(void* config);
41     // IRtpEncoderListener method
42     virtual void OnRtpPacket(unsigned char* data, uint32_t nSize);
43 
44     /**
45      * @brief Set the local ip address and port number
46      */
47     void SetLocalAddress(const RtpAddress& address);
48 
49     /**
50      * @brief Set the peer ip address and port number
51      */
52     void SetPeerAddress(const RtpAddress& address);
53 
54     /**
55      * @brief Set the camera facing and device orientation parameter for cvo extension in rtp header
56      *
57      * @param facing The facing of camera define in kCameraFacing in ImsMediaVideoUtil.h
58      * @param orientation The orientation value to send in degree unit
59      * @return true Return true when the extension data set properly
60      * @return false Return false when the cvo configuration is disabled
61      */
62     bool SetCvoExtension(const int64_t facing, const int64_t orientation);
63 
64     /**
65      * @brief Convert list of the RtpHeaderExtension to Rtp header extension payload
66      */
67     void SetRtpHeaderExtension(std::list<RtpHeaderExtension>* listExtension);
68 
69     /**
70      * @brief Sets RTP stack with params such as SSRC, sequence number, timestamp, etc. RTP Packets
71      * sent after this call will follow the new params set.
72      *
73      * @param rtpContextParams holds the rtp parameters such as ssrc, timestamp,
74      * sequence number, etc.
75      */
76     void SetRtpContext(RtpContextParams& rtpContextParams);
77 
78     void GetRtpContext(RtpContextParams& rtpContextParams);
79 
80 private:
81     void ProcessAudioData(
82             ImsMediaSubType subtype, uint8_t* data, uint32_t size, uint32_t timestamp);
83     void ProcessVideoData(
84             ImsMediaSubType subtype, uint8_t* data, uint32_t size, uint32_t timestamp, bool mark);
85     void ProcessTextData(
86             ImsMediaSubType subtype, uint8_t* data, uint32_t size, uint32_t timestamp, bool mark);
87 
88     IRtpSession* mRtpSession;
89     std::mutex mMutex;
90     RtpAddress mLocalAddress;
91     RtpAddress mPeerAddress;
92     bool mDtmfMode;
93     bool mMark;
94     uint32_t mPrevTimestamp;
95     int8_t mSamplingRate;
96     int8_t mRtpPayloadTx;
97     int8_t mRtpPayloadRx;
98     int8_t mRtpTxDtmfPayload;
99     int8_t mRtpRxDtmfPayload;
100     int8_t mDtmfSamplingRate;
101     int32_t mDtmfTimestamp;
102     int32_t mCvoValue;
103     int8_t mRedundantPayload;
104     int8_t mRedundantLevel;
105     std::list<RtpHeaderExtensionInfo> mListRtpExtension;
106     RtpContextParams mRtpContextParams;
107     int32_t mArrivalTime;
108 };
109 
110 #endif
111