blob: b6ba04e5e87f964c75bb1ab2d5583b58a4f5aba7 [file] [log] [blame]
Pengfei Liuc181a982020-01-07 19:27:13 +08001#include <stdio.h>
Pengfei Liub4734232020-01-17 18:25:10 +08002#include <unistd.h>
3#include <stdlib.h>
Pengfei Liuc181a982020-01-07 19:27:13 +08004#include <pthread.h>
Pengfei Liub4734232020-01-17 18:25:10 +08005#include <string.h>
Pengfei Liu47ed6c92020-01-17 11:23:41 +08006#include "dvr_types.h"
Pengfei Liuc181a982020-01-07 19:27:13 +08007#include "dvr_record.h"
8#include "dvr_crypto.h"
9#include "record_device.h"
10#include "segment.h"
pengfei.liu567d6d82020-04-17 16:48:59 +080011#include <sys/time.h>
Pengfei Liuc181a982020-01-07 19:27:13 +080012
hualing chen5605eed2020-05-26 18:18:06 +080013//#define DEBUG_PERFORMANCE
Pengfei Liuc181a982020-01-07 19:27:13 +080014#define MAX_DVR_RECORD_SESSION_COUNT 2
hualing chen266b9502020-04-04 17:39:39 +080015#define RECORD_BLOCK_SIZE (256 * 1024)
pengfei.liuab5a2262020-02-14 17:33:40 +080016
17/**\brief DVR index file type*/
18typedef enum {
19 DVR_INDEX_TYPE_PCR, /**< DVR index file use pcr*/
20 DVR_INDEX_TYPE_LOCAL_CLOCK, /**< DVR index file use local clock*/
21 DVR_INDEX_TYPE_INVALID /**< DVR index file type invalid type*/
22} DVR_IndexType_t;
23
24/**\brief DVR VOD context*/
25typedef struct {
26 pthread_mutex_t mutex; /**< VOD mutex lock*/
27 pthread_cond_t cond; /**< VOD condition*/
28 void *buffer; /**< VOD buffer*/
29 uint32_t buf_len; /**< VOD buffer len*/
30} DVR_VodContext_t;
31
pengfei.liu07ddc8a2020-03-24 23:36:53 +080032/**\brief DVR record secure mode buffer*/
33typedef struct {
34 uint32_t addr; /**< Secure mode record buffer address*/
35 uint32_t len; /**< Secure mode record buffer length*/
36} DVR_SecureBuffer_t;
37
Pengfei Liub4734232020-01-17 18:25:10 +080038/**\brief DVR record context*/
Pengfei Liuc181a982020-01-07 19:27:13 +080039typedef struct {
Pengfei Liub4734232020-01-17 18:25:10 +080040 pthread_t thread; /**< DVR thread handle*/
41 Record_DeviceHandle_t dev_handle; /**< DVR device handle*/
42 Segment_Handle_t segment_handle; /**< DVR segment handle*/
43 DVR_RecordState_t state; /**< DVR record state*/
44 char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/
45 DVR_RecordSegmentStartParams_t segment_params; /**< DVR record start parameters*/
46 DVR_RecordSegmentInfo_t segment_info; /**< DVR record current segment info*/
47 size_t notification_size; /**< DVR record nogification size*/
48 DVR_RecordEventFunction_t event_notify_fn; /**< DVR record event notify function*/
49 void *event_userdata; /**< DVR record event userdata*/
pengfei.liuab5a2262020-02-14 17:33:40 +080050 //DVR_VodContext_t vod; /**< DVR record vod context*/
51 int is_vod; /**< Indicate current mode is VOD record mode*/
pengfei.liu27cc4ec2020-04-03 16:28:16 +080052 DVR_CryptoFunction_t enc_func; /**< Encrypt function*/
pengfei.liu07ddc8a2020-03-24 23:36:53 +080053 void *enc_userdata; /**< Encrypt userdata*/
54 int is_secure_mode; /**< Record session run in secure pipeline */
hualing chen2615aa82020-04-02 21:32:51 +080055 size_t last_send_size; /**< Last send notify segment size */
pengfei.liufda2a972020-04-09 14:47:15 +080056 uint32_t block_size; /**< DVR record block size */
Pengfei Liuc181a982020-01-07 19:27:13 +080057} DVR_RecordContext_t;
58
59static DVR_RecordContext_t record_ctx[MAX_DVR_RECORD_SESSION_COUNT] = {
60 {
Pengfei Liub4734232020-01-17 18:25:10 +080061 .state = DVR_RECORD_STATE_CLOSED
Pengfei Liuc181a982020-01-07 19:27:13 +080062 },
63 {
Pengfei Liub4734232020-01-17 18:25:10 +080064 .state = DVR_RECORD_STATE_CLOSED
Pengfei Liuc181a982020-01-07 19:27:13 +080065 }
66};
67
Zhiqiang Han51646a02020-04-05 18:43:22 +080068static int record_is_valid_pid(DVR_RecordContext_t *p_ctx, int pid)
69{
70 int i;
71
72 for (i = 0; i < p_ctx->segment_info.nb_pids; i++) {
73 if (pid == p_ctx->segment_info.pids[i].pid)
74 return 1;
75 }
76 return 0;
77}
78
pengfei.liuab5a2262020-02-14 17:33:40 +080079static int record_save_pcr(DVR_RecordContext_t *p_ctx, uint8_t *buf, loff_t pos)
80{
81 uint8_t *p = buf;
82 int len;
83 uint8_t afc;
84 uint64_t pcr = 0;
85 int has_pcr = 0;
86 int pid;
87 int adp_field_len;
88
89 pid = ((p[1] & 0x1f) << 8) | p[2];
Zhiqiang Han51646a02020-04-05 18:43:22 +080090 if (pid == 0x1fff || !record_is_valid_pid(p_ctx, pid))
pengfei.liuab5a2262020-02-14 17:33:40 +080091 return has_pcr;
92
93 //scramble = p[3] >> 6;
94 //cc = p[3] & 0x0f;
95 afc = (p[3] >> 4) & 0x03;
96
97 p += 4;
98 len = 184;
99
100 if (afc & 2) {
101 adp_field_len = p[0];
102 /* Skip adaptation len */
103 p++;
104 len--;
105 /* Parse pcr field, see 13818 spec table I-2-6,adaptation_field */
hualing chen5605eed2020-05-26 18:18:06 +0800106 if (p[0] & 0x10 && len >= 6 && adp_field_len >= 6) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800107 /* get pcr value,pcr is 33bit value */
108 pcr = (((uint64_t)(p[1])) << 25)
109 | (((uint64_t)p[2]) << 17)
110 | (((uint64_t)(p[3])) << 9)
111 | (((uint64_t)p[4]) << 1)
112 | ((((uint64_t)p[5]) & 0x80) >> 7);
113 has_pcr = 1;
114 }
115
116 p += adp_field_len;
117 len -= adp_field_len;
118
119 if (len < 0) {
120 DVR_DEBUG(1, "parser pcr: illegal adaptation field length");
121 return 0;
122 }
123 }
124
125 if (has_pcr) {
126 segment_update_pts(p_ctx->segment_handle, pcr/90, pos);
127 }
128 return has_pcr;
129}
130
131static int record_do_pcr_index(DVR_RecordContext_t *p_ctx, uint8_t *buf, int len)
132{
133 uint8_t *p = buf;
134 int left = len;
135 loff_t pos;
136 int has_pcr = 0;
137
138 pos = segment_tell_position(p_ctx->segment_handle);
139 while (left >= 188) {
140 if (*p == 0x47) {
141 has_pcr |= record_save_pcr(p_ctx, p, pos);
142 p += 188;
143 left -= 188;
144 pos += 188;
145 } else {
146 p++;
147 left --;
148 pos++;
149 }
150 }
151 return has_pcr;
152}
153
pengfei.liu567d6d82020-04-17 16:48:59 +0800154static int get_diff_time(struct timeval start_tv, struct timeval end_tv)
155{
156 return end_tv.tv_sec * 1000 + end_tv.tv_usec / 1000 - start_tv.tv_sec * 1000 - start_tv.tv_usec / 1000;
157}
158
Pengfei Liuc181a982020-01-07 19:27:13 +0800159void *record_thread(void *arg)
160{
161 DVR_RecordContext_t *p_ctx = (DVR_RecordContext_t *)arg;
162 ssize_t len;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800163 uint8_t *buf, *buf_out;
pengfei.liufda2a972020-04-09 14:47:15 +0800164 uint32_t block_size = p_ctx->block_size;
pengfei.liuab5a2262020-02-14 17:33:40 +0800165 loff_t pos = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800166 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800167 struct timespec start_ts, end_ts;
168 DVR_RecordStatus_t record_status;
pengfei.liuab5a2262020-02-14 17:33:40 +0800169 int has_pcr;
170 int index_type = DVR_INDEX_TYPE_INVALID;
hualing chen87072a82020-03-12 16:20:12 +0800171 time_t pre_time = 0;
hualing chen2932d372020-04-29 13:44:00 +0800172 #define DVR_STORE_INFO_TIME (400)
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800173 DVR_SecureBuffer_t secure_buf;
Pengfei Liuc181a982020-01-07 19:27:13 +0800174
Pengfei Liub4734232020-01-17 18:25:10 +0800175 buf = (uint8_t *)malloc(block_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800176 if (!buf) {
177 DVR_DEBUG(1, "%s, malloc failed", __func__);
178 return NULL;
179 }
180
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800181 buf_out = (uint8_t *)malloc(block_size + 188);
182 if (!buf_out) {
183 DVR_DEBUG(1, "%s, malloc failed", __func__);
Pengfei Liufaf38e42020-05-22 00:28:02 +0800184 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800185 return NULL;
186 }
187
hualing chen266b9502020-04-04 17:39:39 +0800188 memset(&record_status, 0, sizeof(record_status));
189 record_status.state = DVR_RECORD_STATE_STARTED;
pengfei.liufda2a972020-04-09 14:47:15 +0800190 if (p_ctx->event_notify_fn) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800191 record_status.info.id = p_ctx->segment_info.id;
pengfei.liufda2a972020-04-09 14:47:15 +0800192 p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata);
hualing chen4b7c15d2020-04-07 16:13:48 +0800193 DVR_DEBUG(1, "%s line %d notify record status, state:%d id=%lld",
194 __func__,__LINE__, record_status.state, p_ctx->segment_info.id);
pengfei.liufda2a972020-04-09 14:47:15 +0800195 }
196 DVR_DEBUG(1, "%s, secure_mode:%d, block_size:%d", __func__, p_ctx->is_secure_mode, block_size);
hualing chen266b9502020-04-04 17:39:39 +0800197
Pengfei Liub4734232020-01-17 18:25:10 +0800198 clock_gettime(CLOCK_MONOTONIC, &start_ts);
pengfei.liu567d6d82020-04-17 16:48:59 +0800199
200 struct timeval t1, t2, t3, t4, t5, t6, t7;
Pengfei Liuc181a982020-01-07 19:27:13 +0800201 while (p_ctx->state == DVR_RECORD_STATE_STARTED) {
pengfei.liu567d6d82020-04-17 16:48:59 +0800202 gettimeofday(&t1, NULL);
hualing chenc70a8df2020-05-12 19:23:11 +0800203
pengfei.liuab5a2262020-02-14 17:33:40 +0800204 /* data from dmx, normal dvr case */
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800205 if (p_ctx->is_secure_mode) {
206 memset(&secure_buf, 0, sizeof(secure_buf));
207 len = record_device_read(p_ctx->dev_handle, &secure_buf, sizeof(secure_buf), 1000);
hualing chen266b9502020-04-04 17:39:39 +0800208 if (len != DVR_FAILURE) {
209 DVR_DEBUG(1, "%s, secure_buf:%#x, size:%#x", __func__, secure_buf.addr, secure_buf.len);
210 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800211 } else {
212 len = record_device_read(p_ctx->dev_handle, buf, block_size, 1000);
213 }
Pengfei Liub4734232020-01-17 18:25:10 +0800214 if (len == DVR_FAILURE) {
hualing chen6c126382020-04-13 15:47:51 +0800215 //usleep(10*1000);
hualing chen2932d372020-04-29 13:44:00 +0800216 DVR_DEBUG(1, "%s, start_read error", __func__);
Pengfei Liub4734232020-01-17 18:25:10 +0800217 continue;
218 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800219 gettimeofday(&t2, NULL);
hualing chenc70a8df2020-05-12 19:23:11 +0800220
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800221 /* Got data from device, record it */
222 if (p_ctx->enc_func) {
223 /* Encrypt record data */
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800224 DVR_CryptoParams_t crypto_params;
225
226 memset(&crypto_params, 0, sizeof(crypto_params));
227 crypto_params.type = DVR_CRYPTO_TYPE_ENCRYPT;
hualing chen7a56cba2020-04-14 14:09:27 +0800228 memcpy(crypto_params.location, p_ctx->location, sizeof(p_ctx->location));
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800229 crypto_params.segment_id = p_ctx->segment_info.id;
230 crypto_params.offset = p_ctx->segment_info.size;
231
232 if (p_ctx->is_secure_mode) {
233 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_SECURE;
234 crypto_params.input_buffer.addr = secure_buf.addr;
235 crypto_params.input_buffer.size = secure_buf.len;
236 } else {
237 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
238 crypto_params.input_buffer.addr = (size_t)buf;
239 crypto_params.input_buffer.size = len;
240 }
241
242 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL;
243 crypto_params.output_buffer.addr = (size_t)buf_out;
244 crypto_params.output_buffer.size = block_size + 188;
245
246 p_ctx->enc_func(&crypto_params, p_ctx->enc_userdata);
pengfei.liu567d6d82020-04-17 16:48:59 +0800247 gettimeofday(&t3, NULL);
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800248 /* Out buffer length may not equal in buffer length */
249 ret = segment_write(p_ctx->segment_handle, buf_out, crypto_params.output_size);
250 len = crypto_params.output_size;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800251 } else {
pengfei.liu567d6d82020-04-17 16:48:59 +0800252 gettimeofday(&t3, NULL);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800253 ret = segment_write(p_ctx->segment_handle, buf, len);
254 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800255 gettimeofday(&t4, NULL);
hualing chen626204e2020-04-07 11:55:08 +0800256 //add DVR_RECORD_EVENT_WRITE_ERROR event if write error
pengfei.liufda2a972020-04-09 14:47:15 +0800257 if (ret == -1 && len > 0 && p_ctx->event_notify_fn) {
hualing chen626204e2020-04-07 11:55:08 +0800258 //send write event
hualing chen9b434f02020-06-10 15:06:54 +0800259 if (p_ctx->event_notify_fn) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800260 memset(&record_status, 0, sizeof(record_status));
hualing chenfbf8e022020-06-15 13:43:11 +0800261 DVR_DEBUG(1, "%s,send event write error", __func__,__LINE__);
hualing chen4b7c15d2020-04-07 16:13:48 +0800262 p_ctx->event_notify_fn(DVR_RECORD_EVENT_WRITE_ERROR, &record_status, p_ctx->event_userdata);
263 }
hualing chenc70a8df2020-05-12 19:23:11 +0800264 DVR_DEBUG(1, "%s,write error %d", __func__,__LINE__);
hualing chen626204e2020-04-07 11:55:08 +0800265 goto end;
266 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800267 /* Do time index */
268 uint8_t *index_buf = p_ctx->enc_func ? buf_out : buf;
pengfei.liuab5a2262020-02-14 17:33:40 +0800269 pos = segment_tell_position(p_ctx->segment_handle);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800270 has_pcr = record_do_pcr_index(p_ctx, index_buf, len);
pengfei.liuab5a2262020-02-14 17:33:40 +0800271 if (has_pcr == 0 && index_type == DVR_INDEX_TYPE_INVALID) {
272 clock_gettime(CLOCK_MONOTONIC, &end_ts);
273 if ((end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) -
274 (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) > 40) {
275 /* PCR interval threshlod > 40 ms*/
276 DVR_DEBUG(1, "%s use local clock time index", __func__);
277 index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
278 }
279 } else if (has_pcr && index_type == DVR_INDEX_TYPE_INVALID){
280 DVR_DEBUG(1, "%s use pcr time index", __func__);
281 index_type = DVR_INDEX_TYPE_PCR;
Pengfei Liuc181a982020-01-07 19:27:13 +0800282 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800283 gettimeofday(&t5, NULL);
pengfei.liuab5a2262020-02-14 17:33:40 +0800284
285 /* Update segment info */
Pengfei Liub4734232020-01-17 18:25:10 +0800286 p_ctx->segment_info.size += len;
pengfei.liuab5a2262020-02-14 17:33:40 +0800287 /*Duration need use pcr to calculate, todo...*/
288 if (index_type == DVR_INDEX_TYPE_PCR) {
pengfei.liu8b563292020-02-26 15:49:02 +0800289 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
hualing chen87072a82020-03-12 16:20:12 +0800290 if (pre_time == 0)
291 pre_time = p_ctx->segment_info.duration;
pengfei.liuab5a2262020-02-14 17:33:40 +0800292 } else if (index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) {
293 clock_gettime(CLOCK_MONOTONIC, &end_ts);
294 p_ctx->segment_info.duration = (end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) -
295 (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000);
hualing chen87072a82020-03-12 16:20:12 +0800296 if (pre_time == 0)
297 pre_time = p_ctx->segment_info.duration;
pengfei.liuab5a2262020-02-14 17:33:40 +0800298 segment_update_pts(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
299 } else {
300 DVR_DEBUG(1, "%s can NOT do time index", __func__);
301 }
302 p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188;
Pengfei Liub4734232020-01-17 18:25:10 +0800303
hualing chen87072a82020-03-12 16:20:12 +0800304 if (p_ctx->segment_info.duration - pre_time > DVR_STORE_INFO_TIME) {
305 pre_time = p_ctx->segment_info.duration + DVR_STORE_INFO_TIME;
hualing chenc70a8df2020-05-12 19:23:11 +0800306 segment_store_info(p_ctx->segment_handle, &(p_ctx->segment_info));
hualing chen87072a82020-03-12 16:20:12 +0800307 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800308 gettimeofday(&t6, NULL);
hualing chen87072a82020-03-12 16:20:12 +0800309 /*Event notification*/
Pengfei Liub4734232020-01-17 18:25:10 +0800310 if (p_ctx->notification_size &&
311 p_ctx->event_notify_fn &&
hualing chen2615aa82020-04-02 21:32:51 +0800312 /*!(p_ctx->segment_info.size % p_ctx->notification_size)*/
313 (p_ctx->segment_info.size -p_ctx->last_send_size) >= p_ctx->notification_size&&
pengfei.liuab5a2262020-02-14 17:33:40 +0800314 p_ctx->segment_info.duration > 0) {
Pengfei Liub4734232020-01-17 18:25:10 +0800315 memset(&record_status, 0, sizeof(record_status));
pengfei.liuab5a2262020-02-14 17:33:40 +0800316 //clock_gettime(CLOCK_MONOTONIC, &end_ts);
hualing chen2615aa82020-04-02 21:32:51 +0800317 p_ctx->last_send_size = p_ctx->segment_info.size;
Pengfei Liub4734232020-01-17 18:25:10 +0800318 record_status.state = p_ctx->state;
319 record_status.info.id = p_ctx->segment_info.id;
pengfei.liuab5a2262020-02-14 17:33:40 +0800320 record_status.info.duration = p_ctx->segment_info.duration;
Pengfei Liub4734232020-01-17 18:25:10 +0800321 record_status.info.size = p_ctx->segment_info.size;
322 record_status.info.nb_packets = p_ctx->segment_info.size/188;
323 p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata);
324 DVR_DEBUG(1, "%s notify record status, state:%d, id:%lld, duration:%ld ms, size:%zu",
325 __func__, record_status.state, record_status.info.id, record_status.info.duration, record_status.info.size);
326 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800327 gettimeofday(&t7, NULL);
328#ifdef DEBUG_PERFORMANCE
hualing chen2932d372020-04-29 13:44:00 +0800329 DVR_DEBUG(1, "record count, read:%dms, encrypt:%dms, write:%dms, index:%dms, store:%dms, notify:%dms total:%dms read len:%zd ",
pengfei.liu567d6d82020-04-17 16:48:59 +0800330 get_diff_time(t1, t2), get_diff_time(t2, t3), get_diff_time(t3, t4), get_diff_time(t4, t5),
hualing chen2932d372020-04-29 13:44:00 +0800331 get_diff_time(t5, t6), get_diff_time(t6, t7), get_diff_time(t1, t5), len);
pengfei.liu567d6d82020-04-17 16:48:59 +0800332#endif
Pengfei Liuc181a982020-01-07 19:27:13 +0800333 }
hualing chen626204e2020-04-07 11:55:08 +0800334end:
Pengfei Liub4734232020-01-17 18:25:10 +0800335 free((void *)buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800336 free((void *)buf_out);
Pengfei Liub4734232020-01-17 18:25:10 +0800337 DVR_DEBUG(1, "exit %s", __func__);
Pengfei Liuc181a982020-01-07 19:27:13 +0800338 return NULL;
339}
340
341int dvr_record_open(DVR_RecordHandle_t *p_handle, DVR_RecordOpenParams_t *params)
342{
343 DVR_RecordContext_t *p_ctx;
344 Record_DeviceOpenParams_t dev_open_params;
345 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800346 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800347
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800348 DVR_RETURN_IF_FALSE(p_handle);
349 DVR_RETURN_IF_FALSE(params);
Pengfei Liuc181a982020-01-07 19:27:13 +0800350
351 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
Pengfei Liub4734232020-01-17 18:25:10 +0800352 if (record_ctx[i].state == DVR_RECORD_STATE_CLOSED) {
Pengfei Liuc181a982020-01-07 19:27:13 +0800353 break;
354 }
355 }
Pengfei Liufaf38e42020-05-22 00:28:02 +0800356 DVR_RETURN_IF_FALSE(i < MAX_DVR_RECORD_SESSION_COUNT);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800357 DVR_RETURN_IF_FALSE(record_ctx[i].state == DVR_RECORD_STATE_CLOSED);
Pengfei Liuc181a982020-01-07 19:27:13 +0800358 p_ctx = &record_ctx[i];
hualing chenfbf8e022020-06-15 13:43:11 +0800359 DVR_DEBUG(1, "%s , current state:%d, dmx_id:%d, notification_size:%zu ", __func__,
Pengfei Liub4734232020-01-17 18:25:10 +0800360 p_ctx->state, params->dmx_dev_id, params->notification_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800361
Pengfei Liub4734232020-01-17 18:25:10 +0800362 /*Process event params*/
363 p_ctx->notification_size = params->notification_size;
364 p_ctx->event_notify_fn = params->event_fn;
365 p_ctx->event_userdata = params->event_userdata;
hualing chen2615aa82020-04-02 21:32:51 +0800366 p_ctx->last_send_size = 0;
Pengfei Liub4734232020-01-17 18:25:10 +0800367 /*Process crypto params, todo*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800368
Pengfei Liub4734232020-01-17 18:25:10 +0800369 memset((void *)&dev_open_params, 0, sizeof(dev_open_params));
pengfei.liuab5a2262020-02-14 17:33:40 +0800370 if (params->data_from_memory) {
371 /* data from memory, VOD case */
372 p_ctx->is_vod = 1;
373 } else {
374 p_ctx->is_vod = 0;
375 /* data from dmx, normal dvr case */
hualing chen958fe4c2020-03-24 17:35:07 +0800376 dev_open_params.dmx_dev_id = params->dmx_dev_id;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800377 dev_open_params.buf_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE);
Pengfei Liub4734232020-01-17 18:25:10 +0800378
pengfei.liuab5a2262020-02-14 17:33:40 +0800379 ret = record_device_open(&p_ctx->dev_handle, &dev_open_params);
380 if (ret != DVR_SUCCESS) {
381 DVR_DEBUG(1, "%s, open record devices failed", __func__);
382 return DVR_FAILURE;
383 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800384 }
385
pengfei.liufda2a972020-04-09 14:47:15 +0800386 p_ctx->block_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800387 p_ctx->enc_func = NULL;
388 p_ctx->enc_userdata = NULL;
389 p_ctx->is_secure_mode = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800390 p_ctx->state = DVR_RECORD_STATE_OPENED;
391
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800392 *p_handle = p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800393 return DVR_SUCCESS;
394}
395
396int dvr_record_close(DVR_RecordHandle_t handle)
397{
398 DVR_RecordContext_t *p_ctx;
399 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800400 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800401
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800402 p_ctx = (DVR_RecordContext_t *)handle;
403 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
404 if (p_ctx == &record_ctx[i])
405 break;
406 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800407 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800408
Pengfei Liub4734232020-01-17 18:25:10 +0800409 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800410 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
Pengfei Liuc181a982020-01-07 19:27:13 +0800411
pengfei.liuab5a2262020-02-14 17:33:40 +0800412 if (p_ctx->is_vod) {
413 ret = DVR_SUCCESS;
414 } else {
415 ret = record_device_close(p_ctx->dev_handle);
416 if (ret != DVR_SUCCESS) {
417 DVR_DEBUG(1, "%s, failed", __func__);
418 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800419 }
420
Pengfei Liub4734232020-01-17 18:25:10 +0800421 p_ctx->state = DVR_RECORD_STATE_CLOSED;
Pengfei Liuc181a982020-01-07 19:27:13 +0800422 return ret;
423}
424
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800425#if 0
Pengfei Liuc181a982020-01-07 19:27:13 +0800426int dvr_record_register_encryption(DVR_RecordHandle_t handle,
427 DVR_CryptoFunction_t cb,
428 DVR_CryptoParams_t params,
429 void *userdata)
430{
431 return DVR_SUCCESS;
432}
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800433#endif
Pengfei Liuc181a982020-01-07 19:27:13 +0800434
435int dvr_record_start_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params)
436{
437 DVR_RecordContext_t *p_ctx;
438 Segment_OpenParams_t open_params;
439 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800440 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800441
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800442 p_ctx = (DVR_RecordContext_t *)handle;
443 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
444 if (p_ctx == &record_ctx[i])
445 break;
446 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800447 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800448
Pengfei Liub4734232020-01-17 18:25:10 +0800449 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800450 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
451 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
452 DVR_RETURN_IF_FALSE(params);
Pengfei Liuc181a982020-01-07 19:27:13 +0800453
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800454 DVR_RETURN_IF_FALSE(strlen((const char *)params->location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liuc181a982020-01-07 19:27:13 +0800455 memset(&open_params, 0, sizeof(open_params));
hualing chen7a56cba2020-04-14 14:09:27 +0800456 memcpy(open_params.location, params->location, sizeof(params->location));
Pengfei Liuc181a982020-01-07 19:27:13 +0800457 open_params.segment_id = params->segment.segment_id;
458 open_params.mode = SEGMENT_MODE_WRITE;
459
460 ret = segment_open(&open_params, &p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800461 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800462
Pengfei Liub4734232020-01-17 18:25:10 +0800463 /*process params*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800464 {
hualing chen7a56cba2020-04-14 14:09:27 +0800465 memcpy(p_ctx->location, params->location, sizeof(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800466 //need all params??
Pengfei Liuc181a982020-01-07 19:27:13 +0800467 memcpy(&p_ctx->segment_params, &params->segment, sizeof(params->segment));
Pengfei Liub4734232020-01-17 18:25:10 +0800468 /*save current segment info*/
469 memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info));
470 p_ctx->segment_info.id = params->segment.segment_id;
471 p_ctx->segment_info.nb_pids = params->segment.nb_pids;
472 memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800473 }
474
pengfei.liuab5a2262020-02-14 17:33:40 +0800475 if (!p_ctx->is_vod) {
476 /* normal dvr case */
477 for (i = 0; i < params->segment.nb_pids; i++) {
478 ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
479 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
480 }
481 ret = record_device_start(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800482 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800483 }
484
Zhiqiang Han5ebad992020-04-28 18:19:59 +0800485 ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info);
486
Pengfei Liuc181a982020-01-07 19:27:13 +0800487 p_ctx->state = DVR_RECORD_STATE_STARTED;
pengfei.liuab5a2262020-02-14 17:33:40 +0800488 if (!p_ctx->is_vod)
489 pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +0800490
Pengfei Liuc181a982020-01-07 19:27:13 +0800491 return DVR_SUCCESS;
492}
493
494int dvr_record_next_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, DVR_RecordSegmentInfo_t *p_info)
495{
496 DVR_RecordContext_t *p_ctx;
Pengfei Liub4734232020-01-17 18:25:10 +0800497 Segment_OpenParams_t open_params;
Pengfei Liuc181a982020-01-07 19:27:13 +0800498 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800499 uint32_t i;
hualing chen2932d372020-04-29 13:44:00 +0800500 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800501
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800502 p_ctx = (DVR_RecordContext_t *)handle;
503 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
504 if (p_ctx == &record_ctx[i])
505 break;
506 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800507 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800508
Pengfei Liub4734232020-01-17 18:25:10 +0800509 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800510 DVR_RETURN_IF_FALSE(p_ctx->state == DVR_RECORD_STATE_STARTED);
511 //DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
512 DVR_RETURN_IF_FALSE(params);
513 DVR_RETURN_IF_FALSE(p_info);
pengfei.liuab5a2262020-02-14 17:33:40 +0800514 DVR_RETURN_IF_FALSE(!p_ctx->is_vod);
Pengfei Liuc181a982020-01-07 19:27:13 +0800515
516 /*Stop the on going record segment*/
Pengfei Liub4734232020-01-17 18:25:10 +0800517 //ret = record_device_stop(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800518 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800519 p_ctx->state = DVR_RECORD_STATE_STOPPED;
520 pthread_join(p_ctx->thread, NULL);
521
hualing chen2932d372020-04-29 13:44:00 +0800522 //add index file store
523 pos = segment_tell_position(p_ctx->segment_handle);
524 segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
525
526 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
Pengfei Liub4734232020-01-17 18:25:10 +0800527 /*Update segment info*/
528 memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
529
530 ret = segment_store_info(p_ctx->segment_handle, p_info);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800531 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800532
hualing chena540a7e2020-03-27 16:44:05 +0800533 DVR_DEBUG(1, "%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d params->segment.nb_pids:%d",
534 __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets, params->segment.nb_pids);
Pengfei Liuc181a982020-01-07 19:27:13 +0800535
Pengfei Liub4734232020-01-17 18:25:10 +0800536 /*Close current segment*/
537 ret = segment_close(p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800538 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800539 /*Open the new record segment*/
540 memset(&open_params, 0, sizeof(open_params));
hualing chen7a56cba2020-04-14 14:09:27 +0800541 memcpy(open_params.location, p_ctx->location, sizeof(p_ctx->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800542 open_params.segment_id = params->segment.segment_id;
543 open_params.mode = SEGMENT_MODE_WRITE;
hualing chen7a56cba2020-04-14 14:09:27 +0800544 DVR_DEBUG(1, "%s: p_ctx->location:%s params->location:%s", __func__, p_ctx->location,params->location);
Pengfei Liub4734232020-01-17 18:25:10 +0800545
546 ret = segment_open(&open_params, &p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800547 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800548 /*process params*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800549 {
Pengfei Liub4734232020-01-17 18:25:10 +0800550 //need all params??
Pengfei Liuc181a982020-01-07 19:27:13 +0800551 memcpy(&p_ctx->segment_params, &params->segment, sizeof(params->segment));
Pengfei Liub4734232020-01-17 18:25:10 +0800552 /*save current segment info*/
553 memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info));
554 p_ctx->segment_info.id = params->segment.segment_id;
555 memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800556 }
557
Pengfei Liub4734232020-01-17 18:25:10 +0800558 p_ctx->segment_info.nb_pids = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800559 for (i = 0; i < params->segment.nb_pids; i++) {
Pengfei Liub4734232020-01-17 18:25:10 +0800560 switch (params->segment.pid_action[i]) {
561 case DVR_RECORD_PID_CREATE:
562 DVR_DEBUG(1, "%s create pid:%d", __func__, params->segment.pids[i].pid);
563 ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
564 p_ctx->segment_info.nb_pids++;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800565 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800566 break;
567 case DVR_RECORD_PID_KEEP:
568 DVR_DEBUG(1, "%s keep pid:%d", __func__, params->segment.pids[i].pid);
569 p_ctx->segment_info.nb_pids++;
570 break;
571 case DVR_RECORD_PID_CLOSE:
572 DVR_DEBUG(1, "%s close pid:%d", __func__, params->segment.pids[i].pid);
573 ret = record_device_remove_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800574 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800575 break;
576 default:
577 DVR_DEBUG(1, "%s wrong action pid:%d", __func__, params->segment.pids[i].pid);
578 return DVR_FAILURE;
579 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800580 }
581
Pengfei Liub4734232020-01-17 18:25:10 +0800582 //ret = record_device_start(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800583 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
hualing chen4b7c15d2020-04-07 16:13:48 +0800584 /*Update segment info*/
585 ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info);
Pengfei Liuc181a982020-01-07 19:27:13 +0800586
Pengfei Liuc181a982020-01-07 19:27:13 +0800587 p_ctx->state = DVR_RECORD_STATE_STARTED;
Zhiqiang Han0d60f2b2020-05-26 14:39:54 +0800588 pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800589 return DVR_SUCCESS;
590}
591
592int dvr_record_stop_segment(DVR_RecordHandle_t handle, DVR_RecordSegmentInfo_t *p_info)
593{
594 DVR_RecordContext_t *p_ctx;
595 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800596 uint32_t i;
hualing chen2932d372020-04-29 13:44:00 +0800597 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800598
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800599 p_ctx = (DVR_RecordContext_t *)handle;
600 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
601 if (p_ctx == &record_ctx[i])
602 break;
603 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800604 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800605
Pengfei Liub4734232020-01-17 18:25:10 +0800606 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800607 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STOPPED);
608 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800609 DVR_RETURN_IF_FALSE(p_info);/*should support NULL*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800610
Pengfei Liuc181a982020-01-07 19:27:13 +0800611 p_ctx->state = DVR_RECORD_STATE_STOPPED;
pengfei.liuab5a2262020-02-14 17:33:40 +0800612 if (p_ctx->is_vod) {
pengfei.liu8b563292020-02-26 15:49:02 +0800613 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
pengfei.liuab5a2262020-02-14 17:33:40 +0800614 p_ctx->segment_info.duration = 10*1000; //debug, should delete it
615 } else {
616 ret = record_device_stop(p_ctx->dev_handle);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800617 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
618 if (ret != DVR_SUCCESS)
619 goto end;
pengfei.liuab5a2262020-02-14 17:33:40 +0800620 //p_ctx->state = DVR_RECORD_STATE_STOPPED;
621 pthread_join(p_ctx->thread, NULL);
622 }
623
hualing chen2932d372020-04-29 13:44:00 +0800624 //add index file store
625 pos = segment_tell_position(p_ctx->segment_handle);
626 segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
hualing chene41f4372020-06-06 16:29:17 +0800627 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +0800628
Pengfei Liub4734232020-01-17 18:25:10 +0800629 /*Update segment info*/
630 memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
631
632 ret = segment_store_info(p_ctx->segment_handle, p_info);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800633 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
634 if (ret != DVR_SUCCESS)
635 goto end;
Pengfei Liub4734232020-01-17 18:25:10 +0800636
637 DVR_DEBUG(1, "%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d",
638 __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets);
639
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800640end:
Pengfei Liuc181a982020-01-07 19:27:13 +0800641 ret = segment_close(p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800642 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800643 return DVR_SUCCESS;
644}
Pengfei Liuc181a982020-01-07 19:27:13 +0800645
Pengfei Liub4734232020-01-17 18:25:10 +0800646int dvr_record_resume_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, uint64_t *p_resume_size)
647{
648 DVR_RecordContext_t *p_ctx;
649 uint32_t i;
pengfei.liuab5a2262020-02-14 17:33:40 +0800650 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800651
652 p_ctx = (DVR_RecordContext_t *)handle;
653 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
654 if (p_ctx == &record_ctx[i])
655 break;
Pengfei Liuc181a982020-01-07 19:27:13 +0800656 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800657 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
658 DVR_RETURN_IF_FALSE(params);
659 DVR_RETURN_IF_FALSE(p_resume_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800660
pengfei.liuab5a2262020-02-14 17:33:40 +0800661 DVR_DEBUG(1, "%s , current state:%d, resume size:%lld", __func__, p_ctx->state, *p_resume_size);
662 ret = dvr_record_start_segment(handle, params);
663 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
664
665 p_ctx->segment_info.size = *p_resume_size;
666
667 return DVR_SUCCESS;
668}
669
670int dvr_record_get_status(DVR_RecordHandle_t handle, DVR_RecordStatus_t *p_status)
671{
672 DVR_RecordContext_t *p_ctx;
673 int i;
674
675 p_ctx = (DVR_RecordContext_t *)handle;
676 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
677 if (p_ctx == &record_ctx[i])
678 break;
679 }
680 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
681 DVR_RETURN_IF_FALSE(p_status);
682
683 //lock
684 p_status->state = p_ctx->state;
685 p_status->info.id = p_ctx->segment_info.id;
686 p_status->info.duration = p_ctx->segment_info.duration;
687 p_status->info.size = p_ctx->segment_info.size;
688 p_status->info.nb_packets = p_ctx->segment_info.size/188;
689
690 return DVR_SUCCESS;
691}
692
693int dvr_record_write(DVR_RecordHandle_t handle, void *buffer, uint32_t len)
694{
695 DVR_RecordContext_t *p_ctx;
696 uint32_t i;
697 off_t pos = 0;
698 int ret;
699 int has_pcr;
700
701 p_ctx = (DVR_RecordContext_t *)handle;
702 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
703 if (p_ctx == &record_ctx[i])
704 break;
705 }
706 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
707 DVR_RETURN_IF_FALSE(buffer);
708 DVR_RETURN_IF_FALSE(len);
709
710 pos = segment_tell_position(p_ctx->segment_handle);
711 has_pcr = record_do_pcr_index(p_ctx, buffer, len);
712 if (has_pcr == 0) {
713 /* Pull VOD record shoud use PCR time index */
714 DVR_DEBUG(1, "%s has no pcr, can NOT do time index", __func__);
715 }
716 ret = segment_write(p_ctx->segment_handle, buffer, len);
hualing chen2932d372020-04-29 13:44:00 +0800717 if (ret != len) {
718 DVR_DEBUG(1, "%s write error ret:%d len:%d", __func__, ret, len);
719 }
pengfei.liuab5a2262020-02-14 17:33:40 +0800720 p_ctx->segment_info.size += len;
721 p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188;
722
Pengfei Liuc181a982020-01-07 19:27:13 +0800723 return DVR_SUCCESS;
724}
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800725
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800726int dvr_record_set_encrypt_callback(DVR_RecordHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800727{
728 DVR_RecordContext_t *p_ctx;
729 uint32_t i;
730
731 p_ctx = (DVR_RecordContext_t *)handle;
732 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
733 if (p_ctx == &record_ctx[i])
734 break;
735 }
736 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
737 DVR_RETURN_IF_FALSE(func);
738
739 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
740 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
741 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
742
743 p_ctx->enc_func = func;
744 p_ctx->enc_userdata = userdata;
745 return DVR_SUCCESS;
746}
747
748int dvr_record_set_secure_buffer(DVR_RecordHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
749{
750 DVR_RecordContext_t *p_ctx;
751 uint32_t i;
752 int ret;
753
754 p_ctx = (DVR_RecordContext_t *)handle;
755 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
756 if (p_ctx == &record_ctx[i])
757 break;
758 }
759 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
760 DVR_RETURN_IF_FALSE(p_secure_buf);
761 DVR_RETURN_IF_FALSE(len);
762
763 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
764 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
765 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
766
767 ret = record_device_set_secure_buffer(p_ctx->dev_handle, p_secure_buf, len);
768 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
769
770 p_ctx->is_secure_mode = 1;
771 return ret;
772}