blob: 3a0ab398828c67b078d471d55bd71f3a38b139e5 [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());
hualing chene83bf812020-04-23 13:42:37 +080022 DVR_DEBUG(1, "%s try to delete [%s-%lld]", __func__, segment_file->location, segment_file->id);
23 ret = segment_delete(segment_file->location, segment_file->id);
24 DVR_DEBUG(1, "%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;
39 //this segment will be free at del thread when used end.if thread
40 //creat error.will be free now.
41 segment = (DVR_SegmentFile_t *)malloc(sizeof(DVR_SegmentFile_t));
Pengfei Liub4734232020-01-17 18:25:10 +080042
43 DVR_DEBUG(1, "%s in, %s,id:%lld", __func__, location, segment_id);
hualing chene83bf812020-04-23 13:42:37 +080044 DVR_RETURN_IF_FALSE(segment);
Pengfei Liu3b1a8202020-02-12 23:04:21 +080045 DVR_RETURN_IF_FALSE(location);
46 DVR_RETURN_IF_FALSE(strlen(location) < DVR_MAX_LOCATION_SIZE);
hualing chene83bf812020-04-23 13:42:37 +080047 memset(segment->location, 0, sizeof(segment->location));
48 memcpy(segment->location, location, strlen(location));
49 segment->id = segment_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080050
hualing chene83bf812020-04-23 13:42:37 +080051 int ret = pthread_create(&thread, NULL, dvr_segment_thread, segment);
52 if (ret != 0) {
53 //creat thread error,need free segment
54 if (segment != NULL) {
55 free(segment);
56 segment = NULL;
57 }
58 }
Pengfei Liub4734232020-01-17 18:25:10 +080059 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +080060}
61
hualing chen4b7c15d2020-04-07 16:13:48 +080062int dvr_segment_del_by_location(const char *location)
63{
64 FILE *fp;
hualing chenb270cfa2020-08-19 15:04:35 +080065 char cmd[DVR_MAX_LOCATION_SIZE + 64];
hualing chen4b7c15d2020-04-07 16:13:48 +080066
67 DVR_RETURN_IF_FALSE(location);
68
69 DVR_DEBUG(1, "%s location:%s", __func__, location);
hualing chen4b7c15d2020-04-07 16:13:48 +080070 {
71 /* del file */
72 memset(cmd, 0, sizeof(cmd));
hualing chen926a8ec2021-12-20 20:38:24 +080073 sprintf(cmd, "rm %s-* %s.list %s.stats %s.odb %s.dat", location, location, location, location, location);
hualing chen4b7c15d2020-04-07 16:13:48 +080074 fp = popen(cmd, "r");
75 DVR_RETURN_IF_FALSE(fp);
76 }
Zhiqiang Han5c805cf2020-05-09 16:51:08 +080077 pclose(fp);
hualing chen40dd5462021-11-26 19:56:20 +080078 DVR_DEBUG(1, "%s location:%s end", __func__, location);
hualing chen4b7c15d2020-04-07 16:13:48 +080079 return DVR_SUCCESS;
80}
81
Pengfei Liuc181a982020-01-07 19:27:13 +080082int dvr_segment_get_list(const char *location, uint32_t *p_segment_nb, uint64_t **pp_segment_ids)
83{
Pengfei Liu3b1a8202020-02-12 23:04:21 +080084 FILE *fp;
85 char fpath[DVR_MAX_LOCATION_SIZE];
Zhiqiang Hanee5ac412020-05-07 20:54:32 +080086 uint32_t i = 0, j = 0, n = 0;
hualing chenb270cfa2020-08-19 15:04:35 +080087 char buf[DVR_MAX_LOCATION_SIZE + 10];
Pengfei Liu3b1a8202020-02-12 23:04:21 +080088 uint64_t *p = NULL;
hualing chenb270cfa2020-08-19 15:04:35 +080089 char cmd[DVR_MAX_LOCATION_SIZE + 64];
Pengfei Liu3b1a8202020-02-12 23:04:21 +080090
91 DVR_RETURN_IF_FALSE(location);
92 DVR_RETURN_IF_FALSE(p_segment_nb);
93 DVR_RETURN_IF_FALSE(pp_segment_ids);
94
hualing chenb270cfa2020-08-19 15:04:35 +080095 //DVR_DEBUG(1, "%s location:%s", __func__, location);
Pengfei Liu3b1a8202020-02-12 23:04:21 +080096 memset(fpath, 0, sizeof(fpath));
97 sprintf(fpath, "%s.list", location);
98
99 if (access(fpath, 0) != -1) {
100 /*the list file is exist*/
101 fp = fopen(fpath, "r");
102 DVR_RETURN_IF_FALSE(fp);
103 /*get segment numbers*/
hualing chenb270cfa2020-08-19 15:04:35 +0800104 while (fgets(buf, sizeof(buf), fp) != NULL) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800105 i++;
106 }
107 *p_segment_nb = i;
108 rewind(fp);
109 /*malloc*/
110 p = malloc(i * sizeof(uint64_t));
111 i = 0;
112 /*set value*/
113 memset(buf, 0, sizeof(buf));
hualing chenb270cfa2020-08-19 15:04:35 +0800114 while (fgets(buf, sizeof(buf), fp) != NULL) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800115 p[i++] = strtoull(buf, NULL, 10);
116 memset(buf, 0, sizeof(buf));
117 }
118 *pp_segment_ids = p;
119 fclose(fp);
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800120 DVR_DEBUG(1, "%s location:%s segments:%d", __func__, location, i);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800121 } else {
hualing chenb270cfa2020-08-19 15:04:35 +0800122 uint32_t id = 0;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800123 /*the list file does not exist*/
124 memset(cmd, 0, sizeof(cmd));
Zhiqiang Hanade34ff2020-04-17 16:50:59 +0800125 sprintf(cmd, "ls -l %s-*.ts | wc -l", location);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800126 fp = popen(cmd, "r");
127 DVR_RETURN_IF_FALSE(fp);
128 memset(buf, 0, sizeof(buf));
129 if (fgets(buf, sizeof(buf), fp) != NULL) {
130 i = strtoull(buf, NULL, 10);
131 pclose(fp);
132 } else {
133 pclose(fp);
134 return DVR_FAILURE;
135 }
hualing chenb270cfa2020-08-19 15:04:35 +0800136 //DVR_DEBUG(1, "%s location:%s i: %d ls buf:%s", __func__, location, i, buf);
Zhiqiang Hanee5ac412020-05-07 20:54:32 +0800137 n = i;
138 p = malloc(n * sizeof(uint64_t));
Zhiqiang Han8dd08b32020-05-13 16:50:04 +0800139 /*try to get the 1st segment id*/
140 memset(cmd, 0, sizeof(cmd));
hualing chenb270cfa2020-08-19 15:04:35 +0800141 sprintf(cmd, "ls %s-*.ts", location);
Zhiqiang Han8dd08b32020-05-13 16:50:04 +0800142 fp = popen(cmd, "r");
143 DVR_RETURN_IF_FALSE(fp);
144 memset(buf, 0, sizeof(buf));
hualing chenb270cfa2020-08-19 15:04:35 +0800145 j = 0;
146 snprintf(fpath, sizeof(fpath), "%s-%%d.ts", location);
147 while (fgets(buf, sizeof(buf), fp) != NULL) {
148 //DVR_DEBUG(1, "%s buf:%s", __func__, buf);
149 if (sscanf(buf, fpath, &id) != 1) {
150 DVR_DEBUG(1, "%s location:%s buf:%s not get id", __func__, location, buf);
151 id = 0;
152 n = n -1;
153 } else {
154 //DVR_DEBUG(1, "%s location:%s buf:%s get id:%d", __func__, location, buf, id);
155 p[j++] = id;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800156 }
hualing chenb270cfa2020-08-19 15:04:35 +0800157 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800158 }
hualing chen926a8ec2021-12-20 20:38:24 +0800159 DVR_DEBUG(1, "%s location:%s n=%d j=%d end", __func__, location, n, j);
hualing chenb270cfa2020-08-19 15:04:35 +0800160 pclose(fp);
161 *p_segment_nb = n;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800162 *pp_segment_ids = p;
163 }
Pengfei Liufaf38e42020-05-22 00:28:02 +0800164
Pengfei Liub4734232020-01-17 18:25:10 +0800165 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800166}
167
Pengfei Liub4734232020-01-17 18:25:10 +0800168int dvr_segment_get_info(const char *location, uint64_t segment_id, DVR_RecordSegmentInfo_t *p_info)
Pengfei Liuc181a982020-01-07 19:27:13 +0800169{
Pengfei Liub4734232020-01-17 18:25:10 +0800170 int ret;
171 Segment_OpenParams_t open_params;
172 Segment_Handle_t segment_handle;
173
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800174 DVR_RETURN_IF_FALSE(location);
175 DVR_RETURN_IF_FALSE(p_info);
176 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liub4734232020-01-17 18:25:10 +0800177
178 memset(&open_params, 0, sizeof(open_params));
179 memcpy(open_params.location, location, strlen(location));
180 open_params.segment_id = segment_id;
181 open_params.mode = SEGMENT_MODE_READ;
182 ret = segment_open(&open_params, &segment_handle);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800183 if (ret == DVR_SUCCESS) {
184 ret = segment_load_info(segment_handle, p_info);
185 }
hualing chen926a8ec2021-12-20 20:38:24 +0800186 DVR_DEBUG(1, "%s, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d",
187 __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 +0800188 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800189 ret = segment_close(segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800190 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800191
192 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800193}
194
hualing chenb9a02922021-12-14 11:29:47 +0800195int dvr_segment_get_allInfo(const char *location, struct list_head *list)
196{
197 int ret;
198 Segment_OpenParams_t open_params;
199 Segment_Handle_t segment_handle;
200
201 DVR_RETURN_IF_FALSE(location);
202 DVR_RETURN_IF_FALSE(list);
203 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
204
205 memset(&open_params, 0, sizeof(open_params));
206 memcpy(open_params.location, location, strlen(location));
207 open_params.segment_id = 0;
208 open_params.mode = SEGMENT_MODE_READ;
209 ret = segment_open(&open_params, &segment_handle);
210 if (ret == DVR_SUCCESS) {
211 ret = segment_load_allInfo(segment_handle, list);
hualing chen926a8ec2021-12-20 20:38:24 +0800212 if (ret == DVR_FAILURE) {
213 segment_close(segment_handle);
214 return DVR_FAILURE;
215 }
hualing chenb9a02922021-12-14 11:29:47 +0800216 }
217 ret = segment_close(segment_handle);
218 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
219
220 return DVR_SUCCESS;
221}
222
223
Pengfei Liuc181a982020-01-07 19:27:13 +0800224int dvr_segment_link(const char *location, uint32_t nb_segments, uint64_t *p_segment_ids)
225{
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800226 return dvr_segment_link_op(location, nb_segments, p_segment_ids, LSEG_OP_NEW);
227}
228
229int dvr_segment_link_op(const char *location, uint32_t nb_segments, uint64_t *p_segment_ids, int op)
230{
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800231 FILE *fp;
232 char fpath[DVR_MAX_LOCATION_SIZE];
233 uint32_t i;
hualing chenb270cfa2020-08-19 15:04:35 +0800234 char buf[DVR_MAX_LOCATION_SIZE + 64];
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800235
236 DVR_RETURN_IF_FALSE(location);
237 DVR_RETURN_IF_FALSE(p_segment_ids);
238 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
239
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800240 DVR_DEBUG(1, "%s op[%d] location:%s, nb_segments:%d", __func__, op, location, nb_segments);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800241 memset(fpath, 0, sizeof(fpath));
242 sprintf(fpath, "%s.list", location);
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800243 fp = fopen(fpath, (op == LSEG_OP_ADD) ? "a+" : "w+");
244 if (!fp) {
245 DVR_DEBUG(1, "failed to open list file, err:%d:%s", errno, strerror(errno));
246 return DVR_FAILURE;
247 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800248 for (i = 0; i< nb_segments; i++) {
249 memset(buf, 0, sizeof(buf));
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800250 sprintf(buf, "%lld\n", p_segment_ids[i]);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800251 fwrite(buf, 1, strlen(buf), fp);
252 }
253
254 fflush(fp);
255 fsync(fileno(fp));
256 fclose(fp);
Pengfei Liub4734232020-01-17 18:25:10 +0800257 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800258}