blob: 85034724dc24fb80542db2bed9a68a08b4bb9cf1 [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 Liuc181a982020-01-07 19:27:13 +080022} Segment_Context_t;
23
Pengfei Liub4734232020-01-17 18:25:10 +080024/**\brief Segment file type*/
Pengfei Liuc181a982020-01-07 19:27:13 +080025typedef enum {
Pengfei Liub4734232020-01-17 18:25:10 +080026 SEGMENT_FILE_TYPE_TS, /**< Used for store TS data*/
27 SEGMENT_FILE_TYPE_INDEX, /**< Used for store index data*/
28 SEGMENT_FILE_TYPE_DAT, /**< Used for store information data, such as duration etc*/
Pengfei Liuc181a982020-01-07 19:27:13 +080029} Segment_FileType_t;
30
31static void segment_get_fname(char fname[MAX_SEGMENT_PATH_SIZE],
Pengfei Liub4734232020-01-17 18:25:10 +080032 const char location[DVR_MAX_LOCATION_SIZE],
Pengfei Liuc181a982020-01-07 19:27:13 +080033 uint64_t segment_id,
34 Segment_FileType_t type)
35{
36 int offset;
37
38 memset(fname, 0, MAX_SEGMENT_PATH_SIZE);
39 strncpy(fname, location, strlen(location));
40 offset = strlen(location);
41 strncpy(fname + offset, "-", 1);
42 offset += 1;
43 sprintf(fname + offset, "%04llu", segment_id);
44 offset += 4;
45 if (type == SEGMENT_FILE_TYPE_TS)
46 strncpy(fname + offset, ".ts", 3);
47 else if (type == SEGMENT_FILE_TYPE_INDEX)
48 strncpy(fname + offset, ".idx", 4);
Pengfei Liub4734232020-01-17 18:25:10 +080049 else if (type == SEGMENT_FILE_TYPE_DAT)
50 strncpy(fname + offset, ".dat", 4);
Pengfei Liuc181a982020-01-07 19:27:13 +080051}
52
Pengfei Liu3b1a8202020-02-12 23:04:21 +080053static void segment_get_dirname(char dir_name[MAX_SEGMENT_PATH_SIZE],
54 const char location[DVR_MAX_LOCATION_SIZE])
55{
56 char *p;
57 int i;
58 int found = 0;
59
60 for (i = 0; i < (int)strlen(location); i++) {
61 if (location[i] == '/') {
62 p = (char *)location + i;
63 found = 1;
64 }
65 }
66 if (found)
67 memcpy(dir_name, location, p - location);
68}
69
Pengfei Liuc181a982020-01-07 19:27:13 +080070int segment_open(Segment_OpenParams_t *params, Segment_Handle_t *p_handle)
71{
72 Segment_Context_t *p_ctx;
73 char ts_fname[MAX_SEGMENT_PATH_SIZE];
74 char index_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liub4734232020-01-17 18:25:10 +080075 char dat_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liu3b1a8202020-02-12 23:04:21 +080076 char dir_name[MAX_SEGMENT_PATH_SIZE];
Pengfei Liuc181a982020-01-07 19:27:13 +080077
Pengfei Liu3b1a8202020-02-12 23:04:21 +080078 DVR_RETURN_IF_FALSE(params);
79 DVR_RETURN_IF_FALSE(p_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +080080
Pengfei Liub038b6a2020-01-14 15:57:01 +080081 DVR_DEBUG(1, "%s, location:%s, id:%llu", __func__, params->location, params->segment_id);
Pengfei Liuc181a982020-01-07 19:27:13 +080082
Pengfei Liub038b6a2020-01-14 15:57:01 +080083 p_ctx = (void*)malloc(sizeof(Segment_Context_t));
Pengfei Liu3b1a8202020-02-12 23:04:21 +080084 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +080085 memset(p_ctx, 0, sizeof(Segment_Context_t));
Pengfei Liuc181a982020-01-07 19:27:13 +080086
87 memset(ts_fname, 0, sizeof(ts_fname));
88 segment_get_fname(ts_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_TS);
89
90 memset(index_fname, 0, sizeof(index_fname));
91 segment_get_fname(index_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_INDEX);
92
Pengfei Liub4734232020-01-17 18:25:10 +080093 memset(dat_fname, 0, sizeof(dat_fname));
94 segment_get_fname(dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_DAT);
95
Pengfei Liu3b1a8202020-02-12 23:04:21 +080096 memset(dir_name, 0, sizeof(dir_name));
97 segment_get_dirname(dir_name, params->location);
98 if (access(dir_name, F_OK) == -1) {
99 DVR_DEBUG(1, "%s dir %s is not exist, create it", __func__, dir_name);
100 mkdir(dir_name, 0666);
101 }
102
Pengfei Liuc181a982020-01-07 19:27:13 +0800103 if (params->mode == SEGMENT_MODE_READ) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800104 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800105 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800106 p_ctx->dat_fp = fopen(dat_fname, "r");
Pengfei Liuc181a982020-01-07 19:27:13 +0800107 } else if (params->mode == SEGMENT_MODE_WRITE) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800108 p_ctx->ts_fd = open(ts_fname, O_CREAT | O_RDWR | O_TRUNC, 0644);
Pengfei Liuc181a982020-01-07 19:27:13 +0800109 p_ctx->index_fp = fopen(index_fname, "w+");
Pengfei Liub4734232020-01-17 18:25:10 +0800110 p_ctx->dat_fp = fopen(dat_fname, "w+");
Pengfei Liub038b6a2020-01-14 15:57:01 +0800111 p_ctx->first_pts = ULLONG_MAX;
112 p_ctx->last_pts = ULLONG_MAX;
Pengfei Liuc181a982020-01-07 19:27:13 +0800113 } else {
114 DVR_DEBUG(1, "%s, unknow mode use default", __func__);
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");
Pengfei Liuc181a982020-01-07 19:27:13 +0800118 }
119
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800120 if (p_ctx->ts_fd == -1 || !p_ctx->index_fp || !p_ctx->dat_fp) {
Pengfei Liub4734232020-01-17 18:25:10 +0800121 DVR_DEBUG(1, "%s open file failed [%s, %s, %s], reason:%s", __func__,
122 ts_fname, index_fname, dat_fname, strerror(errno));
Pengfei Liuc181a982020-01-07 19:27:13 +0800123 free(p_ctx);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800124 *p_handle = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800125 return DVR_FAILURE;
126 }
Pengfei Liub4734232020-01-17 18:25:10 +0800127
128 DVR_DEBUG(1, "%s, open file success", __func__);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800129 *p_handle = (Segment_Handle_t)p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800130 return DVR_SUCCESS;
131}
132
133int segment_close(Segment_Handle_t handle)
134{
135 Segment_Context_t *p_ctx;
136
Pengfei Liub038b6a2020-01-14 15:57:01 +0800137 p_ctx = (void *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800138 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800139
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800140 if (p_ctx->ts_fd != -1) {
141 close(p_ctx->ts_fd);
Pengfei Liuc181a982020-01-07 19:27:13 +0800142 }
143
144 if (p_ctx->index_fp) {
145 fclose(p_ctx->index_fp);
146 }
147
Pengfei Liub4734232020-01-17 18:25:10 +0800148 if (p_ctx->dat_fp) {
149 fclose(p_ctx->dat_fp);
150 }
151
Pengfei Liuc181a982020-01-07 19:27:13 +0800152 free(p_ctx);
153 return 0;
154}
155
156ssize_t segment_read(Segment_Handle_t handle, void *buf, size_t count)
157{
158 Segment_Context_t *p_ctx;
159
160 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800161 DVR_RETURN_IF_FALSE(p_ctx);
162 DVR_RETURN_IF_FALSE(buf);
163 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800164
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800165 return read(p_ctx->ts_fd, buf, count);
Pengfei Liuc181a982020-01-07 19:27:13 +0800166}
167
168ssize_t segment_write(Segment_Handle_t handle, void *buf, size_t count)
169{
170 Segment_Context_t *p_ctx;
171
172 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800173 DVR_RETURN_IF_FALSE(p_ctx);
174 DVR_RETURN_IF_FALSE(buf);
175 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800176
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800177 return write(p_ctx->ts_fd, buf, count);
Pengfei Liuc181a982020-01-07 19:27:13 +0800178}
179
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800180int segment_update_pts(Segment_Handle_t handle, uint64_t pts, loff_t offset)
Pengfei Liuc181a982020-01-07 19:27:13 +0800181{
182 Segment_Context_t *p_ctx;
183 char buf[256];
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(p_ctx->index_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800188
Pengfei Liub038b6a2020-01-14 15:57:01 +0800189 if (p_ctx->first_pts == ULLONG_MAX) {
190 p_ctx->first_pts = pts;
191 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800192 memset(buf, 0, sizeof(buf));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800193 if (p_ctx->last_pts == ULLONG_MAX) {
194 /*Last pts is init value*/
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800195 sprintf(buf, "{time=%llu, offset=%lld}\n", pts - p_ctx->first_pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800196 } else {
197 /*Last pts has valid value*/
198 if (pts - p_ctx->last_pts > MAX_PTS_THRESHOLD) {
199 /*Current pts has a transition*/
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800200 sprintf(buf, "{time=%llu, offset=%lld}\n", p_ctx->last_pts - p_ctx->first_pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800201 } else {
202 /*This is a normal pts, record it*/
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800203 sprintf(buf, "{time=%llu, offset=%lld}\n", pts - p_ctx->first_pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800204 }
205 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800206
207 fputs(buf, p_ctx->index_fp);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800208 p_ctx->last_pts = pts;
209 fflush(p_ctx->index_fp);
210 fsync(fileno(p_ctx->index_fp));
Pengfei Liub4734232020-01-17 18:25:10 +0800211
Pengfei Liuc181a982020-01-07 19:27:13 +0800212 return DVR_SUCCESS;
213}
214
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800215loff_t segment_seek(Segment_Handle_t handle, uint64_t time)
Pengfei Liuc181a982020-01-07 19:27:13 +0800216{
217 Segment_Context_t *p_ctx;
218 char buf[256];
219 char value[256];
220 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800221 loff_t offset;
Pengfei Liuc181a982020-01-07 19:27:13 +0800222 char *p1, *p2;
223
224 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800225 DVR_RETURN_IF_FALSE(p_ctx);
226 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
227 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800228
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800229 memset(buf, 0, sizeof(buf));
230 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800231
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800232 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
Pengfei Liuc181a982020-01-07 19:27:13 +0800233 memset(value, 0, sizeof(value));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800234 if ((p1 = strstr(buf, "time="))) {
Pengfei Liuc181a982020-01-07 19:27:13 +0800235 p1 += 4;
236 if ((p2 = strstr(buf, ","))) {
237 memcpy(value, p1, p2 - p1);
238 }
239 pts = strtoull(value, NULL, 10);
240 }
241
242 memset(value, 0, sizeof(value));
243 if ((p1 = strstr(buf, "offset="))) {
244 p1 += 7;
245 if ((p2 = strstr(buf, "}"))) {
246 memcpy(value, p1, p2 - p1);
247 }
248 offset = strtoull(value, NULL, 10);
249 }
250
251 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800252 DVR_DEBUG(1, "time=%llu, offset=%lld\n", pts, offset);
Pengfei Liuc181a982020-01-07 19:27:13 +0800253 if (time < pts) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800254 DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800255 return offset;
256 }
257 }
258
259 return DVR_FAILURE;
260}
261
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800262loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800263{
264 Segment_Context_t *p_ctx;
265
266 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800267 DVR_RETURN_IF_FALSE(p_ctx);
268 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800269
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800270 return lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
271}
272
273uint64_t segment_tell_time(Segment_Handle_t handle)
274{
275 Segment_Context_t *p_ctx;
276 char buf[256];
277 char value[256];
278 uint64_t pts;
279 loff_t offset, position;
280 char *p1, *p2;
281
282 p_ctx = (Segment_Context_t *)handle;
283 DVR_RETURN_IF_FALSE(p_ctx);
284 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
285 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
286
287 memset(buf, 0, sizeof(buf));
288 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
289 position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
290 DVR_RETURN_IF_FALSE(position != -1);
291
292 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
293 memset(value, 0, sizeof(value));
294 if ((p1 = strstr(buf, "time="))) {
295 p1 += 4;
296 if ((p2 = strstr(buf, ","))) {
297 memcpy(value, p1, p2 - p1);
298 }
299 pts = strtoull(value, NULL, 10);
300 }
301
302 memset(value, 0, sizeof(value));
303 if ((p1 = strstr(buf, "offset="))) {
304 p1 += 7;
305 if ((p2 = strstr(buf, "}"))) {
306 memcpy(value, p1, p2 - p1);
307 }
308 offset = strtoull(value, NULL, 10);
309 }
310
311 memset(buf, 0, sizeof(buf));
312 DVR_DEBUG(1, "time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
313 if (position < offset) {
314 return pts;
315 }
316 }
317
318 return DVR_FAILURE;
Pengfei Liuc181a982020-01-07 19:27:13 +0800319}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800320
Pengfei Liub4734232020-01-17 18:25:10 +0800321int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
322{
323 Segment_Context_t *p_ctx;
324 char buf[256];
325 uint32_t i;
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->dat_fp);
330 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800331
332 /*Save segment id*/
333 memset(buf, 0, sizeof(buf));
334 sprintf(buf, "id=%lld\n", p_info->id);
335 fputs(buf, p_ctx->dat_fp);
336
337 /*Save number of pids*/
338 memset(buf, 0, sizeof(buf));
339 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
340 fputs(buf, p_ctx->dat_fp);
341
342 /*Save pid information*/
343 for (i = 0; i < p_info->nb_pids; i++) {
344 memset(buf, 0, sizeof(buf));
345 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
346 fputs(buf, p_ctx->dat_fp);
347 }
348
349 /*Save segment duration*/
350 memset(buf, 0, sizeof(buf));
351 sprintf(buf, "duration=%ld\n", p_info->duration);
352 fputs(buf, p_ctx->dat_fp);
353
354 /*Save segment size*/
355 memset(buf, 0, sizeof(buf));
356 sprintf(buf, "size=%zu\n", p_info->size);
357 fputs(buf, p_ctx->dat_fp);
358
359 /*Save number of packets*/
360 memset(buf, 0, sizeof(buf));
361 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
362 fputs(buf, p_ctx->dat_fp);
363
364 fflush(p_ctx->dat_fp);
365 fsync(fileno(p_ctx->dat_fp));
366 return DVR_SUCCESS;
367}
368
369int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
370{
371 Segment_Context_t *p_ctx;
372 uint32_t i;
373 char buf[256];
374 char value[256];
375 char *p1, *p2;
376
377 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800378 DVR_RETURN_IF_FALSE(p_ctx);
379 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800380
381 /*Load segment id*/
382 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800383 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800384 p1 = strstr(buf, "id=");
385 p_info->id = strtoull(p1 + 3, NULL, 10);
386
387 /*Save number of pids*/
388 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800389 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800390 p1 = strstr(buf, "nb_pids=");
391 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
392
393 /*Save pid information*/
394 for (i = 0; i < p_info->nb_pids; i++) {
395 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800396 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800397 memset(value, 0, sizeof(value));
398 if ((p1 = strstr(buf, "pid="))) {
399 p1 += 4;
400 if ((p2 = strstr(buf, ","))) {
401 memcpy(value, p1, p2 - p1);
402 }
403 p_info->pids[i].pid = strtoull(value, NULL, 10);
404 }
405
406 memset(value, 0, sizeof(value));
407 if ((p1 = strstr(buf, "type="))) {
408 p1 += 5;
409 if ((p2 = strstr(buf, "}"))) {
410 memcpy(value, p1, p2 - p1);
411 }
412 p_info->pids[i].type = strtoull(value, NULL, 10);
413 }
414 }
415
416 /*Save segment duration*/
417 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800418 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800419 p1 = strstr(buf, "duration=");
420 p_info->duration = strtoull(p1 + 9, NULL, 10);
421
422 /*Save segment size*/
423 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800424 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800425 p1 = strstr(buf, "size=");
426 p_info->size = strtoull(p1 + 5, NULL, 10);
427
428 /*Save number of packets*/
429 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800430 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800431 p1 = strstr(buf, "nb_packets=");
432 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
433
434 return DVR_SUCCESS;
435}
436
437int segment_delete(const char *location, uint64_t segment_id)
438{
439 char fname[MAX_SEGMENT_PATH_SIZE];
440 int ret;
441
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800442 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800443
444 /*delete ts file*/
445 memset(fname, 0, sizeof(fname));
446 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
447 ret = unlink(fname);
448 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800449 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800450
451 /*delete index file*/
452 memset(fname, 0, sizeof(fname));
453 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
454 unlink(fname);
455 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800456 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800457
458 /*delete store information file*/
459 memset(fname, 0, sizeof(fname));
460 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
461 unlink(fname);
462 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800463 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800464
465 return DVR_SUCCESS;
466}
467
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800468loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800469{
470 Segment_Context_t *p_ctx;
471 char buf[256];
472 char value[256];
473 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800474 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800475 char *p1, *p2;
476
477 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800478 DVR_RETURN_IF_FALSE(p_ctx);
479 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
480 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800481
482 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800483 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800484 printf("start gets pts\n");
485 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
486 printf("buf[%s]\n", buf);
487 memset(value, 0, sizeof(value));
488 if ((p1 = strstr(buf, "time="))) {
489 p1 += 4;
490 if ((p2 = strstr(buf, ","))) {
491 memcpy(value, p1, p2 - p1);
492 }
493 pts = strtoull(value, NULL, 10);
494 }
495
496 memset(value, 0, sizeof(value));
497 if ((p1 = strstr(buf, "offset="))) {
498 p1 += 7;
499 if ((p2 = strstr(buf, "}"))) {
500 memcpy(value, p1, p2 - p1);
501 }
502 offset = strtoull(value, NULL, 10);
503 }
504
505 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800506 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800507 }
508
509 return 0;
510}