liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * Copyright (c) 2021 by Allwinnertech Co., Ltd.
 *
 * 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 V4L2_CAMERA_HAL_CAMERA_STREAM_H_
#define V4L2_CAMERA_HAL_CAMERA_STREAM_H_
 
#include "common.h"
#ifdef LOG_NDEBUG
#undef LOG_NDEBUG
#endif
 
#if DBG_CAMERA_STREAM
#undef NDEBUG
#define LOG_NDEBUG 0
#else
#define LOG_NDEBUG 1
#endif
 
#include <utils/StrongPointer.h>
#include <utils/Thread.h>
 
#include <array>
#include <condition_variable>
#include <map>
#include <memory>
#include <queue>
#include <string>
 
#include "CameraMetadata.h"
#include "camera.h"
#include "metadata/metadata.h"
#include "type_camera.h"
 
namespace v4l2_camera_hal {
class V4L2Camera;
class V4L2Stream;
class StreamManager;
 
typedef enum {
    CAM_FLASH_MODE_OFF,
    CAM_FLASH_MODE_AUTO,
    CAM_FLASH_MODE_ON,
    CAM_FLASH_MODE_TORCH,
    CAM_FLASH_MODE_SINGLE,
    CAM_FLASH_MODE_VIDEO_OFF,
    CAM_FLASH_MODE_MAX
} cam_flash_mode_t;
 
typedef struct {
    int32_t x_min;
    int32_t y_min;
    int32_t x_max;
    int32_t y_max;
} cam_rect_t;
 
typedef struct {
    cam_rect_t rect;
    int32_t weight;
} cam_area_t;
 
typedef struct {
    int32_t left;
    int32_t top;
    int32_t width;
    int32_t height;
} cam_crop_rect_t;
 
typedef enum {
    CAM_FOCUS_MODE_OFF,
    CAM_FOCUS_MODE_AUTO,
    CAM_FOCUS_MODE_MACRO,
    CAM_FOCUS_MODE_CONTINUOUS_VIDEO,
    CAM_FOCUS_MODE_CONTINUOUS_PICTURE,
    CAM_FOCUS_MODE_EDOF,
    CAM_FOCUS_MODE_START,
    CAM_FOCUS_MODE_STOP,
    CAM_FOCUS_MODE_START_UP,
    CAM_FOCUS_MODE_MAX
} cam_focus_mode_t;
 
typedef enum {
    CAM_FOCUS_TRIGGER_IDLE,
    CAM_FOCUS_TRIGGER_START,
    CAM_FOCUS_TRIGGER_CANCLE,
    CAM_FOCUS_TRIGGER_MAX
} cam_focus_trigger_t;
 
class CameraStream {
  friend class StreamManager;
  friend class V4L2Stream;
 public:
  static CameraStream* NewCameraStream(std::shared_ptr<V4L2Stream> stream,
                                       int isBlob,
                                       int isMirror);
  CameraStream(std::shared_ptr<V4L2Stream> stream, int isBlob);
  int configurateManager(StreamManager* manager);
  void calProperRectSize(cam_crop_rect_t srcRect, cam_crop_rect_t* dstRect);
 
  template <typename T>
  int inRange(T low, T hight, T val);
 
  virtual ~CameraStream();
  virtual int start();
  virtual int stop();
  virtual int flush();
  virtual int initialize(uint32_t width,
                         uint32_t height,
                         int      format,
                         uint32_t usage,
                         bool     mergeStreamFlag);
  virtual int setFormat(uint32_t width,
                        uint32_t height,
                        int format,
                        uint32_t usage);
  virtual int request(buffer_handle_t * buffer,
                      uint32_t frameNumber,
                      CameraMetadata* metadata);
  virtual int getBuffer(buffer_handle_t ** buffer, uint32_t* frameNumber);
  virtual int waitBuffer();
  virtual int enqueueBuffer();
  virtual int dequeueBuffer(void ** src_addr, struct timeval * ts);
  virtual int encodebuffer(void * dst_addr,
                           void * src_addr,
                           unsigned long mJpegBufferSizes);
  virtual int copy_ycbcr_buffer(
      android_ycbcr* dst_addr_ycbcr, void * src_addr);
  virtual int setIspFlash(int CameraFlashMode);
  virtual int getFocusStatus();
  virtual int setFocusInit();
  virtual int setIspFocus(int cameraFocusMode);
  virtual int setIspCrop(cam_crop_rect_t camCropRect);
  virtual int setIspFocusRegions(cam_rect_t cam_regions);
 
