-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_reader.cpp
executable file
·253 lines (242 loc) · 6.31 KB
/
data_reader.cpp
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
#include "data_reader.h"
#include "Units.h"
#include "video_manger.h"
extern video_manger * g_vm;
extern sql_manger * g_sm;
data_reader::data_reader(void)
{
m_fp = NULL;
m_blast = false;
m_endpos = 0;
m_video_id = 0;
m_first_len = 0;
m_reader_len = 0;
}
data_reader::~data_reader(void)
{
}
bool data_reader::init_reader(unsigned int video_id,time_t read_st)
{
m_st = read_st;
m_et = 0;
m_file_list.clear();
m_video_id = video_id;
m_offmap.clear();
int size = sizeof(unsigned int)+sizeof(unsigned int)+sizeof(unsigned int)+MAX_FILE_INDEX*(sizeof(time_t)+sizeof(unsigned int));
g_sm->get_track_video_list_sql(video_id,m_file_list,read_st);
if(m_file_list.size())
{
int offset = 0;
file_list::iterator item = m_file_list.begin();
m_fp = fopen((*item).file_path.c_str(),"rb");
if(m_fp)
{
m_first_len = (*item).file_lenth;
unsigned char *header = (unsigned char *)malloc(size);
fread(header,size,1,m_fp);
for(int i=0;i<MAX_FILE_INDEX;i++)
{
time_t t =ReadInt64(header+12+i*(sizeof(time_t)+sizeof(int)));
//cout<<"read time "<<t<<endl;
if(t==0)
{
break;
}
offset = ReadInt32(header+12+i*(sizeof(time_t)+sizeof(int))+sizeof(time_t));
//cout<<"read offset"<<offset<<endl;
if(t==read_st)
{
fseek(m_fp,offset,0);
cout<<"初始化reader 开始时间"<<read_st<<"的偏移位置为"<<offset<<endl;
break;
}
}
free(header);
}
//cout<<"初始化reader "<<read_st<<"的偏移位置为"<<offset<<endl;
m_offsetpos = offset?offset:size;
m_file_list.pop_front();
return true;
}
return false;
}
bool data_reader::init_reader(unsigned int video_id,time_t read_st,time_t read_et)
{
m_st = read_st;
m_et = read_et;
m_video_id = video_id;
m_file_list.clear();
int size = sizeof(unsigned int)+sizeof(unsigned int)+sizeof(unsigned int)+MAX_FILE_INDEX*(sizeof(time_t)+sizeof(unsigned int));
g_sm->get_track_video_list_sql(video_id,m_file_list,read_st,read_et);
cout<<"录像回放文件块总数"<<m_file_list.size()<<endl;
if(m_file_list.size())
{
int offset = 0;
file_list::iterator item = m_file_list.begin();
cout<<"打开文件"<<(*item).file_path.c_str()<<endl;
m_fp = fopen((*item).file_path.c_str(),"rb");
if(m_fp)
{
m_reader_len = 0;
for(item=m_file_list.begin();item!=m_file_list.end();item++)
{
if(read_st>(*item).file_st&&read_st<=(*item).file_et)
{
int len = (*item).file_lenth*((*item).file_et- read_st)/((*item).file_et-(*item).file_st);
m_reader_len+=len;
}
else if(read_et>=(*item).file_st&&read_et<(*item).file_et)
{
int len = (*item).file_lenth*(read_et - (*item).file_st)/((*item).file_et-(*item).file_st);
m_reader_len+=len;
}
else
{
m_reader_len+=(*item).file_lenth;
}
//cout<<"文件名"<<(*item).file_path.c_str()<<endl;
//cout<<"累计录像段长度"<<m_reader_len<<endl;
}
unsigned char *header = (unsigned char *)malloc(size);
fread(header,size,1,m_fp);
for(int i=0;i<MAX_FILE_INDEX;i++)
{
time_t t =ReadInt64(header+12+i*(sizeof(time_t)+sizeof(int)));
if(t==0)
{
cout<<"时间读取值为0"<<endl;
break;
}
if(t>read_st)
{
cout<<"时间读取值为"<<t<<endl;
break;
}
//struct tm *st_tb;
//st_tb = localtime(&t);
//char timestr[128];
//sprintf(timestr,"%4d-%02d-%02dT%02d:%02d:%02d",st_tb->tm_year+1900,st_tb->tm_mon+1,st_tb->tm_mday,st_tb->tm_hour,st_tb->tm_min,st_tb->tm_sec);
//cout<<"时间索引值"<<timestr<<"--"<<t<<"---"<<read_st<<endl;
offset = ReadInt32(header+12+i*(sizeof(time_t)+sizeof(int))+sizeof(time_t));
if(t==read_st)
{
cout<<"找到起始索引位置"<<offset<<endl;
fseek(m_fp,offset,0);
if(m_file_list.size()!=1)
{
m_offsetpos = offset;
//m_reader_len = m_reader_len -m_offsetpos;
m_file_list.pop_front();
cout<<"1初始化reader "<<read_st<<"的偏移位置为"<<offset<<endl;
return true;
}
}
if(t==read_et)
{
cout<<"找到结束索引位置"<<offset<<endl;
m_endpos = offset;
m_blast = true;
}
}
free(header);
}
else
{
cout<<"文件打开失败"<<errno<<endl;
}
m_offsetpos = offset?offset:size;
//m_reader_len = m_reader_len-m_offsetpos;
m_file_list.pop_front();
cout<<"2初始化reader "<<read_st<<"的偏移位置为"<<m_offsetpos<<"总长度"<<m_reader_len<<endl;
return true;
}
return false;
}
uint64_t data_reader::get_reader_length()
{
//cout<<"当前录像总长度"<<m_reader_len<<endl;
return m_reader_len;
}
int data_reader::getbuf(char *buf,unsigned int len)
{
int leng = 0;
if(m_fp)
{
leng = fread(buf,1,len,m_fp);
//cout<<"读取文件长度"<<len<<endl;
if(leng<len&&leng>0)
{
fclose(m_fp);
m_fp = NULL;
m_offsetpos = 0;
if (m_file_list.size())
{
file_list::iterator item = m_file_list.begin();
m_fp = fopen((*item).file_path.c_str(),"rb");
int size = sizeof(unsigned int)+sizeof(unsigned int)+sizeof(unsigned int)+MAX_FILE_INDEX*(sizeof(time_t)+sizeof(unsigned int));
if(m_file_list.size()==1&&m_et!=0)
{
m_blast = true;
unsigned char *header = (unsigned char *)malloc(size);
fread(header,size,1,m_fp);
time_t ct =ReadInt64(header+12+(sizeof(time_t)+sizeof(int)));
if(ct==0)
{
//文件块正在被使用
memset(header,0,size);
g_vm->get_idx_buffer(m_video_id,(char *)header,size);
}
for(int i=0;i<MAX_FILE_INDEX;i++)
{
time_t t =ReadInt64(header+12+i*(sizeof(time_t)+sizeof(int)));
if(t==0)
{
break;
}
int offset = ReadInt32(header+12+i*(sizeof(time_t)+sizeof(int))+sizeof(time_t));
if(t==m_et)
{
m_endpos = offset;
}
}
free(header);
}
fseek(m_fp,size,0);
m_offsetpos = size;
m_file_list.pop_front();
}
}
else if(leng<=0)
{
}
else
{
m_offsetpos+=leng;
if(m_blast&&m_offsetpos>m_endpos)
{
fclose(m_fp);
m_fp = NULL;
m_offsetpos = 0;
}
}
return leng;
}
else
{
return 0;
}
return 0;
}
bool data_reader::uninit_reader()
{
if(m_fp)
{
fclose(m_fp);
m_fp = NULL;
}
return false;
}
int data_reader::getesbuf(char *buf,unsigned int len,/*[out]*/ int *ntype)
{
return 0;
}