blob: a464d0e049db2260cc6c2043d9f6321b76312483 [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
pengfei.liu8b563292020-02-26 15:49:02 +0800278uint64_t segment_tell_current_time(Segment_Handle_t handle)
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800279{
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.liu8b563292020-02-26 15:49:02 +0800326uint64_t segment_tell_total_time(Segment_Handle_t handle)
327{
328 Segment_Context_t *p_ctx;
329 char buf[256];
330 char last_buf[256];
331 char value[256];
332 uint64_t pts = ULLONG_MAX;
333 loff_t offset = 0, position = 0;
334 char *p1, *p2;
335 int line = 0;
336
337 p_ctx = (Segment_Context_t *)handle;
338 DVR_RETURN_IF_FALSE(p_ctx);
339 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
340 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
341
342 memset(buf, 0, sizeof(buf));
343 memset(last_buf, 0, sizeof(last_buf));
344 position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
345 DVR_RETURN_IF_FALSE(position != -1);
346
347 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -100L, SEEK_END) != -1);
348 /* Save last line buffer */
349 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
350 memset(last_buf, 0, sizeof(last_buf));
351 memcpy(last_buf, buf, strlen(buf));
352 memset(buf, 0, sizeof(buf));
353 line++;
354 }
355
356 /* Extract time value */
357 memset(value, 0, sizeof(value));
358 if ((p1 = strstr(last_buf, "time="))) {
359 p1 += 5;
360 if ((p2 = strstr(last_buf, ","))) {
361 memcpy(value, p1, p2 - p1);
362 }
363 pts = strtoull(value, NULL, 10);
364 }
365
366 memset(value, 0, sizeof(value));
367 if ((p1 = strstr(last_buf, "offset="))) {
368 p1 += 7;
369 if ((p2 = strstr(last_buf, "}"))) {
370 memcpy(value, p1, p2 - p1);
371 }
372 offset = strtoull(value, NULL, 10);
373 }
374 if (line < 2)
375 DVR_DEBUG(1, "time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line);
376
377 return (pts == ULLONG_MAX ? DVR_FAILURE : pts);
378}
379
pengfei.liuab5a2262020-02-14 17:33:40 +0800380/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800381int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
382{
383 Segment_Context_t *p_ctx;
384 char buf[256];
385 uint32_t i;
386
387 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800388 DVR_RETURN_IF_FALSE(p_ctx);
389 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
390 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800391
392 /*Save segment id*/
393 memset(buf, 0, sizeof(buf));
394 sprintf(buf, "id=%lld\n", p_info->id);
395 fputs(buf, p_ctx->dat_fp);
396
397 /*Save number of pids*/
398 memset(buf, 0, sizeof(buf));
399 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
400 fputs(buf, p_ctx->dat_fp);
401
402 /*Save pid information*/
403 for (i = 0; i < p_info->nb_pids; i++) {
404 memset(buf, 0, sizeof(buf));
405 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
406 fputs(buf, p_ctx->dat_fp);
407 }
408
409 /*Save segment duration*/
410 memset(buf, 0, sizeof(buf));
411 sprintf(buf, "duration=%ld\n", p_info->duration);
412 fputs(buf, p_ctx->dat_fp);
413
414 /*Save segment size*/
415 memset(buf, 0, sizeof(buf));
416 sprintf(buf, "size=%zu\n", p_info->size);
417 fputs(buf, p_ctx->dat_fp);
418
419 /*Save number of packets*/
420 memset(buf, 0, sizeof(buf));
421 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
422 fputs(buf, p_ctx->dat_fp);
423
424 fflush(p_ctx->dat_fp);
425 fsync(fileno(p_ctx->dat_fp));
426 return DVR_SUCCESS;
427}
428
pengfei.liuab5a2262020-02-14 17:33:40 +0800429/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800430int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
431{
432 Segment_Context_t *p_ctx;
433 uint32_t i;
434 char buf[256];
435 char value[256];
436 char *p1, *p2;
437
438 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800439 DVR_RETURN_IF_FALSE(p_ctx);
440 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800441
442 /*Load segment id*/
443 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800444 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800445 p1 = strstr(buf, "id=");
446 p_info->id = strtoull(p1 + 3, NULL, 10);
447
448 /*Save number of pids*/
449 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800450 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800451 p1 = strstr(buf, "nb_pids=");
452 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
453
454 /*Save pid information*/
455 for (i = 0; i < p_info->nb_pids; i++) {
456 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800457 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800458 memset(value, 0, sizeof(value));
459 if ((p1 = strstr(buf, "pid="))) {
460 p1 += 4;
461 if ((p2 = strstr(buf, ","))) {
462 memcpy(value, p1, p2 - p1);
463 }
464 p_info->pids[i].pid = strtoull(value, NULL, 10);
465 }
466
467 memset(value, 0, sizeof(value));
468 if ((p1 = strstr(buf, "type="))) {
469 p1 += 5;
470 if ((p2 = strstr(buf, "}"))) {
471 memcpy(value, p1, p2 - p1);
472 }
473 p_info->pids[i].type = strtoull(value, NULL, 10);
474 }
475 }
476
477 /*Save segment duration*/
478 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800479 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800480 p1 = strstr(buf, "duration=");
481 p_info->duration = strtoull(p1 + 9, NULL, 10);
482
483 /*Save segment size*/
484 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800485 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800486 p1 = strstr(buf, "size=");
487 p_info->size = strtoull(p1 + 5, NULL, 10);
488
489 /*Save number of packets*/
490 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800491 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800492 p1 = strstr(buf, "nb_packets=");
493 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
494
495 return DVR_SUCCESS;
496}
497
498int segment_delete(const char *location, uint64_t segment_id)
499{
500 char fname[MAX_SEGMENT_PATH_SIZE];
501 int ret;
502
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800503 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800504
505 /*delete ts file*/
506 memset(fname, 0, sizeof(fname));
507 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
508 ret = unlink(fname);
509 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800510 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800511
512 /*delete index file*/
513 memset(fname, 0, sizeof(fname));
514 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
515 unlink(fname);
516 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800517 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800518
519 /*delete store information file*/
520 memset(fname, 0, sizeof(fname));
521 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
522 unlink(fname);
523 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800524 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800525
526 return DVR_SUCCESS;
527}
528
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800529loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800530{
531 Segment_Context_t *p_ctx;
532 char buf[256];
533 char value[256];
534 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800535 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800536 char *p1, *p2;
537
538 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800539 DVR_RETURN_IF_FALSE(p_ctx);
540 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
541 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800542
543 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800544 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800545 printf("start gets pts\n");
546 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
547 printf("buf[%s]\n", buf);
548 memset(value, 0, sizeof(value));
549 if ((p1 = strstr(buf, "time="))) {
550 p1 += 4;
551 if ((p2 = strstr(buf, ","))) {
552 memcpy(value, p1, p2 - p1);
553 }
554 pts = strtoull(value, NULL, 10);
555 }
556
557 memset(value, 0, sizeof(value));
558 if ((p1 = strstr(buf, "offset="))) {
559 p1 += 7;
560 if ((p2 = strstr(buf, "}"))) {
561 memcpy(value, p1, p2 - p1);
562 }
563 offset = strtoull(value, NULL, 10);
564 }
565
566 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800567 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800568 }
569
570 return 0;
571}