blob: 603eccd18279bcbf69b490c0ffed376edfa9b24b [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*/
20 uint64_t first_pts; /**< First pts value, use for write mode*/
21 uint64_t last_pts; /**< Last pts value, use for write mode*/
pengfei.liuab5a2262020-02-14 17:33:40 +080022 uint64_t cur_time; /**< Current time save in index file */
Pengfei Liuc181a982020-01-07 19:27:13 +080023} Segment_Context_t;
24
Pengfei Liub4734232020-01-17 18:25:10 +080025/**\brief Segment file type*/
Pengfei Liuc181a982020-01-07 19:27:13 +080026typedef enum {
Pengfei Liub4734232020-01-17 18:25:10 +080027 SEGMENT_FILE_TYPE_TS, /**< Used for store TS data*/
28 SEGMENT_FILE_TYPE_INDEX, /**< Used for store index data*/
29 SEGMENT_FILE_TYPE_DAT, /**< Used for store information data, such as duration etc*/
Pengfei Liuc181a982020-01-07 19:27:13 +080030} Segment_FileType_t;
31
32static void segment_get_fname(char fname[MAX_SEGMENT_PATH_SIZE],
Pengfei Liub4734232020-01-17 18:25:10 +080033 const char location[DVR_MAX_LOCATION_SIZE],
Pengfei Liuc181a982020-01-07 19:27:13 +080034 uint64_t segment_id,
35 Segment_FileType_t type)
36{
37 int offset;
38
39 memset(fname, 0, MAX_SEGMENT_PATH_SIZE);
40 strncpy(fname, location, strlen(location));
41 offset = strlen(location);
42 strncpy(fname + offset, "-", 1);
43 offset += 1;
44 sprintf(fname + offset, "%04llu", segment_id);
45 offset += 4;
46 if (type == SEGMENT_FILE_TYPE_TS)
47 strncpy(fname + offset, ".ts", 3);
48 else if (type == SEGMENT_FILE_TYPE_INDEX)
49 strncpy(fname + offset, ".idx", 4);
Pengfei Liub4734232020-01-17 18:25:10 +080050 else if (type == SEGMENT_FILE_TYPE_DAT)
51 strncpy(fname + offset, ".dat", 4);
Pengfei Liuc181a982020-01-07 19:27:13 +080052}
53
Pengfei Liu3b1a8202020-02-12 23:04:21 +080054static void segment_get_dirname(char dir_name[MAX_SEGMENT_PATH_SIZE],
55 const char location[DVR_MAX_LOCATION_SIZE])
56{
57 char *p;
58 int i;
59 int found = 0;
60
61 for (i = 0; i < (int)strlen(location); i++) {
62 if (location[i] == '/') {
63 p = (char *)location + i;
64 found = 1;
65 }
66 }
67 if (found)
68 memcpy(dir_name, location, p - location);
69}
70
Pengfei Liuc181a982020-01-07 19:27:13 +080071int segment_open(Segment_OpenParams_t *params, Segment_Handle_t *p_handle)
72{
73 Segment_Context_t *p_ctx;
74 char ts_fname[MAX_SEGMENT_PATH_SIZE];
75 char index_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liub4734232020-01-17 18:25:10 +080076 char dat_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liu3b1a8202020-02-12 23:04:21 +080077 char dir_name[MAX_SEGMENT_PATH_SIZE];
Pengfei Liuc181a982020-01-07 19:27:13 +080078
Pengfei Liu3b1a8202020-02-12 23:04:21 +080079 DVR_RETURN_IF_FALSE(params);
80 DVR_RETURN_IF_FALSE(p_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +080081
Pengfei Liub038b6a2020-01-14 15:57:01 +080082 DVR_DEBUG(1, "%s, location:%s, id:%llu", __func__, params->location, params->segment_id);
Pengfei Liuc181a982020-01-07 19:27:13 +080083
Pengfei Liub038b6a2020-01-14 15:57:01 +080084 p_ctx = (void*)malloc(sizeof(Segment_Context_t));
Pengfei Liu3b1a8202020-02-12 23:04:21 +080085 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +080086 memset(p_ctx, 0, sizeof(Segment_Context_t));
Pengfei Liuc181a982020-01-07 19:27:13 +080087
88 memset(ts_fname, 0, sizeof(ts_fname));
89 segment_get_fname(ts_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_TS);
90
91 memset(index_fname, 0, sizeof(index_fname));
92 segment_get_fname(index_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_INDEX);
93
Pengfei Liub4734232020-01-17 18:25:10 +080094 memset(dat_fname, 0, sizeof(dat_fname));
95 segment_get_fname(dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_DAT);
96
Pengfei Liu3b1a8202020-02-12 23:04:21 +080097 memset(dir_name, 0, sizeof(dir_name));
98 segment_get_dirname(dir_name, params->location);
99 if (access(dir_name, F_OK) == -1) {
100 DVR_DEBUG(1, "%s dir %s is not exist, create it", __func__, dir_name);
101 mkdir(dir_name, 0666);
102 }
103
Pengfei Liuc181a982020-01-07 19:27:13 +0800104 if (params->mode == SEGMENT_MODE_READ) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800105 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800106 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800107 p_ctx->dat_fp = fopen(dat_fname, "r");
Pengfei Liuc181a982020-01-07 19:27:13 +0800108 } else if (params->mode == SEGMENT_MODE_WRITE) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800109 p_ctx->ts_fd = open(ts_fname, O_CREAT | O_RDWR | O_TRUNC, 0644);
Pengfei Liuc181a982020-01-07 19:27:13 +0800110 p_ctx->index_fp = fopen(index_fname, "w+");
Pengfei Liub4734232020-01-17 18:25:10 +0800111 p_ctx->dat_fp = fopen(dat_fname, "w+");
Pengfei Liub038b6a2020-01-14 15:57:01 +0800112 p_ctx->first_pts = ULLONG_MAX;
113 p_ctx->last_pts = ULLONG_MAX;
Pengfei Liuc181a982020-01-07 19:27:13 +0800114 } else {
115 DVR_DEBUG(1, "%s, unknow mode use default", __func__);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800116 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800117 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800118 p_ctx->dat_fp = fopen(dat_fname, "r");
Pengfei Liuc181a982020-01-07 19:27:13 +0800119 }
120
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800121 if (p_ctx->ts_fd == -1 || !p_ctx->index_fp || !p_ctx->dat_fp) {
Pengfei Liub4734232020-01-17 18:25:10 +0800122 DVR_DEBUG(1, "%s open file failed [%s, %s, %s], reason:%s", __func__,
123 ts_fname, index_fname, dat_fname, strerror(errno));
Pengfei Liuc181a982020-01-07 19:27:13 +0800124 free(p_ctx);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800125 *p_handle = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800126 return DVR_FAILURE;
127 }
Pengfei Liub4734232020-01-17 18:25:10 +0800128
129 DVR_DEBUG(1, "%s, open file success", __func__);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800130 *p_handle = (Segment_Handle_t)p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800131 return DVR_SUCCESS;
132}
133
134int segment_close(Segment_Handle_t handle)
135{
136 Segment_Context_t *p_ctx;
137
Pengfei Liub038b6a2020-01-14 15:57:01 +0800138 p_ctx = (void *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800139 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800140
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800141 if (p_ctx->ts_fd != -1) {
142 close(p_ctx->ts_fd);
Pengfei Liuc181a982020-01-07 19:27:13 +0800143 }
144
145 if (p_ctx->index_fp) {
146 fclose(p_ctx->index_fp);
147 }
148
Pengfei Liub4734232020-01-17 18:25:10 +0800149 if (p_ctx->dat_fp) {
150 fclose(p_ctx->dat_fp);
151 }
152
Pengfei Liuc181a982020-01-07 19:27:13 +0800153 free(p_ctx);
154 return 0;
155}
156
157ssize_t segment_read(Segment_Handle_t handle, void *buf, size_t count)
158{
159 Segment_Context_t *p_ctx;
160
161 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800162 DVR_RETURN_IF_FALSE(p_ctx);
163 DVR_RETURN_IF_FALSE(buf);
164 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800165
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800166 return read(p_ctx->ts_fd, buf, count);
Pengfei Liuc181a982020-01-07 19:27:13 +0800167}
168
169ssize_t segment_write(Segment_Handle_t handle, void *buf, size_t count)
170{
171 Segment_Context_t *p_ctx;
172
173 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800174 DVR_RETURN_IF_FALSE(p_ctx);
175 DVR_RETURN_IF_FALSE(buf);
176 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800177
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800178 return write(p_ctx->ts_fd, buf, count);
Pengfei Liuc181a982020-01-07 19:27:13 +0800179}
180
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800181int segment_update_pts(Segment_Handle_t handle, uint64_t pts, loff_t offset)
Pengfei Liuc181a982020-01-07 19:27:13 +0800182{
183 Segment_Context_t *p_ctx;
184 char buf[256];
185
186 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800187 DVR_RETURN_IF_FALSE(p_ctx);
188 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800189
Pengfei Liub038b6a2020-01-14 15:57:01 +0800190 if (p_ctx->first_pts == ULLONG_MAX) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800191 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800192 p_ctx->first_pts = pts;
193 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800194 memset(buf, 0, sizeof(buf));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800195 if (p_ctx->last_pts == ULLONG_MAX) {
196 /*Last pts is init value*/
hualing chen2aba4022020-03-02 13:49:55 +0800197 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
pengfei.liuab5a2262020-02-14 17:33:40 +0800198 p_ctx->cur_time = pts - p_ctx->first_pts;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800199 } else {
200 /*Last pts has valid value*/
201 if (pts - p_ctx->last_pts > MAX_PTS_THRESHOLD) {
202 /*Current pts has a transition*/
pengfei.liuab5a2262020-02-14 17:33:40 +0800203 DVR_DEBUG(1, "Current pts has a transition, [%llu, %llu, %llu]",
204 p_ctx->first_pts, p_ctx->last_pts, pts);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800205 } else {
206 /*This is a normal pts, record it*/
pengfei.liuab5a2262020-02-14 17:33:40 +0800207 p_ctx->cur_time += (pts - p_ctx->last_pts);
hualing chen2aba4022020-03-02 13:49:55 +0800208 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800209 }
210 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800211
212 fputs(buf, p_ctx->index_fp);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800213 p_ctx->last_pts = pts;
214 fflush(p_ctx->index_fp);
215 fsync(fileno(p_ctx->index_fp));
Pengfei Liub4734232020-01-17 18:25:10 +0800216
Pengfei Liuc181a982020-01-07 19:27:13 +0800217 return DVR_SUCCESS;
218}
219
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800220loff_t segment_seek(Segment_Handle_t handle, uint64_t time)
Pengfei Liuc181a982020-01-07 19:27:13 +0800221{
222 Segment_Context_t *p_ctx;
223 char buf[256];
224 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800225 uint64_t pts = 0L;
226 loff_t offset = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800227 char *p1, *p2;
228
229 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800230 DVR_RETURN_IF_FALSE(p_ctx);
231 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
232 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800233
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800234 memset(buf, 0, sizeof(buf));
235 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chen2aba4022020-03-02 13:49:55 +0800236 int line = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800237 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800238 line++;
Pengfei Liuc181a982020-01-07 19:27:13 +0800239 memset(value, 0, sizeof(value));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800240 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800241 p1 += 5;
Pengfei Liuc181a982020-01-07 19:27:13 +0800242 if ((p2 = strstr(buf, ","))) {
243 memcpy(value, p1, p2 - p1);
244 }
245 pts = strtoull(value, NULL, 10);
246 }
247
248 memset(value, 0, sizeof(value));
249 if ((p1 = strstr(buf, "offset="))) {
250 p1 += 7;
251 if ((p2 = strstr(buf, "}"))) {
252 memcpy(value, p1, p2 - p1);
253 }
254 offset = strtoull(value, NULL, 10);
255 }
hualing chen2aba4022020-03-02 13:49:55 +0800256 if (0)
257 {
258 DVR_DEBUG(1, "seek buf[%s]", buf);
259 DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
260 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800261 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800262 //DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
263 if (time <= pts) {
hualing chen2aba4022020-03-02 13:49:55 +0800264 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800265 DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800266 return offset;
267 }
hualing chen2aba4022020-03-02 13:49:55 +0800268 }
269 DVR_DEBUG(1, "seek error line [%d]", line);
Pengfei Liuc181a982020-01-07 19:27:13 +0800270 return DVR_FAILURE;
271}
272
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800273loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800274{
275 Segment_Context_t *p_ctx;
276
277 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800278 DVR_RETURN_IF_FALSE(p_ctx);
279 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800280
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800281 return lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
282}
283
pengfei.liu8b563292020-02-26 15:49:02 +0800284uint64_t segment_tell_current_time(Segment_Handle_t handle)
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800285{
286 Segment_Context_t *p_ctx;
287 char buf[256];
288 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800289 uint64_t pts = 0L;
290 loff_t offset = 0, position = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800291 char *p1, *p2;
292
293 p_ctx = (Segment_Context_t *)handle;
294 DVR_RETURN_IF_FALSE(p_ctx);
295 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
296 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
297
298 memset(buf, 0, sizeof(buf));
299 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
300 position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
301 DVR_RETURN_IF_FALSE(position != -1);
302
303 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
304 memset(value, 0, sizeof(value));
305 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800306 p1 += 5;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800307 if ((p2 = strstr(buf, ","))) {
308 memcpy(value, p1, p2 - p1);
309 }
310 pts = strtoull(value, NULL, 10);
311 }
312
313 memset(value, 0, sizeof(value));
314 if ((p1 = strstr(buf, "offset="))) {
315 p1 += 7;
316 if ((p2 = strstr(buf, "}"))) {
317 memcpy(value, p1, p2 - p1);
318 }
319 offset = strtoull(value, NULL, 10);
320 }
321
322 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800323 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800324 if (position <= offset) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800325 return pts;
326 }
327 }
hualing chen2aba4022020-03-02 13:49:55 +0800328 DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
329 return pts;
Pengfei Liuc181a982020-01-07 19:27:13 +0800330}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800331
pengfei.liu8b563292020-02-26 15:49:02 +0800332uint64_t segment_tell_total_time(Segment_Handle_t handle)
333{
334 Segment_Context_t *p_ctx;
335 char buf[256];
336 char last_buf[256];
337 char value[256];
338 uint64_t pts = ULLONG_MAX;
339 loff_t offset = 0, position = 0;
340 char *p1, *p2;
341 int line = 0;
342
343 p_ctx = (Segment_Context_t *)handle;
344 DVR_RETURN_IF_FALSE(p_ctx);
345 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
346 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
347
348 memset(buf, 0, sizeof(buf));
349 memset(last_buf, 0, sizeof(last_buf));
350 position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
351 DVR_RETURN_IF_FALSE(position != -1);
352
hualing chen2aba4022020-03-02 13:49:55 +0800353 //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1);
pengfei.liu8b563292020-02-26 15:49:02 +0800354 /* Save last line buffer */
355 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800356 if (strlen(buf) <= 0) {
357 continue;
358 }
pengfei.liu8b563292020-02-26 15:49:02 +0800359 memset(last_buf, 0, sizeof(last_buf));
360 memcpy(last_buf, buf, strlen(buf));
361 memset(buf, 0, sizeof(buf));
362 line++;
363 }
364
365 /* Extract time value */
366 memset(value, 0, sizeof(value));
367 if ((p1 = strstr(last_buf, "time="))) {
368 p1 += 5;
369 if ((p2 = strstr(last_buf, ","))) {
370 memcpy(value, p1, p2 - p1);
371 }
372 pts = strtoull(value, NULL, 10);
373 }
374
375 memset(value, 0, sizeof(value));
376 if ((p1 = strstr(last_buf, "offset="))) {
377 p1 += 7;
378 if ((p2 = strstr(last_buf, "}"))) {
379 memcpy(value, p1, p2 - p1);
380 }
381 offset = strtoull(value, NULL, 10);
382 }
383 if (line < 2)
hualing chencc91e1c2020-02-28 13:26:17 +0800384 DVR_DEBUG(1, "totle time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line);
pengfei.liu8b563292020-02-26 15:49:02 +0800385
386 return (pts == ULLONG_MAX ? DVR_FAILURE : pts);
387}
388
pengfei.liuab5a2262020-02-14 17:33:40 +0800389/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800390int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
391{
392 Segment_Context_t *p_ctx;
393 char buf[256];
394 uint32_t i;
395
396 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800397 DVR_RETURN_IF_FALSE(p_ctx);
398 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
399 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800400
401 /*Save segment id*/
402 memset(buf, 0, sizeof(buf));
403 sprintf(buf, "id=%lld\n", p_info->id);
404 fputs(buf, p_ctx->dat_fp);
405
406 /*Save number of pids*/
407 memset(buf, 0, sizeof(buf));
408 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
409 fputs(buf, p_ctx->dat_fp);
410
411 /*Save pid information*/
412 for (i = 0; i < p_info->nb_pids; i++) {
413 memset(buf, 0, sizeof(buf));
414 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
415 fputs(buf, p_ctx->dat_fp);
416 }
417
418 /*Save segment duration*/
419 memset(buf, 0, sizeof(buf));
420 sprintf(buf, "duration=%ld\n", p_info->duration);
421 fputs(buf, p_ctx->dat_fp);
422
423 /*Save segment size*/
424 memset(buf, 0, sizeof(buf));
425 sprintf(buf, "size=%zu\n", p_info->size);
426 fputs(buf, p_ctx->dat_fp);
427
428 /*Save number of packets*/
429 memset(buf, 0, sizeof(buf));
430 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
431 fputs(buf, p_ctx->dat_fp);
432
433 fflush(p_ctx->dat_fp);
434 fsync(fileno(p_ctx->dat_fp));
435 return DVR_SUCCESS;
436}
437
pengfei.liuab5a2262020-02-14 17:33:40 +0800438/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800439int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
440{
441 Segment_Context_t *p_ctx;
442 uint32_t i;
443 char buf[256];
444 char value[256];
445 char *p1, *p2;
446
447 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800448 DVR_RETURN_IF_FALSE(p_ctx);
449 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800450
451 /*Load segment id*/
452 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800453 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800454 p1 = strstr(buf, "id=");
hualing chen2aba4022020-03-02 13:49:55 +0800455 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800456 p_info->id = strtoull(p1 + 3, NULL, 10);
457
458 /*Save number of pids*/
459 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800460 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800461 p1 = strstr(buf, "nb_pids=");
hualing chen2aba4022020-03-02 13:49:55 +0800462 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800463 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
464
465 /*Save pid information*/
466 for (i = 0; i < p_info->nb_pids; i++) {
467 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800468 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800469 memset(value, 0, sizeof(value));
470 if ((p1 = strstr(buf, "pid="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800471 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800472 p1 += 4;
473 if ((p2 = strstr(buf, ","))) {
hualing chen2aba4022020-03-02 13:49:55 +0800474 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800475 memcpy(value, p1, p2 - p1);
476 }
477 p_info->pids[i].pid = strtoull(value, NULL, 10);
478 }
479
480 memset(value, 0, sizeof(value));
481 if ((p1 = strstr(buf, "type="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800482 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800483 p1 += 5;
484 if ((p2 = strstr(buf, "}"))) {
hualing chen2aba4022020-03-02 13:49:55 +0800485 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800486 memcpy(value, p1, p2 - p1);
487 }
488 p_info->pids[i].type = strtoull(value, NULL, 10);
489 }
490 }
491
492 /*Save segment duration*/
493 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800494 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800495 p1 = strstr(buf, "duration=");
hualing chen2aba4022020-03-02 13:49:55 +0800496 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800497 p_info->duration = strtoull(p1 + 9, NULL, 10);
498
499 /*Save segment size*/
500 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800501 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800502 p1 = strstr(buf, "size=");
hualing chen2aba4022020-03-02 13:49:55 +0800503 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800504 p_info->size = strtoull(p1 + 5, NULL, 10);
505
506 /*Save number of packets*/
507 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800508 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800509 p1 = strstr(buf, "nb_packets=");
hualing chen2aba4022020-03-02 13:49:55 +0800510 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800511 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
512
513 return DVR_SUCCESS;
514}
515
516int segment_delete(const char *location, uint64_t segment_id)
517{
518 char fname[MAX_SEGMENT_PATH_SIZE];
519 int ret;
520
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800521 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800522
523 /*delete ts file*/
524 memset(fname, 0, sizeof(fname));
525 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
526 ret = unlink(fname);
527 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800528 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800529
530 /*delete index file*/
531 memset(fname, 0, sizeof(fname));
532 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
533 unlink(fname);
534 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800535 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800536
537 /*delete store information file*/
538 memset(fname, 0, sizeof(fname));
539 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
540 unlink(fname);
541 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800542 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800543
544 return DVR_SUCCESS;
545}
546
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800547loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800548{
549 Segment_Context_t *p_ctx;
550 char buf[256];
551 char value[256];
552 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800553 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800554 char *p1, *p2;
555
556 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800557 DVR_RETURN_IF_FALSE(p_ctx);
558 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
559 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800560
561 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800562 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800563 printf("start gets pts\n");
564 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
565 printf("buf[%s]\n", buf);
566 memset(value, 0, sizeof(value));
567 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800568 p1 += 5;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800569 if ((p2 = strstr(buf, ","))) {
570 memcpy(value, p1, p2 - p1);
571 }
572 pts = strtoull(value, NULL, 10);
573 }
574
575 memset(value, 0, sizeof(value));
576 if ((p1 = strstr(buf, "offset="))) {
577 p1 += 7;
578 if ((p2 = strstr(buf, "}"))) {
579 memcpy(value, p1, p2 - p1);
580 }
581 offset = strtoull(value, NULL, 10);
582 }
583
584 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800585 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800586 }
587
588 return 0;
589}