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 IVIDEO_RENDERER_NODE_H_INCLUDED
18 #define IVIDEO_RENDERER_NODE_H_INCLUDED
19 
20 #include <BaseNode.h>
21 #include <JitterBufferControlNode.h>
22 #include <IImsMediaThread.h>
23 #include <ImsMediaCondition.h>
24 #include <ImsMediaVideoRenderer.h>
25 #include <ImsMediaVideoUtil.h>
26 #include <android/native_window.h>
27 #include <mutex>
28 
29 #define USE_JITTER_BUFFER  // off this definition ONLY for test purpose.
30 #define DEMON_NTP2MSEC 65.55
31 
32 enum FrameType
33 {
34     UNKNOWN,
35     SPS,
36     PPS,
37     VPS,
38     IDR,
39     NonIDR,
40 };
41 
42 /**
43  * @brief This class describes an interface between depacketization module and audio device
44  */
45 class IVideoRendererNode : public JitterBufferControlNode
46 {
47 public:
48     IVideoRendererNode(BaseSessionCallback* callback = nullptr);
49     virtual ~IVideoRendererNode();
50     virtual kBaseNodeId GetNodeId();
51     virtual ImsMediaResult Start();
52     virtual void Stop();
53     virtual bool IsRunTime();
54     virtual bool IsSourceNode();
55     virtual void SetConfig(void* config);
56     virtual bool IsSameConfig(void* config);
57     virtual void ProcessData();
58 
59     /**
60      * @brief Updates display surface
61      *
62      * @param window surface buffer to update
63      */
64     void UpdateSurface(ANativeWindow* window);
65 
66     /**
67      * @brief Update network round trip time delay to the VideoJitterBuffer
68      *
69      * @param delay time delay in ntp timestamp unit
70      */
71     void UpdateRoundTripTimeDelay(int32_t delay);
72 
73     /**
74      * @brief Set the packet loss monitoring duration and packet loss rate threshold
75      *
76      * @param time The time duration of milliseconds unit to monitor the packet loss
77      * @param rate The packet loss rate threshold in the monitoring duration range
78      */
79     void SetPacketLossParam(uint32_t time, uint32_t rate);
80 
81 private:
82     bool hasStartingCode(uint8_t* buffer, uint32_t bufferSize);
83     FrameType GetFrameType(uint8_t* buffer, uint32_t bufferSize);
84     void SaveConfigFrame(uint8_t* buffer, uint32_t bufferSize, uint32_t type);
85     bool RemoveAUDNalUnit(
86             uint8_t* inBuffer, uint32_t ibufferSize, uint8_t** outBuffer, uint32_t* outBufferSize);
87     void CheckResolution(uint32_t nWidth, uint32_t nHeight);
88     ImsMediaResult ParseAvcSps(uint8_t* buffer, uint32_t bufferSize, tCodecConfig* config);
89     ImsMediaResult ParseHevcSps(uint8_t* buffer, uint32_t bufferSize, tCodecConfig* config);
90     void QueueConfigFrame(uint32_t timestamp);
91     void NotifyPeerDimensionChanged();
92 
93     std::unique_ptr<ImsMediaVideoRenderer> mVideoRenderer;
94     ANativeWindow* mWindow;
95     ImsMediaCondition mCondition;
96     std::mutex mMutex;
97     int32_t mCodecType;
98     uint32_t mWidth;
99     uint32_t mHeight;
100     int8_t mSamplingRate;
101     int32_t mCvoValue;
102     uint8_t mConfigBuffer[MAX_CONFIG_INDEX][MAX_CONFIG_LEN];
103     uint32_t mConfigLen[MAX_CONFIG_INDEX];
104     uint8_t mBuffer[MAX_RTP_PAYLOAD_BUFFER_SIZE];
105     uint32_t mDeviceOrientation;
106     bool mFirstFrame;
107     ImsMediaSubType mSubtype;
108     uint32_t mFramerate;
109     uint32_t mLossDuration;
110     uint32_t mLossRateThreshold;
111     uint32_t mPrevTimestamp;
112     uint32_t mCurrentFramerate;
113 };
114 
115 #endif