blob: dc6f6bfe6755530ae4957efcecbafbea0544a636 [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
50int dvr_segment_get_list(const char *location, uint32_t *p_segment_nb, uint64_t **pp_segment_ids)
51{
Pengfei Liu3b1a8202020-02-12 23:04:21 +080052 FILE *fp;
53 char fpath[DVR_MAX_LOCATION_SIZE];
54 uint32_t i = 0, j = 0;
55 char buf[64];
56 uint64_t *p = NULL;
57 char cmd[256];
58
59 DVR_RETURN_IF_FALSE(location);
60 DVR_RETURN_IF_FALSE(p_segment_nb);
61 DVR_RETURN_IF_FALSE(pp_segment_ids);
62
63 DVR_DEBUG(1, "%s location:%s", __func__, location);
64 memset(fpath, 0, sizeof(fpath));
65 sprintf(fpath, "%s.list", location);
66
67 if (access(fpath, 0) != -1) {
68 /*the list file is exist*/
69 fp = fopen(fpath, "r");
70 DVR_RETURN_IF_FALSE(fp);
71 /*get segment numbers*/
72 while (fgets(buf, sizeof(buf), fp) != NULL) {
73 i++;
74 }
75 *p_segment_nb = i;
76 rewind(fp);
77 /*malloc*/
78 p = malloc(i * sizeof(uint64_t));
79 i = 0;
80 /*set value*/
81 memset(buf, 0, sizeof(buf));
82 while (fgets(buf, sizeof(buf), fp) != NULL) {
83 p[i++] = strtoull(buf, NULL, 10);
84 memset(buf, 0, sizeof(buf));
85 }
86 *pp_segment_ids = p;
87 fclose(fp);
88 } else {
89 /*the list file does not exist*/
90 memset(cmd, 0, sizeof(cmd));
91 sprintf(cmd, "ls -l %s*.ts | wc -l", location);
92 fp = popen(cmd, "r");
93 DVR_RETURN_IF_FALSE(fp);
94 memset(buf, 0, sizeof(buf));
95 if (fgets(buf, sizeof(buf), fp) != NULL) {
96 i = strtoull(buf, NULL, 10);
97 pclose(fp);
98 } else {
99 pclose(fp);
100 return DVR_FAILURE;
101 }
102
103 *p_segment_nb = i;
104 p = malloc(i * sizeof(uint64_t));
105 for (i = 0;;i++) {
106 memset(fpath, 0, sizeof(fpath));
107 sprintf(fpath, "%s-%04d.ts", location, i);
108 if (access(fpath, 0) != -1) {
109 p[j++] = i;
110 }
111 if (j >= *p_segment_nb) {
112 break;
113 }
114 }
115 *pp_segment_ids = p;
116 }
Pengfei Liub4734232020-01-17 18:25:10 +0800117 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800118}
119
Pengfei Liub4734232020-01-17 18:25:10 +0800120int dvr_segment_get_info(const char *location, uint64_t segment_id, DVR_RecordSegmentInfo_t *p_info)
Pengfei Liuc181a982020-01-07 19:27:13 +0800121{
Pengfei Liub4734232020-01-17 18:25:10 +0800122 int ret;
123 Segment_OpenParams_t open_params;
124 Segment_Handle_t segment_handle;
125
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800126 DVR_RETURN_IF_FALSE(location);
127 DVR_RETURN_IF_FALSE(p_info);
128 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liub4734232020-01-17 18:25:10 +0800129
130 memset(&open_params, 0, sizeof(open_params));
131 memcpy(open_params.location, location, strlen(location));
132 open_params.segment_id = segment_id;
133 open_params.mode = SEGMENT_MODE_READ;
134 ret = segment_open(&open_params, &segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800135 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800136
137 ret = segment_load_info(segment_handle, p_info);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800138 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
hualing chena540a7e2020-03-27 16:44:05 +0800139 //DVR_DEBUG(1, "%s, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d",
140 // __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 +0800141
142 ret = segment_close(segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800143 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800144
145 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800146}
147
148int dvr_segment_link(const char *location, uint32_t nb_segments, uint64_t *p_segment_ids)
149{
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800150 FILE *fp;
151 char fpath[DVR_MAX_LOCATION_SIZE];
152 uint32_t i;
153 char buf[64];
154
155 DVR_RETURN_IF_FALSE(location);
156 DVR_RETURN_IF_FALSE(p_segment_ids);
157 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
158
159 DVR_DEBUG(1, "%s location:%s, nb_segments:%d", __func__, location, nb_segments);
160 memset(fpath, 0, sizeof(fpath));
161 sprintf(fpath, "%s.list", location);
162 fp = fopen(fpath, "w+");
163 for (i = 0; i< nb_segments; i++) {
164 memset(buf, 0, sizeof(buf));
165 sprintf(buf, "%lld", p_segment_ids[i]);
166 fwrite(buf, 1, strlen(buf), fp);
167 }
168
169 fflush(fp);
170 fsync(fileno(fp));
171 fclose(fp);
Pengfei Liub4734232020-01-17 18:25:10 +0800172 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800173}