blob: ce5caaa0ab922f4080c8d17ba1b5855b5d20f2b6 [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
hualing chen157641d2022-02-22 17:54:52 +080020#define IDX_FILE_SYNC_TIME (10)//10*PCR_RECORD_INTERVAL_MS
21#define TS_FILE_SYNC_TIME (9)//9*PCR_RECORD_INTERVAL_MS
22
hualing chen2932d372020-04-29 13:44:00 +080023
Pengfei Liub4734232020-01-17 18:25:10 +080024/**\brief Segment context*/
Pengfei Liuc181a982020-01-07 19:27:13 +080025typedef struct {
Pengfei Liu3b1a8202020-02-12 23:04:21 +080026 int ts_fd; /**< Segment ts file fd*/
Pengfei Liub4734232020-01-17 18:25:10 +080027 FILE *index_fp; /**< Time index file fd*/
28 FILE *dat_fp; /**< Information file fd*/
hualing chenb9a02922021-12-14 11:29:47 +080029 FILE *all_dat_fp; /**< Information file fd*/
pengfei.liu567d6d82020-04-17 16:48:59 +080030 FILE *ongoing_fp; /**< Ongoing file fd, used to verify timedhift mode*/
Pengfei Liub4734232020-01-17 18:25:10 +080031 uint64_t first_pts; /**< First pts value, use for write mode*/
pengfei.liu567d6d82020-04-17 16:48:59 +080032 uint64_t last_pts; /**< Last input pts value, use for write mode*/
33 uint64_t last_record_pts; /**< Last record pts value, use for write mode*/
pengfei.liuab5a2262020-02-14 17:33:40 +080034 uint64_t cur_time; /**< Current time save in index file */
pengfei.liu567d6d82020-04-17 16:48:59 +080035 uint64_t segment_id; /**< Current segment ID */
36 char location[MAX_SEGMENT_PATH_SIZE]; /**< Current time save in index file */
hualing chena5f03222021-12-02 11:22:35 +080037 loff_t first_offset;
38 loff_t last_offset;
39 loff_t last_record_offset;
40 float avg_rate;
hualing chen157641d2022-02-22 17:54:52 +080041 int time;
42 } Segment_Context_t;
Pengfei Liuc181a982020-01-07 19:27:13 +080043
Pengfei Liub4734232020-01-17 18:25:10 +080044/**\brief Segment file type*/
Pengfei Liuc181a982020-01-07 19:27:13 +080045typedef enum {
Pengfei Liub4734232020-01-17 18:25:10 +080046 SEGMENT_FILE_TYPE_TS, /**< Used for store TS data*/
47 SEGMENT_FILE_TYPE_INDEX, /**< Used for store index data*/
48 SEGMENT_FILE_TYPE_DAT, /**< Used for store information data, such as duration etc*/
pengfei.liu567d6d82020-04-17 16:48:59 +080049 SEGMENT_FILE_TYPE_ONGOING, /**< Used for store information data, such as duration etc*/
hualing chenb9a02922021-12-14 11:29:47 +080050 SEGMENT_FILE_TYPE_ALLDAT, /**< Used for store all information data*/
Pengfei Liuc181a982020-01-07 19:27:13 +080051} Segment_FileType_t;
52
53static void segment_get_fname(char fname[MAX_SEGMENT_PATH_SIZE],
Pengfei Liub4734232020-01-17 18:25:10 +080054 const char location[DVR_MAX_LOCATION_SIZE],
Pengfei Liuc181a982020-01-07 19:27:13 +080055 uint64_t segment_id,
56 Segment_FileType_t type)
57{
58 int offset;
59
60 memset(fname, 0, MAX_SEGMENT_PATH_SIZE);
61 strncpy(fname, location, strlen(location));
62 offset = strlen(location);
hualing chenb9a02922021-12-14 11:29:47 +080063 if (type != SEGMENT_FILE_TYPE_ALLDAT) {
64 strncpy(fname + offset, "-", 1);
65 offset += 1;
66 sprintf(fname + offset, "%04llu", segment_id);
67 offset += 4;
68 }
69
Pengfei Liuc181a982020-01-07 19:27:13 +080070 if (type == SEGMENT_FILE_TYPE_TS)
71 strncpy(fname + offset, ".ts", 3);
72 else if (type == SEGMENT_FILE_TYPE_INDEX)
73 strncpy(fname + offset, ".idx", 4);
Pengfei Liub4734232020-01-17 18:25:10 +080074 else if (type == SEGMENT_FILE_TYPE_DAT)
75 strncpy(fname + offset, ".dat", 4);
hualing chen87072a82020-03-12 16:20:12 +080076 else if (type == SEGMENT_FILE_TYPE_ONGOING)
77 strncpy(fname + offset, ".going", 6);
hualing chenb9a02922021-12-14 11:29:47 +080078 else if (type == SEGMENT_FILE_TYPE_ALLDAT)
79 strncpy(fname + offset, ".dat", 4);
80
Pengfei Liuc181a982020-01-07 19:27:13 +080081}
82
Pengfei Liu3b1a8202020-02-12 23:04:21 +080083static void segment_get_dirname(char dir_name[MAX_SEGMENT_PATH_SIZE],
84 const char location[DVR_MAX_LOCATION_SIZE])
85{
86 char *p;
87 int i;
88 int found = 0;
89
90 for (i = 0; i < (int)strlen(location); i++) {
91 if (location[i] == '/') {
92 p = (char *)location + i;
93 found = 1;
94 }
95 }
96 if (found)
97 memcpy(dir_name, location, p - location);
98}
99
Pengfei Liuc181a982020-01-07 19:27:13 +0800100int segment_open(Segment_OpenParams_t *params, Segment_Handle_t *p_handle)
101{
102 Segment_Context_t *p_ctx;
103 char ts_fname[MAX_SEGMENT_PATH_SIZE];
104 char index_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liub4734232020-01-17 18:25:10 +0800105 char dat_fname[MAX_SEGMENT_PATH_SIZE];
hualing chenb9a02922021-12-14 11:29:47 +0800106 char all_dat_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800107 char dir_name[MAX_SEGMENT_PATH_SIZE];
hualing chen87072a82020-03-12 16:20:12 +0800108 char going_name[MAX_SEGMENT_PATH_SIZE];
Pengfei Liuc181a982020-01-07 19:27:13 +0800109
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800110 DVR_RETURN_IF_FALSE(params);
111 DVR_RETURN_IF_FALSE(p_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +0800112
hualing chena540a7e2020-03-27 16:44:05 +0800113 //DVR_DEBUG(1, "%s, location:%s, id:%llu", __func__, params->location, params->segment_id);
Pengfei Liuc181a982020-01-07 19:27:13 +0800114
Pengfei Liub038b6a2020-01-14 15:57:01 +0800115 p_ctx = (void*)malloc(sizeof(Segment_Context_t));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800116 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +0800117 memset(p_ctx, 0, sizeof(Segment_Context_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800118
119 memset(ts_fname, 0, sizeof(ts_fname));
120 segment_get_fname(ts_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_TS);
121
122 memset(index_fname, 0, sizeof(index_fname));
123 segment_get_fname(index_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_INDEX);
124
Pengfei Liub4734232020-01-17 18:25:10 +0800125 memset(dat_fname, 0, sizeof(dat_fname));
126 segment_get_fname(dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_DAT);
127
hualing chenb9a02922021-12-14 11:29:47 +0800128 memset(all_dat_fname, 0, sizeof(all_dat_fname));
129 segment_get_fname(all_dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_ALLDAT);
130
131
hualing chen87072a82020-03-12 16:20:12 +0800132 memset(going_name, 0, sizeof(going_name));
133 segment_get_fname(going_name, params->location, params->segment_id, SEGMENT_FILE_TYPE_ONGOING);
134
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800135 memset(dir_name, 0, sizeof(dir_name));
136 segment_get_dirname(dir_name, params->location);
137 if (access(dir_name, F_OK) == -1) {
138 DVR_DEBUG(1, "%s dir %s is not exist, create it", __func__, dir_name);
139 mkdir(dir_name, 0666);
140 }
141
Pengfei Liuc181a982020-01-07 19:27:13 +0800142 if (params->mode == SEGMENT_MODE_READ) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800143 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800144 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800145 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800146 p_ctx->ongoing_fp = NULL;
hualing chenb9a02922021-12-14 11:29:47 +0800147 p_ctx->all_dat_fp = fopen(all_dat_fname, "r");
Pengfei Liuc181a982020-01-07 19:27:13 +0800148 } else if (params->mode == SEGMENT_MODE_WRITE) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800149 p_ctx->ts_fd = open(ts_fname, O_CREAT | O_RDWR | O_TRUNC, 0644);
Pengfei Liuc181a982020-01-07 19:27:13 +0800150 p_ctx->index_fp = fopen(index_fname, "w+");
Pengfei Liub4734232020-01-17 18:25:10 +0800151 p_ctx->dat_fp = fopen(dat_fname, "w+");
hualing chenb9a02922021-12-14 11:29:47 +0800152 p_ctx->all_dat_fp = fopen(all_dat_fname, "a+");
hualing chen8aed9582021-12-24 17:59:56 +0800153 DVR_DEBUG(1, "%s dir %s is opened", __func__, all_dat_fname);
hualing chen87072a82020-03-12 16:20:12 +0800154 p_ctx->ongoing_fp = fopen(going_name, "w+");
Pengfei Liub038b6a2020-01-14 15:57:01 +0800155 p_ctx->first_pts = ULLONG_MAX;
156 p_ctx->last_pts = ULLONG_MAX;
pengfei.liu567d6d82020-04-17 16:48:59 +0800157 p_ctx->last_record_pts = ULLONG_MAX;
hualing chena5f03222021-12-02 11:22:35 +0800158 p_ctx->avg_rate = 0.0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800159 } else {
160 DVR_DEBUG(1, "%s, unknow mode use default", __func__);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800161 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800162 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800163 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chenb9a02922021-12-14 11:29:47 +0800164 p_ctx->all_dat_fp = fopen(all_dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800165 p_ctx->ongoing_fp = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800166 }
167
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800168 if (p_ctx->ts_fd == -1 || !p_ctx->index_fp || !p_ctx->dat_fp) {
Pengfei Liub4734232020-01-17 18:25:10 +0800169 DVR_DEBUG(1, "%s open file failed [%s, %s, %s], reason:%s", __func__,
170 ts_fname, index_fname, dat_fname, strerror(errno));
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800171 if (p_ctx->ts_fd != -1)
172 close(p_ctx->ts_fd);
173 if (p_ctx->index_fp)
174 fclose(p_ctx->index_fp);
175 if (p_ctx->dat_fp)
176 fclose(p_ctx->dat_fp);
hualing chen926a8ec2021-12-20 20:38:24 +0800177 if (p_ctx->all_dat_fp)
178 fclose(p_ctx->all_dat_fp);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800179 if (p_ctx->ongoing_fp)
180 fclose(p_ctx->ongoing_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800181 free(p_ctx);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800182 *p_handle = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800183 return DVR_FAILURE;
184 }
hualing chen87072a82020-03-12 16:20:12 +0800185 p_ctx->segment_id = params->segment_id;
186 strncpy(p_ctx->location, params->location, strlen(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800187
hualing chen7a56cba2020-04-14 14:09:27 +0800188 //DVR_DEBUG(1, "%s, open file success p_ctx->location [%s]", __func__, p_ctx->location, params->mode);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800189 *p_handle = (Segment_Handle_t)p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800190 return DVR_SUCCESS;
191}
192
193int segment_close(Segment_Handle_t handle)
194{
195 Segment_Context_t *p_ctx;
196
Pengfei Liub038b6a2020-01-14 15:57:01 +0800197 p_ctx = (void *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800198 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800199
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800200 if (p_ctx->ts_fd != -1) {
201 close(p_ctx->ts_fd);
Pengfei Liuc181a982020-01-07 19:27:13 +0800202 }
203
204 if (p_ctx->index_fp) {
205 fclose(p_ctx->index_fp);
206 }
207
Pengfei Liub4734232020-01-17 18:25:10 +0800208 if (p_ctx->dat_fp) {
209 fclose(p_ctx->dat_fp);
210 }
hualing chen926a8ec2021-12-20 20:38:24 +0800211 if (p_ctx->all_dat_fp) {
212 fclose(p_ctx->all_dat_fp);
213 }
hualing chen87072a82020-03-12 16:20:12 +0800214 if (p_ctx->ongoing_fp != NULL) {
215 fclose(p_ctx->ongoing_fp);
216 char going_name[MAX_SEGMENT_PATH_SIZE];
217 memset(going_name, 0, sizeof(going_name));
218 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
219 DVR_DEBUG(1, "segment close del [%s]", going_name);
220 unlink(going_name);
221 }
222
Pengfei Liuc181a982020-01-07 19:27:13 +0800223 free(p_ctx);
224 return 0;
225}
226
227ssize_t segment_read(Segment_Handle_t handle, void *buf, size_t count)
228{
229 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800230 ssize_t len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800231 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800232 DVR_RETURN_IF_FALSE(p_ctx);
233 DVR_RETURN_IF_FALSE(buf);
234 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800235 len = read(p_ctx->ts_fd, buf, count);
236 return len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800237}
238
239ssize_t segment_write(Segment_Handle_t handle, void *buf, size_t count)
240{
241 Segment_Context_t *p_ctx;
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800242 ssize_t len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800243 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800244 DVR_RETURN_IF_FALSE(p_ctx);
245 DVR_RETURN_IF_FALSE(buf);
246 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800247 len = write(p_ctx->ts_fd, buf, count);
hualing chen157641d2022-02-22 17:54:52 +0800248 if (p_ctx->time % TS_FILE_SYNC_TIME == 0)
249 fsync(p_ctx->ts_fd);
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800250 return len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800251}
252
hualing chen2932d372020-04-29 13:44:00 +0800253int segment_update_pts_force(Segment_Handle_t handle, uint64_t pts, loff_t offset)
Pengfei Liuc181a982020-01-07 19:27:13 +0800254{
255 Segment_Context_t *p_ctx;
256 char buf[256];
pengfei.liu567d6d82020-04-17 16:48:59 +0800257 int record_diff = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800258
259 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800260 DVR_RETURN_IF_FALSE(p_ctx);
261 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800262
Pengfei Liub038b6a2020-01-14 15:57:01 +0800263 if (p_ctx->first_pts == ULLONG_MAX) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800264 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800265 p_ctx->first_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800266 p_ctx->first_offset = offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800267 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800268 memset(buf, 0, sizeof(buf));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800269 if (p_ctx->last_pts == ULLONG_MAX) {
270 /*Last pts is init value*/
hualing chen2aba4022020-03-02 13:49:55 +0800271 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
pengfei.liuab5a2262020-02-14 17:33:40 +0800272 p_ctx->cur_time = pts - p_ctx->first_pts;
hualing chena5f03222021-12-02 11:22:35 +0800273 DVR_DEBUG(1, "%s force pcr:%llu -1", __func__, pts);
hualing chen2932d372020-04-29 13:44:00 +0800274 } else {
275 /*Last pts has valid value*/
276 int diff = pts - p_ctx->last_pts;
277 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
278 /*Current pts has a transition*/
279 DVR_DEBUG(1, "[%s]force update Current pts has a transition, [%llu, %llu, %llu]",__func__,
280 p_ctx->first_pts, p_ctx->last_pts, pts);
281 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
282 } else {
283 /*This is a normal pts, record it*/
hualing chena5f03222021-12-02 11:22:35 +0800284 //check if this pcr is transition.if true,add 200ms
285 //other case normal.
hualing chen2932d372020-04-29 13:44:00 +0800286 p_ctx->cur_time += diff;
287 DVR_DEBUG(1, "%s force pcr:%llu -1 diff [%d]", __func__, pts, diff);
288 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
289 }
290 }
291
292 record_diff = pts - p_ctx->last_record_pts;
293 if (strlen(buf) > 0) {
294 DVR_DEBUG(1, "%s force pcr:%llu buf:%s", __func__, pts, buf);
295 fputs(buf, p_ctx->index_fp);
296 fflush(p_ctx->index_fp);
297 fsync(fileno(p_ctx->index_fp));
298 p_ctx->last_record_pts = pts;
299 }
300 p_ctx->last_pts = pts;
hualing chen2932d372020-04-29 13:44:00 +0800301 return DVR_SUCCESS;
302}
303
304int segment_update_pts(Segment_Handle_t handle, uint64_t pts, loff_t offset)
305{
306 Segment_Context_t *p_ctx;
307 char buf[256];
308 int record_diff = 0;
309
310 p_ctx = (Segment_Context_t *)handle;
311 DVR_RETURN_IF_FALSE(p_ctx);
312 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
313
314 if (p_ctx->first_pts == ULLONG_MAX) {
315 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
316 p_ctx->first_pts = pts;
317 //p_ctx->cur_time = p_ctx->cur_time + PTS_HEAD_DEVIATION;
318 }
319 memset(buf, 0, sizeof(buf));
320 if (p_ctx->last_pts == ULLONG_MAX) {
321 /*Last pts is init value*/
322 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
323 p_ctx->cur_time = pts - p_ctx->first_pts;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800324 } else {
325 /*Last pts has valid value*/
pengfei.liu567d6d82020-04-17 16:48:59 +0800326 int diff = pts - p_ctx->last_pts;
hualing chen4b7c15d2020-04-07 16:13:48 +0800327 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
Pengfei Liub038b6a2020-01-14 15:57:01 +0800328 /*Current pts has a transition*/
pengfei.liuab5a2262020-02-14 17:33:40 +0800329 DVR_DEBUG(1, "Current pts has a transition, [%llu, %llu, %llu]",
330 p_ctx->first_pts, p_ctx->last_pts, pts);
pengfei.liu567d6d82020-04-17 16:48:59 +0800331 p_ctx->last_record_pts = pts;
hualing chen2932d372020-04-29 13:44:00 +0800332 //p_ctx->cur_time = p_ctx->cur_time + PTS_DISCONTINE_DEVIATION;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800333 } else {
334 /*This is a normal pts, record it*/
hualing chena5f03222021-12-02 11:22:35 +0800335 loff_t off_diff = offset - p_ctx->last_offset;
336 float rate = (float) (off_diff) / (float)(diff);
337 if (p_ctx->avg_rate == 0.0) {
338 p_ctx->avg_rate = (float) offset / (float)(p_ctx->cur_time + diff);
339 }
hualing chen86b8e602021-12-23 11:04:19 +0800340 if (diff >= PCR_JUMP_DUR) {
hualing chena5f03222021-12-02 11:22:35 +0800341 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));
342 if (p_ctx->avg_rate != 0 && (int)(p_ctx->avg_rate) >= (int)(rate * 4)) {
343 diff = off_diff / p_ctx->avg_rate;
344 p_ctx->cur_time += diff;
345 } else {
346 p_ctx->cur_time += diff;
347 }
348 } else {
349 p_ctx->cur_time += diff;
350 }
hualing chen2aba4022020-03-02 13:49:55 +0800351 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800352 }
353 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800354
355 record_diff = pts - p_ctx->last_record_pts;
356 if (strlen(buf) > 0 &&
357 (record_diff > PCR_RECORD_INTERVAL_MS || p_ctx->last_record_pts == ULLONG_MAX)){
hualing chen4b7c15d2020-04-07 16:13:48 +0800358 fputs(buf, p_ctx->index_fp);
pengfei.liu567d6d82020-04-17 16:48:59 +0800359 fflush(p_ctx->index_fp);
hualing chen157641d2022-02-22 17:54:52 +0800360 p_ctx->time++;
361 //flush idx file 3s
362 if ((p_ctx->time > 0 && p_ctx->time % IDX_FILE_SYNC_TIME == 0))
363 fsync(fileno(p_ctx->index_fp));
364 if (p_ctx->time > IDX_FILE_SYNC_TIME)
365 p_ctx->time = 0;
pengfei.liu567d6d82020-04-17 16:48:59 +0800366 p_ctx->last_record_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800367 p_ctx->last_record_offset = offset;
368 if (p_ctx->cur_time > 0)
369 p_ctx->avg_rate = (float) offset / (float)p_ctx->cur_time;
pengfei.liu567d6d82020-04-17 16:48:59 +0800370 }
Pengfei Liub038b6a2020-01-14 15:57:01 +0800371 p_ctx->last_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800372 p_ctx->last_offset = offset;
Pengfei Liuc181a982020-01-07 19:27:13 +0800373 return DVR_SUCCESS;
374}
375
hualing chen266b9502020-04-04 17:39:39 +0800376loff_t segment_seek(Segment_Handle_t handle, uint64_t time, int block_size)
Pengfei Liuc181a982020-01-07 19:27:13 +0800377{
378 Segment_Context_t *p_ctx;
379 char buf[256];
380 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800381 uint64_t pts = 0L;
382 loff_t offset = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800383 char *p1, *p2;
384
hualing chen8a657f32021-08-30 13:12:49 +0800385 DVR_DEBUG(1, "into seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
386
Pengfei Liuc181a982020-01-07 19:27:13 +0800387 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->index_fp);
390 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800391
hualing chen266b9502020-04-04 17:39:39 +0800392 if (time == 0) {
393 offset = 0;
hualing chend241c7a2021-06-22 13:34:27 +0800394 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
395 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen266b9502020-04-04 17:39:39 +0800396 return offset;
397 }
398
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800399 memset(buf, 0, sizeof(buf));
400 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chen2aba4022020-03-02 13:49:55 +0800401 int line = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800402 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800403 line++;
Pengfei Liuc181a982020-01-07 19:27:13 +0800404 memset(value, 0, sizeof(value));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800405 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800406 p1 += 5;
Pengfei Liuc181a982020-01-07 19:27:13 +0800407 if ((p2 = strstr(buf, ","))) {
408 memcpy(value, p1, p2 - p1);
409 }
410 pts = strtoull(value, NULL, 10);
411 }
412
413 memset(value, 0, sizeof(value));
414 if ((p1 = strstr(buf, "offset="))) {
415 p1 += 7;
416 if ((p2 = strstr(buf, "}"))) {
417 memcpy(value, p1, p2 - p1);
418 }
419 offset = strtoull(value, NULL, 10);
420 }
hualing chen2aba4022020-03-02 13:49:55 +0800421 if (0)
422 {
423 DVR_DEBUG(1, "seek buf[%s]", buf);
424 DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
425 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800426 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800427 if (time <= pts) {
hualing chen266b9502020-04-04 17:39:39 +0800428 if (block_size > 0) {
429 offset = offset - offset%block_size;
430 }
hualing chend241c7a2021-06-22 13:34:27 +0800431 //DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line);
432 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800433 return offset;
434 }
hualing chen2aba4022020-03-02 13:49:55 +0800435 }
hualing chen2932d372020-04-29 13:44:00 +0800436 if (time > pts) {
437 if (block_size > 0) {
438 offset = offset - offset%block_size;
439 }
440 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 +0800441 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen2932d372020-04-29 13:44:00 +0800442 return offset;
443 }
hualing chen2aba4022020-03-02 13:49:55 +0800444 DVR_DEBUG(1, "seek error line [%d]", line);
Pengfei Liuc181a982020-01-07 19:27:13 +0800445 return DVR_FAILURE;
446}
447
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800448loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800449{
450 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800451 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800452 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800453 DVR_RETURN_IF_FALSE(p_ctx);
454 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800455 pos = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
456 return pos;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800457}
458
hualing chen5605eed2020-05-26 18:18:06 +0800459uint64_t segment_tell_position_time(Segment_Handle_t handle, loff_t position)
460{
461 Segment_Context_t *p_ctx;
462 char buf[256];
463 char value[256];
hualing chen969fe7b2021-05-26 15:13:17 +0800464 uint64_t ret = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800465 uint64_t pts = 0L;
hualing chen969fe7b2021-05-26 15:13:17 +0800466 uint64_t pts_p = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800467 loff_t offset = 0;
hualing chen969fe7b2021-05-26 15:13:17 +0800468 loff_t offset_p = 0;
hualing chen5605eed2020-05-26 18:18:06 +0800469 char *p1, *p2;
470
471 p_ctx = (Segment_Context_t *)handle;
472 DVR_RETURN_IF_FALSE(p_ctx);
473 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
474 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
475
476 memset(buf, 0, sizeof(buf));
477 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
478 DVR_RETURN_IF_FALSE(position != -1);
479
480 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
481 memset(value, 0, sizeof(value));
482 if ((p1 = strstr(buf, "time="))) {
483 p1 += 5;
484 if ((p2 = strstr(buf, ","))) {
485 memcpy(value, p1, p2 - p1);
486 }
487 pts = strtoull(value, NULL, 10);
488 }
489
490 memset(value, 0, sizeof(value));
491 if ((p1 = strstr(buf, "offset="))) {
492 p1 += 7;
493 if ((p2 = strstr(buf, "}"))) {
494 memcpy(value, p1, p2 - p1);
495 }
496 offset = strtoull(value, NULL, 10);
497 }
498
499 memset(buf, 0, sizeof(buf));
500 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chend241c7a2021-06-22 13:34:27 +0800501 if (position <= offset
502 &&position >= offset_p
503 && offset - offset_p > 0) {
hualing chen969fe7b2021-05-26 15:13:17 +0800504 ret = pts_p + (pts - pts_p) * (position - offset_p) / (offset - offset_p);
hualing chend241c7a2021-06-22 13:34:27 +0800505 //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 +0800506 return ret;
hualing chen5605eed2020-05-26 18:18:06 +0800507 }
hualing chen969fe7b2021-05-26 15:13:17 +0800508 offset_p = offset;
509 pts_p = pts;
hualing chen5605eed2020-05-26 18:18:06 +0800510 }
511 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
512 return pts;
513}
514
515
pengfei.liu8b563292020-02-26 15:49:02 +0800516uint64_t segment_tell_current_time(Segment_Handle_t handle)
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800517{
518 Segment_Context_t *p_ctx;
519 char buf[256];
520 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800521 uint64_t pts = 0L;
522 loff_t offset = 0, position = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800523 char *p1, *p2;
524
525 p_ctx = (Segment_Context_t *)handle;
526 DVR_RETURN_IF_FALSE(p_ctx);
527 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
528 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
529
530 memset(buf, 0, sizeof(buf));
531 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800532 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800533 DVR_RETURN_IF_FALSE(position != -1);
534
535 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
536 memset(value, 0, sizeof(value));
537 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800538 p1 += 5;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800539 if ((p2 = strstr(buf, ","))) {
540 memcpy(value, p1, p2 - p1);
541 }
542 pts = strtoull(value, NULL, 10);
543 }
544
545 memset(value, 0, sizeof(value));
546 if ((p1 = strstr(buf, "offset="))) {
547 p1 += 7;
548 if ((p2 = strstr(buf, "}"))) {
549 memcpy(value, p1, p2 - p1);
550 }
551 offset = strtoull(value, NULL, 10);
552 }
553
554 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800555 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800556 if (position <= offset) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800557 return pts;
558 }
559 }
hualing chena540a7e2020-03-27 16:44:05 +0800560 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800561 return pts;
Pengfei Liuc181a982020-01-07 19:27:13 +0800562}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800563
pengfei.liu8b563292020-02-26 15:49:02 +0800564uint64_t segment_tell_total_time(Segment_Handle_t handle)
565{
566 Segment_Context_t *p_ctx;
567 char buf[256];
568 char last_buf[256];
569 char value[256];
570 uint64_t pts = ULLONG_MAX;
571 loff_t offset = 0, position = 0;
572 char *p1, *p2;
573 int line = 0;
574
575 p_ctx = (Segment_Context_t *)handle;
576 DVR_RETURN_IF_FALSE(p_ctx);
577 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
578 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
579
580 memset(buf, 0, sizeof(buf));
581 memset(last_buf, 0, sizeof(last_buf));
hualing chend241c7a2021-06-22 13:34:27 +0800582 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
pengfei.liu8b563292020-02-26 15:49:02 +0800583 DVR_RETURN_IF_FALSE(position != -1);
584
hualing chen041c4092020-04-05 15:11:50 +0800585 //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1);
586 //if seek error.we need seek 0 pos.
587 if (fseek(p_ctx->index_fp, -1000L, SEEK_END) == -1) {
588 fseek(p_ctx->index_fp, 0L, SEEK_SET);
589 }
pengfei.liu8b563292020-02-26 15:49:02 +0800590 /* Save last line buffer */
591 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800592 if (strlen(buf) <= 0) {
hualing chen87072a82020-03-12 16:20:12 +0800593 DVR_DEBUG(1, "read index buf is len 0");
hualing chen2aba4022020-03-02 13:49:55 +0800594 continue;
595 }
pengfei.liu8b563292020-02-26 15:49:02 +0800596 memset(last_buf, 0, sizeof(last_buf));
597 memcpy(last_buf, buf, strlen(buf));
598 memset(buf, 0, sizeof(buf));
599 line++;
600 }
601
602 /* Extract time value */
603 memset(value, 0, sizeof(value));
604 if ((p1 = strstr(last_buf, "time="))) {
605 p1 += 5;
606 if ((p2 = strstr(last_buf, ","))) {
607 memcpy(value, p1, p2 - p1);
608 }
609 pts = strtoull(value, NULL, 10);
610 }
611
612 memset(value, 0, sizeof(value));
613 if ((p1 = strstr(last_buf, "offset="))) {
614 p1 += 7;
615 if ((p2 = strstr(last_buf, "}"))) {
616 memcpy(value, p1, p2 - p1);
617 }
618 offset = strtoull(value, NULL, 10);
619 }
hualing chen87072a82020-03-12 16:20:12 +0800620 //if (line < 2)
621 //DVR_DEBUG(1, "totle time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line);
pengfei.liu8b563292020-02-26 15:49:02 +0800622 return (pts == ULLONG_MAX ? DVR_FAILURE : pts);
623}
624
pengfei.liuab5a2262020-02-14 17:33:40 +0800625/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800626int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
627{
628 Segment_Context_t *p_ctx;
629 char buf[256];
630 uint32_t i;
631
632 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800633 DVR_RETURN_IF_FALSE(p_ctx);
634 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
635 DVR_RETURN_IF_FALSE(p_info);
hualing chen87072a82020-03-12 16:20:12 +0800636 //seek 0, rewrite info
637 DVR_RETURN_IF_FALSE(fseek(p_ctx->dat_fp, 0, SEEK_SET) != -1);
Pengfei Liub4734232020-01-17 18:25:10 +0800638
639 /*Save segment id*/
640 memset(buf, 0, sizeof(buf));
641 sprintf(buf, "id=%lld\n", p_info->id);
642 fputs(buf, p_ctx->dat_fp);
643
644 /*Save number of pids*/
645 memset(buf, 0, sizeof(buf));
646 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
647 fputs(buf, p_ctx->dat_fp);
648
649 /*Save pid information*/
650 for (i = 0; i < p_info->nb_pids; i++) {
651 memset(buf, 0, sizeof(buf));
652 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
653 fputs(buf, p_ctx->dat_fp);
654 }
655
656 /*Save segment duration*/
657 memset(buf, 0, sizeof(buf));
hualing chen87072a82020-03-12 16:20:12 +0800658 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800659 sprintf(buf, "duration=%ld\n", p_info->duration);
660 fputs(buf, p_ctx->dat_fp);
661
662 /*Save segment size*/
663 memset(buf, 0, sizeof(buf));
664 sprintf(buf, "size=%zu\n", p_info->size);
665 fputs(buf, p_ctx->dat_fp);
666
667 /*Save number of packets*/
668 memset(buf, 0, sizeof(buf));
669 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
670 fputs(buf, p_ctx->dat_fp);
671
672 fflush(p_ctx->dat_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800673 fsync(fileno(p_ctx->dat_fp));
Pengfei Liub4734232020-01-17 18:25:10 +0800674 return DVR_SUCCESS;
675}
676
pengfei.liuab5a2262020-02-14 17:33:40 +0800677/* Should consider the case of cut power, todo... */
hualing chenb9a02922021-12-14 11:29:47 +0800678int segment_store_allInfo(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
679{
680 Segment_Context_t *p_ctx;
681 char buf[256];
682 uint32_t i;
683
684 p_ctx = (Segment_Context_t *)handle;
685 DVR_RETURN_IF_FALSE(p_ctx);
686 DVR_RETURN_IF_FALSE(p_ctx->all_dat_fp);
687 DVR_RETURN_IF_FALSE(p_info);
688 //seek 0, rewrite info
689 DVR_RETURN_IF_FALSE(fseek(p_ctx->all_dat_fp, 0, SEEK_END) != -1);
690
691 /*Save segment id*/
692 memset(buf, 0, sizeof(buf));
693 sprintf(buf, "id=%lld\n", p_info->id);
694 fputs(buf, p_ctx->all_dat_fp);
695
696 /*Save number of pids*/
697 memset(buf, 0, sizeof(buf));
698 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
699 fputs(buf, p_ctx->all_dat_fp);
700
701 /*Save pid information*/
702 for (i = 0; i < p_info->nb_pids; i++) {
703 memset(buf, 0, sizeof(buf));
704 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
705 fputs(buf, p_ctx->all_dat_fp);
706 }
707
708 /*Save segment duration*/
709 memset(buf, 0, sizeof(buf));
710 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
711 sprintf(buf, "duration=%ld\n", p_info->duration);
712 fputs(buf, p_ctx->all_dat_fp);
713
714 /*Save segment size*/
715 memset(buf, 0, sizeof(buf));
716 sprintf(buf, "size=%zu\n", p_info->size);
717 fputs(buf, p_ctx->all_dat_fp);
718
719 /*Save number of packets*/
720 memset(buf, 0, sizeof(buf));
721 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
722 fputs(buf, p_ctx->all_dat_fp);
723
724 fflush(p_ctx->all_dat_fp);
725 fsync(fileno(p_ctx->all_dat_fp));
726 return DVR_SUCCESS;
727}
728
729/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800730int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
731{
732 Segment_Context_t *p_ctx;
733 uint32_t i;
734 char buf[256];
735 char value[256];
736 char *p1, *p2;
737
738 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800739 DVR_RETURN_IF_FALSE(p_ctx);
740 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800741
742 /*Load segment id*/
743 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800744 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800745 p1 = strstr(buf, "id=");
hualing chen2aba4022020-03-02 13:49:55 +0800746 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800747 p_info->id = strtoull(p1 + 3, NULL, 10);
748
749 /*Save number of pids*/
750 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800751 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800752 p1 = strstr(buf, "nb_pids=");
hualing chen2aba4022020-03-02 13:49:55 +0800753 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800754 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
755
756 /*Save pid information*/
757 for (i = 0; i < p_info->nb_pids; i++) {
758 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800759 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800760 memset(value, 0, sizeof(value));
761 if ((p1 = strstr(buf, "pid="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800762 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800763 p1 += 4;
764 if ((p2 = strstr(buf, ","))) {
hualing chen2aba4022020-03-02 13:49:55 +0800765 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800766 memcpy(value, p1, p2 - p1);
767 }
768 p_info->pids[i].pid = strtoull(value, NULL, 10);
769 }
770
771 memset(value, 0, sizeof(value));
772 if ((p1 = strstr(buf, "type="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800773 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800774 p1 += 5;
775 if ((p2 = strstr(buf, "}"))) {
hualing chen2aba4022020-03-02 13:49:55 +0800776 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800777 memcpy(value, p1, p2 - p1);
778 }
779 p_info->pids[i].type = strtoull(value, NULL, 10);
780 }
781 }
782
783 /*Save segment duration*/
784 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800785 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800786 p1 = strstr(buf, "duration=");
hualing chen2aba4022020-03-02 13:49:55 +0800787 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800788 p_info->duration = strtoull(p1 + 9, NULL, 10);
hualing chena540a7e2020-03-27 16:44:05 +0800789 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800790
791 /*Save segment size*/
792 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800793 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800794 p1 = strstr(buf, "size=");
hualing chen2aba4022020-03-02 13:49:55 +0800795 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800796 p_info->size = strtoull(p1 + 5, NULL, 10);
797
798 /*Save number of packets*/
799 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800800 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800801 p1 = strstr(buf, "nb_packets=");
hualing chen2aba4022020-03-02 13:49:55 +0800802 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800803 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
804
805 return DVR_SUCCESS;
806}
807
hualing chenb9a02922021-12-14 11:29:47 +0800808/* Should consider the case of cut power, todo... */
809int segment_load_allInfo(Segment_Handle_t handle, struct list_head *list)
810{
811 Segment_Context_t *p_ctx;
812 uint32_t i;
813 char buf[256];
814 char value[256];
815 char *p1, *p2;
816
817 p_ctx = (Segment_Context_t *)handle;
818 DVR_RETURN_IF_FALSE(p_ctx);
819 DVR_RETURN_IF_FALSE(list);
hualing chen926a8ec2021-12-20 20:38:24 +0800820 if (p_ctx->all_dat_fp == NULL) {
821 DVR_DEBUG(1, "all dat file not open\n");
822 return DVR_FAILURE;
823 }
hualing chenb9a02922021-12-14 11:29:47 +0800824 //first get
825 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
826 DVR_RETURN_IF_FALSE(p1);
827
828 do {
829
830 DVR_RecordSegmentInfo_t *p_info;
831
832 p_info = malloc(sizeof(DVR_RecordSegmentInfo_t));
833 memset(p_info, 0, sizeof(DVR_RecordSegmentInfo_t));
834
835 list_add_tail(&p_info->head, list);
836
837 /*Load segment id*/
838 DVR_RETURN_IF_FALSE(p1);
839 p1 = strstr(buf, "id=");
840 DVR_RETURN_IF_FALSE(p1);
841 p_info->id = strtoull(p1 + 3, NULL, 10);
842
843 /*Save number of pids*/
844 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
845 DVR_RETURN_IF_FALSE(p1);
846 p1 = strstr(buf, "nb_pids=");
847 DVR_RETURN_IF_FALSE(p1);
848 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
849
850 /*Save pid information*/
851 for (i = 0; i < p_info->nb_pids; i++) {
852 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
853 DVR_RETURN_IF_FALSE(p1);
854 memset(value, 0, sizeof(value));
855 if ((p1 = strstr(buf, "pid="))) {
856 DVR_RETURN_IF_FALSE(p1);
857 p1 += 4;
858 if ((p2 = strstr(buf, ","))) {
859 DVR_RETURN_IF_FALSE(p2);
860 memcpy(value, p1, p2 - p1);
861 }
862 p_info->pids[i].pid = strtoull(value, NULL, 10);
863 }
864
865 memset(value, 0, sizeof(value));
866 if ((p1 = strstr(buf, "type="))) {
867 DVR_RETURN_IF_FALSE(p1);
868 p1 += 5;
869 if ((p2 = strstr(buf, "}"))) {
870 DVR_RETURN_IF_FALSE(p2);
871 memcpy(value, p1, p2 - p1);
872 }
873 p_info->pids[i].type = strtoull(value, NULL, 10);
874 }
875 }
876
877 /*Save segment duration*/
878 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
879 DVR_RETURN_IF_FALSE(p1);
880 p1 = strstr(buf, "duration=");
881 DVR_RETURN_IF_FALSE(p1);
882 p_info->duration = strtoull(p1 + 9, NULL, 10);
883 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
884
885 /*Save segment size*/
886 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
887 DVR_RETURN_IF_FALSE(p1);
888 p1 = strstr(buf, "size=");
889 DVR_RETURN_IF_FALSE(p1);
890 p_info->size = strtoull(p1 + 5, NULL, 10);
891
892 /*Save number of packets*/
893 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
894 DVR_RETURN_IF_FALSE(p1);
895 p1 = strstr(buf, "nb_packets=");
896 DVR_RETURN_IF_FALSE(p1);
897 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
898 //if reach end,exit loop
899 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
900 } while (p1);
901
902 return DVR_SUCCESS;
903}
904
Pengfei Liub4734232020-01-17 18:25:10 +0800905int segment_delete(const char *location, uint64_t segment_id)
906{
907 char fname[MAX_SEGMENT_PATH_SIZE];
908 int ret;
909
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800910 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800911
912 /*delete ts file*/
913 memset(fname, 0, sizeof(fname));
914 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
915 ret = unlink(fname);
916 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800917 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800918
919 /*delete index file*/
920 memset(fname, 0, sizeof(fname));
921 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
922 unlink(fname);
923 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800924 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800925
926 /*delete store information file*/
927 memset(fname, 0, sizeof(fname));
928 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
929 unlink(fname);
930 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800931 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800932
933 return DVR_SUCCESS;
934}
935
hualing chen87072a82020-03-12 16:20:12 +0800936int segment_ongoing(Segment_Handle_t handle)
937{
938 Segment_Context_t *p_ctx;
939 p_ctx = (Segment_Context_t *)handle;
940 struct stat mstat;
941
942 char going_name[MAX_SEGMENT_PATH_SIZE];
943 memset(going_name, 0, sizeof(going_name));
944 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
945 int ret = stat(going_name, &mstat);
946 DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret);
947 if (ret != 0) {
948 return DVR_FAILURE;
949 }
950 return DVR_SUCCESS;
951}
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800952loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800953{
954 Segment_Context_t *p_ctx;
955 char buf[256];
956 char value[256];
957 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800958 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800959 char *p1, *p2;
960
961 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800962 DVR_RETURN_IF_FALSE(p_ctx);
963 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
964 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800965
966 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800967 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800968 printf("start gets pts\n");
969 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
970 printf("buf[%s]\n", buf);
971 memset(value, 0, sizeof(value));
972 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800973 p1 += 5;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800974 if ((p2 = strstr(buf, ","))) {
975 memcpy(value, p1, p2 - p1);
976 }
977 pts = strtoull(value, NULL, 10);
978 }
979
980 memset(value, 0, sizeof(value));
981 if ((p1 = strstr(buf, "offset="))) {
982 p1 += 7;
983 if ((p2 = strstr(buf, "}"))) {
984 memcpy(value, p1, p2 - p1);
985 }
986 offset = strtoull(value, NULL, 10);
987 }
988
989 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800990 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800991 }
992
993 return 0;
994}