Cam_FrameCatch.c
5.2 KB
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include "JZsdkLib.h"
#include "BaseConfig.h"
#include "../V4L2_camera/V4L2_Record.h"
#include "../V4L2_camera/V4L2_CameraParameterSetting.h"
#include "../../MultProc/MultProc.h"
#include "../../MediaParm.h"
#include "../../VideoMgmt/VideoMgmt.h"
#include "../Camera.h"
static int CameraFd = 0;
//临时方案 后续会改写法合并到t_JZsdk_TaskFuntionInput(搞起来有点麻烦)
typedef struct t_FrameCatch_TaskFuntionInput
{
void (*task_function)(void*); //任务函数指针,用于指定 执行的任务
void* data; //数据指针
unsigned int data_size; //数据大小
} t_FrameCatch_TaskFuntionInput;
//多线程抓取数据线程
static void *JZsdk_Catch_MultiThread(void *args)
{
while (1)
{
unsigned int buf_size = 0;
unsigned char *buf = NULL;
//从相机中读取一张照片
V4L2_CameraFrameRecord_OnlyGetFrame(&buf, &buf_size);
//printf("read: len:%d data[3]:%x data[4]:%x\n", buf_size, buf[3], buf[4]);
if (buf == NULL)
{
JZSDK_LOG_ERROR("相机数据读取失败");
continue;
}
//放入缓冲池 //将数据放入缓冲池,从而被其他线程使用
VideoMgmt_write_data(&VideoMgmt_FirstVideo_index, buf, buf_size);
//归还图片
V4L2_CameraFrameRecord_OnlyReturnFrame();
}
}
/******************************
*
* 相机抓取初始化
* ThreadMode: 0为单线程 1为多线程
*
* ******************************/
T_JZsdkReturnCode JZsdk_FrameCatch_Init(int ThreadMode)
{
T_JZsdkReturnCode ret;
//初始化数据接收线程
pthread_t ReadDataTask;
pthread_attr_t task_attribute; //线程属性
pthread_attr_init(&task_attribute); //初始化线程属性
pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED); //设置线程分离属性
if (ThreadMode == 0)
{
int opus_Protection = pthread_create(&ReadDataTask,&task_attribute,JZsdk_Catch_SingleThread,NULL); //线程
if(opus_Protection != 0)
{
JZSDK_LOG_ERROR("创建相机抓取并处理初始化线程失败!");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
}
else if (ThreadMode == 1)
{
int opus_Protection = pthread_create(&ReadDataTask,&task_attribute,JZsdk_Catch_MultiThread,NULL); //线程
if(opus_Protection != 0)
{
JZSDK_LOG_ERROR("创建相机抓取并处理初始化线程失败!");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
}
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
//单线程抓取数据线程
static void *JZsdk_Catch_SingleThread2(void *args)
{
t_FrameCatch_TaskFuntionInput *task = (t_FrameCatch_TaskFuntionInput *)args;
while (1)
{
task->task_function();
unsigned int buf_size = 0;
unsigned char *buf = NULL;
//从相机中读取一张照片
V4L2_CameraFrameRecord_OnlyGetFrame(&buf, &buf_size);
//printf("read: len:%d data[3]:%x data[4]:%x\n", buf_size, buf[3], buf[4]);
if (buf == NULL)
{
JZSDK_LOG_ERROR("相机数据读取失败");
continue;
}
//进行数据处理
VideoMgmt_Single_FrameIn(buf, buf_size);
task->task_function();
//归还图片
V4L2_CameraFrameRecord_OnlyReturnFrame();
}
}
/********************************************
*
*
* 相机抓取初始化
*
* 传入线程的抓取模式 ThreadMode: 0为单线程 1为多线程
* 传入线程的处理函数 task_function
*
* *****************************************/
T_JZsdkReturnCode JZsdk_FrameCatch_Init2(int ThreadMode, T_JZsdkReturnCode (*task_function)(void*))
{
T_JZsdkReturnCode ret;
//初始化数据接收线程
pthread_t ReadDataTask;
pthread_attr_t task_attribute; //线程属性
pthread_attr_init(&task_attribute); //初始化线程属性
pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED); //设置线程分离属性
t_FrameCatch_TaskFuntionInput *task = (t_FrameCatch_TaskFuntionInput*)malloc(sizeof(t_FrameCatch_TaskFuntionInput));
if (task == NULL)
{
// 处理内存分配失败的情况
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
task->task_function = task_function;
task->args = NULL;
if (ThreadMode == 0)
{
int opus_Protection = pthread_create(&ReadDataTask,&task_attribute,JZsdk_Catch_SingleThread,(void *)task); //线程
if(opus_Protection != 0)
{
JZSDK_LOG_ERROR("创建相机抓取并处理初始化线程失败!");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
}
else if (ThreadMode == 1)
{
int opus_Protection = pthread_create(&ReadDataTask,&task_attribute,JZsdk_Catch_MultiThread,(void *)task); //线程
if(opus_Protection != 0)
{
JZSDK_LOG_ERROR("创建相机抓取并处理初始化线程失败!");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
}
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}