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) |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 15 | #define PCR_RECORD_INTERVAL_MS (400) |
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)); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 144 | free(p_ctx); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 145 | *p_handle = NULL; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 146 | return DVR_FAILURE; |
| 147 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 148 | p_ctx->segment_id = params->segment_id; |
| 149 | strncpy(p_ctx->location, params->location, strlen(params->location)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 150 | |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 151 | //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] | 152 | *p_handle = (Segment_Handle_t)p_ctx; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 153 | return DVR_SUCCESS; |
| 154 | } |
| 155 | |
| 156 | int segment_close(Segment_Handle_t handle) |
| 157 | { |
| 158 | Segment_Context_t *p_ctx; |
| 159 | |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 160 | p_ctx = (void *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 161 | DVR_RETURN_IF_FALSE(p_ctx); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 162 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 163 | if (p_ctx->ts_fd != -1) { |
| 164 | close(p_ctx->ts_fd); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | if (p_ctx->index_fp) { |
| 168 | fclose(p_ctx->index_fp); |
| 169 | } |
| 170 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 171 | if (p_ctx->dat_fp) { |
| 172 | fclose(p_ctx->dat_fp); |
| 173 | } |
| 174 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 175 | if (p_ctx->ongoing_fp != NULL) { |
| 176 | fclose(p_ctx->ongoing_fp); |
| 177 | char going_name[MAX_SEGMENT_PATH_SIZE]; |
| 178 | memset(going_name, 0, sizeof(going_name)); |
| 179 | segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING); |
| 180 | DVR_DEBUG(1, "segment close del [%s]", going_name); |
| 181 | unlink(going_name); |
| 182 | } |
| 183 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 184 | free(p_ctx); |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | ssize_t segment_read(Segment_Handle_t handle, void *buf, size_t count) |
| 189 | { |
| 190 | Segment_Context_t *p_ctx; |
| 191 | |
| 192 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 193 | DVR_RETURN_IF_FALSE(p_ctx); |
| 194 | DVR_RETURN_IF_FALSE(buf); |
| 195 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 196 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 197 | return read(p_ctx->ts_fd, buf, count); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | ssize_t segment_write(Segment_Handle_t handle, void *buf, size_t count) |
| 201 | { |
| 202 | Segment_Context_t *p_ctx; |
| 203 | |
| 204 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 205 | DVR_RETURN_IF_FALSE(p_ctx); |
| 206 | DVR_RETURN_IF_FALSE(buf); |
| 207 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 208 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 209 | return write(p_ctx->ts_fd, buf, count); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 210 | } |
| 211 | |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 212 | 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] | 213 | { |
| 214 | Segment_Context_t *p_ctx; |
| 215 | char buf[256]; |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 216 | int record_diff = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 217 | |
| 218 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 219 | DVR_RETURN_IF_FALSE(p_ctx); |
| 220 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 221 | |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 222 | if (p_ctx->first_pts == ULLONG_MAX) { |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 223 | DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 224 | p_ctx->first_pts = pts; |
| 225 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 226 | memset(buf, 0, sizeof(buf)); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 227 | if (p_ctx->last_pts == ULLONG_MAX) { |
| 228 | /*Last pts is init value*/ |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 229 | sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 230 | p_ctx->cur_time = pts - p_ctx->first_pts; |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 231 | DVR_DEBUG(1, "%s force pcr:%llu -1", __func__, pts); |
| 232 | } else { |
| 233 | /*Last pts has valid value*/ |
| 234 | int diff = pts - p_ctx->last_pts; |
| 235 | if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) { |
| 236 | /*Current pts has a transition*/ |
| 237 | DVR_DEBUG(1, "[%s]force update Current pts has a transition, [%llu, %llu, %llu]",__func__, |
| 238 | p_ctx->first_pts, p_ctx->last_pts, pts); |
| 239 | sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset); |
| 240 | } else { |
| 241 | /*This is a normal pts, record it*/ |
| 242 | p_ctx->cur_time += diff; |
| 243 | DVR_DEBUG(1, "%s force pcr:%llu -1 diff [%d]", __func__, pts, diff); |
| 244 | sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | record_diff = pts - p_ctx->last_record_pts; |
| 249 | if (strlen(buf) > 0) { |
| 250 | DVR_DEBUG(1, "%s force pcr:%llu buf:%s", __func__, pts, buf); |
| 251 | fputs(buf, p_ctx->index_fp); |
| 252 | fflush(p_ctx->index_fp); |
| 253 | fsync(fileno(p_ctx->index_fp)); |
| 254 | p_ctx->last_record_pts = pts; |
| 255 | } |
| 256 | p_ctx->last_pts = pts; |
| 257 | |
| 258 | return DVR_SUCCESS; |
| 259 | } |
| 260 | |
| 261 | int segment_update_pts(Segment_Handle_t handle, uint64_t pts, loff_t offset) |
| 262 | { |
| 263 | Segment_Context_t *p_ctx; |
| 264 | char buf[256]; |
| 265 | int record_diff = 0; |
| 266 | |
| 267 | p_ctx = (Segment_Context_t *)handle; |
| 268 | DVR_RETURN_IF_FALSE(p_ctx); |
| 269 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 270 | |
| 271 | if (p_ctx->first_pts == ULLONG_MAX) { |
| 272 | DVR_DEBUG(1, "%s first pcr:%llu", __func__, pts); |
| 273 | p_ctx->first_pts = pts; |
| 274 | //p_ctx->cur_time = p_ctx->cur_time + PTS_HEAD_DEVIATION; |
| 275 | } |
| 276 | memset(buf, 0, sizeof(buf)); |
| 277 | if (p_ctx->last_pts == ULLONG_MAX) { |
| 278 | /*Last pts is init value*/ |
| 279 | sprintf(buf, "{time=%llu, offset=%lld}", pts - p_ctx->first_pts, offset); |
| 280 | p_ctx->cur_time = pts - p_ctx->first_pts; |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 281 | } else { |
| 282 | /*Last pts has valid value*/ |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 283 | int diff = pts - p_ctx->last_pts; |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 284 | if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) { |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 285 | /*Current pts has a transition*/ |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 286 | DVR_DEBUG(1, "Current pts has a transition, [%llu, %llu, %llu]", |
| 287 | p_ctx->first_pts, p_ctx->last_pts, pts); |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 288 | p_ctx->last_record_pts = pts; |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 289 | //p_ctx->cur_time = p_ctx->cur_time + PTS_DISCONTINE_DEVIATION; |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 290 | } else { |
| 291 | /*This is a normal pts, record it*/ |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 292 | p_ctx->cur_time += diff; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 293 | sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 294 | } |
| 295 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 296 | |
| 297 | record_diff = pts - p_ctx->last_record_pts; |
| 298 | if (strlen(buf) > 0 && |
| 299 | (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] | 300 | fputs(buf, p_ctx->index_fp); |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 301 | fflush(p_ctx->index_fp); |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 302 | //fsync(fileno(p_ctx->index_fp)); |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 303 | p_ctx->last_record_pts = pts; |
| 304 | } |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 305 | p_ctx->last_pts = pts; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 306 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 307 | return DVR_SUCCESS; |
| 308 | } |
| 309 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 310 | 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] | 311 | { |
| 312 | Segment_Context_t *p_ctx; |
| 313 | char buf[256]; |
| 314 | char value[256]; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 315 | uint64_t pts = 0L; |
| 316 | loff_t offset = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 317 | char *p1, *p2; |
| 318 | |
| 319 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 320 | DVR_RETURN_IF_FALSE(p_ctx); |
| 321 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 322 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 323 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 324 | if (time == 0) { |
| 325 | offset = 0; |
| 326 | DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time); |
| 327 | DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1); |
| 328 | return offset; |
| 329 | } |
| 330 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 331 | memset(buf, 0, sizeof(buf)); |
| 332 | 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] | 333 | int line = 0; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 334 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 335 | line++; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 336 | memset(value, 0, sizeof(value)); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 337 | if ((p1 = strstr(buf, "time="))) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 338 | p1 += 5; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 339 | if ((p2 = strstr(buf, ","))) { |
| 340 | memcpy(value, p1, p2 - p1); |
| 341 | } |
| 342 | pts = strtoull(value, NULL, 10); |
| 343 | } |
| 344 | |
| 345 | memset(value, 0, sizeof(value)); |
| 346 | if ((p1 = strstr(buf, "offset="))) { |
| 347 | p1 += 7; |
| 348 | if ((p2 = strstr(buf, "}"))) { |
| 349 | memcpy(value, p1, p2 - p1); |
| 350 | } |
| 351 | offset = strtoull(value, NULL, 10); |
| 352 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 353 | if (0) |
| 354 | { |
| 355 | DVR_DEBUG(1, "seek buf[%s]", buf); |
| 356 | DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset); |
| 357 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 358 | memset(buf, 0, sizeof(buf)); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 359 | //DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset); |
| 360 | if (time <= pts) { |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 361 | if (block_size > 0) { |
| 362 | offset = offset - offset%block_size; |
| 363 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 364 | 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] | 365 | 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] | 366 | return offset; |
| 367 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 368 | } |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 369 | if (time > pts) { |
| 370 | if (block_size > 0) { |
| 371 | offset = offset - offset%block_size; |
| 372 | } |
| 373 | DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu line %d end\n", pts, offset, time, line); |
| 374 | DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1); |
| 375 | return offset; |
| 376 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 377 | DVR_DEBUG(1, "seek error line [%d]", line); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 378 | return DVR_FAILURE; |
| 379 | } |
| 380 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 381 | loff_t segment_tell_position(Segment_Handle_t handle) |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 382 | { |
| 383 | Segment_Context_t *p_ctx; |
| 384 | |
| 385 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 386 | DVR_RETURN_IF_FALSE(p_ctx); |
| 387 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 388 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 389 | return lseek64(p_ctx->ts_fd, 0, SEEK_CUR); |
| 390 | } |
| 391 | |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 392 | uint64_t segment_tell_current_time(Segment_Handle_t handle) |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 393 | { |
| 394 | Segment_Context_t *p_ctx; |
| 395 | char buf[256]; |
| 396 | char value[256]; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 397 | uint64_t pts = 0L; |
| 398 | loff_t offset = 0, position = 0; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 399 | char *p1, *p2; |
| 400 | |
| 401 | p_ctx = (Segment_Context_t *)handle; |
| 402 | DVR_RETURN_IF_FALSE(p_ctx); |
| 403 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 404 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd); |
| 405 | |
| 406 | memset(buf, 0, sizeof(buf)); |
| 407 | DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1); |
| 408 | position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR); |
| 409 | DVR_RETURN_IF_FALSE(position != -1); |
| 410 | |
| 411 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
| 412 | memset(value, 0, sizeof(value)); |
| 413 | if ((p1 = strstr(buf, "time="))) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 414 | p1 += 5; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 415 | if ((p2 = strstr(buf, ","))) { |
| 416 | memcpy(value, p1, p2 - p1); |
| 417 | } |
| 418 | pts = strtoull(value, NULL, 10); |
| 419 | } |
| 420 | |
| 421 | memset(value, 0, sizeof(value)); |
| 422 | if ((p1 = strstr(buf, "offset="))) { |
| 423 | p1 += 7; |
| 424 | if ((p2 = strstr(buf, "}"))) { |
| 425 | memcpy(value, p1, p2 - p1); |
| 426 | } |
| 427 | offset = strtoull(value, NULL, 10); |
| 428 | } |
| 429 | |
| 430 | memset(buf, 0, sizeof(buf)); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 431 | //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] | 432 | if (position <= offset) { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 433 | return pts; |
| 434 | } |
| 435 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 436 | //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] | 437 | return pts; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 438 | } |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 439 | |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 440 | uint64_t segment_tell_total_time(Segment_Handle_t handle) |
| 441 | { |
| 442 | Segment_Context_t *p_ctx; |
| 443 | char buf[256]; |
| 444 | char last_buf[256]; |
| 445 | char value[256]; |
| 446 | uint64_t pts = ULLONG_MAX; |
| 447 | loff_t offset = 0, position = 0; |
| 448 | char *p1, *p2; |
| 449 | int line = 0; |
| 450 | |
| 451 | p_ctx = (Segment_Context_t *)handle; |
| 452 | DVR_RETURN_IF_FALSE(p_ctx); |
| 453 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 454 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd); |
| 455 | |
| 456 | memset(buf, 0, sizeof(buf)); |
| 457 | memset(last_buf, 0, sizeof(last_buf)); |
| 458 | position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR); |
| 459 | DVR_RETURN_IF_FALSE(position != -1); |
| 460 | |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 461 | //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1); |
| 462 | //if seek error.we need seek 0 pos. |
| 463 | if (fseek(p_ctx->index_fp, -1000L, SEEK_END) == -1) { |
| 464 | fseek(p_ctx->index_fp, 0L, SEEK_SET); |
| 465 | } |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 466 | /* Save last line buffer */ |
| 467 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 468 | if (strlen(buf) <= 0) { |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 469 | DVR_DEBUG(1, "read index buf is len 0"); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 470 | continue; |
| 471 | } |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 472 | memset(last_buf, 0, sizeof(last_buf)); |
| 473 | memcpy(last_buf, buf, strlen(buf)); |
| 474 | memset(buf, 0, sizeof(buf)); |
| 475 | line++; |
| 476 | } |
| 477 | |
| 478 | /* Extract time value */ |
| 479 | memset(value, 0, sizeof(value)); |
| 480 | if ((p1 = strstr(last_buf, "time="))) { |
| 481 | p1 += 5; |
| 482 | if ((p2 = strstr(last_buf, ","))) { |
| 483 | memcpy(value, p1, p2 - p1); |
| 484 | } |
| 485 | pts = strtoull(value, NULL, 10); |
| 486 | } |
| 487 | |
| 488 | memset(value, 0, sizeof(value)); |
| 489 | if ((p1 = strstr(last_buf, "offset="))) { |
| 490 | p1 += 7; |
| 491 | if ((p2 = strstr(last_buf, "}"))) { |
| 492 | memcpy(value, p1, p2 - p1); |
| 493 | } |
| 494 | offset = strtoull(value, NULL, 10); |
| 495 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 496 | //if (line < 2) |
| 497 | //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] | 498 | return (pts == ULLONG_MAX ? DVR_FAILURE : pts); |
| 499 | } |
| 500 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 501 | /* Should consider the case of cut power, todo... */ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 502 | int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info) |
| 503 | { |
| 504 | Segment_Context_t *p_ctx; |
| 505 | char buf[256]; |
| 506 | uint32_t i; |
| 507 | |
| 508 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 509 | DVR_RETURN_IF_FALSE(p_ctx); |
| 510 | DVR_RETURN_IF_FALSE(p_ctx->dat_fp); |
| 511 | DVR_RETURN_IF_FALSE(p_info); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 512 | //seek 0, rewrite info |
| 513 | 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] | 514 | |
| 515 | /*Save segment id*/ |
| 516 | memset(buf, 0, sizeof(buf)); |
| 517 | sprintf(buf, "id=%lld\n", p_info->id); |
| 518 | fputs(buf, p_ctx->dat_fp); |
| 519 | |
| 520 | /*Save number of pids*/ |
| 521 | memset(buf, 0, sizeof(buf)); |
| 522 | sprintf(buf, "nb_pids=%d\n", p_info->nb_pids); |
| 523 | fputs(buf, p_ctx->dat_fp); |
| 524 | |
| 525 | /*Save pid information*/ |
| 526 | for (i = 0; i < p_info->nb_pids; i++) { |
| 527 | memset(buf, 0, sizeof(buf)); |
| 528 | sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type); |
| 529 | fputs(buf, p_ctx->dat_fp); |
| 530 | } |
| 531 | |
| 532 | /*Save segment duration*/ |
| 533 | memset(buf, 0, sizeof(buf)); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 534 | DVR_DEBUG(1, "duration store:[%ld]", p_info->duration); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 535 | sprintf(buf, "duration=%ld\n", p_info->duration); |
| 536 | fputs(buf, p_ctx->dat_fp); |
| 537 | |
| 538 | /*Save segment size*/ |
| 539 | memset(buf, 0, sizeof(buf)); |
| 540 | sprintf(buf, "size=%zu\n", p_info->size); |
| 541 | fputs(buf, p_ctx->dat_fp); |
| 542 | |
| 543 | /*Save number of packets*/ |
| 544 | memset(buf, 0, sizeof(buf)); |
| 545 | sprintf(buf, "nb_packets=%d\n", p_info->nb_packets); |
| 546 | fputs(buf, p_ctx->dat_fp); |
| 547 | |
| 548 | fflush(p_ctx->dat_fp); |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 549 | //fsync(fileno(p_ctx->dat_fp)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 550 | return DVR_SUCCESS; |
| 551 | } |
| 552 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 553 | /* Should consider the case of cut power, todo... */ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 554 | int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info) |
| 555 | { |
| 556 | Segment_Context_t *p_ctx; |
| 557 | uint32_t i; |
| 558 | char buf[256]; |
| 559 | char value[256]; |
| 560 | char *p1, *p2; |
| 561 | |
| 562 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 563 | DVR_RETURN_IF_FALSE(p_ctx); |
| 564 | DVR_RETURN_IF_FALSE(p_info); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 565 | |
| 566 | /*Load segment id*/ |
| 567 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 568 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 569 | p1 = strstr(buf, "id="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 570 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 571 | p_info->id = strtoull(p1 + 3, NULL, 10); |
| 572 | |
| 573 | /*Save number of pids*/ |
| 574 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 575 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 576 | p1 = strstr(buf, "nb_pids="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 577 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 578 | p_info->nb_pids = strtoull(p1 + 8, NULL, 10); |
| 579 | |
| 580 | /*Save pid information*/ |
| 581 | for (i = 0; i < p_info->nb_pids; i++) { |
| 582 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 583 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 584 | memset(value, 0, sizeof(value)); |
| 585 | if ((p1 = strstr(buf, "pid="))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 586 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 587 | p1 += 4; |
| 588 | if ((p2 = strstr(buf, ","))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 589 | DVR_RETURN_IF_FALSE(p2); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 590 | memcpy(value, p1, p2 - p1); |
| 591 | } |
| 592 | p_info->pids[i].pid = strtoull(value, NULL, 10); |
| 593 | } |
| 594 | |
| 595 | memset(value, 0, sizeof(value)); |
| 596 | if ((p1 = strstr(buf, "type="))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 597 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 598 | p1 += 5; |
| 599 | if ((p2 = strstr(buf, "}"))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 600 | DVR_RETURN_IF_FALSE(p2); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 601 | memcpy(value, p1, p2 - p1); |
| 602 | } |
| 603 | p_info->pids[i].type = strtoull(value, NULL, 10); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /*Save segment duration*/ |
| 608 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 609 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 610 | p1 = strstr(buf, "duration="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 611 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 612 | p_info->duration = strtoull(p1 + 9, NULL, 10); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 613 | //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 614 | |
| 615 | /*Save segment size*/ |
| 616 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 617 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 618 | p1 = strstr(buf, "size="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 619 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 620 | p_info->size = strtoull(p1 + 5, NULL, 10); |
| 621 | |
| 622 | /*Save number of packets*/ |
| 623 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 624 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 625 | p1 = strstr(buf, "nb_packets="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 626 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 627 | p_info->nb_packets = strtoull(p1 + 11, NULL, 10); |
| 628 | |
| 629 | return DVR_SUCCESS; |
| 630 | } |
| 631 | |
| 632 | int segment_delete(const char *location, uint64_t segment_id) |
| 633 | { |
| 634 | char fname[MAX_SEGMENT_PATH_SIZE]; |
| 635 | int ret; |
| 636 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 637 | DVR_RETURN_IF_FALSE(location); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 638 | |
| 639 | /*delete ts file*/ |
| 640 | memset(fname, 0, sizeof(fname)); |
| 641 | segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS); |
| 642 | ret = unlink(fname); |
| 643 | DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 644 | DVR_RETURN_IF_FALSE(ret == 0); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 645 | |
| 646 | /*delete index file*/ |
| 647 | memset(fname, 0, sizeof(fname)); |
| 648 | segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX); |
| 649 | unlink(fname); |
| 650 | DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 651 | DVR_RETURN_IF_FALSE(ret == 0); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 652 | |
| 653 | /*delete store information file*/ |
| 654 | memset(fname, 0, sizeof(fname)); |
| 655 | segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT); |
| 656 | unlink(fname); |
| 657 | DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 658 | DVR_RETURN_IF_FALSE(ret == 0); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 659 | |
| 660 | return DVR_SUCCESS; |
| 661 | } |
| 662 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 663 | int segment_ongoing(Segment_Handle_t handle) |
| 664 | { |
| 665 | Segment_Context_t *p_ctx; |
| 666 | p_ctx = (Segment_Context_t *)handle; |
| 667 | struct stat mstat; |
| 668 | |
| 669 | char going_name[MAX_SEGMENT_PATH_SIZE]; |
| 670 | memset(going_name, 0, sizeof(going_name)); |
| 671 | segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING); |
| 672 | int ret = stat(going_name, &mstat); |
| 673 | DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret); |
| 674 | if (ret != 0) { |
| 675 | return DVR_FAILURE; |
| 676 | } |
| 677 | return DVR_SUCCESS; |
| 678 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 679 | loff_t segment_dump_pts(Segment_Handle_t handle) |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 680 | { |
| 681 | Segment_Context_t *p_ctx; |
| 682 | char buf[256]; |
| 683 | char value[256]; |
| 684 | uint64_t pts; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 685 | loff_t offset; |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 686 | char *p1, *p2; |
| 687 | |
| 688 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 689 | DVR_RETURN_IF_FALSE(p_ctx); |
| 690 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 691 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 692 | |
| 693 | memset(buf, 0, sizeof(buf)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 694 | 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] | 695 | printf("start gets pts\n"); |
| 696 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
| 697 | printf("buf[%s]\n", buf); |
| 698 | memset(value, 0, sizeof(value)); |
| 699 | if ((p1 = strstr(buf, "time="))) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 700 | p1 += 5; |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 701 | if ((p2 = strstr(buf, ","))) { |
| 702 | memcpy(value, p1, p2 - p1); |
| 703 | } |
| 704 | pts = strtoull(value, NULL, 10); |
| 705 | } |
| 706 | |
| 707 | memset(value, 0, sizeof(value)); |
| 708 | if ((p1 = strstr(buf, "offset="))) { |
| 709 | p1 += 7; |
| 710 | if ((p2 = strstr(buf, "}"))) { |
| 711 | memcpy(value, p1, p2 - p1); |
| 712 | } |
| 713 | offset = strtoull(value, NULL, 10); |
| 714 | } |
| 715 | |
| 716 | memset(buf, 0, sizeof(buf)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 717 | printf("pts=%llu, offset=%lld\n", pts, offset); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | return 0; |
| 721 | } |