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