blob: 6d0f1f13817ab8e11827d855e0c6c90061d6c4bb [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{
17 DVR_SegmentFile_t file;
18 int ret;
19
hualing chen31140872020-03-25 12:29:26 +080020 pthread_detach(pthread_self());
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080021
Pengfei Liub4734232020-01-17 18:25:10 +080022 memcpy(&file, arg, sizeof(DVR_SegmentFile_t));
Pengfei Liu3b1a8202020-02-12 23:04:21 +080023 DVR_DEBUG(1, "%s try to delete [%s-%lld]", __func__, file.location, file.id);
Pengfei Liub4734232020-01-17 18:25:10 +080024 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 Liu3b1a8202020-02-12 23:04:21 +080030
Pengfei Liuc181a982020-01-07 19:27:13 +080031int dvr_segment_delete(const char *location, uint64_t segment_id)
32{
hualing chen6d24aa92020-03-23 18:43:47 +080033
Pengfei Liub4734232020-01-17 18:25:10 +080034 pthread_t thread;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080035 DVR_SegmentFile_t segment;
Pengfei Liub4734232020-01-17 18:25:10 +080036
37 DVR_DEBUG(1, "%s in, %s,id:%lld", __func__, location, segment_id);
Pengfei Liu3b1a8202020-02-12 23:04:21 +080038 DVR_RETURN_IF_FALSE(location);
39 DVR_RETURN_IF_FALSE(strlen(location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liub4734232020-01-17 18:25:10 +080040 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 Han2d8cd822020-03-16 13:58:10 +080044 /*make sure the thread running and args taken*/
Pengfei Liu3b1a8202020-02-12 23:04:21 +080045 usleep(10*1000);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080046
Pengfei Liub4734232020-01-17 18:25:10 +080047 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +080048}
49
hualing chen4b7c15d2020-04-07 16:13:48 +080050int dvr_segment_del_by_location(const char *location)
51{
52 FILE *fp;
53 char fpath[DVR_MAX_LOCATION_SIZE];
54 char cmd[256];
55
56 DVR_RETURN_IF_FALSE(location);
57
58 DVR_DEBUG(1, "%s location:%s", __func__, location);
59 memset(fpath, 0, sizeof(fpath));
60 sprintf(fpath, "%s.del", location);
61 {
62 /* del file */
63 memset(cmd, 0, sizeof(cmd));
64 sprintf(cmd, "rm %s-*", location);
65 fp = popen(cmd, "r");
66 DVR_RETURN_IF_FALSE(fp);
67 }
68 return DVR_SUCCESS;
69}
70
71
Pengfei Liuc181a982020-01-07 19:27:13 +080072int dvr_segment_get_list(const char *location, uint32_t *p_segment_nb, uint64_t **pp_segment_ids)
73{
Pengfei Liu3b1a8202020-02-12 23:04:21 +080074 FILE *fp;
75 char fpath[DVR_MAX_LOCATION_SIZE];
76 uint32_t i = 0, j = 0;
77 char buf[64];
78 uint64_t *p = NULL;
79 char cmd[256];
80
81 DVR_RETURN_IF_FALSE(location);
82 DVR_RETURN_IF_FALSE(p_segment_nb);
83 DVR_RETURN_IF_FALSE(pp_segment_ids);
84
85 DVR_DEBUG(1, "%s location:%s", __func__, location);
86 memset(fpath, 0, sizeof(fpath));
87 sprintf(fpath, "%s.list", location);
88
89 if (access(fpath, 0) != -1) {
90 /*the list file is exist*/
91 fp = fopen(fpath, "r");
92 DVR_RETURN_IF_FALSE(fp);
93 /*get segment numbers*/
94 while (fgets(buf, sizeof(buf), fp) != NULL) {
95 i++;
96 }
97 *p_segment_nb = i;
98 rewind(fp);
99 /*malloc*/
100 p = malloc(i * sizeof(uint64_t));
101 i = 0;
102 /*set value*/
103 memset(buf, 0, sizeof(buf));
104 while (fgets(buf, sizeof(buf), fp) != NULL) {
105 p[i++] = strtoull(buf, NULL, 10);
106 memset(buf, 0, sizeof(buf));
107 }
108 *pp_segment_ids = p;
109 fclose(fp);
110 } else {
111 /*the list file does not exist*/
112 memset(cmd, 0, sizeof(cmd));
113 sprintf(cmd, "ls -l %s*.ts | wc -l", location);
114 fp = popen(cmd, "r");
115 DVR_RETURN_IF_FALSE(fp);
116 memset(buf, 0, sizeof(buf));
117 if (fgets(buf, sizeof(buf), fp) != NULL) {
118 i = strtoull(buf, NULL, 10);
119 pclose(fp);
120 } else {
121 pclose(fp);
122 return DVR_FAILURE;
123 }
124
125 *p_segment_nb = i;
126 p = malloc(i * sizeof(uint64_t));
127 for (i = 0;;i++) {
128 memset(fpath, 0, sizeof(fpath));
129 sprintf(fpath, "%s-%04d.ts", location, i);
130 if (access(fpath, 0) != -1) {
131 p[j++] = i;
132 }
133 if (j >= *p_segment_nb) {
134 break;
135 }
136 }
137 *pp_segment_ids = p;
138 }
Pengfei Liub4734232020-01-17 18:25:10 +0800139 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800140}
141
Pengfei Liub4734232020-01-17 18:25:10 +0800142int dvr_segment_get_info(const char *location, uint64_t segment_id, DVR_RecordSegmentInfo_t *p_info)
Pengfei Liuc181a982020-01-07 19:27:13 +0800143{
Pengfei Liub4734232020-01-17 18:25:10 +0800144 int ret;
145 Segment_OpenParams_t open_params;
146 Segment_Handle_t segment_handle;
147
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800148 DVR_RETURN_IF_FALSE(location);
149 DVR_RETURN_IF_FALSE(p_info);
150 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liub4734232020-01-17 18:25:10 +0800151
152 memset(&open_params, 0, sizeof(open_params));
153 memcpy(open_params.location, location, strlen(location));
154 open_params.segment_id = segment_id;
155 open_params.mode = SEGMENT_MODE_READ;
156 ret = segment_open(&open_params, &segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800157 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800158
159 ret = segment_load_info(segment_handle, p_info);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800160 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
hualing chena540a7e2020-03-27 16:44:05 +0800161 //DVR_DEBUG(1, "%s, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d",
162 // __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets);
Pengfei Liub4734232020-01-17 18:25:10 +0800163
164 ret = segment_close(segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800165 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800166
167 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800168}
169
170int dvr_segment_link(const char *location, uint32_t nb_segments, uint64_t *p_segment_ids)
171{
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800172 FILE *fp;
173 char fpath[DVR_MAX_LOCATION_SIZE];
174 uint32_t i;
175 char buf[64];
176
177 DVR_RETURN_IF_FALSE(location);
178 DVR_RETURN_IF_FALSE(p_segment_ids);
179 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
180
181 DVR_DEBUG(1, "%s location:%s, nb_segments:%d", __func__, location, nb_segments);
182 memset(fpath, 0, sizeof(fpath));
183 sprintf(fpath, "%s.list", location);
184 fp = fopen(fpath, "w+");
185 for (i = 0; i< nb_segments; i++) {
186 memset(buf, 0, sizeof(buf));
187 sprintf(buf, "%lld", p_segment_ids[i]);
188 fwrite(buf, 1, strlen(buf), fp);
189 }
190
191 fflush(fp);
192 fsync(fileno(fp));
193 fclose(fp);
Pengfei Liub4734232020-01-17 18:25:10 +0800194 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800195}