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