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