blob: 684d07039e9a893b8f35de8f9f74f6ea2bca7ca8 [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);
hualing chen926a8ec2021-12-20 20:38:24 +0800171 if (p_ctx->all_dat_fp)
172 fclose(p_ctx->all_dat_fp);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800173 if (p_ctx->ongoing_fp)
174 fclose(p_ctx->ongoing_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800175 free(p_ctx);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800176 *p_handle = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800177 return DVR_FAILURE;
178 }
hualing chen87072a82020-03-12 16:20:12 +0800179 p_ctx->segment_id = params->segment_id;
180 strncpy(p_ctx->location, params->location, strlen(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800181
hualing chen7a56cba2020-04-14 14:09:27 +0800182 //DVR_DEBUG(1, "%s, open file success p_ctx->location [%s]", __func__, p_ctx->location, params->mode);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800183 *p_handle = (Segment_Handle_t)p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800184 return DVR_SUCCESS;
185}
186
187int segment_close(Segment_Handle_t handle)
188{
189 Segment_Context_t *p_ctx;
190
Pengfei Liub038b6a2020-01-14 15:57:01 +0800191 p_ctx = (void *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800192 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800193
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800194 if (p_ctx->ts_fd != -1) {
195 close(p_ctx->ts_fd);
Pengfei Liuc181a982020-01-07 19:27:13 +0800196 }
197
198 if (p_ctx->index_fp) {
199 fclose(p_ctx->index_fp);
200 }
201
Pengfei Liub4734232020-01-17 18:25:10 +0800202 if (p_ctx->dat_fp) {
203 fclose(p_ctx->dat_fp);
204 }
hualing chen926a8ec2021-12-20 20:38:24 +0800205 if (p_ctx->all_dat_fp) {
206 fclose(p_ctx->all_dat_fp);
207 }
hualing chen87072a82020-03-12 16:20:12 +0800208 if (p_ctx->ongoing_fp != NULL) {
209 fclose(p_ctx->ongoing_fp);
210 char going_name[MAX_SEGMENT_PATH_SIZE];
211 memset(going_name, 0, sizeof(going_name));
212 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
213 DVR_DEBUG(1, "segment close del [%s]", going_name);
214 unlink(going_name);
215 }
216
Pengfei Liuc181a982020-01-07 19:27:13 +0800217 free(p_ctx);
218 return 0;
219}
220
221ssize_t segment_read(Segment_Handle_t handle, void *buf, size_t count)
222{
223 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800224 ssize_t len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800225 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800226 DVR_RETURN_IF_FALSE(p_ctx);
227 DVR_RETURN_IF_FALSE(buf);
228 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800229 len = read(p_ctx->ts_fd, buf, count);
230 return len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800231}
232
233ssize_t segment_write(Segment_Handle_t handle, void *buf, size_t count)
234{
235 Segment_Context_t *p_ctx;
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800236 ssize_t len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800237 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800238 DVR_RETURN_IF_FALSE(p_ctx);
239 DVR_RETURN_IF_FALSE(buf);
240 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800241 len = write(p_ctx->ts_fd, buf, count);
242 fsync(p_ctx->ts_fd);
243 return len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800244}
245
hualing chen2932d372020-04-29 13:44:00 +0800246int segment_update_pts_force(Segment_Handle_t handle, uint64_t pts, loff_t offset)
Pengfei Liuc181a982020-01-07 19:27:13 +0800247{
248 Segment_Context_t *p_ctx;
249 char buf[256];
pengfei.liu567d6d82020-04-17 16:48:59 +0800250 int record_diff = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800251
252 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800253 DVR_RETURN_IF_FALSE(p_ctx);
254 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800255
Pengfei Liub038b6a2020-01-14 15:57:01 +0800256 if (p_ctx->first_pts == ULLONG_MAX) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800257 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800258 p_ctx->first_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800259 p_ctx->first_offset = offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800260 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800261 memset(buf, 0, sizeof(buf));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800262 if (p_ctx->last_pts == ULLONG_MAX) {
263 /*Last pts is init value*/
hualing chen2aba4022020-03-02 13:49:55 +0800264 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
pengfei.liuab5a2262020-02-14 17:33:40 +0800265 p_ctx->cur_time = pts - p_ctx->first_pts;
hualing chena5f03222021-12-02 11:22:35 +0800266 DVR_DEBUG(1, "%s force pcr:%llu -1", __func__, pts);
hualing chen2932d372020-04-29 13:44:00 +0800267 } else {
268 /*Last pts has valid value*/
269 int diff = pts - p_ctx->last_pts;
270 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
271 /*Current pts has a transition*/
272 DVR_DEBUG(1, "[%s]force update Current pts has a transition, [%llu, %llu, %llu]",__func__,
273 p_ctx->first_pts, p_ctx->last_pts, pts);
274 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
275 } else {
276 /*This is a normal pts, record it*/
hualing chena5f03222021-12-02 11:22:35 +0800277 //check if this pcr is transition.if true,add 200ms
278 //other case normal.
hualing chen2932d372020-04-29 13:44:00 +0800279 p_ctx->cur_time += diff;
280 DVR_DEBUG(1, "%s force pcr:%llu -1 diff [%d]", __func__, pts, diff);
281 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
282 }
283 }
284
285 record_diff = pts - p_ctx->last_record_pts;
286 if (strlen(buf) > 0) {
287 DVR_DEBUG(1, "%s force pcr:%llu buf:%s", __func__, pts, buf);
288 fputs(buf, p_ctx->index_fp);
289 fflush(p_ctx->index_fp);
290 fsync(fileno(p_ctx->index_fp));
291 p_ctx->last_record_pts = pts;
292 }
293 p_ctx->last_pts = pts;
hualing chen2932d372020-04-29 13:44:00 +0800294 return DVR_SUCCESS;
295}
296
297int segment_update_pts(Segment_Handle_t handle, uint64_t pts, loff_t offset)
298{
299 Segment_Context_t *p_ctx;
300 char buf[256];
301 int record_diff = 0;
302
303 p_ctx = (Segment_Context_t *)handle;
304 DVR_RETURN_IF_FALSE(p_ctx);
305 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
306
307 if (p_ctx->first_pts == ULLONG_MAX) {
308 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
309 p_ctx->first_pts = pts;
310 //p_ctx->cur_time = p_ctx->cur_time + PTS_HEAD_DEVIATION;
311 }
312 memset(buf, 0, sizeof(buf));
313 if (p_ctx->last_pts == ULLONG_MAX) {
314 /*Last pts is init value*/
315 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
316 p_ctx->cur_time = pts - p_ctx->first_pts;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800317 } else {
318 /*Last pts has valid value*/
pengfei.liu567d6d82020-04-17 16:48:59 +0800319 int diff = pts - p_ctx->last_pts;
hualing chen4b7c15d2020-04-07 16:13:48 +0800320 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
Pengfei Liub038b6a2020-01-14 15:57:01 +0800321 /*Current pts has a transition*/
pengfei.liuab5a2262020-02-14 17:33:40 +0800322 DVR_DEBUG(1, "Current pts has a transition, [%llu, %llu, %llu]",
323 p_ctx->first_pts, p_ctx->last_pts, pts);
pengfei.liu567d6d82020-04-17 16:48:59 +0800324 p_ctx->last_record_pts = pts;
hualing chen2932d372020-04-29 13:44:00 +0800325 //p_ctx->cur_time = p_ctx->cur_time + PTS_DISCONTINE_DEVIATION;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800326 } else {
327 /*This is a normal pts, record it*/
hualing chena5f03222021-12-02 11:22:35 +0800328 loff_t off_diff = offset - p_ctx->last_offset;
329 float rate = (float) (off_diff) / (float)(diff);
330 if (p_ctx->avg_rate == 0.0) {
331 p_ctx->avg_rate = (float) offset / (float)(p_ctx->cur_time + diff);
332 }
333 if (diff >= 1000) {
334 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));
335 if (p_ctx->avg_rate != 0 && (int)(p_ctx->avg_rate) >= (int)(rate * 4)) {
336 diff = off_diff / p_ctx->avg_rate;
337 p_ctx->cur_time += diff;
338 } else {
339 p_ctx->cur_time += diff;
340 }
341 } else {
342 p_ctx->cur_time += diff;
343 }
hualing chen2aba4022020-03-02 13:49:55 +0800344 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800345 }
346 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800347
348 record_diff = pts - p_ctx->last_record_pts;
349 if (strlen(buf) > 0 &&
350 (record_diff > PCR_RECORD_INTERVAL_MS || p_ctx->last_record_pts == ULLONG_MAX)){
hualing chen4b7c15d2020-04-07 16:13:48 +0800351 fputs(buf, p_ctx->index_fp);
pengfei.liu567d6d82020-04-17 16:48:59 +0800352 fflush(p_ctx->index_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800353 fsync(fileno(p_ctx->index_fp));
pengfei.liu567d6d82020-04-17 16:48:59 +0800354 p_ctx->last_record_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800355 p_ctx->last_record_offset = offset;
356 if (p_ctx->cur_time > 0)
357 p_ctx->avg_rate = (float) offset / (float)p_ctx->cur_time;
pengfei.liu567d6d82020-04-17 16:48:59 +0800358 }
Pengfei Liub038b6a2020-01-14 15:57:01 +0800359 p_ctx->last_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800360 p_ctx->last_offset = offset;
Pengfei Liuc181a982020-01-07 19:27:13 +0800361 return DVR_SUCCESS;
362}
363
hualing chen266b9502020-04-04 17:39:39 +0800364loff_t segment_seek(Segment_Handle_t handle, uint64_t time, int block_size)
Pengfei Liuc181a982020-01-07 19:27:13 +0800365{
366 Segment_Context_t *p_ctx;
367 char buf[256];
368 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800369 uint64_t pts = 0L;
370 loff_t offset = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800371 char *p1, *p2;
372
hualing chen8a657f32021-08-30 13:12:49 +0800373 DVR_DEBUG(1, "into seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
374
Pengfei Liuc181a982020-01-07 19:27:13 +0800375 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800376 DVR_RETURN_IF_FALSE(p_ctx);
377 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
378 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800379
hualing chen266b9502020-04-04 17:39:39 +0800380 if (time == 0) {
381 offset = 0;
hualing chend241c7a2021-06-22 13:34:27 +0800382 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
383 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen266b9502020-04-04 17:39:39 +0800384 return offset;
385 }
386
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800387 memset(buf, 0, sizeof(buf));
388 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chen2aba4022020-03-02 13:49:55 +0800389 int line = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800390 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800391 line++;
Pengfei Liuc181a982020-01-07 19:27:13 +0800392 memset(value, 0, sizeof(value));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800393 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800394 p1 += 5;
Pengfei Liuc181a982020-01-07 19:27:13 +0800395 if ((p2 = strstr(buf, ","))) {
396 memcpy(value, p1, p2 - p1);
397 }
398 pts = strtoull(value, NULL, 10);
399 }
400
401 memset(value, 0, sizeof(value));
402 if ((p1 = strstr(buf, "offset="))) {
403 p1 += 7;
404 if ((p2 = strstr(buf, "}"))) {
405 memcpy(value, p1, p2 - p1);
406 }
407 offset = strtoull(value, NULL, 10);
408 }
hualing chen2aba4022020-03-02 13:49:55 +0800409 if (0)
410 {
411 DVR_DEBUG(1, "seek buf[%s]", buf);
412 DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
413 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800414 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800415 if (time <= pts) {
hualing chen266b9502020-04-04 17:39:39 +0800416 if (block_size > 0) {
417 offset = offset - offset%block_size;
418 }
hualing chend241c7a2021-06-22 13:34:27 +0800419 //DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line);
420 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800421 return offset;
422 }
hualing chen2aba4022020-03-02 13:49:55 +0800423 }
hualing chen2932d372020-04-29 13:44:00 +0800424 if (time > pts) {
425 if (block_size > 0) {
426 offset = offset - offset%block_size;
427 }
428 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 +0800429 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen2932d372020-04-29 13:44:00 +0800430 return offset;
431 }
hualing chen2aba4022020-03-02 13:49:55 +0800432 DVR_DEBUG(1, "seek error line [%d]", line);
Pengfei Liuc181a982020-01-07 19:27:13 +0800433 return DVR_FAILURE;
434}
435
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800436loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800437{
438 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800439 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800440 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800441 DVR_RETURN_IF_FALSE(p_ctx);
442 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800443 pos = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
444 return pos;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800445}
446
hualing chen5605eed2020-05-26 18:18:06 +0800447uint64_t segment_tell_position_time(Segment_Handle_t handle, loff_t position)
448{
449 Segment_Context_t *p_ctx;
450 char buf[256];
451 char value[256];
hualing chen969fe7b2021-05-26 15:13:17 +0800452 uint64_t ret = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800453 uint64_t pts = 0L;
hualing chen969fe7b2021-05-26 15:13:17 +0800454 uint64_t pts_p = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800455 loff_t offset = 0;
hualing chen969fe7b2021-05-26 15:13:17 +0800456 loff_t offset_p = 0;
hualing chen5605eed2020-05-26 18:18:06 +0800457 char *p1, *p2;
458
459 p_ctx = (Segment_Context_t *)handle;
460 DVR_RETURN_IF_FALSE(p_ctx);
461 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
462 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
463
464 memset(buf, 0, sizeof(buf));
465 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
466 DVR_RETURN_IF_FALSE(position != -1);
467
468 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
469 memset(value, 0, sizeof(value));
470 if ((p1 = strstr(buf, "time="))) {
471 p1 += 5;
472 if ((p2 = strstr(buf, ","))) {
473 memcpy(value, p1, p2 - p1);
474 }
475 pts = strtoull(value, NULL, 10);
476 }
477
478 memset(value, 0, sizeof(value));
479 if ((p1 = strstr(buf, "offset="))) {
480 p1 += 7;
481 if ((p2 = strstr(buf, "}"))) {
482 memcpy(value, p1, p2 - p1);
483 }
484 offset = strtoull(value, NULL, 10);
485 }
486
487 memset(buf, 0, sizeof(buf));
488 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chend241c7a2021-06-22 13:34:27 +0800489 if (position <= offset
490 &&position >= offset_p
491 && offset - offset_p > 0) {
hualing chen969fe7b2021-05-26 15:13:17 +0800492 ret = pts_p + (pts - pts_p) * (position - offset_p) / (offset - offset_p);
hualing chend241c7a2021-06-22 13:34:27 +0800493 //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 +0800494 return ret;
hualing chen5605eed2020-05-26 18:18:06 +0800495 }
hualing chen969fe7b2021-05-26 15:13:17 +0800496 offset_p = offset;
497 pts_p = pts;
hualing chen5605eed2020-05-26 18:18:06 +0800498 }
499 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
500 return pts;
501}
502
503
pengfei.liu8b563292020-02-26 15:49:02 +0800504uint64_t segment_tell_current_time(Segment_Handle_t handle)
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800505{
506 Segment_Context_t *p_ctx;
507 char buf[256];
508 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800509 uint64_t pts = 0L;
510 loff_t offset = 0, position = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800511 char *p1, *p2;
512
513 p_ctx = (Segment_Context_t *)handle;
514 DVR_RETURN_IF_FALSE(p_ctx);
515 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
516 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
517
518 memset(buf, 0, sizeof(buf));
519 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800520 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800521 DVR_RETURN_IF_FALSE(position != -1);
522
523 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
524 memset(value, 0, sizeof(value));
525 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800526 p1 += 5;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800527 if ((p2 = strstr(buf, ","))) {
528 memcpy(value, p1, p2 - p1);
529 }
530 pts = strtoull(value, NULL, 10);
531 }
532
533 memset(value, 0, sizeof(value));
534 if ((p1 = strstr(buf, "offset="))) {
535 p1 += 7;
536 if ((p2 = strstr(buf, "}"))) {
537 memcpy(value, p1, p2 - p1);
538 }
539 offset = strtoull(value, NULL, 10);
540 }
541
542 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800543 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800544 if (position <= offset) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800545 return pts;
546 }
547 }
hualing chena540a7e2020-03-27 16:44:05 +0800548 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800549 return pts;
Pengfei Liuc181a982020-01-07 19:27:13 +0800550}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800551
pengfei.liu8b563292020-02-26 15:49:02 +0800552uint64_t segment_tell_total_time(Segment_Handle_t handle)
553{
554 Segment_Context_t *p_ctx;
555 char buf[256];
556 char last_buf[256];
557 char value[256];
558 uint64_t pts = ULLONG_MAX;
559 loff_t offset = 0, position = 0;
560 char *p1, *p2;
561 int line = 0;
562
563 p_ctx = (Segment_Context_t *)handle;
564 DVR_RETURN_IF_FALSE(p_ctx);
565 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
566 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
567
568 memset(buf, 0, sizeof(buf));
569 memset(last_buf, 0, sizeof(last_buf));
hualing chend241c7a2021-06-22 13:34:27 +0800570 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
pengfei.liu8b563292020-02-26 15:49:02 +0800571 DVR_RETURN_IF_FALSE(position != -1);
572
hualing chen041c4092020-04-05 15:11:50 +0800573 //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1);
574 //if seek error.we need seek 0 pos.
575 if (fseek(p_ctx->index_fp, -1000L, SEEK_END) == -1) {
576 fseek(p_ctx->index_fp, 0L, SEEK_SET);
577 }
pengfei.liu8b563292020-02-26 15:49:02 +0800578 /* Save last line buffer */
579 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800580 if (strlen(buf) <= 0) {
hualing chen87072a82020-03-12 16:20:12 +0800581 DVR_DEBUG(1, "read index buf is len 0");
hualing chen2aba4022020-03-02 13:49:55 +0800582 continue;
583 }
pengfei.liu8b563292020-02-26 15:49:02 +0800584 memset(last_buf, 0, sizeof(last_buf));
585 memcpy(last_buf, buf, strlen(buf));
586 memset(buf, 0, sizeof(buf));
587 line++;
588 }
589
590 /* Extract time value */
591 memset(value, 0, sizeof(value));
592 if ((p1 = strstr(last_buf, "time="))) {
593 p1 += 5;
594 if ((p2 = strstr(last_buf, ","))) {
595 memcpy(value, p1, p2 - p1);
596 }
597 pts = strtoull(value, NULL, 10);
598 }
599
600 memset(value, 0, sizeof(value));
601 if ((p1 = strstr(last_buf, "offset="))) {
602 p1 += 7;
603 if ((p2 = strstr(last_buf, "}"))) {
604 memcpy(value, p1, p2 - p1);
605 }
606 offset = strtoull(value, NULL, 10);
607 }
hualing chen87072a82020-03-12 16:20:12 +0800608 //if (line < 2)
609 //DVR_DEBUG(1, "totle time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line);
pengfei.liu8b563292020-02-26 15:49:02 +0800610 return (pts == ULLONG_MAX ? DVR_FAILURE : pts);
611}
612
pengfei.liuab5a2262020-02-14 17:33:40 +0800613/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800614int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
615{
616 Segment_Context_t *p_ctx;
617 char buf[256];
618 uint32_t i;
619
620 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800621 DVR_RETURN_IF_FALSE(p_ctx);
622 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
623 DVR_RETURN_IF_FALSE(p_info);
hualing chen87072a82020-03-12 16:20:12 +0800624 //seek 0, rewrite info
625 DVR_RETURN_IF_FALSE(fseek(p_ctx->dat_fp, 0, SEEK_SET) != -1);
Pengfei Liub4734232020-01-17 18:25:10 +0800626
627 /*Save segment id*/
628 memset(buf, 0, sizeof(buf));
629 sprintf(buf, "id=%lld\n", p_info->id);
630 fputs(buf, p_ctx->dat_fp);
631
632 /*Save number of pids*/
633 memset(buf, 0, sizeof(buf));
634 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
635 fputs(buf, p_ctx->dat_fp);
636
637 /*Save pid information*/
638 for (i = 0; i < p_info->nb_pids; i++) {
639 memset(buf, 0, sizeof(buf));
640 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
641 fputs(buf, p_ctx->dat_fp);
642 }
643
644 /*Save segment duration*/
645 memset(buf, 0, sizeof(buf));
hualing chen87072a82020-03-12 16:20:12 +0800646 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800647 sprintf(buf, "duration=%ld\n", p_info->duration);
648 fputs(buf, p_ctx->dat_fp);
649
650 /*Save segment size*/
651 memset(buf, 0, sizeof(buf));
652 sprintf(buf, "size=%zu\n", p_info->size);
653 fputs(buf, p_ctx->dat_fp);
654
655 /*Save number of packets*/
656 memset(buf, 0, sizeof(buf));
657 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
658 fputs(buf, p_ctx->dat_fp);
659
660 fflush(p_ctx->dat_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800661 fsync(fileno(p_ctx->dat_fp));
Pengfei Liub4734232020-01-17 18:25:10 +0800662 return DVR_SUCCESS;
663}
664
pengfei.liuab5a2262020-02-14 17:33:40 +0800665/* Should consider the case of cut power, todo... */
hualing chenb9a02922021-12-14 11:29:47 +0800666int segment_store_allInfo(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
667{
668 Segment_Context_t *p_ctx;
669 char buf[256];
670 uint32_t i;
671
672 p_ctx = (Segment_Context_t *)handle;
673 DVR_RETURN_IF_FALSE(p_ctx);
674 DVR_RETURN_IF_FALSE(p_ctx->all_dat_fp);
675 DVR_RETURN_IF_FALSE(p_info);
676 //seek 0, rewrite info
677 DVR_RETURN_IF_FALSE(fseek(p_ctx->all_dat_fp, 0, SEEK_END) != -1);
678
679 /*Save segment id*/
680 memset(buf, 0, sizeof(buf));
681 sprintf(buf, "id=%lld\n", p_info->id);
682 fputs(buf, p_ctx->all_dat_fp);
683
684 /*Save number of pids*/
685 memset(buf, 0, sizeof(buf));
686 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
687 fputs(buf, p_ctx->all_dat_fp);
688
689 /*Save pid information*/
690 for (i = 0; i < p_info->nb_pids; i++) {
691 memset(buf, 0, sizeof(buf));
692 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
693 fputs(buf, p_ctx->all_dat_fp);
694 }
695
696 /*Save segment duration*/
697 memset(buf, 0, sizeof(buf));
698 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
699 sprintf(buf, "duration=%ld\n", p_info->duration);
700 fputs(buf, p_ctx->all_dat_fp);
701
702 /*Save segment size*/
703 memset(buf, 0, sizeof(buf));
704 sprintf(buf, "size=%zu\n", p_info->size);
705 fputs(buf, p_ctx->all_dat_fp);
706
707 /*Save number of packets*/
708 memset(buf, 0, sizeof(buf));
709 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
710 fputs(buf, p_ctx->all_dat_fp);
711
712 fflush(p_ctx->all_dat_fp);
713 fsync(fileno(p_ctx->all_dat_fp));
714 return DVR_SUCCESS;
715}
716
717/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800718int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
719{
720 Segment_Context_t *p_ctx;
721 uint32_t i;
722 char buf[256];
723 char value[256];
724 char *p1, *p2;
725
726 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800727 DVR_RETURN_IF_FALSE(p_ctx);
728 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800729
730 /*Load segment id*/
731 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800732 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800733 p1 = strstr(buf, "id=");
hualing chen2aba4022020-03-02 13:49:55 +0800734 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800735 p_info->id = strtoull(p1 + 3, NULL, 10);
736
737 /*Save number of pids*/
738 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800739 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800740 p1 = strstr(buf, "nb_pids=");
hualing chen2aba4022020-03-02 13:49:55 +0800741 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800742 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
743
744 /*Save pid information*/
745 for (i = 0; i < p_info->nb_pids; i++) {
746 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800747 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800748 memset(value, 0, sizeof(value));
749 if ((p1 = strstr(buf, "pid="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800750 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800751 p1 += 4;
752 if ((p2 = strstr(buf, ","))) {
hualing chen2aba4022020-03-02 13:49:55 +0800753 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800754 memcpy(value, p1, p2 - p1);
755 }
756 p_info->pids[i].pid = strtoull(value, NULL, 10);
757 }
758
759 memset(value, 0, sizeof(value));
760 if ((p1 = strstr(buf, "type="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800761 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800762 p1 += 5;
763 if ((p2 = strstr(buf, "}"))) {
hualing chen2aba4022020-03-02 13:49:55 +0800764 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800765 memcpy(value, p1, p2 - p1);
766 }
767 p_info->pids[i].type = strtoull(value, NULL, 10);
768 }
769 }
770
771 /*Save segment duration*/
772 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800773 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800774 p1 = strstr(buf, "duration=");
hualing chen2aba4022020-03-02 13:49:55 +0800775 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800776 p_info->duration = strtoull(p1 + 9, NULL, 10);
hualing chena540a7e2020-03-27 16:44:05 +0800777 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800778
779 /*Save segment size*/
780 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800781 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800782 p1 = strstr(buf, "size=");
hualing chen2aba4022020-03-02 13:49:55 +0800783 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800784 p_info->size = strtoull(p1 + 5, NULL, 10);
785
786 /*Save number of packets*/
787 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800788 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800789 p1 = strstr(buf, "nb_packets=");
hualing chen2aba4022020-03-02 13:49:55 +0800790 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800791 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
792
793 return DVR_SUCCESS;
794}
795
hualing chenb9a02922021-12-14 11:29:47 +0800796/* Should consider the case of cut power, todo... */
797int segment_load_allInfo(Segment_Handle_t handle, struct list_head *list)
798{
799 Segment_Context_t *p_ctx;
800 uint32_t i;
801 char buf[256];
802 char value[256];
803 char *p1, *p2;
804
805 p_ctx = (Segment_Context_t *)handle;
806 DVR_RETURN_IF_FALSE(p_ctx);
807 DVR_RETURN_IF_FALSE(list);
hualing chen926a8ec2021-12-20 20:38:24 +0800808 if (p_ctx->all_dat_fp == NULL) {
809 DVR_DEBUG(1, "all dat file not open\n");
810 return DVR_FAILURE;
811 }
hualing chenb9a02922021-12-14 11:29:47 +0800812 //first get
813 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
814 DVR_RETURN_IF_FALSE(p1);
815
816 do {
817
818 DVR_RecordSegmentInfo_t *p_info;
819
820 p_info = malloc(sizeof(DVR_RecordSegmentInfo_t));
821 memset(p_info, 0, sizeof(DVR_RecordSegmentInfo_t));
822
823 list_add_tail(&p_info->head, list);
824
825 /*Load segment id*/
826 DVR_RETURN_IF_FALSE(p1);
827 p1 = strstr(buf, "id=");
828 DVR_RETURN_IF_FALSE(p1);
829 p_info->id = strtoull(p1 + 3, NULL, 10);
830
831 /*Save number of pids*/
832 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
833 DVR_RETURN_IF_FALSE(p1);
834 p1 = strstr(buf, "nb_pids=");
835 DVR_RETURN_IF_FALSE(p1);
836 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
837
838 /*Save pid information*/
839 for (i = 0; i < p_info->nb_pids; i++) {
840 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
841 DVR_RETURN_IF_FALSE(p1);
842 memset(value, 0, sizeof(value));
843 if ((p1 = strstr(buf, "pid="))) {
844 DVR_RETURN_IF_FALSE(p1);
845 p1 += 4;
846 if ((p2 = strstr(buf, ","))) {
847 DVR_RETURN_IF_FALSE(p2);
848 memcpy(value, p1, p2 - p1);
849 }
850 p_info->pids[i].pid = strtoull(value, NULL, 10);
851 }
852
853 memset(value, 0, sizeof(value));
854 if ((p1 = strstr(buf, "type="))) {
855 DVR_RETURN_IF_FALSE(p1);
856 p1 += 5;
857 if ((p2 = strstr(buf, "}"))) {
858 DVR_RETURN_IF_FALSE(p2);
859 memcpy(value, p1, p2 - p1);
860 }
861 p_info->pids[i].type = strtoull(value, NULL, 10);
862 }
863 }
864
865 /*Save segment duration*/
866 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
867 DVR_RETURN_IF_FALSE(p1);
868 p1 = strstr(buf, "duration=");
869 DVR_RETURN_IF_FALSE(p1);
870 p_info->duration = strtoull(p1 + 9, NULL, 10);
871 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
872
873 /*Save segment size*/
874 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
875 DVR_RETURN_IF_FALSE(p1);
876 p1 = strstr(buf, "size=");
877 DVR_RETURN_IF_FALSE(p1);
878 p_info->size = strtoull(p1 + 5, NULL, 10);
879
880 /*Save number of packets*/
881 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
882 DVR_RETURN_IF_FALSE(p1);
883 p1 = strstr(buf, "nb_packets=");
884 DVR_RETURN_IF_FALSE(p1);
885 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
886 //if reach end,exit loop
887 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
888 } while (p1);
889
890 return DVR_SUCCESS;
891}
892
Pengfei Liub4734232020-01-17 18:25:10 +0800893int segment_delete(const char *location, uint64_t segment_id)
894{
895 char fname[MAX_SEGMENT_PATH_SIZE];
896 int ret;
897
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800898 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800899
900 /*delete ts file*/
901 memset(fname, 0, sizeof(fname));
902 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
903 ret = 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 index file*/
908 memset(fname, 0, sizeof(fname));
909 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
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 /*delete store information file*/
915 memset(fname, 0, sizeof(fname));
916 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
917 unlink(fname);
918 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800919 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800920
921 return DVR_SUCCESS;
922}
923
hualing chen87072a82020-03-12 16:20:12 +0800924int segment_ongoing(Segment_Handle_t handle)
925{
926 Segment_Context_t *p_ctx;
927 p_ctx = (Segment_Context_t *)handle;
928 struct stat mstat;
929
930 char going_name[MAX_SEGMENT_PATH_SIZE];
931 memset(going_name, 0, sizeof(going_name));
932 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
933 int ret = stat(going_name, &mstat);
934 DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret);
935 if (ret != 0) {
936 return DVR_FAILURE;
937 }
938 return DVR_SUCCESS;
939}
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800940loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800941{
942 Segment_Context_t *p_ctx;
943 char buf[256];
944 char value[256];
945 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800946 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800947 char *p1, *p2;
948
949 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800950 DVR_RETURN_IF_FALSE(p_ctx);
951 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
952 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800953
954 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800955 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800956 printf("start gets pts\n");
957 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
958 printf("buf[%s]\n", buf);
959 memset(value, 0, sizeof(value));
960 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800961 p1 += 5;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800962 if ((p2 = strstr(buf, ","))) {
963 memcpy(value, p1, p2 - p1);
964 }
965 pts = strtoull(value, NULL, 10);
966 }
967
968 memset(value, 0, sizeof(value));
969 if ((p1 = strstr(buf, "offset="))) {
970 p1 += 7;
971 if ((p2 = strstr(buf, "}"))) {
972 memcpy(value, p1, p2 - p1);
973 }
974 offset = strtoull(value, NULL, 10);
975 }
976
977 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800978 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800979 }
980
981 return 0;
982}