Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 2 | #include <unistd.h> |
| 3 | #include <stdlib.h> |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 4 | #include <string.h> |
Zhiqiang Han | e0a1c38 | 2021-06-08 11:28:05 +0800 | [diff] [blame^] | 5 | #include <errno.h> |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 6 | #include <pthread.h> |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 7 | #include "dvr_segment.h" |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 8 | #include <segment.h> |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 9 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 10 | /**\brief DVR segment file information*/ |
| 11 | typedef struct { |
| 12 | char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/ |
| 13 | uint64_t id; /**< DVR Segment id*/ |
| 14 | } DVR_SegmentFile_t; |
| 15 | |
| 16 | void *dvr_segment_thread(void *arg) |
| 17 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 18 | int ret; |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 19 | DVR_SegmentFile_t *segment_file = (DVR_SegmentFile_t*)arg; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 20 | |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 21 | pthread_detach(pthread_self()); |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 22 | DVR_DEBUG(1, "%s try to delete [%s-%lld]", __func__, segment_file->location, segment_file->id); |
| 23 | ret = segment_delete(segment_file->location, segment_file->id); |
| 24 | DVR_DEBUG(1, "%s delete segment [%s-%lld] %s", __func__, segment_file->location, segment_file->id, |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 25 | ret == DVR_SUCCESS ? "success" : "failed"); |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 26 | if (segment_file != NULL) { |
| 27 | //malloc at delete api.free at this |
| 28 | free(segment_file); |
| 29 | segment_file = NULL; |
| 30 | } |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 31 | return NULL; |
| 32 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 33 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 34 | int dvr_segment_delete(const char *location, uint64_t segment_id) |
| 35 | { |
hualing chen | 6d24aa9 | 2020-03-23 18:43:47 +0800 | [diff] [blame] | 36 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 37 | pthread_t thread; |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 38 | DVR_SegmentFile_t *segment; |
| 39 | //this segment will be free at del thread when used end.if thread |
| 40 | //creat error.will be free now. |
| 41 | segment = (DVR_SegmentFile_t *)malloc(sizeof(DVR_SegmentFile_t)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 42 | |
| 43 | DVR_DEBUG(1, "%s in, %s,id:%lld", __func__, location, segment_id); |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 44 | DVR_RETURN_IF_FALSE(segment); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 45 | DVR_RETURN_IF_FALSE(location); |
| 46 | DVR_RETURN_IF_FALSE(strlen(location) < DVR_MAX_LOCATION_SIZE); |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 47 | memset(segment->location, 0, sizeof(segment->location)); |
| 48 | memcpy(segment->location, location, strlen(location)); |
| 49 | segment->id = segment_id; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 50 | |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 51 | int ret = pthread_create(&thread, NULL, dvr_segment_thread, segment); |
| 52 | if (ret != 0) { |
| 53 | //creat thread error,need free segment |
| 54 | if (segment != NULL) { |
| 55 | free(segment); |
| 56 | segment = NULL; |
| 57 | } |
| 58 | } |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 59 | return DVR_SUCCESS; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 60 | } |
| 61 | |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 62 | int dvr_segment_del_by_location(const char *location) |
| 63 | { |
| 64 | FILE *fp; |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 65 | char cmd[DVR_MAX_LOCATION_SIZE + 64]; |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 66 | |
| 67 | DVR_RETURN_IF_FALSE(location); |
| 68 | |
| 69 | DVR_DEBUG(1, "%s location:%s", __func__, location); |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 70 | { |
| 71 | /* del file */ |
| 72 | memset(cmd, 0, sizeof(cmd)); |
Zhiqiang Han | e0a1c38 | 2021-06-08 11:28:05 +0800 | [diff] [blame^] | 73 | sprintf(cmd, "rm %s-*;rm %s.list", location, location); |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 74 | fp = popen(cmd, "r"); |
| 75 | DVR_RETURN_IF_FALSE(fp); |
| 76 | } |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 77 | DVR_DEBUG(1, "%s location:%s end", __func__, location); |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 78 | pclose(fp); |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 79 | return DVR_SUCCESS; |
| 80 | } |
| 81 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 82 | int dvr_segment_get_list(const char *location, uint32_t *p_segment_nb, uint64_t **pp_segment_ids) |
| 83 | { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 84 | FILE *fp; |
| 85 | char fpath[DVR_MAX_LOCATION_SIZE]; |
Zhiqiang Han | ee5ac41 | 2020-05-07 20:54:32 +0800 | [diff] [blame] | 86 | uint32_t i = 0, j = 0, n = 0; |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 87 | char buf[DVR_MAX_LOCATION_SIZE + 10]; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 88 | uint64_t *p = NULL; |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 89 | char cmd[DVR_MAX_LOCATION_SIZE + 64]; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 90 | |
| 91 | DVR_RETURN_IF_FALSE(location); |
| 92 | DVR_RETURN_IF_FALSE(p_segment_nb); |
| 93 | DVR_RETURN_IF_FALSE(pp_segment_ids); |
| 94 | |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 95 | //DVR_DEBUG(1, "%s location:%s", __func__, location); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 96 | memset(fpath, 0, sizeof(fpath)); |
| 97 | sprintf(fpath, "%s.list", location); |
| 98 | |
| 99 | if (access(fpath, 0) != -1) { |
| 100 | /*the list file is exist*/ |
| 101 | fp = fopen(fpath, "r"); |
| 102 | DVR_RETURN_IF_FALSE(fp); |
| 103 | /*get segment numbers*/ |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 104 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 105 | i++; |
| 106 | } |
| 107 | *p_segment_nb = i; |
| 108 | rewind(fp); |
| 109 | /*malloc*/ |
| 110 | p = malloc(i * sizeof(uint64_t)); |
| 111 | i = 0; |
| 112 | /*set value*/ |
| 113 | memset(buf, 0, sizeof(buf)); |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 114 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 115 | p[i++] = strtoull(buf, NULL, 10); |
| 116 | memset(buf, 0, sizeof(buf)); |
| 117 | } |
| 118 | *pp_segment_ids = p; |
| 119 | fclose(fp); |
Zhiqiang Han | e0a1c38 | 2021-06-08 11:28:05 +0800 | [diff] [blame^] | 120 | DVR_DEBUG(1, "%s location:%s segments:%d", __func__, location, i); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 121 | } else { |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 122 | uint32_t id = 0; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 123 | /*the list file does not exist*/ |
| 124 | memset(cmd, 0, sizeof(cmd)); |
Zhiqiang Han | ade34ff | 2020-04-17 16:50:59 +0800 | [diff] [blame] | 125 | sprintf(cmd, "ls -l %s-*.ts | wc -l", location); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 126 | fp = popen(cmd, "r"); |
| 127 | DVR_RETURN_IF_FALSE(fp); |
| 128 | memset(buf, 0, sizeof(buf)); |
| 129 | if (fgets(buf, sizeof(buf), fp) != NULL) { |
| 130 | i = strtoull(buf, NULL, 10); |
| 131 | pclose(fp); |
| 132 | } else { |
| 133 | pclose(fp); |
| 134 | return DVR_FAILURE; |
| 135 | } |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 136 | //DVR_DEBUG(1, "%s location:%s i: %d ls buf:%s", __func__, location, i, buf); |
Zhiqiang Han | ee5ac41 | 2020-05-07 20:54:32 +0800 | [diff] [blame] | 137 | n = i; |
| 138 | p = malloc(n * sizeof(uint64_t)); |
Zhiqiang Han | 8dd08b3 | 2020-05-13 16:50:04 +0800 | [diff] [blame] | 139 | /*try to get the 1st segment id*/ |
| 140 | memset(cmd, 0, sizeof(cmd)); |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 141 | sprintf(cmd, "ls %s-*.ts", location); |
Zhiqiang Han | 8dd08b3 | 2020-05-13 16:50:04 +0800 | [diff] [blame] | 142 | fp = popen(cmd, "r"); |
| 143 | DVR_RETURN_IF_FALSE(fp); |
| 144 | memset(buf, 0, sizeof(buf)); |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 145 | j = 0; |
| 146 | snprintf(fpath, sizeof(fpath), "%s-%%d.ts", location); |
| 147 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 148 | //DVR_DEBUG(1, "%s buf:%s", __func__, buf); |
| 149 | if (sscanf(buf, fpath, &id) != 1) { |
| 150 | DVR_DEBUG(1, "%s location:%s buf:%s not get id", __func__, location, buf); |
| 151 | id = 0; |
| 152 | n = n -1; |
| 153 | } else { |
| 154 | //DVR_DEBUG(1, "%s location:%s buf:%s get id:%d", __func__, location, buf, id); |
| 155 | p[j++] = id; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 156 | } |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 157 | memset(buf, 0, sizeof(buf)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 158 | } |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 159 | //DVR_DEBUG(1, "%s location:%s n=%d j=%d end", __func__, location, n, j); |
| 160 | pclose(fp); |
| 161 | *p_segment_nb = n; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 162 | *pp_segment_ids = p; |
| 163 | } |
Pengfei Liu | faf38e4 | 2020-05-22 00:28:02 +0800 | [diff] [blame] | 164 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 165 | return DVR_SUCCESS; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 166 | } |
| 167 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 168 | int dvr_segment_get_info(const char *location, uint64_t segment_id, DVR_RecordSegmentInfo_t *p_info) |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 169 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 170 | int ret; |
| 171 | Segment_OpenParams_t open_params; |
| 172 | Segment_Handle_t segment_handle; |
| 173 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 174 | DVR_RETURN_IF_FALSE(location); |
| 175 | DVR_RETURN_IF_FALSE(p_info); |
| 176 | DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 177 | |
| 178 | memset(&open_params, 0, sizeof(open_params)); |
| 179 | memcpy(open_params.location, location, strlen(location)); |
| 180 | open_params.segment_id = segment_id; |
| 181 | open_params.mode = SEGMENT_MODE_READ; |
| 182 | ret = segment_open(&open_params, &segment_handle); |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 183 | if (ret == DVR_SUCCESS) { |
| 184 | ret = segment_load_info(segment_handle, p_info); |
| 185 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 186 | //DVR_DEBUG(1, "%s, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d", |
| 187 | // __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets); |
hualing chen | b224235 | 2021-05-18 17:16:16 +0800 | [diff] [blame] | 188 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 189 | ret = segment_close(segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 190 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 191 | |
| 192 | return DVR_SUCCESS; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | int dvr_segment_link(const char *location, uint32_t nb_segments, uint64_t *p_segment_ids) |
| 196 | { |
Zhiqiang Han | e0a1c38 | 2021-06-08 11:28:05 +0800 | [diff] [blame^] | 197 | return dvr_segment_link_op(location, nb_segments, p_segment_ids, LSEG_OP_NEW); |
| 198 | } |
| 199 | |
| 200 | int dvr_segment_link_op(const char *location, uint32_t nb_segments, uint64_t *p_segment_ids, int op) |
| 201 | { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 202 | FILE *fp; |
| 203 | char fpath[DVR_MAX_LOCATION_SIZE]; |
| 204 | uint32_t i; |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 205 | char buf[DVR_MAX_LOCATION_SIZE + 64]; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 206 | |
| 207 | DVR_RETURN_IF_FALSE(location); |
| 208 | DVR_RETURN_IF_FALSE(p_segment_ids); |
| 209 | DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE); |
| 210 | |
Zhiqiang Han | e0a1c38 | 2021-06-08 11:28:05 +0800 | [diff] [blame^] | 211 | DVR_DEBUG(1, "%s op[%d] location:%s, nb_segments:%d", __func__, op, location, nb_segments); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 212 | memset(fpath, 0, sizeof(fpath)); |
| 213 | sprintf(fpath, "%s.list", location); |
Zhiqiang Han | e0a1c38 | 2021-06-08 11:28:05 +0800 | [diff] [blame^] | 214 | fp = fopen(fpath, (op == LSEG_OP_ADD) ? "a+" : "w+"); |
| 215 | if (!fp) { |
| 216 | DVR_DEBUG(1, "failed to open list file, err:%d:%s", errno, strerror(errno)); |
| 217 | return DVR_FAILURE; |
| 218 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 219 | for (i = 0; i< nb_segments; i++) { |
| 220 | memset(buf, 0, sizeof(buf)); |
Zhiqiang Han | e0a1c38 | 2021-06-08 11:28:05 +0800 | [diff] [blame^] | 221 | sprintf(buf, "%lld\n", p_segment_ids[i]); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 222 | fwrite(buf, 1, strlen(buf), fp); |
| 223 | } |
| 224 | |
| 225 | fflush(fp); |
| 226 | fsync(fileno(fp)); |
| 227 | fclose(fp); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 228 | return DVR_SUCCESS; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 229 | } |