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