blob: 4429c208f2e24a9a63f5e35e6a3774b1efd7f40d [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>
5#include <pthread.h>
Pengfei Liuc181a982020-01-07 19:27:13 +08006#include "dvr_segment.h"
Pengfei Liub4734232020-01-17 18:25:10 +08007#include <segment.h>
Pengfei Liuc181a982020-01-07 19:27:13 +08008
Pengfei Liub4734232020-01-17 18:25:10 +08009/**\brief DVR segment file information*/
10typedef struct {
11 char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/
12 uint64_t id; /**< DVR Segment id*/
13} DVR_SegmentFile_t;
14
15void *dvr_segment_thread(void *arg)
16{
Pengfei Liub4734232020-01-17 18:25:10 +080017 int ret;
hualing chene83bf812020-04-23 13:42:37 +080018 DVR_SegmentFile_t *segment_file = (DVR_SegmentFile_t*)arg;
Pengfei Liub4734232020-01-17 18:25:10 +080019
hualing chen31140872020-03-25 12:29:26 +080020 pthread_detach(pthread_self());
hualing chene83bf812020-04-23 13:42:37 +080021 DVR_DEBUG(1, "%s try to delete [%s-%lld]", __func__, segment_file->location, segment_file->id);
22 ret = segment_delete(segment_file->location, segment_file->id);
23 DVR_DEBUG(1, "%s delete segment [%s-%lld] %s", __func__, segment_file->location, segment_file->id,
Pengfei Liub4734232020-01-17 18:25:10 +080024 ret == DVR_SUCCESS ? "success" : "failed");
hualing chene83bf812020-04-23 13:42:37 +080025 if (segment_file != NULL) {
26 //malloc at delete api.free at this
27 free(segment_file);
28 segment_file = NULL;
29 }
Pengfei Liub4734232020-01-17 18:25:10 +080030 return NULL;
31}
Pengfei Liu3b1a8202020-02-12 23:04:21 +080032
Pengfei Liuc181a982020-01-07 19:27:13 +080033int dvr_segment_delete(const char *location, uint64_t segment_id)
34{
hualing chen6d24aa92020-03-23 18:43:47 +080035
Pengfei Liub4734232020-01-17 18:25:10 +080036 pthread_t thread;
hualing chene83bf812020-04-23 13:42:37 +080037 DVR_SegmentFile_t *segment;
38 //this segment will be free at del thread when used end.if thread
39 //creat error.will be free now.
40 segment = (DVR_SegmentFile_t *)malloc(sizeof(DVR_SegmentFile_t));
Pengfei Liub4734232020-01-17 18:25:10 +080041
42 DVR_DEBUG(1, "%s in, %s,id:%lld", __func__, location, segment_id);
hualing chene83bf812020-04-23 13:42:37 +080043 DVR_RETURN_IF_FALSE(segment);
Pengfei Liu3b1a8202020-02-12 23:04:21 +080044 DVR_RETURN_IF_FALSE(location);
45 DVR_RETURN_IF_FALSE(strlen(location) < DVR_MAX_LOCATION_SIZE);
hualing chene83bf812020-04-23 13:42:37 +080046 memset(segment->location, 0, sizeof(segment->location));
47 memcpy(segment->location, location, strlen(location));
48 segment->id = segment_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080049
hualing chene83bf812020-04-23 13:42:37 +080050 int ret = pthread_create(&thread, NULL, dvr_segment_thread, segment);
51 if (ret != 0) {
52 //creat thread error,need free segment
53 if (segment != NULL) {
54 free(segment);
55 segment = NULL;
56 }
57 }
Pengfei Liub4734232020-01-17 18:25:10 +080058 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +080059}
60
hualing chen4b7c15d2020-04-07 16:13:48 +080061int dvr_segment_del_by_location(const char *location)
62{
63 FILE *fp;
64 char fpath[DVR_MAX_LOCATION_SIZE];
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);
70 memset(fpath, 0, sizeof(fpath));
71 sprintf(fpath, "%s.del", location);
72 {
73 /* del file */
74 memset(cmd, 0, sizeof(cmd));
Zhiqiang Hanade34ff2020-04-17 16:50:59 +080075 sprintf(cmd, "rm %s-*", location);
hualing chen4b7c15d2020-04-07 16:13:48 +080076 fp = popen(cmd, "r");
77 DVR_RETURN_IF_FALSE(fp);
78 }
hualing chen7a56cba2020-04-14 14:09:27 +080079 DVR_DEBUG(1, "%s location:%s end", __func__, location);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +080080 pclose(fp);
hualing chen4b7c15d2020-04-07 16:13:48 +080081 return DVR_SUCCESS;
82}
83
Pengfei Liuc181a982020-01-07 19:27:13 +080084int dvr_segment_get_list(const char *location, uint32_t *p_segment_nb, uint64_t **pp_segment_ids)
85{
Pengfei Liu3b1a8202020-02-12 23:04:21 +080086 FILE *fp;
87 char fpath[DVR_MAX_LOCATION_SIZE];
Zhiqiang Hanee5ac412020-05-07 20:54:32 +080088 uint32_t i = 0, j = 0, n = 0;
hualing chenb270cfa2020-08-19 15:04:35 +080089 char buf[DVR_MAX_LOCATION_SIZE + 10];
Pengfei Liu3b1a8202020-02-12 23:04:21 +080090 uint64_t *p = NULL;
hualing chenb270cfa2020-08-19 15:04:35 +080091 char cmd[DVR_MAX_LOCATION_SIZE + 64];
Pengfei Liu3b1a8202020-02-12 23:04:21 +080092
93 DVR_RETURN_IF_FALSE(location);
94 DVR_RETURN_IF_FALSE(p_segment_nb);
95 DVR_RETURN_IF_FALSE(pp_segment_ids);
96
hualing chenb270cfa2020-08-19 15:04:35 +080097 //DVR_DEBUG(1, "%s location:%s", __func__, location);
Pengfei Liu3b1a8202020-02-12 23:04:21 +080098 memset(fpath, 0, sizeof(fpath));
99 sprintf(fpath, "%s.list", location);
100
101 if (access(fpath, 0) != -1) {
102 /*the list file is exist*/
103 fp = fopen(fpath, "r");
104 DVR_RETURN_IF_FALSE(fp);
105 /*get segment numbers*/
hualing chenb270cfa2020-08-19 15:04:35 +0800106 while (fgets(buf, sizeof(buf), fp) != NULL) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800107 i++;
108 }
109 *p_segment_nb = i;
110 rewind(fp);
111 /*malloc*/
112 p = malloc(i * sizeof(uint64_t));
113 i = 0;
114 /*set value*/
115 memset(buf, 0, sizeof(buf));
hualing chenb270cfa2020-08-19 15:04:35 +0800116 while (fgets(buf, sizeof(buf), fp) != NULL) {
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800117 p[i++] = strtoull(buf, NULL, 10);
118 memset(buf, 0, sizeof(buf));
119 }
120 *pp_segment_ids = p;
121 fclose(fp);
122 } 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);
135 return DVR_FAILURE;
136 }
hualing chenb270cfa2020-08-19 15:04:35 +0800137 //DVR_DEBUG(1, "%s location:%s i: %d ls buf:%s", __func__, location, i, buf);
Zhiqiang Hanee5ac412020-05-07 20:54:32 +0800138 n = i;
139 p = malloc(n * sizeof(uint64_t));
Zhiqiang Han8dd08b32020-05-13 16:50:04 +0800140 /*try to get the 1st segment id*/
141 memset(cmd, 0, sizeof(cmd));
hualing chenb270cfa2020-08-19 15:04:35 +0800142 sprintf(cmd, "ls %s-*.ts", location);
Zhiqiang Han8dd08b32020-05-13 16:50:04 +0800143 fp = popen(cmd, "r");
144 DVR_RETURN_IF_FALSE(fp);
145 memset(buf, 0, sizeof(buf));
hualing chenb270cfa2020-08-19 15:04:35 +0800146 j = 0;
147 snprintf(fpath, sizeof(fpath), "%s-%%d.ts", location);
148 while (fgets(buf, sizeof(buf), fp) != NULL) {
149 //DVR_DEBUG(1, "%s buf:%s", __func__, buf);
150 if (sscanf(buf, fpath, &id) != 1) {
151 DVR_DEBUG(1, "%s location:%s buf:%s not get id", __func__, location, buf);
152 id = 0;
153 n = n -1;
154 } else {
155 //DVR_DEBUG(1, "%s location:%s buf:%s get id:%d", __func__, location, buf, id);
156 p[j++] = id;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800157 }
hualing chenb270cfa2020-08-19 15:04:35 +0800158 memset(buf, 0, sizeof(buf));
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800159 }
hualing chenb270cfa2020-08-19 15:04:35 +0800160 //DVR_DEBUG(1, "%s location:%s n=%d j=%d end", __func__, location, n, j);
161 pclose(fp);
162 *p_segment_nb = n;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800163 *pp_segment_ids = p;
164 }
Pengfei Liufaf38e42020-05-22 00:28:02 +0800165
Pengfei Liub4734232020-01-17 18:25:10 +0800166 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800167}
168
Pengfei Liub4734232020-01-17 18:25:10 +0800169int dvr_segment_get_info(const char *location, uint64_t segment_id, DVR_RecordSegmentInfo_t *p_info)
Pengfei Liuc181a982020-01-07 19:27:13 +0800170{
Pengfei Liub4734232020-01-17 18:25:10 +0800171 int ret;
172 Segment_OpenParams_t open_params;
173 Segment_Handle_t segment_handle;
174
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800175 DVR_RETURN_IF_FALSE(location);
176 DVR_RETURN_IF_FALSE(p_info);
177 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liub4734232020-01-17 18:25:10 +0800178
179 memset(&open_params, 0, sizeof(open_params));
180 memcpy(open_params.location, location, strlen(location));
181 open_params.segment_id = segment_id;
182 open_params.mode = SEGMENT_MODE_READ;
183 ret = segment_open(&open_params, &segment_handle);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800184 if (ret == DVR_SUCCESS) {
185 ret = segment_load_info(segment_handle, p_info);
186 }
hualing chena540a7e2020-03-27 16:44:05 +0800187 //DVR_DEBUG(1, "%s, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d",
188 // __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 +0800189 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800190 ret = segment_close(segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800191 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800192
193 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800194}
195
196int dvr_segment_link(const char *location, uint32_t nb_segments, uint64_t *p_segment_ids)
197{
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800198 FILE *fp;
199 char fpath[DVR_MAX_LOCATION_SIZE];
200 uint32_t i;
hualing chenb270cfa2020-08-19 15:04:35 +0800201 char buf[DVR_MAX_LOCATION_SIZE + 64];
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800202
203 DVR_RETURN_IF_FALSE(location);
204 DVR_RETURN_IF_FALSE(p_segment_ids);
205 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
206
207 DVR_DEBUG(1, "%s location:%s, nb_segments:%d", __func__, location, nb_segments);
208 memset(fpath, 0, sizeof(fpath));
209 sprintf(fpath, "%s.list", location);
210 fp = fopen(fpath, "w+");
211 for (i = 0; i< nb_segments; i++) {
212 memset(buf, 0, sizeof(buf));
213 sprintf(buf, "%lld", p_segment_ids[i]);
214 fwrite(buf, 1, strlen(buf), fp);
215 }
216
217 fflush(fp);
218 fsync(fileno(fp));
219 fclose(fp);
Pengfei Liub4734232020-01-17 18:25:10 +0800220 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800221}