blob: b338294520aee52f464a8ed567d9f8af0ba89426 [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 */
Pengfei Liuc181a982020-01-07 19:27:13 +080032} Segment_Context_t;
33
Pengfei Liub4734232020-01-17 18:25:10 +080034/**\brief Segment file type*/
Pengfei Liuc181a982020-01-07 19:27:13 +080035typedef enum {
Pengfei Liub4734232020-01-17 18:25:10 +080036 SEGMENT_FILE_TYPE_TS, /**< Used for store TS data*/
37 SEGMENT_FILE_TYPE_INDEX, /**< Used for store index data*/
38 SEGMENT_FILE_TYPE_DAT, /**< Used for store information data, such as duration etc*/
pengfei.liu567d6d82020-04-17 16:48:59 +080039 SEGMENT_FILE_TYPE_ONGOING, /**< Used for store information data, such as duration etc*/
Pengfei Liuc181a982020-01-07 19:27:13 +080040} Segment_FileType_t;
41
42static void segment_get_fname(char fname[MAX_SEGMENT_PATH_SIZE],
Pengfei Liub4734232020-01-17 18:25:10 +080043 const char location[DVR_MAX_LOCATION_SIZE],
Pengfei Liuc181a982020-01-07 19:27:13 +080044 uint64_t segment_id,
45 Segment_FileType_t type)
46{
47 int offset;
48
49 memset(fname, 0, MAX_SEGMENT_PATH_SIZE);
50 strncpy(fname, location, strlen(location));
51 offset = strlen(location);
52 strncpy(fname + offset, "-", 1);
53 offset += 1;
54 sprintf(fname + offset, "%04llu", segment_id);
55 offset += 4;
56 if (type == SEGMENT_FILE_TYPE_TS)
57 strncpy(fname + offset, ".ts", 3);
58 else if (type == SEGMENT_FILE_TYPE_INDEX)
59 strncpy(fname + offset, ".idx", 4);
Pengfei Liub4734232020-01-17 18:25:10 +080060 else if (type == SEGMENT_FILE_TYPE_DAT)
61 strncpy(fname + offset, ".dat", 4);
hualing chen87072a82020-03-12 16:20:12 +080062 else if (type == SEGMENT_FILE_TYPE_ONGOING)
63 strncpy(fname + offset, ".going", 6);
Pengfei Liuc181a982020-01-07 19:27:13 +080064}
65
Pengfei Liu3b1a8202020-02-12 23:04:21 +080066static void segment_get_dirname(char dir_name[MAX_SEGMENT_PATH_SIZE],
67 const char location[DVR_MAX_LOCATION_SIZE])
68{
69 char *p;
70 int i;
71 int found = 0;
72
73 for (i = 0; i < (int)strlen(location); i++) {
74 if (location[i] == '/') {
75 p = (char *)location + i;
76 found = 1;
77 }
78 }
79 if (found)
80 memcpy(dir_name, location, p - location);
81}
82
Pengfei Liuc181a982020-01-07 19:27:13 +080083int segment_open(Segment_OpenParams_t *params, Segment_Handle_t *p_handle)
84{
85 Segment_Context_t *p_ctx;
86 char ts_fname[MAX_SEGMENT_PATH_SIZE];
87 char index_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liub4734232020-01-17 18:25:10 +080088 char dat_fname[MAX_SEGMENT_PATH_SIZE];
Pengfei Liu3b1a8202020-02-12 23:04:21 +080089 char dir_name[MAX_SEGMENT_PATH_SIZE];
hualing chen87072a82020-03-12 16:20:12 +080090 char going_name[MAX_SEGMENT_PATH_SIZE];
Pengfei Liuc181a982020-01-07 19:27:13 +080091
Pengfei Liu3b1a8202020-02-12 23:04:21 +080092 DVR_RETURN_IF_FALSE(params);
93 DVR_RETURN_IF_FALSE(p_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +080094
hualing chena540a7e2020-03-27 16:44:05 +080095 //DVR_DEBUG(1, "%s, location:%s, id:%llu", __func__, params->location, params->segment_id);
Pengfei Liuc181a982020-01-07 19:27:13 +080096
Pengfei Liub038b6a2020-01-14 15:57:01 +080097 p_ctx = (void*)malloc(sizeof(Segment_Context_t));
Pengfei Liu3b1a8202020-02-12 23:04:21 +080098 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +080099 memset(p_ctx, 0, sizeof(Segment_Context_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800100
101 memset(ts_fname, 0, sizeof(ts_fname));
102 segment_get_fname(ts_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_TS);
103
104 memset(index_fname, 0, sizeof(index_fname));
105 segment_get_fname(index_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_INDEX);
106
Pengfei Liub4734232020-01-17 18:25:10 +0800107 memset(dat_fname, 0, sizeof(dat_fname));
108 segment_get_fname(dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_DAT);
109
hualing chen87072a82020-03-12 16:20:12 +0800110 memset(going_name, 0, sizeof(going_name));
111 segment_get_fname(going_name, params->location, params->segment_id, SEGMENT_FILE_TYPE_ONGOING);
112
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800113 memset(dir_name, 0, sizeof(dir_name));
114 segment_get_dirname(dir_name, params->location);
115 if (access(dir_name, F_OK) == -1) {
116 DVR_DEBUG(1, "%s dir %s is not exist, create it", __func__, dir_name);
117 mkdir(dir_name, 0666);
118 }
119
Pengfei Liuc181a982020-01-07 19:27:13 +0800120 if (params->mode == SEGMENT_MODE_READ) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800121 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800122 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800123 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800124 p_ctx->ongoing_fp = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800125 } else if (params->mode == SEGMENT_MODE_WRITE) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800126 p_ctx->ts_fd = open(ts_fname, O_CREAT | O_RDWR | O_TRUNC, 0644);
Pengfei Liuc181a982020-01-07 19:27:13 +0800127 p_ctx->index_fp = fopen(index_fname, "w+");
Pengfei Liub4734232020-01-17 18:25:10 +0800128 p_ctx->dat_fp = fopen(dat_fname, "w+");
hualing chen87072a82020-03-12 16:20:12 +0800129 p_ctx->ongoing_fp = fopen(going_name, "w+");
Pengfei Liub038b6a2020-01-14 15:57:01 +0800130 p_ctx->first_pts = ULLONG_MAX;
131 p_ctx->last_pts = ULLONG_MAX;
pengfei.liu567d6d82020-04-17 16:48:59 +0800132 p_ctx->last_record_pts = ULLONG_MAX;
Pengfei Liuc181a982020-01-07 19:27:13 +0800133 } else {
134 DVR_DEBUG(1, "%s, unknow mode use default", __func__);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800135 p_ctx->ts_fd = open(ts_fname, O_RDONLY);
Pengfei Liuc181a982020-01-07 19:27:13 +0800136 p_ctx->index_fp = fopen(index_fname, "r");
Pengfei Liub4734232020-01-17 18:25:10 +0800137 p_ctx->dat_fp = fopen(dat_fname, "r");
hualing chen87072a82020-03-12 16:20:12 +0800138 p_ctx->ongoing_fp = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800139 }
140
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800141 if (p_ctx->ts_fd == -1 || !p_ctx->index_fp || !p_ctx->dat_fp) {
Pengfei Liub4734232020-01-17 18:25:10 +0800142 DVR_DEBUG(1, "%s open file failed [%s, %s, %s], reason:%s", __func__,
143 ts_fname, index_fname, dat_fname, strerror(errno));
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800144 if (p_ctx->ts_fd != -1)
145 close(p_ctx->ts_fd);
146 if (p_ctx->index_fp)
147 fclose(p_ctx->index_fp);
148 if (p_ctx->dat_fp)
149 fclose(p_ctx->dat_fp);
150 if (p_ctx->ongoing_fp)
151 fclose(p_ctx->ongoing_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800152 free(p_ctx);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800153 *p_handle = NULL;
Pengfei Liuc181a982020-01-07 19:27:13 +0800154 return DVR_FAILURE;
155 }
hualing chen87072a82020-03-12 16:20:12 +0800156 p_ctx->segment_id = params->segment_id;
157 strncpy(p_ctx->location, params->location, strlen(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800158
hualing chen7a56cba2020-04-14 14:09:27 +0800159 //DVR_DEBUG(1, "%s, open file success p_ctx->location [%s]", __func__, p_ctx->location, params->mode);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800160 *p_handle = (Segment_Handle_t)p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800161 return DVR_SUCCESS;
162}
163
164int segment_close(Segment_Handle_t handle)
165{
166 Segment_Context_t *p_ctx;
167
Pengfei Liub038b6a2020-01-14 15:57:01 +0800168 p_ctx = (void *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800169 DVR_RETURN_IF_FALSE(p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800170
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800171 if (p_ctx->ts_fd != -1) {
172 close(p_ctx->ts_fd);
Pengfei Liuc181a982020-01-07 19:27:13 +0800173 }
174
175 if (p_ctx->index_fp) {
176 fclose(p_ctx->index_fp);
177 }
178
Pengfei Liub4734232020-01-17 18:25:10 +0800179 if (p_ctx->dat_fp) {
180 fclose(p_ctx->dat_fp);
181 }
182
hualing chen87072a82020-03-12 16:20:12 +0800183 if (p_ctx->ongoing_fp != NULL) {
184 fclose(p_ctx->ongoing_fp);
185 char going_name[MAX_SEGMENT_PATH_SIZE];
186 memset(going_name, 0, sizeof(going_name));
187 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
188 DVR_DEBUG(1, "segment close del [%s]", going_name);
189 unlink(going_name);
190 }
191
Pengfei Liuc181a982020-01-07 19:27:13 +0800192 free(p_ctx);
193 return 0;
194}
195
196ssize_t segment_read(Segment_Handle_t handle, void *buf, size_t count)
197{
198 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800199 ssize_t len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800200 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800201 DVR_RETURN_IF_FALSE(p_ctx);
202 DVR_RETURN_IF_FALSE(buf);
203 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800204 len = read(p_ctx->ts_fd, buf, count);
205 return len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800206}
207
208ssize_t segment_write(Segment_Handle_t handle, void *buf, size_t count)
209{
210 Segment_Context_t *p_ctx;
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800211 ssize_t len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800212 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800213 DVR_RETURN_IF_FALSE(p_ctx);
214 DVR_RETURN_IF_FALSE(buf);
215 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Chuanzhi Wangccecb8d2020-12-02 11:22:54 +0800216 len = write(p_ctx->ts_fd, buf, count);
217 fsync(p_ctx->ts_fd);
218 return len;
Pengfei Liuc181a982020-01-07 19:27:13 +0800219}
220
hualing chen2932d372020-04-29 13:44:00 +0800221int segment_update_pts_force(Segment_Handle_t handle, uint64_t pts, loff_t offset)
Pengfei Liuc181a982020-01-07 19:27:13 +0800222{
223 Segment_Context_t *p_ctx;
224 char buf[256];
pengfei.liu567d6d82020-04-17 16:48:59 +0800225 int record_diff = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800226
227 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800228 DVR_RETURN_IF_FALSE(p_ctx);
229 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
Pengfei Liuc181a982020-01-07 19:27:13 +0800230
Pengfei Liub038b6a2020-01-14 15:57:01 +0800231 if (p_ctx->first_pts == ULLONG_MAX) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800232 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800233 p_ctx->first_pts = pts;
234 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800235 memset(buf, 0, sizeof(buf));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800236 if (p_ctx->last_pts == ULLONG_MAX) {
237 /*Last pts is init value*/
hualing chen2aba4022020-03-02 13:49:55 +0800238 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
pengfei.liuab5a2262020-02-14 17:33:40 +0800239 p_ctx->cur_time = pts - p_ctx->first_pts;
hualing chen2932d372020-04-29 13:44:00 +0800240 DVR_DEBUG(1, "%s force pcr:%llu -1", __func__, pts);
241 } else {
242 /*Last pts has valid value*/
243 int diff = pts - p_ctx->last_pts;
244 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
245 /*Current pts has a transition*/
246 DVR_DEBUG(1, "[%s]force update Current pts has a transition, [%llu, %llu, %llu]",__func__,
247 p_ctx->first_pts, p_ctx->last_pts, pts);
248 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
249 } else {
250 /*This is a normal pts, record it*/
251 p_ctx->cur_time += diff;
252 DVR_DEBUG(1, "%s force pcr:%llu -1 diff [%d]", __func__, pts, diff);
253 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
254 }
255 }
256
257 record_diff = pts - p_ctx->last_record_pts;
258 if (strlen(buf) > 0) {
259 DVR_DEBUG(1, "%s force pcr:%llu buf:%s", __func__, pts, buf);
260 fputs(buf, p_ctx->index_fp);
261 fflush(p_ctx->index_fp);
262 fsync(fileno(p_ctx->index_fp));
263 p_ctx->last_record_pts = pts;
264 }
265 p_ctx->last_pts = pts;
266
267 return DVR_SUCCESS;
268}
269
270int segment_update_pts(Segment_Handle_t handle, uint64_t pts, loff_t offset)
271{
272 Segment_Context_t *p_ctx;
273 char buf[256];
274 int record_diff = 0;
275
276 p_ctx = (Segment_Context_t *)handle;
277 DVR_RETURN_IF_FALSE(p_ctx);
278 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
279
280 if (p_ctx->first_pts == ULLONG_MAX) {
281 DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts);
282 p_ctx->first_pts = pts;
283 //p_ctx->cur_time = p_ctx->cur_time + PTS_HEAD_DEVIATION;
284 }
285 memset(buf, 0, sizeof(buf));
286 if (p_ctx->last_pts == ULLONG_MAX) {
287 /*Last pts is init value*/
288 sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset);
289 p_ctx->cur_time = pts - p_ctx->first_pts;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800290 } else {
291 /*Last pts has valid value*/
pengfei.liu567d6d82020-04-17 16:48:59 +0800292 int diff = pts - p_ctx->last_pts;
hualing chen4b7c15d2020-04-07 16:13:48 +0800293 if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) {
Pengfei Liub038b6a2020-01-14 15:57:01 +0800294 /*Current pts has a transition*/
pengfei.liuab5a2262020-02-14 17:33:40 +0800295 DVR_DEBUG(1, "Current pts has a transition, [%llu, %llu, %llu]",
296 p_ctx->first_pts, p_ctx->last_pts, pts);
pengfei.liu567d6d82020-04-17 16:48:59 +0800297 p_ctx->last_record_pts = pts;
hualing chen2932d372020-04-29 13:44:00 +0800298 //p_ctx->cur_time = p_ctx->cur_time + PTS_DISCONTINE_DEVIATION;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800299 } else {
300 /*This is a normal pts, record it*/
hualing chen4b7c15d2020-04-07 16:13:48 +0800301 p_ctx->cur_time += diff;
hualing chen2aba4022020-03-02 13:49:55 +0800302 sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800303 }
304 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800305
306 record_diff = pts - p_ctx->last_record_pts;
307 if (strlen(buf) > 0 &&
308 (record_diff > PCR_RECORD_INTERVAL_MS || p_ctx->last_record_pts == ULLONG_MAX)){
hualing chen4b7c15d2020-04-07 16:13:48 +0800309 fputs(buf, p_ctx->index_fp);
pengfei.liu567d6d82020-04-17 16:48:59 +0800310 fflush(p_ctx->index_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800311 fsync(fileno(p_ctx->index_fp));
pengfei.liu567d6d82020-04-17 16:48:59 +0800312 p_ctx->last_record_pts = pts;
313 }
Pengfei Liub038b6a2020-01-14 15:57:01 +0800314 p_ctx->last_pts = pts;
Pengfei Liub4734232020-01-17 18:25:10 +0800315
Pengfei Liuc181a982020-01-07 19:27:13 +0800316 return DVR_SUCCESS;
317}
318
hualing chen266b9502020-04-04 17:39:39 +0800319loff_t segment_seek(Segment_Handle_t handle, uint64_t time, int block_size)
Pengfei Liuc181a982020-01-07 19:27:13 +0800320{
321 Segment_Context_t *p_ctx;
322 char buf[256];
323 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800324 uint64_t pts = 0L;
325 loff_t offset = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800326 char *p1, *p2;
327
328 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800329 DVR_RETURN_IF_FALSE(p_ctx);
330 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
331 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800332
hualing chen266b9502020-04-04 17:39:39 +0800333 if (time == 0) {
334 offset = 0;
hualing chend241c7a2021-06-22 13:34:27 +0800335 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
336 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen266b9502020-04-04 17:39:39 +0800337 return offset;
338 }
339
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800340 memset(buf, 0, sizeof(buf));
341 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chen2aba4022020-03-02 13:49:55 +0800342 int line = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800343 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800344 line++;
Pengfei Liuc181a982020-01-07 19:27:13 +0800345 memset(value, 0, sizeof(value));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800346 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800347 p1 += 5;
Pengfei Liuc181a982020-01-07 19:27:13 +0800348 if ((p2 = strstr(buf, ","))) {
349 memcpy(value, p1, p2 - p1);
350 }
351 pts = strtoull(value, NULL, 10);
352 }
353
354 memset(value, 0, sizeof(value));
355 if ((p1 = strstr(buf, "offset="))) {
356 p1 += 7;
357 if ((p2 = strstr(buf, "}"))) {
358 memcpy(value, p1, p2 - p1);
359 }
360 offset = strtoull(value, NULL, 10);
361 }
hualing chen2aba4022020-03-02 13:49:55 +0800362 if (0)
363 {
364 DVR_DEBUG(1, "seek buf[%s]", buf);
365 DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
366 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800367 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800368 if (time <= pts) {
hualing chen266b9502020-04-04 17:39:39 +0800369 if (block_size > 0) {
370 offset = offset - offset%block_size;
371 }
hualing chend241c7a2021-06-22 13:34:27 +0800372 //DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line);
373 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800374 return offset;
375 }
hualing chen2aba4022020-03-02 13:49:55 +0800376 }
hualing chen2932d372020-04-29 13:44:00 +0800377 if (time > pts) {
378 if (block_size > 0) {
379 offset = offset - offset%block_size;
380 }
381 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 +0800382 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen2932d372020-04-29 13:44:00 +0800383 return offset;
384 }
hualing chen2aba4022020-03-02 13:49:55 +0800385 DVR_DEBUG(1, "seek error line [%d]", line);
Pengfei Liuc181a982020-01-07 19:27:13 +0800386 return DVR_FAILURE;
387}
388
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800389loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800390{
391 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800392 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800393 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800394 DVR_RETURN_IF_FALSE(p_ctx);
395 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800396 pos = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
397 return pos;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800398}
399
hualing chen5605eed2020-05-26 18:18:06 +0800400uint64_t segment_tell_position_time(Segment_Handle_t handle, loff_t position)
401{
402 Segment_Context_t *p_ctx;
403 char buf[256];
404 char value[256];
hualing chen969fe7b2021-05-26 15:13:17 +0800405 uint64_t ret = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800406 uint64_t pts = 0L;
hualing chen969fe7b2021-05-26 15:13:17 +0800407 uint64_t pts_p = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800408 loff_t offset = 0;
hualing chen969fe7b2021-05-26 15:13:17 +0800409 loff_t offset_p = 0;
hualing chen5605eed2020-05-26 18:18:06 +0800410 char *p1, *p2;
411
412 p_ctx = (Segment_Context_t *)handle;
413 DVR_RETURN_IF_FALSE(p_ctx);
414 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
415 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
416
417 memset(buf, 0, sizeof(buf));
418 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
419 DVR_RETURN_IF_FALSE(position != -1);
420
421 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
422 memset(value, 0, sizeof(value));
423 if ((p1 = strstr(buf, "time="))) {
424 p1 += 5;
425 if ((p2 = strstr(buf, ","))) {
426 memcpy(value, p1, p2 - p1);
427 }
428 pts = strtoull(value, NULL, 10);
429 }
430
431 memset(value, 0, sizeof(value));
432 if ((p1 = strstr(buf, "offset="))) {
433 p1 += 7;
434 if ((p2 = strstr(buf, "}"))) {
435 memcpy(value, p1, p2 - p1);
436 }
437 offset = strtoull(value, NULL, 10);
438 }
439
440 memset(buf, 0, sizeof(buf));
441 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chend241c7a2021-06-22 13:34:27 +0800442 if (position <= offset
443 &&position >= offset_p
444 && offset - offset_p > 0) {
hualing chen969fe7b2021-05-26 15:13:17 +0800445 ret = pts_p + (pts - pts_p) * (position - offset_p) / (offset - offset_p);
hualing chend241c7a2021-06-22 13:34:27 +0800446 //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 +0800447 return ret;
hualing chen5605eed2020-05-26 18:18:06 +0800448 }
hualing chen969fe7b2021-05-26 15:13:17 +0800449 offset_p = offset;
450 pts_p = pts;
hualing chen5605eed2020-05-26 18:18:06 +0800451 }
452 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
453 return pts;
454}
455
456
pengfei.liu8b563292020-02-26 15:49:02 +0800457uint64_t segment_tell_current_time(Segment_Handle_t handle)
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800458{
459 Segment_Context_t *p_ctx;
460 char buf[256];
461 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800462 uint64_t pts = 0L;
463 loff_t offset = 0, position = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800464 char *p1, *p2;
465
466 p_ctx = (Segment_Context_t *)handle;
467 DVR_RETURN_IF_FALSE(p_ctx);
468 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
469 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
470
471 memset(buf, 0, sizeof(buf));
472 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800473 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800474 DVR_RETURN_IF_FALSE(position != -1);
475
476 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
477 memset(value, 0, sizeof(value));
478 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800479 p1 += 5;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800480 if ((p2 = strstr(buf, ","))) {
481 memcpy(value, p1, p2 - p1);
482 }
483 pts = strtoull(value, NULL, 10);
484 }
485
486 memset(value, 0, sizeof(value));
487 if ((p1 = strstr(buf, "offset="))) {
488 p1 += 7;
489 if ((p2 = strstr(buf, "}"))) {
490 memcpy(value, p1, p2 - p1);
491 }
492 offset = strtoull(value, NULL, 10);
493 }
494
495 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800496 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800497 if (position <= offset) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800498 return pts;
499 }
500 }
hualing chena540a7e2020-03-27 16:44:05 +0800501 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800502 return pts;
Pengfei Liuc181a982020-01-07 19:27:13 +0800503}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800504
pengfei.liu8b563292020-02-26 15:49:02 +0800505uint64_t segment_tell_total_time(Segment_Handle_t handle)
506{
507 Segment_Context_t *p_ctx;
508 char buf[256];
509 char last_buf[256];
510 char value[256];
511 uint64_t pts = ULLONG_MAX;
512 loff_t offset = 0, position = 0;
513 char *p1, *p2;
514 int line = 0;
515
516 p_ctx = (Segment_Context_t *)handle;
517 DVR_RETURN_IF_FALSE(p_ctx);
518 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
519 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
520
521 memset(buf, 0, sizeof(buf));
522 memset(last_buf, 0, sizeof(last_buf));
hualing chend241c7a2021-06-22 13:34:27 +0800523 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
pengfei.liu8b563292020-02-26 15:49:02 +0800524 DVR_RETURN_IF_FALSE(position != -1);
525
hualing chen041c4092020-04-05 15:11:50 +0800526 //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1);
527 //if seek error.we need seek 0 pos.
528 if (fseek(p_ctx->index_fp, -1000L, SEEK_END) == -1) {
529 fseek(p_ctx->index_fp, 0L, SEEK_SET);
530 }
pengfei.liu8b563292020-02-26 15:49:02 +0800531 /* Save last line buffer */
532 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800533 if (strlen(buf) <= 0) {
hualing chen87072a82020-03-12 16:20:12 +0800534 DVR_DEBUG(1, "read index buf is len 0");
hualing chen2aba4022020-03-02 13:49:55 +0800535 continue;
536 }
pengfei.liu8b563292020-02-26 15:49:02 +0800537 memset(last_buf, 0, sizeof(last_buf));
538 memcpy(last_buf, buf, strlen(buf));
539 memset(buf, 0, sizeof(buf));
540 line++;
541 }
542
543 /* Extract time value */
544 memset(value, 0, sizeof(value));
545 if ((p1 = strstr(last_buf, "time="))) {
546 p1 += 5;
547 if ((p2 = strstr(last_buf, ","))) {
548 memcpy(value, p1, p2 - p1);
549 }
550 pts = strtoull(value, NULL, 10);
551 }
552
553 memset(value, 0, sizeof(value));
554 if ((p1 = strstr(last_buf, "offset="))) {
555 p1 += 7;
556 if ((p2 = strstr(last_buf, "}"))) {
557 memcpy(value, p1, p2 - p1);
558 }
559 offset = strtoull(value, NULL, 10);
560 }
hualing chen87072a82020-03-12 16:20:12 +0800561 //if (line < 2)
562 //DVR_DEBUG(1, "totle time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line);
pengfei.liu8b563292020-02-26 15:49:02 +0800563 return (pts == ULLONG_MAX ? DVR_FAILURE : pts);
564}
565
pengfei.liuab5a2262020-02-14 17:33:40 +0800566/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800567int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
568{
569 Segment_Context_t *p_ctx;
570 char buf[256];
571 uint32_t i;
572
573 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800574 DVR_RETURN_IF_FALSE(p_ctx);
575 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
576 DVR_RETURN_IF_FALSE(p_info);
hualing chen87072a82020-03-12 16:20:12 +0800577 //seek 0, rewrite info
578 DVR_RETURN_IF_FALSE(fseek(p_ctx->dat_fp, 0, SEEK_SET) != -1);
Pengfei Liub4734232020-01-17 18:25:10 +0800579
580 /*Save segment id*/
581 memset(buf, 0, sizeof(buf));
582 sprintf(buf, "id=%lld\n", p_info->id);
583 fputs(buf, p_ctx->dat_fp);
584
585 /*Save number of pids*/
586 memset(buf, 0, sizeof(buf));
587 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
588 fputs(buf, p_ctx->dat_fp);
589
590 /*Save pid information*/
591 for (i = 0; i < p_info->nb_pids; i++) {
592 memset(buf, 0, sizeof(buf));
593 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
594 fputs(buf, p_ctx->dat_fp);
595 }
596
597 /*Save segment duration*/
598 memset(buf, 0, sizeof(buf));
hualing chen87072a82020-03-12 16:20:12 +0800599 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800600 sprintf(buf, "duration=%ld\n", p_info->duration);
601 fputs(buf, p_ctx->dat_fp);
602
603 /*Save segment size*/
604 memset(buf, 0, sizeof(buf));
605 sprintf(buf, "size=%zu\n", p_info->size);
606 fputs(buf, p_ctx->dat_fp);
607
608 /*Save number of packets*/
609 memset(buf, 0, sizeof(buf));
610 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
611 fputs(buf, p_ctx->dat_fp);
612
613 fflush(p_ctx->dat_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800614 fsync(fileno(p_ctx->dat_fp));
Pengfei Liub4734232020-01-17 18:25:10 +0800615 return DVR_SUCCESS;
616}
617
pengfei.liuab5a2262020-02-14 17:33:40 +0800618/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800619int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
620{
621 Segment_Context_t *p_ctx;
622 uint32_t i;
623 char buf[256];
624 char value[256];
625 char *p1, *p2;
626
627 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800628 DVR_RETURN_IF_FALSE(p_ctx);
629 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800630
631 /*Load segment id*/
632 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800633 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800634 p1 = strstr(buf, "id=");
hualing chen2aba4022020-03-02 13:49:55 +0800635 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800636 p_info->id = strtoull(p1 + 3, NULL, 10);
637
638 /*Save number of pids*/
639 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800640 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800641 p1 = strstr(buf, "nb_pids=");
hualing chen2aba4022020-03-02 13:49:55 +0800642 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800643 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
644
645 /*Save pid information*/
646 for (i = 0; i < p_info->nb_pids; i++) {
647 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800648 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800649 memset(value, 0, sizeof(value));
650 if ((p1 = strstr(buf, "pid="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800651 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800652 p1 += 4;
653 if ((p2 = strstr(buf, ","))) {
hualing chen2aba4022020-03-02 13:49:55 +0800654 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800655 memcpy(value, p1, p2 - p1);
656 }
657 p_info->pids[i].pid = strtoull(value, NULL, 10);
658 }
659
660 memset(value, 0, sizeof(value));
661 if ((p1 = strstr(buf, "type="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800662 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800663 p1 += 5;
664 if ((p2 = strstr(buf, "}"))) {
hualing chen2aba4022020-03-02 13:49:55 +0800665 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800666 memcpy(value, p1, p2 - p1);
667 }
668 p_info->pids[i].type = strtoull(value, NULL, 10);
669 }
670 }
671
672 /*Save segment duration*/
673 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800674 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800675 p1 = strstr(buf, "duration=");
hualing chen2aba4022020-03-02 13:49:55 +0800676 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800677 p_info->duration = strtoull(p1 + 9, NULL, 10);
hualing chena540a7e2020-03-27 16:44:05 +0800678 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800679
680 /*Save segment size*/
681 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800682 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800683 p1 = strstr(buf, "size=");
hualing chen2aba4022020-03-02 13:49:55 +0800684 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800685 p_info->size = strtoull(p1 + 5, NULL, 10);
686
687 /*Save number of packets*/
688 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800689 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800690 p1 = strstr(buf, "nb_packets=");
hualing chen2aba4022020-03-02 13:49:55 +0800691 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800692 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
693
694 return DVR_SUCCESS;
695}
696
697int segment_delete(const char *location, uint64_t segment_id)
698{
699 char fname[MAX_SEGMENT_PATH_SIZE];
700 int ret;
701
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800702 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800703
704 /*delete ts file*/
705 memset(fname, 0, sizeof(fname));
706 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
707 ret = unlink(fname);
708 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800709 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800710
711 /*delete index file*/
712 memset(fname, 0, sizeof(fname));
713 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
714 unlink(fname);
715 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800716 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800717
718 /*delete store information file*/
719 memset(fname, 0, sizeof(fname));
720 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
721 unlink(fname);
722 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800723 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800724
725 return DVR_SUCCESS;
726}
727
hualing chen87072a82020-03-12 16:20:12 +0800728int segment_ongoing(Segment_Handle_t handle)
729{
730 Segment_Context_t *p_ctx;
731 p_ctx = (Segment_Context_t *)handle;
732 struct stat mstat;
733
734 char going_name[MAX_SEGMENT_PATH_SIZE];
735 memset(going_name, 0, sizeof(going_name));
736 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
737 int ret = stat(going_name, &mstat);
738 DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret);
739 if (ret != 0) {
740 return DVR_FAILURE;
741 }
742 return DVR_SUCCESS;
743}
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800744loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800745{
746 Segment_Context_t *p_ctx;
747 char buf[256];
748 char value[256];
749 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800750 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800751 char *p1, *p2;
752
753 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800754 DVR_RETURN_IF_FALSE(p_ctx);
755 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
756 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800757
758 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800759 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800760 printf("start gets pts\n");
761 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
762 printf("buf[%s]\n", buf);
763 memset(value, 0, sizeof(value));
764 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800765 p1 += 5;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800766 if ((p2 = strstr(buf, ","))) {
767 memcpy(value, p1, p2 - p1);
768 }
769 pts = strtoull(value, NULL, 10);
770 }
771
772 memset(value, 0, sizeof(value));
773 if ((p1 = strstr(buf, "offset="))) {
774 p1 += 7;
775 if ((p2 = strstr(buf, "}"))) {
776 memcpy(value, p1, p2 - p1);
777 }
778 offset = strtoull(value, NULL, 10);
779 }
780
781 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800782 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800783 }
784
785 return 0;
786}