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