summaryrefslogtreecommitdiff
path: root/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/include/nodes/BaseNode.h
blob: 9ca795183808c92428268b6c18e9eac10b0b25de (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
 * 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 BASE_NODE_H
#define BASE_NODE_H

#include <stdint.h>
#include <ImsMediaDataQueue.h>
#include <BaseSessionCallback.h>
#include <StreamSchedulerCallback.h>

#define MAX_AUDIO_PAYLOAD_SIZE (1500)
#define MAX_FRAME_IN_PACKET    ((MAX_AUDIO_PAYLOAD_SIZE - 1) / 32)

enum kBaseNodeState
{
    /* the state after stop method called normally*/
    kNodeStateStopped,
    /* the state after start without error*/
    kNodeStateRunning,
};

enum kBaseNodeId
{
    kNodeIdUnknown,
    // for socket
    kNodeIdSocketWriter,
    kNodeIdSocketReader,
    // for rtp
    kNodeIdRtpEncoder,
    kNodeIdRtpDecoder,
    // for rtcp
    kNodeIdRtcpEncoder,
    kNodeIdRtcpDecoder,
    // for Audio
    kNodeIdAudioSource,
    kNodeIdAudioPlayer,
    kNodeIdDtmfEncoder,
    kNodeIdAudioPayloadEncoder,
    kNodeIdAudioPayloadDecoder,
    // for Video
    kNodeIdVideoSource,
    kNodeIdVideoRenderer,
    kNodeIdVideoPayloadEncoder,
    kNodeIdVideoPayloadDecoder,
    // for Text
    kNodeIdTextSource,
    kNodeIdTextRenderer,
    kNodeIdTextPayloadEncoder,
    kNodeIdTextPayloadDecoder,
};

/**
 * @brief BaseNode object
 *
 */
class BaseNode
{
public:
    BaseNode(BaseSessionCallback* callback = nullptr);
    virtual ~BaseNode();
    /**
     * @brief Sets the BaseSession callback listener
     *
     * @param callback the callback instance
     */
    void SetSessionCallback(BaseSessionCallback* callback);

    /**
     * @brief Sets the session scheduler callback listener
     *
     * @param callback the instance of callback listener
     */
    void SetSchedulerCallback(std::shared_ptr<StreamSchedulerCallback>& callback);

    /**
     * @brief Connects a node to rear to this node. It makes to pass the processed data to next node
     *
     * @param pRearNode The instance of node to connect to next node
     */
    void ConnectRearNode(BaseNode* pRearNode);

    /**
     * @brief Disconnect nodes connected to rear and front
     */
    void DisconnectNodes();

    /**
     * @brief Empty the data queue
     *
     */
    void ClearDataQueue();

    /**
     * @brief Gets the node id to identify the IAudioSourceNoce
     *
     * @return BaseNodeID The node id
     */
    virtual kBaseNodeId GetNodeId();

    /**
     * @brief Starts to run the node with the configuration already set by the SetConfig method
     *
     * @return ImsMediaResult return RESULT_SUCCESS when it starts well without error
     */
    virtual ImsMediaResult Start();

    /**
     * @brief Starts to run node with the configuration already set by the SetConfig method in
     * scheduler thread
     *
     * @return ImsMediaResult return RESULT_SUCCESS when it starts well without error
     */
    virtual ImsMediaResult ProcessStart();

    /**
     * @brief Stops the node operation
     *
     */
    virtual void Stop() = 0;

    /**
     * @brief Checks the node processes data in main thread.
     */
    virtual bool IsRunTime() = 0;

    /**
     * @brief Checks the node to start in main thread
     */
    virtual bool IsRunTimeStart();

    /**
     * @brief Checks the node is initial node of data source
     *
     * @return true It is a source node
     * @return false It is not a source node
     */
    virtual bool IsSourceNode() = 0;

    /**
     * @brief Sets the config to delivers the parameter to use in the node
     *
     * @param config Sets the Audio/Video/TextConfig.
     */
    virtual void SetConfig(void* config);

    /**
     * @brief Compares the config with the member valuables in the node
     *
     * @param config Audio/Video/TextConfig to compare
     * @return true The member valuables in the config is same with the member valuables in the node
     * @return false There is at least one member valuables not same with config
     */
    virtual bool IsSameConfig(void* config);

    /**
     * @brief Updates the node member valuable and re start the running operation with the given
     * config.
     *
     * @param config The Audio/Video/TextConfig to update
     * @return ImsMediaResult Returns RETURN_SUCCESS when the update succeed
     */
    virtual ImsMediaResult UpdateConfig(void* config);

    /**
     * @brief This method is invoked by the thread created in StreamScheduler
     *
     */
    virtual void ProcessData();

    /**
     * @brief Gets the node name with char types
     *
     * @return const char* The node name
     */
    virtual const char* GetNodeName();

    /**
     * @brief Sets the media type
     *
     * @param eType the types can be Audio/Video/Text. Check the type definition.
     */
    virtual void SetMediaType(ImsMediaType eType);

    /**
     * @brief Gets the media type
     *
     * @return ImsMediaType the types of the node
     */
    virtual ImsMediaType GetMediaType();

    /**
     * @brief Gets the state of the node
     *
     * @return kBaseNodeState The returning node states is running or stopped.
     */
    virtual kBaseNodeState GetState();

    virtual void SetState(kBaseNodeState state);
    /**
     * @brief Gets the number of data stored in this node
     *
     * @return uint32_t The data count
     */
    virtual uint32_t GetDataCount();

    /**
     * @brief Gets one data stored in front of data queue in the node
     *
     * @param subtype The subtype of data stored in the queue. It can be various subtype according
     * to the characteristics of the given data
     * @param data The data buffer
     * @param dataSize The size of data
     * @param timestamp The timestamp of data, it can be milliseconds unit or rtp timestamp unit
     * @param mark It is true when the data has marker bit set
     * @param seq The sequence number of data. it is 0 when there is no valid sequence number set
     * @param dataType The additional data type for the video frames
     * @param arrivalTime The arrival time of the packet
     * @return true Succeeds to gets the valid data
     * @return false Fails to gets the valid data
     */
    virtual bool GetData(ImsMediaSubType* subtype, uint8_t** data, uint32_t* dataSize,
            uint32_t* timestamp, bool* mark, uint32_t* seq, ImsMediaSubType* dataType = nullptr,
            uint32_t* arrivalTime = nullptr);

    /**
     * @brief Deletes the data stored in the front of the data queue
     *
     */
    virtual void DeleteData();

    /**
     * @brief Sends processed data to next node
     *
     * @param subtype The subtype of data stored in the queue. It can be various subtype according
     * to the characteristics of the given data
     * @param data The data buffer
     * @param dataSize The size of data
     * @param timestamp The timestamp of data, it can be milliseconds unit or rtp timestamp unit
     * @param mark It is true when the data has marker bit set
     * @param seq The sequence number of data. it is 0 when there is no valid sequence number set
     * @param dataType The additional data type for the video frames
     * @param arrivalTime The arrival time of the packet in milliseconds unit
     */
    virtual void SendDataToRearNode(ImsMediaSubType subtype, uint8_t* data, uint32_t dataSize,
            uint32_t timestamp, bool mark, uint32_t seq,
            ImsMediaSubType nDataType = ImsMediaSubType::MEDIASUBTYPE_UNDEFINED,
            uint32_t arrivalTime = 0);

    /**
     * @brief This method is invoked when the front node calls SendDataToRearNode to pass the
     * processed data to next connected rear node
     *
     * @param subtype The subtype of data stored in the queue. It can be various subtype according
     * to the characteristics of the given data
     * @param data The data buffer
     * @param dataSize The size of data
     * @param timestamp The timestamp of data, it can be milliseconds unit or rtp timestamp unit
     * @param mark It is true when the data has marker bit set
     * @param seq The sequence number of data. it is 0 when there is no valid sequence number set
     * @param dataType The additional data type for the video frames
     * @param arrivalTime The arrival time of the packet
     */
    virtual void OnDataFromFrontNode(ImsMediaSubType subtype, uint8_t* pData, uint32_t nDataSize,
            uint32_t timestamp, bool mark, uint32_t nSeqNum,
            ImsMediaSubType nDataType = ImsMediaSubType::MEDIASUBTYPE_UNDEFINED,
            uint32_t arrivalTime = 0);

protected:
    /**
     * @brief Disconnects the front node from this node.
     *
     * @param pFrontNode The instance of node to disconnect
     */
    void DisconnectFrontNode(BaseNode* pFrontNode);

    /**
     * @brief Disconnects the rear node from this node.
     *
     * @param pRearNode The instance of node to disconnect.
     */
    void DisconnectRearNode(BaseNode* pRearNode);

    std::shared_ptr<StreamSchedulerCallback> mScheduler;
    BaseSessionCallback* mCallback;
    kBaseNodeState mNodeState;
    ImsMediaDataQueue mDataQueue;
    std::list<BaseNode*> mListFrontNodes;
    std::list<BaseNode*> mListRearNodes;
    ImsMediaType mMediaType;
};

#endif