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 IMSMEDIA_VIDEO_RENDERER_H_INCLUDED
18 #define IMSMEDIA_VIDEO_RENDERER_H_INCLUDED
19 
20 #include <ImsMediaDefine.h>
21 #include <ImsMediaCondition.h>
22 #include <BaseSessionCallback.h>
23 #include <android/native_window.h>
24 #include <media/NdkMediaCodec.h>
25 #include <media/NdkMediaFormat.h>
26 #include <mutex>
27 #include <list>
28 
29 struct FrameData
30 {
31 public:
32     FrameData(uint8_t* data = nullptr, uint32_t size = 0, uint32_t timestamp = 0,
33             bool isConfig = false)
34     {
35         this->data = nullptr;
36 
37         if (size != 0 && data != nullptr)
38         {
39             this->data = new uint8_t[size];
40             memcpy(this->data, data, size);
41         }
42 
43         this->size = size;
44         this->timestamp = timestamp;
45         this->isConfig = isConfig;
46     }
47 
~FrameDataFrameData48     ~FrameData()
49     {
50         if (data != nullptr)
51         {
52             delete[] data;
53         }
54     }
55 
56     uint8_t* data;
57     uint32_t size;
58     uint32_t timestamp;
59     bool isConfig;
60 };
61 
62 /**
63  * @brief This class uses android video renderer interface to display video frames.
64  */
65 class ImsMediaVideoRenderer
66 {
67 public:
68     ImsMediaVideoRenderer();
69     ~ImsMediaVideoRenderer();
70     void SetSessionCallback(BaseSessionCallback* callback);
71     void SetCodec(int32_t codecType);
72     void SetResolution(uint32_t width, uint32_t height);
73     void SetDeviceOrientation(uint32_t orientation);
74     void SetSurface(ANativeWindow* window);
75     bool Start();
76     void Stop();
77     void OnDataFrame(uint8_t* data, uint32_t size, uint32_t timestamp, bool isConfigFrame);
78     void processBuffers();
79     void UpdateDeviceOrientation(uint32_t degree);
80     void UpdatePeerOrientation(uint32_t degree);
81 
82 private:
83     BaseSessionCallback* mCallback;
84     ANativeWindow* mWindow;
85     AMediaCodec* mCodec;
86     AMediaFormat* mFormat;
87     std::list<FrameData*> mFrameDatas;
88     std::mutex mMutex;
89     ImsMediaCondition mConditionExit;
90     int32_t mCodecType;
91     uint32_t mWidth;
92     uint32_t mHeight;
93     uint32_t mFarOrientationDegree;
94     uint32_t mNearOrientationDegree;
95     bool mStopped;
96 };
97 
98 #endif  // IMSMEDIA_VIDEO_RENDERER_H_INCLUDED
99