blob: fd1d9e7643858e4b5c8103d6d206d73b7a04fe5c [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;
199
200 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);
Pengfei Liuc181a982020-01-07 19:27:13 +0800204
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800205 return read(p_ctx->ts_fd, buf, count);
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 chen92f3a142020-07-08 20:59:33 +0800335 //DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time);
hualing chen266b9502020-04-04 17:39:39 +0800336 DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1);
337 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 //DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset);
369 if (time <= pts) {
hualing chen266b9502020-04-04 17:39:39 +0800370 if (block_size > 0) {
371 offset = offset - offset%block_size;
372 }
hualing chen2aba4022020-03-02 13:49:55 +0800373 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800374 DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800375 return offset;
376 }
hualing chen2aba4022020-03-02 13:49:55 +0800377 }
hualing chen2932d372020-04-29 13:44:00 +0800378 if (time > pts) {
379 if (block_size > 0) {
380 offset = offset - offset%block_size;
381 }
382 DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d end\n", pts, offset, time, line);
383 DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1);
384 return offset;
385 }
hualing chen2aba4022020-03-02 13:49:55 +0800386 DVR_DEBUG(1, "seek error line [%d]", line);
Pengfei Liuc181a982020-01-07 19:27:13 +0800387 return DVR_FAILURE;
388}
389
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800390loff_t segment_tell_position(Segment_Handle_t handle)
Pengfei Liuc181a982020-01-07 19:27:13 +0800391{
392 Segment_Context_t *p_ctx;
393
394 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800395 DVR_RETURN_IF_FALSE(p_ctx);
396 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liuc181a982020-01-07 19:27:13 +0800397
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800398 return lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
399}
400
hualing chen5605eed2020-05-26 18:18:06 +0800401uint64_t segment_tell_position_time(Segment_Handle_t handle, loff_t position)
402{
403 Segment_Context_t *p_ctx;
404 char buf[256];
405 char value[256];
hualing chen969fe7b2021-05-26 15:13:17 +0800406 uint64_t ret = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800407 uint64_t pts = 0L;
hualing chen969fe7b2021-05-26 15:13:17 +0800408 uint64_t pts_p = 0L;
hualing chen5605eed2020-05-26 18:18:06 +0800409 loff_t offset = 0;
hualing chen969fe7b2021-05-26 15:13:17 +0800410 loff_t offset_p = 0;
hualing chen5605eed2020-05-26 18:18:06 +0800411 char *p1, *p2;
412
413 p_ctx = (Segment_Context_t *)handle;
414 DVR_RETURN_IF_FALSE(p_ctx);
415 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
416 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
417
418 memset(buf, 0, sizeof(buf));
419 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
420 DVR_RETURN_IF_FALSE(position != -1);
421
422 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
423 memset(value, 0, sizeof(value));
424 if ((p1 = strstr(buf, "time="))) {
425 p1 += 5;
426 if ((p2 = strstr(buf, ","))) {
427 memcpy(value, p1, p2 - p1);
428 }
429 pts = strtoull(value, NULL, 10);
430 }
431
432 memset(value, 0, sizeof(value));
433 if ((p1 = strstr(buf, "offset="))) {
434 p1 += 7;
435 if ((p2 = strstr(buf, "}"))) {
436 memcpy(value, p1, p2 - p1);
437 }
438 offset = strtoull(value, NULL, 10);
439 }
440
441 memset(buf, 0, sizeof(buf));
442 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
443 if (position <= offset) {
hualing chen969fe7b2021-05-26 15:13:17 +0800444 ret = pts_p + (pts - pts_p) * (position - offset_p) / (offset - offset_p);
445 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);
446 return ret;
hualing chen5605eed2020-05-26 18:18:06 +0800447 }
hualing chen969fe7b2021-05-26 15:13:17 +0800448 offset_p = offset;
449 pts_p = pts;
hualing chen5605eed2020-05-26 18:18:06 +0800450 }
451 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
452 return pts;
453}
454
455
pengfei.liu8b563292020-02-26 15:49:02 +0800456uint64_t segment_tell_current_time(Segment_Handle_t handle)
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800457{
458 Segment_Context_t *p_ctx;
459 char buf[256];
460 char value[256];
hualing chen2aba4022020-03-02 13:49:55 +0800461 uint64_t pts = 0L;
462 loff_t offset = 0, position = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800463 char *p1, *p2;
464
465 p_ctx = (Segment_Context_t *)handle;
466 DVR_RETURN_IF_FALSE(p_ctx);
467 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
468 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
469
470 memset(buf, 0, sizeof(buf));
471 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
472 position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
473 DVR_RETURN_IF_FALSE(position != -1);
474
475 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
476 memset(value, 0, sizeof(value));
477 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800478 p1 += 5;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800479 if ((p2 = strstr(buf, ","))) {
480 memcpy(value, p1, p2 - p1);
481 }
482 pts = strtoull(value, NULL, 10);
483 }
484
485 memset(value, 0, sizeof(value));
486 if ((p1 = strstr(buf, "offset="))) {
487 p1 += 7;
488 if ((p2 = strstr(buf, "}"))) {
489 memcpy(value, p1, p2 - p1);
490 }
491 offset = strtoull(value, NULL, 10);
492 }
493
494 memset(buf, 0, sizeof(buf));
hualing chencc91e1c2020-02-28 13:26:17 +0800495 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800496 if (position <= offset) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800497 return pts;
498 }
499 }
hualing chena540a7e2020-03-27 16:44:05 +0800500 //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position);
hualing chen2aba4022020-03-02 13:49:55 +0800501 return pts;
Pengfei Liuc181a982020-01-07 19:27:13 +0800502}
Pengfei Liub038b6a2020-01-14 15:57:01 +0800503
pengfei.liu8b563292020-02-26 15:49:02 +0800504uint64_t segment_tell_total_time(Segment_Handle_t handle)
505{
506 Segment_Context_t *p_ctx;
507 char buf[256];
508 char last_buf[256];
509 char value[256];
510 uint64_t pts = ULLONG_MAX;
511 loff_t offset = 0, position = 0;
512 char *p1, *p2;
513 int line = 0;
514
515 p_ctx = (Segment_Context_t *)handle;
516 DVR_RETURN_IF_FALSE(p_ctx);
517 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
518 DVR_RETURN_IF_FALSE(p_ctx->ts_fd);
519
520 memset(buf, 0, sizeof(buf));
521 memset(last_buf, 0, sizeof(last_buf));
522 position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR);
523 DVR_RETURN_IF_FALSE(position != -1);
524
hualing chen041c4092020-04-05 15:11:50 +0800525 //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1);
526 //if seek error.we need seek 0 pos.
527 if (fseek(p_ctx->index_fp, -1000L, SEEK_END) == -1) {
528 fseek(p_ctx->index_fp, 0L, SEEK_SET);
529 }
pengfei.liu8b563292020-02-26 15:49:02 +0800530 /* Save last line buffer */
531 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
hualing chen2aba4022020-03-02 13:49:55 +0800532 if (strlen(buf) <= 0) {
hualing chen87072a82020-03-12 16:20:12 +0800533 DVR_DEBUG(1, "read index buf is len 0");
hualing chen2aba4022020-03-02 13:49:55 +0800534 continue;
535 }
pengfei.liu8b563292020-02-26 15:49:02 +0800536 memset(last_buf, 0, sizeof(last_buf));
537 memcpy(last_buf, buf, strlen(buf));
538 memset(buf, 0, sizeof(buf));
539 line++;
540 }
541
542 /* Extract time value */
543 memset(value, 0, sizeof(value));
544 if ((p1 = strstr(last_buf, "time="))) {
545 p1 += 5;
546 if ((p2 = strstr(last_buf, ","))) {
547 memcpy(value, p1, p2 - p1);
548 }
549 pts = strtoull(value, NULL, 10);
550 }
551
552 memset(value, 0, sizeof(value));
553 if ((p1 = strstr(last_buf, "offset="))) {
554 p1 += 7;
555 if ((p2 = strstr(last_buf, "}"))) {
556 memcpy(value, p1, p2 - p1);
557 }
558 offset = strtoull(value, NULL, 10);
559 }
hualing chen87072a82020-03-12 16:20:12 +0800560 //if (line < 2)
561 //DVR_DEBUG(1, "totle time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line);
pengfei.liu8b563292020-02-26 15:49:02 +0800562 return (pts == ULLONG_MAX ? DVR_FAILURE : pts);
563}
564
pengfei.liuab5a2262020-02-14 17:33:40 +0800565/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800566int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
567{
568 Segment_Context_t *p_ctx;
569 char buf[256];
570 uint32_t i;
571
572 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800573 DVR_RETURN_IF_FALSE(p_ctx);
574 DVR_RETURN_IF_FALSE(p_ctx->dat_fp);
575 DVR_RETURN_IF_FALSE(p_info);
hualing chen87072a82020-03-12 16:20:12 +0800576 //seek 0, rewrite info
577 DVR_RETURN_IF_FALSE(fseek(p_ctx->dat_fp, 0, SEEK_SET) != -1);
Pengfei Liub4734232020-01-17 18:25:10 +0800578
579 /*Save segment id*/
580 memset(buf, 0, sizeof(buf));
581 sprintf(buf, "id=%lld\n", p_info->id);
582 fputs(buf, p_ctx->dat_fp);
583
584 /*Save number of pids*/
585 memset(buf, 0, sizeof(buf));
586 sprintf(buf, "nb_pids=%d\n", p_info->nb_pids);
587 fputs(buf, p_ctx->dat_fp);
588
589 /*Save pid information*/
590 for (i = 0; i < p_info->nb_pids; i++) {
591 memset(buf, 0, sizeof(buf));
592 sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type);
593 fputs(buf, p_ctx->dat_fp);
594 }
595
596 /*Save segment duration*/
597 memset(buf, 0, sizeof(buf));
hualing chen87072a82020-03-12 16:20:12 +0800598 DVR_DEBUG(1, "duration store:[%ld]", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800599 sprintf(buf, "duration=%ld\n", p_info->duration);
600 fputs(buf, p_ctx->dat_fp);
601
602 /*Save segment size*/
603 memset(buf, 0, sizeof(buf));
604 sprintf(buf, "size=%zu\n", p_info->size);
605 fputs(buf, p_ctx->dat_fp);
606
607 /*Save number of packets*/
608 memset(buf, 0, sizeof(buf));
609 sprintf(buf, "nb_packets=%d\n", p_info->nb_packets);
610 fputs(buf, p_ctx->dat_fp);
611
612 fflush(p_ctx->dat_fp);
hualing chen47c34bc2020-05-11 09:15:47 +0800613 fsync(fileno(p_ctx->dat_fp));
Pengfei Liub4734232020-01-17 18:25:10 +0800614 return DVR_SUCCESS;
615}
616
pengfei.liuab5a2262020-02-14 17:33:40 +0800617/* Should consider the case of cut power, todo... */
Pengfei Liub4734232020-01-17 18:25:10 +0800618int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
619{
620 Segment_Context_t *p_ctx;
621 uint32_t i;
622 char buf[256];
623 char value[256];
624 char *p1, *p2;
625
626 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800627 DVR_RETURN_IF_FALSE(p_ctx);
628 DVR_RETURN_IF_FALSE(p_info);
Pengfei Liub4734232020-01-17 18:25:10 +0800629
630 /*Load segment id*/
631 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800632 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800633 p1 = strstr(buf, "id=");
hualing chen2aba4022020-03-02 13:49:55 +0800634 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800635 p_info->id = strtoull(p1 + 3, NULL, 10);
636
637 /*Save number of pids*/
638 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800639 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800640 p1 = strstr(buf, "nb_pids=");
hualing chen2aba4022020-03-02 13:49:55 +0800641 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800642 p_info->nb_pids = strtoull(p1 + 8, NULL, 10);
643
644 /*Save pid information*/
645 for (i = 0; i < p_info->nb_pids; i++) {
646 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800647 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800648 memset(value, 0, sizeof(value));
649 if ((p1 = strstr(buf, "pid="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800650 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800651 p1 += 4;
652 if ((p2 = strstr(buf, ","))) {
hualing chen2aba4022020-03-02 13:49:55 +0800653 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800654 memcpy(value, p1, p2 - p1);
655 }
656 p_info->pids[i].pid = strtoull(value, NULL, 10);
657 }
658
659 memset(value, 0, sizeof(value));
660 if ((p1 = strstr(buf, "type="))) {
hualing chen2aba4022020-03-02 13:49:55 +0800661 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800662 p1 += 5;
663 if ((p2 = strstr(buf, "}"))) {
hualing chen2aba4022020-03-02 13:49:55 +0800664 DVR_RETURN_IF_FALSE(p2);
Pengfei Liub4734232020-01-17 18:25:10 +0800665 memcpy(value, p1, p2 - p1);
666 }
667 p_info->pids[i].type = strtoull(value, NULL, 10);
668 }
669 }
670
671 /*Save segment duration*/
672 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800673 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800674 p1 = strstr(buf, "duration=");
hualing chen2aba4022020-03-02 13:49:55 +0800675 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800676 p_info->duration = strtoull(p1 + 9, NULL, 10);
hualing chena540a7e2020-03-27 16:44:05 +0800677 //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration);
Pengfei Liub4734232020-01-17 18:25:10 +0800678
679 /*Save segment size*/
680 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800681 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800682 p1 = strstr(buf, "size=");
hualing chen2aba4022020-03-02 13:49:55 +0800683 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800684 p_info->size = strtoull(p1 + 5, NULL, 10);
685
686 /*Save number of packets*/
687 p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800688 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800689 p1 = strstr(buf, "nb_packets=");
hualing chen2aba4022020-03-02 13:49:55 +0800690 DVR_RETURN_IF_FALSE(p1);
Pengfei Liub4734232020-01-17 18:25:10 +0800691 p_info->nb_packets = strtoull(p1 + 11, NULL, 10);
692
693 return DVR_SUCCESS;
694}
695
696int segment_delete(const char *location, uint64_t segment_id)
697{
698 char fname[MAX_SEGMENT_PATH_SIZE];
699 int ret;
700
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800701 DVR_RETURN_IF_FALSE(location);
Pengfei Liub4734232020-01-17 18:25:10 +0800702
703 /*delete ts file*/
704 memset(fname, 0, sizeof(fname));
705 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS);
706 ret = unlink(fname);
707 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800708 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800709
710 /*delete index file*/
711 memset(fname, 0, sizeof(fname));
712 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX);
713 unlink(fname);
714 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800715 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800716
717 /*delete store information file*/
718 memset(fname, 0, sizeof(fname));
719 segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT);
720 unlink(fname);
721 DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800722 DVR_RETURN_IF_FALSE(ret == 0);
Pengfei Liub4734232020-01-17 18:25:10 +0800723
724 return DVR_SUCCESS;
725}
726
hualing chen87072a82020-03-12 16:20:12 +0800727int segment_ongoing(Segment_Handle_t handle)
728{
729 Segment_Context_t *p_ctx;
730 p_ctx = (Segment_Context_t *)handle;
731 struct stat mstat;
732
733 char going_name[MAX_SEGMENT_PATH_SIZE];
734 memset(going_name, 0, sizeof(going_name));
735 segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING);
736 int ret = stat(going_name, &mstat);
737 DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret);
738 if (ret != 0) {
739 return DVR_FAILURE;
740 }
741 return DVR_SUCCESS;
742}
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800743loff_t segment_dump_pts(Segment_Handle_t handle)
Pengfei Liub038b6a2020-01-14 15:57:01 +0800744{
745 Segment_Context_t *p_ctx;
746 char buf[256];
747 char value[256];
748 uint64_t pts;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800749 loff_t offset;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800750 char *p1, *p2;
751
752 p_ctx = (Segment_Context_t *)handle;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800753 DVR_RETURN_IF_FALSE(p_ctx);
754 DVR_RETURN_IF_FALSE(p_ctx->index_fp);
755 DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800756
757 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800758 DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800759 printf("start gets pts\n");
760 while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) {
761 printf("buf[%s]\n", buf);
762 memset(value, 0, sizeof(value));
763 if ((p1 = strstr(buf, "time="))) {
hualing chencc91e1c2020-02-28 13:26:17 +0800764 p1 += 5;
Pengfei Liub038b6a2020-01-14 15:57:01 +0800765 if ((p2 = strstr(buf, ","))) {
766 memcpy(value, p1, p2 - p1);
767 }
768 pts = strtoull(value, NULL, 10);
769 }
770
771 memset(value, 0, sizeof(value));
772 if ((p1 = strstr(buf, "offset="))) {
773 p1 += 7;
774 if ((p2 = strstr(buf, "}"))) {
775 memcpy(value, p1, p2 - p1);
776 }
777 offset = strtoull(value, NULL, 10);
778 }
779
780 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800781 printf("pts=%llu, offset=%lld\n", pts, offset);
Pengfei Liub038b6a2020-01-14 15:57:01 +0800782 }
783
784 return 0;
785}