blob: 5d0b884225b54ff9666f5582b346277477543482 [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
Pengfei Liub038b6a2020-01-14 15:57:01 +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 chen87072a82020-03-12 16:20:12 +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
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800244loff_t segment_seek(Segment_Handle_t handle, uint64_t time)
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
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800258 memset(buf, 0, sizeof(buf));
259 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chen2aba4022020-03-02 13:49:55 +0800260 int line = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800261 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800262 line++;
Pengfei Liuc181a982020-01-07 19:27:13 +0800263 memset(value, 0, sizeof(value));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800264 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800265 p1 += 5;
Pengfei Liuc181a982020-01-07 19:27:13 +0800266 if ((p2 = strstr(buf, ","))) {
267 memcpy(value, p1, p2 - p1);
268 }
269 pts = strtoull(value, NULL, 10);
270 }
271
272 memset(value, 0, sizeof(value));
273 if ((p1 = strstr(buf, "offset="))) {
274 p1 += 7;
275 if ((p2 = strstr(buf, "}"))) {
276 memcpy(value, p1, p2 - p1);
277 }
278 offset = strtoull(value, NULL, 10);
279 }
hualing chen2aba4022020-03-02 13:49:55 +0800280 if (0)
281 {
282 DVR_DEBUG(1, "seek buf[%s]", buf);
283 DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
284 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800285 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800286 //DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
287 if (time <= pts) {
hualing chen2aba4022020-03-02 13:49:55 +0800288 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800289 DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800290 return offset;
291 }
hualing chen2aba4022020-03-02 13:49:55 +0800292 }
293 DVR_DEBUG(1, "seek error line [%d]", line);
Pengfei Liuc181a982020-01-07 19:27:13 +0800294 return DVR_FAILURE;
295}
296
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800297loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800298{
299 Segment_Context_t *p_ctx;
300
301 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800302 DVR_RETURN_IF_FALSE(p_ctx);
303 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800304
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800305 return lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
306}
307
pengfei.liu8b563292020-02-26 15:49:02 +0800308uint64_t segment_tell_current_time(Segment_Handle_t handle)
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800309{
310 Segment_Context_t *p_ctx;
311 char buf[256];
312 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800313 uint64_t pts = 0L;
314 loff_t offset = 0, position = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800315 char *p1, *p2;
316
317 p_ctx = (Segment_Context_t *)handle;
318 DVR_RETURN_IF_FALSE(p_ctx);
319 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
320 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
321
322 memset(buf, 0, sizeof(buf));
323 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
324 position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
325 DVR_RETURN_IF_FALSE(position != -1);
326
327 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
328 memset(value, 0, sizeof(value));
329 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800330 p1 += 5;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800331 if ((p2 = strstr(buf, ","))) {
332 memcpy(value, p1, p2 - p1);
333 }
334 pts = strtoull(value, NULL, 10);
335 }
336
337 memset(value, 0, sizeof(value));
338 if ((p1 = strstr(buf, "offset="))) {
339 p1 += 7;
340 if ((p2 = strstr(buf, "}"))) {
341 memcpy(value, p1, p2 - p1);
342 }
343 offset = strtoull(value, NULL, 10);
344 }
345
346 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800347 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800348 if (position <= offset) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800349 return pts;
350 }
351 }
hualing chen2aba4022020-03-02 13:49:55 +0800352 DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
353 return pts;
Pengfei Liuc181a982020-01-07 19:27:13 +0800354}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800355
pengfei.liu8b563292020-02-26 15:49:02 +0800356uint64_t segment_tell_total_time(Segment_Handle_t handle)
357{
358 Segment_Context_t *p_ctx;
359 char buf[256];
360 char last_buf[256];
361 char value[256];
362 uint64_t pts = ULLONG_MAX;
363 loff_t offset = 0, position = 0;
364 char *p1, *p2;
365 int line = 0;
366
367 p_ctx = (Segment_Context_t *)handle;
368 DVR_RETURN_IF_FALSE(p_ctx);
369 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
370 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
371
372 memset(buf, 0, sizeof(buf));
373 memset(last_buf, 0, sizeof(last_buf));
374 position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
375 DVR_RETURN_IF_FALSE(position != -1);
376
hualing chen87072a82020-03-12 16:20:12 +0800377 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1);
pengfei.liu8b563292020-02-26 15:49:02 +0800378 /* Save last line buffer */
379 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800380 if (strlen(buf) <= 0) {
hualing chen87072a82020-03-12 16:20:12 +0800381 DVR_DEBUG(1, "read index buf is len 0");
hualing chen2aba4022020-03-02 13:49:55 +0800382 continue;
383 }
pengfei.liu8b563292020-02-26 15:49:02 +0800384 memset(last_buf, 0, sizeof(last_buf));
385 memcpy(last_buf, buf, strlen(buf));
386 memset(buf, 0, sizeof(buf));
387 line++;
388 }
389
390 /* Extract time value */
391 memset(value, 0, sizeof(value));
392 if ((p1 = strstr(last_buf, "time="))) {
393 p1 += 5;
394 if ((p2 = strstr(last_buf, ","))) {
395 memcpy(value, p1, p2 - p1);
396 }
397 pts = strtoull(value, NULL, 10);
398 }
399
400 memset(value, 0, sizeof(value));
401 if ((p1 = strstr(last_buf, "offset="))) {
402 p1 += 7;
403 if ((p2 = strstr(last_buf, "}"))) {
404 memcpy(value, p1, p2 - p1);
405 }
406 offset = strtoull(value, NULL, 10);
407 }
hualing chen87072a82020-03-12 16:20:12 +0800408 //if (line < 2)
409 //DVR_DEBUG(1, "totle time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line);
pengfei.liu8b563292020-02-26 15:49:02 +0800410
411 return (pts == ULLONG_MAX ? DVR_FAILURE : pts);
412}
413
pengfei.liuab5a2262020-02-14 17:33:40 +0800414/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800415int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
416{
417 Segment_Context_t *p_ctx;
418 char buf[256];
419 uint32_t i;
420
421 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800422 DVR_RETURN_IF_FALSE(p_ctx);
423 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
424 DVR_RETURN_IF_FALSE(p_info);
hualing chen87072a82020-03-12 16:20:12 +0800425 //seek 0, rewrite info
426 DVR_RETURN_IF_FALSE(fseek(p_ctx->dat_fp, 0, SEEK_SET) != -1);
Pengfei Liub4734232020-01-17 18:25:10 +0800427
428 /*Save segment id*/
429 memset(buf, 0, sizeof(buf));
430 sprintf(buf, "id=%lld\n", p_info->id);
431 fputs(buf, p_ctx->dat_fp);
432
433 /*Save number of pids*/
434 memset(buf, 0, sizeof(buf));
435 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
436 fputs(buf, p_ctx->dat_fp);
437
438 /*Save pid information*/
439 for (i = 0; i < p_info->nb_pids; i++) {
440 memset(buf, 0, sizeof(buf));
441 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
442 fputs(buf, p_ctx->dat_fp);
443 }
444
445 /*Save segment duration*/
446 memset(buf, 0, sizeof(buf));
hualing chen87072a82020-03-12 16:20:12 +0800447 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800448 sprintf(buf, "duration=%ld\n", p_info->duration);
449 fputs(buf, p_ctx->dat_fp);
450
451 /*Save segment size*/
452 memset(buf, 0, sizeof(buf));
453 sprintf(buf, "size=%zu\n", p_info->size);
454 fputs(buf, p_ctx->dat_fp);
455
456 /*Save number of packets*/
457 memset(buf, 0, sizeof(buf));
458 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
459 fputs(buf, p_ctx->dat_fp);
460
461 fflush(p_ctx->dat_fp);
462 fsync(fileno(p_ctx->dat_fp));
463 return DVR_SUCCESS;
464}
465
pengfei.liuab5a2262020-02-14 17:33:40 +0800466/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800467int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
468{
469 Segment_Context_t *p_ctx;
470 uint32_t i;
471 char buf[256];
472 char value[256];
473 char *p1, *p2;
474
475 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800476 DVR_RETURN_IF_FALSE(p_ctx);
477 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800478
479 /*Load segment id*/
480 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800481 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800482 p1 = strstr(buf, "id=");
hualing chen2aba4022020-03-02 13:49:55 +0800483 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800484 p_info->id = strtoull(p1 + 3, NULL, 10);
485
486 /*Save number of pids*/
487 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800488 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800489 p1 = strstr(buf, "nb_pids=");
hualing chen2aba4022020-03-02 13:49:55 +0800490 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800491 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
492
493 /*Save pid information*/
494 for (i = 0; i < p_info->nb_pids; i++) {
495 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800496 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800497 memset(value, 0, sizeof(value));
498 if ((p1 = strstr(buf, "pid="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800499 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800500 p1 += 4;
501 if ((p2 = strstr(buf, ","))) {
hualing chen2aba4022020-03-02 13:49:55 +0800502 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800503 memcpy(value, p1, p2 - p1);
504 }
505 p_info->pids[i].pid = strtoull(value, NULL, 10);
506 }
507
508 memset(value, 0, sizeof(value));
509 if ((p1 = strstr(buf, "type="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800510 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800511 p1 += 5;
512 if ((p2 = strstr(buf, "}"))) {
hualing chen2aba4022020-03-02 13:49:55 +0800513 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800514 memcpy(value, p1, p2 - p1);
515 }
516 p_info->pids[i].type = strtoull(value, NULL, 10);
517 }
518 }
519
520 /*Save segment duration*/
521 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800522 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800523 p1 = strstr(buf, "duration=");
hualing chen2aba4022020-03-02 13:49:55 +0800524 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800525 p_info->duration = strtoull(p1 + 9, NULL, 10);
hualing chen87072a82020-03-12 16:20:12 +0800526 DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800527
528 /*Save segment size*/
529 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800530 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800531 p1 = strstr(buf, "size=");
hualing chen2aba4022020-03-02 13:49:55 +0800532 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800533 p_info->size = strtoull(p1 + 5, NULL, 10);
534
535 /*Save number of packets*/
536 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800537 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800538 p1 = strstr(buf, "nb_packets=");
hualing chen2aba4022020-03-02 13:49:55 +0800539 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800540 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
541
542 return DVR_SUCCESS;
543}
544
545int segment_delete(const char *location, uint64_t segment_id)
546{
547 char fname[MAX_SEGMENT_PATH_SIZE];
548 int ret;
549
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800550 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800551
552 /*delete ts file*/
553 memset(fname, 0, sizeof(fname));
554 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
555 ret = unlink(fname);
556 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800557 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800558
559 /*delete index file*/
560 memset(fname, 0, sizeof(fname));
561 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
562 unlink(fname);
563 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800564 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800565
566 /*delete store information file*/
567 memset(fname, 0, sizeof(fname));
568 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
569 unlink(fname);
570 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800571 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800572
573 return DVR_SUCCESS;
574}
575
hualing chen87072a82020-03-12 16:20:12 +0800576int segment_ongoing(Segment_Handle_t handle)
577{
578 Segment_Context_t *p_ctx;
579 p_ctx = (Segment_Context_t *)handle;
580 struct stat mstat;
581
582 char going_name[MAX_SEGMENT_PATH_SIZE];
583 memset(going_name, 0, sizeof(going_name));
584 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
585 int ret = stat(going_name, &mstat);
586 DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret);
587 if (ret != 0) {
588 return DVR_FAILURE;
589 }
590 return DVR_SUCCESS;
591}
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800592loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800593{
594 Segment_Context_t *p_ctx;
595 char buf[256];
596 char value[256];
597 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800598 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800599 char *p1, *p2;
600
601 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800602 DVR_RETURN_IF_FALSE(p_ctx);
603 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
604 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800605
606 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800607 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800608 printf("start gets pts\n");
609 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
610 printf("buf[%s]\n", buf);
611 memset(value, 0, sizeof(value));
612 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800613 p1 += 5;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800614 if ((p2 = strstr(buf, ","))) {
615 memcpy(value, p1, p2 - p1);
616 }
617 pts = strtoull(value, NULL, 10);
618 }
619
620 memset(value, 0, sizeof(value));
621 if ((p1 = strstr(buf, "offset="))) {
622 p1 += 7;
623 if ((p2 = strstr(buf, "}"))) {
624 memcpy(value, p1, p2 - p1);
625 }
626 offset = strtoull(value, NULL, 10);
627 }
628
629 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800630 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800631 }
632
633 return 0;
634}