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 | |
| 50 | int dvr_segment_get_list(const char *location, uint32_t *p_segment_nb, uint64_t **pp_segment_ids) |
| 51 | { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 52 | FILE *fp; |
| 53 | char fpath[DVR_MAX_LOCATION_SIZE]; |
| 54 | uint32_t i = 0, j = 0; |
| 55 | char buf[64]; |
| 56 | uint64_t *p = NULL; |
| 57 | char cmd[256]; |
| 58 | |
| 59 | DVR_RETURN_IF_FALSE(location); |
| 60 | DVR_RETURN_IF_FALSE(p_segment_nb); |
| 61 | DVR_RETURN_IF_FALSE(pp_segment_ids); |
| 62 | |
| 63 | DVR_DEBUG(1, "%s location:%s", __func__, location); |
| 64 | memset(fpath, 0, sizeof(fpath)); |
| 65 | sprintf(fpath, "%s.list", location); |
| 66 | |
| 67 | if (access(fpath, 0) != -1) { |
| 68 | /*the list file is exist*/ |
| 69 | fp = fopen(fpath, "r"); |
| 70 | DVR_RETURN_IF_FALSE(fp); |
| 71 | /*get segment numbers*/ |
| 72 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 73 | i++; |
| 74 | } |
| 75 | *p_segment_nb = i; |
| 76 | rewind(fp); |
| 77 | /*malloc*/ |
| 78 | p = malloc(i * sizeof(uint64_t)); |
| 79 | i = 0; |
| 80 | /*set value*/ |
| 81 | memset(buf, 0, sizeof(buf)); |
| 82 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 83 | p[i++] = strtoull(buf, NULL, 10); |
| 84 | memset(buf, 0, sizeof(buf)); |
| 85 | } |
| 86 | *pp_segment_ids = p; |
| 87 | fclose(fp); |
| 88 | } else { |
| 89 | /*the list file does not exist*/ |
| 90 | memset(cmd, 0, sizeof(cmd)); |
| 91 | sprintf(cmd, "ls -l %s*.ts | wc -l", location); |
| 92 | fp = popen(cmd, "r"); |
| 93 | DVR_RETURN_IF_FALSE(fp); |
| 94 | memset(buf, 0, sizeof(buf)); |
| 95 | if (fgets(buf, sizeof(buf), fp) != NULL) { |
| 96 | i = strtoull(buf, NULL, 10); |
| 97 | pclose(fp); |
| 98 | } else { |
| 99 | pclose(fp); |
| 100 | return DVR_FAILURE; |
| 101 | } |
| 102 | |
| 103 | *p_segment_nb = i; |
| 104 | p = malloc(i * sizeof(uint64_t)); |
| 105 | for (i = 0;;i++) { |
| 106 | memset(fpath, 0, sizeof(fpath)); |
| 107 | sprintf(fpath, "%s-%04d.ts", location, i); |
| 108 | if (access(fpath, 0) != -1) { |
| 109 | p[j++] = i; |
| 110 | } |
| 111 | if (j >= *p_segment_nb) { |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | *pp_segment_ids = p; |
| 116 | } |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 117 | return DVR_SUCCESS; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 118 | } |
| 119 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 120 | 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] | 121 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 122 | int ret; |
| 123 | Segment_OpenParams_t open_params; |
| 124 | Segment_Handle_t segment_handle; |
| 125 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 126 | DVR_RETURN_IF_FALSE(location); |
| 127 | DVR_RETURN_IF_FALSE(p_info); |
| 128 | DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 129 | |
| 130 | memset(&open_params, 0, sizeof(open_params)); |
| 131 | memcpy(open_params.location, location, strlen(location)); |
| 132 | open_params.segment_id = segment_id; |
| 133 | open_params.mode = SEGMENT_MODE_READ; |
| 134 | ret = segment_open(&open_params, &segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 135 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 136 | |
| 137 | ret = segment_load_info(segment_handle, p_info); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 138 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 139 | //DVR_DEBUG(1, "%s, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d", |
| 140 | // __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] | 141 | |
| 142 | ret = segment_close(segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 143 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 144 | |
| 145 | return DVR_SUCCESS; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int dvr_segment_link(const char *location, uint32_t nb_segments, uint64_t *p_segment_ids) |
| 149 | { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 150 | FILE *fp; |
| 151 | char fpath[DVR_MAX_LOCATION_SIZE]; |
| 152 | uint32_t i; |
| 153 | char buf[64]; |
| 154 | |
| 155 | DVR_RETURN_IF_FALSE(location); |
| 156 | DVR_RETURN_IF_FALSE(p_segment_ids); |
| 157 | DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE); |
| 158 | |
| 159 | DVR_DEBUG(1, "%s location:%s, nb_segments:%d", __func__, location, nb_segments); |
| 160 | memset(fpath, 0, sizeof(fpath)); |
| 161 | sprintf(fpath, "%s.list", location); |
| 162 | fp = fopen(fpath, "w+"); |
| 163 | for (i = 0; i< nb_segments; i++) { |
| 164 | memset(buf, 0, sizeof(buf)); |
| 165 | sprintf(buf, "%lld", p_segment_ids[i]); |
| 166 | fwrite(buf, 1, strlen(buf), fp); |
| 167 | } |
| 168 | |
| 169 | fflush(fp); |
| 170 | fsync(fileno(fp)); |
| 171 | fclose(fp); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 172 | return DVR_SUCCESS; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 173 | } |