hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
/*
 * Copyright (C) 2019 Rockchip Electronics Co., Ltd.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL), available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
 
#ifndef __UVC_IPC_H__
#define __UVC_IPC_H__
 
#ifdef __cplusplus
extern "C" {
#endif
#include "uvc_ipc_ext.h"
#include <stddef.h>
#include <pthread.h>
 
extern void uvc_read_camera_buffer(void *cam_buf, struct MPP_ENC_INFO *info,
                                   void *extra_data, size_t extra_size);
#ifdef __cplusplus
}
#endif
 
#include <shmc/shm_queue.h>
#include <stdint.h>
#include "uvc_data.pb.h"
#include <thread>
#include <vector>
#include <mutex>
#include <map>
#include <string>
using namespace std;
 
 
//#include "rockit/RTMediaBuffer.h"
 
namespace
{
constexpr const char *kShmUVCWriteKey  = "0x20002";//"0x10022";
constexpr const char *kShmUVCReadKey   = "0x20001";//"0x10021";
constexpr size_t      kQueueBufSize    = 1024 * 1024 * 0.5;
} // namespace
using namespace shmc;
//typedef void* (*UVCBufferRecvCB)(RTMediaBuffer *);
 
#if DBUG
#define UVC_IPC_DYNAMIC_DEBUG_ON 1 //release version can set to 0
#else
#define UVC_IPC_DYNAMIC_DEBUG_ON 0
#endif
 
#define UVC_IPC_DYNAMIC_DEBUG_FPS "/tmp/uvc_ipc_fps"
#define UVC_IPC_DYNAMIC_DEBUG_STATE "/tmp/uvc_ipc_state"
#define UVC_IPC_DYNAMIC_DEBUG_EPTZ "/tmp/uvc_ipc_eptz"
#define UVC_IPC_DYNAMIC_DEBUG_ISP_FPS "/tmp/uvc_isp_fps"
#define UVC_IPC_DYNAMIC_DEBUG_ISP_STATE "/tmp/uvc_isp_state"
 
#define UVC_SENDBUFF_USE_INIT_ALLOC 1
#define UVC_STREAM_OFF_NOT_SEND_SETTING 1
#define UVC_ABANDON_FRM_COUNT 0
 
namespace ShmControl
{
 
enum ShmUVCMessageType
{
    MSG_UVC_START   = 1,
    MSG_UVC_STOP    = 2,
    MSG_UVC_ENABLE_ETPTZ  = 3,
    MSG_UVC_SET_ZOOM      = 4,
    MSG_UVC_TRANSPORT_BUF = 5,
    MSG_UVC_CONFIG_CAMERA = 6,
    MSG_UVC_SET_EPTZ_PAN = 7,
    MSG_UVC_SET_EPTZ_TILT = 8,
    MSG_UVC_ENABLE_BYPASS  = 9,
};
 
enum UVC_IPC_RECV_STATE
{
    UVC_IPC_STATE_RECV_ENTER = 1,
    UVC_IPC_STATE_RECV_ENC_ENTER = 2,
    UVC_IPC_STATE_RECV_ENC_EXIT  = 3,
    UVC_IPC_STATE_RECV_EXIT = 4,
};
enum UVC_IPC_SEND_STATE
{
    UVC_IPC_STATE_SEND_ENTER = 1,
    UVC_IPC_STATE_SEND_ENTER_UNLOCK = 2,
    UVC_IPC_STATE_SEND_READY = 3,
    UVC_IPC_STATE_SEND_EXIT = 4,
};
 
string shm_meg_name[] = {
    "uvc_start",
    "uvc_stop",
    "uvc_enable_eptz",
    "uvc_set_zoom",
    "uvc_buf",
    "uvc_cfg_camera",
    "uvc_set_eptz",
};
 
struct SendBufferInfo
{
    int32_t id;
    int32_t size;
    int32_t fd;
    int32_t handle;
    int64_t data;
    int64_t priv_data;
    int32_t seq;
};
 
struct DrmBuffMap
{
    char *buf;
    int32_t id;
//    int32_t fd;
    int32_t size;
    int32_t handle;
    int buf_fd;
};
 
class ShmUVCController
{
public:
    ShmUVCController();
    ~ShmUVCController();
 
    void ShmUVCDrmRelease();
    void sendUVCBuffer(enum ShmUVCMessageType event, void *data);
    void startRecvMessage();
    void clearRecvMessage();
    void stopRecvMessage();
    void recvUVCMessageLoop();
#if UVC_IPC_DYNAMIC_DEBUG_ON
    void uvcIPCDebugLoop();
#endif
private:
    void initialize();
    void handleUVCMessage(std::string &msg);
    void recvUVCBuffer(MediaBufferInfo *bufferInfo);
 
private:
    ShmQueue<shmc::SVIPC> shmWriteQueue;
    ShmQueue<shmc::SVIPC> shmWriteReadQueue;
    ShmQueue<shmc::SVIPC> shmReadQueue;
    std::mutex            wQueueMtx;
    std::mutex            rQueueMtx;
    std::thread          *recvThread;
#if UVC_IPC_DYNAMIC_DEBUG_ON
    std::thread          *debugThread;
    bool                 debugLooping;
#endif
    map<int, DrmBuffMap>  drm_info;
    bool                  recvLooping;
    int32_t               drmFd;
    int32_t               width;
    int32_t               height;
    int32_t               nv12_size;
    int32_t               frm;
    int32_t               isp_frm;
    float                 fps;
#if UVC_IPC_DYNAMIC_DEBUG_ON
    struct timeval        ipc_enter_time;
    struct timeval        isp_enter_time;
#endif
    UVCMessage            send_message;
    UVC_IPC_SEND_STATE    send_state;
    UVC_IPC_RECV_STATE    recv_state;
    int32_t               norecv_count;
    int32_t               norecv_err_count;
    int32_t               abandon_count;
    enum UVC_IPC_ENC_TYPE encode_type;
    int32_t               recv_seq;
    int32_t               send_seq;
    int32_t               recv_count;
    int32_t               send_count;
    int32_t               isp_buf_count;
    int32_t               set_buf_count;
    int32_t               uvc_fps_set;
};
 
struct UVC_IPC_INFO
{
    ShmUVCController *shm_control;
    pthread_mutex_t mutex;
    bool start;
    bool stop;
    bool history[4];
    int enable_eptz;
    int zoom;
    int eptz_pan;
    int eptz_tilt;
    struct CAMERA_INFO camera;
};
 
} // namespace ShmControl
 
#endif