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_SOURCE_H_INCLUDED
18 #define IMSMEDIA_VIDEO_SOURCE_H_INCLUDED
19 
20 #include <ImsMediaVideoUtil.h>
21 #include <ImsMediaDefine.h>
22 #include <android/native_window.h>
23 #include <ImsMediaCamera.h>
24 #include <media/NdkMediaCodec.h>
25 #include <media/NdkMediaFormat.h>
26 #include <media/NdkImageReader.h>
27 #include <ImsMediaCondition.h>
28 #include "ImsMediaPauseImageSource.h"
29 
30 class IVideoSourceCallback
31 {
32 public:
~IVideoSourceCallback()33     virtual ~IVideoSourceCallback() {}
34     virtual void OnUplinkEvent(
35             uint8_t* pBitstream, uint32_t nSize, int64_t pstUsec, uint32_t flag) = 0;
36     virtual void OnEvent(int32_t type, int32_t param1, int32_t param2) = 0;
37 };
38 
39 enum kImsMediaVideoMode
40 {
41     kVideoModePreview = 0,
42     kVideoModeRecording,
43     kVideoModePauseImage,
44 };
45 
46 enum kVideoSourceEvent
47 {
48     kVideoSourceEventUpdateOrientation = 0,
49     kVideoSourceEventCameraError,
50 };
51 
52 /**
53  * @brief
54  *
55  */
56 class ImsMediaVideoSource
57 {
58 public:
59     ImsMediaVideoSource();
60     ~ImsMediaVideoSource();
61 
62     /**
63      * @brief Set the IVideoSourceCallback object listener
64      */
65     void SetListener(IVideoSourceCallback* listener);
66 
67     /**
68      * @brief Set the VideoMode defined in VideoConfig
69      */
70     void SetVideoMode(const int32_t mode);
71 
72     /**
73      * @brief Set the Camera configuration parameter, it should set before open camera
74      *
75      * @param cameraId The camera device id
76      * @param cameraZoom The camera zoom level
77      */
78     void SetCameraConfig(const uint32_t cameraId, const uint32_t cameraZoom);
79 
80     /**
81      * @brief Set the pause image path stored
82      */
83     void SetImagePath(const android::String8& path);
84 
85     /**
86      * @brief Set the Codec configuration parameter, this method should be called before calling
87      * start method of the ImsMediaVideoSource instance
88      *
89      * @param codecType The codec type to run, Avc and HEVC codec is available
90      * @param profile The codec profile
91      * @param level The codec level
92      * @param bitrate The bitrate of encoding frames in kbps units
93      * @param framerate The framerate of the encoding frames
94      * @param interval The interval of the idr frames
95      */
96     void SetCodecConfig(const int32_t codecType, const uint32_t profile, const uint32_t level,
97             const uint32_t bitrate, const uint32_t framerate, const uint32_t interval);
98 
99     /**
100      * @brief Set the resolution of encoded output frames required
101      */
102     void SetResolution(const uint32_t width, const uint32_t height);
103 
104     /**
105      * @brief Set the surface buffer for preview surface view
106      */
107     void SetSurface(ANativeWindow* window);
108 
109     /**
110      * @brief Set the device orientation value
111      *
112      * @param degree The orientation in degree units
113      */
114     void SetDeviceOrientation(const uint32_t degree);
115 
116     /** Start the acquires raw frame from the source and encode or sends it to the preview surface
117      * buffer based on the VideoMode configured*/
118     bool Start();
119 
120     /** Stop the image flow */
121     void Stop();
122 
123     /**
124      * @brief Get the video source node is stopped
125      */
126     bool IsStopped();
127 
128     /**
129      * @brief Called when the valid input camera frame is ready.
130      *
131      * @param pImage The camera frame object
132      */
133     void onCameraFrame(AImage* pImage);
134 
135     /**
136      * @brief Change bitrate of the encoding frames
137      *
138      * @param bitrate The bitrate in bps units
139      */
140     bool changeBitrate(const uint32_t bitrate);
141 
142     /**
143      * @brief Request a new IDR frame to the codec output streaming
144      */
145     void requestIdrFrame();
146 
147 private:
148     void EncodePauseImage();
149     void processOutputBuffer();
150     ANativeWindow* CreateImageReader(int width, int height);
151 
152     ImsMediaCamera* mCamera;
153     ANativeWindow* mWindow;
154     AMediaCodec* mCodec;
155     AMediaFormat* mFormat;
156     ANativeWindow* mImageReaderSurface;
157     AImageReader* mImageReader;
158     std::mutex mMutex;
159     ImsMediaCondition mConditionExit;
160     IVideoSourceCallback* mListener;
161     ImsMediaPauseImageSource mPauseImageSource;
162     int32_t mCodecType;
163     int32_t mVideoMode;
164     uint32_t mCodecProfile;
165     uint32_t mCodecLevel;
166     uint32_t mCameraId;
167     uint32_t mCameraZoom;
168     uint32_t mWidth;
169     uint32_t mHeight;
170     int32_t mCodecStride;
171     uint32_t mFramerate;
172     uint32_t mBitrate;
173     uint32_t mIntraInterval;
174     android::String8 mImagePath;
175     int32_t mDeviceOrientation;
176     uint64_t mTimestamp;
177     uint64_t mPrevTimestamp;
178     bool mStopped;
179 };
180 #endif