blob: e0c11eb388a66f29165f50e727388107d158ccd7 [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)
hualing chen06209692020-11-05 14:51:46 +080015#define PCR_RECORD_INTERVAL_MS (300)
hualing chen2932d372020-04-29 13:44:00 +080016#define PTS_DISCONTINE_DEVIATION (40)
17#define PTS_HEAD_DEVIATION (40)
hualing chen86b8e602021-12-23 11:04:19 +080018#define PCR_JUMP_DUR (5000)
hualing chen2932d372020-04-29 13:44:00 +080019
20
Pengfei Liub4734232020-01-17 18:25:10 +080021/**\brief Segment context*/
Pengfei Liuc181a982020-01-07 19:27:13 +080022typedef struct {
Pengfei Liu3b1a8202020-02-12 23:04:21 +080023 int ts_fd; /**< Segment ts file fd*/
Pengfei Liub4734232020-01-17 18:25:10 +080024 FILE *index_fp; /**< Time index file fd*/
25 FILE *dat_fp; /**< Information file fd*/
hualing chenb9a02922021-12-14 11:29:47 +080026 FILE *all_dat_fp; /**< Information file fd*/
pengfei.liu567d6d82020-04-17 16:48:59 +080027 FILE *ongoing_fp; /**< Ongoing file fd, used to verify timedhift mode*/
Pengfei Liub4734232020-01-17 18:25:10 +080028 uint64_t first_pts; /**< First pts value, use for write mode*/
pengfei.liu567d6d82020-04-17 16:48:59 +080029 uint64_t last_pts; /**< Last input pts value, use for write mode*/
30 uint64_t last_record_pts; /**< Last record pts value, use for write mode*/
pengfei.liuab5a2262020-02-14 17:33:40 +080031 uint64_t cur_time; /**< Current time save in index file */
pengfei.liu567d6d82020-04-17 16:48:59 +080032 uint64_t segment_id; /**< Current segment ID */
33 char location[MAX_SEGMENT_PATH_SIZE]; /**< Current time save in index file */
hualing chena5f03222021-12-02 11:22:35 +080034 loff_t first_offset;
35 loff_t last_offset;
36 loff_t last_record_offset;
37 float avg_rate;
Pengfei Liuc181a982020-01-07 19:27:13 +080038} Segment_Context_t;
39
Pengfei Liub4734232020-01-17 18:25:10 +080040/**\brief Segment file type*/
Pengfei Liuc181a982020-01-07 19:27:13 +080041typedef enum {
Pengfei Liub4734232020-01-17 18:25:10 +080042 SEGMENT_FILE_TYPE_TS, /**< Used for store TS data*/
43 SEGMENT_FILE_TYPE_INDEX, /**< Used for store index data*/
44 SEGMENT_FILE_TYPE_DAT, /**< Used for store information data, such as duration etc*/
pengfei.liu567d6d82020-04-17 16:48:59 +080045 SEGMENT_FILE_TYPE_ONGOING, /**< Used for store information data, such as duration etc*/
hualing chenb9a02922021-12-14 11:29:47 +080046 SEGMENT_FILE_TYPE_ALLDAT, /**< Used for store all information data*/
Pengfei Liuc181a982020-01-07 19:27:13 +080047} Segment_FileType_t;
48
49static void segment_get_fname(char fname[MAX_SEGMENT_PATH_SIZE],
Pengfei Liub4734232020-01-17 18:25:10 +080050 const char location[DVR_MAX_LOCATION_SIZE],
Pengfei Liuc181a982020-01-07 19:27:13 +080051 uint64_t segment_id,
52 Segment_FileType_t type)
53{
54 int offset;
55
56 memset(fname, 0, MAX_SEGMENT_PATH_SIZE);
57 strncpy(fname, location, strlen(location));
58 offset = strlen(location);
hualing chenb9a02922021-12-14 11:29:47 +080059 if (type != SEGMENT_FILE_TYPE_ALLDAT) {
60 strncpy(fname + offset, "-", 1);
61 offset += 1;
62 sprintf(fname + offset, "%04llu", segment_id);
63 offset += 4;
64 }
65
Pengfei Liuc181a982020-01-07 19:27:13 +080066 if (type == SEGMENT_FILE_TYPE_TS)
67 strncpy(fname + offset, ".ts", 3);
68 else if (type == SEGMENT_FILE_TYPE_INDEX)
69 strncpy(fname + offset, ".idx", 4);
Pengfei Liub4734232020-01-17 18:25:10 +080070 else if (type == SEGMENT_FILE_TYPE_DAT)
71 strncpy(fname + offset, ".dat", 4);
hualing chen87072a82020-03-12 16:20:12 +080072 else if (type == SEGMENT_FILE_TYPE_ONGOING)
73 strncpy(fname + offset, ".going", 6);
hualing chenb9a02922021-12-14 11:29:47 +080074 else if (type == SEGMENT_FILE_TYPE_ALLDAT)
75 strncpy(fname + offset, ".dat", 4);
76
Pengfei Liuc181a982020-01-07 19:27:13 +080077}
78
Pengfei Liu3b1a8202020-02-12 23:04:21 +080079static void segment_get_dirname(char dir_name[MAX_SEGMENT_PATH_SIZE],
80 const char location[DVR_MAX_LOCATION_SIZE])
81{
82 char *p;
83 int i;
84 int found = 0;
85
86 for (i = 0; i < (int)strlen(location); i++) {
87 if (location[i] == '/') {
88 p = (char *)location + i;
89 found = 1;
90 }
91 }
92 if (found)
93 memcpy(dir_name, location, p - location);
94}
95
Pengfei Liuc181a982020-01-07 19:27:13 +080096int segment_open(Segment_OpenParams_t *params, Segment_Handle_t *p_handle)
97{
98 Segment_Context_t *p_ctx;
99 char ts_fname[MAX_SEGMENT_PATH_SIZE];
100 char index_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liub4734232020-01-17 18:25:10 +0800101 char dat_fname[MAX_SEGMENT_PATH_SIZE];
hualing chenb9a02922021-12-14 11:29:47 +0800102 char all_dat_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800103 char dir_name[MAX_SEGMENT_PATH_SIZE];
hualing chen87072a82020-03-12 16:20:12 +0800104 char going_name[MAX_SEGMENT_PATH_SIZE];
Pengfei Liuc181a982020-01-07 19:27:13 +0800105
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800106 DVR_RETURN_IF_FALSE(params);
107 DVR_RETURN_IF_FALSE(p_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +0800108
hualing chena540a7e2020-03-27 16:44:05 +0800109 //DVR_DEBUG(1, "%s, location:%s, id:%llu", __func__, params->location, params->segment_id);
Pengfei Liuc181a982020-01-07 19:27:13 +0800110
Pengfei Liub038b6a2020-01-14 15:57:01 +0800111 p_ctx = (void*)malloc(sizeof(Segment_Context_t));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800112 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +0800113 memset(p_ctx, 0, sizeof(Segment_Context_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800114
115 memset(ts_fname, 0, sizeof(ts_fname));
116 segment_get_fname(ts_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_TS);
117
118 memset(index_fname, 0, sizeof(index_fname));
119 segment_get_fname(index_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_INDEX);
120
Pengfei Liub4734232020-01-17 18:25:10 +0800121 memset(dat_fname, 0, sizeof(dat_fname));
122 segment_get_fname(dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_DAT);
123
hualing chenb9a02922021-12-14 11:29:47 +0800124 memset(all_dat_fname, 0, sizeof(all_dat_fname));
125 segment_get_fname(all_dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_ALLDAT);
126
127
hualing chen87072a82020-03-12 16:20:12 +0800128 memset(going_name, 0, sizeof(going_name));
129 segment_get_fname(going_name, params->location, params->segment_id, SEGMENT_FILE_TYPE_ONGOING);
130
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800131 memset(dir_name, 0, sizeof(dir_name));
132 segment_get_dirname(dir_name, params->location);
133 if (access(dir_name, F_OK) == -1) {
134 DVR_DEBUG(1, "%s dir %s is not exist, create it", __func__, dir_name);
135 mkdir(dir_name, 0666);
136 }
137
Pengfei Liuc181a982020-01-07 19:27:13 +0800138 if (params->mode == SEGMENT_MODE_READ) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800139 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800140 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800141 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800142 p_ctx->ongoing_fp = NULL;
hualing chenb9a02922021-12-14 11:29:47 +0800143 p_ctx->all_dat_fp = fopen(all_dat_fname, "r");
Pengfei Liuc181a982020-01-07 19:27:13 +0800144 } else if (params->mode == SEGMENT_MODE_WRITE) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800145 p_ctx->ts_fd = open(ts_fname, O_CREAT | O_RDWR | O_TRUNC, 0644);
Pengfei Liuc181a982020-01-07 19:27:13 +0800146 p_ctx->index_fp = fopen(index_fname, "w+");
Pengfei Liub4734232020-01-17 18:25:10 +0800147 p_ctx->dat_fp = fopen(dat_fname, "w+");
hualing chenb9a02922021-12-14 11:29:47 +0800148 p_ctx->all_dat_fp = fopen(all_dat_fname, "a+");
hualing chen8aed9582021-12-24 17:59:56 +0800149 DVR_DEBUG(1, "%s dir %s is opened", __func__, all_dat_fname);
hualing chen87072a82020-03-12 16:20:12 +0800150 p_ctx->ongoing_fp = fopen(going_name, "w+");
Pengfei Liub038b6a2020-01-14 15:57:01 +0800151 p_ctx->first_pts = ULLONG_MAX;
152 p_ctx->last_pts = ULLONG_MAX;
pengfei.liu567d6d82020-04-17 16:48:59 +0800153 p_ctx->last_record_pts = ULLONG_MAX;
hualing chena5f03222021-12-02 11:22:35 +0800154 p_ctx->avg_rate = 0.0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800155 } else {
156 DVR_DEBUG(1, "%s, unknow mode use default", __func__);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800157 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800158 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800159 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chenb9a02922021-12-14 11:29:47 +0800160 p_ctx->all_dat_fp = fopen(all_dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800161 p_ctx->ongoing_fp = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800162 }
163
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800164 if (p_ctx->ts_fd == -1 || !p_ctx->index_fp || !p_ctx->dat_fp) {
Pengfei Liub4734232020-01-17 18:25:10 +0800165 DVR_DEBUG(1, "%s open file failed [%s, %s, %s], reason:%s", __func__,
166 ts_fname, index_fname, dat_fname, strerror(errno));
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800167 if (p_ctx->ts_fd != -1)
168 close(p_ctx->ts_fd);
169 if (p_ctx->index_fp)
170 fclose(p_ctx->index_fp);
171 if (p_ctx->dat_fp)
172 fclose(p_ctx->dat_fp);
hualing chen926a8ec2021-12-20 20:38:24 +0800173 if (p_ctx->all_dat_fp)
174 fclose(p_ctx->all_dat_fp);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800175 if (p_ctx->ongoing_fp)
176 fclose(p_ctx->ongoing_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800177 free(p_ctx);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800178 *p_handle = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800179 return DVR_FAILURE;
180 }
hualing chen87072a82020-03-12 16:20:12 +0800181 p_ctx->segment_id = params->segment_id;
182 strncpy(p_ctx->location, params->location, strlen(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800183
hualing chen7a56cba2020-04-14 14:09:27 +0800184 //DVR_DEBUG(1, "%s, open file success p_ctx->location [%s]", __func__, p_ctx->location, params->mode);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800185 *p_handle = (Segment_Handle_t)p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800186 return DVR_SUCCESS;
187}
188
189int segment_close(Segment_Handle_t handle)
190{
191 Segment_Context_t *p_ctx;
192
Pengfei Liub038b6a2020-01-14 15:57:01 +0800193 p_ctx = (void *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800194 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800195
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800196 if (p_ctx->ts_fd != -1) {
197 close(p_ctx->ts_fd);
Pengfei Liuc181a982020-01-07 19:27:13 +0800198 }
199
200 if (p_ctx->index_fp) {
201 fclose(p_ctx->index_fp);
202 }
203
Pengfei Liub4734232020-01-17 18:25:10 +0800204 if (p_ctx->dat_fp) {
205 fclose(p_ctx->dat_fp);
206 }
hualing chen926a8ec2021-12-20 20:38:24 +0800207 if (p_ctx->all_dat_fp) {
208 fclose(p_ctx->all_dat_fp);
209 }
hualing chen87072a82020-03-12 16:20:12 +0800210 if (p_ctx->ongoing_fp != NULL) {
211 fclose(p_ctx->ongoing_fp);
212 char going_name[MAX_SEGMENT_PATH_SIZE];
213 memset(going_name, 0, sizeof(going_name));
214 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
215 DVR_DEBUG(1, "segment close del [%s]", going_name);
216 unlink(going_name);
217 }
218
Pengfei Liuc181a982020-01-07 19:27:13 +0800219 free(p_ctx);
220 return 0;
221}
222
223ssize_t segment_read(Segment_Handle_t handle, void *buf, size_t count)
224{
225 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800226 ssize_t len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800227 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800228 DVR_RETURN_IF_FALSE(p_ctx);
229 DVR_RETURN_IF_FALSE(buf);
230 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800231 len = read(p_ctx->ts_fd, buf, count);
232 return len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800233}
234
235ssize_t segment_write(Segment_Handle_t handle, void *buf, size_t count)
236{
237 Segment_Context_t *p_ctx;
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800238 ssize_t len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800239 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800240 DVR_RETURN_IF_FALSE(p_ctx);
241 DVR_RETURN_IF_FALSE(buf);
242 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800243 len = write(p_ctx->ts_fd, buf, count);
244 fsync(p_ctx->ts_fd);
245 return len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800246}
247
hualing chen2932d372020-04-29 13:44:00 +0800248int segment_update_pts_force(Segment_Handle_t handle, uint64_t pts, loff_t offset)
Pengfei Liuc181a982020-01-07 19:27:13 +0800249{
250 Segment_Context_t *p_ctx;
251 char buf[256];
pengfei.liu567d6d82020-04-17 16:48:59 +0800252 int record_diff = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800253
254 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800255 DVR_RETURN_IF_FALSE(p_ctx);
256 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800257
Pengfei Liub038b6a2020-01-14 15:57:01 +0800258 if (p_ctx->first_pts == ULLONG_MAX) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800259 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800260 p_ctx->first_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800261 p_ctx->first_offset = offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800262 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800263 memset(buf, 0, sizeof(buf));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800264 if (p_ctx->last_pts == ULLONG_MAX) {
265 /*Last pts is init value*/
hualing chen2aba4022020-03-02 13:49:55 +0800266 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
pengfei.liuab5a2262020-02-14 17:33:40 +0800267 p_ctx->cur_time = pts - p_ctx->first_pts;
hualing chena5f03222021-12-02 11:22:35 +0800268 DVR_DEBUG(1, "%s force pcr:%llu -1", __func__, pts);
hualing chen2932d372020-04-29 13:44:00 +0800269 } else {
270 /*Last pts has valid value*/
271 int diff = pts - p_ctx->last_pts;
272 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
273 /*Current pts has a transition*/
274 DVR_DEBUG(1, "[%s]force update Current pts has a transition, [%llu, %llu, %llu]",__func__,
275 p_ctx->first_pts, p_ctx->last_pts, pts);
276 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
277 } else {
278 /*This is a normal pts, record it*/
hualing chena5f03222021-12-02 11:22:35 +0800279 //check if this pcr is transition.if true,add 200ms
280 //other case normal.
hualing chen2932d372020-04-29 13:44:00 +0800281 p_ctx->cur_time += diff;
282 DVR_DEBUG(1, "%s force pcr:%llu -1 diff [%d]", __func__, pts, diff);
283 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
284 }
285 }
286
287 record_diff = pts - p_ctx->last_record_pts;
288 if (strlen(buf) > 0) {
289 DVR_DEBUG(1, "%s force pcr:%llu buf:%s", __func__, pts, buf);
290 fputs(buf, p_ctx->index_fp);
291 fflush(p_ctx->index_fp);
292 fsync(fileno(p_ctx->index_fp));
293 p_ctx->last_record_pts = pts;
294 }
295 p_ctx->last_pts = pts;
hualing chen2932d372020-04-29 13:44:00 +0800296 return DVR_SUCCESS;
297}
298
299int segment_update_pts(Segment_Handle_t handle, uint64_t pts, loff_t offset)
300{
301 Segment_Context_t *p_ctx;
302 char buf[256];
303 int record_diff = 0;
304
305 p_ctx = (Segment_Context_t *)handle;
306 DVR_RETURN_IF_FALSE(p_ctx);
307 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
308
309 if (p_ctx->first_pts == ULLONG_MAX) {
310 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
311 p_ctx->first_pts = pts;
312 //p_ctx->cur_time = p_ctx->cur_time + PTS_HEAD_DEVIATION;
313 }
314 memset(buf, 0, sizeof(buf));
315 if (p_ctx->last_pts == ULLONG_MAX) {
316 /*Last pts is init value*/
317 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
318 p_ctx->cur_time = pts - p_ctx->first_pts;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800319 } else {
320 /*Last pts has valid value*/
pengfei.liu567d6d82020-04-17 16:48:59 +0800321 int diff = pts - p_ctx->last_pts;
hualing chen4b7c15d2020-04-07 16:13:48 +0800322 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
Pengfei Liub038b6a2020-01-14 15:57:01 +0800323 /*Current pts has a transition*/
pengfei.liuab5a2262020-02-14 17:33:40 +0800324 DVR_DEBUG(1, "Current pts has a transition, [%llu, %llu, %llu]",
325 p_ctx->first_pts, p_ctx->last_pts, pts);
pengfei.liu567d6d82020-04-17 16:48:59 +0800326 p_ctx->last_record_pts = pts;
hualing chen2932d372020-04-29 13:44:00 +0800327 //p_ctx->cur_time = p_ctx->cur_time + PTS_DISCONTINE_DEVIATION;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800328 } else {
329 /*This is a normal pts, record it*/
hualing chena5f03222021-12-02 11:22:35 +0800330 loff_t off_diff = offset - p_ctx->last_offset;
331 float rate = (float) (off_diff) / (float)(diff);
332 if (p_ctx->avg_rate == 0.0) {
333 p_ctx->avg_rate = (float) offset / (float)(p_ctx->cur_time + diff);
334 }
hualing chen86b8e602021-12-23 11:04:19 +0800335 if (diff >= PCR_JUMP_DUR) {
hualing chena5f03222021-12-02 11:22:35 +0800336 DVR_DEBUG(1, "PTS TRANSITION ERROR last pcr[%llu]pts[%llu]pcr diff[%d]off[%llu]off_diff[%llu]rate[%f]avg rate[%f]4*rate[%d]av_diff[%d]",p_ctx->last_pts, pts, diff, offset,off_diff, rate, p_ctx->avg_rate, (int)(rate * 4), (int)(off_diff / p_ctx->avg_rate));
337 if (p_ctx->avg_rate != 0 && (int)(p_ctx->avg_rate) >= (int)(rate * 4)) {
338 diff = off_diff / p_ctx->avg_rate;
339 p_ctx->cur_time += diff;
340 } else {
341 p_ctx->cur_time += diff;
342 }
343 } else {
344 p_ctx->cur_time += diff;
345 }
hualing chen2aba4022020-03-02 13:49:55 +0800346 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800347 }
348 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800349
350 record_diff = pts - p_ctx->last_record_pts;
351 if (strlen(buf) > 0 &&
352 (record_diff > PCR_RECORD_INTERVAL_MS || p_ctx->last_record_pts == ULLONG_MAX)){
hualing chen4b7c15d2020-04-07 16:13:48 +0800353 fputs(buf, p_ctx->index_fp);
pengfei.liu567d6d82020-04-17 16:48:59 +0800354 fflush(p_ctx->index_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800355 fsync(fileno(p_ctx->index_fp));
pengfei.liu567d6d82020-04-17 16:48:59 +0800356 p_ctx->last_record_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800357 p_ctx->last_record_offset = offset;
358 if (p_ctx->cur_time > 0)
359 p_ctx->avg_rate = (float) offset / (float)p_ctx->cur_time;
pengfei.liu567d6d82020-04-17 16:48:59 +0800360 }
Pengfei Liub038b6a2020-01-14 15:57:01 +0800361 p_ctx->last_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800362 p_ctx->last_offset = offset;
Pengfei Liuc181a982020-01-07 19:27:13 +0800363 return DVR_SUCCESS;
364}
365
hualing chen266b9502020-04-04 17:39:39 +0800366loff_t segment_seek(Segment_Handle_t handle, uint64_t time, int block_size)
Pengfei Liuc181a982020-01-07 19:27:13 +0800367{
368 Segment_Context_t *p_ctx;
369 char buf[256];
370 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800371 uint64_t pts = 0L;
372 loff_t offset = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800373 char *p1, *p2;
374
hualing chen8a657f32021-08-30 13:12:49 +0800375 DVR_DEBUG(1, "into seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
376
Pengfei Liuc181a982020-01-07 19:27:13 +0800377 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_ctx->index_fp);
380 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800381
hualing chen266b9502020-04-04 17:39:39 +0800382 if (time == 0) {
383 offset = 0;
hualing chend241c7a2021-06-22 13:34:27 +0800384 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
385 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen266b9502020-04-04 17:39:39 +0800386 return offset;
387 }
388
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800389 memset(buf, 0, sizeof(buf));
390 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chen2aba4022020-03-02 13:49:55 +0800391 int line = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800392 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800393 line++;
Pengfei Liuc181a982020-01-07 19:27:13 +0800394 memset(value, 0, sizeof(value));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800395 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800396 p1 += 5;
Pengfei Liuc181a982020-01-07 19:27:13 +0800397 if ((p2 = strstr(buf, ","))) {
398 memcpy(value, p1, p2 - p1);
399 }
400 pts = strtoull(value, NULL, 10);
401 }
402
403 memset(value, 0, sizeof(value));
404 if ((p1 = strstr(buf, "offset="))) {
405 p1 += 7;
406 if ((p2 = strstr(buf, "}"))) {
407 memcpy(value, p1, p2 - p1);
408 }
409 offset = strtoull(value, NULL, 10);
410 }
hualing chen2aba4022020-03-02 13:49:55 +0800411 if (0)
412 {
413 DVR_DEBUG(1, "seek buf[%s]", buf);
414 DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
415 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800416 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800417 if (time <= pts) {
hualing chen266b9502020-04-04 17:39:39 +0800418 if (block_size > 0) {
419 offset = offset - offset%block_size;
420 }
hualing chend241c7a2021-06-22 13:34:27 +0800421 //DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line);
422 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800423 return offset;
424 }
hualing chen2aba4022020-03-02 13:49:55 +0800425 }
hualing chen2932d372020-04-29 13:44:00 +0800426 if (time > pts) {
427 if (block_size > 0) {
428 offset = offset - offset%block_size;
429 }
430 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d end\n", pts, offset, time, line);
hualing chend241c7a2021-06-22 13:34:27 +0800431 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen2932d372020-04-29 13:44:00 +0800432 return offset;
433 }
hualing chen2aba4022020-03-02 13:49:55 +0800434 DVR_DEBUG(1, "seek error line [%d]", line);
Pengfei Liuc181a982020-01-07 19:27:13 +0800435 return DVR_FAILURE;
436}
437
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800438loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800439{
440 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800441 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800442 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800443 DVR_RETURN_IF_FALSE(p_ctx);
444 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800445 pos = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
446 return pos;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800447}
448
hualing chen5605eed2020-05-26 18:18:06 +0800449uint64_t segment_tell_position_time(Segment_Handle_t handle, loff_t position)
450{
451 Segment_Context_t *p_ctx;
452 char buf[256];
453 char value[256];
hualing chen969fe7b2021-05-26 15:13:17 +0800454 uint64_t ret = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800455 uint64_t pts = 0L;
hualing chen969fe7b2021-05-26 15:13:17 +0800456 uint64_t pts_p = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800457 loff_t offset = 0;
hualing chen969fe7b2021-05-26 15:13:17 +0800458 loff_t offset_p = 0;
hualing chen5605eed2020-05-26 18:18:06 +0800459 char *p1, *p2;
460
461 p_ctx = (Segment_Context_t *)handle;
462 DVR_RETURN_IF_FALSE(p_ctx);
463 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
464 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
465
466 memset(buf, 0, sizeof(buf));
467 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
468 DVR_RETURN_IF_FALSE(position != -1);
469
470 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
471 memset(value, 0, sizeof(value));
472 if ((p1 = strstr(buf, "time="))) {
473 p1 += 5;
474 if ((p2 = strstr(buf, ","))) {
475 memcpy(value, p1, p2 - p1);
476 }
477 pts = strtoull(value, NULL, 10);
478 }
479
480 memset(value, 0, sizeof(value));
481 if ((p1 = strstr(buf, "offset="))) {
482 p1 += 7;
483 if ((p2 = strstr(buf, "}"))) {
484 memcpy(value, p1, p2 - p1);
485 }
486 offset = strtoull(value, NULL, 10);
487 }
488
489 memset(buf, 0, sizeof(buf));
490 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chend241c7a2021-06-22 13:34:27 +0800491 if (position <= offset
492 &&position >= offset_p
493 && offset - offset_p > 0) {
hualing chen969fe7b2021-05-26 15:13:17 +0800494 ret = pts_p + (pts - pts_p) * (position - offset_p) / (offset - offset_p);
hualing chend241c7a2021-06-22 13:34:27 +0800495 //DVR_DEBUG(1, "tell cur time=%llu, pts_p = %llu, offset=%lld, position=%lld offset_p+%lld\n", pts, pts_p, offset, position, offset_p);
hualing chen969fe7b2021-05-26 15:13:17 +0800496 return ret;
hualing chen5605eed2020-05-26 18:18:06 +0800497 }
hualing chen969fe7b2021-05-26 15:13:17 +0800498 offset_p = offset;
499 pts_p = pts;
hualing chen5605eed2020-05-26 18:18:06 +0800500 }
501 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
502 return pts;
503}
504
505
pengfei.liu8b563292020-02-26 15:49:02 +0800506uint64_t segment_tell_current_time(Segment_Handle_t handle)
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800507{
508 Segment_Context_t *p_ctx;
509 char buf[256];
510 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800511 uint64_t pts = 0L;
512 loff_t offset = 0, position = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800513 char *p1, *p2;
514
515 p_ctx = (Segment_Context_t *)handle;
516 DVR_RETURN_IF_FALSE(p_ctx);
517 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
518 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
519
520 memset(buf, 0, sizeof(buf));
521 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800522 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800523 DVR_RETURN_IF_FALSE(position != -1);
524
525 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
526 memset(value, 0, sizeof(value));
527 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800528 p1 += 5;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800529 if ((p2 = strstr(buf, ","))) {
530 memcpy(value, p1, p2 - p1);
531 }
532 pts = strtoull(value, NULL, 10);
533 }
534
535 memset(value, 0, sizeof(value));
536 if ((p1 = strstr(buf, "offset="))) {
537 p1 += 7;
538 if ((p2 = strstr(buf, "}"))) {
539 memcpy(value, p1, p2 - p1);
540 }
541 offset = strtoull(value, NULL, 10);
542 }
543
544 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800545 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800546 if (position <= offset) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800547 return pts;
548 }
549 }
hualing chena540a7e2020-03-27 16:44:05 +0800550 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800551 return pts;
Pengfei Liuc181a982020-01-07 19:27:13 +0800552}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800553
pengfei.liu8b563292020-02-26 15:49:02 +0800554uint64_t segment_tell_total_time(Segment_Handle_t handle)
555{
556 Segment_Context_t *p_ctx;
557 char buf[256];
558 char last_buf[256];
559 char value[256];
560 uint64_t pts = ULLONG_MAX;
561 loff_t offset = 0, position = 0;
562 char *p1, *p2;
563 int line = 0;
564
565 p_ctx = (Segment_Context_t *)handle;
566 DVR_RETURN_IF_FALSE(p_ctx);
567 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
568 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
569
570 memset(buf, 0, sizeof(buf));
571 memset(last_buf, 0, sizeof(last_buf));
hualing chend241c7a2021-06-22 13:34:27 +0800572 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
pengfei.liu8b563292020-02-26 15:49:02 +0800573 DVR_RETURN_IF_FALSE(position != -1);
574
hualing chen041c4092020-04-05 15:11:50 +0800575 //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1);
576 //if seek error.we need seek 0 pos.
577 if (fseek(p_ctx->index_fp, -1000L, SEEK_END) == -1) {
578 fseek(p_ctx->index_fp, 0L, SEEK_SET);
579 }
pengfei.liu8b563292020-02-26 15:49:02 +0800580 /* Save last line buffer */
581 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800582 if (strlen(buf) <= 0) {
hualing chen87072a82020-03-12 16:20:12 +0800583 DVR_DEBUG(1, "read index buf is len 0");
hualing chen2aba4022020-03-02 13:49:55 +0800584 continue;
585 }
pengfei.liu8b563292020-02-26 15:49:02 +0800586 memset(last_buf, 0, sizeof(last_buf));
587 memcpy(last_buf, buf, strlen(buf));
588 memset(buf, 0, sizeof(buf));
589 line++;
590 }
591
592 /* Extract time value */
593 memset(value, 0, sizeof(value));
594 if ((p1 = strstr(last_buf, "time="))) {
595 p1 += 5;
596 if ((p2 = strstr(last_buf, ","))) {
597 memcpy(value, p1, p2 - p1);
598 }
599 pts = strtoull(value, NULL, 10);
600 }
601
602 memset(value, 0, sizeof(value));
603 if ((p1 = strstr(last_buf, "offset="))) {
604 p1 += 7;
605 if ((p2 = strstr(last_buf, "}"))) {
606 memcpy(value, p1, p2 - p1);
607 }
608 offset = strtoull(value, NULL, 10);
609 }
hualing chen87072a82020-03-12 16:20:12 +0800610 //if (line < 2)
611 //DVR_DEBUG(1, "totle time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line);
pengfei.liu8b563292020-02-26 15:49:02 +0800612 return (pts == ULLONG_MAX ? DVR_FAILURE : pts);
613}
614
pengfei.liuab5a2262020-02-14 17:33:40 +0800615/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800616int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
617{
618 Segment_Context_t *p_ctx;
619 char buf[256];
620 uint32_t i;
621
622 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800623 DVR_RETURN_IF_FALSE(p_ctx);
624 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
625 DVR_RETURN_IF_FALSE(p_info);
hualing chen87072a82020-03-12 16:20:12 +0800626 //seek 0, rewrite info
627 DVR_RETURN_IF_FALSE(fseek(p_ctx->dat_fp, 0, SEEK_SET) != -1);
Pengfei Liub4734232020-01-17 18:25:10 +0800628
629 /*Save segment id*/
630 memset(buf, 0, sizeof(buf));
631 sprintf(buf, "id=%lld\n", p_info->id);
632 fputs(buf, p_ctx->dat_fp);
633
634 /*Save number of pids*/
635 memset(buf, 0, sizeof(buf));
636 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
637 fputs(buf, p_ctx->dat_fp);
638
639 /*Save pid information*/
640 for (i = 0; i < p_info->nb_pids; i++) {
641 memset(buf, 0, sizeof(buf));
642 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
643 fputs(buf, p_ctx->dat_fp);
644 }
645
646 /*Save segment duration*/
647 memset(buf, 0, sizeof(buf));
hualing chen87072a82020-03-12 16:20:12 +0800648 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800649 sprintf(buf, "duration=%ld\n", p_info->duration);
650 fputs(buf, p_ctx->dat_fp);
651
652 /*Save segment size*/
653 memset(buf, 0, sizeof(buf));
654 sprintf(buf, "size=%zu\n", p_info->size);
655 fputs(buf, p_ctx->dat_fp);
656
657 /*Save number of packets*/
658 memset(buf, 0, sizeof(buf));
659 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
660 fputs(buf, p_ctx->dat_fp);
661
662 fflush(p_ctx->dat_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800663 fsync(fileno(p_ctx->dat_fp));
Pengfei Liub4734232020-01-17 18:25:10 +0800664 return DVR_SUCCESS;
665}
666
pengfei.liuab5a2262020-02-14 17:33:40 +0800667/* Should consider the case of cut power, todo... */
hualing chenb9a02922021-12-14 11:29:47 +0800668int segment_store_allInfo(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
669{
670 Segment_Context_t *p_ctx;
671 char buf[256];
672 uint32_t i;
673
674 p_ctx = (Segment_Context_t *)handle;
675 DVR_RETURN_IF_FALSE(p_ctx);
676 DVR_RETURN_IF_FALSE(p_ctx->all_dat_fp);
677 DVR_RETURN_IF_FALSE(p_info);
678 //seek 0, rewrite info
679 DVR_RETURN_IF_FALSE(fseek(p_ctx->all_dat_fp, 0, SEEK_END) != -1);
680
681 /*Save segment id*/
682 memset(buf, 0, sizeof(buf));
683 sprintf(buf, "id=%lld\n", p_info->id);
684 fputs(buf, p_ctx->all_dat_fp);
685
686 /*Save number of pids*/
687 memset(buf, 0, sizeof(buf));
688 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
689 fputs(buf, p_ctx->all_dat_fp);
690
691 /*Save pid information*/
692 for (i = 0; i < p_info->nb_pids; i++) {
693 memset(buf, 0, sizeof(buf));
694 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
695 fputs(buf, p_ctx->all_dat_fp);
696 }
697
698 /*Save segment duration*/
699 memset(buf, 0, sizeof(buf));
700 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
701 sprintf(buf, "duration=%ld\n", p_info->duration);
702 fputs(buf, p_ctx->all_dat_fp);
703
704 /*Save segment size*/
705 memset(buf, 0, sizeof(buf));
706 sprintf(buf, "size=%zu\n", p_info->size);
707 fputs(buf, p_ctx->all_dat_fp);
708
709 /*Save number of packets*/
710 memset(buf, 0, sizeof(buf));
711 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
712 fputs(buf, p_ctx->all_dat_fp);
713
714 fflush(p_ctx->all_dat_fp);
715 fsync(fileno(p_ctx->all_dat_fp));
716 return DVR_SUCCESS;
717}
718
719/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800720int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
721{
722 Segment_Context_t *p_ctx;
723 uint32_t i;
724 char buf[256];
725 char value[256];
726 char *p1, *p2;
727
728 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800729 DVR_RETURN_IF_FALSE(p_ctx);
730 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800731
732 /*Load segment id*/
733 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800734 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800735 p1 = strstr(buf, "id=");
hualing chen2aba4022020-03-02 13:49:55 +0800736 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800737 p_info->id = strtoull(p1 + 3, NULL, 10);
738
739 /*Save number of pids*/
740 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800741 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800742 p1 = strstr(buf, "nb_pids=");
hualing chen2aba4022020-03-02 13:49:55 +0800743 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800744 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
745
746 /*Save pid information*/
747 for (i = 0; i < p_info->nb_pids; i++) {
748 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800749 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800750 memset(value, 0, sizeof(value));
751 if ((p1 = strstr(buf, "pid="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800752 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800753 p1 += 4;
754 if ((p2 = strstr(buf, ","))) {
hualing chen2aba4022020-03-02 13:49:55 +0800755 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800756 memcpy(value, p1, p2 - p1);
757 }
758 p_info->pids[i].pid = strtoull(value, NULL, 10);
759 }
760
761 memset(value, 0, sizeof(value));
762 if ((p1 = strstr(buf, "type="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800763 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800764 p1 += 5;
765 if ((p2 = strstr(buf, "}"))) {
hualing chen2aba4022020-03-02 13:49:55 +0800766 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800767 memcpy(value, p1, p2 - p1);
768 }
769 p_info->pids[i].type = strtoull(value, NULL, 10);
770 }
771 }
772
773 /*Save segment duration*/
774 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800775 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800776 p1 = strstr(buf, "duration=");
hualing chen2aba4022020-03-02 13:49:55 +0800777 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800778 p_info->duration = strtoull(p1 + 9, NULL, 10);
hualing chena540a7e2020-03-27 16:44:05 +0800779 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800780
781 /*Save segment size*/
782 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800783 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800784 p1 = strstr(buf, "size=");
hualing chen2aba4022020-03-02 13:49:55 +0800785 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800786 p_info->size = strtoull(p1 + 5, NULL, 10);
787
788 /*Save number of packets*/
789 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800790 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800791 p1 = strstr(buf, "nb_packets=");
hualing chen2aba4022020-03-02 13:49:55 +0800792 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800793 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
794
795 return DVR_SUCCESS;
796}
797
hualing chenb9a02922021-12-14 11:29:47 +0800798/* Should consider the case of cut power, todo... */
799int segment_load_allInfo(Segment_Handle_t handle, struct list_head *list)
800{
801 Segment_Context_t *p_ctx;
802 uint32_t i;
803 char buf[256];
804 char value[256];
805 char *p1, *p2;
806
807 p_ctx = (Segment_Context_t *)handle;
808 DVR_RETURN_IF_FALSE(p_ctx);
809 DVR_RETURN_IF_FALSE(list);
hualing chen926a8ec2021-12-20 20:38:24 +0800810 if (p_ctx->all_dat_fp == NULL) {
811 DVR_DEBUG(1, "all dat file not open\n");
812 return DVR_FAILURE;
813 }
hualing chenb9a02922021-12-14 11:29:47 +0800814 //first get
815 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
816 DVR_RETURN_IF_FALSE(p1);
817
818 do {
819
820 DVR_RecordSegmentInfo_t *p_info;
821
822 p_info = malloc(sizeof(DVR_RecordSegmentInfo_t));
823 memset(p_info, 0, sizeof(DVR_RecordSegmentInfo_t));
824
825 list_add_tail(&p_info->head, list);
826
827 /*Load segment id*/
828 DVR_RETURN_IF_FALSE(p1);
829 p1 = strstr(buf, "id=");
830 DVR_RETURN_IF_FALSE(p1);
831 p_info->id = strtoull(p1 + 3, NULL, 10);
832
833 /*Save number of pids*/
834 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
835 DVR_RETURN_IF_FALSE(p1);
836 p1 = strstr(buf, "nb_pids=");
837 DVR_RETURN_IF_FALSE(p1);
838 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
839
840 /*Save pid information*/
841 for (i = 0; i < p_info->nb_pids; i++) {
842 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
843 DVR_RETURN_IF_FALSE(p1);
844 memset(value, 0, sizeof(value));
845 if ((p1 = strstr(buf, "pid="))) {
846 DVR_RETURN_IF_FALSE(p1);
847 p1 += 4;
848 if ((p2 = strstr(buf, ","))) {
849 DVR_RETURN_IF_FALSE(p2);
850 memcpy(value, p1, p2 - p1);
851 }
852 p_info->pids[i].pid = strtoull(value, NULL, 10);
853 }
854
855 memset(value, 0, sizeof(value));
856 if ((p1 = strstr(buf, "type="))) {
857 DVR_RETURN_IF_FALSE(p1);
858 p1 += 5;
859 if ((p2 = strstr(buf, "}"))) {
860 DVR_RETURN_IF_FALSE(p2);
861 memcpy(value, p1, p2 - p1);
862 }
863 p_info->pids[i].type = strtoull(value, NULL, 10);
864 }
865 }
866
867 /*Save segment duration*/
868 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
869 DVR_RETURN_IF_FALSE(p1);
870 p1 = strstr(buf, "duration=");
871 DVR_RETURN_IF_FALSE(p1);
872 p_info->duration = strtoull(p1 + 9, NULL, 10);
873 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
874
875 /*Save segment size*/
876 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
877 DVR_RETURN_IF_FALSE(p1);
878 p1 = strstr(buf, "size=");
879 DVR_RETURN_IF_FALSE(p1);
880 p_info->size = strtoull(p1 + 5, NULL, 10);
881
882 /*Save number of packets*/
883 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
884 DVR_RETURN_IF_FALSE(p1);
885 p1 = strstr(buf, "nb_packets=");
886 DVR_RETURN_IF_FALSE(p1);
887 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
888 //if reach end,exit loop
889 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
890 } while (p1);
891
892 return DVR_SUCCESS;
893}
894
Pengfei Liub4734232020-01-17 18:25:10 +0800895int segment_delete(const char *location, uint64_t segment_id)
896{
897 char fname[MAX_SEGMENT_PATH_SIZE];
898 int ret;
899
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800900 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800901
902 /*delete ts file*/
903 memset(fname, 0, sizeof(fname));
904 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
905 ret = unlink(fname);
906 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800907 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800908
909 /*delete index file*/
910 memset(fname, 0, sizeof(fname));
911 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
912 unlink(fname);
913 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800914 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800915
916 /*delete store information file*/
917 memset(fname, 0, sizeof(fname));
918 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
919 unlink(fname);
920 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800921 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800922
923 return DVR_SUCCESS;
924}
925
hualing chen87072a82020-03-12 16:20:12 +0800926int segment_ongoing(Segment_Handle_t handle)
927{
928 Segment_Context_t *p_ctx;
929 p_ctx = (Segment_Context_t *)handle;
930 struct stat mstat;
931
932 char going_name[MAX_SEGMENT_PATH_SIZE];
933 memset(going_name, 0, sizeof(going_name));
934 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
935 int ret = stat(going_name, &mstat);
936 DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret);
937 if (ret != 0) {
938 return DVR_FAILURE;
939 }
940 return DVR_SUCCESS;
941}
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800942loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800943{
944 Segment_Context_t *p_ctx;
945 char buf[256];
946 char value[256];
947 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800948 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800949 char *p1, *p2;
950
951 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800952 DVR_RETURN_IF_FALSE(p_ctx);
953 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
954 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800955
956 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800957 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800958 printf("start gets pts\n");
959 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
960 printf("buf[%s]\n", buf);
961 memset(value, 0, sizeof(value));
962 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800963 p1 += 5;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800964 if ((p2 = strstr(buf, ","))) {
965 memcpy(value, p1, p2 - p1);
966 }
967 pts = strtoull(value, NULL, 10);
968 }
969
970 memset(value, 0, sizeof(value));
971 if ((p1 = strstr(buf, "offset="))) {
972 p1 += 7;
973 if ((p2 = strstr(buf, "}"))) {
974 memcpy(value, p1, p2 - p1);
975 }
976 offset = strtoull(value, NULL, 10);
977 }
978
979 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800980 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800981 }
982
983 return 0;
984}