Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 2 | #include <unistd.h> |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 3 | #include <sys/types.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <string.h> |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 7 | #include <stdlib.h> |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 8 | #include <errno.h> |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 9 | #include "dvr_types.h" |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 10 | #include "segment.h" |
| 11 | |
| 12 | #define MAX_SEGMENT_FD_COUNT (128) |
| 13 | #define MAX_SEGMENT_PATH_SIZE (DVR_MAX_LOCATION_SIZE + 32) |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 14 | #define MAX_PTS_THRESHOLD (10*1000) |
hualing chen | 0620969 | 2020-11-05 14:51:46 +0800 | [diff] [blame] | 15 | #define PCR_RECORD_INTERVAL_MS (300) |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 16 | #define PTS_DISCONTINE_DEVIATION (40) |
| 17 | #define PTS_HEAD_DEVIATION (40) |
hualing chen | 86b8e60 | 2021-12-23 11:04:19 +0800 | [diff] [blame] | 18 | #define PCR_JUMP_DUR (5000) |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 19 | |
hualing chen | 157641d | 2022-02-22 17:54:52 +0800 | [diff] [blame] | 20 | #define IDX_FILE_SYNC_TIME (10)//10*PCR_RECORD_INTERVAL_MS |
| 21 | #define TS_FILE_SYNC_TIME (9)//9*PCR_RECORD_INTERVAL_MS |
| 22 | |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 23 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 24 | /**\brief Segment context*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 25 | typedef struct { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 26 | int ts_fd; /**< Segment ts file fd*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 27 | FILE *index_fp; /**< Time index file fd*/ |
| 28 | FILE *dat_fp; /**< Information file fd*/ |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 29 | FILE *all_dat_fp; /**< Information file fd*/ |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 30 | FILE *ongoing_fp; /**< Ongoing file fd, used to verify timedhift mode*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 31 | uint64_t first_pts; /**< First pts value, use for write mode*/ |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 32 | uint64_t last_pts; /**< Last input pts value, use for write mode*/ |
| 33 | uint64_t last_record_pts; /**< Last record pts value, use for write mode*/ |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 34 | uint64_t cur_time; /**< Current time save in index file */ |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 35 | uint64_t segment_id; /**< Current segment ID */ |
| 36 | char location[MAX_SEGMENT_PATH_SIZE]; /**< Current time save in index file */ |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 37 | loff_t first_offset; |
| 38 | loff_t last_offset; |
| 39 | loff_t last_record_offset; |
| 40 | float avg_rate; |
hualing chen | 157641d | 2022-02-22 17:54:52 +0800 | [diff] [blame] | 41 | int time; |
wentao.ma | 35a69d4 | 2022-03-10 18:08:40 +0800 | [diff] [blame^] | 42 | DVR_Bool_t force_sysclock; /**< If ture, force to use system clock as PVR index time source. If false, libdvr can determine index time source based on actual situation*/ |
hualing chen | 157641d | 2022-02-22 17:54:52 +0800 | [diff] [blame] | 43 | } Segment_Context_t; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 44 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 45 | /**\brief Segment file type*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 46 | typedef enum { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 47 | SEGMENT_FILE_TYPE_TS, /**< Used for store TS data*/ |
| 48 | SEGMENT_FILE_TYPE_INDEX, /**< Used for store index data*/ |
| 49 | SEGMENT_FILE_TYPE_DAT, /**< Used for store information data, such as duration etc*/ |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 50 | SEGMENT_FILE_TYPE_ONGOING, /**< Used for store information data, such as duration etc*/ |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 51 | SEGMENT_FILE_TYPE_ALLDAT, /**< Used for store all information data*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 52 | } Segment_FileType_t; |
| 53 | |
| 54 | static void segment_get_fname(char fname[MAX_SEGMENT_PATH_SIZE], |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 55 | const char location[DVR_MAX_LOCATION_SIZE], |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 56 | uint64_t segment_id, |
| 57 | Segment_FileType_t type) |
| 58 | { |
| 59 | int offset; |
| 60 | |
| 61 | memset(fname, 0, MAX_SEGMENT_PATH_SIZE); |
| 62 | strncpy(fname, location, strlen(location)); |
| 63 | offset = strlen(location); |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 64 | if (type != SEGMENT_FILE_TYPE_ALLDAT) { |
| 65 | strncpy(fname + offset, "-", 1); |
| 66 | offset += 1; |
| 67 | sprintf(fname + offset, "%04llu", segment_id); |
| 68 | offset += 4; |
| 69 | } |
| 70 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 71 | if (type == SEGMENT_FILE_TYPE_TS) |
| 72 | strncpy(fname + offset, ".ts", 3); |
| 73 | else if (type == SEGMENT_FILE_TYPE_INDEX) |
| 74 | strncpy(fname + offset, ".idx", 4); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 75 | else if (type == SEGMENT_FILE_TYPE_DAT) |
| 76 | strncpy(fname + offset, ".dat", 4); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 77 | else if (type == SEGMENT_FILE_TYPE_ONGOING) |
| 78 | strncpy(fname + offset, ".going", 6); |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 79 | else if (type == SEGMENT_FILE_TYPE_ALLDAT) |
| 80 | strncpy(fname + offset, ".dat", 4); |
| 81 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 82 | } |
| 83 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 84 | static void segment_get_dirname(char dir_name[MAX_SEGMENT_PATH_SIZE], |
| 85 | const char location[DVR_MAX_LOCATION_SIZE]) |
| 86 | { |
| 87 | char *p; |
| 88 | int i; |
| 89 | int found = 0; |
| 90 | |
| 91 | for (i = 0; i < (int)strlen(location); i++) { |
| 92 | if (location[i] == '/') { |
| 93 | p = (char *)location + i; |
| 94 | found = 1; |
| 95 | } |
| 96 | } |
| 97 | if (found) |
| 98 | memcpy(dir_name, location, p - location); |
| 99 | } |
| 100 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 101 | int segment_open(Segment_OpenParams_t *params, Segment_Handle_t *p_handle) |
| 102 | { |
| 103 | Segment_Context_t *p_ctx; |
| 104 | char ts_fname[MAX_SEGMENT_PATH_SIZE]; |
| 105 | char index_fname[MAX_SEGMENT_PATH_SIZE]; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 106 | char dat_fname[MAX_SEGMENT_PATH_SIZE]; |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 107 | char all_dat_fname[MAX_SEGMENT_PATH_SIZE]; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 108 | char dir_name[MAX_SEGMENT_PATH_SIZE]; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 109 | char going_name[MAX_SEGMENT_PATH_SIZE]; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 110 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 111 | DVR_RETURN_IF_FALSE(params); |
| 112 | DVR_RETURN_IF_FALSE(p_handle); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 113 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 114 | //DVR_DEBUG(1, "%s, location:%s, id:%llu", __func__, params->location, params->segment_id); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 115 | |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 116 | p_ctx = (void*)malloc(sizeof(Segment_Context_t)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 117 | DVR_RETURN_IF_FALSE(p_ctx); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 118 | memset(p_ctx, 0, sizeof(Segment_Context_t)); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 119 | |
| 120 | memset(ts_fname, 0, sizeof(ts_fname)); |
| 121 | segment_get_fname(ts_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_TS); |
| 122 | |
| 123 | memset(index_fname, 0, sizeof(index_fname)); |
| 124 | segment_get_fname(index_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_INDEX); |
| 125 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 126 | memset(dat_fname, 0, sizeof(dat_fname)); |
| 127 | segment_get_fname(dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_DAT); |
| 128 | |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 129 | memset(all_dat_fname, 0, sizeof(all_dat_fname)); |
| 130 | segment_get_fname(all_dat_fname, params->location, params->segment_id, SEGMENT_FILE_TYPE_ALLDAT); |
| 131 | |
| 132 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 133 | memset(going_name, 0, sizeof(going_name)); |
| 134 | segment_get_fname(going_name, params->location, params->segment_id, SEGMENT_FILE_TYPE_ONGOING); |
| 135 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 136 | memset(dir_name, 0, sizeof(dir_name)); |
| 137 | segment_get_dirname(dir_name, params->location); |
| 138 | if (access(dir_name, F_OK) == -1) { |
| 139 | DVR_DEBUG(1, "%s dir %s is not exist, create it", __func__, dir_name); |
| 140 | mkdir(dir_name, 0666); |
| 141 | } |
| 142 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 143 | if (params->mode == SEGMENT_MODE_READ) { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 144 | p_ctx->ts_fd = open(ts_fname, O_RDONLY); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 145 | p_ctx->index_fp = fopen(index_fname, "r"); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 146 | p_ctx->dat_fp = fopen(dat_fname, "r"); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 147 | p_ctx->ongoing_fp = NULL; |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 148 | p_ctx->all_dat_fp = fopen(all_dat_fname, "r"); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 149 | } else if (params->mode == SEGMENT_MODE_WRITE) { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 150 | p_ctx->ts_fd = open(ts_fname, O_CREAT | O_RDWR | O_TRUNC, 0644); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 151 | p_ctx->index_fp = fopen(index_fname, "w+"); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 152 | p_ctx->dat_fp = fopen(dat_fname, "w+"); |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 153 | p_ctx->all_dat_fp = fopen(all_dat_fname, "a+"); |
hualing chen | 8aed958 | 2021-12-24 17:59:56 +0800 | [diff] [blame] | 154 | DVR_DEBUG(1, "%s dir %s is opened", __func__, all_dat_fname); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 155 | p_ctx->ongoing_fp = fopen(going_name, "w+"); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 156 | p_ctx->first_pts = ULLONG_MAX; |
| 157 | p_ctx->last_pts = ULLONG_MAX; |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 158 | p_ctx->last_record_pts = ULLONG_MAX; |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 159 | p_ctx->avg_rate = 0.0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 160 | } else { |
| 161 | DVR_DEBUG(1, "%s, unknow mode use default", __func__); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 162 | p_ctx->ts_fd = open(ts_fname, O_RDONLY); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 163 | p_ctx->index_fp = fopen(index_fname, "r"); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 164 | p_ctx->dat_fp = fopen(dat_fname, "r"); |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 165 | p_ctx->all_dat_fp = fopen(all_dat_fname, "r"); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 166 | p_ctx->ongoing_fp = NULL; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 167 | } |
| 168 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 169 | if (p_ctx->ts_fd == -1 || !p_ctx->index_fp || !p_ctx->dat_fp) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 170 | DVR_DEBUG(1, "%s open file failed [%s, %s, %s], reason:%s", __func__, |
| 171 | ts_fname, index_fname, dat_fname, strerror(errno)); |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 172 | if (p_ctx->ts_fd != -1) |
| 173 | close(p_ctx->ts_fd); |
| 174 | if (p_ctx->index_fp) |
| 175 | fclose(p_ctx->index_fp); |
| 176 | if (p_ctx->dat_fp) |
| 177 | fclose(p_ctx->dat_fp); |
hualing chen | 926a8ec | 2021-12-20 20:38:24 +0800 | [diff] [blame] | 178 | if (p_ctx->all_dat_fp) |
| 179 | fclose(p_ctx->all_dat_fp); |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 180 | if (p_ctx->ongoing_fp) |
| 181 | fclose(p_ctx->ongoing_fp); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 182 | free(p_ctx); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 183 | *p_handle = NULL; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 184 | return DVR_FAILURE; |
| 185 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 186 | p_ctx->segment_id = params->segment_id; |
| 187 | strncpy(p_ctx->location, params->location, strlen(params->location)); |
wentao.ma | 35a69d4 | 2022-03-10 18:08:40 +0800 | [diff] [blame^] | 188 | p_ctx->force_sysclock = params->force_sysclock; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 189 | |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 190 | //DVR_DEBUG(1, "%s, open file success p_ctx->location [%s]", __func__, p_ctx->location, params->mode); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 191 | *p_handle = (Segment_Handle_t)p_ctx; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 192 | return DVR_SUCCESS; |
| 193 | } |
| 194 | |
| 195 | int segment_close(Segment_Handle_t handle) |
| 196 | { |
| 197 | Segment_Context_t *p_ctx; |
| 198 | |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 199 | p_ctx = (void *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 200 | DVR_RETURN_IF_FALSE(p_ctx); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 201 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 202 | if (p_ctx->ts_fd != -1) { |
| 203 | close(p_ctx->ts_fd); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | if (p_ctx->index_fp) { |
| 207 | fclose(p_ctx->index_fp); |
| 208 | } |
| 209 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 210 | if (p_ctx->dat_fp) { |
| 211 | fclose(p_ctx->dat_fp); |
| 212 | } |
hualing chen | 926a8ec | 2021-12-20 20:38:24 +0800 | [diff] [blame] | 213 | if (p_ctx->all_dat_fp) { |
| 214 | fclose(p_ctx->all_dat_fp); |
| 215 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 216 | if (p_ctx->ongoing_fp != NULL) { |
| 217 | fclose(p_ctx->ongoing_fp); |
| 218 | char going_name[MAX_SEGMENT_PATH_SIZE]; |
| 219 | memset(going_name, 0, sizeof(going_name)); |
| 220 | segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING); |
| 221 | DVR_DEBUG(1, "segment close del [%s]", going_name); |
| 222 | unlink(going_name); |
| 223 | } |
| 224 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 225 | free(p_ctx); |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | ssize_t segment_read(Segment_Handle_t handle, void *buf, size_t count) |
| 230 | { |
| 231 | Segment_Context_t *p_ctx; |
hualing chen | d241c7a | 2021-06-22 13:34:27 +0800 | [diff] [blame] | 232 | ssize_t len; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 233 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 234 | DVR_RETURN_IF_FALSE(p_ctx); |
| 235 | DVR_RETURN_IF_FALSE(buf); |
| 236 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
hualing chen | d241c7a | 2021-06-22 13:34:27 +0800 | [diff] [blame] | 237 | len = read(p_ctx->ts_fd, buf, count); |
| 238 | return len; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | ssize_t segment_write(Segment_Handle_t handle, void *buf, size_t count) |
| 242 | { |
| 243 | Segment_Context_t *p_ctx; |
Chuanzhi Wang | ccecb8d | 2020-12-02 11:22:54 +0800 | [diff] [blame] | 244 | ssize_t len; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 245 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 246 | DVR_RETURN_IF_FALSE(p_ctx); |
| 247 | DVR_RETURN_IF_FALSE(buf); |
| 248 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
Chuanzhi Wang | ccecb8d | 2020-12-02 11:22:54 +0800 | [diff] [blame] | 249 | len = write(p_ctx->ts_fd, buf, count); |
hualing chen | 157641d | 2022-02-22 17:54:52 +0800 | [diff] [blame] | 250 | if (p_ctx->time % TS_FILE_SYNC_TIME == 0) |
| 251 | fsync(p_ctx->ts_fd); |
Chuanzhi Wang | ccecb8d | 2020-12-02 11:22:54 +0800 | [diff] [blame] | 252 | return len; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 253 | } |
| 254 | |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 255 | int segment_update_pts_force(Segment_Handle_t handle, uint64_t pts, loff_t offset) |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 256 | { |
| 257 | Segment_Context_t *p_ctx; |
| 258 | char buf[256]; |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 259 | int record_diff = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 260 | |
| 261 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 262 | DVR_RETURN_IF_FALSE(p_ctx); |
| 263 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 264 | |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 265 | if (p_ctx->first_pts == ULLONG_MAX) { |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 266 | DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 267 | p_ctx->first_pts = pts; |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 268 | p_ctx->first_offset = offset; |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 269 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 270 | memset(buf, 0, sizeof(buf)); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 271 | if (p_ctx->last_pts == ULLONG_MAX) { |
| 272 | /*Last pts is init value*/ |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 273 | sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 274 | p_ctx->cur_time = pts - p_ctx->first_pts; |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 275 | DVR_DEBUG(1, "%s force pcr:%llu -1", __func__, pts); |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 276 | } else { |
| 277 | /*Last pts has valid value*/ |
| 278 | int diff = pts - p_ctx->last_pts; |
| 279 | if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) { |
| 280 | /*Current pts has a transition*/ |
| 281 | DVR_DEBUG(1, "[%s]force update Current pts has a transition, [%llu, %llu, %llu]",__func__, |
| 282 | p_ctx->first_pts, p_ctx->last_pts, pts); |
| 283 | sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset); |
| 284 | } else { |
| 285 | /*This is a normal pts, record it*/ |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 286 | //check if this pcr is transition.if true,add 200ms |
| 287 | //other case normal. |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 288 | p_ctx->cur_time += diff; |
| 289 | DVR_DEBUG(1, "%s force pcr:%llu -1 diff [%d]", __func__, pts, diff); |
| 290 | sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | record_diff = pts - p_ctx->last_record_pts; |
| 295 | if (strlen(buf) > 0) { |
| 296 | DVR_DEBUG(1, "%s force pcr:%llu buf:%s", __func__, pts, buf); |
| 297 | fputs(buf, p_ctx->index_fp); |
| 298 | fflush(p_ctx->index_fp); |
| 299 | fsync(fileno(p_ctx->index_fp)); |
| 300 | p_ctx->last_record_pts = pts; |
| 301 | } |
| 302 | p_ctx->last_pts = pts; |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 303 | return DVR_SUCCESS; |
| 304 | } |
| 305 | |
| 306 | int segment_update_pts(Segment_Handle_t handle, uint64_t pts, loff_t offset) |
| 307 | { |
| 308 | Segment_Context_t *p_ctx; |
| 309 | char buf[256]; |
| 310 | int record_diff = 0; |
| 311 | |
| 312 | p_ctx = (Segment_Context_t *)handle; |
| 313 | DVR_RETURN_IF_FALSE(p_ctx); |
| 314 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 315 | |
| 316 | if (p_ctx->first_pts == ULLONG_MAX) { |
| 317 | DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts); |
| 318 | p_ctx->first_pts = pts; |
| 319 | //p_ctx->cur_time = p_ctx->cur_time + PTS_HEAD_DEVIATION; |
| 320 | } |
| 321 | memset(buf, 0, sizeof(buf)); |
| 322 | if (p_ctx->last_pts == ULLONG_MAX) { |
| 323 | /*Last pts is init value*/ |
| 324 | sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset); |
| 325 | p_ctx->cur_time = pts - p_ctx->first_pts; |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 326 | } else { |
wentao.ma | 35a69d4 | 2022-03-10 18:08:40 +0800 | [diff] [blame^] | 327 | if (!p_ctx->force_sysclock) { |
| 328 | /* if force_sysclock is off, we follow old manner. Please refer to |
| 329 | * SWPL-75327*/ |
| 330 | /*Last pts has valid value*/ |
| 331 | int diff = pts - p_ctx->last_pts; |
| 332 | if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) { |
| 333 | /*Current pts has a transition*/ |
| 334 | DVR_DEBUG(1, "Current pts has a transition, [%llu, %llu, %llu]", |
| 335 | p_ctx->first_pts, p_ctx->last_pts, pts); |
| 336 | p_ctx->last_record_pts = pts; |
| 337 | //p_ctx->cur_time = p_ctx->cur_time + PTS_DISCONTINE_DEVIATION; |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 338 | } else { |
wentao.ma | 35a69d4 | 2022-03-10 18:08:40 +0800 | [diff] [blame^] | 339 | /*This is a normal pts, record it*/ |
| 340 | loff_t off_diff = offset - p_ctx->last_offset; |
| 341 | float rate = (float) (off_diff) / (float)(diff); |
| 342 | if (p_ctx->avg_rate == 0.0) { |
| 343 | p_ctx->avg_rate = (float) offset / (float)(p_ctx->cur_time + diff); |
| 344 | } |
| 345 | if (diff >= PCR_JUMP_DUR) { |
| 346 | DVR_DEBUG(1, "PTS TRANSITION ERROR last pcr[%llu]pts[%llu]pcr diff[%d]off[%llu]off_diff[%llu]rate[%f]avg rate[%f]4*rate[%d]av_diff[%d]",p_ctx->last_pts, pts, diff, offset,off_diff, rate, p_ctx->avg_rate, (int)(rate * 4), (int)(off_diff / p_ctx->avg_rate)); |
| 347 | if (p_ctx->avg_rate != 0 && (int)(p_ctx->avg_rate) >= (int)(rate * 4)) { |
| 348 | diff = off_diff / p_ctx->avg_rate; |
| 349 | p_ctx->cur_time += diff; |
| 350 | } else { |
| 351 | p_ctx->cur_time += diff; |
| 352 | } |
| 353 | } else { |
| 354 | p_ctx->cur_time += diff; |
| 355 | } |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 356 | } |
wentao.ma | 35a69d4 | 2022-03-10 18:08:40 +0800 | [diff] [blame^] | 357 | } else { |
| 358 | /* if force_sysclock is on, we simply calculate cur_time based on system |
| 359 | * time. Please refer to SWPL-75327*/ |
| 360 | p_ctx->cur_time = pts - p_ctx->first_pts; |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 361 | } |
wentao.ma | 35a69d4 | 2022-03-10 18:08:40 +0800 | [diff] [blame^] | 362 | sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 363 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 364 | |
| 365 | record_diff = pts - p_ctx->last_record_pts; |
| 366 | if (strlen(buf) > 0 && |
| 367 | (record_diff > PCR_RECORD_INTERVAL_MS || p_ctx->last_record_pts == ULLONG_MAX)){ |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 368 | fputs(buf, p_ctx->index_fp); |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 369 | fflush(p_ctx->index_fp); |
hualing chen | 157641d | 2022-02-22 17:54:52 +0800 | [diff] [blame] | 370 | p_ctx->time++; |
| 371 | //flush idx file 3s |
| 372 | if ((p_ctx->time > 0 && p_ctx->time % IDX_FILE_SYNC_TIME == 0)) |
| 373 | fsync(fileno(p_ctx->index_fp)); |
| 374 | if (p_ctx->time > IDX_FILE_SYNC_TIME) |
| 375 | p_ctx->time = 0; |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 376 | p_ctx->last_record_pts = pts; |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 377 | p_ctx->last_record_offset = offset; |
| 378 | if (p_ctx->cur_time > 0) |
| 379 | p_ctx->avg_rate = (float) offset / (float)p_ctx->cur_time; |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 380 | } |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 381 | p_ctx->last_pts = pts; |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 382 | p_ctx->last_offset = offset; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 383 | return DVR_SUCCESS; |
| 384 | } |
| 385 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 386 | loff_t segment_seek(Segment_Handle_t handle, uint64_t time, int block_size) |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 387 | { |
| 388 | Segment_Context_t *p_ctx; |
| 389 | char buf[256]; |
| 390 | char value[256]; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 391 | uint64_t pts = 0L; |
| 392 | loff_t offset = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 393 | char *p1, *p2; |
| 394 | |
hualing chen | 8a657f3 | 2021-08-30 13:12:49 +0800 | [diff] [blame] | 395 | DVR_DEBUG(1, "into seek time=%llu, offset=%lld time--%llu\n", pts, offset, time); |
| 396 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 397 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 398 | DVR_RETURN_IF_FALSE(p_ctx); |
| 399 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 400 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 401 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 402 | if (time == 0) { |
| 403 | offset = 0; |
hualing chen | d241c7a | 2021-06-22 13:34:27 +0800 | [diff] [blame] | 404 | DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time); |
| 405 | DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 406 | return offset; |
| 407 | } |
| 408 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 409 | memset(buf, 0, sizeof(buf)); |
| 410 | DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 411 | int line = 0; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 412 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 413 | line++; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 414 | memset(value, 0, sizeof(value)); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 415 | if ((p1 = strstr(buf, "time="))) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 416 | p1 += 5; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 417 | if ((p2 = strstr(buf, ","))) { |
| 418 | memcpy(value, p1, p2 - p1); |
| 419 | } |
| 420 | pts = strtoull(value, NULL, 10); |
| 421 | } |
| 422 | |
| 423 | memset(value, 0, sizeof(value)); |
| 424 | if ((p1 = strstr(buf, "offset="))) { |
| 425 | p1 += 7; |
| 426 | if ((p2 = strstr(buf, "}"))) { |
| 427 | memcpy(value, p1, p2 - p1); |
| 428 | } |
| 429 | offset = strtoull(value, NULL, 10); |
| 430 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 431 | if (0) |
| 432 | { |
| 433 | DVR_DEBUG(1, "seek buf[%s]", buf); |
| 434 | DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset); |
| 435 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 436 | memset(buf, 0, sizeof(buf)); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 437 | if (time <= pts) { |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 438 | if (block_size > 0) { |
| 439 | offset = offset - offset%block_size; |
| 440 | } |
hualing chen | d241c7a | 2021-06-22 13:34:27 +0800 | [diff] [blame] | 441 | //DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line); |
| 442 | DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 443 | return offset; |
| 444 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 445 | } |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 446 | if (time > pts) { |
| 447 | if (block_size > 0) { |
| 448 | offset = offset - offset%block_size; |
| 449 | } |
| 450 | DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d end\n", pts, offset, time, line); |
hualing chen | d241c7a | 2021-06-22 13:34:27 +0800 | [diff] [blame] | 451 | DVR_RETURN_IF_FALSE(lseek(p_ctx->ts_fd, offset, SEEK_SET) != -1); |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 452 | return offset; |
| 453 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 454 | DVR_DEBUG(1, "seek error line [%d]", line); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 455 | return DVR_FAILURE; |
| 456 | } |
| 457 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 458 | loff_t segment_tell_position(Segment_Handle_t handle) |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 459 | { |
| 460 | Segment_Context_t *p_ctx; |
hualing chen | d241c7a | 2021-06-22 13:34:27 +0800 | [diff] [blame] | 461 | loff_t pos; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 462 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 463 | DVR_RETURN_IF_FALSE(p_ctx); |
| 464 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
hualing chen | d241c7a | 2021-06-22 13:34:27 +0800 | [diff] [blame] | 465 | pos = lseek(p_ctx->ts_fd, 0, SEEK_CUR); |
| 466 | return pos; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 467 | } |
| 468 | |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 469 | uint64_t segment_tell_position_time(Segment_Handle_t handle, loff_t position) |
| 470 | { |
| 471 | Segment_Context_t *p_ctx; |
| 472 | char buf[256]; |
| 473 | char value[256]; |
hualing chen | 969fe7b | 2021-05-26 15:13:17 +0800 | [diff] [blame] | 474 | uint64_t ret = 0L; |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 475 | uint64_t pts = 0L; |
hualing chen | 969fe7b | 2021-05-26 15:13:17 +0800 | [diff] [blame] | 476 | uint64_t pts_p = 0L; |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 477 | loff_t offset = 0; |
hualing chen | 969fe7b | 2021-05-26 15:13:17 +0800 | [diff] [blame] | 478 | loff_t offset_p = 0; |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 479 | char *p1, *p2; |
| 480 | |
| 481 | p_ctx = (Segment_Context_t *)handle; |
| 482 | DVR_RETURN_IF_FALSE(p_ctx); |
| 483 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 484 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd); |
| 485 | |
| 486 | memset(buf, 0, sizeof(buf)); |
| 487 | DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1); |
| 488 | DVR_RETURN_IF_FALSE(position != -1); |
| 489 | |
| 490 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
| 491 | memset(value, 0, sizeof(value)); |
| 492 | if ((p1 = strstr(buf, "time="))) { |
| 493 | p1 += 5; |
| 494 | if ((p2 = strstr(buf, ","))) { |
| 495 | memcpy(value, p1, p2 - p1); |
| 496 | } |
| 497 | pts = strtoull(value, NULL, 10); |
| 498 | } |
| 499 | |
| 500 | memset(value, 0, sizeof(value)); |
| 501 | if ((p1 = strstr(buf, "offset="))) { |
| 502 | p1 += 7; |
| 503 | if ((p2 = strstr(buf, "}"))) { |
| 504 | memcpy(value, p1, p2 - p1); |
| 505 | } |
| 506 | offset = strtoull(value, NULL, 10); |
| 507 | } |
| 508 | |
| 509 | memset(buf, 0, sizeof(buf)); |
| 510 | //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position); |
hualing chen | d241c7a | 2021-06-22 13:34:27 +0800 | [diff] [blame] | 511 | if (position <= offset |
| 512 | &&position >= offset_p |
| 513 | && offset - offset_p > 0) { |
hualing chen | 969fe7b | 2021-05-26 15:13:17 +0800 | [diff] [blame] | 514 | ret = pts_p + (pts - pts_p) * (position - offset_p) / (offset - offset_p); |
hualing chen | d241c7a | 2021-06-22 13:34:27 +0800 | [diff] [blame] | 515 | //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 chen | 969fe7b | 2021-05-26 15:13:17 +0800 | [diff] [blame] | 516 | return ret; |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 517 | } |
hualing chen | 969fe7b | 2021-05-26 15:13:17 +0800 | [diff] [blame] | 518 | offset_p = offset; |
| 519 | pts_p = pts; |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 520 | } |
| 521 | //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position); |
| 522 | return pts; |
| 523 | } |
| 524 | |
| 525 | |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 526 | uint64_t segment_tell_current_time(Segment_Handle_t handle) |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 527 | { |
| 528 | Segment_Context_t *p_ctx; |
| 529 | char buf[256]; |
| 530 | char value[256]; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 531 | uint64_t pts = 0L; |
| 532 | loff_t offset = 0, position = 0; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 533 | char *p1, *p2; |
| 534 | |
| 535 | p_ctx = (Segment_Context_t *)handle; |
| 536 | DVR_RETURN_IF_FALSE(p_ctx); |
| 537 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 538 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd); |
| 539 | |
| 540 | memset(buf, 0, sizeof(buf)); |
| 541 | DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1); |
hualing chen | d241c7a | 2021-06-22 13:34:27 +0800 | [diff] [blame] | 542 | position = lseek(p_ctx->ts_fd, 0, SEEK_CUR); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 543 | DVR_RETURN_IF_FALSE(position != -1); |
| 544 | |
| 545 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
| 546 | memset(value, 0, sizeof(value)); |
| 547 | if ((p1 = strstr(buf, "time="))) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 548 | p1 += 5; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 549 | if ((p2 = strstr(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(buf, "offset="))) { |
| 557 | p1 += 7; |
| 558 | if ((p2 = strstr(buf, "}"))) { |
| 559 | memcpy(value, p1, p2 - p1); |
| 560 | } |
| 561 | offset = strtoull(value, NULL, 10); |
| 562 | } |
| 563 | |
| 564 | memset(buf, 0, sizeof(buf)); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 565 | //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 566 | if (position <= offset) { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 567 | return pts; |
| 568 | } |
| 569 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 570 | //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 571 | return pts; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 572 | } |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 573 | |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 574 | uint64_t segment_tell_total_time(Segment_Handle_t handle) |
| 575 | { |
| 576 | Segment_Context_t *p_ctx; |
| 577 | char buf[256]; |
| 578 | char last_buf[256]; |
| 579 | char value[256]; |
| 580 | uint64_t pts = ULLONG_MAX; |
| 581 | loff_t offset = 0, position = 0; |
| 582 | char *p1, *p2; |
| 583 | int line = 0; |
| 584 | |
| 585 | p_ctx = (Segment_Context_t *)handle; |
| 586 | DVR_RETURN_IF_FALSE(p_ctx); |
| 587 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 588 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd); |
| 589 | |
| 590 | memset(buf, 0, sizeof(buf)); |
| 591 | memset(last_buf, 0, sizeof(last_buf)); |
hualing chen | d241c7a | 2021-06-22 13:34:27 +0800 | [diff] [blame] | 592 | position = lseek(p_ctx->ts_fd, 0, SEEK_CUR); |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 593 | DVR_RETURN_IF_FALSE(position != -1); |
| 594 | |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 595 | //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1); |
| 596 | //if seek error.we need seek 0 pos. |
| 597 | if (fseek(p_ctx->index_fp, -1000L, SEEK_END) == -1) { |
| 598 | fseek(p_ctx->index_fp, 0L, SEEK_SET); |
| 599 | } |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 600 | /* Save last line buffer */ |
| 601 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 602 | if (strlen(buf) <= 0) { |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 603 | DVR_DEBUG(1, "read index buf is len 0"); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 604 | continue; |
| 605 | } |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 606 | memset(last_buf, 0, sizeof(last_buf)); |
| 607 | memcpy(last_buf, buf, strlen(buf)); |
| 608 | memset(buf, 0, sizeof(buf)); |
| 609 | line++; |
| 610 | } |
| 611 | |
| 612 | /* Extract time value */ |
| 613 | memset(value, 0, sizeof(value)); |
| 614 | if ((p1 = strstr(last_buf, "time="))) { |
| 615 | p1 += 5; |
| 616 | if ((p2 = strstr(last_buf, ","))) { |
| 617 | memcpy(value, p1, p2 - p1); |
| 618 | } |
| 619 | pts = strtoull(value, NULL, 10); |
| 620 | } |
| 621 | |
| 622 | memset(value, 0, sizeof(value)); |
| 623 | if ((p1 = strstr(last_buf, "offset="))) { |
| 624 | p1 += 7; |
| 625 | if ((p2 = strstr(last_buf, "}"))) { |
| 626 | memcpy(value, p1, p2 - p1); |
| 627 | } |
| 628 | offset = strtoull(value, NULL, 10); |
| 629 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 630 | //if (line < 2) |
| 631 | //DVR_DEBUG(1, "totle time=%llu, offset=%lld, position=%lld, line:%d\n", pts, offset, position, line); |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 632 | return (pts == ULLONG_MAX ? DVR_FAILURE : pts); |
| 633 | } |
| 634 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 635 | /* Should consider the case of cut power, todo... */ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 636 | int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info) |
| 637 | { |
| 638 | Segment_Context_t *p_ctx; |
| 639 | char buf[256]; |
| 640 | uint32_t i; |
| 641 | |
| 642 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 643 | DVR_RETURN_IF_FALSE(p_ctx); |
| 644 | DVR_RETURN_IF_FALSE(p_ctx->dat_fp); |
| 645 | DVR_RETURN_IF_FALSE(p_info); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 646 | //seek 0, rewrite info |
| 647 | DVR_RETURN_IF_FALSE(fseek(p_ctx->dat_fp, 0, SEEK_SET) != -1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 648 | |
| 649 | /*Save segment id*/ |
| 650 | memset(buf, 0, sizeof(buf)); |
| 651 | sprintf(buf, "id=%lld\n", p_info->id); |
| 652 | fputs(buf, p_ctx->dat_fp); |
| 653 | |
| 654 | /*Save number of pids*/ |
| 655 | memset(buf, 0, sizeof(buf)); |
| 656 | sprintf(buf, "nb_pids=%d\n", p_info->nb_pids); |
| 657 | fputs(buf, p_ctx->dat_fp); |
| 658 | |
| 659 | /*Save pid information*/ |
| 660 | for (i = 0; i < p_info->nb_pids; i++) { |
| 661 | memset(buf, 0, sizeof(buf)); |
| 662 | sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type); |
| 663 | fputs(buf, p_ctx->dat_fp); |
| 664 | } |
| 665 | |
| 666 | /*Save segment duration*/ |
| 667 | memset(buf, 0, sizeof(buf)); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 668 | DVR_DEBUG(1, "duration store:[%ld]", p_info->duration); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 669 | sprintf(buf, "duration=%ld\n", p_info->duration); |
| 670 | fputs(buf, p_ctx->dat_fp); |
| 671 | |
| 672 | /*Save segment size*/ |
| 673 | memset(buf, 0, sizeof(buf)); |
| 674 | sprintf(buf, "size=%zu\n", p_info->size); |
| 675 | fputs(buf, p_ctx->dat_fp); |
| 676 | |
| 677 | /*Save number of packets*/ |
| 678 | memset(buf, 0, sizeof(buf)); |
| 679 | sprintf(buf, "nb_packets=%d\n", p_info->nb_packets); |
| 680 | fputs(buf, p_ctx->dat_fp); |
| 681 | |
| 682 | fflush(p_ctx->dat_fp); |
hualing chen | 47c34bc | 2020-05-11 09:15:47 +0800 | [diff] [blame] | 683 | fsync(fileno(p_ctx->dat_fp)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 684 | return DVR_SUCCESS; |
| 685 | } |
| 686 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 687 | /* Should consider the case of cut power, todo... */ |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 688 | int segment_store_allInfo(Segment_Handle_t handle, Segment_StoreInfo_t *p_info) |
| 689 | { |
| 690 | Segment_Context_t *p_ctx; |
| 691 | char buf[256]; |
| 692 | uint32_t i; |
| 693 | |
| 694 | p_ctx = (Segment_Context_t *)handle; |
| 695 | DVR_RETURN_IF_FALSE(p_ctx); |
| 696 | DVR_RETURN_IF_FALSE(p_ctx->all_dat_fp); |
| 697 | DVR_RETURN_IF_FALSE(p_info); |
| 698 | //seek 0, rewrite info |
| 699 | DVR_RETURN_IF_FALSE(fseek(p_ctx->all_dat_fp, 0, SEEK_END) != -1); |
| 700 | |
| 701 | /*Save segment id*/ |
| 702 | memset(buf, 0, sizeof(buf)); |
| 703 | sprintf(buf, "id=%lld\n", p_info->id); |
| 704 | fputs(buf, p_ctx->all_dat_fp); |
| 705 | |
| 706 | /*Save number of pids*/ |
| 707 | memset(buf, 0, sizeof(buf)); |
| 708 | sprintf(buf, "nb_pids=%d\n", p_info->nb_pids); |
| 709 | fputs(buf, p_ctx->all_dat_fp); |
| 710 | |
| 711 | /*Save pid information*/ |
| 712 | for (i = 0; i < p_info->nb_pids; i++) { |
| 713 | memset(buf, 0, sizeof(buf)); |
| 714 | sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type); |
| 715 | fputs(buf, p_ctx->all_dat_fp); |
| 716 | } |
| 717 | |
| 718 | /*Save segment duration*/ |
| 719 | memset(buf, 0, sizeof(buf)); |
| 720 | DVR_DEBUG(1, "duration store:[%ld]", p_info->duration); |
| 721 | sprintf(buf, "duration=%ld\n", p_info->duration); |
| 722 | fputs(buf, p_ctx->all_dat_fp); |
| 723 | |
| 724 | /*Save segment size*/ |
| 725 | memset(buf, 0, sizeof(buf)); |
| 726 | sprintf(buf, "size=%zu\n", p_info->size); |
| 727 | fputs(buf, p_ctx->all_dat_fp); |
| 728 | |
| 729 | /*Save number of packets*/ |
| 730 | memset(buf, 0, sizeof(buf)); |
| 731 | sprintf(buf, "nb_packets=%d\n", p_info->nb_packets); |
| 732 | fputs(buf, p_ctx->all_dat_fp); |
| 733 | |
| 734 | fflush(p_ctx->all_dat_fp); |
| 735 | fsync(fileno(p_ctx->all_dat_fp)); |
| 736 | return DVR_SUCCESS; |
| 737 | } |
| 738 | |
| 739 | /* Should consider the case of cut power, todo... */ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 740 | int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info) |
| 741 | { |
| 742 | Segment_Context_t *p_ctx; |
| 743 | uint32_t i; |
| 744 | char buf[256]; |
| 745 | char value[256]; |
| 746 | char *p1, *p2; |
| 747 | |
| 748 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 749 | DVR_RETURN_IF_FALSE(p_ctx); |
| 750 | DVR_RETURN_IF_FALSE(p_info); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 751 | |
| 752 | /*Load segment id*/ |
| 753 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 754 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 755 | p1 = strstr(buf, "id="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 756 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 757 | p_info->id = strtoull(p1 + 3, NULL, 10); |
| 758 | |
| 759 | /*Save number of pids*/ |
| 760 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 761 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 762 | p1 = strstr(buf, "nb_pids="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 763 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 764 | p_info->nb_pids = strtoull(p1 + 8, NULL, 10); |
| 765 | |
| 766 | /*Save pid information*/ |
| 767 | for (i = 0; i < p_info->nb_pids; i++) { |
| 768 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 769 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 770 | memset(value, 0, sizeof(value)); |
| 771 | if ((p1 = strstr(buf, "pid="))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 772 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 773 | p1 += 4; |
| 774 | if ((p2 = strstr(buf, ","))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 775 | DVR_RETURN_IF_FALSE(p2); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 776 | memcpy(value, p1, p2 - p1); |
| 777 | } |
| 778 | p_info->pids[i].pid = strtoull(value, NULL, 10); |
| 779 | } |
| 780 | |
| 781 | memset(value, 0, sizeof(value)); |
| 782 | if ((p1 = strstr(buf, "type="))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 783 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 784 | p1 += 5; |
| 785 | if ((p2 = strstr(buf, "}"))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 786 | DVR_RETURN_IF_FALSE(p2); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 787 | memcpy(value, p1, p2 - p1); |
| 788 | } |
| 789 | p_info->pids[i].type = strtoull(value, NULL, 10); |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | /*Save segment duration*/ |
| 794 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 795 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 796 | p1 = strstr(buf, "duration="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 797 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 798 | p_info->duration = strtoull(p1 + 9, NULL, 10); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 799 | //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 800 | |
| 801 | /*Save segment size*/ |
| 802 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 803 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 804 | p1 = strstr(buf, "size="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 805 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 806 | p_info->size = strtoull(p1 + 5, NULL, 10); |
| 807 | |
| 808 | /*Save number of packets*/ |
| 809 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 810 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 811 | p1 = strstr(buf, "nb_packets="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 812 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 813 | p_info->nb_packets = strtoull(p1 + 11, NULL, 10); |
| 814 | |
| 815 | return DVR_SUCCESS; |
| 816 | } |
| 817 | |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 818 | /* Should consider the case of cut power, todo... */ |
| 819 | int segment_load_allInfo(Segment_Handle_t handle, struct list_head *list) |
| 820 | { |
| 821 | Segment_Context_t *p_ctx; |
| 822 | uint32_t i; |
| 823 | char buf[256]; |
| 824 | char value[256]; |
| 825 | char *p1, *p2; |
| 826 | |
| 827 | p_ctx = (Segment_Context_t *)handle; |
| 828 | DVR_RETURN_IF_FALSE(p_ctx); |
| 829 | DVR_RETURN_IF_FALSE(list); |
hualing chen | 926a8ec | 2021-12-20 20:38:24 +0800 | [diff] [blame] | 830 | if (p_ctx->all_dat_fp == NULL) { |
| 831 | DVR_DEBUG(1, "all dat file not open\n"); |
| 832 | return DVR_FAILURE; |
| 833 | } |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 834 | //first get |
| 835 | p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp); |
| 836 | DVR_RETURN_IF_FALSE(p1); |
| 837 | |
| 838 | do { |
| 839 | |
| 840 | DVR_RecordSegmentInfo_t *p_info; |
| 841 | |
| 842 | p_info = malloc(sizeof(DVR_RecordSegmentInfo_t)); |
| 843 | memset(p_info, 0, sizeof(DVR_RecordSegmentInfo_t)); |
| 844 | |
| 845 | list_add_tail(&p_info->head, list); |
| 846 | |
| 847 | /*Load segment id*/ |
| 848 | DVR_RETURN_IF_FALSE(p1); |
| 849 | p1 = strstr(buf, "id="); |
| 850 | DVR_RETURN_IF_FALSE(p1); |
| 851 | p_info->id = strtoull(p1 + 3, NULL, 10); |
| 852 | |
| 853 | /*Save number of pids*/ |
| 854 | p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp); |
| 855 | DVR_RETURN_IF_FALSE(p1); |
| 856 | p1 = strstr(buf, "nb_pids="); |
| 857 | DVR_RETURN_IF_FALSE(p1); |
| 858 | p_info->nb_pids = strtoull(p1 + 8, NULL, 10); |
| 859 | |
| 860 | /*Save pid information*/ |
| 861 | for (i = 0; i < p_info->nb_pids; i++) { |
| 862 | p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp); |
| 863 | DVR_RETURN_IF_FALSE(p1); |
| 864 | memset(value, 0, sizeof(value)); |
| 865 | if ((p1 = strstr(buf, "pid="))) { |
| 866 | DVR_RETURN_IF_FALSE(p1); |
| 867 | p1 += 4; |
| 868 | if ((p2 = strstr(buf, ","))) { |
| 869 | DVR_RETURN_IF_FALSE(p2); |
| 870 | memcpy(value, p1, p2 - p1); |
| 871 | } |
| 872 | p_info->pids[i].pid = strtoull(value, NULL, 10); |
| 873 | } |
| 874 | |
| 875 | memset(value, 0, sizeof(value)); |
| 876 | if ((p1 = strstr(buf, "type="))) { |
| 877 | DVR_RETURN_IF_FALSE(p1); |
| 878 | p1 += 5; |
| 879 | if ((p2 = strstr(buf, "}"))) { |
| 880 | DVR_RETURN_IF_FALSE(p2); |
| 881 | memcpy(value, p1, p2 - p1); |
| 882 | } |
| 883 | p_info->pids[i].type = strtoull(value, NULL, 10); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | /*Save segment duration*/ |
| 888 | p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp); |
| 889 | DVR_RETURN_IF_FALSE(p1); |
| 890 | p1 = strstr(buf, "duration="); |
| 891 | DVR_RETURN_IF_FALSE(p1); |
| 892 | p_info->duration = strtoull(p1 + 9, NULL, 10); |
| 893 | //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration); |
| 894 | |
| 895 | /*Save segment size*/ |
| 896 | p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp); |
| 897 | DVR_RETURN_IF_FALSE(p1); |
| 898 | p1 = strstr(buf, "size="); |
| 899 | DVR_RETURN_IF_FALSE(p1); |
| 900 | p_info->size = strtoull(p1 + 5, NULL, 10); |
| 901 | |
| 902 | /*Save number of packets*/ |
| 903 | p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp); |
| 904 | DVR_RETURN_IF_FALSE(p1); |
| 905 | p1 = strstr(buf, "nb_packets="); |
| 906 | DVR_RETURN_IF_FALSE(p1); |
| 907 | p_info->nb_packets = strtoull(p1 + 11, NULL, 10); |
| 908 | //if reach end,exit loop |
| 909 | p1 = fgets(buf, sizeof(buf), p_ctx->all_dat_fp); |
| 910 | } while (p1); |
| 911 | |
| 912 | return DVR_SUCCESS; |
| 913 | } |
| 914 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 915 | int segment_delete(const char *location, uint64_t segment_id) |
| 916 | { |
| 917 | char fname[MAX_SEGMENT_PATH_SIZE]; |
| 918 | int ret; |
| 919 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 920 | DVR_RETURN_IF_FALSE(location); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 921 | |
| 922 | /*delete ts file*/ |
| 923 | memset(fname, 0, sizeof(fname)); |
| 924 | segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS); |
| 925 | ret = unlink(fname); |
| 926 | DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 927 | DVR_RETURN_IF_FALSE(ret == 0); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 928 | |
| 929 | /*delete index file*/ |
| 930 | memset(fname, 0, sizeof(fname)); |
| 931 | segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX); |
| 932 | unlink(fname); |
| 933 | DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 934 | DVR_RETURN_IF_FALSE(ret == 0); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 935 | |
| 936 | /*delete store information file*/ |
| 937 | memset(fname, 0, sizeof(fname)); |
| 938 | segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT); |
| 939 | unlink(fname); |
| 940 | DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 941 | DVR_RETURN_IF_FALSE(ret == 0); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 942 | |
| 943 | return DVR_SUCCESS; |
| 944 | } |
| 945 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 946 | int segment_ongoing(Segment_Handle_t handle) |
| 947 | { |
| 948 | Segment_Context_t *p_ctx; |
| 949 | p_ctx = (Segment_Context_t *)handle; |
| 950 | struct stat mstat; |
| 951 | |
| 952 | char going_name[MAX_SEGMENT_PATH_SIZE]; |
| 953 | memset(going_name, 0, sizeof(going_name)); |
| 954 | segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING); |
| 955 | int ret = stat(going_name, &mstat); |
| 956 | DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret); |
| 957 | if (ret != 0) { |
| 958 | return DVR_FAILURE; |
| 959 | } |
| 960 | return DVR_SUCCESS; |
| 961 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 962 | loff_t segment_dump_pts(Segment_Handle_t handle) |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 963 | { |
| 964 | Segment_Context_t *p_ctx; |
| 965 | char buf[256]; |
| 966 | char value[256]; |
| 967 | uint64_t pts; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 968 | loff_t offset; |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 969 | char *p1, *p2; |
| 970 | |
| 971 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 972 | DVR_RETURN_IF_FALSE(p_ctx); |
| 973 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 974 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 975 | |
| 976 | memset(buf, 0, sizeof(buf)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 977 | DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 978 | printf("start gets pts\n"); |
| 979 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
| 980 | printf("buf[%s]\n", buf); |
| 981 | memset(value, 0, sizeof(value)); |
| 982 | if ((p1 = strstr(buf, "time="))) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 983 | p1 += 5; |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 984 | if ((p2 = strstr(buf, ","))) { |
| 985 | memcpy(value, p1, p2 - p1); |
| 986 | } |
| 987 | pts = strtoull(value, NULL, 10); |
| 988 | } |
| 989 | |
| 990 | memset(value, 0, sizeof(value)); |
| 991 | if ((p1 = strstr(buf, "offset="))) { |
| 992 | p1 += 7; |
| 993 | if ((p2 = strstr(buf, "}"))) { |
| 994 | memcpy(value, p1, p2 - p1); |
| 995 | } |
| 996 | offset = strtoull(value, NULL, 10); |
| 997 | } |
| 998 | |
| 999 | memset(buf, 0, sizeof(buf)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 1000 | printf("pts=%llu, offset=%lld\n", pts, offset); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | return 0; |
| 1004 | } |