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