blob: 58866f70ba679d8ad06337b69e7deec8d2a031c0 [file] [log] [blame]
Pengfei Liuc181a982020-01-07 19:27:13 +08001#include <stdio.h>
Pengfei Liub038b6a2020-01-14 15:57:01 +08002#include <unistd.h>
Pengfei Liuc181a982020-01-07 19:27:13 +08003#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <string.h>
Pengfei Liub038b6a2020-01-14 15:57:01 +08007#include <stdlib.h>
Pengfei Liuc181a982020-01-07 19:27:13 +08008#include <errno.h>
Pengfei Liu47ed6c92020-01-17 11:23:41 +08009#include "dvr_types.h"
Pengfei Liuc181a982020-01-07 19:27:13 +080010#include "segment.h"
11
12#define MAX_SEGMENT_FD_COUNT (128)
13#define MAX_SEGMENT_PATH_SIZE (DVR_MAX_LOCATION_SIZE + 32)
Pengfei Liub038b6a2020-01-14 15:57:01 +080014#define MAX_PTS_THRESHOLD (10*1000)
hualing chen06209692020-11-05 14:51:46 +080015#define PCR_RECORD_INTERVAL_MS (300)
hualing chen2932d372020-04-29 13:44:00 +080016#define PTS_DISCONTINE_DEVIATION (40)
17#define PTS_HEAD_DEVIATION (40)
hualing chen86b8e602021-12-23 11:04:19 +080018#define PCR_JUMP_DUR (5000)
hualing chen2932d372020-04-29 13:44:00 +080019
20
Pengfei Liub4734232020-01-17 18:25:10 +080021/**\brief Segment context*/
Pengfei Liuc181a982020-01-07 19:27:13 +080022typedef struct {
Pengfei Liu3b1a8202020-02-12 23:04:21 +080023 int ts_fd; /**< Segment ts file fd*/
Pengfei Liub4734232020-01-17 18:25:10 +080024 FILE *index_fp; /**< Time index file fd*/
25 FILE *dat_fp; /**< Information file fd*/
hualing chenb9a02922021-12-14 11:29:47 +080026 FILE *all_dat_fp; /**< Information file fd*/
pengfei.liu567d6d82020-04-17 16:48:59 +080027 FILE *ongoing_fp; /**< Ongoing file fd, used to verify timedhift mode*/
Pengfei Liub4734232020-01-17 18:25:10 +080028 uint64_t first_pts; /**< First pts value, use for write mode*/
pengfei.liu567d6d82020-04-17 16:48:59 +080029 uint64_t last_pts; /**< Last input pts value, use for write mode*/
30 uint64_t last_record_pts; /**< Last record pts value, use for write mode*/
pengfei.liuab5a2262020-02-14 17:33:40 +080031 uint64_t cur_time; /**< Current time save in index file */
pengfei.liu567d6d82020-04-17 16:48:59 +080032 uint64_t segment_id; /**< Current segment ID */
33 char location[MAX_SEGMENT_PATH_SIZE]; /**< Current time save in index file */
hualing chena5f03222021-12-02 11:22:35 +080034 loff_t first_offset;
35 loff_t last_offset;
36 loff_t last_record_offset;
37 float avg_rate;
Pengfei Liuc181a982020-01-07 19:27:13 +080038} Segment_Context_t;
39
Pengfei Liub4734232020-01-17 18:25:10 +080040/**\brief Segment file type*/
Pengfei Liuc181a982020-01-07 19:27:13 +080041typedef enum {
Pengfei Liub4734232020-01-17 18:25:10 +080042 SEGMENT_FILE_TYPE_TS, /**< Used for store TS data*/
43 SEGMENT_FILE_TYPE_INDEX, /**< Used for store index data*/
44 SEGMENT_FILE_TYPE_DAT, /**< Used for store information data, such as duration etc*/
pengfei.liu567d6d82020-04-17 16:48:59 +080045 SEGMENT_FILE_TYPE_ONGOING, /**< Used for store information data, such as duration etc*/
hualing chenb9a02922021-12-14 11:29:47 +080046 SEGMENT_FILE_TYPE_ALLDAT, /**< Used for store all information data*/
Pengfei Liuc181a982020-01-07 19:27:13 +080047} Segment_FileType_t;
48
49static void segment_get_fname(char fname[MAX_SEGMENT_PATH_SIZE],
Pengfei Liub4734232020-01-17 18:25:10 +080050 const char location[DVR_MAX_LOCATION_SIZE],
Pengfei Liuc181a982020-01-07 19:27:13 +080051 uint64_t segment_id,
52 Segment_FileType_t type)
53{
54 int offset;
55
56 memset(fname, 0, MAX_SEGMENT_PATH_SIZE);
57 strncpy(fname, location, strlen(location));
58 offset = strlen(location);
hualing chenb9a02922021-12-14 11:29:47 +080059 if (type != SEGMENT_FILE_TYPE_ALLDAT) {
60 strncpy(fname + offset, "-", 1);
61 offset += 1;
62 sprintf(fname + offset, "%04llu", segment_id);
63 offset += 4;
64 }
65
Pengfei Liuc181a982020-01-07 19:27:13 +080066 if (type == SEGMENT_FILE_TYPE_TS)
67 strncpy(fname + offset, ".ts", 3);
68 else if (type == SEGMENT_FILE_TYPE_INDEX)
69 strncpy(fname + offset, ".idx", 4);
Pengfei Liub4734232020-01-17 18:25:10 +080070 else if (type == SEGMENT_FILE_TYPE_DAT)
71 strncpy(fname + offset, ".dat", 4);
hualing chen87072a82020-03-12 16:20:12 +080072 else if (type == SEGMENT_FILE_TYPE_ONGOING)
73 strncpy(fname + offset, ".going", 6);
hualing chenb9a02922021-12-14 11:29:47 +080074 else if (type == SEGMENT_FILE_TYPE_ALLDAT)
75 strncpy(fname + offset, ".dat", 4);
76
Pengfei Liuc181a982020-01-07 19:27:13 +080077}
78
Pengfei Liu3b1a8202020-02-12 23:04:21 +080079static void segment_get_dirname(char dir_name[MAX_SEGMENT_PATH_SIZE],
80 const char location[DVR_MAX_LOCATION_SIZE])
81{
82 char *p;
83 int i;
84 int found = 0;
85
86 for (i = 0; i < (int)strlen(location); i++) {
87 if (location[i] == '/') {
88 p = (char *)location + i;
89 found = 1;
90 }
91 }
92 if (found)
93 memcpy(dir_name, location, p - location);
94}
95
Pengfei Liuc181a982020-01-07 19:27:13 +080096int segment_open(Segment_OpenParams_t *params, Segment_Handle_t *p_handle)
97{
98 Segment_Context_t *p_ctx;
99 char ts_fname[MAX_SEGMENT_PATH_SIZE];
100 char index_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liub4734232020-01-17 18:25:10 +0800101 char dat_fname[MAX_SEGMENT_PATH_SIZE];
hualing chenb9a02922021-12-14 11:29:47 +0800102 char all_dat_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800103 char dir_name[MAX_SEGMENT_PATH_SIZE];
hualing chen87072a82020-03-12 16:20:12 +0800104 char going_name[MAX_SEGMENT_PATH_SIZE];
Pengfei Liuc181a982020-01-07 19:27:13 +0800105
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800106 DVR_RETURN_IF_FALSE(params);
107 DVR_RETURN_IF_FALSE(p_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +0800108
hualing chena540a7e2020-03-27 16:44:05 +0800109 //DVR_DEBUG(1, "%s, location:%s, id:%llu", __func__, params->location, params->segment_id);
Pengfei Liuc181a982020-01-07 19:27:13 +0800110
Pengfei Liub038b6a2020-01-14 15:57:01 +0800111 p_ctx = (void*)malloc(sizeof(Segment_Context_t));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800112 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +0800113 memset(p_ctx, 0, sizeof(Segment_Context_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800114
115 memset(ts_fname, 0, sizeof(ts_fname));
116 segment_get_fname(ts_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_TS);
117
118 memset(index_fname, 0, sizeof(index_fname));
119 segment_get_fname(index_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_INDEX);
120
Pengfei Liub4734232020-01-17 18:25:10 +0800121 memset(dat_fname, 0, sizeof(dat_fname));
122 segment_get_fname(dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_DAT);
123
hualing chenb9a02922021-12-14 11:29:47 +0800124 memset(all_dat_fname, 0, sizeof(all_dat_fname));
125 segment_get_fname(all_dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_ALLDAT);
126
127
hualing chen87072a82020-03-12 16:20:12 +0800128 memset(going_name, 0, sizeof(going_name));
129 segment_get_fname(going_name, params->location, params->segment_id, SEGMENT_FILE_TYPE_ONGOING);
130
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800131 memset(dir_name, 0, sizeof(dir_name));
132 segment_get_dirname(dir_name, params->location);
133 if (access(dir_name, F_OK) == -1) {
134 DVR_DEBUG(1, "%s dir %s is not exist, create it", __func__, dir_name);
135 mkdir(dir_name, 0666);
136 }
137
Pengfei Liuc181a982020-01-07 19:27:13 +0800138 if (params->mode == SEGMENT_MODE_READ) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800139 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800140 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800141 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800142 p_ctx->ongoing_fp = NULL;
hualing chenb9a02922021-12-14 11:29:47 +0800143 p_ctx->all_dat_fp = fopen(all_dat_fname, "r");
Pengfei Liuc181a982020-01-07 19:27:13 +0800144 } else if (params->mode == SEGMENT_MODE_WRITE) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800145 p_ctx->ts_fd = open(ts_fname, O_CREAT | O_RDWR | O_TRUNC, 0644);
Pengfei Liuc181a982020-01-07 19:27:13 +0800146 p_ctx->index_fp = fopen(index_fname, "w+");
Pengfei Liub4734232020-01-17 18:25:10 +0800147 p_ctx->dat_fp = fopen(dat_fname, "w+");
hualing chenb9a02922021-12-14 11:29:47 +0800148 p_ctx->all_dat_fp = fopen(all_dat_fname, "a+");
hualing chen87072a82020-03-12 16:20:12 +0800149 p_ctx->ongoing_fp = fopen(going_name, "w+");
Pengfei Liub038b6a2020-01-14 15:57:01 +0800150 p_ctx->first_pts = ULLONG_MAX;
151 p_ctx->last_pts = ULLONG_MAX;
pengfei.liu567d6d82020-04-17 16:48:59 +0800152 p_ctx->last_record_pts = ULLONG_MAX;
hualing chena5f03222021-12-02 11:22:35 +0800153 p_ctx->avg_rate = 0.0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800154 } else {
155 DVR_DEBUG(1, "%s, unknow mode use default", __func__);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800156 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800157 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800158 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chenb9a02922021-12-14 11:29:47 +0800159 p_ctx->all_dat_fp = fopen(all_dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800160 p_ctx->ongoing_fp = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800161 }
162
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800163 if (p_ctx->ts_fd == -1 || !p_ctx->index_fp || !p_ctx->dat_fp) {
Pengfei Liub4734232020-01-17 18:25:10 +0800164 DVR_DEBUG(1, "%s open file failed [%s, %s, %s], reason:%s", __func__,
165 ts_fname, index_fname, dat_fname, strerror(errno));
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800166 if (p_ctx->ts_fd != -1)
167 close(p_ctx->ts_fd);
168 if (p_ctx->index_fp)
169 fclose(p_ctx->index_fp);
170 if (p_ctx->dat_fp)
171 fclose(p_ctx->dat_fp);
hualing chen926a8ec2021-12-20 20:38:24 +0800172 if (p_ctx->all_dat_fp)
173 fclose(p_ctx->all_dat_fp);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800174 if (p_ctx->ongoing_fp)
175 fclose(p_ctx->ongoing_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800176 free(p_ctx);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800177 *p_handle = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800178 return DVR_FAILURE;
179 }
hualing chen87072a82020-03-12 16:20:12 +0800180 p_ctx->segment_id = params->segment_id;
181 strncpy(p_ctx->location, params->location, strlen(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800182
hualing chen7a56cba2020-04-14 14:09:27 +0800183 //DVR_DEBUG(1, "%s, open file success p_ctx->location [%s]", __func__, p_ctx->location, params->mode);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800184 *p_handle = (Segment_Handle_t)p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800185 return DVR_SUCCESS;
186}
187
188int segment_close(Segment_Handle_t handle)
189{
190 Segment_Context_t *p_ctx;
191
Pengfei Liub038b6a2020-01-14 15:57:01 +0800192 p_ctx = (void *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800193 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800194
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800195 if (p_ctx->ts_fd != -1) {
196 close(p_ctx->ts_fd);
Pengfei Liuc181a982020-01-07 19:27:13 +0800197 }
198
199 if (p_ctx->index_fp) {
200 fclose(p_ctx->index_fp);
201 }
202
Pengfei Liub4734232020-01-17 18:25:10 +0800203 if (p_ctx->dat_fp) {
204 fclose(p_ctx->dat_fp);
205 }
hualing chen926a8ec2021-12-20 20:38:24 +0800206 if (p_ctx->all_dat_fp) {
207 fclose(p_ctx->all_dat_fp);
208 }
hualing chen87072a82020-03-12 16:20:12 +0800209 if (p_ctx->ongoing_fp != NULL) {
210 fclose(p_ctx->ongoing_fp);
211 char going_name[MAX_SEGMENT_PATH_SIZE];
212 memset(going_name, 0, sizeof(going_name));
213 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
214 DVR_DEBUG(1, "segment close del [%s]", going_name);
215 unlink(going_name);
216 }
217
Pengfei Liuc181a982020-01-07 19:27:13 +0800218 free(p_ctx);
219 return 0;
220}
221
222ssize_t segment_read(Segment_Handle_t handle, void *buf, size_t count)
223{
224 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800225 ssize_t len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800226 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800227 DVR_RETURN_IF_FALSE(p_ctx);
228 DVR_RETURN_IF_FALSE(buf);
229 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800230 len = read(p_ctx->ts_fd, buf, count);
231 return len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800232}
233
234ssize_t segment_write(Segment_Handle_t handle, void *buf, size_t count)
235{
236 Segment_Context_t *p_ctx;
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800237 ssize_t len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800238 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800239 DVR_RETURN_IF_FALSE(p_ctx);
240 DVR_RETURN_IF_FALSE(buf);
241 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800242 len = write(p_ctx->ts_fd, buf, count);
243 fsync(p_ctx->ts_fd);
244 return len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800245}
246
hualing chen2932d372020-04-29 13:44:00 +0800247int segment_update_pts_force(Segment_Handle_t handle, uint64_t pts, loff_t offset)
Pengfei Liuc181a982020-01-07 19:27:13 +0800248{
249 Segment_Context_t *p_ctx;
250 char buf[256];
pengfei.liu567d6d82020-04-17 16:48:59 +0800251 int record_diff = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800252
253 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800254 DVR_RETURN_IF_FALSE(p_ctx);
255 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800256
Pengfei Liub038b6a2020-01-14 15:57:01 +0800257 if (p_ctx->first_pts == ULLONG_MAX) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800258 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800259 p_ctx->first_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800260 p_ctx->first_offset = offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800261 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800262 memset(buf, 0, sizeof(buf));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800263 if (p_ctx->last_pts == ULLONG_MAX) {
264 /*Last pts is init value*/
hualing chen2aba4022020-03-02 13:49:55 +0800265 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
pengfei.liuab5a2262020-02-14 17:33:40 +0800266 p_ctx->cur_time = pts - p_ctx->first_pts;
hualing chena5f03222021-12-02 11:22:35 +0800267 DVR_DEBUG(1, "%s force pcr:%llu -1", __func__, pts);
hualing chen2932d372020-04-29 13:44:00 +0800268 } else {
269 /*Last pts has valid value*/
270 int diff = pts - p_ctx->last_pts;
271 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
272 /*Current pts has a transition*/
273 DVR_DEBUG(1, "[%s]force update Current pts has a transition, [%llu, %llu, %llu]",__func__,
274 p_ctx->first_pts, p_ctx->last_pts, pts);
275 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
276 } else {
277 /*This is a normal pts, record it*/
hualing chena5f03222021-12-02 11:22:35 +0800278 //check if this pcr is transition.if true,add 200ms
279 //other case normal.
hualing chen2932d372020-04-29 13:44:00 +0800280 p_ctx->cur_time += diff;
281 DVR_DEBUG(1, "%s force pcr:%llu -1 diff [%d]", __func__, pts, diff);
282 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
283 }
284 }
285
286 record_diff = pts - p_ctx->last_record_pts;
287 if (strlen(buf) > 0) {
288 DVR_DEBUG(1, "%s force pcr:%llu buf:%s", __func__, pts, buf);
289 fputs(buf, p_ctx->index_fp);
290 fflush(p_ctx->index_fp);
291 fsync(fileno(p_ctx->index_fp));
292 p_ctx->last_record_pts = pts;
293 }
294 p_ctx->last_pts = pts;
hualing chen2932d372020-04-29 13:44:00 +0800295 return DVR_SUCCESS;
296}
297
298int segment_update_pts(Segment_Handle_t handle, uint64_t pts, loff_t offset)
299{
300 Segment_Context_t *p_ctx;
301 char buf[256];
302 int record_diff = 0;
303
304 p_ctx = (Segment_Context_t *)handle;
305 DVR_RETURN_IF_FALSE(p_ctx);
306 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
307
308 if (p_ctx->first_pts == ULLONG_MAX) {
309 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
310 p_ctx->first_pts = pts;
311 //p_ctx->cur_time = p_ctx->cur_time + PTS_HEAD_DEVIATION;
312 }
313 memset(buf, 0, sizeof(buf));
314 if (p_ctx->last_pts == ULLONG_MAX) {
315 /*Last pts is init value*/
316 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
317 p_ctx->cur_time = pts - p_ctx->first_pts;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800318 } else {
319 /*Last pts has valid value*/
pengfei.liu567d6d82020-04-17 16:48:59 +0800320 int diff = pts - p_ctx->last_pts;
hualing chen4b7c15d2020-04-07 16:13:48 +0800321 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
Pengfei Liub038b6a2020-01-14 15:57:01 +0800322 /*Current pts has a transition*/
pengfei.liuab5a2262020-02-14 17:33:40 +0800323 DVR_DEBUG(1, "Current pts has a transition, [%llu, %llu, %llu]",
324 p_ctx->first_pts, p_ctx->last_pts, pts);
pengfei.liu567d6d82020-04-17 16:48:59 +0800325 p_ctx->last_record_pts = pts;
hualing chen2932d372020-04-29 13:44:00 +0800326 //p_ctx->cur_time = p_ctx->cur_time + PTS_DISCONTINE_DEVIATION;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800327 } else {
328 /*This is a normal pts, record it*/
hualing chena5f03222021-12-02 11:22:35 +0800329 loff_t off_diff = offset - p_ctx->last_offset;
330 float rate = (float) (off_diff) / (float)(diff);
331 if (p_ctx->avg_rate == 0.0) {
332 p_ctx->avg_rate = (float) offset / (float)(p_ctx->cur_time + diff);
333 }
hualing chen86b8e602021-12-23 11:04:19 +0800334 if (diff >= PCR_JUMP_DUR) {
hualing chena5f03222021-12-02 11:22:35 +0800335 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));
336 if (p_ctx->avg_rate != 0 && (int)(p_ctx->avg_rate) >= (int)(rate * 4)) {
337 diff = off_diff / p_ctx->avg_rate;
338 p_ctx->cur_time += diff;
339 } else {
340 p_ctx->cur_time += diff;
341 }
342 } else {
343 p_ctx->cur_time += diff;
344 }
hualing chen2aba4022020-03-02 13:49:55 +0800345 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800346 }
347 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800348
349 record_diff = pts - p_ctx->last_record_pts;
350 if (strlen(buf) > 0 &&
351 (record_diff > PCR_RECORD_INTERVAL_MS || p_ctx->last_record_pts == ULLONG_MAX)){
hualing chen4b7c15d2020-04-07 16:13:48 +0800352 fputs(buf, p_ctx->index_fp);
pengfei.liu567d6d82020-04-17 16:48:59 +0800353 fflush(p_ctx->index_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800354 fsync(fileno(p_ctx->index_fp));
pengfei.liu567d6d82020-04-17 16:48:59 +0800355 p_ctx->last_record_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800356 p_ctx->last_record_offset = offset;
357 if (p_ctx->cur_time > 0)
358 p_ctx->avg_rate = (float) offset / (float)p_ctx->cur_time;
pengfei.liu567d6d82020-04-17 16:48:59 +0800359 }
Pengfei Liub038b6a2020-01-14 15:57:01 +0800360 p_ctx->last_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800361 p_ctx->last_offset = offset;
Pengfei Liuc181a982020-01-07 19:27:13 +0800362 return DVR_SUCCESS;
363}
364
hualing chen266b9502020-04-04 17:39:39 +0800365loff_t segment_seek(Segment_Handle_t handle, uint64_t time, int block_size)
Pengfei Liuc181a982020-01-07 19:27:13 +0800366{
367 Segment_Context_t *p_ctx;
368 char buf[256];
369 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800370 uint64_t pts = 0L;
371 loff_t offset = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800372 char *p1, *p2;
373
hualing chen8a657f32021-08-30 13:12:49 +0800374 DVR_DEBUG(1, "into seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
375
Pengfei Liuc181a982020-01-07 19:27:13 +0800376 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800377 DVR_RETURN_IF_FALSE(p_ctx);
378 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
379 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800380
hualing chen266b9502020-04-04 17:39:39 +0800381 if (time == 0) {
382 offset = 0;
hualing chend241c7a2021-06-22 13:34:27 +0800383 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
384 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen266b9502020-04-04 17:39:39 +0800385 return offset;
386 }
387
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800388 memset(buf, 0, sizeof(buf));
389 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chen2aba4022020-03-02 13:49:55 +0800390 int line = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800391 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800392 line++;
Pengfei Liuc181a982020-01-07 19:27:13 +0800393 memset(value, 0, sizeof(value));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800394 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800395 p1 += 5;
Pengfei Liuc181a982020-01-07 19:27:13 +0800396 if ((p2 = strstr(buf, ","))) {
397 memcpy(value, p1, p2 - p1);
398 }
399 pts = strtoull(value, NULL, 10);
400 }
401
402 memset(value, 0, sizeof(value));
403 if ((p1 = strstr(buf, "offset="))) {
404 p1 += 7;
405 if ((p2 = strstr(buf, "}"))) {
406 memcpy(value, p1, p2 - p1);
407 }
408 offset = strtoull(value, NULL, 10);
409 }
hualing chen2aba4022020-03-02 13:49:55 +0800410 if (0)
411 {
412 DVR_DEBUG(1, "seek buf[%s]", buf);
413 DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
414 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800415 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800416 if (time <= pts) {
hualing chen266b9502020-04-04 17:39:39 +0800417 if (block_size > 0) {
418 offset = offset - offset%block_size;
419 }
hualing chend241c7a2021-06-22 13:34:27 +0800420 //DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line);
421 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800422 return offset;
423 }
hualing chen2aba4022020-03-02 13:49:55 +0800424 }
hualing chen2932d372020-04-29 13:44:00 +0800425 if (time > pts) {
426 if (block_size > 0) {
427 offset = offset - offset%block_size;
428 }
429 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 +0800430 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen2932d372020-04-29 13:44:00 +0800431 return offset;
432 }
hualing chen2aba4022020-03-02 13:49:55 +0800433 DVR_DEBUG(1, "seek error line [%d]", line);
Pengfei Liuc181a982020-01-07 19:27:13 +0800434 return DVR_FAILURE;
435}
436
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800437loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800438{
439 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800440 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800441 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800442 DVR_RETURN_IF_FALSE(p_ctx);
443 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800444 pos = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
445 return pos;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800446}
447
hualing chen5605eed2020-05-26 18:18:06 +0800448uint64_t segment_tell_position_time(Segment_Handle_t handle, loff_t position)
449{
450 Segment_Context_t *p_ctx;
451 char buf[256];
452 char value[256];
hualing chen969fe7b2021-05-26 15:13:17 +0800453 uint64_t ret = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800454 uint64_t pts = 0L;
hualing chen969fe7b2021-05-26 15:13:17 +0800455 uint64_t pts_p = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800456 loff_t offset = 0;
hualing chen969fe7b2021-05-26 15:13:17 +0800457 loff_t offset_p = 0;
hualing chen5605eed2020-05-26 18:18:06 +0800458 char *p1, *p2;
459
460 p_ctx = (Segment_Context_t *)handle;
461 DVR_RETURN_IF_FALSE(p_ctx);
462 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
463 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
464
465 memset(buf, 0, sizeof(buf));
466 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
467 DVR_RETURN_IF_FALSE(position != -1);
468
469 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
470 memset(value, 0, sizeof(value));
471 if ((p1 = strstr(buf, "time="))) {
472 p1 += 5;
473 if ((p2 = strstr(buf, ","))) {
474 memcpy(value, p1, p2 - p1);
475 }
476 pts = strtoull(value, NULL, 10);
477 }
478
479 memset(value, 0, sizeof(value));
480 if ((p1 = strstr(buf, "offset="))) {
481 p1 += 7;
482 if ((p2 = strstr(buf, "}"))) {
483 memcpy(value, p1, p2 - p1);
484 }
485 offset = strtoull(value, NULL, 10);
486 }
487
488 memset(buf, 0, sizeof(buf));
489 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chend241c7a2021-06-22 13:34:27 +0800490 if (position <= offset
491 &&position >= offset_p
492 && offset - offset_p > 0) {
hualing chen969fe7b2021-05-26 15:13:17 +0800493 ret = pts_p + (pts - pts_p) * (position - offset_p) / (offset - offset_p);
hualing chend241c7a2021-06-22 13:34:27 +0800494 //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 +0800495 return ret;
hualing chen5605eed2020-05-26 18:18:06 +0800496 }
hualing chen969fe7b2021-05-26 15:13:17 +0800497 offset_p = offset;
498 pts_p = pts;
hualing chen5605eed2020-05-26 18:18:06 +0800499 }
500 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
501 return pts;
502}
503
504
pengfei.liu8b563292020-02-26 15:49:02 +0800505uint64_t segment_tell_current_time(Segment_Handle_t handle)
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800506{
507 Segment_Context_t *p_ctx;
508 char buf[256];
509 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800510 uint64_t pts = 0L;
511 loff_t offset = 0, position = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800512 char *p1, *p2;
513
514 p_ctx = (Segment_Context_t *)handle;
515 DVR_RETURN_IF_FALSE(p_ctx);
516 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
517 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
518
519 memset(buf, 0, sizeof(buf));
520 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800521 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800522 DVR_RETURN_IF_FALSE(position != -1);
523
524 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
525 memset(value, 0, sizeof(value));
526 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800527 p1 += 5;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800528 if ((p2 = strstr(buf, ","))) {
529 memcpy(value, p1, p2 - p1);
530 }
531 pts = strtoull(value, NULL, 10);
532 }
533
534 memset(value, 0, sizeof(value));
535 if ((p1 = strstr(buf, "offset="))) {
536 p1 += 7;
537 if ((p2 = strstr(buf, "}"))) {
538 memcpy(value, p1, p2 - p1);
539 }
540 offset = strtoull(value, NULL, 10);
541 }
542
543 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800544 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800545 if (position <= offset) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800546 return pts;
547 }
548 }
hualing chena540a7e2020-03-27 16:44:05 +0800549 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800550 return pts;
Pengfei Liuc181a982020-01-07 19:27:13 +0800551}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800552
pengfei.liu8b563292020-02-26 15:49:02 +0800553uint64_t segment_tell_total_time(Segment_Handle_t handle)
554{
555 Segment_Context_t *p_ctx;
556 char buf[256];
557 char last_buf[256];
558 char value[256];
559 uint64_t pts = ULLONG_MAX;
560 loff_t offset = 0, position = 0;
561 char *p1, *p2;
562 int line = 0;
563
564 p_ctx = (Segment_Context_t *)handle;
565 DVR_RETURN_IF_FALSE(p_ctx);
566 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
567 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
568
569 memset(buf, 0, sizeof(buf));
570 memset(last_buf, 0, sizeof(last_buf));
hualing chend241c7a2021-06-22 13:34:27 +0800571 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
pengfei.liu8b563292020-02-26 15:49:02 +0800572 DVR_RETURN_IF_FALSE(position != -1);
573
hualing chen041c4092020-04-05 15:11:50 +0800574 //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1);
575 //if seek error.we need seek 0 pos.
576 if (fseek(p_ctx->index_fp, -1000L, SEEK_END) == -1) {
577 fseek(p_ctx->index_fp, 0L, SEEK_SET);
578 }
pengfei.liu8b563292020-02-26 15:49:02 +0800579 /* Save last line buffer */
580 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800581 if (strlen(buf) <= 0) {
hualing chen87072a82020-03-12 16:20:12 +0800582 DVR_DEBUG(1, "read index buf is len 0");
hualing chen2aba4022020-03-02 13:49:55 +0800583 continue;
584 }
pengfei.liu8b563292020-02-26 15:49:02 +0800585 memset(last_buf, 0, sizeof(last_buf));
586 memcpy(last_buf, buf, strlen(buf));
587 memset(buf, 0, sizeof(buf));
588 line++;
589 }
590
591 /* Extract time value */
592 memset(value, 0, sizeof(value));
593 if ((p1 = strstr(last_buf, "time="))) {
594 p1 += 5;
595 if ((p2 = strstr(last_buf, ","))) {
596 memcpy(value, p1, p2 - p1);
597 }
598 pts = strtoull(value, NULL, 10);
599 }
600
601 memset(value, 0, sizeof(value));
602 if ((p1 = strstr(last_buf, "offset="))) {
603 p1 += 7;
604 if ((p2 = strstr(last_buf, "}"))) {
605 memcpy(value, p1, p2 - p1);
606 }
607 offset = strtoull(value, NULL, 10);
608 }
hualing chen87072a82020-03-12 16:20:12 +0800609 //if (line < 2)
610 //DVR_DEBUG(1, "totle time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line);
pengfei.liu8b563292020-02-26 15:49:02 +0800611 return (pts == ULLONG_MAX ? DVR_FAILURE : pts);
612}
613
pengfei.liuab5a2262020-02-14 17:33:40 +0800614/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800615int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
616{
617 Segment_Context_t *p_ctx;
618 char buf[256];
619 uint32_t i;
620
621 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800622 DVR_RETURN_IF_FALSE(p_ctx);
623 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
624 DVR_RETURN_IF_FALSE(p_info);
hualing chen87072a82020-03-12 16:20:12 +0800625 //seek 0, rewrite info
626 DVR_RETURN_IF_FALSE(fseek(p_ctx->dat_fp, 0, SEEK_SET) != -1);
Pengfei Liub4734232020-01-17 18:25:10 +0800627
628 /*Save segment id*/
629 memset(buf, 0, sizeof(buf));
630 sprintf(buf, "id=%lld\n", p_info->id);
631 fputs(buf, p_ctx->dat_fp);
632
633 /*Save number of pids*/
634 memset(buf, 0, sizeof(buf));
635 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
636 fputs(buf, p_ctx->dat_fp);
637
638 /*Save pid information*/
639 for (i = 0; i < p_info->nb_pids; i++) {
640 memset(buf, 0, sizeof(buf));
641 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
642 fputs(buf, p_ctx->dat_fp);
643 }
644
645 /*Save segment duration*/
646 memset(buf, 0, sizeof(buf));
hualing chen87072a82020-03-12 16:20:12 +0800647 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800648 sprintf(buf, "duration=%ld\n", p_info->duration);
649 fputs(buf, p_ctx->dat_fp);
650
651 /*Save segment size*/
652 memset(buf, 0, sizeof(buf));
653 sprintf(buf, "size=%zu\n", p_info->size);
654 fputs(buf, p_ctx->dat_fp);
655
656 /*Save number of packets*/
657 memset(buf, 0, sizeof(buf));
658 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
659 fputs(buf, p_ctx->dat_fp);
660
661 fflush(p_ctx->dat_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800662 fsync(fileno(p_ctx->dat_fp));
Pengfei Liub4734232020-01-17 18:25:10 +0800663 return DVR_SUCCESS;
664}
665
pengfei.liuab5a2262020-02-14 17:33:40 +0800666/* Should consider the case of cut power, todo... */
hualing chenb9a02922021-12-14 11:29:47 +0800667int segment_store_allInfo(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
668{
669 Segment_Context_t *p_ctx;
670 char buf[256];
671 uint32_t i;
672
673 p_ctx = (Segment_Context_t *)handle;
674 DVR_RETURN_IF_FALSE(p_ctx);
675 DVR_RETURN_IF_FALSE(p_ctx->all_dat_fp);
676 DVR_RETURN_IF_FALSE(p_info);
677 //seek 0, rewrite info
678 DVR_RETURN_IF_FALSE(fseek(p_ctx->all_dat_fp, 0, SEEK_END) != -1);
679
680 /*Save segment id*/
681 memset(buf, 0, sizeof(buf));
682 sprintf(buf, "id=%lld\n", p_info->id);
683 fputs(buf, p_ctx->all_dat_fp);
684
685 /*Save number of pids*/
686 memset(buf, 0, sizeof(buf));
687 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
688 fputs(buf, p_ctx->all_dat_fp);
689
690 /*Save pid information*/
691 for (i = 0; i < p_info->nb_pids; i++) {
692 memset(buf, 0, sizeof(buf));
693 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
694 fputs(buf, p_ctx->all_dat_fp);
695 }
696
697 /*Save segment duration*/
698 memset(buf, 0, sizeof(buf));
699 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
700 sprintf(buf, "duration=%ld\n", p_info->duration);
701 fputs(buf, p_ctx->all_dat_fp);
702
703 /*Save segment size*/
704 memset(buf, 0, sizeof(buf));
705 sprintf(buf, "size=%zu\n", p_info->size);
706 fputs(buf, p_ctx->all_dat_fp);
707
708 /*Save number of packets*/
709 memset(buf, 0, sizeof(buf));
710 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
711 fputs(buf, p_ctx->all_dat_fp);
712
713 fflush(p_ctx->all_dat_fp);
714 fsync(fileno(p_ctx->all_dat_fp));
715 return DVR_SUCCESS;
716}
717
718/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800719int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
720{
721 Segment_Context_t *p_ctx;
722 uint32_t i;
723 char buf[256];
724 char value[256];
725 char *p1, *p2;
726
727 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800728 DVR_RETURN_IF_FALSE(p_ctx);
729 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800730
731 /*Load segment id*/
732 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800733 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800734 p1 = strstr(buf, "id=");
hualing chen2aba4022020-03-02 13:49:55 +0800735 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800736 p_info->id = strtoull(p1 + 3, NULL, 10);
737
738 /*Save number of pids*/
739 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800740 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800741 p1 = strstr(buf, "nb_pids=");
hualing chen2aba4022020-03-02 13:49:55 +0800742 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800743 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
744
745 /*Save pid information*/
746 for (i = 0; i < p_info->nb_pids; i++) {
747 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800748 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800749 memset(value, 0, sizeof(value));
750 if ((p1 = strstr(buf, "pid="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800751 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800752 p1 += 4;
753 if ((p2 = strstr(buf, ","))) {
hualing chen2aba4022020-03-02 13:49:55 +0800754 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800755 memcpy(value, p1, p2 - p1);
756 }
757 p_info->pids[i].pid = strtoull(value, NULL, 10);
758 }
759
760 memset(value, 0, sizeof(value));
761 if ((p1 = strstr(buf, "type="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800762 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800763 p1 += 5;
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].type = strtoull(value, NULL, 10);
769 }
770 }
771
772 /*Save segment duration*/
773 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800774 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800775 p1 = strstr(buf, "duration=");
hualing chen2aba4022020-03-02 13:49:55 +0800776 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800777 p_info->duration = strtoull(p1 + 9, NULL, 10);
hualing chena540a7e2020-03-27 16:44:05 +0800778 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800779
780 /*Save segment size*/
781 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800782 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800783 p1 = strstr(buf, "size=");
hualing chen2aba4022020-03-02 13:49:55 +0800784 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800785 p_info->size = strtoull(p1 + 5, NULL, 10);
786
787 /*Save number of packets*/
788 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800789 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800790 p1 = strstr(buf, "nb_packets=");
hualing chen2aba4022020-03-02 13:49:55 +0800791 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800792 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
793
794 return DVR_SUCCESS;
795}
796
hualing chenb9a02922021-12-14 11:29:47 +0800797/* Should consider the case of cut power, todo... */
798int segment_load_allInfo(Segment_Handle_t handle, struct list_head *list)
799{
800 Segment_Context_t *p_ctx;
801 uint32_t i;
802 char buf[256];
803 char value[256];
804 char *p1, *p2;
805
806 p_ctx = (Segment_Context_t *)handle;
807 DVR_RETURN_IF_FALSE(p_ctx);
808 DVR_RETURN_IF_FALSE(list);
hualing chen926a8ec2021-12-20 20:38:24 +0800809 if (p_ctx->all_dat_fp == NULL) {
810 DVR_DEBUG(1, "all dat file not open\n");
811 return DVR_FAILURE;
812 }
hualing chenb9a02922021-12-14 11:29:47 +0800813 //first get
814 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
815 DVR_RETURN_IF_FALSE(p1);
816
817 do {
818
819 DVR_RecordSegmentInfo_t *p_info;
820
821 p_info = malloc(sizeof(DVR_RecordSegmentInfo_t));
822 memset(p_info, 0, sizeof(DVR_RecordSegmentInfo_t));
823
824 list_add_tail(&p_info->head, list);
825
826 /*Load segment id*/
827 DVR_RETURN_IF_FALSE(p1);
828 p1 = strstr(buf, "id=");
829 DVR_RETURN_IF_FALSE(p1);
830 p_info->id = strtoull(p1 + 3, NULL, 10);
831
832 /*Save number of pids*/
833 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
834 DVR_RETURN_IF_FALSE(p1);
835 p1 = strstr(buf, "nb_pids=");
836 DVR_RETURN_IF_FALSE(p1);
837 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
838
839 /*Save pid information*/
840 for (i = 0; i < p_info->nb_pids; i++) {
841 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
842 DVR_RETURN_IF_FALSE(p1);
843 memset(value, 0, sizeof(value));
844 if ((p1 = strstr(buf, "pid="))) {
845 DVR_RETURN_IF_FALSE(p1);
846 p1 += 4;
847 if ((p2 = strstr(buf, ","))) {
848 DVR_RETURN_IF_FALSE(p2);
849 memcpy(value, p1, p2 - p1);
850 }
851 p_info->pids[i].pid = strtoull(value, NULL, 10);
852 }
853
854 memset(value, 0, sizeof(value));
855 if ((p1 = strstr(buf, "type="))) {
856 DVR_RETURN_IF_FALSE(p1);
857 p1 += 5;
858 if ((p2 = strstr(buf, "}"))) {
859 DVR_RETURN_IF_FALSE(p2);
860 memcpy(value, p1, p2 - p1);
861 }
862 p_info->pids[i].type = strtoull(value, NULL, 10);
863 }
864 }
865
866 /*Save segment duration*/
867 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
868 DVR_RETURN_IF_FALSE(p1);
869 p1 = strstr(buf, "duration=");
870 DVR_RETURN_IF_FALSE(p1);
871 p_info->duration = strtoull(p1 + 9, NULL, 10);
872 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
873
874 /*Save segment size*/
875 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
876 DVR_RETURN_IF_FALSE(p1);
877 p1 = strstr(buf, "size=");
878 DVR_RETURN_IF_FALSE(p1);
879 p_info->size = strtoull(p1 + 5, NULL, 10);
880
881 /*Save number of packets*/
882 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
883 DVR_RETURN_IF_FALSE(p1);
884 p1 = strstr(buf, "nb_packets=");
885 DVR_RETURN_IF_FALSE(p1);
886 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
887 //if reach end,exit loop
888 p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp);
889 } while (p1);
890
891 return DVR_SUCCESS;
892}
893
Pengfei Liub4734232020-01-17 18:25:10 +0800894int segment_delete(const char *location, uint64_t segment_id)
895{
896 char fname[MAX_SEGMENT_PATH_SIZE];
897 int ret;
898
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800899 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800900
901 /*delete ts file*/
902 memset(fname, 0, sizeof(fname));
903 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
904 ret = unlink(fname);
905 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800906 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800907
908 /*delete index file*/
909 memset(fname, 0, sizeof(fname));
910 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
911 unlink(fname);
912 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800913 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800914
915 /*delete store information file*/
916 memset(fname, 0, sizeof(fname));
917 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
918 unlink(fname);
919 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800920 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800921
922 return DVR_SUCCESS;
923}
924
hualing chen87072a82020-03-12 16:20:12 +0800925int segment_ongoing(Segment_Handle_t handle)
926{
927 Segment_Context_t *p_ctx;
928 p_ctx = (Segment_Context_t *)handle;
929 struct stat mstat;
930
931 char going_name[MAX_SEGMENT_PATH_SIZE];
932 memset(going_name, 0, sizeof(going_name));
933 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
934 int ret = stat(going_name, &mstat);
935 DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret);
936 if (ret != 0) {
937 return DVR_FAILURE;
938 }
939 return DVR_SUCCESS;
940}
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800941loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800942{
943 Segment_Context_t *p_ctx;
944 char buf[256];
945 char value[256];
946 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800947 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800948 char *p1, *p2;
949
950 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800951 DVR_RETURN_IF_FALSE(p_ctx);
952 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
953 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800954
955 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800956 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800957 printf("start gets pts\n");
958 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
959 printf("buf[%s]\n", buf);
960 memset(value, 0, sizeof(value));
961 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800962 p1 += 5;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800963 if ((p2 = strstr(buf, ","))) {
964 memcpy(value, p1, p2 - p1);
965 }
966 pts = strtoull(value, NULL, 10);
967 }
968
969 memset(value, 0, sizeof(value));
970 if ((p1 = strstr(buf, "offset="))) {
971 p1 += 7;
972 if ((p2 = strstr(buf, "}"))) {
973 memcpy(value, p1, p2 - p1);
974 }
975 offset = strtoull(value, NULL, 10);
976 }
977
978 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800979 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800980 }
981
982 return 0;
983}