blob: 886be0dc62e7a15fcb3e0d39b25f4f8b055072e3 [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
hualing chen8a657f32021-08-30 13:12:49 +0800328 DVR_DEBUG(1, "into seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
329
Pengfei Liuc181a982020-01-07 19:27:13 +0800330 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800331 DVR_RETURN_IF_FALSE(p_ctx);
332 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
333 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800334
hualing chen266b9502020-04-04 17:39:39 +0800335 if (time == 0) {
336 offset = 0;
hualing chend241c7a2021-06-22 13:34:27 +0800337 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
338 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen266b9502020-04-04 17:39:39 +0800339 return offset;
340 }
341
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800342 memset(buf, 0, sizeof(buf));
343 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chen2aba4022020-03-02 13:49:55 +0800344 int line = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800345 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800346 line++;
Pengfei Liuc181a982020-01-07 19:27:13 +0800347 memset(value, 0, sizeof(value));
Pengfei Liub038b6a2020-01-14 15:57:01 +0800348 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800349 p1 += 5;
Pengfei Liuc181a982020-01-07 19:27:13 +0800350 if ((p2 = strstr(buf, ","))) {
351 memcpy(value, p1, p2 - p1);
352 }
353 pts = strtoull(value, NULL, 10);
354 }
355
356 memset(value, 0, sizeof(value));
357 if ((p1 = strstr(buf, "offset="))) {
358 p1 += 7;
359 if ((p2 = strstr(buf, "}"))) {
360 memcpy(value, p1, p2 - p1);
361 }
362 offset = strtoull(value, NULL, 10);
363 }
hualing chen2aba4022020-03-02 13:49:55 +0800364 if (0)
365 {
366 DVR_DEBUG(1, "seek buf[%s]", buf);
367 DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
368 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800369 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800370 if (time <= pts) {
hualing chen266b9502020-04-04 17:39:39 +0800371 if (block_size > 0) {
372 offset = offset - offset%block_size;
373 }
hualing chend241c7a2021-06-22 13:34:27 +0800374 //DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line);
375 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800376 return offset;
377 }
hualing chen2aba4022020-03-02 13:49:55 +0800378 }
hualing chen2932d372020-04-29 13:44:00 +0800379 if (time > pts) {
380 if (block_size > 0) {
381 offset = offset - offset%block_size;
382 }
383 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 +0800384 DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1);
hualing chen2932d372020-04-29 13:44:00 +0800385 return offset;
386 }
hualing chen2aba4022020-03-02 13:49:55 +0800387 DVR_DEBUG(1, "seek error line [%d]", line);
Pengfei Liuc181a982020-01-07 19:27:13 +0800388 return DVR_FAILURE;
389}
390
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800391loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800392{
393 Segment_Context_t *p_ctx;
hualing chend241c7a2021-06-22 13:34:27 +0800394 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800395 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800396 DVR_RETURN_IF_FALSE(p_ctx);
397 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800398 pos = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
399 return pos;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800400}
401
hualing chen5605eed2020-05-26 18:18:06 +0800402uint64_t segment_tell_position_time(Segment_Handle_t handle, loff_t position)
403{
404 Segment_Context_t *p_ctx;
405 char buf[256];
406 char value[256];
hualing chen969fe7b2021-05-26 15:13:17 +0800407 uint64_t ret = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800408 uint64_t pts = 0L;
hualing chen969fe7b2021-05-26 15:13:17 +0800409 uint64_t pts_p = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800410 loff_t offset = 0;
hualing chen969fe7b2021-05-26 15:13:17 +0800411 loff_t offset_p = 0;
hualing chen5605eed2020-05-26 18:18:06 +0800412 char *p1, *p2;
413
414 p_ctx = (Segment_Context_t *)handle;
415 DVR_RETURN_IF_FALSE(p_ctx);
416 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
417 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
418
419 memset(buf, 0, sizeof(buf));
420 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
421 DVR_RETURN_IF_FALSE(position != -1);
422
423 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
424 memset(value, 0, sizeof(value));
425 if ((p1 = strstr(buf, "time="))) {
426 p1 += 5;
427 if ((p2 = strstr(buf, ","))) {
428 memcpy(value, p1, p2 - p1);
429 }
430 pts = strtoull(value, NULL, 10);
431 }
432
433 memset(value, 0, sizeof(value));
434 if ((p1 = strstr(buf, "offset="))) {
435 p1 += 7;
436 if ((p2 = strstr(buf, "}"))) {
437 memcpy(value, p1, p2 - p1);
438 }
439 offset = strtoull(value, NULL, 10);
440 }
441
442 memset(buf, 0, sizeof(buf));
443 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chend241c7a2021-06-22 13:34:27 +0800444 if (position <= offset
445 &&position >= offset_p
446 && offset - offset_p > 0) {
hualing chen969fe7b2021-05-26 15:13:17 +0800447 ret = pts_p + (pts - pts_p) * (position - offset_p) / (offset - offset_p);
hualing chend241c7a2021-06-22 13:34:27 +0800448 //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 +0800449 return ret;
hualing chen5605eed2020-05-26 18:18:06 +0800450 }
hualing chen969fe7b2021-05-26 15:13:17 +0800451 offset_p = offset;
452 pts_p = pts;
hualing chen5605eed2020-05-26 18:18:06 +0800453 }
454 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
455 return pts;
456}
457
458
pengfei.liu8b563292020-02-26 15:49:02 +0800459uint64_t segment_tell_current_time(Segment_Handle_t handle)
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800460{
461 Segment_Context_t *p_ctx;
462 char buf[256];
463 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800464 uint64_t pts = 0L;
465 loff_t offset = 0, position = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800466 char *p1, *p2;
467
468 p_ctx = (Segment_Context_t *)handle;
469 DVR_RETURN_IF_FALSE(p_ctx);
470 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
471 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
472
473 memset(buf, 0, sizeof(buf));
474 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
hualing chend241c7a2021-06-22 13:34:27 +0800475 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800476 DVR_RETURN_IF_FALSE(position != -1);
477
478 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
479 memset(value, 0, sizeof(value));
480 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800481 p1 += 5;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800482 if ((p2 = strstr(buf, ","))) {
483 memcpy(value, p1, p2 - p1);
484 }
485 pts = strtoull(value, NULL, 10);
486 }
487
488 memset(value, 0, sizeof(value));
489 if ((p1 = strstr(buf, "offset="))) {
490 p1 += 7;
491 if ((p2 = strstr(buf, "}"))) {
492 memcpy(value, p1, p2 - p1);
493 }
494 offset = strtoull(value, NULL, 10);
495 }
496
497 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800498 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800499 if (position <= offset) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800500 return pts;
501 }
502 }
hualing chena540a7e2020-03-27 16:44:05 +0800503 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800504 return pts;
Pengfei Liuc181a982020-01-07 19:27:13 +0800505}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800506
pengfei.liu8b563292020-02-26 15:49:02 +0800507uint64_t segment_tell_total_time(Segment_Handle_t handle)
508{
509 Segment_Context_t *p_ctx;
510 char buf[256];
511 char last_buf[256];
512 char value[256];
513 uint64_t pts = ULLONG_MAX;
514 loff_t offset = 0, position = 0;
515 char *p1, *p2;
516 int line = 0;
517
518 p_ctx = (Segment_Context_t *)handle;
519 DVR_RETURN_IF_FALSE(p_ctx);
520 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
521 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
522
523 memset(buf, 0, sizeof(buf));
524 memset(last_buf, 0, sizeof(last_buf));
hualing chend241c7a2021-06-22 13:34:27 +0800525 position = lseek(p_ctx->ts_fd, 0, SEEK_CUR);
pengfei.liu8b563292020-02-26 15:49:02 +0800526 DVR_RETURN_IF_FALSE(position != -1);
527
hualing chen041c4092020-04-05 15:11:50 +0800528 //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1);
529 //if seek error.we need seek 0 pos.
530 if (fseek(p_ctx->index_fp, -1000L, SEEK_END) == -1) {
531 fseek(p_ctx->index_fp, 0L, SEEK_SET);
532 }
pengfei.liu8b563292020-02-26 15:49:02 +0800533 /* Save last line buffer */
534 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800535 if (strlen(buf) <= 0) {
hualing chen87072a82020-03-12 16:20:12 +0800536 DVR_DEBUG(1, "read index buf is len 0");
hualing chen2aba4022020-03-02 13:49:55 +0800537 continue;
538 }
pengfei.liu8b563292020-02-26 15:49:02 +0800539 memset(last_buf, 0, sizeof(last_buf));
540 memcpy(last_buf, buf, strlen(buf));
541 memset(buf, 0, sizeof(buf));
542 line++;
543 }
544
545 /* Extract time value */
546 memset(value, 0, sizeof(value));
547 if ((p1 = strstr(last_buf, "time="))) {
548 p1 += 5;
549 if ((p2 = strstr(last_buf, ","))) {
550 memcpy(value, p1, p2 - p1);
551 }
552 pts = strtoull(value, NULL, 10);
553 }
554
555 memset(value, 0, sizeof(value));
556 if ((p1 = strstr(last_buf, "offset="))) {
557 p1 += 7;
558 if ((p2 = strstr(last_buf, "}"))) {
559 memcpy(value, p1, p2 - p1);
560 }
561 offset = strtoull(value, NULL, 10);
562 }
hualing chen87072a82020-03-12 16:20:12 +0800563 //if (line < 2)
564 //DVR_DEBUG(1, "totle time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line);
pengfei.liu8b563292020-02-26 15:49:02 +0800565 return (pts == ULLONG_MAX ? DVR_FAILURE : pts);
566}
567
pengfei.liuab5a2262020-02-14 17:33:40 +0800568/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800569int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
570{
571 Segment_Context_t *p_ctx;
572 char buf[256];
573 uint32_t i;
574
575 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800576 DVR_RETURN_IF_FALSE(p_ctx);
577 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
578 DVR_RETURN_IF_FALSE(p_info);
hualing chen87072a82020-03-12 16:20:12 +0800579 //seek 0, rewrite info
580 DVR_RETURN_IF_FALSE(fseek(p_ctx->dat_fp, 0, SEEK_SET) != -1);
Pengfei Liub4734232020-01-17 18:25:10 +0800581
582 /*Save segment id*/
583 memset(buf, 0, sizeof(buf));
584 sprintf(buf, "id=%lld\n", p_info->id);
585 fputs(buf, p_ctx->dat_fp);
586
587 /*Save number of pids*/
588 memset(buf, 0, sizeof(buf));
589 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
590 fputs(buf, p_ctx->dat_fp);
591
592 /*Save pid information*/
593 for (i = 0; i < p_info->nb_pids; i++) {
594 memset(buf, 0, sizeof(buf));
595 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
596 fputs(buf, p_ctx->dat_fp);
597 }
598
599 /*Save segment duration*/
600 memset(buf, 0, sizeof(buf));
hualing chen87072a82020-03-12 16:20:12 +0800601 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800602 sprintf(buf, "duration=%ld\n", p_info->duration);
603 fputs(buf, p_ctx->dat_fp);
604
605 /*Save segment size*/
606 memset(buf, 0, sizeof(buf));
607 sprintf(buf, "size=%zu\n", p_info->size);
608 fputs(buf, p_ctx->dat_fp);
609
610 /*Save number of packets*/
611 memset(buf, 0, sizeof(buf));
612 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
613 fputs(buf, p_ctx->dat_fp);
614
615 fflush(p_ctx->dat_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800616 fsync(fileno(p_ctx->dat_fp));
Pengfei Liub4734232020-01-17 18:25:10 +0800617 return DVR_SUCCESS;
618}
619
pengfei.liuab5a2262020-02-14 17:33:40 +0800620/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800621int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
622{
623 Segment_Context_t *p_ctx;
624 uint32_t i;
625 char buf[256];
626 char value[256];
627 char *p1, *p2;
628
629 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800630 DVR_RETURN_IF_FALSE(p_ctx);
631 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800632
633 /*Load segment id*/
634 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800635 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800636 p1 = strstr(buf, "id=");
hualing chen2aba4022020-03-02 13:49:55 +0800637 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800638 p_info->id = strtoull(p1 + 3, NULL, 10);
639
640 /*Save number of pids*/
641 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800642 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800643 p1 = strstr(buf, "nb_pids=");
hualing chen2aba4022020-03-02 13:49:55 +0800644 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800645 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
646
647 /*Save pid information*/
648 for (i = 0; i < p_info->nb_pids; i++) {
649 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800650 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800651 memset(value, 0, sizeof(value));
652 if ((p1 = strstr(buf, "pid="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800653 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800654 p1 += 4;
655 if ((p2 = strstr(buf, ","))) {
hualing chen2aba4022020-03-02 13:49:55 +0800656 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800657 memcpy(value, p1, p2 - p1);
658 }
659 p_info->pids[i].pid = strtoull(value, NULL, 10);
660 }
661
662 memset(value, 0, sizeof(value));
663 if ((p1 = strstr(buf, "type="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800664 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800665 p1 += 5;
666 if ((p2 = strstr(buf, "}"))) {
hualing chen2aba4022020-03-02 13:49:55 +0800667 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800668 memcpy(value, p1, p2 - p1);
669 }
670 p_info->pids[i].type = strtoull(value, NULL, 10);
671 }
672 }
673
674 /*Save segment duration*/
675 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800676 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800677 p1 = strstr(buf, "duration=");
hualing chen2aba4022020-03-02 13:49:55 +0800678 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800679 p_info->duration = strtoull(p1 + 9, NULL, 10);
hualing chena540a7e2020-03-27 16:44:05 +0800680 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800681
682 /*Save segment size*/
683 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800684 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800685 p1 = strstr(buf, "size=");
hualing chen2aba4022020-03-02 13:49:55 +0800686 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800687 p_info->size = strtoull(p1 + 5, NULL, 10);
688
689 /*Save number of packets*/
690 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800691 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800692 p1 = strstr(buf, "nb_packets=");
hualing chen2aba4022020-03-02 13:49:55 +0800693 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800694 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
695
696 return DVR_SUCCESS;
697}
698
699int segment_delete(const char *location, uint64_t segment_id)
700{
701 char fname[MAX_SEGMENT_PATH_SIZE];
702 int ret;
703
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800704 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800705
706 /*delete ts file*/
707 memset(fname, 0, sizeof(fname));
708 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
709 ret = unlink(fname);
710 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800711 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800712
713 /*delete index file*/
714 memset(fname, 0, sizeof(fname));
715 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
716 unlink(fname);
717 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800718 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800719
720 /*delete store information file*/
721 memset(fname, 0, sizeof(fname));
722 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
723 unlink(fname);
724 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800725 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800726
727 return DVR_SUCCESS;
728}
729
hualing chen87072a82020-03-12 16:20:12 +0800730int segment_ongoing(Segment_Handle_t handle)
731{
732 Segment_Context_t *p_ctx;
733 p_ctx = (Segment_Context_t *)handle;
734 struct stat mstat;
735
736 char going_name[MAX_SEGMENT_PATH_SIZE];
737 memset(going_name, 0, sizeof(going_name));
738 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
739 int ret = stat(going_name, &mstat);
740 DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret);
741 if (ret != 0) {
742 return DVR_FAILURE;
743 }
744 return DVR_SUCCESS;
745}
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800746loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800747{
748 Segment_Context_t *p_ctx;
749 char buf[256];
750 char value[256];
751 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800752 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800753 char *p1, *p2;
754
755 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800756 DVR_RETURN_IF_FALSE(p_ctx);
757 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
758 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800759
760 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800761 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800762 printf("start gets pts\n");
763 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
764 printf("buf[%s]\n", buf);
765 memset(value, 0, sizeof(value));
766 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800767 p1 += 5;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800768 if ((p2 = strstr(buf, ","))) {
769 memcpy(value, p1, p2 - p1);
770 }
771 pts = strtoull(value, NULL, 10);
772 }
773
774 memset(value, 0, sizeof(value));
775 if ((p1 = strstr(buf, "offset="))) {
776 p1 += 7;
777 if ((p2 = strstr(buf, "}"))) {
778 memcpy(value, p1, p2 - p1);
779 }
780 offset = strtoull(value, NULL, 10);
781 }
782
783 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800784 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800785 }
786
787 return 0;
788}