 protected:
  int isBlobFlag;
  bool initialized;
  std::shared_ptr<V4L2Stream> stream_;
  StreamManager* manager_;
  std::condition_variable requests_available_;
  bool mStreamOn;
  CameraMetadata*  mMetadata;
  std::unique_ptr<CameraMetadata> mStaticMetadata;
 
  uint32_t mWidth;
  uint32_t mHeight;
  int      mFormat;
  uint32_t mUsage;
  std::string mStreamType;
 
  typedef struct frame_bufferHandle_map_t{
      uint32_t    frameNum;
      buffer_handle_t*     bufferHandleptr;
  } frame_bufferHandle_map_t;
 
  std::mutex yuv_buffer_queue_lock_;
  std::queue<frame_bufferHandle_map_t> frameNumber_buffers_map_queue_;
  std::condition_variable yuv_buffer_availabl_queue_;
  std::mutex blob_buffer_queue_lock_;
  std::queue<frame_bufferHandle_map_t> blob_frameNumber_buffers_map_queue_;
  std::condition_variable blob_buffer_availabl_queue_;
 
 private:
  DISALLOW_COPY_AND_ASSIGN(CameraStream);
};
 
class CameraMainStream : public CameraStream {
 public:
  CameraMainStream(std::shared_ptr<V4L2Stream> stream, int isBlob);
 
  ~CameraMainStream();
  int start();
  int stop();
  int flush();
  int initialize(uint32_t width,
                 uint32_t height,
                 int      format,
                 uint32_t usage,
                 bool     mergeStreamFlag);
  int setFormat(uint32_t width, uint32_t height, int format, uint32_t usage);
  int getBuffer(buffer_handle_t ** buffer, uint32_t* frameNumber);
  int waitBuffer();
  int request(
      buffer_handle_t* buffer, uint32_t frameNumber, CameraMetadata* metadata);
  int dequeueBuffer(void** src_addr, struct timeval* ts);
  int enqueueBuffer();
  int encodebuffer(
      void* dst_addr, void* src_addr, unsigned long mJpegBufferSizes);
  int copy_ycbcr_buffer(android_ycbcr* dst_addr_ycbcr, void* src_addr);
  int setIspFlash(int CameraFlashMode);
  int getFocusStatus();
  int setFocusInit();
  int setIspFocus(int cameraFocusMode);
  int setIspCrop(cam_crop_rect_t camCropRect);
  int setIspFocusRegions(cam_rect_t cam_regions);
 
 private:
  DISALLOW_COPY_AND_ASSIGN(CameraMainStream);
};
 
class CameraSubStream : public CameraStream {
 public:
  CameraSubStream(std::shared_ptr<V4L2Stream> stream, int isBlob);
  int start();
  int stop();
  int flush();
  int initialize(uint32_t width,
                 uint32_t height,
                 int      format,
                 uint32_t usage,
                 bool     mergeStreamFlag);
  int setFormat(uint32_t width, uint32_t height, int format, uint32_t usage);
  int getBuffer(buffer_handle_t** buffer, uint32_t* frameNumber);
  int waitBuffer();
  int request(
      buffer_handle_t* buffer, uint32_t frameNumber, CameraMetadata* metadata);
  int dequeueBuffer(void** src_addr, struct timeval* ts);
  int enqueueBuffer();
  int encodebuffer(
      void* dst_addr, void* src_addr, unsigned long mJpegBufferSizes);
  int copy_ycbcr_buffer(android_ycbcr* dst_addr_ycbcr, void* src_addr);
  int setIspFlash(int CameraFlashMode);
  int getFocusStatus();
  int setFocusInit();
  int setIspFocus(int cameraFocusMode);
  int setIspCrop(cam_crop_rect_t camCropRect);
  int setIspFocusRegions(cam_rect_t cam_regions);
 
