blob: c002b0d7f71f7583422fd9aab307852603cb4777 [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)
Pengfei Liub4734232020-01-17 18:25:10 +080015/**\brief Segment context*/
Pengfei Liuc181a982020-01-07 19:27:13 +080016typedef struct {
Pengfei Liu3b1a8202020-02-12 23:04:21 +080017 int ts_fd; /**< Segment ts file fd*/
Pengfei Liub4734232020-01-17 18:25:10 +080018 FILE *index_fp; /**< Time index file fd*/
19 FILE *dat_fp; /**< Information file fd*/
hualing chen87072a82020-03-12 16:20:12 +080020 FILE *ongoing_fp; /**< Ongoing file fd, used to verify timedhift mode*/
Pengfei Liub4734232020-01-17 18:25:10 +080021 uint64_t first_pts; /**< First pts value, use for write mode*/
22 uint64_t last_pts; /**< Last pts value, use for write mode*/
pengfei.liuab5a2262020-02-14 17:33:40 +080023 uint64_t cur_time; /**< Current time save in index file */
hualing chen87072a82020-03-12 16:20:12 +080024 uint64_t segment_id;
25 char location[MAX_SEGMENT_PATH_SIZE];/**< Current time save in index file */
Pengfei Liuc181a982020-01-07 19:27:13 +080026} Segment_Context_t;
27
Pengfei Liub4734232020-01-17 18:25:10 +080028/**\brief Segment file type*/
Pengfei Liuc181a982020-01-07 19:27:13 +080029typedef enum {
Pengfei Liub4734232020-01-17 18:25:10 +080030 SEGMENT_FILE_TYPE_TS, /**< Used for store TS data*/
31 SEGMENT_FILE_TYPE_INDEX, /**< Used for store index data*/
32 SEGMENT_FILE_TYPE_DAT, /**< Used for store information data, such as duration etc*/
hualing chen87072a82020-03-12 16:20:12 +080033 SEGMENT_FILE_TYPE_ONGOING, /**< Used for store information data, such as duration etc*/
Pengfei Liuc181a982020-01-07 19:27:13 +080034} Segment_FileType_t;
35
36static void segment_get_fname(char fname[MAX_SEGMENT_PATH_SIZE],
Pengfei Liub4734232020-01-17 18:25:10 +080037 const char location[DVR_MAX_LOCATION_SIZE],
Pengfei Liuc181a982020-01-07 19:27:13 +080038 uint64_t segment_id,
39 Segment_FileType_t type)
40{
41 int offset;
42
43 memset(fname, 0, MAX_SEGMENT_PATH_SIZE);
44 strncpy(fname, location, strlen(location));
45 offset = strlen(location);
46 strncpy(fname + offset, "-", 1);
47 offset += 1;
48 sprintf(fname + offset, "%04llu", segment_id);
49 offset += 4;
50 if (type == SEGMENT_FILE_TYPE_TS)
51 strncpy(fname + offset, ".ts", 3);
52 else if (type == SEGMENT_FILE_TYPE_INDEX)
53 strncpy(fname + offset, ".idx", 4);
Pengfei Liub4734232020-01-17 18:25:10 +080054 else if (type == SEGMENT_FILE_TYPE_DAT)
55 strncpy(fname + offset, ".dat", 4);
hualing chen87072a82020-03-12 16:20:12 +080056 else if (type == SEGMENT_FILE_TYPE_ONGOING)
57 strncpy(fname + offset, ".going", 6);
Pengfei Liuc181a982020-01-07 19:27:13 +080058}
59
Pengfei Liu3b1a8202020-02-12 23:04:21 +080060static void segment_get_dirname(char dir_name[MAX_SEGMENT_PATH_SIZE],
61 const char location[DVR_MAX_LOCATION_SIZE])
62{
63 char *p;
64 int i;
65 int found = 0;
66
67 for (i = 0; i < (int)strlen(location); i++) {
68 if (location[i] == '/') {
69 p = (char *)location + i;
70 found = 1;
71 }
72 }
73 if (found)
74 memcpy(dir_name, location, p - location);
75}
76
Pengfei Liuc181a982020-01-07 19:27:13 +080077int segment_open(Segment_OpenParams_t *params, Segment_Handle_t *p_handle)
78{
79 Segment_Context_t *p_ctx;
80 char ts_fname[MAX_SEGMENT_PATH_SIZE];
81 char index_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liub4734232020-01-17 18:25:10 +080082 char dat_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liu3b1a8202020-02-12 23:04:21 +080083 char dir_name[MAX_SEGMENT_PATH_SIZE];
hualing chen87072a82020-03-12 16:20:12 +080084 char going_name[MAX_SEGMENT_PATH_SIZE];
Pengfei Liuc181a982020-01-07 19:27:13 +080085
Pengfei Liu3b1a8202020-02-12 23:04:21 +080086 DVR_RETURN_IF_FALSE(params);
87 DVR_RETURN_IF_FALSE(p_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +080088
hualing chena540a7e2020-03-27 16:44:05 +080089 //DVR_DEBUG(1, "%s, location:%s, id:%llu", __func__, params->location, params->segment_id);
Pengfei Liuc181a982020-01-07 19:27:13 +080090
Pengfei Liub038b6a2020-01-14 15:57:01 +080091 p_ctx = (void*)malloc(sizeof(Segment_Context_t));
Pengfei Liu3b1a8202020-02-12 23:04:21 +080092 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +080093 memset(p_ctx, 0, sizeof(Segment_Context_t));
Pengfei Liuc181a982020-01-07 19:27:13 +080094
95 memset(ts_fname, 0, sizeof(ts_fname));
96 segment_get_fname(ts_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_TS);
97
98 memset(index_fname, 0, sizeof(index_fname));
99 segment_get_fname(index_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_INDEX);
100
Pengfei Liub4734232020-01-17 18:25:10 +0800101 memset(dat_fname, 0, sizeof(dat_fname));
102 segment_get_fname(dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_DAT);
103
hualing chen87072a82020-03-12 16:20:12 +0800104 memset(going_name, 0, sizeof(going_name));
105 segment_get_fname(going_name, params->location, params->segment_id, SEGMENT_FILE_TYPE_ONGOING);
106
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800107 memset(dir_name, 0, sizeof(dir_name));
108 segment_get_dirname(dir_name, params->location);
109 if (access(dir_name, F_OK) == -1) {
110 DVR_DEBUG(1, "%s dir %s is not exist, create it", __func__, dir_name);
111 mkdir(dir_name, 0666);
112 }
113
Pengfei Liuc181a982020-01-07 19:27:13 +0800114 if (params->mode == SEGMENT_MODE_READ) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800115 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800116 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800117 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800118 p_ctx->ongoing_fp = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800119 } else if (params->mode == SEGMENT_MODE_WRITE) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800120 p_ctx->ts_fd = open(ts_fname, O_CREAT | O_RDWR | O_TRUNC, 0644);
Pengfei Liuc181a982020-01-07 19:27:13 +0800121 p_ctx->index_fp = fopen(index_fname, "w+");
Pengfei Liub4734232020-01-17 18:25:10 +0800122 p_ctx->dat_fp = fopen(dat_fname, "w+");
hualing chen87072a82020-03-12 16:20:12 +0800123 p_ctx->ongoing_fp = fopen(going_name, "w+");
Pengfei Liub038b6a2020-01-14 15:57:01 +0800124 p_ctx->first_pts = ULLONG_MAX;
125 p_ctx->last_pts = ULLONG_MAX;
Pengfei Liuc181a982020-01-07 19:27:13 +0800126 } else {
127 DVR_DEBUG(1, "%s, unknow mode use default", __func__);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800128 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800129 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800130 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800131 p_ctx->ongoing_fp = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800132 }
133
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800134 if (p_ctx->ts_fd == -1 || !p_ctx->index_fp || !p_ctx->dat_fp) {
Pengfei Liub4734232020-01-17 18:25:10 +0800135 DVR_DEBUG(1, "%s open file failed [%s, %s, %s], reason:%s", __func__,
136 ts_fname, index_fname, dat_fname, strerror(errno));
Pengfei Liuc181a982020-01-07 19:27:13 +0800137 free(p_ctx);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800138 *p_handle = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800139 return DVR_FAILURE;
140 }
hualing chen87072a82020-03-12 16:20:12 +0800141 p_ctx->segment_id = params->segment_id;
142 strncpy(p_ctx->location, params->location, strlen(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800143
hualing chena540a7e2020-03-27 16:44:05 +0800144 //DVR_DEBUG(1, "%s, open file success p_ctx->location [%s]", __func__, p_ctx->location);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800145 *p_handle = (Segment_Handle_t)p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800146 return DVR_SUCCESS;
147}
148
149int segment_close(Segment_Handle_t handle)
150{
151 Segment_Context_t *p_ctx;
152
Pengfei Liub038b6a2020-01-14 15:57:01 +0800153 p_ctx = (void *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800154 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800155
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800156 if (p_ctx->ts_fd != -1) {
157 close(p_ctx->ts_fd);
Pengfei Liuc181a982020-01-07 19:27:13 +0800158 }
159
160 if (p_ctx->index_fp) {
161 fclose(p_ctx->index_fp);
162 }
163
Pengfei Liub4734232020-01-17 18:25:10 +0800164 if (p_ctx->dat_fp) {
165 fclose(p_ctx->dat_fp);
166 }
167
hualing chen87072a82020-03-12 16:20:12 +0800168 if (p_ctx->ongoing_fp != NULL) {
169 fclose(p_ctx->ongoing_fp);
170 char going_name[MAX_SEGMENT_PATH_SIZE];
171 memset(going_name, 0, sizeof(going_name));
172 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
173 DVR_DEBUG(1, "segment close del [%s]", going_name);
174 unlink(going_name);
175 }
176
Pengfei Liuc181a982020-01-07 19:27:13 +0800177 free(p_ctx);
178 return 0;
179}
180
181ssize_t segment_read(Segment_Handle_t handle, void *buf, size_t count)
182{
183 Segment_Context_t *p_ctx;
184
185 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800186 DVR_RETURN_IF_FALSE(p_ctx);
187 DVR_RETURN_IF_FALSE(buf);
188 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800189
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800190 return read(p_ctx->ts_fd, buf, count);
Pengfei Liuc181a982020-01-07 19:27:13 +0800191}
192
193ssize_t segment_write(Segment_Handle_t handle, void *buf, size_t count)
194{
195 Segment_Context_t *p_ctx;
196
197 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800198 DVR_RETURN_IF_FALSE(p_ctx);
199 DVR_RETURN_IF_FALSE(buf);
200 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800201
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800202 return write(p_ctx->ts_fd, buf, count);
Pengfei Liuc181a982020-01-07 19:27:13 +0800203}
204
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800205int segment_update_pts(Segment_Handle_t handle, uint64_t pts, loff_t offset)
Pengfei Liuc181a982020-01-07 19:27:13 +0800206{
207 Segment_Context_t *p_ctx;
208 char buf[256];
209
210 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800211 DVR_RETURN_IF_FALSE(p_ctx);
212 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800213
Pengfei Liub038b6a2020-01-14 15:57:01 +0800214 if (p_ctx->first_pts == ULLONG_MAX) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800215 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800216 p_ctx->first_pts = pts;
217 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800218 memset(buf, 0, sizeof(buf));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800219 if (p_ctx->last_pts == ULLONG_MAX) {
220 /*Last pts is init value*/
hualing chen2aba4022020-03-02 13:49:55 +0800221 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
pengfei.liuab5a2262020-02-14 17:33:40 +0800222 p_ctx->cur_time = pts - p_ctx->first_pts;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800223 } else {
224 /*Last pts has valid value*/
225 if (pts - p_ctx->last_pts > MAX_PTS_THRESHOLD) {
226 /*Current pts has a transition*/
pengfei.liuab5a2262020-02-14 17:33:40 +0800227 DVR_DEBUG(1, "Current pts has a transition, [%llu, %llu, %llu]",
228 p_ctx->first_pts, p_ctx->last_pts, pts);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800229 } else {
230 /*This is a normal pts, record it*/
pengfei.liuab5a2262020-02-14 17:33:40 +0800231 p_ctx->cur_time += (pts - p_ctx->last_pts);
hualing chen2aba4022020-03-02 13:49:55 +0800232 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800233 }
234 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800235
236 fputs(buf, p_ctx->index_fp);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800237 p_ctx->last_pts = pts;
238 fflush(p_ctx->index_fp);
239 fsync(fileno(p_ctx->index_fp));
Pengfei Liub4734232020-01-17 18:25:10 +0800240
Pengfei Liuc181a982020-01-07 19:27:13 +0800241 return DVR_SUCCESS;
242}
243
hualing chen266b9502020-04-04 17:39:39 +0800244loff_t segment_seek(Segment_Handle_t handle, uint64_t time, int block_size)
Pengfei Liuc181a982020-01-07 19:27:13 +0800245{
246 Segment_Context_t *p_ctx;
247 char buf[256];
248 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800249 uint64_t pts = 0L;
250 loff_t offset = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800251 char *p1, *p2;
252
253 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800254 DVR_RETURN_IF_FALSE(p_ctx);
255 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
256 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800257
hualing chen266b9502020-04-04 17:39:39 +0800258 if (time == 0) {
259 offset = 0;
260 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
261 DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1);
262 return offset;
263 }
264
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800265 memset(buf, 0, sizeof(buf));
266 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chen2aba4022020-03-02 13:49:55 +0800267 int line = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800268 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800269 line++;
Pengfei Liuc181a982020-01-07 19:27:13 +0800270 memset(value, 0, sizeof(value));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800271 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800272 p1 += 5;
Pengfei Liuc181a982020-01-07 19:27:13 +0800273 if ((p2 = strstr(buf, ","))) {
274 memcpy(value, p1, p2 - p1);
275 }
276 pts = strtoull(value, NULL, 10);
277 }
278
279 memset(value, 0, sizeof(value));
280 if ((p1 = strstr(buf, "offset="))) {
281 p1 += 7;
282 if ((p2 = strstr(buf, "}"))) {
283 memcpy(value, p1, p2 - p1);
284 }
285 offset = strtoull(value, NULL, 10);
286 }
hualing chen2aba4022020-03-02 13:49:55 +0800287 if (0)
288 {
289 DVR_DEBUG(1, "seek buf[%s]", buf);
290 DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
291 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800292 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800293 //DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
294 if (time <= pts) {
hualing chen266b9502020-04-04 17:39:39 +0800295 if (block_size > 0) {
296 offset = offset - offset%block_size;
297 }
hualing chen2aba4022020-03-02 13:49:55 +0800298 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800299 DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800300 return offset;
301 }
hualing chen2aba4022020-03-02 13:49:55 +0800302 }
303 DVR_DEBUG(1, "seek error line [%d]", line);
Pengfei Liuc181a982020-01-07 19:27:13 +0800304 return DVR_FAILURE;
305}
306
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800307loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800308{
309 Segment_Context_t *p_ctx;
310
311 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800312 DVR_RETURN_IF_FALSE(p_ctx);
313 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800314
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800315 return lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
316}
317
pengfei.liu8b563292020-02-26 15:49:02 +0800318uint64_t segment_tell_current_time(Segment_Handle_t handle)
Pengfei Liu3b1a8202020-02-12 23:04:21 +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, position = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800325 char *p1, *p2;
326
327 p_ctx = (Segment_Context_t *)handle;
328 DVR_RETURN_IF_FALSE(p_ctx);
329 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
330 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
331
332 memset(buf, 0, sizeof(buf));
333 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
334 position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
335 DVR_RETURN_IF_FALSE(position != -1);
336
337 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
338 memset(value, 0, sizeof(value));
339 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800340 p1 += 5;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800341 if ((p2 = strstr(buf, ","))) {
342 memcpy(value, p1, p2 - p1);
343 }
344 pts = strtoull(value, NULL, 10);
345 }
346
347 memset(value, 0, sizeof(value));
348 if ((p1 = strstr(buf, "offset="))) {
349 p1 += 7;
350 if ((p2 = strstr(buf, "}"))) {
351 memcpy(value, p1, p2 - p1);
352 }
353 offset = strtoull(value, NULL, 10);
354 }
355
356 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800357 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800358 if (position <= offset) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800359 return pts;
360 }
361 }
hualing chena540a7e2020-03-27 16:44:05 +0800362 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800363 return pts;
Pengfei Liuc181a982020-01-07 19:27:13 +0800364}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800365
pengfei.liu8b563292020-02-26 15:49:02 +0800366uint64_t segment_tell_total_time(Segment_Handle_t handle)
367{
368 Segment_Context_t *p_ctx;
369 char buf[256];
370 char last_buf[256];
371 char value[256];
372 uint64_t pts = ULLONG_MAX;
373 loff_t offset = 0, position = 0;
374 char *p1, *p2;
375 int line = 0;
376
377 p_ctx = (Segment_Context_t *)handle;
378 DVR_RETURN_IF_FALSE(p_ctx);
379 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
380 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
381
382 memset(buf, 0, sizeof(buf));
383 memset(last_buf, 0, sizeof(last_buf));
384 position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
385 DVR_RETURN_IF_FALSE(position != -1);
386
hualing chen87072a82020-03-12 16:20:12 +0800387 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1);
pengfei.liu8b563292020-02-26 15:49:02 +0800388 /* Save last line buffer */
389 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800390 if (strlen(buf) <= 0) {
hualing chen87072a82020-03-12 16:20:12 +0800391 DVR_DEBUG(1, "read index buf is len 0");
hualing chen2aba4022020-03-02 13:49:55 +0800392 continue;
393 }
pengfei.liu8b563292020-02-26 15:49:02 +0800394 memset(last_buf, 0, sizeof(last_buf));
395 memcpy(last_buf, buf, strlen(buf));
396 memset(buf, 0, sizeof(buf));
397 line++;
398 }
399
400 /* Extract time value */
401 memset(value, 0, sizeof(value));
402 if ((p1 = strstr(last_buf, "time="))) {
403 p1 += 5;
404 if ((p2 = strstr(last_buf, ","))) {
405 memcpy(value, p1, p2 - p1);
406 }
407 pts = strtoull(value, NULL, 10);
408 }
409
410 memset(value, 0, sizeof(value));
411 if ((p1 = strstr(last_buf, "offset="))) {
412 p1 += 7;
413 if ((p2 = strstr(last_buf, "}"))) {
414 memcpy(value, p1, p2 - p1);
415 }
416 offset = strtoull(value, NULL, 10);
417 }
hualing chen87072a82020-03-12 16:20:12 +0800418 //if (line < 2)
419 //DVR_DEBUG(1, "totle time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line);
pengfei.liu8b563292020-02-26 15:49:02 +0800420 return (pts == ULLONG_MAX ? DVR_FAILURE : pts);
421}
422
pengfei.liuab5a2262020-02-14 17:33:40 +0800423/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800424int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
425{
426 Segment_Context_t *p_ctx;
427 char buf[256];
428 uint32_t i;
429
430 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800431 DVR_RETURN_IF_FALSE(p_ctx);
432 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
433 DVR_RETURN_IF_FALSE(p_info);
hualing chen87072a82020-03-12 16:20:12 +0800434 //seek 0, rewrite info
435 DVR_RETURN_IF_FALSE(fseek(p_ctx->dat_fp, 0, SEEK_SET) != -1);
Pengfei Liub4734232020-01-17 18:25:10 +0800436
437 /*Save segment id*/
438 memset(buf, 0, sizeof(buf));
439 sprintf(buf, "id=%lld\n", p_info->id);
440 fputs(buf, p_ctx->dat_fp);
441
442 /*Save number of pids*/
443 memset(buf, 0, sizeof(buf));
444 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
445 fputs(buf, p_ctx->dat_fp);
446
447 /*Save pid information*/
448 for (i = 0; i < p_info->nb_pids; i++) {
449 memset(buf, 0, sizeof(buf));
450 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
451 fputs(buf, p_ctx->dat_fp);
452 }
453
454 /*Save segment duration*/
455 memset(buf, 0, sizeof(buf));
hualing chen87072a82020-03-12 16:20:12 +0800456 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800457 sprintf(buf, "duration=%ld\n", p_info->duration);
458 fputs(buf, p_ctx->dat_fp);
459
460 /*Save segment size*/
461 memset(buf, 0, sizeof(buf));
462 sprintf(buf, "size=%zu\n", p_info->size);
463 fputs(buf, p_ctx->dat_fp);
464
465 /*Save number of packets*/
466 memset(buf, 0, sizeof(buf));
467 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
468 fputs(buf, p_ctx->dat_fp);
469
470 fflush(p_ctx->dat_fp);
471 fsync(fileno(p_ctx->dat_fp));
472 return DVR_SUCCESS;
473}
474
pengfei.liuab5a2262020-02-14 17:33:40 +0800475/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800476int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
477{
478 Segment_Context_t *p_ctx;
479 uint32_t i;
480 char buf[256];
481 char value[256];
482 char *p1, *p2;
483
484 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800485 DVR_RETURN_IF_FALSE(p_ctx);
486 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800487
488 /*Load segment id*/
489 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800490 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800491 p1 = strstr(buf, "id=");
hualing chen2aba4022020-03-02 13:49:55 +0800492 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800493 p_info->id = strtoull(p1 + 3, NULL, 10);
494
495 /*Save number of pids*/
496 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800497 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800498 p1 = strstr(buf, "nb_pids=");
hualing chen2aba4022020-03-02 13:49:55 +0800499 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800500 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
501
502 /*Save pid information*/
503 for (i = 0; i < p_info->nb_pids; i++) {
504 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800505 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800506 memset(value, 0, sizeof(value));
507 if ((p1 = strstr(buf, "pid="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800508 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800509 p1 += 4;
510 if ((p2 = strstr(buf, ","))) {
hualing chen2aba4022020-03-02 13:49:55 +0800511 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800512 memcpy(value, p1, p2 - p1);
513 }
514 p_info->pids[i].pid = strtoull(value, NULL, 10);
515 }
516
517 memset(value, 0, sizeof(value));
518 if ((p1 = strstr(buf, "type="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800519 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800520 p1 += 5;
521 if ((p2 = strstr(buf, "}"))) {
hualing chen2aba4022020-03-02 13:49:55 +0800522 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800523 memcpy(value, p1, p2 - p1);
524 }
525 p_info->pids[i].type = strtoull(value, NULL, 10);
526 }
527 }
528
529 /*Save segment duration*/
530 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800531 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800532 p1 = strstr(buf, "duration=");
hualing chen2aba4022020-03-02 13:49:55 +0800533 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800534 p_info->duration = strtoull(p1 + 9, NULL, 10);
hualing chena540a7e2020-03-27 16:44:05 +0800535 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800536
537 /*Save segment size*/
538 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800539 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800540 p1 = strstr(buf, "size=");
hualing chen2aba4022020-03-02 13:49:55 +0800541 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800542 p_info->size = strtoull(p1 + 5, NULL, 10);
543
544 /*Save number of packets*/
545 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800546 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800547 p1 = strstr(buf, "nb_packets=");
hualing chen2aba4022020-03-02 13:49:55 +0800548 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800549 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
550
551 return DVR_SUCCESS;
552}
553
554int segment_delete(const char *location, uint64_t segment_id)
555{
556 char fname[MAX_SEGMENT_PATH_SIZE];
557 int ret;
558
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800559 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800560
561 /*delete ts file*/
562 memset(fname, 0, sizeof(fname));
563 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
564 ret = unlink(fname);
565 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800566 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800567
568 /*delete index file*/
569 memset(fname, 0, sizeof(fname));
570 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
571 unlink(fname);
572 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800573 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800574
575 /*delete store information file*/
576 memset(fname, 0, sizeof(fname));
577 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
578 unlink(fname);
579 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800580 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800581
582 return DVR_SUCCESS;
583}
584
hualing chen87072a82020-03-12 16:20:12 +0800585int segment_ongoing(Segment_Handle_t handle)
586{
587 Segment_Context_t *p_ctx;
588 p_ctx = (Segment_Context_t *)handle;
589 struct stat mstat;
590
591 char going_name[MAX_SEGMENT_PATH_SIZE];
592 memset(going_name, 0, sizeof(going_name));
593 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
594 int ret = stat(going_name, &mstat);
595 DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret);
596 if (ret != 0) {
597 return DVR_FAILURE;
598 }
599 return DVR_SUCCESS;
600}
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800601loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800602{
603 Segment_Context_t *p_ctx;
604 char buf[256];
605 char value[256];
606 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800607 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800608 char *p1, *p2;
609
610 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800611 DVR_RETURN_IF_FALSE(p_ctx);
612 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
613 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800614
615 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800616 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800617 printf("start gets pts\n");
618 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
619 printf("buf[%s]\n", buf);
620 memset(value, 0, sizeof(value));
621 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800622 p1 += 5;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800623 if ((p2 = strstr(buf, ","))) {
624 memcpy(value, p1, p2 - p1);
625 }
626 pts = strtoull(value, NULL, 10);
627 }
628
629 memset(value, 0, sizeof(value));
630 if ((p1 = strstr(buf, "offset="))) {
631 p1 += 7;
632 if ((p2 = strstr(buf, "}"))) {
633 memcpy(value, p1, p2 - p1);
634 }
635 offset = strtoull(value, NULL, 10);
636 }
637
638 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800639 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800640 }
641
642 return 0;
643}