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; |
| 199 | |
| 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); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 204 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 205 | return read(p_ctx->ts_fd, buf, count); |
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 | |
| 328 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 329 | DVR_RETURN_IF_FALSE(p_ctx); |
| 330 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 331 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 332 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 333 | if (time == 0) { |
| 334 | offset = 0; |
hualing chen | 92f3a14 | 2020-07-08 20:59:33 +0800 | [diff] [blame] | 335 | //DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 336 | DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1); |
| 337 | return offset; |
| 338 | } |
| 339 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 340 | memset(buf, 0, sizeof(buf)); |
| 341 | 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] | 342 | int line = 0; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 343 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 344 | line++; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 345 | memset(value, 0, sizeof(value)); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 346 | if ((p1 = strstr(buf, "time="))) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 347 | p1 += 5; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 348 | if ((p2 = strstr(buf, ","))) { |
| 349 | memcpy(value, p1, p2 - p1); |
| 350 | } |
| 351 | pts = strtoull(value, NULL, 10); |
| 352 | } |
| 353 | |
| 354 | memset(value, 0, sizeof(value)); |
| 355 | if ((p1 = strstr(buf, "offset="))) { |
| 356 | p1 += 7; |
| 357 | if ((p2 = strstr(buf, "}"))) { |
| 358 | memcpy(value, p1, p2 - p1); |
| 359 | } |
| 360 | offset = strtoull(value, NULL, 10); |
| 361 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 362 | if (0) |
| 363 | { |
| 364 | DVR_DEBUG(1, "seek buf[%s]", buf); |
| 365 | DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset); |
| 366 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 367 | memset(buf, 0, sizeof(buf)); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 368 | //DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset); |
| 369 | if (time <= pts) { |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 370 | if (block_size > 0) { |
| 371 | offset = offset - offset%block_size; |
| 372 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 373 | DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d\n", pts, offset, time, line); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 374 | DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 375 | return offset; |
| 376 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 377 | } |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 378 | if (time > pts) { |
| 379 | if (block_size > 0) { |
| 380 | offset = offset - offset%block_size; |
| 381 | } |
| 382 | DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d end\n", pts, offset, time, line); |
| 383 | DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1); |
| 384 | return offset; |
| 385 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 386 | DVR_DEBUG(1, "seek error line [%d]", line); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 387 | return DVR_FAILURE; |
| 388 | } |
| 389 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 390 | loff_t segment_tell_position(Segment_Handle_t handle) |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 391 | { |
| 392 | Segment_Context_t *p_ctx; |
| 393 | |
| 394 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 395 | DVR_RETURN_IF_FALSE(p_ctx); |
| 396 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 397 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 398 | return lseek64(p_ctx->ts_fd, 0, SEEK_CUR); |
| 399 | } |
| 400 | |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 401 | uint64_t segment_tell_position_time(Segment_Handle_t handle, loff_t position) |
| 402 | { |
| 403 | Segment_Context_t *p_ctx; |
| 404 | char buf[256]; |
| 405 | char value[256]; |
hualing chen | 969fe7b | 2021-05-26 15:13:17 +0800 | [diff] [blame^] | 406 | uint64_t ret = 0L; |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 407 | uint64_t pts = 0L; |
hualing chen | 969fe7b | 2021-05-26 15:13:17 +0800 | [diff] [blame^] | 408 | uint64_t pts_p = 0L; |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 409 | loff_t offset = 0; |
hualing chen | 969fe7b | 2021-05-26 15:13:17 +0800 | [diff] [blame^] | 410 | loff_t offset_p = 0; |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 411 | char *p1, *p2; |
| 412 | |
| 413 | p_ctx = (Segment_Context_t *)handle; |
| 414 | DVR_RETURN_IF_FALSE(p_ctx); |
| 415 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 416 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd); |
| 417 | |
| 418 | memset(buf, 0, sizeof(buf)); |
| 419 | DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1); |
| 420 | DVR_RETURN_IF_FALSE(position != -1); |
| 421 | |
| 422 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
| 423 | memset(value, 0, sizeof(value)); |
| 424 | if ((p1 = strstr(buf, "time="))) { |
| 425 | p1 += 5; |
| 426 | if ((p2 = strstr(buf, ","))) { |
| 427 | memcpy(value, p1, p2 - p1); |
| 428 | } |
| 429 | pts = strtoull(value, NULL, 10); |
| 430 | } |
| 431 | |
| 432 | memset(value, 0, sizeof(value)); |
| 433 | if ((p1 = strstr(buf, "offset="))) { |
| 434 | p1 += 7; |
| 435 | if ((p2 = strstr(buf, "}"))) { |
| 436 | memcpy(value, p1, p2 - p1); |
| 437 | } |
| 438 | offset = strtoull(value, NULL, 10); |
| 439 | } |
| 440 | |
| 441 | memset(buf, 0, sizeof(buf)); |
| 442 | //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position); |
| 443 | if (position <= offset) { |
hualing chen | 969fe7b | 2021-05-26 15:13:17 +0800 | [diff] [blame^] | 444 | ret = pts_p + (pts - pts_p) * (position - offset_p) / (offset - offset_p); |
| 445 | 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); |
| 446 | return ret; |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 447 | } |
hualing chen | 969fe7b | 2021-05-26 15:13:17 +0800 | [diff] [blame^] | 448 | offset_p = offset; |
| 449 | pts_p = pts; |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 450 | } |
| 451 | //DVR_DEBUG(1, "tell cur time=%llu, offset=%lld, position=%lld\n", pts, offset, position); |
| 452 | return pts; |
| 453 | } |
| 454 | |
| 455 | |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 456 | uint64_t segment_tell_current_time(Segment_Handle_t handle) |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 457 | { |
| 458 | Segment_Context_t *p_ctx; |
| 459 | char buf[256]; |
| 460 | char value[256]; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 461 | uint64_t pts = 0L; |
| 462 | loff_t offset = 0, position = 0; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 463 | char *p1, *p2; |
| 464 | |
| 465 | p_ctx = (Segment_Context_t *)handle; |
| 466 | DVR_RETURN_IF_FALSE(p_ctx); |
| 467 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 468 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd); |
| 469 | |
| 470 | memset(buf, 0, sizeof(buf)); |
| 471 | DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1); |
| 472 | position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR); |
| 473 | DVR_RETURN_IF_FALSE(position != -1); |
| 474 | |
| 475 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
| 476 | memset(value, 0, sizeof(value)); |
| 477 | if ((p1 = strstr(buf, "time="))) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 478 | p1 += 5; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 479 | if ((p2 = strstr(buf, ","))) { |
| 480 | memcpy(value, p1, p2 - p1); |
| 481 | } |
| 482 | pts = strtoull(value, NULL, 10); |
| 483 | } |
| 484 | |
| 485 | memset(value, 0, sizeof(value)); |
| 486 | if ((p1 = strstr(buf, "offset="))) { |
| 487 | p1 += 7; |
| 488 | if ((p2 = strstr(buf, "}"))) { |
| 489 | memcpy(value, p1, p2 - p1); |
| 490 | } |
| 491 | offset = strtoull(value, NULL, 10); |
| 492 | } |
| 493 | |
| 494 | memset(buf, 0, sizeof(buf)); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 495 | //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] | 496 | if (position <= offset) { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 497 | return pts; |
| 498 | } |
| 499 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 500 | //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] | 501 | return pts; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 502 | } |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 503 | |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 504 | uint64_t segment_tell_total_time(Segment_Handle_t handle) |
| 505 | { |
| 506 | Segment_Context_t *p_ctx; |
| 507 | char buf[256]; |
| 508 | char last_buf[256]; |
| 509 | char value[256]; |
| 510 | uint64_t pts = ULLONG_MAX; |
| 511 | loff_t offset = 0, position = 0; |
| 512 | char *p1, *p2; |
| 513 | int line = 0; |
| 514 | |
| 515 | p_ctx = (Segment_Context_t *)handle; |
| 516 | DVR_RETURN_IF_FALSE(p_ctx); |
| 517 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 518 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd); |
| 519 | |
| 520 | memset(buf, 0, sizeof(buf)); |
| 521 | memset(last_buf, 0, sizeof(last_buf)); |
| 522 | position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR); |
| 523 | DVR_RETURN_IF_FALSE(position != -1); |
| 524 | |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 525 | //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1); |
| 526 | //if seek error.we need seek 0 pos. |
| 527 | if (fseek(p_ctx->index_fp, -1000L, SEEK_END) == -1) { |
| 528 | fseek(p_ctx->index_fp, 0L, SEEK_SET); |
| 529 | } |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 530 | /* Save last line buffer */ |
| 531 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 532 | if (strlen(buf) <= 0) { |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 533 | DVR_DEBUG(1, "read index buf is len 0"); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 534 | continue; |
| 535 | } |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 536 | memset(last_buf, 0, sizeof(last_buf)); |
| 537 | memcpy(last_buf, buf, strlen(buf)); |
| 538 | memset(buf, 0, sizeof(buf)); |
| 539 | line++; |
| 540 | } |
| 541 | |
| 542 | /* Extract time value */ |
| 543 | memset(value, 0, sizeof(value)); |
| 544 | if ((p1 = strstr(last_buf, "time="))) { |
| 545 | p1 += 5; |
| 546 | if ((p2 = strstr(last_buf, ","))) { |
| 547 | memcpy(value, p1, p2 - p1); |
| 548 | } |
| 549 | pts = strtoull(value, NULL, 10); |
| 550 | } |
| 551 | |
| 552 | memset(value, 0, sizeof(value)); |
| 553 | if ((p1 = strstr(last_buf, "offset="))) { |
| 554 | p1 += 7; |
| 555 | if ((p2 = strstr(last_buf, "}"))) { |
| 556 | memcpy(value, p1, p2 - p1); |
| 557 | } |
| 558 | offset = strtoull(value, NULL, 10); |
| 559 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 560 | //if (line < 2) |
| 561 | //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] | 562 | return (pts == ULLONG_MAX ? DVR_FAILURE : pts); |
| 563 | } |
| 564 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 565 | /* Should consider the case of cut power, todo... */ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 566 | int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info) |
| 567 | { |
| 568 | Segment_Context_t *p_ctx; |
| 569 | char buf[256]; |
| 570 | uint32_t i; |
| 571 | |
| 572 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 573 | DVR_RETURN_IF_FALSE(p_ctx); |
| 574 | DVR_RETURN_IF_FALSE(p_ctx->dat_fp); |
| 575 | DVR_RETURN_IF_FALSE(p_info); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 576 | //seek 0, rewrite info |
| 577 | 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] | 578 | |
| 579 | /*Save segment id*/ |
| 580 | memset(buf, 0, sizeof(buf)); |
| 581 | sprintf(buf, "id=%lld\n", p_info->id); |
| 582 | fputs(buf, p_ctx->dat_fp); |
| 583 | |
| 584 | /*Save number of pids*/ |
| 585 | memset(buf, 0, sizeof(buf)); |
| 586 | sprintf(buf, "nb_pids=%d\n", p_info->nb_pids); |
| 587 | fputs(buf, p_ctx->dat_fp); |
| 588 | |
| 589 | /*Save pid information*/ |
| 590 | for (i = 0; i < p_info->nb_pids; i++) { |
| 591 | memset(buf, 0, sizeof(buf)); |
| 592 | sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type); |
| 593 | fputs(buf, p_ctx->dat_fp); |
| 594 | } |
| 595 | |
| 596 | /*Save segment duration*/ |
| 597 | memset(buf, 0, sizeof(buf)); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 598 | DVR_DEBUG(1, "duration store:[%ld]", p_info->duration); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 599 | sprintf(buf, "duration=%ld\n", p_info->duration); |
| 600 | fputs(buf, p_ctx->dat_fp); |
| 601 | |
| 602 | /*Save segment size*/ |
| 603 | memset(buf, 0, sizeof(buf)); |
| 604 | sprintf(buf, "size=%zu\n", p_info->size); |
| 605 | fputs(buf, p_ctx->dat_fp); |
| 606 | |
| 607 | /*Save number of packets*/ |
| 608 | memset(buf, 0, sizeof(buf)); |
| 609 | sprintf(buf, "nb_packets=%d\n", p_info->nb_packets); |
| 610 | fputs(buf, p_ctx->dat_fp); |
| 611 | |
| 612 | fflush(p_ctx->dat_fp); |
hualing chen | 47c34bc | 2020-05-11 09:15:47 +0800 | [diff] [blame] | 613 | fsync(fileno(p_ctx->dat_fp)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 614 | return DVR_SUCCESS; |
| 615 | } |
| 616 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 617 | /* Should consider the case of cut power, todo... */ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 618 | int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info) |
| 619 | { |
| 620 | Segment_Context_t *p_ctx; |
| 621 | uint32_t i; |
| 622 | char buf[256]; |
| 623 | char value[256]; |
| 624 | char *p1, *p2; |
| 625 | |
| 626 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 627 | DVR_RETURN_IF_FALSE(p_ctx); |
| 628 | DVR_RETURN_IF_FALSE(p_info); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 629 | |
| 630 | /*Load segment id*/ |
| 631 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 632 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 633 | p1 = strstr(buf, "id="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 634 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 635 | p_info->id = strtoull(p1 + 3, NULL, 10); |
| 636 | |
| 637 | /*Save number of pids*/ |
| 638 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 639 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 640 | p1 = strstr(buf, "nb_pids="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 641 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 642 | p_info->nb_pids = strtoull(p1 + 8, NULL, 10); |
| 643 | |
| 644 | /*Save pid information*/ |
| 645 | for (i = 0; i < p_info->nb_pids; i++) { |
| 646 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 647 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 648 | memset(value, 0, sizeof(value)); |
| 649 | if ((p1 = strstr(buf, "pid="))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 650 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 651 | p1 += 4; |
| 652 | if ((p2 = strstr(buf, ","))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 653 | DVR_RETURN_IF_FALSE(p2); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 654 | memcpy(value, p1, p2 - p1); |
| 655 | } |
| 656 | p_info->pids[i].pid = strtoull(value, NULL, 10); |
| 657 | } |
| 658 | |
| 659 | memset(value, 0, sizeof(value)); |
| 660 | if ((p1 = strstr(buf, "type="))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 661 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 662 | p1 += 5; |
| 663 | if ((p2 = strstr(buf, "}"))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 664 | DVR_RETURN_IF_FALSE(p2); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 665 | memcpy(value, p1, p2 - p1); |
| 666 | } |
| 667 | p_info->pids[i].type = strtoull(value, NULL, 10); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | /*Save segment duration*/ |
| 672 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 673 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 674 | p1 = strstr(buf, "duration="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 675 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 676 | p_info->duration = strtoull(p1 + 9, NULL, 10); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 677 | //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 678 | |
| 679 | /*Save segment size*/ |
| 680 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 681 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 682 | p1 = strstr(buf, "size="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 683 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 684 | p_info->size = strtoull(p1 + 5, NULL, 10); |
| 685 | |
| 686 | /*Save number of packets*/ |
| 687 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 688 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 689 | p1 = strstr(buf, "nb_packets="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 690 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 691 | p_info->nb_packets = strtoull(p1 + 11, NULL, 10); |
| 692 | |
| 693 | return DVR_SUCCESS; |
| 694 | } |
| 695 | |
| 696 | int segment_delete(const char *location, uint64_t segment_id) |
| 697 | { |
| 698 | char fname[MAX_SEGMENT_PATH_SIZE]; |
| 699 | int ret; |
| 700 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 701 | DVR_RETURN_IF_FALSE(location); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 702 | |
| 703 | /*delete ts file*/ |
| 704 | memset(fname, 0, sizeof(fname)); |
| 705 | segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS); |
| 706 | ret = unlink(fname); |
| 707 | DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 708 | DVR_RETURN_IF_FALSE(ret == 0); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 709 | |
| 710 | /*delete index file*/ |
| 711 | memset(fname, 0, sizeof(fname)); |
| 712 | segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX); |
| 713 | unlink(fname); |
| 714 | DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 715 | DVR_RETURN_IF_FALSE(ret == 0); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 716 | |
| 717 | /*delete store information file*/ |
| 718 | memset(fname, 0, sizeof(fname)); |
| 719 | segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT); |
| 720 | unlink(fname); |
| 721 | DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 722 | DVR_RETURN_IF_FALSE(ret == 0); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 723 | |
| 724 | return DVR_SUCCESS; |
| 725 | } |
| 726 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 727 | int segment_ongoing(Segment_Handle_t handle) |
| 728 | { |
| 729 | Segment_Context_t *p_ctx; |
| 730 | p_ctx = (Segment_Context_t *)handle; |
| 731 | struct stat mstat; |
| 732 | |
| 733 | char going_name[MAX_SEGMENT_PATH_SIZE]; |
| 734 | memset(going_name, 0, sizeof(going_name)); |
| 735 | segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING); |
| 736 | int ret = stat(going_name, &mstat); |
| 737 | DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret); |
| 738 | if (ret != 0) { |
| 739 | return DVR_FAILURE; |
| 740 | } |
| 741 | return DVR_SUCCESS; |
| 742 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 743 | loff_t segment_dump_pts(Segment_Handle_t handle) |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 744 | { |
| 745 | Segment_Context_t *p_ctx; |
| 746 | char buf[256]; |
| 747 | char value[256]; |
| 748 | uint64_t pts; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 749 | loff_t offset; |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 750 | char *p1, *p2; |
| 751 | |
| 752 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 753 | DVR_RETURN_IF_FALSE(p_ctx); |
| 754 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 755 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 756 | |
| 757 | memset(buf, 0, sizeof(buf)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 758 | 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] | 759 | printf("start gets pts\n"); |
| 760 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
| 761 | printf("buf[%s]\n", buf); |
| 762 | memset(value, 0, sizeof(value)); |
| 763 | if ((p1 = strstr(buf, "time="))) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 764 | p1 += 5; |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 765 | if ((p2 = strstr(buf, ","))) { |
| 766 | memcpy(value, p1, p2 - p1); |
| 767 | } |
| 768 | pts = strtoull(value, NULL, 10); |
| 769 | } |
| 770 | |
| 771 | memset(value, 0, sizeof(value)); |
| 772 | if ((p1 = strstr(buf, "offset="))) { |
| 773 | p1 += 7; |
| 774 | if ((p2 = strstr(buf, "}"))) { |
| 775 | memcpy(value, p1, p2 - p1); |
| 776 | } |
| 777 | offset = strtoull(value, NULL, 10); |
| 778 | } |
| 779 | |
| 780 | memset(buf, 0, sizeof(buf)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 781 | printf("pts=%llu, offset=%lld\n", pts, offset); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | return 0; |
| 785 | } |