blob: 3bd8938dc520b42d055785d9f4cce3dfd977556b [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*/
pengfei.liu567d6d82020-04-17 16:48:59 +080025 FILE *ongoing_fp; /**< Ongoing file fd, used to verify timedhift mode*/
Pengfei Liub4734232020-01-17 18:25:10 +080026 uint64_t first_pts; /**< First pts value, use for write mode*/
pengfei.liu567d6d82020-04-17 16:48:59 +080027 uint64_t last_pts; /**< Last input pts value, use for write mode*/
28 uint64_t last_record_pts; /**< Last record pts value, use for write mode*/
pengfei.liuab5a2262020-02-14 17:33:40 +080029 uint64_t cur_time; /**< Current time save in index file */
pengfei.liu567d6d82020-04-17 16:48:59 +080030 uint64_t segment_id; /**< Current segment ID */
31 char location[MAX_SEGMENT_PATH_SIZE]; /**< Current time save in index file */
hualing chena5f03222021-12-02 11:22:35 +080032 loff_t first_offset;
33 loff_t last_offset;
34 loff_t last_record_offset;
35 float avg_rate;
Pengfei Liuc181a982020-01-07 19:27:13 +080036} Segment_Context_t;
37
Pengfei Liub4734232020-01-17 18:25:10 +080038/**\brief Segment file type*/
Pengfei Liuc181a982020-01-07 19:27:13 +080039typedef enum {
Pengfei Liub4734232020-01-17 18:25:10 +080040 SEGMENT_FILE_TYPE_TS, /**< Used for store TS data*/
41 SEGMENT_FILE_TYPE_INDEX, /**< Used for store index data*/
42 SEGMENT_FILE_TYPE_DAT, /**< Used for store information data, such as duration etc*/
pengfei.liu567d6d82020-04-17 16:48:59 +080043 SEGMENT_FILE_TYPE_ONGOING, /**< Used for store information data, such as duration etc*/
Pengfei Liuc181a982020-01-07 19:27:13 +080044} Segment_FileType_t;
45
46static void segment_get_fname(char fname[MAX_SEGMENT_PATH_SIZE],
Pengfei Liub4734232020-01-17 18:25:10 +080047 const char location[DVR_MAX_LOCATION_SIZE],
Pengfei Liuc181a982020-01-07 19:27:13 +080048 uint64_t segment_id,
49 Segment_FileType_t type)
50{
51 int offset;
52
53 memset(fname, 0, MAX_SEGMENT_PATH_SIZE);
54 strncpy(fname, location, strlen(location));
55 offset = strlen(location);
56 strncpy(fname + offset, "-", 1);
57 offset += 1;
58 sprintf(fname + offset, "%04llu", segment_id);
59 offset += 4;
60 if (type == SEGMENT_FILE_TYPE_TS)
61 strncpy(fname + offset, ".ts", 3);
62 else if (type == SEGMENT_FILE_TYPE_INDEX)
63 strncpy(fname + offset, ".idx", 4);
Pengfei Liub4734232020-01-17 18:25:10 +080064 else if (type == SEGMENT_FILE_TYPE_DAT)
65 strncpy(fname + offset, ".dat", 4);
hualing chen87072a82020-03-12 16:20:12 +080066 else if (type == SEGMENT_FILE_TYPE_ONGOING)
67 strncpy(fname + offset, ".going", 6);
Pengfei Liuc181a982020-01-07 19:27:13 +080068}
69
Pengfei Liu3b1a8202020-02-12 23:04:21 +080070static void segment_get_dirname(char dir_name[MAX_SEGMENT_PATH_SIZE],
71 const char location[DVR_MAX_LOCATION_SIZE])
72{
73 char *p;
74 int i;
75 int found = 0;
76
77 for (i = 0; i < (int)strlen(location); i++) {
78 if (location[i] == '/') {
79 p = (char *)location + i;
80 found = 1;
81 }
82 }
83 if (found)
84 memcpy(dir_name, location, p - location);
85}
86
Pengfei Liuc181a982020-01-07 19:27:13 +080087int segment_open(Segment_OpenParams_t *params, Segment_Handle_t *p_handle)
88{
89 Segment_Context_t *p_ctx;
90 char ts_fname[MAX_SEGMENT_PATH_SIZE];
91 char index_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liub4734232020-01-17 18:25:10 +080092 char dat_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liu3b1a8202020-02-12 23:04:21 +080093 char dir_name[MAX_SEGMENT_PATH_SIZE];
hualing chen87072a82020-03-12 16:20:12 +080094 char going_name[MAX_SEGMENT_PATH_SIZE];
Pengfei Liuc181a982020-01-07 19:27:13 +080095
Pengfei Liu3b1a8202020-02-12 23:04:21 +080096 DVR_RETURN_IF_FALSE(params);
97 DVR_RETURN_IF_FALSE(p_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +080098
hualing chena540a7e2020-03-27 16:44:05 +080099 //DVR_DEBUG(1, "%s, location:%s, id:%llu", __func__, params->location, params->segment_id);
Pengfei Liuc181a982020-01-07 19:27:13 +0800100
Pengfei Liub038b6a2020-01-14 15:57:01 +0800101 p_ctx = (void*)malloc(sizeof(Segment_Context_t));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800102 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +0800103 memset(p_ctx, 0, sizeof(Segment_Context_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800104
105 memset(ts_fname, 0, sizeof(ts_fname));
106 segment_get_fname(ts_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_TS);
107
108 memset(index_fname, 0, sizeof(index_fname));
109 segment_get_fname(index_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_INDEX);
110
Pengfei Liub4734232020-01-17 18:25:10 +0800111 memset(dat_fname, 0, sizeof(dat_fname));
112 segment_get_fname(dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_DAT);
113
hualing chen87072a82020-03-12 16:20:12 +0800114 memset(going_name, 0, sizeof(going_name));
115 segment_get_fname(going_name, params->location, params->segment_id, SEGMENT_FILE_TYPE_ONGOING);
116
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800117 memset(dir_name, 0, sizeof(dir_name));
118 segment_get_dirname(dir_name, params->location);
119 if (access(dir_name, F_OK) == -1) {
120 DVR_DEBUG(1, "%s dir %s is not exist, create it", __func__, dir_name);
121 mkdir(dir_name, 0666);
122 }
123
Pengfei Liuc181a982020-01-07 19:27:13 +0800124 if (params->mode == SEGMENT_MODE_READ) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800125 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800126 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800127 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800128 p_ctx->ongoing_fp = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800129 } else if (params->mode == SEGMENT_MODE_WRITE) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800130 p_ctx->ts_fd = open(ts_fname, O_CREAT | O_RDWR | O_TRUNC, 0644);
Pengfei Liuc181a982020-01-07 19:27:13 +0800131 p_ctx->index_fp = fopen(index_fname, "w+");
Pengfei Liub4734232020-01-17 18:25:10 +0800132 p_ctx->dat_fp = fopen(dat_fname, "w+");
hualing chen87072a82020-03-12 16:20:12 +0800133 p_ctx->ongoing_fp = fopen(going_name, "w+");
Pengfei Liub038b6a2020-01-14 15:57:01 +0800134 p_ctx->first_pts = ULLONG_MAX;
135 p_ctx->last_pts = ULLONG_MAX;
pengfei.liu567d6d82020-04-17 16:48:59 +0800136 p_ctx->last_record_pts = ULLONG_MAX;
hualing chena5f03222021-12-02 11:22:35 +0800137 p_ctx->avg_rate = 0.0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800138 } else {
139 DVR_DEBUG(1, "%s, unknow mode use default", __func__);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800140 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800141 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800142 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800143 p_ctx->ongoing_fp = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800144 }
145
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800146 if (p_ctx->ts_fd == -1 || !p_ctx->index_fp || !p_ctx->dat_fp) {
Pengfei Liub4734232020-01-17 18:25:10 +0800147 DVR_DEBUG(1, "%s open file failed [%s, %s, %s], reason:%s", __func__,
148 ts_fname, index_fname, dat_fname, strerror(errno));
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800149 if (p_ctx->ts_fd != -1)
150 close(p_ctx->ts_fd);
151 if (p_ctx->index_fp)
152 fclose(p_ctx->index_fp);
153 if (p_ctx->dat_fp)
154 fclose(p_ctx->dat_fp);
155 if (p_ctx->ongoing_fp)
156 fclose(p_ctx->ongoing_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800157 free(p_ctx);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800158 *p_handle = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800159 return DVR_FAILURE;
160 }
hualing chen87072a82020-03-12 16:20:12 +0800161 p_ctx->segment_id = params->segment_id;
162 strncpy(p_ctx->location, params->location, strlen(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800163
hualing chen7a56cba2020-04-14 14:09:27 +0800164 //DVR_DEBUG(1, "%s, open file success p_ctx->location [%s]", __func__, p_ctx->location, params->mode);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800165 *p_handle = (Segment_Handle_t)p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800166 return DVR_SUCCESS;
167}
168
169int segment_close(Segment_Handle_t handle)
170{
171 Segment_Context_t *p_ctx;
172
Pengfei Liub038b6a2020-01-14 15:57:01 +0800173 p_ctx = (void *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800174 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800175
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800176 if (p_ctx->ts_fd != -1) {
177 close(p_ctx->ts_fd);
Pengfei Liuc181a982020-01-07 19:27:13 +0800178 }
179
180 if (p_ctx->index_fp) {
181 fclose(p_ctx->index_fp);
182 }
183
Pengfei Liub4734232020-01-17 18:25:10 +0800184 if (p_ctx->dat_fp) {
185 fclose(p_ctx->dat_fp);
186 }
187
hualing chen87072a82020-03-12 16:20:12 +0800188 if (p_ctx->ongoing_fp != NULL) {
189 fclose(p_ctx->ongoing_fp);
190 char going_name[MAX_SEGMENT_PATH_SIZE];
191 memset(going_name, 0, sizeof(going_name));
192 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
193 DVR_DEBUG(1, "segment close del [%s]", going_name);
194 unlink(going_name);
195 }
196
Pengfei Liuc181a982020-01-07 19:27:13 +0800197 free(p_ctx);
198 return 0;
199}
200
201ssize_t segment_read(Segment_Handle_t handle, void *buf, size_t count)
202{
203 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800204 ssize_t len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800205 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800206 DVR_RETURN_IF_FALSE(p_ctx);
207 DVR_RETURN_IF_FALSE(buf);
208 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800209 len = read(p_ctx->ts_fd, buf, count);
210 return len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800211}
212
213ssize_t segment_write(Segment_Handle_t handle, void *buf, size_t count)
214{
215 Segment_Context_t *p_ctx;
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800216 ssize_t len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800217 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800218 DVR_RETURN_IF_FALSE(p_ctx);
219 DVR_RETURN_IF_FALSE(buf);
220 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800221 len = write(p_ctx->ts_fd, buf, count);
222 fsync(p_ctx->ts_fd);
223 return len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800224}
225
hualing chen2932d372020-04-29 13:44:00 +0800226int segment_update_pts_force(Segment_Handle_t handle, uint64_t pts, loff_t offset)
Pengfei Liuc181a982020-01-07 19:27:13 +0800227{
228 Segment_Context_t *p_ctx;
229 char buf[256];
pengfei.liu567d6d82020-04-17 16:48:59 +0800230 int record_diff = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800231
232 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800233 DVR_RETURN_IF_FALSE(p_ctx);
234 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800235
Pengfei Liub038b6a2020-01-14 15:57:01 +0800236 if (p_ctx->first_pts == ULLONG_MAX) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800237 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800238 p_ctx->first_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800239 p_ctx->first_offset = offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800240 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800241 memset(buf, 0, sizeof(buf));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800242 if (p_ctx->last_pts == ULLONG_MAX) {
243 /*Last pts is init value*/
hualing chen2aba4022020-03-02 13:49:55 +0800244 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
pengfei.liuab5a2262020-02-14 17:33:40 +0800245 p_ctx->cur_time = pts - p_ctx->first_pts;
hualing chena5f03222021-12-02 11:22:35 +0800246 DVR_DEBUG(1, "%s force pcr:%llu -1", __func__, pts);
hualing chen2932d372020-04-29 13:44:00 +0800247 } else {
248 /*Last pts has valid value*/
249 int diff = pts - p_ctx->last_pts;
250 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
251 /*Current pts has a transition*/
252 DVR_DEBUG(1, "[%s]force update Current pts has a transition, [%llu, %llu, %llu]",__func__,
253 p_ctx->first_pts, p_ctx->last_pts, pts);
254 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
255 } else {
256 /*This is a normal pts, record it*/
hualing chena5f03222021-12-02 11:22:35 +0800257 //check if this pcr is transition.if true,add 200ms
258 //other case normal.
hualing chen2932d372020-04-29 13:44:00 +0800259 p_ctx->cur_time += diff;
260 DVR_DEBUG(1, "%s force pcr:%llu -1 diff [%d]", __func__, pts, diff);
261 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
262 }
263 }
264
265 record_diff = pts - p_ctx->last_record_pts;
266 if (strlen(buf) > 0) {
267 DVR_DEBUG(1, "%s force pcr:%llu buf:%s", __func__, pts, buf);
268 fputs(buf, p_ctx->index_fp);
269 fflush(p_ctx->index_fp);
270 fsync(fileno(p_ctx->index_fp));
271 p_ctx->last_record_pts = pts;
272 }
273 p_ctx->last_pts = pts;
hualing chen2932d372020-04-29 13:44:00 +0800274 return DVR_SUCCESS;
275}
276
277int segment_update_pts(Segment_Handle_t handle, uint64_t pts, loff_t offset)
278{
279 Segment_Context_t *p_ctx;
280 char buf[256];
281 int record_diff = 0;
282
283 p_ctx = (Segment_Context_t *)handle;
284 DVR_RETURN_IF_FALSE(p_ctx);
285 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
286
287 if (p_ctx->first_pts == ULLONG_MAX) {
288 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
289 p_ctx->first_pts = pts;
290 //p_ctx->cur_time = p_ctx->cur_time + PTS_HEAD_DEVIATION;
291 }
292 memset(buf, 0, sizeof(buf));
293 if (p_ctx->last_pts == ULLONG_MAX) {
294 /*Last pts is init value*/
295 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
296 p_ctx->cur_time = pts - p_ctx->first_pts;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800297 } else {
298 /*Last pts has valid value*/
pengfei.liu567d6d82020-04-17 16:48:59 +0800299 int diff = pts - p_ctx->last_pts;
hualing chen4b7c15d2020-04-07 16:13:48 +0800300 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
Pengfei Liub038b6a2020-01-14 15:57:01 +0800301 /*Current pts has a transition*/
pengfei.liuab5a2262020-02-14 17:33:40 +0800302 DVR_DEBUG(1, "Current pts has a transition, [%llu, %llu, %llu]",
303 p_ctx->first_pts, p_ctx->last_pts, pts);
pengfei.liu567d6d82020-04-17 16:48:59 +0800304 p_ctx->last_record_pts = pts;
hualing chen2932d372020-04-29 13:44:00 +0800305 //p_ctx->cur_time = p_ctx->cur_time + PTS_DISCONTINE_DEVIATION;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800306 } else {
307 /*This is a normal pts, record it*/
hualing chena5f03222021-12-02 11:22:35 +0800308 loff_t off_diff = offset - p_ctx->last_offset;
309 float rate = (float) (off_diff) / (float)(diff);
310 if (p_ctx->avg_rate == 0.0) {
311 p_ctx->avg_rate = (float) offset / (float)(p_ctx->cur_time + diff);
312 }
313 if (diff >= 1000) {
314 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));
315 if (p_ctx->avg_rate != 0 && (int)(p_ctx->avg_rate) >= (int)(rate * 4)) {
316 diff = off_diff / p_ctx->avg_rate;
317 p_ctx->cur_time += diff;
318 } else {
319 p_ctx->cur_time += diff;
320 }
321 } else {
322 p_ctx->cur_time += diff;
323 }
hualing chen2aba4022020-03-02 13:49:55 +0800324 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800325 }
326 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800327
328 record_diff = pts - p_ctx->last_record_pts;
329 if (strlen(buf) > 0 &&
330 (record_diff > PCR_RECORD_INTERVAL_MS || p_ctx->last_record_pts == ULLONG_MAX)){
hualing chen4b7c15d2020-04-07 16:13:48 +0800331 fputs(buf, p_ctx->index_fp);
pengfei.liu567d6d82020-04-17 16:48:59 +0800332 fflush(p_ctx->index_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800333 fsync(fileno(p_ctx->index_fp));
pengfei.liu567d6d82020-04-17 16:48:59 +0800334 p_ctx->last_record_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800335 p_ctx->last_record_offset = offset;
336 if (p_ctx->cur_time > 0)
337 p_ctx->avg_rate = (float) offset / (float)p_ctx->cur_time;
pengfei.liu567d6d82020-04-17 16:48:59 +0800338 }
Pengfei Liub038b6a2020-01-14 15:57:01 +0800339 p_ctx->last_pts = pts;
hualing chena5f03222021-12-02 11:22:35 +0800340 p_ctx->last_offset = offset;
Pengfei Liuc181a982020-01-07 19:27:13 +0800341 return DVR_SUCCESS;
342}
343
hualing chen266b9502020-04-04 17:39:39 +0800344loff_t segment_seek(Segment_Handle_t handle, uint64_t time, int block_size)
Pengfei Liuc181a982020-01-07 19:27:13 +0800345{
346 Segment_Context_t *p_ctx;
347 char buf[256];
348 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800349 uint64_t pts = 0L;
350 loff_t offset = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800351 char *p1, *p2;
352
hualing chen8a657f32021-08-30 13:12:49 +0800353 DVR_DEBUG(1, "into seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
354
Pengfei Liuc181a982020-01-07 19:27:13 +0800355 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800356 DVR_RETURN_IF_FALSE(p_ctx);
357 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
358 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800359
hualing chen266b9502020-04-04 17:39:39 +0800360 if (time == 0) {
361 offset = 0;
hualing chend241c7a2021-06-22 13:34:27 +0800362 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
363 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen266b9502020-04-04 17:39:39 +0800364 return offset;
365 }
366
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800367 memset(buf, 0, sizeof(buf));
368 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chen2aba4022020-03-02 13:49:55 +0800369 int line = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800370 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800371 line++;
Pengfei Liuc181a982020-01-07 19:27:13 +0800372 memset(value, 0, sizeof(value));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800373 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800374 p1 += 5;
Pengfei Liuc181a982020-01-07 19:27:13 +0800375 if ((p2 = strstr(buf, ","))) {
376 memcpy(value, p1, p2 - p1);
377 }
378 pts = strtoull(value, NULL, 10);
379 }
380
381 memset(value, 0, sizeof(value));
382 if ((p1 = strstr(buf, "offset="))) {
383 p1 += 7;
384 if ((p2 = strstr(buf, "}"))) {
385 memcpy(value, p1, p2 - p1);
386 }
387 offset = strtoull(value, NULL, 10);
388 }
hualing chen2aba4022020-03-02 13:49:55 +0800389 if (0)
390 {
391 DVR_DEBUG(1, "seek buf[%s]", buf);
392 DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
393 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800394 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800395 if (time <= pts) {
hualing chen266b9502020-04-04 17:39:39 +0800396 if (block_size > 0) {
397 offset = offset - offset%block_size;
398 }
hualing chend241c7a2021-06-22 13:34:27 +0800399 //DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line);
400 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800401 return offset;
402 }
hualing chen2aba4022020-03-02 13:49:55 +0800403 }
hualing chen2932d372020-04-29 13:44:00 +0800404 if (time > pts) {
405 if (block_size > 0) {
406 offset = offset - offset%block_size;
407 }
408 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 +0800409 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen2932d372020-04-29 13:44:00 +0800410 return offset;
411 }
hualing chen2aba4022020-03-02 13:49:55 +0800412 DVR_DEBUG(1, "seek error line [%d]", line);
Pengfei Liuc181a982020-01-07 19:27:13 +0800413 return DVR_FAILURE;
414}
415
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800416loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800417{
418 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800419 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800420 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800421 DVR_RETURN_IF_FALSE(p_ctx);
422 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800423 pos = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
424 return pos;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800425}
426
hualing chen5605eed2020-05-26 18:18:06 +0800427uint64_t segment_tell_position_time(Segment_Handle_t handle, loff_t position)
428{
429 Segment_Context_t *p_ctx;
430 char buf[256];
431 char value[256];
hualing chen969fe7b2021-05-26 15:13:17 +0800432 uint64_t ret = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800433 uint64_t pts = 0L;
hualing chen969fe7b2021-05-26 15:13:17 +0800434 uint64_t pts_p = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800435 loff_t offset = 0;
hualing chen969fe7b2021-05-26 15:13:17 +0800436 loff_t offset_p = 0;
hualing chen5605eed2020-05-26 18:18:06 +0800437 char *p1, *p2;
438
439 p_ctx = (Segment_Context_t *)handle;
440 DVR_RETURN_IF_FALSE(p_ctx);
441 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
442 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
443
444 memset(buf, 0, sizeof(buf));
445 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
446 DVR_RETURN_IF_FALSE(position != -1);
447
448 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
449 memset(value, 0, sizeof(value));
450 if ((p1 = strstr(buf, "time="))) {
451 p1 += 5;
452 if ((p2 = strstr(buf, ","))) {
453 memcpy(value, p1, p2 - p1);
454 }
455 pts = strtoull(value, NULL, 10);
456 }
457
458 memset(value, 0, sizeof(value));
459 if ((p1 = strstr(buf, "offset="))) {
460 p1 += 7;
461 if ((p2 = strstr(buf, "}"))) {
462 memcpy(value, p1, p2 - p1);
463 }
464 offset = strtoull(value, NULL, 10);
465 }
466
467 memset(buf, 0, sizeof(buf));
468 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chend241c7a2021-06-22 13:34:27 +0800469 if (position <= offset
470 &&position >= offset_p
471 && offset - offset_p > 0) {
hualing chen969fe7b2021-05-26 15:13:17 +0800472 ret = pts_p + (pts - pts_p) * (position - offset_p) / (offset - offset_p);
hualing chend241c7a2021-06-22 13:34:27 +0800473 //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 +0800474 return ret;
hualing chen5605eed2020-05-26 18:18:06 +0800475 }
hualing chen969fe7b2021-05-26 15:13:17 +0800476 offset_p = offset;
477 pts_p = pts;
hualing chen5605eed2020-05-26 18:18:06 +0800478 }
479 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
480 return pts;
481}
482
483
pengfei.liu8b563292020-02-26 15:49:02 +0800484uint64_t segment_tell_current_time(Segment_Handle_t handle)
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800485{
486 Segment_Context_t *p_ctx;
487 char buf[256];
488 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800489 uint64_t pts = 0L;
490 loff_t offset = 0, position = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800491 char *p1, *p2;
492
493 p_ctx = (Segment_Context_t *)handle;
494 DVR_RETURN_IF_FALSE(p_ctx);
495 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
496 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
497
498 memset(buf, 0, sizeof(buf));
499 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800500 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800501 DVR_RETURN_IF_FALSE(position != -1);
502
503 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
504 memset(value, 0, sizeof(value));
505 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800506 p1 += 5;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800507 if ((p2 = strstr(buf, ","))) {
508 memcpy(value, p1, p2 - p1);
509 }
510 pts = strtoull(value, NULL, 10);
511 }
512
513 memset(value, 0, sizeof(value));
514 if ((p1 = strstr(buf, "offset="))) {
515 p1 += 7;
516 if ((p2 = strstr(buf, "}"))) {
517 memcpy(value, p1, p2 - p1);
518 }
519 offset = strtoull(value, NULL, 10);
520 }
521
522 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800523 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800524 if (position <= offset) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800525 return pts;
526 }
527 }
hualing chena540a7e2020-03-27 16:44:05 +0800528 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800529 return pts;
Pengfei Liuc181a982020-01-07 19:27:13 +0800530}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800531
pengfei.liu8b563292020-02-26 15:49:02 +0800532uint64_t segment_tell_total_time(Segment_Handle_t handle)
533{
534 Segment_Context_t *p_ctx;
535 char buf[256];
536 char last_buf[256];
537 char value[256];
538 uint64_t pts = ULLONG_MAX;
539 loff_t offset = 0, position = 0;
540 char *p1, *p2;
541 int line = 0;
542
543 p_ctx = (Segment_Context_t *)handle;
544 DVR_RETURN_IF_FALSE(p_ctx);
545 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
546 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
547
548 memset(buf, 0, sizeof(buf));
549 memset(last_buf, 0, sizeof(last_buf));
hualing chend241c7a2021-06-22 13:34:27 +0800550 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
pengfei.liu8b563292020-02-26 15:49:02 +0800551 DVR_RETURN_IF_FALSE(position != -1);
552
hualing chen041c4092020-04-05 15:11:50 +0800553 //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1);
554 //if seek error.we need seek 0 pos.
555 if (fseek(p_ctx->index_fp, -1000L, SEEK_END) == -1) {
556 fseek(p_ctx->index_fp, 0L, SEEK_SET);
557 }
pengfei.liu8b563292020-02-26 15:49:02 +0800558 /* Save last line buffer */
559 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800560 if (strlen(buf) <= 0) {
hualing chen87072a82020-03-12 16:20:12 +0800561 DVR_DEBUG(1, "read index buf is len 0");
hualing chen2aba4022020-03-02 13:49:55 +0800562 continue;
563 }
pengfei.liu8b563292020-02-26 15:49:02 +0800564 memset(last_buf, 0, sizeof(last_buf));
565 memcpy(last_buf, buf, strlen(buf));
566 memset(buf, 0, sizeof(buf));
567 line++;
568 }
569
570 /* Extract time value */
571 memset(value, 0, sizeof(value));
572 if ((p1 = strstr(last_buf, "time="))) {
573 p1 += 5;
574 if ((p2 = strstr(last_buf, ","))) {
575 memcpy(value, p1, p2 - p1);
576 }
577 pts = strtoull(value, NULL, 10);
578 }
579
580 memset(value, 0, sizeof(value));
581 if ((p1 = strstr(last_buf, "offset="))) {
582 p1 += 7;
583 if ((p2 = strstr(last_buf, "}"))) {
584 memcpy(value, p1, p2 - p1);
585 }
586 offset = strtoull(value, NULL, 10);
587 }
hualing chen87072a82020-03-12 16:20:12 +0800588 //if (line < 2)
589 //DVR_DEBUG(1, "totle time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line);
pengfei.liu8b563292020-02-26 15:49:02 +0800590 return (pts == ULLONG_MAX ? DVR_FAILURE : pts);
591}
592
pengfei.liuab5a2262020-02-14 17:33:40 +0800593/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800594int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
595{
596 Segment_Context_t *p_ctx;
597 char buf[256];
598 uint32_t i;
599
600 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800601 DVR_RETURN_IF_FALSE(p_ctx);
602 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
603 DVR_RETURN_IF_FALSE(p_info);
hualing chen87072a82020-03-12 16:20:12 +0800604 //seek 0, rewrite info
605 DVR_RETURN_IF_FALSE(fseek(p_ctx->dat_fp, 0, SEEK_SET) != -1);
Pengfei Liub4734232020-01-17 18:25:10 +0800606
607 /*Save segment id*/
608 memset(buf, 0, sizeof(buf));
609 sprintf(buf, "id=%lld\n", p_info->id);
610 fputs(buf, p_ctx->dat_fp);
611
612 /*Save number of pids*/
613 memset(buf, 0, sizeof(buf));
614 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
615 fputs(buf, p_ctx->dat_fp);
616
617 /*Save pid information*/
618 for (i = 0; i < p_info->nb_pids; i++) {
619 memset(buf, 0, sizeof(buf));
620 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
621 fputs(buf, p_ctx->dat_fp);
622 }
623
624 /*Save segment duration*/
625 memset(buf, 0, sizeof(buf));
hualing chen87072a82020-03-12 16:20:12 +0800626 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800627 sprintf(buf, "duration=%ld\n", p_info->duration);
628 fputs(buf, p_ctx->dat_fp);
629
630 /*Save segment size*/
631 memset(buf, 0, sizeof(buf));
632 sprintf(buf, "size=%zu\n", p_info->size);
633 fputs(buf, p_ctx->dat_fp);
634
635 /*Save number of packets*/
636 memset(buf, 0, sizeof(buf));
637 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
638 fputs(buf, p_ctx->dat_fp);
639
640 fflush(p_ctx->dat_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800641 fsync(fileno(p_ctx->dat_fp));
Pengfei Liub4734232020-01-17 18:25:10 +0800642 return DVR_SUCCESS;
643}
644
pengfei.liuab5a2262020-02-14 17:33:40 +0800645/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800646int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
647{
648 Segment_Context_t *p_ctx;
649 uint32_t i;
650 char buf[256];
651 char value[256];
652 char *p1, *p2;
653
654 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800655 DVR_RETURN_IF_FALSE(p_ctx);
656 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800657
658 /*Load segment id*/
659 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800660 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800661 p1 = strstr(buf, "id=");
hualing chen2aba4022020-03-02 13:49:55 +0800662 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800663 p_info->id = strtoull(p1 + 3, NULL, 10);
664
665 /*Save number of pids*/
666 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800667 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800668 p1 = strstr(buf, "nb_pids=");
hualing chen2aba4022020-03-02 13:49:55 +0800669 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800670 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
671
672 /*Save pid information*/
673 for (i = 0; i < p_info->nb_pids; i++) {
674 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800675 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800676 memset(value, 0, sizeof(value));
677 if ((p1 = strstr(buf, "pid="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800678 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800679 p1 += 4;
680 if ((p2 = strstr(buf, ","))) {
hualing chen2aba4022020-03-02 13:49:55 +0800681 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800682 memcpy(value, p1, p2 - p1);
683 }
684 p_info->pids[i].pid = strtoull(value, NULL, 10);
685 }
686
687 memset(value, 0, sizeof(value));
688 if ((p1 = strstr(buf, "type="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800689 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800690 p1 += 5;
691 if ((p2 = strstr(buf, "}"))) {
hualing chen2aba4022020-03-02 13:49:55 +0800692 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800693 memcpy(value, p1, p2 - p1);
694 }
695 p_info->pids[i].type = strtoull(value, NULL, 10);
696 }
697 }
698
699 /*Save segment duration*/
700 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800701 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800702 p1 = strstr(buf, "duration=");
hualing chen2aba4022020-03-02 13:49:55 +0800703 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800704 p_info->duration = strtoull(p1 + 9, NULL, 10);
hualing chena540a7e2020-03-27 16:44:05 +0800705 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800706
707 /*Save segment size*/
708 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800709 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800710 p1 = strstr(buf, "size=");
hualing chen2aba4022020-03-02 13:49:55 +0800711 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800712 p_info->size = strtoull(p1 + 5, NULL, 10);
713
714 /*Save number of packets*/
715 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800716 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800717 p1 = strstr(buf, "nb_packets=");
hualing chen2aba4022020-03-02 13:49:55 +0800718 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800719 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
720
721 return DVR_SUCCESS;
722}
723
724int segment_delete(const char *location, uint64_t segment_id)
725{
726 char fname[MAX_SEGMENT_PATH_SIZE];
727 int ret;
728
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800729 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800730
731 /*delete ts file*/
732 memset(fname, 0, sizeof(fname));
733 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
734 ret = unlink(fname);
735 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800736 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800737
738 /*delete index file*/
739 memset(fname, 0, sizeof(fname));
740 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
741 unlink(fname);
742 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800743 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800744
745 /*delete store information file*/
746 memset(fname, 0, sizeof(fname));
747 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
748 unlink(fname);
749 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800750 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800751
752 return DVR_SUCCESS;
753}
754
hualing chen87072a82020-03-12 16:20:12 +0800755int segment_ongoing(Segment_Handle_t handle)
756{
757 Segment_Context_t *p_ctx;
758 p_ctx = (Segment_Context_t *)handle;
759 struct stat mstat;
760
761 char going_name[MAX_SEGMENT_PATH_SIZE];
762 memset(going_name, 0, sizeof(going_name));
763 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
764 int ret = stat(going_name, &mstat);
765 DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret);
766 if (ret != 0) {
767 return DVR_FAILURE;
768 }
769 return DVR_SUCCESS;
770}
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800771loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800772{
773 Segment_Context_t *p_ctx;
774 char buf[256];
775 char value[256];
776 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800777 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800778 char *p1, *p2;
779
780 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800781 DVR_RETURN_IF_FALSE(p_ctx);
782 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
783 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800784
785 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800786 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800787 printf("start gets pts\n");
788 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
789 printf("buf[%s]\n", buf);
790 memset(value, 0, sizeof(value));
791 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800792 p1 += 5;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800793 if ((p2 = strstr(buf, ","))) {
794 memcpy(value, p1, p2 - p1);
795 }
796 pts = strtoull(value, NULL, 10);
797 }
798
799 memset(value, 0, sizeof(value));
800 if ((p1 = strstr(buf, "offset="))) {
801 p1 += 7;
802 if ((p2 = strstr(buf, "}"))) {
803 memcpy(value, p1, p2 - p1);
804 }
805 offset = strtoull(value, NULL, 10);
806 }
807
808 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800809 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800810 }
811
812 return 0;
813}