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