blob: 182213f60cde830899d0b525a168b88069c0ab99 [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"
Gong Ke2a0ebbe2021-05-25 15:22:50 +08009#include "dvb_utils.h"
Pengfei Liuc181a982020-01-07 19:27:13 +080010#include "record_device.h"
11#include "segment.h"
pengfei.liu567d6d82020-04-17 16:48:59 +080012#include <sys/time.h>
Yahui Han1fbf3292021-11-08 18:17:19 +080013#include "am_crypt.h"
Pengfei Liuc181a982020-01-07 19:27:13 +080014
hualing chend241c7a2021-06-22 13:34:27 +080015#define CONTROL_SPEED_ENABLE 0
16
hualing chen3092e1c2022-01-21 20:02:21 +080017#define CHECK_PTS_MAX_COUNT (20)
18
hualing chenc7aa4c82021-02-03 15:41:37 +080019//#define DEBUG_PERFORMANCE
Yahui Han10993892021-11-02 14:27:33 +080020#define MAX_DVR_RECORD_SESSION_COUNT 4
hualing chen266b9502020-04-04 17:39:39 +080021#define RECORD_BLOCK_SIZE (256 * 1024)
Yahui Hance15e9c2020-12-08 18:08:32 +080022#define NEW_DEVICE_RECORD_BLOCK_SIZE (1024 * 188)
pengfei.liuab5a2262020-02-14 17:33:40 +080023
24/**\brief DVR index file type*/
25typedef enum {
26 DVR_INDEX_TYPE_PCR, /**< DVR index file use pcr*/
27 DVR_INDEX_TYPE_LOCAL_CLOCK, /**< DVR index file use local clock*/
28 DVR_INDEX_TYPE_INVALID /**< DVR index file type invalid type*/
29} DVR_IndexType_t;
30
31/**\brief DVR VOD context*/
32typedef struct {
33 pthread_mutex_t mutex; /**< VOD mutex lock*/
34 pthread_cond_t cond; /**< VOD condition*/
35 void *buffer; /**< VOD buffer*/
36 uint32_t buf_len; /**< VOD buffer len*/
37} DVR_VodContext_t;
38
pengfei.liu07ddc8a2020-03-24 23:36:53 +080039/**\brief DVR record secure mode buffer*/
40typedef struct {
41 uint32_t addr; /**< Secure mode record buffer address*/
42 uint32_t len; /**< Secure mode record buffer length*/
43} DVR_SecureBuffer_t;
44
hualing chenf9867402020-09-23 17:06:20 +080045/**\brief DVR record new dmx secure mode buffer*/
46typedef struct {
47 uint32_t buf_start; /**< Secure mode record buffer address*/
48 uint32_t buf_end; /**< Secure mode record buffer length*/
49 uint32_t data_start; /**< Secure mode record buffer address*/
50 uint32_t data_end; /**< Secure mode record buffer length*/
51} DVR_NewDmxSecureBuffer_t;
52
Pengfei Liub4734232020-01-17 18:25:10 +080053/**\brief DVR record context*/
Pengfei Liuc181a982020-01-07 19:27:13 +080054typedef struct {
Pengfei Liub4734232020-01-17 18:25:10 +080055 pthread_t thread; /**< DVR thread handle*/
56 Record_DeviceHandle_t dev_handle; /**< DVR device handle*/
57 Segment_Handle_t segment_handle; /**< DVR segment handle*/
58 DVR_RecordState_t state; /**< DVR record state*/
59 char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/
60 DVR_RecordSegmentStartParams_t segment_params; /**< DVR record start parameters*/
61 DVR_RecordSegmentInfo_t segment_info; /**< DVR record current segment info*/
62 size_t notification_size; /**< DVR record nogification size*/
63 DVR_RecordEventFunction_t event_notify_fn; /**< DVR record event notify function*/
64 void *event_userdata; /**< DVR record event userdata*/
pengfei.liuab5a2262020-02-14 17:33:40 +080065 //DVR_VodContext_t vod; /**< DVR record vod context*/
66 int is_vod; /**< Indicate current mode is VOD record mode*/
pengfei.liu27cc4ec2020-04-03 16:28:16 +080067 DVR_CryptoFunction_t enc_func; /**< Encrypt function*/
pengfei.liu07ddc8a2020-03-24 23:36:53 +080068 void *enc_userdata; /**< Encrypt userdata*/
69 int is_secure_mode; /**< Record session run in secure pipeline */
Yahui Han1fbf3292021-11-08 18:17:19 +080070 void *cryptor; /**< Cryptor for encrypted PVR on FTA.*/
hualing chen2615aa82020-04-02 21:32:51 +080071 size_t last_send_size; /**< Last send notify segment size */
pengfei.liufda2a972020-04-09 14:47:15 +080072 uint32_t block_size; /**< DVR record block size */
hualing chenf9867402020-09-23 17:06:20 +080073 DVR_Bool_t is_new_dmx; /**< DVR is used new dmx driver */
hualing chen40accc32021-04-21 15:58:09 +080074 int index_type; /**< DVR is used pcr or local time */
wentao.ma35a69d42022-03-10 18:08:40 +080075 DVR_Bool_t force_sysclock; /**< If ture, force to use system clock as PVR index time source. If false, libdvr can determine index time source based on actual situation*/
hualing chen94d8dd22021-12-29 15:13:43 +080076 uint64_t pts; /**< The newest pcr or local time */
hualing chen3092e1c2022-01-21 20:02:21 +080077 int check_pts_count; /**< The check count of pts */
hualing chen002e5b92022-02-23 17:51:21 +080078 int check_no_pts_count; /**< The check count of no pts */
79 int notification_time; /**< DVR record nogification time*/
80 time_t last_send_time; /**< Last send notify segment duration */
Wentao MAeeffdb02022-06-27 16:34:35 +080081 loff_t guarded_segment_size; /**< Guarded segment size in bytes. Libdvr will be forcely stopped to write anymore if current segment reaches this size*/
Yahui Han4577db82022-07-05 17:46:19 +080082 size_t secbuf_size; /**< DVR record secure buffer length*/
Pengfei Liuc181a982020-01-07 19:27:13 +080083} DVR_RecordContext_t;
84
Gong Ke2a0ebbe2021-05-25 15:22:50 +080085extern ssize_t record_device_read_ext(Record_DeviceHandle_t handle, size_t *buf, size_t *len);
86
Pengfei Liuc181a982020-01-07 19:27:13 +080087static DVR_RecordContext_t record_ctx[MAX_DVR_RECORD_SESSION_COUNT] = {
88 {
Pengfei Liub4734232020-01-17 18:25:10 +080089 .state = DVR_RECORD_STATE_CLOSED
Pengfei Liuc181a982020-01-07 19:27:13 +080090 },
91 {
Pengfei Liub4734232020-01-17 18:25:10 +080092 .state = DVR_RECORD_STATE_CLOSED
Pengfei Liuc181a982020-01-07 19:27:13 +080093 }
94};
95
Zhiqiang Han51646a02020-04-05 18:43:22 +080096static int record_is_valid_pid(DVR_RecordContext_t *p_ctx, int pid)
97{
98 int i;
99
100 for (i = 0; i < p_ctx->segment_info.nb_pids; i++) {
101 if (pid == p_ctx->segment_info.pids[i].pid)
102 return 1;
103 }
104 return 0;
105}
106
pengfei.liuab5a2262020-02-14 17:33:40 +0800107static int record_save_pcr(DVR_RecordContext_t *p_ctx, uint8_t *buf, loff_t pos)
108{
109 uint8_t *p = buf;
110 int len;
111 uint8_t afc;
112 uint64_t pcr = 0;
113 int has_pcr = 0;
114 int pid;
115 int adp_field_len;
116
117 pid = ((p[1] & 0x1f) << 8) | p[2];
Zhiqiang Han51646a02020-04-05 18:43:22 +0800118 if (pid == 0x1fff || !record_is_valid_pid(p_ctx, pid))
pengfei.liuab5a2262020-02-14 17:33:40 +0800119 return has_pcr;
120
121 //scramble = p[3] >> 6;
122 //cc = p[3] & 0x0f;
123 afc = (p[3] >> 4) & 0x03;
124
125 p += 4;
126 len = 184;
127
128 if (afc & 2) {
129 adp_field_len = p[0];
130 /* Skip adaptation len */
131 p++;
132 len--;
133 /* Parse pcr field, see 13818 spec table I-2-6,adaptation_field */
hualing chen5605eed2020-05-26 18:18:06 +0800134 if (p[0] & 0x10 && len >= 6 && adp_field_len >= 6) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800135 /* get pcr value,pcr is 33bit value */
136 pcr = (((uint64_t)(p[1])) << 25)
137 | (((uint64_t)p[2]) << 17)
138 | (((uint64_t)(p[3])) << 9)
139 | (((uint64_t)p[4]) << 1)
140 | ((((uint64_t)p[5]) & 0x80) >> 7);
141 has_pcr = 1;
142 }
143
144 p += adp_field_len;
145 len -= adp_field_len;
146
147 if (len < 0) {
Wentao MA96f68962022-06-15 19:45:35 +0800148 DVR_INFO("parser pcr: illegal adaptation field length");
pengfei.liuab5a2262020-02-14 17:33:40 +0800149 return 0;
150 }
151 }
152
hualing chen40accc32021-04-21 15:58:09 +0800153 if (has_pcr && p_ctx->index_type == DVR_INDEX_TYPE_PCR) {
hualing chen94d8dd22021-12-29 15:13:43 +0800154 //save newest pcr
hualing chen3092e1c2022-01-21 20:02:21 +0800155 if (p_ctx->pts == pcr/90 &&
156 p_ctx->check_pts_count < CHECK_PTS_MAX_COUNT) {
hualing chen3092e1c2022-01-21 20:02:21 +0800157 p_ctx->check_pts_count ++;
158 }
hualing chen94d8dd22021-12-29 15:13:43 +0800159 p_ctx->pts = pcr/90;
pengfei.liuab5a2262020-02-14 17:33:40 +0800160 segment_update_pts(p_ctx->segment_handle, pcr/90, pos);
161 }
162 return has_pcr;
163}
164
165static int record_do_pcr_index(DVR_RecordContext_t *p_ctx, uint8_t *buf, int len)
166{
167 uint8_t *p = buf;
168 int left = len;
169 loff_t pos;
170 int has_pcr = 0;
171
172 pos = segment_tell_position(p_ctx->segment_handle);
hualing chen3e0d3a42022-01-25 17:22:01 +0800173 if (pos >= len) {
174 pos = pos - len;
175 }
pengfei.liuab5a2262020-02-14 17:33:40 +0800176 while (left >= 188) {
177 if (*p == 0x47) {
178 has_pcr |= record_save_pcr(p_ctx, p, pos);
179 p += 188;
180 left -= 188;
181 pos += 188;
182 } else {
183 p++;
184 left --;
185 pos++;
186 }
187 }
188 return has_pcr;
189}
190
pengfei.liu567d6d82020-04-17 16:48:59 +0800191static int get_diff_time(struct timeval start_tv, struct timeval end_tv)
192{
193 return end_tv.tv_sec * 1000 + end_tv.tv_usec / 1000 - start_tv.tv_sec * 1000 - start_tv.tv_usec / 1000;
194}
195
Pengfei Liuc181a982020-01-07 19:27:13 +0800196void *record_thread(void *arg)
197{
198 DVR_RecordContext_t *p_ctx = (DVR_RecordContext_t *)arg;
199 ssize_t len;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800200 uint8_t *buf, *buf_out;
pengfei.liufda2a972020-04-09 14:47:15 +0800201 uint32_t block_size = p_ctx->block_size;
pengfei.liuab5a2262020-02-14 17:33:40 +0800202 loff_t pos = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800203 int ret;
hualing chen3092e1c2022-01-21 20:02:21 +0800204 struct timespec start_ts, end_ts, start_nopcr_ts, end_nopcr_ts;
Pengfei Liub4734232020-01-17 18:25:10 +0800205 DVR_RecordStatus_t record_status;
pengfei.liuab5a2262020-02-14 17:33:40 +0800206 int has_pcr;
hualing chen3092e1c2022-01-21 20:02:21 +0800207 int pcr_rec_len = 0;
Wentao MAeeffdb02022-06-27 16:34:35 +0800208 DVR_Bool_t guarded_size_exceeded = DVR_FALSE;
hualing chen40accc32021-04-21 15:58:09 +0800209
hualing chen87072a82020-03-12 16:20:12 +0800210 time_t pre_time = 0;
hualing chen2932d372020-04-29 13:44:00 +0800211 #define DVR_STORE_INFO_TIME (400)
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800212 DVR_SecureBuffer_t secure_buf;
hualing chenf9867402020-09-23 17:06:20 +0800213 DVR_NewDmxSecureBuffer_t new_dmx_secure_buf;
hualing chena5f03222021-12-02 11:22:35 +0800214 int first_read = 0;
hualing chend241c7a2021-06-22 13:34:27 +0800215 if (CONTROL_SPEED_ENABLE == 0)
216 p_ctx->index_type = DVR_INDEX_TYPE_INVALID;
217 else
218 p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
Pengfei Liuc181a982020-01-07 19:27:13 +0800219
wentao.ma35a69d42022-03-10 18:08:40 +0800220 // Force to use LOCAL_CLOCK as index type if force_sysclock is on. Please
221 // refer to SWPL-75327
222 if (p_ctx->force_sysclock)
223 p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
Pengfei Liub4734232020-01-17 18:25:10 +0800224 buf = (uint8_t *)malloc(block_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800225 if (!buf) {
Wentao MA96f68962022-06-15 19:45:35 +0800226 DVR_INFO("%s, malloc failed", __func__);
Pengfei Liuc181a982020-01-07 19:27:13 +0800227 return NULL;
228 }
229
Yahui Han4577db82022-07-05 17:46:19 +0800230 if (p_ctx->is_secure_mode) {
231 buf_out = (uint8_t *)malloc(p_ctx->secbuf_size + 188);
232 } else {
233 buf_out = (uint8_t *)malloc(block_size + 188);
234 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800235 if (!buf_out) {
Wentao MA96f68962022-06-15 19:45:35 +0800236 DVR_INFO("%s, malloc failed", __func__);
Pengfei Liufaf38e42020-05-22 00:28:02 +0800237 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800238 return NULL;
239 }
240
hualing chen266b9502020-04-04 17:39:39 +0800241 memset(&record_status, 0, sizeof(record_status));
242 record_status.state = DVR_RECORD_STATE_STARTED;
pengfei.liufda2a972020-04-09 14:47:15 +0800243 if (p_ctx->event_notify_fn) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800244 record_status.info.id = p_ctx->segment_info.id;
pengfei.liufda2a972020-04-09 14:47:15 +0800245 p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata);
Wentao MA96f68962022-06-15 19:45:35 +0800246 DVR_INFO("%s line %d notify record status, state:%d id=%lld",
hualing chen4b7c15d2020-04-07 16:13:48 +0800247 __func__,__LINE__, record_status.state, p_ctx->segment_info.id);
pengfei.liufda2a972020-04-09 14:47:15 +0800248 }
Wentao MA96f68962022-06-15 19:45:35 +0800249 DVR_INFO("%s, --secure_mode:%d, block_size:%d, cryptor:%p",
hualing chen002e5b92022-02-23 17:51:21 +0800250 __func__, p_ctx->is_secure_mode,
251 block_size, p_ctx->cryptor);
Pengfei Liub4734232020-01-17 18:25:10 +0800252 clock_gettime(CLOCK_MONOTONIC, &start_ts);
hualing chen3092e1c2022-01-21 20:02:21 +0800253 p_ctx->check_pts_count = 0;
254 p_ctx->check_no_pts_count++;
hualing chen002e5b92022-02-23 17:51:21 +0800255 p_ctx->last_send_size = 0;
256 p_ctx->last_send_time = 0;
pengfei.liu567d6d82020-04-17 16:48:59 +0800257 struct timeval t1, t2, t3, t4, t5, t6, t7;
hualing chen03fd4942021-07-15 15:56:41 +0800258 while (p_ctx->state == DVR_RECORD_STATE_STARTED ||
259 p_ctx->state == DVR_RECORD_STATE_PAUSE) {
260
261 if (p_ctx->state == DVR_RECORD_STATE_PAUSE) {
262 //wait resume record
263 usleep(20*1000);
264 continue;
265 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800266 gettimeofday(&t1, NULL);
hualing chenc70a8df2020-05-12 19:23:11 +0800267
pengfei.liuab5a2262020-02-14 17:33:40 +0800268 /* data from dmx, normal dvr case */
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800269 if (p_ctx->is_secure_mode) {
hualing chenf9867402020-09-23 17:06:20 +0800270 if (p_ctx->is_new_dmx) {
Yahui Hance15e9c2020-12-08 18:08:32 +0800271
272 /* We resolve the below invoke for dvbcore to be under safety status */
hualing chenf9867402020-09-23 17:06:20 +0800273 memset(&new_dmx_secure_buf, 0, sizeof(new_dmx_secure_buf));
Yahui Hance15e9c2020-12-08 18:08:32 +0800274 len = record_device_read(p_ctx->dev_handle, &new_dmx_secure_buf, sizeof(new_dmx_secure_buf), 10);
hualing chen002e5b92022-02-23 17:51:21 +0800275 if (len == DVR_FAILURE) {
Wentao MA96f68962022-06-15 19:45:35 +0800276 DVR_INFO("handle[%p] ret:%d\n", p_ctx->dev_handle, ret);
hualing chen002e5b92022-02-23 17:51:21 +0800277 /*For the second recording, poll always failed which we should check
278 * dvbcore further. For now, Just ignore the fack poll fail, I think
279 * it won't influce anything. But we need adjust the poll timeout
280 * from 1000ms to 10ms.
281 */
282 //continue;
283 }
Yahui Hance15e9c2020-12-08 18:08:32 +0800284
hualing chen002e5b92022-02-23 17:51:21 +0800285 /* Read data from secure demux TA */
286 len = record_device_read_ext(p_ctx->dev_handle, &secure_buf.addr,
287 &secure_buf.len);
Yahui Hance15e9c2020-12-08 18:08:32 +0800288
hualing chenf9867402020-09-23 17:06:20 +0800289 } else {
290 memset(&secure_buf, 0, sizeof(secure_buf));
291 len = record_device_read(p_ctx->dev_handle, &secure_buf, sizeof(secure_buf), 1000);
292 }
293 if (len != DVR_FAILURE) {
Wentao MA96f68962022-06-15 19:45:35 +0800294 //DVR_INFO("%s, secure_buf:%#x, size:%#x", __func__, secure_buf.addr, secure_buf.len);
hualing chenf9867402020-09-23 17:06:20 +0800295 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800296 } else {
297 len = record_device_read(p_ctx->dev_handle, buf, block_size, 1000);
298 }
Pengfei Liub4734232020-01-17 18:25:10 +0800299 if (len == DVR_FAILURE) {
hualing chen6c126382020-04-13 15:47:51 +0800300 //usleep(10*1000);
Wentao MA96f68962022-06-15 19:45:35 +0800301 //DVR_INFO("%s, start_read error", __func__);
Pengfei Liub4734232020-01-17 18:25:10 +0800302 continue;
303 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800304 gettimeofday(&t2, NULL);
hualing chenc70a8df2020-05-12 19:23:11 +0800305
Wentao MAeeffdb02022-06-27 16:34:35 +0800306 guarded_size_exceeded = DVR_FALSE;
307 if (p_ctx->segment_info.size+len >= p_ctx->guarded_segment_size) {
308 DVR_ERROR("Segment size %u is too large.",p_ctx->segment_info.size);
309 guarded_size_exceeded = DVR_TRUE;
310 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800311 /* Got data from device, record it */
weishi.zhang0a6d5c82021-12-13 14:05:31 +0800312 //if (p_ctx->is_secure_mode && p_ctx->enc_func) {
Wentao MAeeffdb02022-06-27 16:34:35 +0800313 if (guarded_size_exceeded) {
314 len = 0;
315 ret = 0;
316 } else if (p_ctx->enc_func) {
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800317 /* Encrypt record data */
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800318 DVR_CryptoParams_t crypto_params;
319
320 memset(&crypto_params, 0, sizeof(crypto_params));
321 crypto_params.type = DVR_CRYPTO_TYPE_ENCRYPT;
hualing chen7a56cba2020-04-14 14:09:27 +0800322 memcpy(crypto_params.location, p_ctx->location, sizeof(p_ctx->location));
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800323 crypto_params.segment_id = p_ctx->segment_info.id;
324 crypto_params.offset = p_ctx->segment_info.size;
325
326 if (p_ctx->is_secure_mode) {
327 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_SECURE;
Yahui Han1fbf3292021-11-08 18:17:19 +0800328 crypto_params.input_buffer.addr = secure_buf.addr;
329 crypto_params.input_buffer.size = secure_buf.len;
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800330 } else {
331 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
332 crypto_params.input_buffer.addr = (size_t)buf;
333 crypto_params.input_buffer.size = len;
334 }
335
336 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL;
337 crypto_params.output_buffer.addr = (size_t)buf_out;
Yahui Han4577db82022-07-05 17:46:19 +0800338 crypto_params.output_buffer.size = p_ctx->secbuf_size + 188;
hualing chen9811b212020-10-29 11:21:44 +0800339
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800340 p_ctx->enc_func(&crypto_params, p_ctx->enc_userdata);
pengfei.liu567d6d82020-04-17 16:48:59 +0800341 gettimeofday(&t3, NULL);
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800342 /* Out buffer length may not equal in buffer length */
hualing chenf9867402020-09-23 17:06:20 +0800343 if (crypto_params.output_size > 0) {
344 ret = segment_write(p_ctx->segment_handle, buf_out, crypto_params.output_size);
345 len = crypto_params.output_size;
hualing chenbafc62d2020-11-02 15:44:05 +0800346 } else {
347 len = 0;
hualing chenf9867402020-09-23 17:06:20 +0800348 }
Yahui Han1fbf3292021-11-08 18:17:19 +0800349 } else if (p_ctx->cryptor) {
350 /* Encrypt with clear key */
Yahui Han63b23b42021-12-07 15:37:46 +0800351 int crypt_len = len;
352 am_crypt_des_crypt(p_ctx->cryptor, buf_out, buf, &crypt_len, 0);
353 len = crypt_len;
Yahui Han1fbf3292021-11-08 18:17:19 +0800354 gettimeofday(&t3, NULL);
355 ret = segment_write(p_ctx->segment_handle, buf_out, len);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800356 } else {
hualing chena5f03222021-12-02 11:22:35 +0800357 if (first_read == 0) {
358 first_read = 1;
Wentao MA96f68962022-06-15 19:45:35 +0800359 DVR_INFO("%s:%d,first read ts", __func__,__LINE__);
hualing chena5f03222021-12-02 11:22:35 +0800360 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800361 gettimeofday(&t3, NULL);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800362 ret = segment_write(p_ctx->segment_handle, buf, len);
363 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800364 gettimeofday(&t4, NULL);
hualing chen626204e2020-04-07 11:55:08 +0800365 //add DVR_RECORD_EVENT_WRITE_ERROR event if write error
pengfei.liufda2a972020-04-09 14:47:15 +0800366 if (ret == -1 && len > 0 && p_ctx->event_notify_fn) {
hualing chen626204e2020-04-07 11:55:08 +0800367 //send write event
hualing chen9b434f02020-06-10 15:06:54 +0800368 if (p_ctx->event_notify_fn) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800369 memset(&record_status, 0, sizeof(record_status));
Wentao MAeeffdb02022-06-27 16:34:35 +0800370 DVR_INFO("%s:%d,send event write error", __func__,__LINE__);
hualing chen9ce7d942021-06-30 13:31:01 +0800371 record_status.info.id = p_ctx->segment_info.id;
hualing chen4b7c15d2020-04-07 16:13:48 +0800372 p_ctx->event_notify_fn(DVR_RECORD_EVENT_WRITE_ERROR, &record_status, p_ctx->event_userdata);
373 }
Wentao MA96f68962022-06-15 19:45:35 +0800374 DVR_INFO("%s,write error %d", __func__,__LINE__);
hualing chen626204e2020-04-07 11:55:08 +0800375 goto end;
376 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800377 /* Do time index */
Yahui Han1fbf3292021-11-08 18:17:19 +0800378 uint8_t *index_buf = (p_ctx->enc_func || p_ctx->cryptor)? buf_out : buf;
pengfei.liuab5a2262020-02-14 17:33:40 +0800379 pos = segment_tell_position(p_ctx->segment_handle);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800380 has_pcr = record_do_pcr_index(p_ctx, index_buf, len);
hualing chen40accc32021-04-21 15:58:09 +0800381 if (has_pcr == 0 && p_ctx->index_type == DVR_INDEX_TYPE_INVALID) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800382 clock_gettime(CLOCK_MONOTONIC, &end_ts);
383 if ((end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) -
384 (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) > 40) {
385 /* PCR interval threshlod > 40 ms*/
Wentao MA96f68962022-06-15 19:45:35 +0800386 DVR_INFO("%s use local clock time index", __func__);
hualing chen40accc32021-04-21 15:58:09 +0800387 p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
pengfei.liuab5a2262020-02-14 17:33:40 +0800388 }
hualing chen40accc32021-04-21 15:58:09 +0800389 } else if (has_pcr && p_ctx->index_type == DVR_INDEX_TYPE_INVALID){
Wentao MA96f68962022-06-15 19:45:35 +0800390 DVR_INFO("%s use pcr time index", __func__);
hualing chen40accc32021-04-21 15:58:09 +0800391 p_ctx->index_type = DVR_INDEX_TYPE_PCR;
hualing chen3e0d3a42022-01-25 17:22:01 +0800392 record_do_pcr_index(p_ctx, index_buf, len);
Pengfei Liuc181a982020-01-07 19:27:13 +0800393 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800394 gettimeofday(&t5, NULL);
hualing chen3092e1c2022-01-21 20:02:21 +0800395 if (p_ctx->index_type == DVR_INDEX_TYPE_PCR) {
396 if (has_pcr == 0) {
397 if (p_ctx->check_no_pts_count < 2 * CHECK_PTS_MAX_COUNT) {
398 if (p_ctx->check_no_pts_count == 0) {
hualing chen3092e1c2022-01-21 20:02:21 +0800399 clock_gettime(CLOCK_MONOTONIC, &start_nopcr_ts);
400 clock_gettime(CLOCK_MONOTONIC, &start_ts);
401 }
hualing chen3092e1c2022-01-21 20:02:21 +0800402 p_ctx->check_no_pts_count++;
403 }
404 } else {
hualing chen3092e1c2022-01-21 20:02:21 +0800405 clock_gettime(CLOCK_MONOTONIC, &start_nopcr_ts);
406 p_ctx->check_no_pts_count = 0;
407 }
408 }
409 /* Update segment i nfo */
Pengfei Liub4734232020-01-17 18:25:10 +0800410 p_ctx->segment_info.size += len;
Wentao MAeeffdb02022-06-27 16:34:35 +0800411
pengfei.liuab5a2262020-02-14 17:33:40 +0800412 /*Duration need use pcr to calculate, todo...*/
hualing chen40accc32021-04-21 15:58:09 +0800413 if (p_ctx->index_type == DVR_INDEX_TYPE_PCR) {
pengfei.liu8b563292020-02-26 15:49:02 +0800414 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
hualing chen87072a82020-03-12 16:20:12 +0800415 if (pre_time == 0)
416 pre_time = p_ctx->segment_info.duration;
hualing chen40accc32021-04-21 15:58:09 +0800417 } else if (p_ctx->index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800418 clock_gettime(CLOCK_MONOTONIC, &end_ts);
419 p_ctx->segment_info.duration = (end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) -
hualing chen3092e1c2022-01-21 20:02:21 +0800420 (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) + pcr_rec_len;
wentao.ma35a69d42022-03-10 18:08:40 +0800421 if (pre_time == 0)
422 pre_time = p_ctx->segment_info.duration;
pengfei.liuab5a2262020-02-14 17:33:40 +0800423 segment_update_pts(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
424 } else {
Wentao MA96f68962022-06-15 19:45:35 +0800425 DVR_INFO("%s can NOT do time index", __func__);
pengfei.liuab5a2262020-02-14 17:33:40 +0800426 }
hualing chen3092e1c2022-01-21 20:02:21 +0800427 if (p_ctx->index_type == DVR_INDEX_TYPE_PCR &&
428 p_ctx->check_pts_count == CHECK_PTS_MAX_COUNT) {
Wentao MA96f68962022-06-15 19:45:35 +0800429 DVR_INFO("%s change time from pcr to local time", __func__);
hualing chen56c0a162022-01-27 17:01:50 +0800430 if (pcr_rec_len == 0)
431 pcr_rec_len = segment_tell_total_time(p_ctx->segment_handle);
hualing chen3092e1c2022-01-21 20:02:21 +0800432 p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
hualing chenc94227e2022-02-17 09:48:43 +0800433 if (pcr_rec_len == 0)
434 pcr_rec_len = segment_tell_total_time(p_ctx->segment_handle);
hualing chen3092e1c2022-01-21 20:02:21 +0800435 clock_gettime(CLOCK_MONOTONIC, &start_ts);
436 }
437
438 if (p_ctx->index_type == DVR_INDEX_TYPE_PCR ) {
439 clock_gettime(CLOCK_MONOTONIC, &end_nopcr_ts);
440 int diff = (int)(end_nopcr_ts.tv_sec*1000 + end_nopcr_ts.tv_nsec/1000000) -
441 (int)(start_nopcr_ts.tv_sec*1000 + start_nopcr_ts.tv_nsec/1000000);
hualing chen3092e1c2022-01-21 20:02:21 +0800442 if (diff > 3000) {
Wentao MA96f68962022-06-15 19:45:35 +0800443 DVR_INFO("%s no pcr change time from pcr to local time diff[%d]", __func__, diff);
hualing chen3092e1c2022-01-21 20:02:21 +0800444 if (pcr_rec_len == 0)
445 pcr_rec_len = segment_tell_total_time(p_ctx->segment_handle);
446 p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
447 }
448 }
pengfei.liuab5a2262020-02-14 17:33:40 +0800449 p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188;
Pengfei Liub4734232020-01-17 18:25:10 +0800450
hualing chen87072a82020-03-12 16:20:12 +0800451 if (p_ctx->segment_info.duration - pre_time > DVR_STORE_INFO_TIME) {
452 pre_time = p_ctx->segment_info.duration + DVR_STORE_INFO_TIME;
hualing chenc94227e2022-02-17 09:48:43 +0800453 time_t duration = p_ctx->segment_info.duration;
454 if (p_ctx->index_type == DVR_INDEX_TYPE_LOCAL_CLOCK)
455 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
hualing chenf9867402020-09-23 17:06:20 +0800456 segment_store_info(p_ctx->segment_handle, &(p_ctx->segment_info));
hualing chenc94227e2022-02-17 09:48:43 +0800457 p_ctx->segment_info.duration = duration;
hualing chen87072a82020-03-12 16:20:12 +0800458 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800459 gettimeofday(&t6, NULL);
hualing chen87072a82020-03-12 16:20:12 +0800460 /*Event notification*/
hualing chen002e5b92022-02-23 17:51:21 +0800461 if (((p_ctx->notification_size > 0 && (p_ctx->segment_info.size -p_ctx->last_send_size) >= p_ctx->notification_size) ||
hualing chen2615aa82020-04-02 21:32:51 +0800462 /*!(p_ctx->segment_info.size % p_ctx->notification_size)*/
hualing chen002e5b92022-02-23 17:51:21 +0800463 ( p_ctx->notification_time > 0 && (p_ctx->segment_info.duration -p_ctx->last_send_time) >= p_ctx->notification_time))&&
464 p_ctx->event_notify_fn &&
hualing chenbe68d642021-09-09 13:06:36 +0800465 p_ctx->segment_info.duration > 0 &&
466 p_ctx->state == DVR_RECORD_STATE_STARTED) {
Pengfei Liub4734232020-01-17 18:25:10 +0800467 memset(&record_status, 0, sizeof(record_status));
pengfei.liuab5a2262020-02-14 17:33:40 +0800468 //clock_gettime(CLOCK_MONOTONIC, &end_ts);
hualing chen2615aa82020-04-02 21:32:51 +0800469 p_ctx->last_send_size = p_ctx->segment_info.size;
hualing chen002e5b92022-02-23 17:51:21 +0800470 p_ctx->last_send_time = p_ctx->segment_info.duration;
Pengfei Liub4734232020-01-17 18:25:10 +0800471 record_status.state = p_ctx->state;
472 record_status.info.id = p_ctx->segment_info.id;
hualing chenc94227e2022-02-17 09:48:43 +0800473 if (p_ctx->index_type == DVR_INDEX_TYPE_LOCAL_CLOCK)
474 record_status.info.duration = segment_tell_total_time(p_ctx->segment_handle);
475 else
476 record_status.info.duration = p_ctx->segment_info.duration;
Pengfei Liub4734232020-01-17 18:25:10 +0800477 record_status.info.size = p_ctx->segment_info.size;
478 record_status.info.nb_packets = p_ctx->segment_info.size/188;
479 p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata);
Wentao MA96f68962022-06-15 19:45:35 +0800480 DVR_INFO("%s notify record status, state:%d, id:%lld, duration:%ld ms, size:%zu loc[%s]",
hualing chen4fe3bee2020-10-23 13:58:52 +0800481 __func__, record_status.state,
482 record_status.info.id, record_status.info.duration,
483 record_status.info.size, p_ctx->location);
Pengfei Liub4734232020-01-17 18:25:10 +0800484 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800485 gettimeofday(&t7, NULL);
486#ifdef DEBUG_PERFORMANCE
Wentao MA96f68962022-06-15 19:45:35 +0800487 DVR_INFO("record count, read:%dms, encrypt:%dms, write:%dms, index:%dms, store:%dms, notify:%dms total:%dms read len:%zd notify [%d]diff[%d]",
pengfei.liu567d6d82020-04-17 16:48:59 +0800488 get_diff_time(t1, t2), get_diff_time(t2, t3), get_diff_time(t3, t4), get_diff_time(t4, t5),
hualing chen002e5b92022-02-23 17:51:21 +0800489 get_diff_time(t5, t6), get_diff_time(t6, t7), get_diff_time(t1, t5), len,
490 p_ctx->notification_time,p_ctx->segment_info.duration -p_ctx->last_send_time);
pengfei.liu567d6d82020-04-17 16:48:59 +0800491#endif
Pengfei Liuc181a982020-01-07 19:27:13 +0800492 }
hualing chen626204e2020-04-07 11:55:08 +0800493end:
Pengfei Liub4734232020-01-17 18:25:10 +0800494 free((void *)buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800495 free((void *)buf_out);
Wentao MA96f68962022-06-15 19:45:35 +0800496 DVR_INFO("exit %s", __func__);
Pengfei Liuc181a982020-01-07 19:27:13 +0800497 return NULL;
498}
499
500int dvr_record_open(DVR_RecordHandle_t *p_handle, DVR_RecordOpenParams_t *params)
501{
502 DVR_RecordContext_t *p_ctx;
503 Record_DeviceOpenParams_t dev_open_params;
504 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800505 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800506
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800507 DVR_RETURN_IF_FALSE(p_handle);
508 DVR_RETURN_IF_FALSE(params);
Pengfei Liuc181a982020-01-07 19:27:13 +0800509
510 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
Pengfei Liub4734232020-01-17 18:25:10 +0800511 if (record_ctx[i].state == DVR_RECORD_STATE_CLOSED) {
Pengfei Liuc181a982020-01-07 19:27:13 +0800512 break;
513 }
514 }
Pengfei Liufaf38e42020-05-22 00:28:02 +0800515 DVR_RETURN_IF_FALSE(i < MAX_DVR_RECORD_SESSION_COUNT);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800516 DVR_RETURN_IF_FALSE(record_ctx[i].state == DVR_RECORD_STATE_CLOSED);
Pengfei Liuc181a982020-01-07 19:27:13 +0800517 p_ctx = &record_ctx[i];
Wentao MA96f68962022-06-15 19:45:35 +0800518 DVR_INFO("%s , current state:%d, dmx_id:%d, notification_size:%zu, flags:%d, keylen:%d ",
Yahui Han1fbf3292021-11-08 18:17:19 +0800519 __func__, p_ctx->state, params->dmx_dev_id,
hualing chen002e5b92022-02-23 17:51:21 +0800520 params->notification_size,
521 params->flags, params->keylen);
Pengfei Liuc181a982020-01-07 19:27:13 +0800522
Pengfei Liub4734232020-01-17 18:25:10 +0800523 /*Process event params*/
524 p_ctx->notification_size = params->notification_size;
hualing chen002e5b92022-02-23 17:51:21 +0800525 p_ctx->notification_time = params->notification_time;
526
Pengfei Liub4734232020-01-17 18:25:10 +0800527 p_ctx->event_notify_fn = params->event_fn;
528 p_ctx->event_userdata = params->event_userdata;
hualing chen2615aa82020-04-02 21:32:51 +0800529 p_ctx->last_send_size = 0;
hualing chen002e5b92022-02-23 17:51:21 +0800530 p_ctx->last_send_time = 0;
hualing chen94d8dd22021-12-29 15:13:43 +0800531 p_ctx->pts = ULLONG_MAX;
Yahui Han1fbf3292021-11-08 18:17:19 +0800532
533 if (params->keylen > 0) {
534 p_ctx->cryptor = am_crypt_des_open(params->clearkey,
hualing chen002e5b92022-02-23 17:51:21 +0800535 params->cleariv,
536 params->keylen * 8);
Yahui Han1fbf3292021-11-08 18:17:19 +0800537 if (!p_ctx->cryptor)
Wentao MA96f68962022-06-15 19:45:35 +0800538 DVR_INFO("%s , open des cryptor failed!!!\n", __func__);
Yahui Han1fbf3292021-11-08 18:17:19 +0800539 } else {
540 p_ctx->cryptor = NULL;
541 }
542
hualing chenf9867402020-09-23 17:06:20 +0800543 //check is new driver
544 p_ctx->is_new_dmx = dvr_check_dmx_isNew();
Pengfei Liub4734232020-01-17 18:25:10 +0800545 /*Process crypto params, todo*/
Pengfei Liub4734232020-01-17 18:25:10 +0800546 memset((void *)&dev_open_params, 0, sizeof(dev_open_params));
pengfei.liuab5a2262020-02-14 17:33:40 +0800547 if (params->data_from_memory) {
548 /* data from memory, VOD case */
549 p_ctx->is_vod = 1;
550 } else {
551 p_ctx->is_vod = 0;
552 /* data from dmx, normal dvr case */
hualing chen958fe4c2020-03-24 17:35:07 +0800553 dev_open_params.dmx_dev_id = params->dmx_dev_id;
hualing chen157641d2022-02-22 17:54:52 +0800554 //set dvr flush size
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800555 dev_open_params.buf_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE);
hualing chen157641d2022-02-22 17:54:52 +0800556 //set dvbcore ringbuf size
hualing chen03fd4942021-07-15 15:56:41 +0800557 dev_open_params.ringbuf_size = params->ringbuf_size;
hualing chen157641d2022-02-22 17:54:52 +0800558
pengfei.liuab5a2262020-02-14 17:33:40 +0800559 ret = record_device_open(&p_ctx->dev_handle, &dev_open_params);
560 if (ret != DVR_SUCCESS) {
Wentao MA96f68962022-06-15 19:45:35 +0800561 DVR_INFO("%s, open record devices failed", __func__);
pengfei.liuab5a2262020-02-14 17:33:40 +0800562 return DVR_FAILURE;
563 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800564 }
565
pengfei.liufda2a972020-04-09 14:47:15 +0800566 p_ctx->block_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE);
hualing chen157641d2022-02-22 17:54:52 +0800567
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800568 p_ctx->enc_func = NULL;
569 p_ctx->enc_userdata = NULL;
570 p_ctx->is_secure_mode = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800571 p_ctx->state = DVR_RECORD_STATE_OPENED;
wentao.ma35a69d42022-03-10 18:08:40 +0800572 p_ctx->force_sysclock = params->force_sysclock;
Wentao MAeeffdb02022-06-27 16:34:35 +0800573 p_ctx->guarded_segment_size = params->guarded_segment_size;
Wentao MA96f68962022-06-15 19:45:35 +0800574 DVR_INFO("%s, block_size:%d is_new:%d", __func__, p_ctx->block_size, p_ctx->is_new_dmx);
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800575 *p_handle = p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800576 return DVR_SUCCESS;
577}
578
579int dvr_record_close(DVR_RecordHandle_t handle)
580{
581 DVR_RecordContext_t *p_ctx;
582 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800583 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800584
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800585 p_ctx = (DVR_RecordContext_t *)handle;
586 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
587 if (p_ctx == &record_ctx[i])
588 break;
589 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800590 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800591
Wentao MA96f68962022-06-15 19:45:35 +0800592 DVR_INFO("%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800593 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
Yahui Han1fbf3292021-11-08 18:17:19 +0800594 if (p_ctx->cryptor) {
595 am_crypt_des_close(p_ctx->cryptor);
596 p_ctx->cryptor = NULL;
597 }
pengfei.liuab5a2262020-02-14 17:33:40 +0800598 if (p_ctx->is_vod) {
599 ret = DVR_SUCCESS;
600 } else {
601 ret = record_device_close(p_ctx->dev_handle);
602 if (ret != DVR_SUCCESS) {
Wentao MA96f68962022-06-15 19:45:35 +0800603 DVR_INFO("%s, failed", __func__);
pengfei.liuab5a2262020-02-14 17:33:40 +0800604 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800605 }
hualing chencedcb862022-01-04 15:32:43 +0800606 memset(p_ctx, 0, sizeof(DVR_RecordContext_t));
Pengfei Liub4734232020-01-17 18:25:10 +0800607 p_ctx->state = DVR_RECORD_STATE_CLOSED;
Pengfei Liuc181a982020-01-07 19:27:13 +0800608 return ret;
609}
610
hualing chen03fd4942021-07-15 15:56:41 +0800611int dvr_record_pause(DVR_RecordHandle_t handle)
612{
613 DVR_RecordContext_t *p_ctx;
614 int ret;
615 uint32_t i;
616
617 p_ctx = (DVR_RecordContext_t *)handle;
618 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
619 if (p_ctx == &record_ctx[i])
620 break;
621 }
622 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
623
Wentao MA96f68962022-06-15 19:45:35 +0800624 DVR_INFO("%s , current state:%d", __func__, p_ctx->state);
hualing chen03fd4942021-07-15 15:56:41 +0800625 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
626
627 if (p_ctx->is_vod) {
628 ret = DVR_SUCCESS;
629 }
630 //set pause state,will not store ts into segment
631 p_ctx->state = DVR_RECORD_STATE_PAUSE;
632 return ret;
633}
634
635int dvr_record_resume(DVR_RecordHandle_t handle)
636{
637 DVR_RecordContext_t *p_ctx;
638 int ret;
639 uint32_t i;
640
641 p_ctx = (DVR_RecordContext_t *)handle;
642 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
643 if (p_ctx == &record_ctx[i])
644 break;
645 }
646 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
647
Wentao MA96f68962022-06-15 19:45:35 +0800648 DVR_INFO("%s , current state:%d", __func__, p_ctx->state);
hualing chen03fd4942021-07-15 15:56:41 +0800649 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
650
651 if (p_ctx->is_vod) {
652 ret = DVR_SUCCESS;
653 }
654 //set stated state,will resume store ts into segment
655 p_ctx->state = DVR_RECORD_STATE_STARTED;
656 return ret;
657}
658
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800659#if 0
Pengfei Liuc181a982020-01-07 19:27:13 +0800660int dvr_record_register_encryption(DVR_RecordHandle_t handle,
661 DVR_CryptoFunction_t cb,
662 DVR_CryptoParams_t params,
663 void *userdata)
664{
665 return DVR_SUCCESS;
666}
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800667#endif
Pengfei Liuc181a982020-01-07 19:27:13 +0800668
669int dvr_record_start_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params)
670{
671 DVR_RecordContext_t *p_ctx;
672 Segment_OpenParams_t open_params;
673 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800674 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800675
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800676 p_ctx = (DVR_RecordContext_t *)handle;
677 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
678 if (p_ctx == &record_ctx[i])
679 break;
680 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800681 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800682
Wentao MA96f68962022-06-15 19:45:35 +0800683 DVR_INFO("%s , current state:%d pids:%d params->location:%s", __func__, p_ctx->state, params->segment.nb_pids, params->location);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800684 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
685 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
686 DVR_RETURN_IF_FALSE(params);
Pengfei Liuc181a982020-01-07 19:27:13 +0800687
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800688 DVR_RETURN_IF_FALSE(strlen((const char *)params->location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liuc181a982020-01-07 19:27:13 +0800689 memset(&open_params, 0, sizeof(open_params));
hualing chen7a56cba2020-04-14 14:09:27 +0800690 memcpy(open_params.location, params->location, sizeof(params->location));
Pengfei Liuc181a982020-01-07 19:27:13 +0800691 open_params.segment_id = params->segment.segment_id;
692 open_params.mode = SEGMENT_MODE_WRITE;
wentao.ma35a69d42022-03-10 18:08:40 +0800693 open_params.force_sysclock = p_ctx->force_sysclock;
Pengfei Liuc181a982020-01-07 19:27:13 +0800694
695 ret = segment_open(&open_params, &p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800696 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800697
Pengfei Liub4734232020-01-17 18:25:10 +0800698 /*process params*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800699 {
hualing chen7a56cba2020-04-14 14:09:27 +0800700 memcpy(p_ctx->location, params->location, sizeof(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800701 //need all params??
Pengfei Liuc181a982020-01-07 19:27:13 +0800702 memcpy(&p_ctx->segment_params, &params->segment, sizeof(params->segment));
Pengfei Liub4734232020-01-17 18:25:10 +0800703 /*save current segment info*/
704 memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info));
705 p_ctx->segment_info.id = params->segment.segment_id;
706 p_ctx->segment_info.nb_pids = params->segment.nb_pids;
707 memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800708 }
709
pengfei.liuab5a2262020-02-14 17:33:40 +0800710 if (!p_ctx->is_vod) {
711 /* normal dvr case */
712 for (i = 0; i < params->segment.nb_pids; i++) {
713 ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
714 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
715 }
716 ret = record_device_start(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800717 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800718 }
719
Zhiqiang Han5ebad992020-04-28 18:19:59 +0800720 ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info);
721
Pengfei Liuc181a982020-01-07 19:27:13 +0800722 p_ctx->state = DVR_RECORD_STATE_STARTED;
pengfei.liuab5a2262020-02-14 17:33:40 +0800723 if (!p_ctx->is_vod)
724 pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +0800725
Pengfei Liuc181a982020-01-07 19:27:13 +0800726 return DVR_SUCCESS;
727}
728
729int dvr_record_next_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, DVR_RecordSegmentInfo_t *p_info)
730{
731 DVR_RecordContext_t *p_ctx;
Pengfei Liub4734232020-01-17 18:25:10 +0800732 Segment_OpenParams_t open_params;
Pengfei Liuc181a982020-01-07 19:27:13 +0800733 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800734 uint32_t i;
hualing chen2932d372020-04-29 13:44:00 +0800735 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800736
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800737 p_ctx = (DVR_RecordContext_t *)handle;
738 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
739 if (p_ctx == &record_ctx[i])
740 break;
741 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800742 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800743
Wentao MA96f68962022-06-15 19:45:35 +0800744 DVR_INFO("%s , current state:%d p_ctx->location:%s", __func__, p_ctx->state, p_ctx->location);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800745 DVR_RETURN_IF_FALSE(p_ctx->state == DVR_RECORD_STATE_STARTED);
746 //DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
747 DVR_RETURN_IF_FALSE(params);
748 DVR_RETURN_IF_FALSE(p_info);
pengfei.liuab5a2262020-02-14 17:33:40 +0800749 DVR_RETURN_IF_FALSE(!p_ctx->is_vod);
Pengfei Liuc181a982020-01-07 19:27:13 +0800750
751 /*Stop the on going record segment*/
Pengfei Liub4734232020-01-17 18:25:10 +0800752 //ret = record_device_stop(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800753 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800754 p_ctx->state = DVR_RECORD_STATE_STOPPED;
755 pthread_join(p_ctx->thread, NULL);
756
hualing chen2932d372020-04-29 13:44:00 +0800757 //add index file store
758 pos = segment_tell_position(p_ctx->segment_handle);
759 segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
760
761 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
Pengfei Liub4734232020-01-17 18:25:10 +0800762 /*Update segment info*/
763 memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
764
765 ret = segment_store_info(p_ctx->segment_handle, p_info);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800766 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
hualing chenb9a02922021-12-14 11:29:47 +0800767 ret = segment_store_allInfo(p_ctx->segment_handle, p_info);
Wentao MA96f68962022-06-15 19:45:35 +0800768 DVR_INFO("%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d params->segment.nb_pids:%d",
hualing chena540a7e2020-03-27 16:44:05 +0800769 __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 +0800770
Pengfei Liub4734232020-01-17 18:25:10 +0800771 /*Close current segment*/
772 ret = segment_close(p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800773 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800774 /*Open the new record segment*/
775 memset(&open_params, 0, sizeof(open_params));
hualing chen7a56cba2020-04-14 14:09:27 +0800776 memcpy(open_params.location, p_ctx->location, sizeof(p_ctx->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800777 open_params.segment_id = params->segment.segment_id;
778 open_params.mode = SEGMENT_MODE_WRITE;
wentao.ma35a69d42022-03-10 18:08:40 +0800779 open_params.force_sysclock = p_ctx->force_sysclock;
Wentao MA96f68962022-06-15 19:45:35 +0800780 DVR_INFO("%s: p_ctx->location:%s params->location:%s", __func__, p_ctx->location,params->location);
hualing chen002e5b92022-02-23 17:51:21 +0800781 p_ctx->last_send_size = 0;
782 p_ctx->last_send_time = 0;
Pengfei Liub4734232020-01-17 18:25:10 +0800783 ret = segment_open(&open_params, &p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800784 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800785 /*process params*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800786 {
Pengfei Liub4734232020-01-17 18:25:10 +0800787 //need all params??
Pengfei Liuc181a982020-01-07 19:27:13 +0800788 memcpy(&p_ctx->segment_params, &params->segment, sizeof(params->segment));
Pengfei Liub4734232020-01-17 18:25:10 +0800789 /*save current segment info*/
790 memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info));
791 p_ctx->segment_info.id = params->segment.segment_id;
792 memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800793 }
794
Pengfei Liub4734232020-01-17 18:25:10 +0800795 p_ctx->segment_info.nb_pids = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800796 for (i = 0; i < params->segment.nb_pids; i++) {
Pengfei Liub4734232020-01-17 18:25:10 +0800797 switch (params->segment.pid_action[i]) {
798 case DVR_RECORD_PID_CREATE:
Wentao MA96f68962022-06-15 19:45:35 +0800799 DVR_INFO("%s create pid:%d", __func__, params->segment.pids[i].pid);
Pengfei Liub4734232020-01-17 18:25:10 +0800800 ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
801 p_ctx->segment_info.nb_pids++;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800802 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800803 break;
804 case DVR_RECORD_PID_KEEP:
Wentao MA96f68962022-06-15 19:45:35 +0800805 DVR_INFO("%s keep pid:%d", __func__, params->segment.pids[i].pid);
Pengfei Liub4734232020-01-17 18:25:10 +0800806 p_ctx->segment_info.nb_pids++;
807 break;
808 case DVR_RECORD_PID_CLOSE:
Wentao MA96f68962022-06-15 19:45:35 +0800809 DVR_INFO("%s close pid:%d", __func__, params->segment.pids[i].pid);
Pengfei Liub4734232020-01-17 18:25:10 +0800810 ret = record_device_remove_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800811 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800812 break;
813 default:
Wentao MA96f68962022-06-15 19:45:35 +0800814 DVR_INFO("%s wrong action pid:%d", __func__, params->segment.pids[i].pid);
Pengfei Liub4734232020-01-17 18:25:10 +0800815 return DVR_FAILURE;
816 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800817 }
818
Pengfei Liub4734232020-01-17 18:25:10 +0800819 //ret = record_device_start(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800820 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
hualing chen4b7c15d2020-04-07 16:13:48 +0800821 /*Update segment info*/
822 ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info);
hualing chen94d8dd22021-12-29 15:13:43 +0800823 if (p_ctx->pts != ULLONG_MAX)
824 segment_update_pts(p_ctx->segment_handle, p_ctx->pts, 0);
Pengfei Liuc181a982020-01-07 19:27:13 +0800825
Pengfei Liuc181a982020-01-07 19:27:13 +0800826 p_ctx->state = DVR_RECORD_STATE_STARTED;
Zhiqiang Han0d60f2b2020-05-26 14:39:54 +0800827 pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800828 return DVR_SUCCESS;
829}
830
831int dvr_record_stop_segment(DVR_RecordHandle_t handle, DVR_RecordSegmentInfo_t *p_info)
832{
833 DVR_RecordContext_t *p_ctx;
834 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800835 uint32_t i;
hualing chen2932d372020-04-29 13:44:00 +0800836 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800837
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800838 p_ctx = (DVR_RecordContext_t *)handle;
839 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
840 if (p_ctx == &record_ctx[i])
841 break;
842 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800843 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800844
Wentao MA96f68962022-06-15 19:45:35 +0800845 DVR_INFO("%s , current state:%d p_ctx->location:%s", __func__, p_ctx->state, p_ctx->location);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800846 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STOPPED);
847 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800848 DVR_RETURN_IF_FALSE(p_info);/*should support NULL*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800849
Pengfei Liuc181a982020-01-07 19:27:13 +0800850 p_ctx->state = DVR_RECORD_STATE_STOPPED;
pengfei.liuab5a2262020-02-14 17:33:40 +0800851 if (p_ctx->is_vod) {
pengfei.liu8b563292020-02-26 15:49:02 +0800852 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
pengfei.liuab5a2262020-02-14 17:33:40 +0800853 p_ctx->segment_info.duration = 10*1000; //debug, should delete it
854 } else {
855 ret = record_device_stop(p_ctx->dev_handle);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800856 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
857 if (ret != DVR_SUCCESS)
858 goto end;
pengfei.liuab5a2262020-02-14 17:33:40 +0800859 //p_ctx->state = DVR_RECORD_STATE_STOPPED;
860 pthread_join(p_ctx->thread, NULL);
861 }
862
hualing chen2932d372020-04-29 13:44:00 +0800863 //add index file store
864 pos = segment_tell_position(p_ctx->segment_handle);
865 segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
hualing chene41f4372020-06-06 16:29:17 +0800866 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +0800867
Pengfei Liub4734232020-01-17 18:25:10 +0800868 /*Update segment info*/
869 memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
870
871 ret = segment_store_info(p_ctx->segment_handle, p_info);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800872 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
873 if (ret != DVR_SUCCESS)
874 goto end;
Pengfei Liub4734232020-01-17 18:25:10 +0800875
hualing chenb9a02922021-12-14 11:29:47 +0800876 segment_store_allInfo(p_ctx->segment_handle, p_info);
877
Wentao MA96f68962022-06-15 19:45:35 +0800878 DVR_INFO("%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d",
Pengfei Liub4734232020-01-17 18:25:10 +0800879 __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets);
880
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800881end:
Pengfei Liuc181a982020-01-07 19:27:13 +0800882 ret = segment_close(p_ctx->segment_handle);
hualing chencedcb862022-01-04 15:32:43 +0800883 p_ctx->segment_handle = NULL;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800884 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800885 return DVR_SUCCESS;
886}
Pengfei Liuc181a982020-01-07 19:27:13 +0800887
Pengfei Liub4734232020-01-17 18:25:10 +0800888int dvr_record_resume_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, uint64_t *p_resume_size)
889{
890 DVR_RecordContext_t *p_ctx;
891 uint32_t i;
pengfei.liuab5a2262020-02-14 17:33:40 +0800892 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800893
894 p_ctx = (DVR_RecordContext_t *)handle;
895 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
896 if (p_ctx == &record_ctx[i])
897 break;
Pengfei Liuc181a982020-01-07 19:27:13 +0800898 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800899 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
900 DVR_RETURN_IF_FALSE(params);
901 DVR_RETURN_IF_FALSE(p_resume_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800902
Wentao MA96f68962022-06-15 19:45:35 +0800903 DVR_INFO("%s , current state:%d, resume size:%lld", __func__, p_ctx->state, *p_resume_size);
pengfei.liuab5a2262020-02-14 17:33:40 +0800904 ret = dvr_record_start_segment(handle, params);
905 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
906
907 p_ctx->segment_info.size = *p_resume_size;
908
909 return DVR_SUCCESS;
910}
911
912int dvr_record_get_status(DVR_RecordHandle_t handle, DVR_RecordStatus_t *p_status)
913{
914 DVR_RecordContext_t *p_ctx;
915 int i;
916
917 p_ctx = (DVR_RecordContext_t *)handle;
918 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
919 if (p_ctx == &record_ctx[i])
920 break;
921 }
922 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
923 DVR_RETURN_IF_FALSE(p_status);
924
925 //lock
926 p_status->state = p_ctx->state;
927 p_status->info.id = p_ctx->segment_info.id;
928 p_status->info.duration = p_ctx->segment_info.duration;
929 p_status->info.size = p_ctx->segment_info.size;
930 p_status->info.nb_packets = p_ctx->segment_info.size/188;
931
932 return DVR_SUCCESS;
933}
934
935int dvr_record_write(DVR_RecordHandle_t handle, void *buffer, uint32_t len)
936{
937 DVR_RecordContext_t *p_ctx;
938 uint32_t i;
939 off_t pos = 0;
940 int ret;
941 int has_pcr;
942
943 p_ctx = (DVR_RecordContext_t *)handle;
944 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
945 if (p_ctx == &record_ctx[i])
946 break;
947 }
948 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
949 DVR_RETURN_IF_FALSE(buffer);
950 DVR_RETURN_IF_FALSE(len);
951
952 pos = segment_tell_position(p_ctx->segment_handle);
953 has_pcr = record_do_pcr_index(p_ctx, buffer, len);
954 if (has_pcr == 0) {
955 /* Pull VOD record shoud use PCR time index */
Wentao MA96f68962022-06-15 19:45:35 +0800956 DVR_INFO("%s has no pcr, can NOT do time index", __func__);
pengfei.liuab5a2262020-02-14 17:33:40 +0800957 }
958 ret = segment_write(p_ctx->segment_handle, buffer, len);
hualing chen2932d372020-04-29 13:44:00 +0800959 if (ret != len) {
Wentao MA96f68962022-06-15 19:45:35 +0800960 DVR_INFO("%s write error ret:%d len:%d", __func__, ret, len);
hualing chen2932d372020-04-29 13:44:00 +0800961 }
pengfei.liuab5a2262020-02-14 17:33:40 +0800962 p_ctx->segment_info.size += len;
963 p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188;
964
Pengfei Liuc181a982020-01-07 19:27:13 +0800965 return DVR_SUCCESS;
966}
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800967
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800968int dvr_record_set_encrypt_callback(DVR_RecordHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800969{
970 DVR_RecordContext_t *p_ctx;
971 uint32_t i;
972
973 p_ctx = (DVR_RecordContext_t *)handle;
974 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
975 if (p_ctx == &record_ctx[i])
976 break;
977 }
978 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
979 DVR_RETURN_IF_FALSE(func);
980
Wentao MA96f68962022-06-15 19:45:35 +0800981 DVR_INFO("%s , current state:%d", __func__, p_ctx->state);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800982 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
983 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
984
985 p_ctx->enc_func = func;
986 p_ctx->enc_userdata = userdata;
987 return DVR_SUCCESS;
988}
989
990int dvr_record_set_secure_buffer(DVR_RecordHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
991{
992 DVR_RecordContext_t *p_ctx;
993 uint32_t i;
994 int ret;
995
996 p_ctx = (DVR_RecordContext_t *)handle;
997 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
998 if (p_ctx == &record_ctx[i])
999 break;
1000 }
1001 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
1002 DVR_RETURN_IF_FALSE(p_secure_buf);
1003 DVR_RETURN_IF_FALSE(len);
1004
Wentao MA96f68962022-06-15 19:45:35 +08001005 DVR_INFO("%s , current state:%d", __func__, p_ctx->state);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001006 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
1007 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
1008
1009 ret = record_device_set_secure_buffer(p_ctx->dev_handle, p_secure_buf, len);
1010 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
1011
1012 p_ctx->is_secure_mode = 1;
Yahui Han4577db82022-07-05 17:46:19 +08001013 p_ctx->secbuf_size = len;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001014 return ret;
1015}
hualing chen4fe3bee2020-10-23 13:58:52 +08001016
1017int dvr_record_is_secure_mode(DVR_RecordHandle_t handle)
1018{
1019 DVR_RecordContext_t *p_ctx;
1020 uint32_t i;
1021 int ret;
1022
1023 p_ctx = (DVR_RecordContext_t *)handle;
1024 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
1025 if (p_ctx == &record_ctx[i])
1026 break;
1027 }
hualing chen002e5b92022-02-23 17:51:21 +08001028
hualing chen4fe3bee2020-10-23 13:58:52 +08001029 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
1030
1031 if (p_ctx->is_secure_mode == 1)
1032 ret = 1;
1033 else
1034 ret = 0;
1035 return ret;
Yahui Hance15e9c2020-12-08 18:08:32 +08001036}