blob: 534d0c1a3911b8194fd2a81c385982862bbe3002 [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
20 memcpy(&file, arg, sizeof(DVR_SegmentFile_t));
Pengfei Liu3b1a8202020-02-12 23:04:21 +080021 DVR_DEBUG(1, "%s try to delete [%s-%lld]", __func__, file.location, file.id);
Pengfei Liub4734232020-01-17 18:25:10 +080022 ret = segment_delete(file.location, file.id);
23 DVR_DEBUG(1, "%s delete segment [%s-%lld] %s", __func__, file.location, file.id,
24 ret == DVR_SUCCESS ? "success" : "failed");
25
26 return NULL;
27}
Pengfei Liu3b1a8202020-02-12 23:04:21 +080028
Pengfei Liuc181a982020-01-07 19:27:13 +080029int dvr_segment_delete(const char *location, uint64_t segment_id)
30{
hualing chen6d24aa92020-03-23 18:43:47 +080031
Pengfei Liub4734232020-01-17 18:25:10 +080032 pthread_t thread;
33 static DVR_SegmentFile_t segment;
34
35 DVR_DEBUG(1, "%s in, %s,id:%lld", __func__, location, segment_id);
Pengfei Liu3b1a8202020-02-12 23:04:21 +080036 DVR_RETURN_IF_FALSE(location);
37 DVR_RETURN_IF_FALSE(strlen(location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liub4734232020-01-17 18:25:10 +080038 memset(segment.location, 0, sizeof(segment.location));
39 memcpy(segment.location, location, strlen(location));
40 segment.id = segment_id;
41 pthread_create(&thread, NULL, dvr_segment_thread, &segment);
Pengfei Liu3b1a8202020-02-12 23:04:21 +080042 usleep(10*1000);
Pengfei Liub4734232020-01-17 18:25:10 +080043
44 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +080045}
46
47int dvr_segment_get_list(const char *location, uint32_t *p_segment_nb, uint64_t **pp_segment_ids)
48{
Pengfei Liu3b1a8202020-02-12 23:04:21 +080049 FILE *fp;
50 char fpath[DVR_MAX_LOCATION_SIZE];
51 uint32_t i = 0, j = 0;
52 char buf[64];
53 uint64_t *p = NULL;
54 char cmd[256];
55
56 DVR_RETURN_IF_FALSE(location);
57 DVR_RETURN_IF_FALSE(p_segment_nb);
58 DVR_RETURN_IF_FALSE(pp_segment_ids);
59
60 DVR_DEBUG(1, "%s location:%s", __func__, location);
61 memset(fpath, 0, sizeof(fpath));
62 sprintf(fpath, "%s.list", location);
63
64 if (access(fpath, 0) != -1) {
65 /*the list file is exist*/
66 fp = fopen(fpath, "r");
67 DVR_RETURN_IF_FALSE(fp);
68 /*get segment numbers*/
69 while (fgets(buf, sizeof(buf), fp) != NULL) {
70 i++;
71 }
72 *p_segment_nb = i;
73 rewind(fp);
74 /*malloc*/
75 p = malloc(i * sizeof(uint64_t));
76 i = 0;
77 /*set value*/
78 memset(buf, 0, sizeof(buf));
79 while (fgets(buf, sizeof(buf), fp) != NULL) {
80 p[i++] = strtoull(buf, NULL, 10);
81 memset(buf, 0, sizeof(buf));
82 }
83 *pp_segment_ids = p;
84 fclose(fp);
85 } else {
86 /*the list file does not exist*/
87 memset(cmd, 0, sizeof(cmd));
88 sprintf(cmd, "ls -l %s*.ts | wc -l", location);
89 fp = popen(cmd, "r");
90 DVR_RETURN_IF_FALSE(fp);
91 memset(buf, 0, sizeof(buf));
92 if (fgets(buf, sizeof(buf), fp) != NULL) {
93 i = strtoull(buf, NULL, 10);
94 pclose(fp);
95 } else {
96 pclose(fp);
97 return DVR_FAILURE;
98 }
99
100 *p_segment_nb = i;
101 p = malloc(i * sizeof(uint64_t));
102 for (i = 0;;i++) {
103 memset(fpath, 0, sizeof(fpath));
104 sprintf(fpath, "%s-%04d.ts", location, i);
105 if (access(fpath, 0) != -1) {
106 p[j++] = i;
107 }
108 if (j >= *p_segment_nb) {
109 break;
110 }
111 }
112 *pp_segment_ids = p;
113 }
Pengfei Liub4734232020-01-17 18:25:10 +0800114 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800115}
116
Pengfei Liub4734232020-01-17 18:25:10 +0800117int dvr_segment_get_info(const char *location, uint64_t segment_id, DVR_RecordSegmentInfo_t *p_info)
Pengfei Liuc181a982020-01-07 19:27:13 +0800118{
Pengfei Liub4734232020-01-17 18:25:10 +0800119 int ret;
120 Segment_OpenParams_t open_params;
121 Segment_Handle_t segment_handle;
122
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800123 DVR_RETURN_IF_FALSE(location);
124 DVR_RETURN_IF_FALSE(p_info);
125 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liub4734232020-01-17 18:25:10 +0800126
127 memset(&open_params, 0, sizeof(open_params));
128 memcpy(open_params.location, location, strlen(location));
129 open_params.segment_id = segment_id;
130 open_params.mode = SEGMENT_MODE_READ;
131 ret = segment_open(&open_params, &segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800132 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800133
134 ret = segment_load_info(segment_handle, p_info);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800135 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800136 DVR_DEBUG(1, "%s, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d",
137 __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets);
138
139 ret = segment_close(segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800140 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800141
142 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800143}
144
145int dvr_segment_link(const char *location, uint32_t nb_segments, uint64_t *p_segment_ids)
146{
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800147 FILE *fp;
148 char fpath[DVR_MAX_LOCATION_SIZE];
149 uint32_t i;
150 char buf[64];
151
152 DVR_RETURN_IF_FALSE(location);
153 DVR_RETURN_IF_FALSE(p_segment_ids);
154 DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE);
155
156 DVR_DEBUG(1, "%s location:%s, nb_segments:%d", __func__, location, nb_segments);
157 memset(fpath, 0, sizeof(fpath));
158 sprintf(fpath, "%s.list", location);
159 fp = fopen(fpath, "w+");
160 for (i = 0; i< nb_segments; i++) {
161 memset(buf, 0, sizeof(buf));
162 sprintf(buf, "%lld", p_segment_ids[i]);
163 fwrite(buf, 1, strlen(buf), fp);
164 }
165
166 fflush(fp);
167 fsync(fileno(fp));
168 fclose(fp);
Pengfei Liub4734232020-01-17 18:25:10 +0800169 return DVR_SUCCESS;
Pengfei Liuc181a982020-01-07 19:27:13 +0800170}