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