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