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