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