  ~CameraSubStream();
 
 private:
  DISALLOW_COPY_AND_ASSIGN(CameraSubStream);
};
 
class CameraMainMirrorStream : public CameraStream {
 public:
  CameraMainMirrorStream(std::shared_ptr<V4L2Stream> stream, int isBlob);
  ~CameraMainMirrorStream();
  int start();
  int stop();
  int flush();
  int initialize(uint32_t width,
                 uint32_t height,
                 int      format,
                 uint32_t usage,
                 bool     mergeStreamFlag);
  int setFormat(uint32_t width, uint32_t height, int format, uint32_t usage);
  int getBuffer(buffer_handle_t** buffer, uint32_t* frameNumber);
  int waitBuffer();
  int request(
      buffer_handle_t* buffer, uint32_t frameNumber, CameraMetadata* metadata);
  int dequeueBuffer(void** src_addr, struct timeval* ts);
  int enqueueBuffer();
  int encodebuffer(
      void* dst_addr, void* src_addr, unsigned long mJpegBufferSizes);
  int copy_ycbcr_buffer(android_ycbcr* dst_addr_ycbcr, void* src_addr);
  int setIspFlash(int CameraFlashMode);
  int getFocusStatus();
  int setFocusInit();
  int setIspFocus(int cameraFocusMode);
  int setIspCrop(cam_crop_rect_t camCropRect);
  int setIspFocusRegions(cam_rect_t cam_regions);
 
  DISALLOW_COPY_AND_ASSIGN(CameraMainMirrorStream);
};
 
class CameraSubMirrorStream : public CameraStream {
 public:
  CameraSubMirrorStream(std::shared_ptr<V4L2Stream> stream, int isBlob);
  int start();
  int stop();
  int flush();
  int initialize(uint32_t width,
                 uint32_t height,
                 int      format,
                 uint32_t usage,
                 bool     mergeStreamFlag);
  int setScaleFlag();
  int setFormat(uint32_t width, uint32_t height, int format, uint32_t usage);
  int getBuffer(buffer_handle_t** buffer, uint32_t* frameNumber);
  int waitBuffer();
  int request(
      buffer_handle_t* buffer, uint32_t frameNumber, CameraMetadata* metadata);
  int dequeueBuffer(void** src_addr, struct timeval* ts);
  int enqueueBuffer();
  int encodebuffer(
      void * dst_addr, void * src_addr, unsigned long mJpegBufferSizes);
  int copy_ycbcr_buffer(android_ycbcr* dst_addr_ycbcr, void * src_addr);
  int setIspFlash(int CameraFlashMode);
  int getFocusStatus();
  int setFocusInit();
  int setIspFocus(int cameraFocusMode);
  int setIspCrop(cam_crop_rect_t camCropRect);
  int setIspFocusRegions(cam_rect_t cam_regions);
 
  ~CameraSubMirrorStream();
 
 private:
  int yuv420spDownScale(void* psrc, android_ycbcr* dst_addr_ycbcr,
          int src_w, int src_h, int dst_w, int dst_h);
 
  bool mScaleFlag;
 
  DISALLOW_COPY_AND_ASSIGN(CameraSubMirrorStream);
};
 
}  // namespace v4l2_camera_hal
 
#endif  // V4L2_CAMERA_HAL_V4L2_CAMERA_H_