blob: ff47455bc38c6583d29afe10e4126341ebd9b8e9 [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)
18
19
Pengfei Liub4734232020-01-17 18:25:10 +080020/**\brief Segment context*/
Pengfei Liuc181a982020-01-07 19:27:13 +080021typedef struct {
Pengfei Liu3b1a8202020-02-12 23:04:21 +080022 int ts_fd; /**< Segment ts file fd*/
Pengfei Liub4734232020-01-17 18:25:10 +080023 FILE *index_fp; /**< Time index file fd*/
24 FILE *dat_fp; /**< Information file fd*/
hualing chenb9a02922021-12-14 11:29:47 +080025 FILE *all_dat_fp; /**< Information file fd*/
pengfei.liu567d6d82020-04-17 16:48:59 +080026 FILE *ongoing_fp; /**< Ongoing file fd, used to verify timedhift mode*/
Pengfei Liub4734232020-01-17 18:25:10 +080027 uint64_t first_pts; /**< First pts value, use for write mode*/
pengfei.liu567d6d82020-04-17 16:48:59 +080028 uint64_t last_pts; /**< Last input pts value, use for write mode*/
29 uint64_t last_record_pts; /**< Last record pts value, use for write mode*/
pengfei.liuab5a2262020-02-14 17:33:40 +080030 uint64_t cur_time; /**< Current time save in index file */
pengfei.liu567d6d82020-04-17 16:48:59 +080031 uint64_t segment_id; /**< Current segment ID */
32 char location[MAX_SEGMENT_PATH_SIZE]; /**< Current time save in index file */
hualing chena5f03222021-12-02 11:22:35 +080033 loff_t first_offset;
34 loff_t last_offset;
35 loff_t last_record_offset;
36 float avg_rate;
Pengfei Liuc181a982020-01-07 19:27:13 +080037} Segment_Context_t;
38
Pengfei Liub4734232020-01-17 18:25:10 +080039/**\brief Segment file type*/
Pengfei Liuc181a982020-01-07 19:27:13 +080040typedef enum {
Pengfei Liub4734232020-01-17 18:25:10 +080041 SEGMENT_FILE_TYPE_TS, /**< Used for store TS data*/
42 SEGMENT_FILE_TYPE_INDEX, /**< Used for store index data*/
43 SEGMENT_FILE_TYPE_DAT, /**< Used for store information data, such as duration etc*/
pengfei.liu567d6d82020-04-17 16:48:59 +080044 SEGMENT_FILE_TYPE_ONGOING, /**< Used for store information data, such as duration etc*/
hualing chenb9a02922021-12-14 11:29:47 +080045 SEGMENT_FILE_TYPE_ALLDAT, /**< Used for store all information data*/
Pengfei Liuc181a982020-01-07 19:27:13 +080046} Segment_FileType_t;
47
48static void segment_get_fname(char fname[MAX_SEGMENT_PATH_SIZE],
Pengfei Liub4734232020-01-17 18:25:10 +080049 const char location[DVR_MAX_LOCATION_SIZE],
Pengfei Liuc181a982020-01-07 19:27:13 +080050 uint64_t segment_id,
51 Segment_FileType_t type)
52{
53 int offset;
54
55 memset(fname, 0, MAX_SEGMENT_PATH_SIZE);
56 strncpy(fname, location, strlen(location));
57 offset = strlen(location);
hualing chenb9a02922021-12-14 11:29:47 +080058 if (type != SEGMENT_FILE_TYPE_ALLDAT) {
59 strncpy(fname + offset, "-", 1);
60 offset += 1;
61 sprintf(fname + offset, "%04llu", segment_id);
62 offset += 4;
63 }
64
Pengfei Liuc181a982020-01-07 19:27:13 +080065 if (type == SEGMENT_FILE_TYPE_TS)
66 strncpy(fname + offset, ".ts", 3);
67 else if (type == SEGMENT_FILE_TYPE_INDEX)
68 strncpy(fname + offset, ".idx", 4);
Pengfei Liub4734232020-01-17 18:25:10 +080069 else if (type == SEGMENT_FILE_TYPE_DAT)
70 strncpy(fname + offset, ".dat", 4);
hualing chen87072a82020-03-12 16:20:12 +080071 else if (type == SEGMENT_FILE_TYPE_ONGOING)
72 strncpy(fname + offset, ".going", 6);
hualing chenb9a02922021-12-14 11:29:47 +080073 else if (type == SEGMENT_FILE_TYPE_ALLDAT)
74 strncpy(fname + offset, ".dat", 4);
75
Pengfei Liuc181a982020-01-07 19:27:13 +080076}
77
Pengfei Liu3b1a8202020-02-12 23:04:21 +080078static void segment_get_dirname(char dir_name[MAX_SEGMENT_PATH_SIZE],
79 const char location[DVR_MAX_LOCATION_SIZE])
80{
81 char *p;
82 int i;
83 int found = 0;
84
85 for (i = 0; i < (int)strlen(location); i++) {
86 if (location[i] == '/') {
87 p = (char *)location + i;
88 found = 1;
89 }
90 }
91 if (found)
92 memcpy(dir_name, location, p - location);
93}
94
Pengfei Liuc181a982020-01-07 19:27:13 +080095int segment_open(Segment_OpenParams_t *params, Segment_Handle_t *p_handle)
96{
97 Segment_Context_t *p_ctx;
98 char ts_fname[MAX_SEGMENT_PATH_SIZE];
99 char index_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liub4734232020-01-17 18:25:10 +0800100 char dat_fname[MAX_SEGMENT_PATH_SIZE];
hualing chenb9a02922021-12-14 11:29:47 +0800101 char all_dat_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800102 char dir_name[MAX_SEGMENT_PATH_SIZE];
hualing chen87072a82020-03-12 16:20:12 +0800103 char going_name[MAX_SEGMENT_PATH_SIZE];
Pengfei Liuc181a982020-01-07 19:27:13 +0800104
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800105 DVR_RETURN_IF_FALSE(params);
106 DVR_RETURN_IF_FALSE(p_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +0800107
hualing chena540a7e2020-03-27 16:44:05 +0800108 //DVR_DEBUG(1, "%s, location:%s, id:%llu", __func__, params->location, params->segment_id);
Pengfei Liuc181a982020-01-07 19:27:13 +0800109
Pengfei Liub038b6a2020-01-14 15:57:01 +0800110 p_ctx = (void*)malloc(sizeof(Segment_Context_t));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800111 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +0800112 memset(p_ctx, 0, sizeof(Segment_Context_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800113
114 memset(ts_fname, 0, sizeof(ts_fname));
115 segment_get_fname(ts_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_TS);
116
117 memset(index_fname, 0, sizeof(index_fname));
118 segment_get_fname(index_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_INDEX);
119
Pengfei Liub4734232020-01-17 18:25:10 +0800120 memset(dat_fname, 0, sizeof(dat_fname));
121 segment_get_fname(dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_DAT);
122
hualing chenb9a02922021-12-14 11:29:47 +0800123 memset(all_dat_fname, 0, sizeof(all_dat_fname));
124 segment_get_fname(all_dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_ALLDAT);
125
126
hualing chen87072a82020-03-12 16:20:12 +0800127 memset(going_name, 0, sizeof(going_name));
128 segment_get_fname(going_name, params->location, params->segment_id, SEGMENT_FILE_TYPE_ONGOING);
129
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800130 memset(dir_name, 0, sizeof(dir_name));
131 segment_get_dirname(dir_name, params->location);
132 if (access(dir_name, F_OK) == -1) {
133 DVR_DEBUG(1, "%s dir %s is not exist, create it", __func__, dir_name);
134 mkdir(dir_name, 0666);
135 }
136
Pengfei Liuc181a982020-01-07 19:27:13 +0800137 if (params->mode == SEGMENT_MODE_READ) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800138 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800139 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800140 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800141 p_ctx->ongoing_fp = NULL;
hualing chenb9a02922021-12-14 11:29:47 +0800142 p_ctx->all_dat_fp = fopen(all_dat_fname, "r");
Pengfei Liuc181a982020-01-07 19:27:13 +0800143 } else if (params->mode == SEGMENT_MODE_WRITE) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800144 p_ctx->ts_fd = open(ts_fname, O_CREAT | O_RDWR | O_TRUNC, 0644);
Pengfei Liuc181a982020-01-07 19:27:13 +0800145 p_ctx->index_fp = fopen(index_fname, "w+");
Pengfei Liub4734232020-01-17 18:25:10 +0800146 p_ctx->dat_fp = fopen(dat_fname, "w+");
hualing chenb9a02922021-12-14 11:29:47 +0800147 p_ctx->all_dat_fp = fopen(all_dat_fname, "a+");
hualing chen87072a82020-03-12 16:20:12 +0800148 p_ctx->ongoing_fp = fopen(going_name, "w+");
Pengfei Liub038b6a2020-01-14 15:57:01 +0800149 p_ctx->first_pts = ULLONG_MAX;
150 p_ctx->last_pts = ULLONG_MAX;
pengfei.liu567d6d82020-04-17 16:48:59 +0800151 p_ctx->last_record_pts = ULLONG_MAX;
hualing chena5f03222021-12-02 11:22:35 +0800152 p_ctx->avg_rate = 0.0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800153 } else {
154 DVR_DEBUG(1, "%s, unknow mode use default", __func__);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800155 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800156 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800157 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chenb9a02922021-12-14 11:29:47 +0800158 p_ctx->all_dat_fp = fopen(all_dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800159 p_ctx->ongoing_fp = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800160 }
161
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800162 if (p_ctx->ts_fd == -1 || !p_ctx->index_fp || !p_ctx->dat_fp) {
Pengfei Liub4734232020-01-17 18:25:10 +0800163 DVR_DEBUG(1, "%s open file failed [%s, %s, %s], reason:%s", __func__,
164 ts_fname, index_fname, dat_fname, strerror(errno));
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800165 if (p_ctx->ts_fd != -1)
166 close(p_ctx->ts_fd);
167 if (p_ctx->index_fp)
168 fclose(p_ctx->index_fp);
169 if (p_ctx->dat_fp)
170 fclose(p_ctx->dat_fp);
171 if (p_ctx->ongoing_fp)
172 fclose(p_ctx->ongoing_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800173 free(p_ctx);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800174 *p_handle = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800175 return DVR_FAILURE;
176 }
hualing chen87072a82020-03-12 16:20:12 +0800177 p_ctx->segment_id = params->segment_id;
178 strncpy(p_ctx->location, params->location, strlen(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800179
hualing chen7a56cba2020-04-14 14:09:27 +0800180 //DVR_DEBUG(1, "%s, open file success p_ctx->location [%s]", __func__, p_ctx->location, params->mode);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800181 *p_handle = (Segment_Handle_t)p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800182 return DVR_SUCCESS;
183}
184
185int segment_close(Segment_Handle_t handle)
186{
187 Segment_Context_t *p_ctx;
188
Pengfei Liub038b6a2020-01-14 15:57:01 +0800189 p_ctx = (void *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800190 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800191
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800192 if (p_ctx->ts_fd != -1) {
193 close(p_ctx->ts_fd);
Pengfei Liuc181a982020-01-07 19:27:13 +0800194 }
195
196 if (p_ctx->index_fp) {
197 fclose(p_ctx->index_fp);
198 }
199
Pengfei Liub4734232020-01-17 18:25:10 +0800200 if (p_ctx->dat_fp) {
201 fclose(p_ctx->dat_fp);
202 }
203
hualing chen87072a82020-03-12 16:20:12 +0800204 if (p_ctx->ongoing_fp != NULL) {
205 fclose(p_ctx->ongoing_fp);
206 char going_name[MAX_SEGMENT_PATH_SIZE];
207 memset(going_name, 0, sizeof(going_name));
208 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
209 DVR_DEBUG(1, "segment close del [%s]", going_name);
210 unlink(going_name);
211 }
212
Pengfei Liuc181a982020-01-07 19:27:13 +0800213 free(p_ctx);
214 return 0;
215}
216
217ssize_t segment_read(Segment_Handle_t handle, void *buf, size_t count)
218{
219 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800220 ssize_t len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800221 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800222 DVR_RETURN_IF_FALSE(p_ctx);
223 DVR_RETURN_IF_FALSE(buf);
224 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800225 len = read(p_ctx->ts_fd, buf, count);
226 return len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800227}
228
229ssize_t segment_write(Segment_Handle_t handle, void *buf, size_t count)
230{
231 Segment_Context_t *p_ctx;
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800232 ssize_t len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800233 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800234 DVR_RETURN_IF_FALSE(p_ctx);
235 DVR_RETURN_IF_FALSE(buf);
236 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800237 len = write(p_ctx->ts_fd, buf, count);
238 fsync(p_ctx->ts_fd);
239 return len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800240}
241
hualing chen2932d372020-04-29 13:44:00 +0800242int segment_update_pts_force(Segment_Handle_t handle, uint64_t pts, loff_t offset)
Pengfei Liuc181a982020-01-07 19:27:13 +0800243{
244 Segment_Context_t *p_ctx;
245 char buf[256];
pengfei.liu567d6d82020-04-17 16:48:59 +0800246 int record_diff = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800247
248 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800249 DVR_RETURN_IF_FALSE(p_ctx);
250 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800251
Pengfei Liub038b6a2020-01-14 15:57:01 +0800252 if (p_ctx->first_pts == ULLONG_MAX) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800253 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800254 p_ctx->first_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800255 p_ctx->first_offset = offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800256 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800257 memset(buf, 0, sizeof(buf));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800258 if (p_ctx->last_pts == ULLONG_MAX) {
259 /*Last pts is init value*/
hualing chen2aba4022020-03-02 13:49:55 +0800260 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
pengfei.liuab5a2262020-02-14 17:33:40 +0800261 p_ctx->cur_time = pts - p_ctx->first_pts;
hualing chena5f03222021-12-02 11:22:35 +0800262 DVR_DEBUG(1, "%s force pcr:%llu -1", __func__, pts);
hualing chen2932d372020-04-29 13:44:00 +0800263 } else {
264 /*Last pts has valid value*/
265 int diff = pts - p_ctx->last_pts;
266 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
267 /*Current pts has a transition*/
268 DVR_DEBUG(1, "[%s]force update Current pts has a transition, [%llu, %llu, %llu]",__func__,
269 p_ctx->first_pts, p_ctx->last_pts, pts);
270 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
271 } else {
272 /*This is a normal pts, record it*/
hualing chena5f03222021-12-02 11:22:35 +0800273 //check if this pcr is transition.if true,add 200ms
274 //other case normal.
hualing chen2932d372020-04-29 13:44:00 +0800275 p_ctx->cur_time += diff;
276 DVR_DEBUG(1, "%s force pcr:%llu -1 diff [%d]", __func__, pts, diff);
277 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
278 }
279 }
280
281 record_diff = pts - p_ctx->last_record_pts;
282 if (strlen(buf) > 0) {
283 DVR_DEBUG(1, "%s force pcr:%llu buf:%s", __func__, pts, buf);
284 fputs(buf, p_ctx->index_fp);
285 fflush(p_ctx->index_fp);
286 fsync(fileno(p_ctx->index_fp));
287 p_ctx->last_record_pts = pts;
288 }
289 p_ctx->last_pts = pts;
hualing chen2932d372020-04-29 13:44:00 +0800290 return DVR_SUCCESS;
291}
292
293int segment_update_pts(Segment_Handle_t handle, uint64_t pts, loff_t offset)
294{
295 Segment_Context_t *p_ctx;
296 char buf[256];
297 int record_diff = 0;
298
299 p_ctx = (Segment_Context_t *)handle;
300 DVR_RETURN_IF_FALSE(p_ctx);
301 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
302
303 if (p_ctx->first_pts == ULLONG_MAX) {
304 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
305 p_ctx->first_pts = pts;
306 //p_ctx->cur_time = p_ctx->cur_time + PTS_HEAD_DEVIATION;
307 }
308 memset(buf, 0, sizeof(buf));
309 if (p_ctx->last_pts == ULLONG_MAX) {
310 /*Last pts is init value*/
311 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
312 p_ctx->cur_time = pts - p_ctx->first_pts;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800313 } else {
314 /*Last pts has valid value*/
pengfei.liu567d6d82020-04-17 16:48:59 +0800315 int diff = pts - p_ctx->last_pts;
hualing chen4b7c15d2020-04-07 16:13:48 +0800316 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
Pengfei Liub038b6a2020-01-14 15:57:01 +0800317 /*Current pts has a transition*/
pengfei.liuab5a2262020-02-14 17:33:40 +0800318 DVR_DEBUG(1, "Current pts has a transition, [%llu, %llu, %llu]",
319 p_ctx->first_pts, p_ctx->last_pts, pts);
pengfei.liu567d6d82020-04-17 16:48:59 +0800320 p_ctx->last_record_pts = pts;
hualing chen2932d372020-04-29 13:44:00 +0800321 //p_ctx->cur_time = p_ctx->cur_time + PTS_DISCONTINE_DEVIATION;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800322 } else {
323 /*This is a normal pts, record it*/
hualing chena5f03222021-12-02 11:22:35 +0800324 loff_t off_diff = offset - p_ctx->last_offset;
325 float rate = (float) (off_diff) / (float)(diff);
326 if (p_ctx->avg_rate == 0.0) {
327 p_ctx->avg_rate = (float) offset / (float)(p_ctx->cur_time + diff);
328 }
329 if (diff >= 1000) {
330 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));
331 if (p_ctx->avg_rate != 0 && (int)(p_ctx->avg_rate) >= (int)(rate * 4)) {
332 diff = off_diff / p_ctx->avg_rate;
333 p_ctx->cur_time += diff;
334 } else {
335 p_ctx->cur_time += diff;
336 }
337 } else {
338 p_ctx->cur_time += diff;
339 }
hualing chen2aba4022020-03-02 13:49:55 +0800340 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800341 }
342 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800343
344 record_diff = pts - p_ctx->last_record_pts;
345 if (strlen(buf) > 0 &&
346 (record_diff > PCR_RECORD_INTERVAL_MS || p_ctx->last_record_pts == ULLONG_MAX)){
hualing chen4b7c15d2020-04-07 16:13:48 +0800347 fputs(buf, p_ctx->index_fp);
pengfei.liu567d6d82020-04-17 16:48:59 +0800348 fflush(p_ctx->index_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800349 fsync(fileno(p_ctx->index_fp));
pengfei.liu567d6d82020-04-17 16:48:59 +0800350 p_ctx->last_record_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800351 p_ctx->last_record_offset = offset;
352 if (p_ctx->cur_time > 0)
353 p_ctx->avg_rate = (float) offset / (float)p_ctx->cur_time;
pengfei.liu567d6d82020-04-17 16:48:59 +0800354 }
Pengfei Liub038b6a2020-01-14 15:57:01 +0800355 p_ctx->last_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800356 p_ctx->last_offset = offset;
Pengfei Liuc181a982020-01-07 19:27:13 +0800357 return DVR_SUCCESS;
358}
359
hualing chen266b9502020-04-04 17:39:39 +0800360loff_t segment_seek(Segment_Handle_t handle, uint64_t time, int block_size)
Pengfei Liuc181a982020-01-07 19:27:13 +0800361{
362 Segment_Context_t *p_ctx;
363 char buf[256];
364 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800365 uint64_t pts = 0L;
366 loff_t offset = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800367 char *p1, *p2;
368
hualing chen8a657f32021-08-30 13:12:49 +0800369 DVR_DEBUG(1, "into seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
370
Pengfei Liuc181a982020-01-07 19:27:13 +0800371 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800372 DVR_RETURN_IF_FALSE(p_ctx);
373 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
374 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800375
hualing chen266b9502020-04-04 17:39:39 +0800376 if (time == 0) {
377 offset = 0;
hualing chend241c7a2021-06-22 13:34:27 +0800378 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
379 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen266b9502020-04-04 17:39:39 +0800380 return offset;
381 }
382
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800383 memset(buf, 0, sizeof(buf));
384 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chen2aba4022020-03-02 13:49:55 +0800385 int line = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800386 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800387 line++;
Pengfei Liuc181a982020-01-07 19:27:13 +0800388 memset(value, 0, sizeof(value));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800389 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800390 p1 += 5;
Pengfei Liuc181a982020-01-07 19:27:13 +0800391 if ((p2 = strstr(buf, ","))) {
392 memcpy(value, p1, p2 - p1);
393 }
394 pts = strtoull(value, NULL, 10);
395 }
396
397 memset(value, 0, sizeof(value));
398 if ((p1 = strstr(buf, "offset="))) {
399 p1 += 7;
400 if ((p2 = strstr(buf, "}"))) {
401 memcpy(value, p1, p2 - p1);
402 }
403 offset = strtoull(value, NULL, 10);
404 }
hualing chen2aba4022020-03-02 13:49:55 +0800405 if (0)
406 {
407 DVR_DEBUG(1, "seek buf[%s]", buf);
408 DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
409 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800410 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800411 if (time <= pts) {
hualing chen266b9502020-04-04 17:39:39 +0800412 if (block_size > 0) {
413 offset = offset - offset%block_size;
414 }
hualing chend241c7a2021-06-22 13:34:27 +0800415 //DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line);
416 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800417 return offset;
418 }
hualing chen2aba4022020-03-02 13:49:55 +0800419 }
hualing chen2932d372020-04-29 13:44:00 +0800420 if (time > pts) {
421 if (block_size > 0) {
422 offset = offset - offset%block_size;
423 }
424 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 +0800425 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen2932d372020-04-29 13:44:00 +0800426 return offset;
427 }
hualing chen2aba4022020-03-02 13:49:55 +0800428 DVR_DEBUG(1, "seek error line [%d]", line);
Pengfei Liuc181a982020-01-07 19:27:13 +0800429 return DVR_FAILURE;
430}
431
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800432loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800433{
434 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800435 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800436 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800437 DVR_RETURN_IF_FALSE(p_ctx);
438 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800439 pos = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
440 return pos;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800441}
442
hualing chen5605eed2020-05-26 18:18:06 +0800443uint64_t segment_tell_position_time(Segment_Handle_t handle, loff_t position)
444{
445 Segment_Context_t *p_ctx;
446 char buf[256];
447 char value[256];
hualing chen969fe7b2021-05-26 15:13:17 +0800448 uint64_t ret = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800449 uint64_t pts = 0L;
hualing chen969fe7b2021-05-26 15:13:17 +0800450 uint64_t pts_p = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800451 loff_t offset = 0;
hualing chen969fe7b2021-05-26 15:13:17 +0800452 loff_t offset_p = 0;
hualing chen5605eed2020-05-26 18:18:06 +0800453 char *p1, *p2;
454
455 p_ctx = (Segment_Context_t *)handle;
456 DVR_RETURN_IF_FALSE(p_ctx);
457 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
458 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
459
460 memset(buf, 0, sizeof(buf));
461 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
462 DVR_RETURN_IF_FALSE(position != -1);
463
464 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
465 memset(value, 0, sizeof(value));
466 if ((p1 = strstr(buf, "time="))) {
467 p1 += 5;
468 if ((p2 = strstr(buf, ","))) {
469 memcpy(value, p1, p2 - p1);
470 }
471 pts = strtoull(value, NULL, 10);
472 }
473
474 memset(value, 0, sizeof(value));
475 if ((p1 = strstr(buf, "offset="))) {
476 p1 += 7;
477 if ((p2 = strstr(buf, "}"))) {
478 memcpy(value, p1, p2 - p1);
479 }
480 offset = strtoull(value, NULL, 10);
481 }
482
483 memset(buf, 0, sizeof(buf));
484 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chend241c7a2021-06-22 13:34:27 +0800485 if (position <= offset
486 &&position >= offset_p
487 && offset - offset_p > 0) {
hualing chen969fe7b2021-05-26 15:13:17 +0800488 ret = pts_p + (pts - pts_p) * (position - offset_p) / (offset - offset_p);
hualing chend241c7a2021-06-22 13:34:27 +0800489 //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 +0800490 return ret;
hualing chen5605eed2020-05-26 18:18:06 +0800491 }
hualing chen969fe7b2021-05-26 15:13:17 +0800492 offset_p = offset;
493 pts_p = pts;
hualing chen5605eed2020-05-26 18:18:06 +0800494 }
495 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
496 return pts;
497}
498
499
pengfei.liu8b563292020-02-26 15:49:02 +0800500uint64_t segment_tell_current_time(Segment_Handle_t handle)
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800501{
502 Segment_Context_t *p_ctx;
503 char buf[256];
504 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800505 uint64_t pts = 0L;
506 loff_t offset = 0, position = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800507 char *p1, *p2;
508
509 p_ctx = (Segment_Context_t *)handle;
510 DVR_RETURN_IF_FALSE(p_ctx);
511 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
512 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
513
514 memset(buf, 0, sizeof(buf));
515 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800516 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800517 DVR_RETURN_IF_FALSE(position != -1);
518
519 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
520 memset(value, 0, sizeof(value));
521 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800522 p1 += 5;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800523 if ((p2 = strstr(buf, ","))) {
524 memcpy(value, p1, p2 - p1);
525 }
526 pts = strtoull(value, NULL, 10);
527 }
528
529 memset(value, 0, sizeof(value));
530 if ((p1 = strstr(buf, "offset="))) {
531 p1 += 7;
532 if ((p2 = strstr(buf, "}"))) {
533 memcpy(value, p1, p2 - p1);
534 }
535 offset = strtoull(value, NULL, 10);
536 }
537
538 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800539 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800540 if (position <= offset) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800541 return pts;
542 }
543 }
hualing chena540a7e2020-03-27 16:44:05 +0800544 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800545 return pts;
Pengfei Liuc181a982020-01-07 19:27:13 +0800546}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800547
pengfei.liu8b563292020-02-26 15:49:02 +0800548uint64_t segment_tell_total_time(Segment_Handle_t handle)
549{
550 Segment_Context_t *p_ctx;
551 char buf[256];
552 char last_buf[256];
553 char value[256];
554 uint64_t pts = ULLONG_MAX;
555 loff_t offset = 0, position = 0;
556 char *p1, *p2;
557 int line = 0;
558
559 p_ctx = (Segment_Context_t *)handle;
560 DVR_RETURN_IF_FALSE(p_ctx);
561 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
562 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
563
564 memset(buf, 0, sizeof(buf));
565 memset(last_buf, 0, sizeof(last_buf));
hualing chend241c7a2021-06-22 13:34:27 +0800566 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
pengfei.liu8b563292020-02-26 15:49:02 +0800567 DVR_RETURN_IF_FALSE(position != -1);
568
hualing chen041c4092020-04-05 15:11:50 +0800569 //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1);
570 //if seek error.we need seek 0 pos.
571 if (fseek(p_ctx->index_fp, -1000L, SEEK_END) == -1) {
572 fseek(p_ctx->index_fp, 0L, SEEK_SET);
573 }
pengfei.liu8b563292020-02-26 15:49:02 +0800574 /* Save last line buffer */
575 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800576 if (strlen(buf) <= 0) {
hualing chen87072a82020-03-12 16:20:12 +0800577 DVR_DEBUG(1, "read index buf is len 0");
hualing chen2aba4022020-03-02 13:49:55 +0800578 continue;
579 }
pengfei.liu8b563292020-02-26 15:49:02 +0800580 memset(last_buf, 0, sizeof(last_buf));
581 memcpy(last_buf, buf, strlen(buf));
582 memset(buf, 0, sizeof(buf));
583 line++;
584 }
585
586 /* Extract time value */
587 memset(value, 0, sizeof(value));
588 if ((p1 = strstr(last_buf, "time="))) {
589 p1 += 5;
590 if ((p2 = strstr(last_buf, ","))) {
591 memcpy(value, p1, p2 - p1);
592 }
593 pts = strtoull(value, NULL, 10);
594 }
595
596 memset(value, 0, sizeof(value));
597 if ((p1 = strstr(last_buf, "offset="))) {
598 p1 += 7;
599 if ((p2 = strstr(last_buf, "}"))) {
600 memcpy(value, p1, p2 - p1);
601 }
602 offset = strtoull(value, NULL, 10);
603 }
hualing chen87072a82020-03-12 16:20:12 +0800604 //if (line < 2)
605 //DVR_DEBUG(1, "totle time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line);
pengfei.liu8b563292020-02-26 15:49:02 +0800606 return (pts == ULLONG_MAX ? DVR_FAILURE : pts);
607}
608
pengfei.liuab5a2262020-02-14 17:33:40 +0800609/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800610int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
611{
612 Segment_Context_t *p_ctx;
613 char buf[256];
614 uint32_t i;
615
616 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800617 DVR_RETURN_IF_FALSE(p_ctx);
618 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
619 DVR_RETURN_IF_FALSE(p_info);
hualing chen87072a82020-03-12 16:20:12 +0800620 //seek 0, rewrite info
621 DVR_RETURN_IF_FALSE(fseek(p_ctx->dat_fp, 0, SEEK_SET) != -1);
Pengfei Liub4734232020-01-17 18:25:10 +0800622
623 /*Save segment id*/
624 memset(buf, 0, sizeof(buf));
625 sprintf(buf, "id=%lld\n", p_info->id);
626 fputs(buf, p_ctx->dat_fp);
627
628 /*Save number of pids*/
629 memset(buf, 0, sizeof(buf));
630 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
631 fputs(buf, p_ctx->dat_fp);
632
633 /*Save pid information*/
634 for (i = 0; i < p_info->nb_pids; i++) {
635 memset(buf, 0, sizeof(buf));
636 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
637 fputs(buf, p_ctx->dat_fp);
638 }
639
640 /*Save segment duration*/
641 memset(buf, 0, sizeof(buf));
hualing chen87072a82020-03-12 16:20:12 +0800642 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800643 sprintf(buf, "duration=%ld\n", p_info->duration);
644 fputs(buf, p_ctx->dat_fp);
645
646 /*Save segment size*/
647 memset(buf, 0, sizeof(buf));
648 sprintf(buf, "size=%zu\n", p_info->size);
649 fputs(buf, p_ctx->dat_fp);
650
651 /*Save number of packets*/
652 memset(buf, 0, sizeof(buf));
653 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
654 fputs(buf, p_ctx->dat_fp);
655
656 fflush(p_ctx->dat_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800657 fsync(fileno(p_ctx->dat_fp));
Pengfei Liub4734232020-01-17 18:25:10 +0800658 return DVR_SUCCESS;
659}
660
pengfei.liuab5a2262020-02-14 17:33:40 +0800661/* Should consider the case of cut power, todo... */
hualing chenb9a02922021-12-14 11:29:47 +0800662int segment_store_allInfo(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
663{
664 Segment_Context_t *p_ctx;
665 char buf[256];
666 uint32_t i;
667
668 p_ctx = (Segment_Context_t *)handle;
669 DVR_RETURN_IF_FALSE(p_ctx);
670 DVR_RETURN_IF_FALSE(p_ctx->all_dat_fp);
671 DVR_RETURN_IF_FALSE(p_info);
672 //seek 0, rewrite info
673 DVR_RETURN_IF_FALSE(fseek(p_ctx->all_dat_fp, 0, SEEK_END) != -1);
674
675 /*Save segment id*/
676 memset(buf, 0, sizeof(buf));
677 sprintf(buf, "id=%lld\n", p_info->id);
678 fputs(buf, p_ctx->all_dat_fp);
679
680 /*Save number of pids*/
681 memset(buf, 0, sizeof(buf));
682 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
683 fputs(buf, p_ctx->all_dat_fp);
684
685 /*Save pid information*/
686 for (i = 0; i < p_info->nb_pids; i++) {
687 memset(buf, 0, sizeof(buf));
688 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
689 fputs(buf, p_ctx->all_dat_fp);
690 }
691
692 /*Save segment duration*/
693 memset(buf, 0, sizeof(buf));
694 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
695 sprintf(buf, "duration=%ld\n", p_info->duration);
696 fputs(buf, p_ctx->all_dat_fp);
697
698 /*Save segment size*/
699 memset(buf, 0, sizeof(buf));
700 sprintf(buf, "size=%zu\n", p_info->size);
701 fputs(buf, p_ctx->all_dat_fp);
702
703 /*Save number of packets*/
704 memset(buf, 0, sizeof(buf));
705 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
706 fputs(buf, p_ctx->all_dat_fp);
707
708 fflush(p_ctx->all_dat_fp);
709 fsync(fileno(p_ctx->all_dat_fp));
710 return DVR_SUCCESS;
711}
712
713/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800714int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
715{
716 Segment_Context_t *p_ctx;
717 uint32_t i;
718 char buf[256];
719 char value[256];
720 char *p1, *p2;
721
722 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800723 DVR_RETURN_IF_FALSE(p_ctx);
724 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800725
726 /*Load segment id*/
727 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800728 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800729 p1 = strstr(buf, "id=");
hualing chen2aba4022020-03-02 13:49:55 +0800730 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800731 p_info->id = strtoull(p1 + 3, NULL, 10);
732
733 /*Save number of pids*/
734 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800735 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800736 p1 = strstr(buf, "nb_pids=");
hualing chen2aba4022020-03-02 13:49:55 +0800737 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800738 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
739
740 /*Save pid information*/
741 for (i = 0; i < p_info->nb_pids; i++) {
742 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800743 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800744 memset(value, 0, sizeof(value));
745 if ((p1 = strstr(buf, "pid="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800746 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800747 p1 += 4;
748 if ((p2 = strstr(buf, ","))) {
hualing chen2aba4022020-03-02 13:49:55 +0800749 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800750 memcpy(value, p1, p2 - p1);
751 }
752 p_info->pids[i].pid = strtoull(value, NULL, 10);
753 }
754
755 memset(value, 0, sizeof(value));
756 if ((p1 = strstr(buf, "type="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800757 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800758 p1 += 5;
759 if ((p2 = strstr(buf, "}"))) {
hualing chen2aba4022020-03-02 13:49:55 +0800760 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800761 memcpy(value, p1, p2 - p1);
762 }
763 p_info->pids[i].type = strtoull(value, NULL, 10);
764 }
765 }
766
767 /*Save segment duration*/
768 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800769 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800770 p1 = strstr(buf, "duration=");
hualing chen2aba4022020-03-02 13:49:55 +0800771 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800772 p_info->duration = strtoull(p1 + 9, NULL, 10);
hualing chena540a7e2020-03-27 16:44:05 +0800773 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800774
775 /*Save segment size*/
776 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800777 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800778 p1 = strstr(buf, "size=");
hualing chen2aba4022020-03-02 13:49:55 +0800779 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800780 p_info->size = strtoull(p1 + 5, NULL, 10);
781
782 /*Save number of packets*/
783 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800784 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800785 p1 = strstr(buf, "nb_packets=");
hualing chen2aba4022020-03-02 13:49:55 +0800786 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800787 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
788
789 return DVR_SUCCESS;
790}
791
hualing chenb9a02922021-12-14 11:29:47 +0800792/* Should consider the case of cut power, todo... */
793int segment_load_allInfo(Segment_Handle_t handle, struct list_head *list)
794{
795 Segment_Context_t *p_ctx;
796 uint32_t i;
797 char buf[256];
798 char value[256];
799 char *p1, *p2;
800
801 p_ctx = (Segment_Context_t *)handle;
802 DVR_RETURN_IF_FALSE(p_ctx);
803 DVR_RETURN_IF_FALSE(list);
804
805 //first get
806 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
807 DVR_RETURN_IF_FALSE(p1);
808
809 do {
810
811 DVR_RecordSegmentInfo_t *p_info;
812
813 p_info = malloc(sizeof(DVR_RecordSegmentInfo_t));
814 memset(p_info, 0, sizeof(DVR_RecordSegmentInfo_t));
815
816 list_add_tail(&p_info->head, list);
817
818 /*Load segment id*/
819 DVR_RETURN_IF_FALSE(p1);
820 p1 = strstr(buf, "id=");
821 DVR_RETURN_IF_FALSE(p1);
822 p_info->id = strtoull(p1 + 3, NULL, 10);
823
824 /*Save number of pids*/
825 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
826 DVR_RETURN_IF_FALSE(p1);
827 p1 = strstr(buf, "nb_pids=");
828 DVR_RETURN_IF_FALSE(p1);
829 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
830
831 /*Save pid information*/
832 for (i = 0; i < p_info->nb_pids; i++) {
833 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
834 DVR_RETURN_IF_FALSE(p1);
835 memset(value, 0, sizeof(value));
836 if ((p1 = strstr(buf, "pid="))) {
837 DVR_RETURN_IF_FALSE(p1);
838 p1 += 4;
839 if ((p2 = strstr(buf, ","))) {
840 DVR_RETURN_IF_FALSE(p2);
841 memcpy(value, p1, p2 - p1);
842 }
843 p_info->pids[i].pid = strtoull(value, NULL, 10);
844 }
845
846 memset(value, 0, sizeof(value));
847 if ((p1 = strstr(buf, "type="))) {
848 DVR_RETURN_IF_FALSE(p1);
849 p1 += 5;
850 if ((p2 = strstr(buf, "}"))) {
851 DVR_RETURN_IF_FALSE(p2);
852 memcpy(value, p1, p2 - p1);
853 }
854 p_info->pids[i].type = strtoull(value, NULL, 10);
855 }
856 }
857
858 /*Save segment duration*/
859 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
860 DVR_RETURN_IF_FALSE(p1);
861 p1 = strstr(buf, "duration=");
862 DVR_RETURN_IF_FALSE(p1);
863 p_info->duration = strtoull(p1 + 9, NULL, 10);
864 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
865
866 /*Save segment size*/
867 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
868 DVR_RETURN_IF_FALSE(p1);
869 p1 = strstr(buf, "size=");
870 DVR_RETURN_IF_FALSE(p1);
871 p_info->size = strtoull(p1 + 5, NULL, 10);
872
873 /*Save number of packets*/
874 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
875 DVR_RETURN_IF_FALSE(p1);
876 p1 = strstr(buf, "nb_packets=");
877 DVR_RETURN_IF_FALSE(p1);
878 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
879 //if reach end,exit loop
880 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
881 } while (p1);
882
883 return DVR_SUCCESS;
884}
885
Pengfei Liub4734232020-01-17 18:25:10 +0800886int segment_delete(const char *location, uint64_t segment_id)
887{
888 char fname[MAX_SEGMENT_PATH_SIZE];
889 int ret;
890
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800891 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800892
893 /*delete ts file*/
894 memset(fname, 0, sizeof(fname));
895 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
896 ret = unlink(fname);
897 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800898 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800899
900 /*delete index file*/
901 memset(fname, 0, sizeof(fname));
902 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
903 unlink(fname);
904 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800905 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800906
907 /*delete store information file*/
908 memset(fname, 0, sizeof(fname));
909 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
910 unlink(fname);
911 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800912 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800913
914 return DVR_SUCCESS;
915}
916
hualing chen87072a82020-03-12 16:20:12 +0800917int segment_ongoing(Segment_Handle_t handle)
918{
919 Segment_Context_t *p_ctx;
920 p_ctx = (Segment_Context_t *)handle;
921 struct stat mstat;
922
923 char going_name[MAX_SEGMENT_PATH_SIZE];
924 memset(going_name, 0, sizeof(going_name));
925 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
926 int ret = stat(going_name, &mstat);
927 DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret);
928 if (ret != 0) {
929 return DVR_FAILURE;
930 }
931 return DVR_SUCCESS;
932}
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800933loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800934{
935 Segment_Context_t *p_ctx;
936 char buf[256];
937 char value[256];
938 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800939 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800940 char *p1, *p2;
941
942 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800943 DVR_RETURN_IF_FALSE(p_ctx);
944 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
945 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800946
947 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800948 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800949 printf("start gets pts\n");
950 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
951 printf("buf[%s]\n", buf);
952 memset(value, 0, sizeof(value));
953 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800954 p1 += 5;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800955 if ((p2 = strstr(buf, ","))) {
956 memcpy(value, p1, p2 - p1);
957 }
958 pts = strtoull(value, NULL, 10);
959 }
960
961 memset(value, 0, sizeof(value));
962 if ((p1 = strstr(buf, "offset="))) {
963 p1 += 7;
964 if ((p2 = strstr(buf, "}"))) {
965 memcpy(value, p1, p2 - p1);
966 }
967 offset = strtoull(value, NULL, 10);
968 }
969
970 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800971 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800972 }
973
974 return 0;
975}