blob: 38e363ff04b34ef2f567cdcb5bac4120a8279251 [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));
hualing chen7a56cba2020-04-14 14:09:27 +080064 sprintf(cmd, "rm %s*", location);
hualing chen4b7c15d2020-04-07 16:13:48 +080065 fp = popen(cmd, "r");
66 DVR_RETURN_IF_FALSE(fp);
67 }
hualing chen7a56cba2020-04-14 14:09:27 +080068 DVR_DEBUG(1, "%s location:%s end", __func__, location);
69 fclose(fp);
hualing chen4b7c15d2020-04-07 16:13:48 +080070 return DVR_SUCCESS;
71}
72
73
Pengfei Liuc181a982020-01-07 19:27:13 +080074int dvr_segment_get_list(const char *location, uint32_t *p_segment_nb, uint64_t **pp_segment_ids)
75{
Pengfei Liu3b1a8202020-02-12 23:04:21 +080076 FILE *fp;
77 char fpath[DVR_MAX_LOCATION_SIZE];
78 uint32_t i = 0, j = 0;
79 char buf[64];
80 uint64_t *p = NULL;
81 char cmd[256];
82
83 DVR_RETURN_IF_FALSE(location);
84 DVR_RETURN_IF_FALSE(p_segment_nb);
85 DVR_RETURN_IF_FALSE(pp_segment_ids);
86
87 DVR_DEBUG(1, "%s location:%s", __func__, location);
88 memset(fpath, 0, sizeof(fpath));
89 sprintf(fpath, "%s.list", location);
90
91 if (access(fpath, 0) != -1) {
92 /*the list file is exist*/
93 fp = fopen(fpath, "r");
94 DVR_RETURN_IF_FALSE(fp);
95 /*get segment numbers*/
96 while (fgets(buf, sizeof(buf), fp) != NULL) {
97 i++;
98 }
99 *p_segment_nb = i;
100 rewind(fp);
101 /*malloc*/
102 p = malloc(i * sizeof(uint64_t));
103 i = 0;
104 /*set value*/
105 memset(buf, 0, sizeof(buf));
106 while (fgets(buf, sizeof(buf), fp) != NULL) {
107 p[i++] = strtoull(buf, NULL, 10);
108 memset(buf, 0, sizeof(buf));
109 }
110 *pp_segment_ids = p;
111 fclose(fp);
112 } else {
113 /*the list file does not exist*/
114 memset(cmd, 0, sizeof(cmd));
115 sprintf(cmd, "ls -l %s*.ts | wc -l", location);
116 fp = popen(cmd, "r");
117 DVR_RETURN_IF_FALSE(fp);
118 memset(buf, 0, sizeof(buf));
119 if (fgets(buf, sizeof(buf), fp) != NULL) {
120 i = strtoull(buf, NULL, 10);
121 pclose(fp);
122 } else {
123 pclose(fp);
124 return DVR_FAILURE;
125 }
126
127 *p_segment_nb = i;
128 p = malloc(i * sizeof(uint64_t));
129 for (i = 0;;i++) {
130 memset(fpath, 0, sizeof(fpath));
131 sprintf(fpath, "%s-%04d.ts", location, i);
132 if (access(fpath, 0) != -1) {
133 p[j++] = i;
134 }
135 if (j >= *p_segment_nb) {
136 break;
137 }
138 }
139 *pp_segment_ids = p;
140 }
Pengfei Liub4734232020-01-17 18:25:10 +0800141 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800142}
143
Pengfei Liub4734232020-01-17 18:25:10 +0800144int dvr_segment_get_info(const char *location, uint64_t segment_id, DVR_RecordSegmentInfo_t *p_info)
Pengfei Liuc181a982020-01-07 19:27:13 +0800145{
Pengfei Liub4734232020-01-17 18:25:10 +0800146 int ret;
147 Segment_OpenParams_t open_params;
148 Segment_Handle_t segment_handle;
149
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800150 DVR_RETURN_IF_FALSE(location);
151 DVR_RETURN_IF_FALSE(p_info);
152 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liub4734232020-01-17 18:25:10 +0800153
154 memset(&open_params, 0, sizeof(open_params));
155 memcpy(open_params.location, location, strlen(location));
156 open_params.segment_id = segment_id;
157 open_params.mode = SEGMENT_MODE_READ;
158 ret = segment_open(&open_params, &segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800159 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800160
161 ret = segment_load_info(segment_handle, p_info);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800162 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
hualing chena540a7e2020-03-27 16:44:05 +0800163 //DVR_DEBUG(1, "%s, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d",
164 // __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 +0800165
166 ret = segment_close(segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800167 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800168
169 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800170}
171
172int dvr_segment_link(const char *location, uint32_t nb_segments, uint64_t *p_segment_ids)
173{
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800174 FILE *fp;
175 char fpath[DVR_MAX_LOCATION_SIZE];
176 uint32_t i;
177 char buf[64];
178
179 DVR_RETURN_IF_FALSE(location);
180 DVR_RETURN_IF_FALSE(p_segment_ids);
181 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
182
183 DVR_DEBUG(1, "%s location:%s, nb_segments:%d", __func__, location, nb_segments);
184 memset(fpath, 0, sizeof(fpath));
185 sprintf(fpath, "%s.list", location);
186 fp = fopen(fpath, "w+");
187 for (i = 0; i< nb_segments; i++) {
188 memset(buf, 0, sizeof(buf));
189 sprintf(buf, "%lld", p_segment_ids[i]);
190 fwrite(buf, 1, strlen(buf), fp);
191 }
192
193 fflush(fp);
194 fsync(fileno(fp));
195 fclose(fp);
Pengfei Liub4734232020-01-17 18:25:10 +0800196 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800197}