blob: 80f3284b468e716ea35dc0c93d4b8b3da3e27759 [file] [log] [blame]
Pengfei Liuc181a982020-01-07 19:27:13 +08001#include <stdio.h>
Pengfei Liu3b1a8202020-02-12 23:04:21 +08002#include <unistd.h>
3#include <stdlib.h>
Pengfei Liub4734232020-01-17 18:25:10 +08004#include <string.h>
Zhiqiang Hane0a1c382021-06-08 11:28:05 +08005#include <errno.h>
Pengfei Liub4734232020-01-17 18:25:10 +08006#include <pthread.h>
Pengfei Liuc181a982020-01-07 19:27:13 +08007#include "dvr_segment.h"
Pengfei Liub4734232020-01-17 18:25:10 +08008#include <segment.h>
Pengfei Liuc181a982020-01-07 19:27:13 +08009
Pengfei Liub4734232020-01-17 18:25:10 +080010/**\brief DVR segment file information*/
11typedef struct {
12 char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/
13 uint64_t id; /**< DVR Segment id*/
14} DVR_SegmentFile_t;
15
16void *dvr_segment_thread(void *arg)
17{
Pengfei Liub4734232020-01-17 18:25:10 +080018 int ret;
hualing chene83bf812020-04-23 13:42:37 +080019 DVR_SegmentFile_t *segment_file = (DVR_SegmentFile_t*)arg;
Pengfei Liub4734232020-01-17 18:25:10 +080020
hualing chen31140872020-03-25 12:29:26 +080021 pthread_detach(pthread_self());
Wentao MA96f68962022-06-15 19:45:35 +080022 DVR_INFO("%s try to delete [%s-%lld]", __func__, segment_file->location, segment_file->id);
hualing chene83bf812020-04-23 13:42:37 +080023 ret = segment_delete(segment_file->location, segment_file->id);
Wentao MA96f68962022-06-15 19:45:35 +080024 DVR_INFO("%s delete segment [%s-%lld] %s", __func__, segment_file->location, segment_file->id,
Pengfei Liub4734232020-01-17 18:25:10 +080025 ret == DVR_SUCCESS ? "success" : "failed");
hualing chene83bf812020-04-23 13:42:37 +080026 if (segment_file != NULL) {
27 //malloc at delete api.free at this
28 free(segment_file);
29 segment_file = NULL;
30 }
Pengfei Liub4734232020-01-17 18:25:10 +080031 return NULL;
32}
Pengfei Liu3b1a8202020-02-12 23:04:21 +080033
Pengfei Liuc181a982020-01-07 19:27:13 +080034int dvr_segment_delete(const char *location, uint64_t segment_id)
35{
hualing chen6d24aa92020-03-23 18:43:47 +080036
Pengfei Liub4734232020-01-17 18:25:10 +080037 pthread_t thread;
hualing chene83bf812020-04-23 13:42:37 +080038 DVR_SegmentFile_t *segment;
Pengfei Liub4734232020-01-17 18:25:10 +080039
Pengfei Liu3b1a8202020-02-12 23:04:21 +080040 DVR_RETURN_IF_FALSE(location);
41 DVR_RETURN_IF_FALSE(strlen(location) < DVR_MAX_LOCATION_SIZE);
Wentao MA4b8d4752022-09-14 19:03:45 +080042 DVR_INFO("In function %s, segment %s's id is %lld", __func__, location, segment_id);
43
44 // Memory allocated here will be freed in segment deletion thread under normal conditions.
45 // In case of thread creation failure, it will be freed right away.
46 segment = (DVR_SegmentFile_t *)malloc(sizeof(DVR_SegmentFile_t));
47 DVR_RETURN_IF_FALSE(segment != NULL);
48
hualing chene83bf812020-04-23 13:42:37 +080049 memset(segment->location, 0, sizeof(segment->location));
50 memcpy(segment->location, location, strlen(location));
51 segment->id = segment_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080052
hualing chene83bf812020-04-23 13:42:37 +080053 int ret = pthread_create(&thread, NULL, dvr_segment_thread, segment);
54 if (ret != 0) {
hualing chene83bf812020-04-23 13:42:37 +080055 if (segment != NULL) {
56 free(segment);
57 segment = NULL;
58 }
59 }
Pengfei Liub4734232020-01-17 18:25:10 +080060 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +080061}
62
hualing chen4b7c15d2020-04-07 16:13:48 +080063int dvr_segment_del_by_location(const char *location)
64{
65 FILE *fp;
hualing chenb270cfa2020-08-19 15:04:35 +080066 char cmd[DVR_MAX_LOCATION_SIZE + 64];
hualing chen4b7c15d2020-04-07 16:13:48 +080067
68 DVR_RETURN_IF_FALSE(location);
69
Wentao MA96f68962022-06-15 19:45:35 +080070 DVR_INFO("%s location:%s", __func__, location);
hualing chen4b7c15d2020-04-07 16:13:48 +080071 {
72 /* del file */
73 memset(cmd, 0, sizeof(cmd));
hualing chen926a8ec2021-12-20 20:38:24 +080074 sprintf(cmd, "rm %s-* %s.list %s.stats %s.odb %s.dat", location, location, location, location, location);
hualing chen4b7c15d2020-04-07 16:13:48 +080075 fp = popen(cmd, "r");
76 DVR_RETURN_IF_FALSE(fp);
77 }
Zhiqiang Han5c805cf2020-05-09 16:51:08 +080078 pclose(fp);
Wentao MA96f68962022-06-15 19:45:35 +080079 DVR_INFO("%s location:%s end", __func__, location);
hualing chen4b7c15d2020-04-07 16:13:48 +080080 return DVR_SUCCESS;
81}
82
Pengfei Liuc181a982020-01-07 19:27:13 +080083int dvr_segment_get_list(const char *location, uint32_t *p_segment_nb, uint64_t **pp_segment_ids)
84{
Pengfei Liu3b1a8202020-02-12 23:04:21 +080085 FILE *fp;
86 char fpath[DVR_MAX_LOCATION_SIZE];
Zhiqiang Hanee5ac412020-05-07 20:54:32 +080087 uint32_t i = 0, j = 0, n = 0;
hualing chenb270cfa2020-08-19 15:04:35 +080088 char buf[DVR_MAX_LOCATION_SIZE + 10];
Pengfei Liu3b1a8202020-02-12 23:04:21 +080089 uint64_t *p = NULL;
hualing chenb270cfa2020-08-19 15:04:35 +080090 char cmd[DVR_MAX_LOCATION_SIZE + 64];
Pengfei Liu3b1a8202020-02-12 23:04:21 +080091
92 DVR_RETURN_IF_FALSE(location);
93 DVR_RETURN_IF_FALSE(p_segment_nb);
94 DVR_RETURN_IF_FALSE(pp_segment_ids);
95
Wentao MA96f68962022-06-15 19:45:35 +080096 //DVR_INFO("%s location:%s", __func__, location);
Pengfei Liu3b1a8202020-02-12 23:04:21 +080097 memset(fpath, 0, sizeof(fpath));
98 sprintf(fpath, "%s.list", location);
99
100 if (access(fpath, 0) != -1) {
101 /*the list file is exist*/
102 fp = fopen(fpath, "r");
103 DVR_RETURN_IF_FALSE(fp);
104 /*get segment numbers*/
hualing chenb270cfa2020-08-19 15:04:35 +0800105 while (fgets(buf, sizeof(buf), fp) != NULL) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800106 i++;
107 }
108 *p_segment_nb = i;
109 rewind(fp);
110 /*malloc*/
111 p = malloc(i * sizeof(uint64_t));
112 i = 0;
113 /*set value*/
114 memset(buf, 0, sizeof(buf));
hualing chenb270cfa2020-08-19 15:04:35 +0800115 while (fgets(buf, sizeof(buf), fp) != NULL) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800116 p[i++] = strtoull(buf, NULL, 10);
117 memset(buf, 0, sizeof(buf));
118 }
119 *pp_segment_ids = p;
120 fclose(fp);
Wentao MA96f68962022-06-15 19:45:35 +0800121 DVR_INFO("%s location:%s segments:%d", __func__, location, i);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800122 } else {
hualing chenb270cfa2020-08-19 15:04:35 +0800123 uint32_t id = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800124 /*the list file does not exist*/
125 memset(cmd, 0, sizeof(cmd));
Zhiqiang Hanade34ff2020-04-17 16:50:59 +0800126 sprintf(cmd, "ls -l %s-*.ts | wc -l", location);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800127 fp = popen(cmd, "r");
128 DVR_RETURN_IF_FALSE(fp);
129 memset(buf, 0, sizeof(buf));
130 if (fgets(buf, sizeof(buf), fp) != NULL) {
131 i = strtoull(buf, NULL, 10);
132 pclose(fp);
133 } else {
134 pclose(fp);
Wentao MA96f68962022-06-15 19:45:35 +0800135 DVR_INFO("%s location:%s get null", __func__, location);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800136 return DVR_FAILURE;
137 }
Zhiqiang Hanee5ac412020-05-07 20:54:32 +0800138 n = i;
Wentao MA4b8d4752022-09-14 19:03:45 +0800139 //DVR_INFO("%s location:%s i: %d ls buf:%s", __func__, location, i, buf);
140
Zhiqiang Han8dd08b32020-05-13 16:50:04 +0800141 /*try to get the 1st segment id*/
142 memset(cmd, 0, sizeof(cmd));
hualing chenb270cfa2020-08-19 15:04:35 +0800143 sprintf(cmd, "ls %s-*.ts", location);
Zhiqiang Han8dd08b32020-05-13 16:50:04 +0800144 fp = popen(cmd, "r");
145 DVR_RETURN_IF_FALSE(fp);
146 memset(buf, 0, sizeof(buf));
hualing chenb270cfa2020-08-19 15:04:35 +0800147 j = 0;
148 snprintf(fpath, sizeof(fpath), "%s-%%d.ts", location);
Wentao MA4b8d4752022-09-14 19:03:45 +0800149
150 p = malloc(n * sizeof(uint64_t));
151 DVR_RETURN_IF_FALSE(p != NULL);
152
hualing chenb270cfa2020-08-19 15:04:35 +0800153 while (fgets(buf, sizeof(buf), fp) != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800154 //DVR_INFO("%s buf:%s", __func__, buf);
hualing chenb270cfa2020-08-19 15:04:35 +0800155 if (sscanf(buf, fpath, &id) != 1) {
Wentao MA96f68962022-06-15 19:45:35 +0800156 DVR_INFO("%s location:%s buf:%s not get id", __func__, location, buf);
hualing chenb270cfa2020-08-19 15:04:35 +0800157 id = 0;
158 n = n -1;
159 } else {
Wentao MA96f68962022-06-15 19:45:35 +0800160 //DVR_INFO("%s location:%s buf:%s get id:%d", __func__, location, buf, id);
hualing chenb270cfa2020-08-19 15:04:35 +0800161 p[j++] = id;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800162 }
hualing chenb270cfa2020-08-19 15:04:35 +0800163 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800164 }
Wentao MA96f68962022-06-15 19:45:35 +0800165 DVR_DEBUG("%s location:%s n=%d j=%d end", __func__, location, n, j);
hualing chenb270cfa2020-08-19 15:04:35 +0800166 pclose(fp);
167 *p_segment_nb = n;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800168 *pp_segment_ids = p;
169 }
Pengfei Liufaf38e42020-05-22 00:28:02 +0800170
Pengfei Liub4734232020-01-17 18:25:10 +0800171 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800172}
173
Pengfei Liub4734232020-01-17 18:25:10 +0800174int dvr_segment_get_info(const char *location, uint64_t segment_id, DVR_RecordSegmentInfo_t *p_info)
Pengfei Liuc181a982020-01-07 19:27:13 +0800175{
Pengfei Liub4734232020-01-17 18:25:10 +0800176 int ret;
177 Segment_OpenParams_t open_params;
178 Segment_Handle_t segment_handle;
179
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800180 DVR_RETURN_IF_FALSE(location);
181 DVR_RETURN_IF_FALSE(p_info);
182 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liub4734232020-01-17 18:25:10 +0800183
184 memset(&open_params, 0, sizeof(open_params));
185 memcpy(open_params.location, location, strlen(location));
186 open_params.segment_id = segment_id;
187 open_params.mode = SEGMENT_MODE_READ;
188 ret = segment_open(&open_params, &segment_handle);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800189 if (ret == DVR_SUCCESS) {
190 ret = segment_load_info(segment_handle, p_info);
Wentao MA07d3d742022-09-06 09:58:05 +0800191 if (ret != DVR_SUCCESS) {
192 DVR_ERROR("segment_load_info failed with return value %d",ret);
193 }
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800194 }
Wentao MA96f68962022-06-15 19:45:35 +0800195 DVR_DEBUG("%s, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d",
hualing chen926a8ec2021-12-20 20:38:24 +0800196 __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets);
hualing chenb2242352021-05-18 17:16:16 +0800197 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800198 ret = segment_close(segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800199 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800200
201 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800202}
203
hualing chenb9a02922021-12-14 11:29:47 +0800204int dvr_segment_get_allInfo(const char *location, struct list_head *list)
205{
206 int ret;
207 Segment_OpenParams_t open_params;
208 Segment_Handle_t segment_handle;
209
210 DVR_RETURN_IF_FALSE(location);
211 DVR_RETURN_IF_FALSE(list);
212 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
213
214 memset(&open_params, 0, sizeof(open_params));
215 memcpy(open_params.location, location, strlen(location));
216 open_params.segment_id = 0;
217 open_params.mode = SEGMENT_MODE_READ;
218 ret = segment_open(&open_params, &segment_handle);
219 if (ret == DVR_SUCCESS) {
220 ret = segment_load_allInfo(segment_handle, list);
hualing chen926a8ec2021-12-20 20:38:24 +0800221 if (ret == DVR_FAILURE) {
222 segment_close(segment_handle);
223 return DVR_FAILURE;
224 }
hualing chenb9a02922021-12-14 11:29:47 +0800225 }
226 ret = segment_close(segment_handle);
227 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
228
229 return DVR_SUCCESS;
230}
231
232
Pengfei Liuc181a982020-01-07 19:27:13 +0800233int dvr_segment_link(const char *location, uint32_t nb_segments, uint64_t *p_segment_ids)
234{
Wentao MAe8ba5172022-08-09 11:18:17 +0800235 return dvr_segment_link_op(location, nb_segments, p_segment_ids, SEGMENT_OP_NEW);
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800236}
237
238int dvr_segment_link_op(const char *location, uint32_t nb_segments, uint64_t *p_segment_ids, int op)
239{
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800240 FILE *fp;
241 char fpath[DVR_MAX_LOCATION_SIZE];
242 uint32_t i;
hualing chenb270cfa2020-08-19 15:04:35 +0800243 char buf[DVR_MAX_LOCATION_SIZE + 64];
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800244
245 DVR_RETURN_IF_FALSE(location);
246 DVR_RETURN_IF_FALSE(p_segment_ids);
247 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
248
Wentao MA96f68962022-06-15 19:45:35 +0800249 DVR_INFO("%s op[%d] location:%s, nb_segments:%d", __func__, op, location, nb_segments);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800250 memset(fpath, 0, sizeof(fpath));
251 sprintf(fpath, "%s.list", location);
Wentao MAe8ba5172022-08-09 11:18:17 +0800252 fp = fopen(fpath, (op == SEGMENT_OP_ADD) ? "a+" : "w+");
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800253 if (!fp) {
Wentao MA96f68962022-06-15 19:45:35 +0800254 DVR_INFO("failed to open list file, err:%d:%s", errno, strerror(errno));
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800255 return DVR_FAILURE;
256 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800257 for (i = 0; i< nb_segments; i++) {
258 memset(buf, 0, sizeof(buf));
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800259 sprintf(buf, "%lld\n", p_segment_ids[i]);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800260 fwrite(buf, 1, strlen(buf), fp);
261 }
262
263 fflush(fp);
264 fsync(fileno(fp));
265 fclose(fp);
Pengfei Liub4734232020-01-17 18:25:10 +0800266 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800267}