blob: 96adfc4cb130f6cdc6aaf55002ad34a6aca04d1f [file] [log] [blame]
Pengfei Liuc181a982020-01-07 19:27:13 +08001#include <stdio.h>
Pengfei Liub038b6a2020-01-14 15:57:01 +08002#include <unistd.h>
Pengfei Liuc181a982020-01-07 19:27:13 +08003#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <string.h>
Pengfei Liub038b6a2020-01-14 15:57:01 +08007#include <stdlib.h>
Pengfei Liuc181a982020-01-07 19:27:13 +08008#include <errno.h>
Pengfei Liu47ed6c92020-01-17 11:23:41 +08009#include "dvr_types.h"
Pengfei Liuc181a982020-01-07 19:27:13 +080010#include "segment.h"
11
12#define MAX_SEGMENT_FD_COUNT (128)
13#define MAX_SEGMENT_PATH_SIZE (DVR_MAX_LOCATION_SIZE + 32)
Pengfei Liub038b6a2020-01-14 15:57:01 +080014#define MAX_PTS_THRESHOLD (10*1000)
hualing chen06209692020-11-05 14:51:46 +080015#define PCR_RECORD_INTERVAL_MS (300)
hualing chen2932d372020-04-29 13:44:00 +080016#define PTS_DISCONTINE_DEVIATION (40)
17#define PTS_HEAD_DEVIATION (40)
18
19
Pengfei Liub4734232020-01-17 18:25:10 +080020/**\brief Segment context*/
Pengfei Liuc181a982020-01-07 19:27:13 +080021typedef struct {
Pengfei Liu3b1a8202020-02-12 23:04:21 +080022 int ts_fd; /**< Segment ts file fd*/
Pengfei Liub4734232020-01-17 18:25:10 +080023 FILE *index_fp; /**< Time index file fd*/
24 FILE *dat_fp; /**< Information file fd*/
pengfei.liu567d6d82020-04-17 16:48:59 +080025 FILE *ongoing_fp; /**< Ongoing file fd, used to verify timedhift mode*/
Pengfei Liub4734232020-01-17 18:25:10 +080026 uint64_t first_pts; /**< First pts value, use for write mode*/
pengfei.liu567d6d82020-04-17 16:48:59 +080027 uint64_t last_pts; /**< Last input pts value, use for write mode*/
28 uint64_t last_record_pts; /**< Last record pts value, use for write mode*/
pengfei.liuab5a2262020-02-14 17:33:40 +080029 uint64_t cur_time; /**< Current time save in index file */
pengfei.liu567d6d82020-04-17 16:48:59 +080030 uint64_t segment_id; /**< Current segment ID */
31 char location[MAX_SEGMENT_PATH_SIZE]; /**< Current time save in index file */
Pengfei Liuc181a982020-01-07 19:27:13 +080032} Segment_Context_t;
33
Pengfei Liub4734232020-01-17 18:25:10 +080034/**\brief Segment file type*/
Pengfei Liuc181a982020-01-07 19:27:13 +080035typedef enum {
Pengfei Liub4734232020-01-17 18:25:10 +080036 SEGMENT_FILE_TYPE_TS, /**< Used for store TS data*/
37 SEGMENT_FILE_TYPE_INDEX, /**< Used for store index data*/
38 SEGMENT_FILE_TYPE_DAT, /**< Used for store information data, such as duration etc*/
pengfei.liu567d6d82020-04-17 16:48:59 +080039 SEGMENT_FILE_TYPE_ONGOING, /**< Used for store information data, such as duration etc*/
Pengfei Liuc181a982020-01-07 19:27:13 +080040} Segment_FileType_t;
41
42static void segment_get_fname(char fname[MAX_SEGMENT_PATH_SIZE],
Pengfei Liub4734232020-01-17 18:25:10 +080043 const char location[DVR_MAX_LOCATION_SIZE],
Pengfei Liuc181a982020-01-07 19:27:13 +080044 uint64_t segment_id,
45 Segment_FileType_t type)
46{
47 int offset;
48
49 memset(fname, 0, MAX_SEGMENT_PATH_SIZE);
50 strncpy(fname, location, strlen(location));
51 offset = strlen(location);
52 strncpy(fname + offset, "-", 1);
53 offset += 1;
54 sprintf(fname + offset, "%04llu", segment_id);
55 offset += 4;
56 if (type == SEGMENT_FILE_TYPE_TS)
57 strncpy(fname + offset, ".ts", 3);
58 else if (type == SEGMENT_FILE_TYPE_INDEX)
59 strncpy(fname + offset, ".idx", 4);
Pengfei Liub4734232020-01-17 18:25:10 +080060 else if (type == SEGMENT_FILE_TYPE_DAT)
61 strncpy(fname + offset, ".dat", 4);
hualing chen87072a82020-03-12 16:20:12 +080062 else if (type == SEGMENT_FILE_TYPE_ONGOING)
63 strncpy(fname + offset, ".going", 6);
Pengfei Liuc181a982020-01-07 19:27:13 +080064}
65
Pengfei Liu3b1a8202020-02-12 23:04:21 +080066static void segment_get_dirname(char dir_name[MAX_SEGMENT_PATH_SIZE],
67 const char location[DVR_MAX_LOCATION_SIZE])
68{
69 char *p;
70 int i;
71 int found = 0;
72
73 for (i = 0; i < (int)strlen(location); i++) {
74 if (location[i] == '/') {
75 p = (char *)location + i;
76 found = 1;
77 }
78 }
79 if (found)
80 memcpy(dir_name, location, p - location);
81}
82
Pengfei Liuc181a982020-01-07 19:27:13 +080083int segment_open(Segment_OpenParams_t *params, Segment_Handle_t *p_handle)
84{
85 Segment_Context_t *p_ctx;
86 char ts_fname[MAX_SEGMENT_PATH_SIZE];
87 char index_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liub4734232020-01-17 18:25:10 +080088 char dat_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liu3b1a8202020-02-12 23:04:21 +080089 char dir_name[MAX_SEGMENT_PATH_SIZE];
hualing chen87072a82020-03-12 16:20:12 +080090 char going_name[MAX_SEGMENT_PATH_SIZE];
Pengfei Liuc181a982020-01-07 19:27:13 +080091
Pengfei Liu3b1a8202020-02-12 23:04:21 +080092 DVR_RETURN_IF_FALSE(params);
93 DVR_RETURN_IF_FALSE(p_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +080094
hualing chena540a7e2020-03-27 16:44:05 +080095 //DVR_DEBUG(1, "%s, location:%s, id:%llu", __func__, params->location, params->segment_id);
Pengfei Liuc181a982020-01-07 19:27:13 +080096
Pengfei Liub038b6a2020-01-14 15:57:01 +080097 p_ctx = (void*)malloc(sizeof(Segment_Context_t));
Pengfei Liu3b1a8202020-02-12 23:04:21 +080098 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +080099 memset(p_ctx, 0, sizeof(Segment_Context_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800100
101 memset(ts_fname, 0, sizeof(ts_fname));
102 segment_get_fname(ts_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_TS);
103
104 memset(index_fname, 0, sizeof(index_fname));
105 segment_get_fname(index_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_INDEX);
106
Pengfei Liub4734232020-01-17 18:25:10 +0800107 memset(dat_fname, 0, sizeof(dat_fname));
108 segment_get_fname(dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_DAT);
109
hualing chen87072a82020-03-12 16:20:12 +0800110 memset(going_name, 0, sizeof(going_name));
111 segment_get_fname(going_name, params->location, params->segment_id, SEGMENT_FILE_TYPE_ONGOING);
112
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800113 memset(dir_name, 0, sizeof(dir_name));
114 segment_get_dirname(dir_name, params->location);
115 if (access(dir_name, F_OK) == -1) {
116 DVR_DEBUG(1, "%s dir %s is not exist, create it", __func__, dir_name);
117 mkdir(dir_name, 0666);
118 }
119
Pengfei Liuc181a982020-01-07 19:27:13 +0800120 if (params->mode == SEGMENT_MODE_READ) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800121 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800122 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800123 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800124 p_ctx->ongoing_fp = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800125 } else if (params->mode == SEGMENT_MODE_WRITE) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800126 p_ctx->ts_fd = open(ts_fname, O_CREAT | O_RDWR | O_TRUNC, 0644);
Pengfei Liuc181a982020-01-07 19:27:13 +0800127 p_ctx->index_fp = fopen(index_fname, "w+");
Pengfei Liub4734232020-01-17 18:25:10 +0800128 p_ctx->dat_fp = fopen(dat_fname, "w+");
hualing chen87072a82020-03-12 16:20:12 +0800129 p_ctx->ongoing_fp = fopen(going_name, "w+");
Pengfei Liub038b6a2020-01-14 15:57:01 +0800130 p_ctx->first_pts = ULLONG_MAX;
131 p_ctx->last_pts = ULLONG_MAX;
pengfei.liu567d6d82020-04-17 16:48:59 +0800132 p_ctx->last_record_pts = ULLONG_MAX;
Pengfei Liuc181a982020-01-07 19:27:13 +0800133 } else {
134 DVR_DEBUG(1, "%s, unknow mode use default", __func__);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800135 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800136 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800137 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800138 p_ctx->ongoing_fp = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800139 }
140
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800141 if (p_ctx->ts_fd == -1 || !p_ctx->index_fp || !p_ctx->dat_fp) {
Pengfei Liub4734232020-01-17 18:25:10 +0800142 DVR_DEBUG(1, "%s open file failed [%s, %s, %s], reason:%s", __func__,
143 ts_fname, index_fname, dat_fname, strerror(errno));
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800144 if (p_ctx->ts_fd != -1)
145 close(p_ctx->ts_fd);
146 if (p_ctx->index_fp)
147 fclose(p_ctx->index_fp);
148 if (p_ctx->dat_fp)
149 fclose(p_ctx->dat_fp);
150 if (p_ctx->ongoing_fp)
151 fclose(p_ctx->ongoing_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800152 free(p_ctx);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800153 *p_handle = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800154 return DVR_FAILURE;
155 }
hualing chen87072a82020-03-12 16:20:12 +0800156 p_ctx->segment_id = params->segment_id;
157 strncpy(p_ctx->location, params->location, strlen(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800158
hualing chen7a56cba2020-04-14 14:09:27 +0800159 //DVR_DEBUG(1, "%s, open file success p_ctx->location [%s]", __func__, p_ctx->location, params->mode);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800160 *p_handle = (Segment_Handle_t)p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800161 return DVR_SUCCESS;
162}
163
164int segment_close(Segment_Handle_t handle)
165{
166 Segment_Context_t *p_ctx;
167
Pengfei Liub038b6a2020-01-14 15:57:01 +0800168 p_ctx = (void *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800169 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800170
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800171 if (p_ctx->ts_fd != -1) {
172 close(p_ctx->ts_fd);
Pengfei Liuc181a982020-01-07 19:27:13 +0800173 }
174
175 if (p_ctx->index_fp) {
176 fclose(p_ctx->index_fp);
177 }
178
Pengfei Liub4734232020-01-17 18:25:10 +0800179 if (p_ctx->dat_fp) {
180 fclose(p_ctx->dat_fp);
181 }
182
hualing chen87072a82020-03-12 16:20:12 +0800183 if (p_ctx->ongoing_fp != NULL) {
184 fclose(p_ctx->ongoing_fp);
185 char going_name[MAX_SEGMENT_PATH_SIZE];
186 memset(going_name, 0, sizeof(going_name));
187 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
188 DVR_DEBUG(1, "segment close del [%s]", going_name);
189 unlink(going_name);
190 }
191
Pengfei Liuc181a982020-01-07 19:27:13 +0800192 free(p_ctx);
193 return 0;
194}
195
196ssize_t segment_read(Segment_Handle_t handle, void *buf, size_t count)
197{
198 Segment_Context_t *p_ctx;
199
200 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800201 DVR_RETURN_IF_FALSE(p_ctx);
202 DVR_RETURN_IF_FALSE(buf);
203 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800204
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800205 return read(p_ctx->ts_fd, buf, count);
Pengfei Liuc181a982020-01-07 19:27:13 +0800206}
207
208ssize_t segment_write(Segment_Handle_t handle, void *buf, size_t count)
209{
210 Segment_Context_t *p_ctx;
211
212 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800213 DVR_RETURN_IF_FALSE(p_ctx);
214 DVR_RETURN_IF_FALSE(buf);
215 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800216
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800217 return write(p_ctx->ts_fd, buf, count);
Pengfei Liuc181a982020-01-07 19:27:13 +0800218}
219
hualing chen2932d372020-04-29 13:44:00 +0800220int segment_update_pts_force(Segment_Handle_t handle, uint64_t pts, loff_t offset)
Pengfei Liuc181a982020-01-07 19:27:13 +0800221{
222 Segment_Context_t *p_ctx;
223 char buf[256];
pengfei.liu567d6d82020-04-17 16:48:59 +0800224 int record_diff = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800225
226 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800227 DVR_RETURN_IF_FALSE(p_ctx);
228 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800229
Pengfei Liub038b6a2020-01-14 15:57:01 +0800230 if (p_ctx->first_pts == ULLONG_MAX) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800231 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800232 p_ctx->first_pts = pts;
233 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800234 memset(buf, 0, sizeof(buf));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800235 if (p_ctx->last_pts == ULLONG_MAX) {
236 /*Last pts is init value*/
hualing chen2aba4022020-03-02 13:49:55 +0800237 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
pengfei.liuab5a2262020-02-14 17:33:40 +0800238 p_ctx->cur_time = pts - p_ctx->first_pts;
hualing chen2932d372020-04-29 13:44:00 +0800239 DVR_DEBUG(1, "%s force pcr:%llu -1", __func__, pts);
240 } else {
241 /*Last pts has valid value*/
242 int diff = pts - p_ctx->last_pts;
243 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
244 /*Current pts has a transition*/
245 DVR_DEBUG(1, "[%s]force update Current pts has a transition, [%llu, %llu, %llu]",__func__,
246 p_ctx->first_pts, p_ctx->last_pts, pts);
247 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
248 } else {
249 /*This is a normal pts, record it*/
250 p_ctx->cur_time += diff;
251 DVR_DEBUG(1, "%s force pcr:%llu -1 diff [%d]", __func__, pts, diff);
252 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
253 }
254 }
255
256 record_diff = pts - p_ctx->last_record_pts;
257 if (strlen(buf) > 0) {
258 DVR_DEBUG(1, "%s force pcr:%llu buf:%s", __func__, pts, buf);
259 fputs(buf, p_ctx->index_fp);
260 fflush(p_ctx->index_fp);
261 fsync(fileno(p_ctx->index_fp));
262 p_ctx->last_record_pts = pts;
263 }
264 p_ctx->last_pts = pts;
265
266 return DVR_SUCCESS;
267}
268
269int segment_update_pts(Segment_Handle_t handle, uint64_t pts, loff_t offset)
270{
271 Segment_Context_t *p_ctx;
272 char buf[256];
273 int record_diff = 0;
274
275 p_ctx = (Segment_Context_t *)handle;
276 DVR_RETURN_IF_FALSE(p_ctx);
277 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
278
279 if (p_ctx->first_pts == ULLONG_MAX) {
280 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
281 p_ctx->first_pts = pts;
282 //p_ctx->cur_time = p_ctx->cur_time + PTS_HEAD_DEVIATION;
283 }
284 memset(buf, 0, sizeof(buf));
285 if (p_ctx->last_pts == ULLONG_MAX) {
286 /*Last pts is init value*/
287 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
288 p_ctx->cur_time = pts - p_ctx->first_pts;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800289 } else {
290 /*Last pts has valid value*/
pengfei.liu567d6d82020-04-17 16:48:59 +0800291 int diff = pts - p_ctx->last_pts;
hualing chen4b7c15d2020-04-07 16:13:48 +0800292 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
Pengfei Liub038b6a2020-01-14 15:57:01 +0800293 /*Current pts has a transition*/
pengfei.liuab5a2262020-02-14 17:33:40 +0800294 DVR_DEBUG(1, "Current pts has a transition, [%llu, %llu, %llu]",
295 p_ctx->first_pts, p_ctx->last_pts, pts);
pengfei.liu567d6d82020-04-17 16:48:59 +0800296 p_ctx->last_record_pts = pts;
hualing chen2932d372020-04-29 13:44:00 +0800297 //p_ctx->cur_time = p_ctx->cur_time + PTS_DISCONTINE_DEVIATION;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800298 } else {
299 /*This is a normal pts, record it*/
hualing chen4b7c15d2020-04-07 16:13:48 +0800300 p_ctx->cur_time += diff;
hualing chen2aba4022020-03-02 13:49:55 +0800301 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800302 }
303 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800304
305 record_diff = pts - p_ctx->last_record_pts;
306 if (strlen(buf) > 0 &&
307 (record_diff > PCR_RECORD_INTERVAL_MS || p_ctx->last_record_pts == ULLONG_MAX)){
hualing chen4b7c15d2020-04-07 16:13:48 +0800308 fputs(buf, p_ctx->index_fp);
pengfei.liu567d6d82020-04-17 16:48:59 +0800309 fflush(p_ctx->index_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800310 fsync(fileno(p_ctx->index_fp));
pengfei.liu567d6d82020-04-17 16:48:59 +0800311 p_ctx->last_record_pts = pts;
312 }
Pengfei Liub038b6a2020-01-14 15:57:01 +0800313 p_ctx->last_pts = pts;
Pengfei Liub4734232020-01-17 18:25:10 +0800314
Pengfei Liuc181a982020-01-07 19:27:13 +0800315 return DVR_SUCCESS;
316}
317
hualing chen266b9502020-04-04 17:39:39 +0800318loff_t segment_seek(Segment_Handle_t handle, uint64_t time, int block_size)
Pengfei Liuc181a982020-01-07 19:27:13 +0800319{
320 Segment_Context_t *p_ctx;
321 char buf[256];
322 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800323 uint64_t pts = 0L;
324 loff_t offset = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800325 char *p1, *p2;
326
327 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800328 DVR_RETURN_IF_FALSE(p_ctx);
329 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
330 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800331
hualing chen266b9502020-04-04 17:39:39 +0800332 if (time == 0) {
333 offset = 0;
hualing chen92f3a142020-07-08 20:59:33 +0800334 //DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
hualing chen266b9502020-04-04 17:39:39 +0800335 DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1);
336 return offset;
337 }
338
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800339 memset(buf, 0, sizeof(buf));
340 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chen2aba4022020-03-02 13:49:55 +0800341 int line = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800342 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800343 line++;
Pengfei Liuc181a982020-01-07 19:27:13 +0800344 memset(value, 0, sizeof(value));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800345 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800346 p1 += 5;
Pengfei Liuc181a982020-01-07 19:27:13 +0800347 if ((p2 = strstr(buf, ","))) {
348 memcpy(value, p1, p2 - p1);
349 }
350 pts = strtoull(value, NULL, 10);
351 }
352
353 memset(value, 0, sizeof(value));
354 if ((p1 = strstr(buf, "offset="))) {
355 p1 += 7;
356 if ((p2 = strstr(buf, "}"))) {
357 memcpy(value, p1, p2 - p1);
358 }
359 offset = strtoull(value, NULL, 10);
360 }
hualing chen2aba4022020-03-02 13:49:55 +0800361 if (0)
362 {
363 DVR_DEBUG(1, "seek buf[%s]", buf);
364 DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
365 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800366 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800367 //DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
368 if (time <= pts) {
hualing chen266b9502020-04-04 17:39:39 +0800369 if (block_size > 0) {
370 offset = offset - offset%block_size;
371 }
hualing chen2aba4022020-03-02 13:49:55 +0800372 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800373 DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800374 return offset;
375 }
hualing chen2aba4022020-03-02 13:49:55 +0800376 }
hualing chen2932d372020-04-29 13:44:00 +0800377 if (time > pts) {
378 if (block_size > 0) {
379 offset = offset - offset%block_size;
380 }
381 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d end\n", pts, offset, time, line);
382 DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1);
383 return offset;
384 }
hualing chen2aba4022020-03-02 13:49:55 +0800385 DVR_DEBUG(1, "seek error line [%d]", line);
Pengfei Liuc181a982020-01-07 19:27:13 +0800386 return DVR_FAILURE;
387}
388
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800389loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800390{
391 Segment_Context_t *p_ctx;
392
393 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800394 DVR_RETURN_IF_FALSE(p_ctx);
395 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800396
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800397 return lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
398}
399
hualing chen5605eed2020-05-26 18:18:06 +0800400uint64_t segment_tell_position_time(Segment_Handle_t handle, loff_t position)
401{
402 Segment_Context_t *p_ctx;
403 char buf[256];
404 char value[256];
405 uint64_t pts = 0L;
406 loff_t offset = 0;
407 char *p1, *p2;
408
409 p_ctx = (Segment_Context_t *)handle;
410 DVR_RETURN_IF_FALSE(p_ctx);
411 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
412 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
413
414 memset(buf, 0, sizeof(buf));
415 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
416 DVR_RETURN_IF_FALSE(position != -1);
417
418 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
419 memset(value, 0, sizeof(value));
420 if ((p1 = strstr(buf, "time="))) {
421 p1 += 5;
422 if ((p2 = strstr(buf, ","))) {
423 memcpy(value, p1, p2 - p1);
424 }
425 pts = strtoull(value, NULL, 10);
426 }
427
428 memset(value, 0, sizeof(value));
429 if ((p1 = strstr(buf, "offset="))) {
430 p1 += 7;
431 if ((p2 = strstr(buf, "}"))) {
432 memcpy(value, p1, p2 - p1);
433 }
434 offset = strtoull(value, NULL, 10);
435 }
436
437 memset(buf, 0, sizeof(buf));
438 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
439 if (position <= offset) {
440 return pts;
441 }
442 }
443 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
444 return pts;
445}
446
447
pengfei.liu8b563292020-02-26 15:49:02 +0800448uint64_t segment_tell_current_time(Segment_Handle_t handle)
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800449{
450 Segment_Context_t *p_ctx;
451 char buf[256];
452 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800453 uint64_t pts = 0L;
454 loff_t offset = 0, position = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800455 char *p1, *p2;
456
457 p_ctx = (Segment_Context_t *)handle;
458 DVR_RETURN_IF_FALSE(p_ctx);
459 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
460 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
461
462 memset(buf, 0, sizeof(buf));
463 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
464 position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
465 DVR_RETURN_IF_FALSE(position != -1);
466
467 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
468 memset(value, 0, sizeof(value));
469 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800470 p1 += 5;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800471 if ((p2 = strstr(buf, ","))) {
472 memcpy(value, p1, p2 - p1);
473 }
474 pts = strtoull(value, NULL, 10);
475 }
476
477 memset(value, 0, sizeof(value));
478 if ((p1 = strstr(buf, "offset="))) {
479 p1 += 7;
480 if ((p2 = strstr(buf, "}"))) {
481 memcpy(value, p1, p2 - p1);
482 }
483 offset = strtoull(value, NULL, 10);
484 }
485
486 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800487 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800488 if (position <= offset) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800489 return pts;
490 }
491 }
hualing chena540a7e2020-03-27 16:44:05 +0800492 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800493 return pts;
Pengfei Liuc181a982020-01-07 19:27:13 +0800494}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800495
pengfei.liu8b563292020-02-26 15:49:02 +0800496uint64_t segment_tell_total_time(Segment_Handle_t handle)
497{
498 Segment_Context_t *p_ctx;
499 char buf[256];
500 char last_buf[256];
501 char value[256];
502 uint64_t pts = ULLONG_MAX;
503 loff_t offset = 0, position = 0;
504 char *p1, *p2;
505 int line = 0;
506
507 p_ctx = (Segment_Context_t *)handle;
508 DVR_RETURN_IF_FALSE(p_ctx);
509 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
510 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
511
512 memset(buf, 0, sizeof(buf));
513 memset(last_buf, 0, sizeof(last_buf));
514 position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
515 DVR_RETURN_IF_FALSE(position != -1);
516
hualing chen041c4092020-04-05 15:11:50 +0800517 //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1);
518 //if seek error.we need seek 0 pos.
519 if (fseek(p_ctx->index_fp, -1000L, SEEK_END) == -1) {
520 fseek(p_ctx->index_fp, 0L, SEEK_SET);
521 }
pengfei.liu8b563292020-02-26 15:49:02 +0800522 /* Save last line buffer */
523 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800524 if (strlen(buf) <= 0) {
hualing chen87072a82020-03-12 16:20:12 +0800525 DVR_DEBUG(1, "read index buf is len 0");
hualing chen2aba4022020-03-02 13:49:55 +0800526 continue;
527 }
pengfei.liu8b563292020-02-26 15:49:02 +0800528 memset(last_buf, 0, sizeof(last_buf));
529 memcpy(last_buf, buf, strlen(buf));
530 memset(buf, 0, sizeof(buf));
531 line++;
532 }
533
534 /* Extract time value */
535 memset(value, 0, sizeof(value));
536 if ((p1 = strstr(last_buf, "time="))) {
537 p1 += 5;
538 if ((p2 = strstr(last_buf, ","))) {
539 memcpy(value, p1, p2 - p1);
540 }
541 pts = strtoull(value, NULL, 10);
542 }
543
544 memset(value, 0, sizeof(value));
545 if ((p1 = strstr(last_buf, "offset="))) {
546 p1 += 7;
547 if ((p2 = strstr(last_buf, "}"))) {
548 memcpy(value, p1, p2 - p1);
549 }
550 offset = strtoull(value, NULL, 10);
551 }
hualing chen87072a82020-03-12 16:20:12 +0800552 //if (line < 2)
553 //DVR_DEBUG(1, "totle time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line);
pengfei.liu8b563292020-02-26 15:49:02 +0800554 return (pts == ULLONG_MAX ? DVR_FAILURE : pts);
555}
556
pengfei.liuab5a2262020-02-14 17:33:40 +0800557/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800558int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
559{
560 Segment_Context_t *p_ctx;
561 char buf[256];
562 uint32_t i;
563
564 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800565 DVR_RETURN_IF_FALSE(p_ctx);
566 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
567 DVR_RETURN_IF_FALSE(p_info);
hualing chen87072a82020-03-12 16:20:12 +0800568 //seek 0, rewrite info
569 DVR_RETURN_IF_FALSE(fseek(p_ctx->dat_fp, 0, SEEK_SET) != -1);
Pengfei Liub4734232020-01-17 18:25:10 +0800570
571 /*Save segment id*/
572 memset(buf, 0, sizeof(buf));
573 sprintf(buf, "id=%lld\n", p_info->id);
574 fputs(buf, p_ctx->dat_fp);
575
576 /*Save number of pids*/
577 memset(buf, 0, sizeof(buf));
578 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
579 fputs(buf, p_ctx->dat_fp);
580
581 /*Save pid information*/
582 for (i = 0; i < p_info->nb_pids; i++) {
583 memset(buf, 0, sizeof(buf));
584 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
585 fputs(buf, p_ctx->dat_fp);
586 }
587
588 /*Save segment duration*/
589 memset(buf, 0, sizeof(buf));
hualing chen87072a82020-03-12 16:20:12 +0800590 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800591 sprintf(buf, "duration=%ld\n", p_info->duration);
592 fputs(buf, p_ctx->dat_fp);
593
594 /*Save segment size*/
595 memset(buf, 0, sizeof(buf));
596 sprintf(buf, "size=%zu\n", p_info->size);
597 fputs(buf, p_ctx->dat_fp);
598
599 /*Save number of packets*/
600 memset(buf, 0, sizeof(buf));
601 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
602 fputs(buf, p_ctx->dat_fp);
603
604 fflush(p_ctx->dat_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800605 fsync(fileno(p_ctx->dat_fp));
Pengfei Liub4734232020-01-17 18:25:10 +0800606 return DVR_SUCCESS;
607}
608
pengfei.liuab5a2262020-02-14 17:33:40 +0800609/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800610int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
611{
612 Segment_Context_t *p_ctx;
613 uint32_t i;
614 char buf[256];
615 char value[256];
616 char *p1, *p2;
617
618 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800619 DVR_RETURN_IF_FALSE(p_ctx);
620 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800621
622 /*Load segment id*/
623 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800624 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800625 p1 = strstr(buf, "id=");
hualing chen2aba4022020-03-02 13:49:55 +0800626 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800627 p_info->id = strtoull(p1 + 3, NULL, 10);
628
629 /*Save number of pids*/
630 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800631 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800632 p1 = strstr(buf, "nb_pids=");
hualing chen2aba4022020-03-02 13:49:55 +0800633 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800634 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
635
636 /*Save pid information*/
637 for (i = 0; i < p_info->nb_pids; i++) {
638 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800639 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800640 memset(value, 0, sizeof(value));
641 if ((p1 = strstr(buf, "pid="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800642 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800643 p1 += 4;
644 if ((p2 = strstr(buf, ","))) {
hualing chen2aba4022020-03-02 13:49:55 +0800645 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800646 memcpy(value, p1, p2 - p1);
647 }
648 p_info->pids[i].pid = strtoull(value, NULL, 10);
649 }
650
651 memset(value, 0, sizeof(value));
652 if ((p1 = strstr(buf, "type="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800653 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800654 p1 += 5;
655 if ((p2 = strstr(buf, "}"))) {
hualing chen2aba4022020-03-02 13:49:55 +0800656 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800657 memcpy(value, p1, p2 - p1);
658 }
659 p_info->pids[i].type = strtoull(value, NULL, 10);
660 }
661 }
662
663 /*Save segment duration*/
664 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800665 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800666 p1 = strstr(buf, "duration=");
hualing chen2aba4022020-03-02 13:49:55 +0800667 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800668 p_info->duration = strtoull(p1 + 9, NULL, 10);
hualing chena540a7e2020-03-27 16:44:05 +0800669 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800670
671 /*Save segment size*/
672 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800673 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800674 p1 = strstr(buf, "size=");
hualing chen2aba4022020-03-02 13:49:55 +0800675 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800676 p_info->size = strtoull(p1 + 5, NULL, 10);
677
678 /*Save number of packets*/
679 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800680 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800681 p1 = strstr(buf, "nb_packets=");
hualing chen2aba4022020-03-02 13:49:55 +0800682 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800683 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
684
685 return DVR_SUCCESS;
686}
687
688int segment_delete(const char *location, uint64_t segment_id)
689{
690 char fname[MAX_SEGMENT_PATH_SIZE];
691 int ret;
692
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800693 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800694
695 /*delete ts file*/
696 memset(fname, 0, sizeof(fname));
697 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
698 ret = unlink(fname);
699 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800700 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800701
702 /*delete index file*/
703 memset(fname, 0, sizeof(fname));
704 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
705 unlink(fname);
706 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800707 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800708
709 /*delete store information file*/
710 memset(fname, 0, sizeof(fname));
711 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
712 unlink(fname);
713 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800714 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800715
716 return DVR_SUCCESS;
717}
718
hualing chen87072a82020-03-12 16:20:12 +0800719int segment_ongoing(Segment_Handle_t handle)
720{
721 Segment_Context_t *p_ctx;
722 p_ctx = (Segment_Context_t *)handle;
723 struct stat mstat;
724
725 char going_name[MAX_SEGMENT_PATH_SIZE];
726 memset(going_name, 0, sizeof(going_name));
727 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
728 int ret = stat(going_name, &mstat);
729 DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret);
730 if (ret != 0) {
731 return DVR_FAILURE;
732 }
733 return DVR_SUCCESS;
734}
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800735loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800736{
737 Segment_Context_t *p_ctx;
738 char buf[256];
739 char value[256];
740 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800741 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800742 char *p1, *p2;
743
744 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800745 DVR_RETURN_IF_FALSE(p_ctx);
746 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
747 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800748
749 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800750 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800751 printf("start gets pts\n");
752 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
753 printf("buf[%s]\n", buf);
754 memset(value, 0, sizeof(value));
755 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800756 p1 += 5;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800757 if ((p2 = strstr(buf, ","))) {
758 memcpy(value, p1, p2 - p1);
759 }
760 pts = strtoull(value, NULL, 10);
761 }
762
763 memset(value, 0, sizeof(value));
764 if ((p1 = strstr(buf, "offset="))) {
765 p1 += 7;
766 if ((p2 = strstr(buf, "}"))) {
767 memcpy(value, p1, p2 - p1);
768 }
769 offset = strtoull(value, NULL, 10);
770 }
771
772 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800773 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800774 }
775
776 return 0;
777}