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 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +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 | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 144 | //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] | 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*/ |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 225 | int diff = pts - p_ctx->last_pts; |
| 226 | if ((diff > MAX_PTS_THRESHOLD) || (diff < 0)) { |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 227 | /*Current pts has a transition*/ |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 228 | DVR_DEBUG(1, "Current pts has a transition, [%llu, %llu, %llu]", |
| 229 | p_ctx->first_pts, p_ctx->last_pts, pts); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 230 | } else { |
| 231 | /*This is a normal pts, record it*/ |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 232 | p_ctx->cur_time += diff; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 233 | sprintf(buf, "\n{time=%llu, offset=%lld}", p_ctx->cur_time, offset); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 234 | } |
| 235 | } |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 236 | if (strlen(buf) > 0) |
| 237 | fputs(buf, p_ctx->index_fp); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 238 | p_ctx->last_pts = pts; |
| 239 | fflush(p_ctx->index_fp); |
| 240 | fsync(fileno(p_ctx->index_fp)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 241 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 242 | return DVR_SUCCESS; |
| 243 | } |
| 244 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 245 | 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] | 246 | { |
| 247 | Segment_Context_t *p_ctx; |
| 248 | char buf[256]; |
| 249 | char value[256]; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 250 | uint64_t pts = 0L; |
| 251 | loff_t offset = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 252 | char *p1, *p2; |
| 253 | |
| 254 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 255 | DVR_RETURN_IF_FALSE(p_ctx); |
| 256 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 257 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 258 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 259 | if (time == 0) { |
| 260 | offset = 0; |
| 261 | DVR_DEBUG(1, "seek time=%llu, offset=%lld time--%llu\n", pts, offset, time); |
| 262 | DVR_RETURN_IF_FALSE(lseek64(p_ctx->ts_fd, offset, SEEK_SET) != -1); |
| 263 | return offset; |
| 264 | } |
| 265 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 266 | memset(buf, 0, sizeof(buf)); |
| 267 | 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] | 268 | int line = 0; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 269 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 270 | line++; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 271 | memset(value, 0, sizeof(value)); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 272 | if ((p1 = strstr(buf, "time="))) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 273 | p1 += 5; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 274 | if ((p2 = strstr(buf, ","))) { |
| 275 | memcpy(value, p1, p2 - p1); |
| 276 | } |
| 277 | pts = strtoull(value, NULL, 10); |
| 278 | } |
| 279 | |
| 280 | memset(value, 0, sizeof(value)); |
| 281 | if ((p1 = strstr(buf, "offset="))) { |
| 282 | p1 += 7; |
| 283 | if ((p2 = strstr(buf, "}"))) { |
| 284 | memcpy(value, p1, p2 - p1); |
| 285 | } |
| 286 | offset = strtoull(value, NULL, 10); |
| 287 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 288 | if (0) |
| 289 | { |
| 290 | DVR_DEBUG(1, "seek buf[%s]", buf); |
| 291 | DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset); |
| 292 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 293 | memset(buf, 0, sizeof(buf)); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 294 | //DVR_DEBUG(1, "seek time=%llu, offset=%lld\n", pts, offset); |
| 295 | if (time <= pts) { |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 296 | if (block_size > 0) { |
| 297 | offset = offset - offset%block_size; |
| 298 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 299 | 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] | 300 | 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] | 301 | return offset; |
| 302 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 303 | } |
| 304 | DVR_DEBUG(1, "seek error line [%d]", line); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 305 | return DVR_FAILURE; |
| 306 | } |
| 307 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 308 | loff_t segment_tell_position(Segment_Handle_t handle) |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 309 | { |
| 310 | Segment_Context_t *p_ctx; |
| 311 | |
| 312 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 313 | DVR_RETURN_IF_FALSE(p_ctx); |
| 314 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 315 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 316 | return lseek64(p_ctx->ts_fd, 0, SEEK_CUR); |
| 317 | } |
| 318 | |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 319 | uint64_t segment_tell_current_time(Segment_Handle_t handle) |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 320 | { |
| 321 | Segment_Context_t *p_ctx; |
| 322 | char buf[256]; |
| 323 | char value[256]; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 324 | uint64_t pts = 0L; |
| 325 | loff_t offset = 0, position = 0; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 326 | char *p1, *p2; |
| 327 | |
| 328 | p_ctx = (Segment_Context_t *)handle; |
| 329 | DVR_RETURN_IF_FALSE(p_ctx); |
| 330 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 331 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd); |
| 332 | |
| 333 | memset(buf, 0, sizeof(buf)); |
| 334 | DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, 0, SEEK_SET) != -1); |
| 335 | position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR); |
| 336 | DVR_RETURN_IF_FALSE(position != -1); |
| 337 | |
| 338 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
| 339 | memset(value, 0, sizeof(value)); |
| 340 | if ((p1 = strstr(buf, "time="))) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 341 | p1 += 5; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 342 | if ((p2 = strstr(buf, ","))) { |
| 343 | memcpy(value, p1, p2 - p1); |
| 344 | } |
| 345 | pts = strtoull(value, NULL, 10); |
| 346 | } |
| 347 | |
| 348 | memset(value, 0, sizeof(value)); |
| 349 | if ((p1 = strstr(buf, "offset="))) { |
| 350 | p1 += 7; |
| 351 | if ((p2 = strstr(buf, "}"))) { |
| 352 | memcpy(value, p1, p2 - p1); |
| 353 | } |
| 354 | offset = strtoull(value, NULL, 10); |
| 355 | } |
| 356 | |
| 357 | memset(buf, 0, sizeof(buf)); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 358 | //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] | 359 | if (position <= offset) { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 360 | return pts; |
| 361 | } |
| 362 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 363 | //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] | 364 | return pts; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 365 | } |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 366 | |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 367 | uint64_t segment_tell_total_time(Segment_Handle_t handle) |
| 368 | { |
| 369 | Segment_Context_t *p_ctx; |
| 370 | char buf[256]; |
| 371 | char last_buf[256]; |
| 372 | char value[256]; |
| 373 | uint64_t pts = ULLONG_MAX; |
| 374 | loff_t offset = 0, position = 0; |
| 375 | char *p1, *p2; |
| 376 | int line = 0; |
| 377 | |
| 378 | p_ctx = (Segment_Context_t *)handle; |
| 379 | DVR_RETURN_IF_FALSE(p_ctx); |
| 380 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 381 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd); |
| 382 | |
| 383 | memset(buf, 0, sizeof(buf)); |
| 384 | memset(last_buf, 0, sizeof(last_buf)); |
| 385 | position = lseek64(p_ctx->ts_fd, 0, SEEK_CUR); |
| 386 | DVR_RETURN_IF_FALSE(position != -1); |
| 387 | |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 388 | //DVR_RETURN_IF_FALSE(fseek(p_ctx->index_fp, -1000L, SEEK_END) != -1); |
| 389 | //if seek error.we need seek 0 pos. |
| 390 | if (fseek(p_ctx->index_fp, -1000L, SEEK_END) == -1) { |
| 391 | fseek(p_ctx->index_fp, 0L, SEEK_SET); |
| 392 | } |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 393 | /* Save last line buffer */ |
| 394 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 395 | if (strlen(buf) <= 0) { |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 396 | DVR_DEBUG(1, "read index buf is len 0"); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 397 | continue; |
| 398 | } |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 399 | memset(last_buf, 0, sizeof(last_buf)); |
| 400 | memcpy(last_buf, buf, strlen(buf)); |
| 401 | memset(buf, 0, sizeof(buf)); |
| 402 | line++; |
| 403 | } |
| 404 | |
| 405 | /* Extract time value */ |
| 406 | memset(value, 0, sizeof(value)); |
| 407 | if ((p1 = strstr(last_buf, "time="))) { |
| 408 | p1 += 5; |
| 409 | if ((p2 = strstr(last_buf, ","))) { |
| 410 | memcpy(value, p1, p2 - p1); |
| 411 | } |
| 412 | pts = strtoull(value, NULL, 10); |
| 413 | } |
| 414 | |
| 415 | memset(value, 0, sizeof(value)); |
| 416 | if ((p1 = strstr(last_buf, "offset="))) { |
| 417 | p1 += 7; |
| 418 | if ((p2 = strstr(last_buf, "}"))) { |
| 419 | memcpy(value, p1, p2 - p1); |
| 420 | } |
| 421 | offset = strtoull(value, NULL, 10); |
| 422 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 423 | //if (line < 2) |
| 424 | //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] | 425 | return (pts == ULLONG_MAX ? DVR_FAILURE : pts); |
| 426 | } |
| 427 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 428 | /* Should consider the case of cut power, todo... */ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 429 | int segment_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info) |
| 430 | { |
| 431 | Segment_Context_t *p_ctx; |
| 432 | char buf[256]; |
| 433 | uint32_t i; |
| 434 | |
| 435 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 436 | DVR_RETURN_IF_FALSE(p_ctx); |
| 437 | DVR_RETURN_IF_FALSE(p_ctx->dat_fp); |
| 438 | DVR_RETURN_IF_FALSE(p_info); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 439 | //seek 0, rewrite info |
| 440 | 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] | 441 | |
| 442 | /*Save segment id*/ |
| 443 | memset(buf, 0, sizeof(buf)); |
| 444 | sprintf(buf, "id=%lld\n", p_info->id); |
| 445 | fputs(buf, p_ctx->dat_fp); |
| 446 | |
| 447 | /*Save number of pids*/ |
| 448 | memset(buf, 0, sizeof(buf)); |
| 449 | sprintf(buf, "nb_pids=%d\n", p_info->nb_pids); |
| 450 | fputs(buf, p_ctx->dat_fp); |
| 451 | |
| 452 | /*Save pid information*/ |
| 453 | for (i = 0; i < p_info->nb_pids; i++) { |
| 454 | memset(buf, 0, sizeof(buf)); |
| 455 | sprintf(buf, "{pid=%d, type=%d}\n", p_info->pids[i].pid, p_info->pids[i].type); |
| 456 | fputs(buf, p_ctx->dat_fp); |
| 457 | } |
| 458 | |
| 459 | /*Save segment duration*/ |
| 460 | memset(buf, 0, sizeof(buf)); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 461 | DVR_DEBUG(1, "duration store:[%ld]", p_info->duration); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 462 | sprintf(buf, "duration=%ld\n", p_info->duration); |
| 463 | fputs(buf, p_ctx->dat_fp); |
| 464 | |
| 465 | /*Save segment size*/ |
| 466 | memset(buf, 0, sizeof(buf)); |
| 467 | sprintf(buf, "size=%zu\n", p_info->size); |
| 468 | fputs(buf, p_ctx->dat_fp); |
| 469 | |
| 470 | /*Save number of packets*/ |
| 471 | memset(buf, 0, sizeof(buf)); |
| 472 | sprintf(buf, "nb_packets=%d\n", p_info->nb_packets); |
| 473 | fputs(buf, p_ctx->dat_fp); |
| 474 | |
| 475 | fflush(p_ctx->dat_fp); |
| 476 | fsync(fileno(p_ctx->dat_fp)); |
| 477 | return DVR_SUCCESS; |
| 478 | } |
| 479 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 480 | /* Should consider the case of cut power, todo... */ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 481 | int segment_load_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info) |
| 482 | { |
| 483 | Segment_Context_t *p_ctx; |
| 484 | uint32_t i; |
| 485 | char buf[256]; |
| 486 | char value[256]; |
| 487 | char *p1, *p2; |
| 488 | |
| 489 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 490 | DVR_RETURN_IF_FALSE(p_ctx); |
| 491 | DVR_RETURN_IF_FALSE(p_info); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 492 | |
| 493 | /*Load segment id*/ |
| 494 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 495 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 496 | p1 = strstr(buf, "id="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 497 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 498 | p_info->id = strtoull(p1 + 3, NULL, 10); |
| 499 | |
| 500 | /*Save number of pids*/ |
| 501 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 502 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 503 | p1 = strstr(buf, "nb_pids="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 504 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 505 | p_info->nb_pids = strtoull(p1 + 8, NULL, 10); |
| 506 | |
| 507 | /*Save pid information*/ |
| 508 | for (i = 0; i < p_info->nb_pids; i++) { |
| 509 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 510 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 511 | memset(value, 0, sizeof(value)); |
| 512 | if ((p1 = strstr(buf, "pid="))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 513 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 514 | p1 += 4; |
| 515 | if ((p2 = strstr(buf, ","))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 516 | DVR_RETURN_IF_FALSE(p2); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 517 | memcpy(value, p1, p2 - p1); |
| 518 | } |
| 519 | p_info->pids[i].pid = strtoull(value, NULL, 10); |
| 520 | } |
| 521 | |
| 522 | memset(value, 0, sizeof(value)); |
| 523 | if ((p1 = strstr(buf, "type="))) { |
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 | p1 += 5; |
| 526 | if ((p2 = strstr(buf, "}"))) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 527 | DVR_RETURN_IF_FALSE(p2); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 528 | memcpy(value, p1, p2 - p1); |
| 529 | } |
| 530 | p_info->pids[i].type = strtoull(value, NULL, 10); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | /*Save segment duration*/ |
| 535 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 536 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 537 | p1 = strstr(buf, "duration="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 538 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 539 | p_info->duration = strtoull(p1 + 9, NULL, 10); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 540 | //DVR_DEBUG(1, "load info p_info->duration:%lld", p_info->duration); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 541 | |
| 542 | /*Save segment size*/ |
| 543 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 544 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 545 | p1 = strstr(buf, "size="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 546 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 547 | p_info->size = strtoull(p1 + 5, NULL, 10); |
| 548 | |
| 549 | /*Save number of packets*/ |
| 550 | p1 = fgets(buf, sizeof(buf), p_ctx->dat_fp); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 551 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 552 | p1 = strstr(buf, "nb_packets="); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 553 | DVR_RETURN_IF_FALSE(p1); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 554 | p_info->nb_packets = strtoull(p1 + 11, NULL, 10); |
| 555 | |
| 556 | return DVR_SUCCESS; |
| 557 | } |
| 558 | |
| 559 | int segment_delete(const char *location, uint64_t segment_id) |
| 560 | { |
| 561 | char fname[MAX_SEGMENT_PATH_SIZE]; |
| 562 | int ret; |
| 563 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 564 | DVR_RETURN_IF_FALSE(location); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 565 | |
| 566 | /*delete ts file*/ |
| 567 | memset(fname, 0, sizeof(fname)); |
| 568 | segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_TS); |
| 569 | ret = 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 | /*delete index file*/ |
| 574 | memset(fname, 0, sizeof(fname)); |
| 575 | segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_INDEX); |
| 576 | unlink(fname); |
| 577 | DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 578 | DVR_RETURN_IF_FALSE(ret == 0); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 579 | |
| 580 | /*delete store information file*/ |
| 581 | memset(fname, 0, sizeof(fname)); |
| 582 | segment_get_fname(fname, location, segment_id, SEGMENT_FILE_TYPE_DAT); |
| 583 | unlink(fname); |
| 584 | DVR_DEBUG(1, "%s, [%s] return:%s", __func__, fname, strerror(errno)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 585 | DVR_RETURN_IF_FALSE(ret == 0); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 586 | |
| 587 | return DVR_SUCCESS; |
| 588 | } |
| 589 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 590 | int segment_ongoing(Segment_Handle_t handle) |
| 591 | { |
| 592 | Segment_Context_t *p_ctx; |
| 593 | p_ctx = (Segment_Context_t *)handle; |
| 594 | struct stat mstat; |
| 595 | |
| 596 | char going_name[MAX_SEGMENT_PATH_SIZE]; |
| 597 | memset(going_name, 0, sizeof(going_name)); |
| 598 | segment_get_fname(going_name, p_ctx->location, p_ctx->segment_id, SEGMENT_FILE_TYPE_ONGOING); |
| 599 | int ret = stat(going_name, &mstat); |
| 600 | DVR_DEBUG(1, "segment check ongoing [%s] ret [%d]", going_name, ret); |
| 601 | if (ret != 0) { |
| 602 | return DVR_FAILURE; |
| 603 | } |
| 604 | return DVR_SUCCESS; |
| 605 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 606 | loff_t segment_dump_pts(Segment_Handle_t handle) |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 607 | { |
| 608 | Segment_Context_t *p_ctx; |
| 609 | char buf[256]; |
| 610 | char value[256]; |
| 611 | uint64_t pts; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 612 | loff_t offset; |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 613 | char *p1, *p2; |
| 614 | |
| 615 | p_ctx = (Segment_Context_t *)handle; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 616 | DVR_RETURN_IF_FALSE(p_ctx); |
| 617 | DVR_RETURN_IF_FALSE(p_ctx->index_fp); |
| 618 | DVR_RETURN_IF_FALSE(p_ctx->ts_fd != -1); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 619 | |
| 620 | memset(buf, 0, sizeof(buf)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 621 | 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] | 622 | printf("start gets pts\n"); |
| 623 | while (fgets(buf, sizeof(buf), p_ctx->index_fp) != NULL) { |
| 624 | printf("buf[%s]\n", buf); |
| 625 | memset(value, 0, sizeof(value)); |
| 626 | if ((p1 = strstr(buf, "time="))) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 627 | p1 += 5; |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 628 | if ((p2 = strstr(buf, ","))) { |
| 629 | memcpy(value, p1, p2 - p1); |
| 630 | } |
| 631 | pts = strtoull(value, NULL, 10); |
| 632 | } |
| 633 | |
| 634 | memset(value, 0, sizeof(value)); |
| 635 | if ((p1 = strstr(buf, "offset="))) { |
| 636 | p1 += 7; |
| 637 | if ((p2 = strstr(buf, "}"))) { |
| 638 | memcpy(value, p1, p2 - p1); |
| 639 | } |
| 640 | offset = strtoull(value, NULL, 10); |
| 641 | } |
| 642 | |
| 643 | memset(buf, 0, sizeof(buf)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 644 | printf("pts=%llu, offset=%lld\n", pts, offset); |
Pengfei Liu | b038b6a | 2020-01-14 15:57:01 +0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | return 0; |
| 648 | } |