blob: 62cc6addf7305eecdd69cdee61e9ee60772ab087 [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 Liuc181a982020-01-07 19:27:13 +080011
12#define MAX_DVR_RECORD_SESSION_COUNT 2
hualing chen266b9502020-04-04 17:39:39 +080013//#define RECORD_BLOCK_SIZE (64 * 1024)
14#define RECORD_BLOCK_SIZE (256 * 1024)
pengfei.liuab5a2262020-02-14 17:33:40 +080015
16/**\brief DVR index file type*/
17typedef enum {
18 DVR_INDEX_TYPE_PCR, /**< DVR index file use pcr*/
19 DVR_INDEX_TYPE_LOCAL_CLOCK, /**< DVR index file use local clock*/
20 DVR_INDEX_TYPE_INVALID /**< DVR index file type invalid type*/
21} DVR_IndexType_t;
22
23/**\brief DVR VOD context*/
24typedef struct {
25 pthread_mutex_t mutex; /**< VOD mutex lock*/
26 pthread_cond_t cond; /**< VOD condition*/
27 void *buffer; /**< VOD buffer*/
28 uint32_t buf_len; /**< VOD buffer len*/
29} DVR_VodContext_t;
30
pengfei.liu07ddc8a2020-03-24 23:36:53 +080031/**\brief DVR record secure mode buffer*/
32typedef struct {
33 uint32_t addr; /**< Secure mode record buffer address*/
34 uint32_t len; /**< Secure mode record buffer length*/
35} DVR_SecureBuffer_t;
36
Pengfei Liub4734232020-01-17 18:25:10 +080037/**\brief DVR record context*/
Pengfei Liuc181a982020-01-07 19:27:13 +080038typedef struct {
Pengfei Liub4734232020-01-17 18:25:10 +080039 pthread_t thread; /**< DVR thread handle*/
40 Record_DeviceHandle_t dev_handle; /**< DVR device handle*/
41 Segment_Handle_t segment_handle; /**< DVR segment handle*/
42 DVR_RecordState_t state; /**< DVR record state*/
43 char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/
44 DVR_RecordSegmentStartParams_t segment_params; /**< DVR record start parameters*/
45 DVR_RecordSegmentInfo_t segment_info; /**< DVR record current segment info*/
46 size_t notification_size; /**< DVR record nogification size*/
47 DVR_RecordEventFunction_t event_notify_fn; /**< DVR record event notify function*/
48 void *event_userdata; /**< DVR record event userdata*/
pengfei.liuab5a2262020-02-14 17:33:40 +080049 //DVR_VodContext_t vod; /**< DVR record vod context*/
50 int is_vod; /**< Indicate current mode is VOD record mode*/
pengfei.liu27cc4ec2020-04-03 16:28:16 +080051 DVR_CryptoFunction_t enc_func; /**< Encrypt function*/
pengfei.liu07ddc8a2020-03-24 23:36:53 +080052 void *enc_userdata; /**< Encrypt userdata*/
53 int is_secure_mode; /**< Record session run in secure pipeline */
hualing chen2615aa82020-04-02 21:32:51 +080054 size_t last_send_size; /**< Last send notify segment size */
Pengfei Liuc181a982020-01-07 19:27:13 +080055} DVR_RecordContext_t;
56
57static DVR_RecordContext_t record_ctx[MAX_DVR_RECORD_SESSION_COUNT] = {
58 {
Pengfei Liub4734232020-01-17 18:25:10 +080059 .state = DVR_RECORD_STATE_CLOSED
Pengfei Liuc181a982020-01-07 19:27:13 +080060 },
61 {
Pengfei Liub4734232020-01-17 18:25:10 +080062 .state = DVR_RECORD_STATE_CLOSED
Pengfei Liuc181a982020-01-07 19:27:13 +080063 }
64};
65
Zhiqiang Han51646a02020-04-05 18:43:22 +080066static int record_is_valid_pid(DVR_RecordContext_t *p_ctx, int pid)
67{
68 int i;
69
70 for (i = 0; i < p_ctx->segment_info.nb_pids; i++) {
71 if (pid == p_ctx->segment_info.pids[i].pid)
72 return 1;
73 }
74 return 0;
75}
76
pengfei.liuab5a2262020-02-14 17:33:40 +080077static int record_save_pcr(DVR_RecordContext_t *p_ctx, uint8_t *buf, loff_t pos)
78{
79 uint8_t *p = buf;
80 int len;
81 uint8_t afc;
82 uint64_t pcr = 0;
83 int has_pcr = 0;
84 int pid;
85 int adp_field_len;
86
87 pid = ((p[1] & 0x1f) << 8) | p[2];
Zhiqiang Han51646a02020-04-05 18:43:22 +080088 if (pid == 0x1fff || !record_is_valid_pid(p_ctx, pid))
pengfei.liuab5a2262020-02-14 17:33:40 +080089 return has_pcr;
90
91 //scramble = p[3] >> 6;
92 //cc = p[3] & 0x0f;
93 afc = (p[3] >> 4) & 0x03;
94
95 p += 4;
96 len = 184;
97
98 if (afc & 2) {
99 adp_field_len = p[0];
100 /* Skip adaptation len */
101 p++;
102 len--;
103 /* Parse pcr field, see 13818 spec table I-2-6,adaptation_field */
104 if (p[0] & 0x10 && len >= 6) {
105 /* get pcr value,pcr is 33bit value */
106 pcr = (((uint64_t)(p[1])) << 25)
107 | (((uint64_t)p[2]) << 17)
108 | (((uint64_t)(p[3])) << 9)
109 | (((uint64_t)p[4]) << 1)
110 | ((((uint64_t)p[5]) & 0x80) >> 7);
111 has_pcr = 1;
112 }
113
114 p += adp_field_len;
115 len -= adp_field_len;
116
117 if (len < 0) {
118 DVR_DEBUG(1, "parser pcr: illegal adaptation field length");
119 return 0;
120 }
121 }
122
123 if (has_pcr) {
124 segment_update_pts(p_ctx->segment_handle, pcr/90, pos);
125 }
126 return has_pcr;
127}
128
129static int record_do_pcr_index(DVR_RecordContext_t *p_ctx, uint8_t *buf, int len)
130{
131 uint8_t *p = buf;
132 int left = len;
133 loff_t pos;
134 int has_pcr = 0;
135
136 pos = segment_tell_position(p_ctx->segment_handle);
137 while (left >= 188) {
138 if (*p == 0x47) {
139 has_pcr |= record_save_pcr(p_ctx, p, pos);
140 p += 188;
141 left -= 188;
142 pos += 188;
143 } else {
144 p++;
145 left --;
146 pos++;
147 }
148 }
149 return has_pcr;
150}
151
Pengfei Liuc181a982020-01-07 19:27:13 +0800152void *record_thread(void *arg)
153{
154 DVR_RecordContext_t *p_ctx = (DVR_RecordContext_t *)arg;
155 ssize_t len;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800156 uint8_t *buf, *buf_out;
157 int block_size = RECORD_BLOCK_SIZE;//256*1024;
pengfei.liuab5a2262020-02-14 17:33:40 +0800158 loff_t pos = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800159 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800160 struct timespec start_ts, end_ts;
161 DVR_RecordStatus_t record_status;
pengfei.liuab5a2262020-02-14 17:33:40 +0800162 int has_pcr;
163 int index_type = DVR_INDEX_TYPE_INVALID;
hualing chen87072a82020-03-12 16:20:12 +0800164 time_t pre_time = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800165 uint32_t out_len;
hualing chen87072a82020-03-12 16:20:12 +0800166 #define DVR_STORE_INFO_TIME (1000)
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800167 DVR_SecureBuffer_t secure_buf;
Pengfei Liuc181a982020-01-07 19:27:13 +0800168
Pengfei Liub4734232020-01-17 18:25:10 +0800169 buf = (uint8_t *)malloc(block_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800170 if (!buf) {
171 DVR_DEBUG(1, "%s, malloc failed", __func__);
172 return NULL;
173 }
174
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800175 buf_out = (uint8_t *)malloc(block_size + 188);
176 if (!buf_out) {
177 DVR_DEBUG(1, "%s, malloc failed", __func__);
178 return NULL;
179 }
180
hualing chen266b9502020-04-04 17:39:39 +0800181 memset(&record_status, 0, sizeof(record_status));
182 record_status.state = DVR_RECORD_STATE_STARTED;
183 p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata);
184 DVR_DEBUG(1, "%s notify record status, state:%d",
185 __func__, record_status.state);
186 DVR_DEBUG(1, "%s, secure_mode:%d", __func__, p_ctx->is_secure_mode);
187
188
Pengfei Liub4734232020-01-17 18:25:10 +0800189 clock_gettime(CLOCK_MONOTONIC, &start_ts);
Pengfei Liuc181a982020-01-07 19:27:13 +0800190 while (p_ctx->state == DVR_RECORD_STATE_STARTED) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800191 /* data from dmx, normal dvr case */
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800192 if (p_ctx->is_secure_mode) {
193 memset(&secure_buf, 0, sizeof(secure_buf));
194 len = record_device_read(p_ctx->dev_handle, &secure_buf, sizeof(secure_buf), 1000);
hualing chen266b9502020-04-04 17:39:39 +0800195 if (len != DVR_FAILURE) {
196 DVR_DEBUG(1, "%s, secure_buf:%#x, size:%#x", __func__, secure_buf.addr, secure_buf.len);
197 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800198 } else {
199 len = record_device_read(p_ctx->dev_handle, buf, block_size, 1000);
200 }
Pengfei Liub4734232020-01-17 18:25:10 +0800201 if (len == DVR_FAILURE) {
202 usleep(10*1000);
203 continue;
204 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800205
206 /* Got data from device, record it */
207 if (p_ctx->enc_func) {
208 /* Encrypt record data */
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800209#if 0
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800210 uint8_t *input_buf;
211 uint32_t input_len;
212 /* Out buffer length may not equal in buffer length */
213 if (p_ctx->is_secure_mode) {
214 input_buf = (uint8_t *)secure_buf.addr;
215 input_len = secure_buf.len;
216 } else {
217 input_buf = buf;
218 input_len = len;
219 }
220 out_len = block_size + 188;
221 p_ctx->enc_func(input_buf, input_len, buf_out, &out_len, p_ctx->enc_userdata);
222 ret = segment_write(p_ctx->segment_handle, buf_out, out_len);
223 len = out_len;
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800224#else
225 DVR_CryptoParams_t crypto_params;
226
227 memset(&crypto_params, 0, sizeof(crypto_params));
228 crypto_params.type = DVR_CRYPTO_TYPE_ENCRYPT;
229 memcpy(crypto_params.location, p_ctx->location, strlen(p_ctx->location));
230 crypto_params.segment_id = p_ctx->segment_info.id;
231 crypto_params.offset = p_ctx->segment_info.size;
232
233 if (p_ctx->is_secure_mode) {
234 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_SECURE;
235 crypto_params.input_buffer.addr = secure_buf.addr;
236 crypto_params.input_buffer.size = secure_buf.len;
237 } else {
238 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
239 crypto_params.input_buffer.addr = (size_t)buf;
240 crypto_params.input_buffer.size = len;
241 }
242
243 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL;
244 crypto_params.output_buffer.addr = (size_t)buf_out;
245 crypto_params.output_buffer.size = block_size + 188;
246
247 p_ctx->enc_func(&crypto_params, p_ctx->enc_userdata);
248 /* 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;
251#endif
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800252 } else {
253 ret = segment_write(p_ctx->segment_handle, buf, len);
254 }
255
256 /* Do time index */
257 uint8_t *index_buf = p_ctx->enc_func ? buf_out : buf;
pengfei.liuab5a2262020-02-14 17:33:40 +0800258 pos = segment_tell_position(p_ctx->segment_handle);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800259 has_pcr = record_do_pcr_index(p_ctx, index_buf, len);
pengfei.liuab5a2262020-02-14 17:33:40 +0800260 if (has_pcr == 0 && index_type == DVR_INDEX_TYPE_INVALID) {
261 clock_gettime(CLOCK_MONOTONIC, &end_ts);
262 if ((end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) -
263 (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) > 40) {
264 /* PCR interval threshlod > 40 ms*/
265 DVR_DEBUG(1, "%s use local clock time index", __func__);
266 index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
267 }
268 } else if (has_pcr && index_type == DVR_INDEX_TYPE_INVALID){
269 DVR_DEBUG(1, "%s use pcr time index", __func__);
270 index_type = DVR_INDEX_TYPE_PCR;
Pengfei Liuc181a982020-01-07 19:27:13 +0800271 }
pengfei.liuab5a2262020-02-14 17:33:40 +0800272
273 /* Update segment info */
Pengfei Liub4734232020-01-17 18:25:10 +0800274 p_ctx->segment_info.size += len;
pengfei.liuab5a2262020-02-14 17:33:40 +0800275 /*Duration need use pcr to calculate, todo...*/
276 if (index_type == DVR_INDEX_TYPE_PCR) {
pengfei.liu8b563292020-02-26 15:49:02 +0800277 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
hualing chen87072a82020-03-12 16:20:12 +0800278 if (pre_time == 0)
279 pre_time = p_ctx->segment_info.duration;
pengfei.liuab5a2262020-02-14 17:33:40 +0800280 } else if (index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) {
281 clock_gettime(CLOCK_MONOTONIC, &end_ts);
282 p_ctx->segment_info.duration = (end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) -
283 (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000);
hualing chen87072a82020-03-12 16:20:12 +0800284 if (pre_time == 0)
285 pre_time = p_ctx->segment_info.duration;
pengfei.liuab5a2262020-02-14 17:33:40 +0800286 segment_update_pts(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
287 } else {
288 DVR_DEBUG(1, "%s can NOT do time index", __func__);
289 }
290 p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188;
Pengfei Liub4734232020-01-17 18:25:10 +0800291
hualing chen87072a82020-03-12 16:20:12 +0800292 if (p_ctx->segment_info.duration - pre_time > DVR_STORE_INFO_TIME) {
293 pre_time = p_ctx->segment_info.duration + DVR_STORE_INFO_TIME;
294 segment_store_info(p_ctx->segment_handle, &(p_ctx->segment_info));
295 }
296 /*Event notification*/
Pengfei Liub4734232020-01-17 18:25:10 +0800297 if (p_ctx->notification_size &&
298 p_ctx->event_notify_fn &&
hualing chen2615aa82020-04-02 21:32:51 +0800299 /*!(p_ctx->segment_info.size % p_ctx->notification_size)*/
300 (p_ctx->segment_info.size -p_ctx->last_send_size) >= p_ctx->notification_size&&
pengfei.liuab5a2262020-02-14 17:33:40 +0800301 p_ctx->segment_info.duration > 0) {
Pengfei Liub4734232020-01-17 18:25:10 +0800302 memset(&record_status, 0, sizeof(record_status));
pengfei.liuab5a2262020-02-14 17:33:40 +0800303 //clock_gettime(CLOCK_MONOTONIC, &end_ts);
hualing chen2615aa82020-04-02 21:32:51 +0800304 p_ctx->last_send_size = p_ctx->segment_info.size;
Pengfei Liub4734232020-01-17 18:25:10 +0800305 record_status.state = p_ctx->state;
306 record_status.info.id = p_ctx->segment_info.id;
pengfei.liuab5a2262020-02-14 17:33:40 +0800307 record_status.info.duration = p_ctx->segment_info.duration;
Pengfei Liub4734232020-01-17 18:25:10 +0800308 record_status.info.size = p_ctx->segment_info.size;
309 record_status.info.nb_packets = p_ctx->segment_info.size/188;
310 p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata);
311 DVR_DEBUG(1, "%s notify record status, state:%d, id:%lld, duration:%ld ms, size:%zu",
312 __func__, record_status.state, record_status.info.id, record_status.info.duration, record_status.info.size);
313 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800314 }
315
Pengfei Liub4734232020-01-17 18:25:10 +0800316 free((void *)buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800317 free((void *)buf_out);
Pengfei Liub4734232020-01-17 18:25:10 +0800318 DVR_DEBUG(1, "exit %s", __func__);
Pengfei Liuc181a982020-01-07 19:27:13 +0800319 return NULL;
320}
321
322int dvr_record_open(DVR_RecordHandle_t *p_handle, DVR_RecordOpenParams_t *params)
323{
324 DVR_RecordContext_t *p_ctx;
325 Record_DeviceOpenParams_t dev_open_params;
326 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800327 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800328
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800329 DVR_RETURN_IF_FALSE(p_handle);
330 DVR_RETURN_IF_FALSE(params);
Pengfei Liuc181a982020-01-07 19:27:13 +0800331
332 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
Pengfei Liub4734232020-01-17 18:25:10 +0800333 if (record_ctx[i].state == DVR_RECORD_STATE_CLOSED) {
Pengfei Liuc181a982020-01-07 19:27:13 +0800334 break;
335 }
336 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800337 DVR_RETURN_IF_FALSE(record_ctx[i].state == DVR_RECORD_STATE_CLOSED);
Pengfei Liuc181a982020-01-07 19:27:13 +0800338 p_ctx = &record_ctx[i];
Pengfei Liub4734232020-01-17 18:25:10 +0800339 DVR_DEBUG(1, "%s , current state:%d, dmx_id:%d, notification_size:%zu", __func__,
340 p_ctx->state, params->dmx_dev_id, params->notification_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800341
Pengfei Liub4734232020-01-17 18:25:10 +0800342 /*Process event params*/
343 p_ctx->notification_size = params->notification_size;
344 p_ctx->event_notify_fn = params->event_fn;
345 p_ctx->event_userdata = params->event_userdata;
hualing chen2615aa82020-04-02 21:32:51 +0800346 p_ctx->last_send_size = 0;
Pengfei Liub4734232020-01-17 18:25:10 +0800347 /*Process crypto params, todo*/
348#if 0
349 DVR_CryptoPeriod_t crypto_period; /**< DVR crypto period information*/
350 DVR_CryptoFunction_t crypto_fn; /**< DVR crypto callback function*/
351 void *crypto_data; /**< DVR crypto userdata*/
352#endif
Pengfei Liuc181a982020-01-07 19:27:13 +0800353
Pengfei Liub4734232020-01-17 18:25:10 +0800354 memset((void *)&dev_open_params, 0, sizeof(dev_open_params));
pengfei.liuab5a2262020-02-14 17:33:40 +0800355 if (params->data_from_memory) {
356 /* data from memory, VOD case */
357 p_ctx->is_vod = 1;
358 } else {
359 p_ctx->is_vod = 0;
360 /* data from dmx, normal dvr case */
hualing chen958fe4c2020-03-24 17:35:07 +0800361 dev_open_params.dmx_dev_id = params->dmx_dev_id;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800362 dev_open_params.buf_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE);
Pengfei Liub4734232020-01-17 18:25:10 +0800363
pengfei.liuab5a2262020-02-14 17:33:40 +0800364 ret = record_device_open(&p_ctx->dev_handle, &dev_open_params);
365 if (ret != DVR_SUCCESS) {
366 DVR_DEBUG(1, "%s, open record devices failed", __func__);
367 return DVR_FAILURE;
368 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800369 }
370
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800371 p_ctx->enc_func = NULL;
372 p_ctx->enc_userdata = NULL;
373 p_ctx->is_secure_mode = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800374 p_ctx->state = DVR_RECORD_STATE_OPENED;
375
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800376 *p_handle = p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800377 return DVR_SUCCESS;
378}
379
380int dvr_record_close(DVR_RecordHandle_t handle)
381{
382 DVR_RecordContext_t *p_ctx;
383 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800384 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800385
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800386 p_ctx = (DVR_RecordContext_t *)handle;
387 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
388 if (p_ctx == &record_ctx[i])
389 break;
390 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800391 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800392
Pengfei Liub4734232020-01-17 18:25:10 +0800393 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800394 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
Pengfei Liuc181a982020-01-07 19:27:13 +0800395
pengfei.liuab5a2262020-02-14 17:33:40 +0800396 if (p_ctx->is_vod) {
397 ret = DVR_SUCCESS;
398 } else {
399 ret = record_device_close(p_ctx->dev_handle);
400 if (ret != DVR_SUCCESS) {
401 DVR_DEBUG(1, "%s, failed", __func__);
402 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800403 }
404
Pengfei Liub4734232020-01-17 18:25:10 +0800405 p_ctx->state = DVR_RECORD_STATE_CLOSED;
Pengfei Liuc181a982020-01-07 19:27:13 +0800406 return ret;
407}
408
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800409#if 0
Pengfei Liuc181a982020-01-07 19:27:13 +0800410int dvr_record_register_encryption(DVR_RecordHandle_t handle,
411 DVR_CryptoFunction_t cb,
412 DVR_CryptoParams_t params,
413 void *userdata)
414{
415 return DVR_SUCCESS;
416}
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800417#endif
Pengfei Liuc181a982020-01-07 19:27:13 +0800418
419int dvr_record_start_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params)
420{
421 DVR_RecordContext_t *p_ctx;
422 Segment_OpenParams_t open_params;
423 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800424 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800425
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800426 p_ctx = (DVR_RecordContext_t *)handle;
427 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
428 if (p_ctx == &record_ctx[i])
429 break;
430 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800431 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800432
Pengfei Liub4734232020-01-17 18:25:10 +0800433 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800434 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
435 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
436 DVR_RETURN_IF_FALSE(params);
Pengfei Liuc181a982020-01-07 19:27:13 +0800437
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800438 DVR_RETURN_IF_FALSE(strlen((const char *)params->location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liuc181a982020-01-07 19:27:13 +0800439 memset(&open_params, 0, sizeof(open_params));
440 memcpy(open_params.location, params->location, strlen(params->location));
441 open_params.segment_id = params->segment.segment_id;
442 open_params.mode = SEGMENT_MODE_WRITE;
443
444 ret = segment_open(&open_params, &p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800445 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800446
Pengfei Liub4734232020-01-17 18:25:10 +0800447 /*process params*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800448 {
449 memcpy(p_ctx->location, params->location, strlen(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800450 //need all params??
Pengfei Liuc181a982020-01-07 19:27:13 +0800451 memcpy(&p_ctx->segment_params, &params->segment, sizeof(params->segment));
Pengfei Liub4734232020-01-17 18:25:10 +0800452 /*save current segment info*/
453 memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info));
454 p_ctx->segment_info.id = params->segment.segment_id;
455 p_ctx->segment_info.nb_pids = params->segment.nb_pids;
456 memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800457 }
458
pengfei.liuab5a2262020-02-14 17:33:40 +0800459 if (!p_ctx->is_vod) {
460 /* normal dvr case */
461 for (i = 0; i < params->segment.nb_pids; i++) {
462 ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
463 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
464 }
465 ret = record_device_start(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800466 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800467 }
468
Pengfei Liuc181a982020-01-07 19:27:13 +0800469 p_ctx->state = DVR_RECORD_STATE_STARTED;
pengfei.liuab5a2262020-02-14 17:33:40 +0800470 if (!p_ctx->is_vod)
471 pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +0800472
Pengfei Liuc181a982020-01-07 19:27:13 +0800473 return DVR_SUCCESS;
474}
475
476int dvr_record_next_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, DVR_RecordSegmentInfo_t *p_info)
477{
478 DVR_RecordContext_t *p_ctx;
Pengfei Liub4734232020-01-17 18:25:10 +0800479 Segment_OpenParams_t open_params;
Pengfei Liuc181a982020-01-07 19:27:13 +0800480 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800481 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800482
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800483 p_ctx = (DVR_RecordContext_t *)handle;
484 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
485 if (p_ctx == &record_ctx[i])
486 break;
487 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800488 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800489
Pengfei Liub4734232020-01-17 18:25:10 +0800490 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800491 DVR_RETURN_IF_FALSE(p_ctx->state == DVR_RECORD_STATE_STARTED);
492 //DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
493 DVR_RETURN_IF_FALSE(params);
494 DVR_RETURN_IF_FALSE(p_info);
pengfei.liuab5a2262020-02-14 17:33:40 +0800495 DVR_RETURN_IF_FALSE(!p_ctx->is_vod);
Pengfei Liuc181a982020-01-07 19:27:13 +0800496
497 /*Stop the on going record segment*/
Pengfei Liub4734232020-01-17 18:25:10 +0800498 //ret = record_device_stop(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800499 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800500
501 p_ctx->state = DVR_RECORD_STATE_STOPPED;
502 pthread_join(p_ctx->thread, NULL);
503
Pengfei Liub4734232020-01-17 18:25:10 +0800504 /*Update segment info*/
505 memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
506
507 ret = segment_store_info(p_ctx->segment_handle, p_info);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800508 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800509
hualing chena540a7e2020-03-27 16:44:05 +0800510 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",
511 __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 +0800512
Pengfei Liub4734232020-01-17 18:25:10 +0800513 /*Close current segment*/
514 ret = segment_close(p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800515 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800516 /*Open the new record segment*/
517 memset(&open_params, 0, sizeof(open_params));
518 memcpy(open_params.location, p_ctx->location, strlen(p_ctx->location));
519 open_params.segment_id = params->segment.segment_id;
520 open_params.mode = SEGMENT_MODE_WRITE;
521
522 ret = segment_open(&open_params, &p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800523 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800524 /*process params*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800525 {
Pengfei Liub4734232020-01-17 18:25:10 +0800526 //need all params??
Pengfei Liuc181a982020-01-07 19:27:13 +0800527 memcpy(&p_ctx->segment_params, &params->segment, sizeof(params->segment));
Pengfei Liub4734232020-01-17 18:25:10 +0800528 /*save current segment info*/
529 memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info));
530 p_ctx->segment_info.id = params->segment.segment_id;
531 memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800532 }
533
Pengfei Liub4734232020-01-17 18:25:10 +0800534 p_ctx->segment_info.nb_pids = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800535 for (i = 0; i < params->segment.nb_pids; i++) {
Pengfei Liub4734232020-01-17 18:25:10 +0800536 switch (params->segment.pid_action[i]) {
537 case DVR_RECORD_PID_CREATE:
538 DVR_DEBUG(1, "%s create pid:%d", __func__, params->segment.pids[i].pid);
539 ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
540 p_ctx->segment_info.nb_pids++;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800541 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800542 break;
543 case DVR_RECORD_PID_KEEP:
544 DVR_DEBUG(1, "%s keep pid:%d", __func__, params->segment.pids[i].pid);
545 p_ctx->segment_info.nb_pids++;
546 break;
547 case DVR_RECORD_PID_CLOSE:
548 DVR_DEBUG(1, "%s close pid:%d", __func__, params->segment.pids[i].pid);
549 ret = record_device_remove_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800550 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800551 break;
552 default:
553 DVR_DEBUG(1, "%s wrong action pid:%d", __func__, params->segment.pids[i].pid);
554 return DVR_FAILURE;
555 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800556 }
557
Pengfei Liub4734232020-01-17 18:25:10 +0800558 //ret = record_device_start(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800559 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800560
561 pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx);
562 p_ctx->state = DVR_RECORD_STATE_STARTED;
563 return DVR_SUCCESS;
564}
565
566int dvr_record_stop_segment(DVR_RecordHandle_t handle, DVR_RecordSegmentInfo_t *p_info)
567{
568 DVR_RecordContext_t *p_ctx;
569 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800570 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800571
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800572 p_ctx = (DVR_RecordContext_t *)handle;
573 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
574 if (p_ctx == &record_ctx[i])
575 break;
576 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800577 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800578
Pengfei Liub4734232020-01-17 18:25:10 +0800579 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800580 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STOPPED);
581 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800582 DVR_RETURN_IF_FALSE(p_info);/*should support NULL*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800583
Pengfei Liuc181a982020-01-07 19:27:13 +0800584 p_ctx->state = DVR_RECORD_STATE_STOPPED;
pengfei.liuab5a2262020-02-14 17:33:40 +0800585 if (p_ctx->is_vod) {
pengfei.liu8b563292020-02-26 15:49:02 +0800586 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
pengfei.liuab5a2262020-02-14 17:33:40 +0800587 p_ctx->segment_info.duration = 10*1000; //debug, should delete it
588 } else {
589 ret = record_device_stop(p_ctx->dev_handle);
590 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
591 //p_ctx->state = DVR_RECORD_STATE_STOPPED;
592 pthread_join(p_ctx->thread, NULL);
593 }
594
Pengfei Liuc181a982020-01-07 19:27:13 +0800595
Pengfei Liub4734232020-01-17 18:25:10 +0800596 /*Update segment info*/
597 memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
598
599 ret = segment_store_info(p_ctx->segment_handle, p_info);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800600 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800601
602 DVR_DEBUG(1, "%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d",
603 __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets);
604
Pengfei Liuc181a982020-01-07 19:27:13 +0800605 ret = segment_close(p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800606 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800607 return DVR_SUCCESS;
608}
Pengfei Liuc181a982020-01-07 19:27:13 +0800609
Pengfei Liub4734232020-01-17 18:25:10 +0800610int dvr_record_resume_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, uint64_t *p_resume_size)
611{
612 DVR_RecordContext_t *p_ctx;
613 uint32_t i;
pengfei.liuab5a2262020-02-14 17:33:40 +0800614 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800615
616 p_ctx = (DVR_RecordContext_t *)handle;
617 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
618 if (p_ctx == &record_ctx[i])
619 break;
Pengfei Liuc181a982020-01-07 19:27:13 +0800620 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800621 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
622 DVR_RETURN_IF_FALSE(params);
623 DVR_RETURN_IF_FALSE(p_resume_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800624
pengfei.liuab5a2262020-02-14 17:33:40 +0800625 DVR_DEBUG(1, "%s , current state:%d, resume size:%lld", __func__, p_ctx->state, *p_resume_size);
626 ret = dvr_record_start_segment(handle, params);
627 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
628
629 p_ctx->segment_info.size = *p_resume_size;
630
631 return DVR_SUCCESS;
632}
633
634int dvr_record_get_status(DVR_RecordHandle_t handle, DVR_RecordStatus_t *p_status)
635{
636 DVR_RecordContext_t *p_ctx;
637 int i;
638
639 p_ctx = (DVR_RecordContext_t *)handle;
640 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
641 if (p_ctx == &record_ctx[i])
642 break;
643 }
644 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
645 DVR_RETURN_IF_FALSE(p_status);
646
647 //lock
648 p_status->state = p_ctx->state;
649 p_status->info.id = p_ctx->segment_info.id;
650 p_status->info.duration = p_ctx->segment_info.duration;
651 p_status->info.size = p_ctx->segment_info.size;
652 p_status->info.nb_packets = p_ctx->segment_info.size/188;
653
654 return DVR_SUCCESS;
655}
656
657int dvr_record_write(DVR_RecordHandle_t handle, void *buffer, uint32_t len)
658{
659 DVR_RecordContext_t *p_ctx;
660 uint32_t i;
661 off_t pos = 0;
662 int ret;
663 int has_pcr;
664
665 p_ctx = (DVR_RecordContext_t *)handle;
666 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
667 if (p_ctx == &record_ctx[i])
668 break;
669 }
670 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
671 DVR_RETURN_IF_FALSE(buffer);
672 DVR_RETURN_IF_FALSE(len);
673
674 pos = segment_tell_position(p_ctx->segment_handle);
675 has_pcr = record_do_pcr_index(p_ctx, buffer, len);
676 if (has_pcr == 0) {
677 /* Pull VOD record shoud use PCR time index */
678 DVR_DEBUG(1, "%s has no pcr, can NOT do time index", __func__);
679 }
680 ret = segment_write(p_ctx->segment_handle, buffer, len);
681 p_ctx->segment_info.size += len;
682 p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188;
683
Pengfei Liuc181a982020-01-07 19:27:13 +0800684 return DVR_SUCCESS;
685}
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800686
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800687int dvr_record_set_encrypt_callback(DVR_RecordHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800688{
689 DVR_RecordContext_t *p_ctx;
690 uint32_t i;
691
692 p_ctx = (DVR_RecordContext_t *)handle;
693 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
694 if (p_ctx == &record_ctx[i])
695 break;
696 }
697 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
698 DVR_RETURN_IF_FALSE(func);
699
700 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
701 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
702 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
703
704 p_ctx->enc_func = func;
705 p_ctx->enc_userdata = userdata;
706 return DVR_SUCCESS;
707}
708
709int dvr_record_set_secure_buffer(DVR_RecordHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
710{
711 DVR_RecordContext_t *p_ctx;
712 uint32_t i;
713 int ret;
714
715 p_ctx = (DVR_RecordContext_t *)handle;
716 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
717 if (p_ctx == &record_ctx[i])
718 break;
719 }
720 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
721 DVR_RETURN_IF_FALSE(p_secure_buf);
722 DVR_RETURN_IF_FALSE(len);
723
724 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
725 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
726 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
727
728 ret = record_device_set_secure_buffer(p_ctx->dev_handle, p_secure_buf, len);
729 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
730
731 p_ctx->is_secure_mode = 1;
732 return ret;
733}