blob: 9bf4c1267b5120f735c8e82224e2ed063e0bb7a4 [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*/
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800197 sprintf(buf, "{time=%llu, offset=%lld}\n", 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);
208 sprintf(buf, "{time=%llu, offset=%lld}\n", 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];
225 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800226 loff_t offset;
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);
Pengfei Liuc181a982020-01-07 19:27:13 +0800236
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800237 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
Pengfei Liuc181a982020-01-07 19:27:13 +0800238 memset(value, 0, sizeof(value));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800239 if ((p1 = strstr(buf, "time="))) {
Pengfei Liuc181a982020-01-07 19:27:13 +0800240 p1 += 4;
241 if ((p2 = strstr(buf, ","))) {
242 memcpy(value, p1, p2 - p1);
243 }
244 pts = strtoull(value, NULL, 10);
245 }
246
247 memset(value, 0, sizeof(value));
248 if ((p1 = strstr(buf, "offset="))) {
249 p1 += 7;
250 if ((p2 = strstr(buf, "}"))) {
251 memcpy(value, p1, p2 - p1);
252 }
253 offset = strtoull(value, NULL, 10);
254 }
255
256 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800257 DVR_DEBUG(1, "time=%llu, offset=%lld\n", pts, offset);
Pengfei Liuc181a982020-01-07 19:27:13 +0800258 if (time < pts) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800259 DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800260 return offset;
261 }
262 }
263
264 return DVR_FAILURE;
265}
266
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800267loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800268{
269 Segment_Context_t *p_ctx;
270
271 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800272 DVR_RETURN_IF_FALSE(p_ctx);
273 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800274
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800275 return lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
276}
277
278uint64_t segment_tell_time(Segment_Handle_t handle)
279{
280 Segment_Context_t *p_ctx;
281 char buf[256];
282 char value[256];
283 uint64_t pts;
284 loff_t offset, position;
285 char *p1, *p2;
286
287 p_ctx = (Segment_Context_t *)handle;
288 DVR_RETURN_IF_FALSE(p_ctx);
289 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
290 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
291
292 memset(buf, 0, sizeof(buf));
293 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
294 position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
295 DVR_RETURN_IF_FALSE(position != -1);
296
297 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
298 memset(value, 0, sizeof(value));
299 if ((p1 = strstr(buf, "time="))) {
300 p1 += 4;
301 if ((p2 = strstr(buf, ","))) {
302 memcpy(value, p1, p2 - p1);
303 }
304 pts = strtoull(value, NULL, 10);
305 }
306
307 memset(value, 0, sizeof(value));
308 if ((p1 = strstr(buf, "offset="))) {
309 p1 += 7;
310 if ((p2 = strstr(buf, "}"))) {
311 memcpy(value, p1, p2 - p1);
312 }
313 offset = strtoull(value, NULL, 10);
314 }
315
316 memset(buf, 0, sizeof(buf));
pengfei.liuab5a2262020-02-14 17:33:40 +0800317 //DVR_DEBUG(1, "time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800318 if (position < offset) {
319 return pts;
320 }
321 }
322
323 return DVR_FAILURE;
Pengfei Liuc181a982020-01-07 19:27:13 +0800324}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800325
pengfei.liuab5a2262020-02-14 17:33:40 +0800326/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800327int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
328{
329 Segment_Context_t *p_ctx;
330 char buf[256];
331 uint32_t i;
332
333 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800334 DVR_RETURN_IF_FALSE(p_ctx);
335 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
336 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800337
338 /*Save segment id*/
339 memset(buf, 0, sizeof(buf));
340 sprintf(buf, "id=%lld\n", p_info->id);
341 fputs(buf, p_ctx->dat_fp);
342
343 /*Save number of pids*/
344 memset(buf, 0, sizeof(buf));
345 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
346 fputs(buf, p_ctx->dat_fp);
347
348 /*Save pid information*/
349 for (i = 0; i < p_info->nb_pids; i++) {
350 memset(buf, 0, sizeof(buf));
351 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
352 fputs(buf, p_ctx->dat_fp);
353 }
354
355 /*Save segment duration*/
356 memset(buf, 0, sizeof(buf));
357 sprintf(buf, "duration=%ld\n", p_info->duration);
358 fputs(buf, p_ctx->dat_fp);
359
360 /*Save segment size*/
361 memset(buf, 0, sizeof(buf));
362 sprintf(buf, "size=%zu\n", p_info->size);
363 fputs(buf, p_ctx->dat_fp);
364
365 /*Save number of packets*/
366 memset(buf, 0, sizeof(buf));
367 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
368 fputs(buf, p_ctx->dat_fp);
369
370 fflush(p_ctx->dat_fp);
371 fsync(fileno(p_ctx->dat_fp));
372 return DVR_SUCCESS;
373}
374
pengfei.liuab5a2262020-02-14 17:33:40 +0800375/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800376int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
377{
378 Segment_Context_t *p_ctx;
379 uint32_t i;
380 char buf[256];
381 char value[256];
382 char *p1, *p2;
383
384 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800385 DVR_RETURN_IF_FALSE(p_ctx);
386 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800387
388 /*Load segment id*/
389 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800390 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800391 p1 = strstr(buf, "id=");
392 p_info->id = strtoull(p1 + 3, NULL, 10);
393
394 /*Save number of pids*/
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 p1 = strstr(buf, "nb_pids=");
398 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
399
400 /*Save pid information*/
401 for (i = 0; i < p_info->nb_pids; i++) {
402 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800403 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800404 memset(value, 0, sizeof(value));
405 if ((p1 = strstr(buf, "pid="))) {
406 p1 += 4;
407 if ((p2 = strstr(buf, ","))) {
408 memcpy(value, p1, p2 - p1);
409 }
410 p_info->pids[i].pid = strtoull(value, NULL, 10);
411 }
412
413 memset(value, 0, sizeof(value));
414 if ((p1 = strstr(buf, "type="))) {
415 p1 += 5;
416 if ((p2 = strstr(buf, "}"))) {
417 memcpy(value, p1, p2 - p1);
418 }
419 p_info->pids[i].type = strtoull(value, NULL, 10);
420 }
421 }
422
423 /*Save segment duration*/
424 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800425 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800426 p1 = strstr(buf, "duration=");
427 p_info->duration = strtoull(p1 + 9, NULL, 10);
428
429 /*Save segment size*/
430 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800431 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800432 p1 = strstr(buf, "size=");
433 p_info->size = strtoull(p1 + 5, NULL, 10);
434
435 /*Save number of packets*/
436 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800437 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800438 p1 = strstr(buf, "nb_packets=");
439 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
440
441 return DVR_SUCCESS;
442}
443
444int segment_delete(const char *location, uint64_t segment_id)
445{
446 char fname[MAX_SEGMENT_PATH_SIZE];
447 int ret;
448
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800449 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800450
451 /*delete ts file*/
452 memset(fname, 0, sizeof(fname));
453 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
454 ret = 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 index file*/
459 memset(fname, 0, sizeof(fname));
460 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
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 /*delete store information file*/
466 memset(fname, 0, sizeof(fname));
467 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
468 unlink(fname);
469 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800470 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800471
472 return DVR_SUCCESS;
473}
474
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800475loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800476{
477 Segment_Context_t *p_ctx;
478 char buf[256];
479 char value[256];
480 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800481 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800482 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_ctx->index_fp);
487 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800488
489 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800490 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800491 printf("start gets pts\n");
492 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
493 printf("buf[%s]\n", buf);
494 memset(value, 0, sizeof(value));
495 if ((p1 = strstr(buf, "time="))) {
496 p1 += 4;
497 if ((p2 = strstr(buf, ","))) {
498 memcpy(value, p1, p2 - p1);
499 }
500 pts = strtoull(value, NULL, 10);
501 }
502
503 memset(value, 0, sizeof(value));
504 if ((p1 = strstr(buf, "offset="))) {
505 p1 += 7;
506 if ((p2 = strstr(buf, "}"))) {
507 memcpy(value, p1, p2 - p1);
508 }
509 offset = strtoull(value, NULL, 10);
510 }
511
512 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800513 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800514 }
515
516 return 0;
517}