blob: 23c275cefa7f4068d8ae05b06e00383d56256fbd [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 (256 * 1024)
pengfei.liuab5a2262020-02-14 17:33:40 +080014
15/**\brief DVR index file type*/
16typedef enum {
17 DVR_INDEX_TYPE_PCR, /**< DVR index file use pcr*/
18 DVR_INDEX_TYPE_LOCAL_CLOCK, /**< DVR index file use local clock*/
19 DVR_INDEX_TYPE_INVALID /**< DVR index file type invalid type*/
20} DVR_IndexType_t;
21
22/**\brief DVR VOD context*/
23typedef struct {
24 pthread_mutex_t mutex; /**< VOD mutex lock*/
25 pthread_cond_t cond; /**< VOD condition*/
26 void *buffer; /**< VOD buffer*/
27 uint32_t buf_len; /**< VOD buffer len*/
28} DVR_VodContext_t;
29
pengfei.liu07ddc8a2020-03-24 23:36:53 +080030/**\brief DVR record secure mode buffer*/
31typedef struct {
32 uint32_t addr; /**< Secure mode record buffer address*/
33 uint32_t len; /**< Secure mode record buffer length*/
34} DVR_SecureBuffer_t;
35
Pengfei Liub4734232020-01-17 18:25:10 +080036/**\brief DVR record context*/
Pengfei Liuc181a982020-01-07 19:27:13 +080037typedef struct {
Pengfei Liub4734232020-01-17 18:25:10 +080038 pthread_t thread; /**< DVR thread handle*/
39 Record_DeviceHandle_t dev_handle; /**< DVR device handle*/
40 Segment_Handle_t segment_handle; /**< DVR segment handle*/
41 DVR_RecordState_t state; /**< DVR record state*/
42 char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/
43 DVR_RecordSegmentStartParams_t segment_params; /**< DVR record start parameters*/
44 DVR_RecordSegmentInfo_t segment_info; /**< DVR record current segment info*/
45 size_t notification_size; /**< DVR record nogification size*/
46 DVR_RecordEventFunction_t event_notify_fn; /**< DVR record event notify function*/
47 void *event_userdata; /**< DVR record event userdata*/
pengfei.liuab5a2262020-02-14 17:33:40 +080048 //DVR_VodContext_t vod; /**< DVR record vod context*/
49 int is_vod; /**< Indicate current mode is VOD record mode*/
pengfei.liu27cc4ec2020-04-03 16:28:16 +080050 DVR_CryptoFunction_t enc_func; /**< Encrypt function*/
pengfei.liu07ddc8a2020-03-24 23:36:53 +080051 void *enc_userdata; /**< Encrypt userdata*/
52 int is_secure_mode; /**< Record session run in secure pipeline */
hualing chen2615aa82020-04-02 21:32:51 +080053 size_t last_send_size; /**< Last send notify segment size */
pengfei.liufda2a972020-04-09 14:47:15 +080054 uint32_t block_size; /**< DVR record block 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;
pengfei.liufda2a972020-04-09 14:47:15 +0800157 uint32_t block_size = p_ctx->block_size;
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;
165 #define DVR_STORE_INFO_TIME (1000)
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800166 DVR_SecureBuffer_t secure_buf;
Pengfei Liuc181a982020-01-07 19:27:13 +0800167
Pengfei Liub4734232020-01-17 18:25:10 +0800168 buf = (uint8_t *)malloc(block_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800169 if (!buf) {
170 DVR_DEBUG(1, "%s, malloc failed", __func__);
171 return NULL;
172 }
173
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800174 buf_out = (uint8_t *)malloc(block_size + 188);
175 if (!buf_out) {
176 DVR_DEBUG(1, "%s, malloc failed", __func__);
177 return NULL;
178 }
179
hualing chen266b9502020-04-04 17:39:39 +0800180 memset(&record_status, 0, sizeof(record_status));
181 record_status.state = DVR_RECORD_STATE_STARTED;
pengfei.liufda2a972020-04-09 14:47:15 +0800182 if (p_ctx->event_notify_fn) {
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",
hualing chen266b9502020-04-04 17:39:39 +0800185 __func__, record_status.state);
pengfei.liufda2a972020-04-09 14:47:15 +0800186 }
187 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 +0800188
189
Pengfei Liub4734232020-01-17 18:25:10 +0800190 clock_gettime(CLOCK_MONOTONIC, &start_ts);
Pengfei Liuc181a982020-01-07 19:27:13 +0800191 while (p_ctx->state == DVR_RECORD_STATE_STARTED) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800192 /* data from dmx, normal dvr case */
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800193 if (p_ctx->is_secure_mode) {
194 memset(&secure_buf, 0, sizeof(secure_buf));
195 len = record_device_read(p_ctx->dev_handle, &secure_buf, sizeof(secure_buf), 1000);
hualing chen266b9502020-04-04 17:39:39 +0800196 if (len != DVR_FAILURE) {
197 DVR_DEBUG(1, "%s, secure_buf:%#x, size:%#x", __func__, secure_buf.addr, secure_buf.len);
198 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800199 } else {
200 len = record_device_read(p_ctx->dev_handle, buf, block_size, 1000);
201 }
Pengfei Liub4734232020-01-17 18:25:10 +0800202 if (len == DVR_FAILURE) {
203 usleep(10*1000);
204 continue;
205 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800206
207 /* Got data from device, record it */
208 if (p_ctx->enc_func) {
209 /* Encrypt record data */
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800210 DVR_CryptoParams_t crypto_params;
211
212 memset(&crypto_params, 0, sizeof(crypto_params));
213 crypto_params.type = DVR_CRYPTO_TYPE_ENCRYPT;
214 memcpy(crypto_params.location, p_ctx->location, strlen(p_ctx->location));
215 crypto_params.segment_id = p_ctx->segment_info.id;
216 crypto_params.offset = p_ctx->segment_info.size;
217
218 if (p_ctx->is_secure_mode) {
219 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_SECURE;
220 crypto_params.input_buffer.addr = secure_buf.addr;
221 crypto_params.input_buffer.size = secure_buf.len;
222 } else {
223 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
224 crypto_params.input_buffer.addr = (size_t)buf;
225 crypto_params.input_buffer.size = len;
226 }
227
228 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL;
229 crypto_params.output_buffer.addr = (size_t)buf_out;
230 crypto_params.output_buffer.size = block_size + 188;
231
232 p_ctx->enc_func(&crypto_params, p_ctx->enc_userdata);
233 /* Out buffer length may not equal in buffer length */
234 ret = segment_write(p_ctx->segment_handle, buf_out, crypto_params.output_size);
235 len = crypto_params.output_size;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800236 } else {
237 ret = segment_write(p_ctx->segment_handle, buf, len);
238 }
hualing chen626204e2020-04-07 11:55:08 +0800239 //add DVR_RECORD_EVENT_WRITE_ERROR event if write error
pengfei.liufda2a972020-04-09 14:47:15 +0800240 if (ret == -1 && len > 0 && p_ctx->event_notify_fn) {
hualing chen626204e2020-04-07 11:55:08 +0800241 //send write event
242 p_ctx->event_notify_fn(DVR_RECORD_EVENT_WRITE_ERROR, NULL, p_ctx->event_userdata);
pengfei.liufda2a972020-04-09 14:47:15 +0800243 DVR_DEBUG(1, "%s notify DVR_RECORD_EVENT_WRITE_ERROR, exit record thread", __func__);
hualing chen626204e2020-04-07 11:55:08 +0800244 goto end;
245 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800246 /* Do time index */
247 uint8_t *index_buf = p_ctx->enc_func ? buf_out : buf;
pengfei.liuab5a2262020-02-14 17:33:40 +0800248 pos = segment_tell_position(p_ctx->segment_handle);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800249 has_pcr = record_do_pcr_index(p_ctx, index_buf, len);
pengfei.liuab5a2262020-02-14 17:33:40 +0800250 if (has_pcr == 0 && index_type == DVR_INDEX_TYPE_INVALID) {
251 clock_gettime(CLOCK_MONOTONIC, &end_ts);
252 if ((end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) -
253 (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) > 40) {
254 /* PCR interval threshlod > 40 ms*/
255 DVR_DEBUG(1, "%s use local clock time index", __func__);
256 index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
257 }
258 } else if (has_pcr && index_type == DVR_INDEX_TYPE_INVALID){
259 DVR_DEBUG(1, "%s use pcr time index", __func__);
260 index_type = DVR_INDEX_TYPE_PCR;
Pengfei Liuc181a982020-01-07 19:27:13 +0800261 }
pengfei.liuab5a2262020-02-14 17:33:40 +0800262
263 /* Update segment info */
Pengfei Liub4734232020-01-17 18:25:10 +0800264 p_ctx->segment_info.size += len;
pengfei.liuab5a2262020-02-14 17:33:40 +0800265 /*Duration need use pcr to calculate, todo...*/
266 if (index_type == DVR_INDEX_TYPE_PCR) {
pengfei.liu8b563292020-02-26 15:49:02 +0800267 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
hualing chen87072a82020-03-12 16:20:12 +0800268 if (pre_time == 0)
269 pre_time = p_ctx->segment_info.duration;
pengfei.liuab5a2262020-02-14 17:33:40 +0800270 } else if (index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) {
271 clock_gettime(CLOCK_MONOTONIC, &end_ts);
272 p_ctx->segment_info.duration = (end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) -
273 (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000);
hualing chen87072a82020-03-12 16:20:12 +0800274 if (pre_time == 0)
275 pre_time = p_ctx->segment_info.duration;
pengfei.liuab5a2262020-02-14 17:33:40 +0800276 segment_update_pts(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
277 } else {
278 DVR_DEBUG(1, "%s can NOT do time index", __func__);
279 }
280 p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188;
Pengfei Liub4734232020-01-17 18:25:10 +0800281
hualing chen87072a82020-03-12 16:20:12 +0800282 if (p_ctx->segment_info.duration - pre_time > DVR_STORE_INFO_TIME) {
283 pre_time = p_ctx->segment_info.duration + DVR_STORE_INFO_TIME;
284 segment_store_info(p_ctx->segment_handle, &(p_ctx->segment_info));
285 }
286 /*Event notification*/
Pengfei Liub4734232020-01-17 18:25:10 +0800287 if (p_ctx->notification_size &&
288 p_ctx->event_notify_fn &&
hualing chen2615aa82020-04-02 21:32:51 +0800289 /*!(p_ctx->segment_info.size % p_ctx->notification_size)*/
290 (p_ctx->segment_info.size -p_ctx->last_send_size) >= p_ctx->notification_size&&
pengfei.liuab5a2262020-02-14 17:33:40 +0800291 p_ctx->segment_info.duration > 0) {
Pengfei Liub4734232020-01-17 18:25:10 +0800292 memset(&record_status, 0, sizeof(record_status));
pengfei.liuab5a2262020-02-14 17:33:40 +0800293 //clock_gettime(CLOCK_MONOTONIC, &end_ts);
hualing chen2615aa82020-04-02 21:32:51 +0800294 p_ctx->last_send_size = p_ctx->segment_info.size;
Pengfei Liub4734232020-01-17 18:25:10 +0800295 record_status.state = p_ctx->state;
296 record_status.info.id = p_ctx->segment_info.id;
pengfei.liuab5a2262020-02-14 17:33:40 +0800297 record_status.info.duration = p_ctx->segment_info.duration;
Pengfei Liub4734232020-01-17 18:25:10 +0800298 record_status.info.size = p_ctx->segment_info.size;
299 record_status.info.nb_packets = p_ctx->segment_info.size/188;
300 p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata);
301 DVR_DEBUG(1, "%s notify record status, state:%d, id:%lld, duration:%ld ms, size:%zu",
302 __func__, record_status.state, record_status.info.id, record_status.info.duration, record_status.info.size);
303 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800304 }
hualing chen626204e2020-04-07 11:55:08 +0800305end:
Pengfei Liub4734232020-01-17 18:25:10 +0800306 free((void *)buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800307 free((void *)buf_out);
Pengfei Liub4734232020-01-17 18:25:10 +0800308 DVR_DEBUG(1, "exit %s", __func__);
Pengfei Liuc181a982020-01-07 19:27:13 +0800309 return NULL;
310}
311
312int dvr_record_open(DVR_RecordHandle_t *p_handle, DVR_RecordOpenParams_t *params)
313{
314 DVR_RecordContext_t *p_ctx;
315 Record_DeviceOpenParams_t dev_open_params;
316 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800317 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800318
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800319 DVR_RETURN_IF_FALSE(p_handle);
320 DVR_RETURN_IF_FALSE(params);
Pengfei Liuc181a982020-01-07 19:27:13 +0800321
322 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
Pengfei Liub4734232020-01-17 18:25:10 +0800323 if (record_ctx[i].state == DVR_RECORD_STATE_CLOSED) {
Pengfei Liuc181a982020-01-07 19:27:13 +0800324 break;
325 }
326 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800327 DVR_RETURN_IF_FALSE(record_ctx[i].state == DVR_RECORD_STATE_CLOSED);
Pengfei Liuc181a982020-01-07 19:27:13 +0800328 p_ctx = &record_ctx[i];
Pengfei Liub4734232020-01-17 18:25:10 +0800329 DVR_DEBUG(1, "%s , current state:%d, dmx_id:%d, notification_size:%zu", __func__,
330 p_ctx->state, params->dmx_dev_id, params->notification_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800331
Pengfei Liub4734232020-01-17 18:25:10 +0800332 /*Process event params*/
333 p_ctx->notification_size = params->notification_size;
334 p_ctx->event_notify_fn = params->event_fn;
335 p_ctx->event_userdata = params->event_userdata;
hualing chen2615aa82020-04-02 21:32:51 +0800336 p_ctx->last_send_size = 0;
Pengfei Liub4734232020-01-17 18:25:10 +0800337 /*Process crypto params, todo*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800338
Pengfei Liub4734232020-01-17 18:25:10 +0800339 memset((void *)&dev_open_params, 0, sizeof(dev_open_params));
pengfei.liuab5a2262020-02-14 17:33:40 +0800340 if (params->data_from_memory) {
341 /* data from memory, VOD case */
342 p_ctx->is_vod = 1;
343 } else {
344 p_ctx->is_vod = 0;
345 /* data from dmx, normal dvr case */
hualing chen958fe4c2020-03-24 17:35:07 +0800346 dev_open_params.dmx_dev_id = params->dmx_dev_id;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800347 dev_open_params.buf_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE);
Pengfei Liub4734232020-01-17 18:25:10 +0800348
pengfei.liuab5a2262020-02-14 17:33:40 +0800349 ret = record_device_open(&p_ctx->dev_handle, &dev_open_params);
350 if (ret != DVR_SUCCESS) {
351 DVR_DEBUG(1, "%s, open record devices failed", __func__);
352 return DVR_FAILURE;
353 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800354 }
355
pengfei.liufda2a972020-04-09 14:47:15 +0800356 p_ctx->block_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800357 p_ctx->enc_func = NULL;
358 p_ctx->enc_userdata = NULL;
359 p_ctx->is_secure_mode = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800360 p_ctx->state = DVR_RECORD_STATE_OPENED;
361
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800362 *p_handle = p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800363 return DVR_SUCCESS;
364}
365
366int dvr_record_close(DVR_RecordHandle_t handle)
367{
368 DVR_RecordContext_t *p_ctx;
369 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800370 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800371
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800372 p_ctx = (DVR_RecordContext_t *)handle;
373 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
374 if (p_ctx == &record_ctx[i])
375 break;
376 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800377 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800378
Pengfei Liub4734232020-01-17 18:25:10 +0800379 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800380 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
Pengfei Liuc181a982020-01-07 19:27:13 +0800381
pengfei.liuab5a2262020-02-14 17:33:40 +0800382 if (p_ctx->is_vod) {
383 ret = DVR_SUCCESS;
384 } else {
385 ret = record_device_close(p_ctx->dev_handle);
386 if (ret != DVR_SUCCESS) {
387 DVR_DEBUG(1, "%s, failed", __func__);
388 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800389 }
390
Pengfei Liub4734232020-01-17 18:25:10 +0800391 p_ctx->state = DVR_RECORD_STATE_CLOSED;
Pengfei Liuc181a982020-01-07 19:27:13 +0800392 return ret;
393}
394
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800395#if 0
Pengfei Liuc181a982020-01-07 19:27:13 +0800396int dvr_record_register_encryption(DVR_RecordHandle_t handle,
397 DVR_CryptoFunction_t cb,
398 DVR_CryptoParams_t params,
399 void *userdata)
400{
401 return DVR_SUCCESS;
402}
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800403#endif
Pengfei Liuc181a982020-01-07 19:27:13 +0800404
405int dvr_record_start_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params)
406{
407 DVR_RecordContext_t *p_ctx;
408 Segment_OpenParams_t open_params;
409 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800410 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800411
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800412 p_ctx = (DVR_RecordContext_t *)handle;
413 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
414 if (p_ctx == &record_ctx[i])
415 break;
416 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800417 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800418
Pengfei Liub4734232020-01-17 18:25:10 +0800419 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800420 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
421 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
422 DVR_RETURN_IF_FALSE(params);
Pengfei Liuc181a982020-01-07 19:27:13 +0800423
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800424 DVR_RETURN_IF_FALSE(strlen((const char *)params->location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liuc181a982020-01-07 19:27:13 +0800425 memset(&open_params, 0, sizeof(open_params));
426 memcpy(open_params.location, params->location, strlen(params->location));
427 open_params.segment_id = params->segment.segment_id;
428 open_params.mode = SEGMENT_MODE_WRITE;
429
430 ret = segment_open(&open_params, &p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800431 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800432
Pengfei Liub4734232020-01-17 18:25:10 +0800433 /*process params*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800434 {
435 memcpy(p_ctx->location, params->location, strlen(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800436 //need all params??
Pengfei Liuc181a982020-01-07 19:27:13 +0800437 memcpy(&p_ctx->segment_params, &params->segment, sizeof(params->segment));
Pengfei Liub4734232020-01-17 18:25:10 +0800438 /*save current segment info*/
439 memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info));
440 p_ctx->segment_info.id = params->segment.segment_id;
441 p_ctx->segment_info.nb_pids = params->segment.nb_pids;
442 memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800443 }
444
pengfei.liuab5a2262020-02-14 17:33:40 +0800445 if (!p_ctx->is_vod) {
446 /* normal dvr case */
447 for (i = 0; i < params->segment.nb_pids; i++) {
448 ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
449 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
450 }
451 ret = record_device_start(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800452 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800453 }
454
Pengfei Liuc181a982020-01-07 19:27:13 +0800455 p_ctx->state = DVR_RECORD_STATE_STARTED;
pengfei.liuab5a2262020-02-14 17:33:40 +0800456 if (!p_ctx->is_vod)
457 pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +0800458
Pengfei Liuc181a982020-01-07 19:27:13 +0800459 return DVR_SUCCESS;
460}
461
462int dvr_record_next_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, DVR_RecordSegmentInfo_t *p_info)
463{
464 DVR_RecordContext_t *p_ctx;
Pengfei Liub4734232020-01-17 18:25:10 +0800465 Segment_OpenParams_t open_params;
Pengfei Liuc181a982020-01-07 19:27:13 +0800466 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800467 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800468
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800469 p_ctx = (DVR_RecordContext_t *)handle;
470 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
471 if (p_ctx == &record_ctx[i])
472 break;
473 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800474 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800475
Pengfei Liub4734232020-01-17 18:25:10 +0800476 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800477 DVR_RETURN_IF_FALSE(p_ctx->state == DVR_RECORD_STATE_STARTED);
478 //DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
479 DVR_RETURN_IF_FALSE(params);
480 DVR_RETURN_IF_FALSE(p_info);
pengfei.liuab5a2262020-02-14 17:33:40 +0800481 DVR_RETURN_IF_FALSE(!p_ctx->is_vod);
Pengfei Liuc181a982020-01-07 19:27:13 +0800482
483 /*Stop the on going record segment*/
Pengfei Liub4734232020-01-17 18:25:10 +0800484 //ret = record_device_stop(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800485 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800486
487 p_ctx->state = DVR_RECORD_STATE_STOPPED;
488 pthread_join(p_ctx->thread, NULL);
489
Pengfei Liub4734232020-01-17 18:25:10 +0800490 /*Update segment info*/
491 memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
492
493 ret = segment_store_info(p_ctx->segment_handle, p_info);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800494 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800495
hualing chena540a7e2020-03-27 16:44:05 +0800496 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",
497 __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 +0800498
Pengfei Liub4734232020-01-17 18:25:10 +0800499 /*Close current segment*/
500 ret = segment_close(p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800501 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800502 /*Open the new record segment*/
503 memset(&open_params, 0, sizeof(open_params));
504 memcpy(open_params.location, p_ctx->location, strlen(p_ctx->location));
505 open_params.segment_id = params->segment.segment_id;
506 open_params.mode = SEGMENT_MODE_WRITE;
507
508 ret = segment_open(&open_params, &p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800509 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800510 /*process params*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800511 {
Pengfei Liub4734232020-01-17 18:25:10 +0800512 //need all params??
Pengfei Liuc181a982020-01-07 19:27:13 +0800513 memcpy(&p_ctx->segment_params, &params->segment, sizeof(params->segment));
Pengfei Liub4734232020-01-17 18:25:10 +0800514 /*save current segment info*/
515 memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info));
516 p_ctx->segment_info.id = params->segment.segment_id;
517 memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800518 }
519
Pengfei Liub4734232020-01-17 18:25:10 +0800520 p_ctx->segment_info.nb_pids = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800521 for (i = 0; i < params->segment.nb_pids; i++) {
Pengfei Liub4734232020-01-17 18:25:10 +0800522 switch (params->segment.pid_action[i]) {
523 case DVR_RECORD_PID_CREATE:
524 DVR_DEBUG(1, "%s create pid:%d", __func__, params->segment.pids[i].pid);
525 ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
526 p_ctx->segment_info.nb_pids++;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800527 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800528 break;
529 case DVR_RECORD_PID_KEEP:
530 DVR_DEBUG(1, "%s keep pid:%d", __func__, params->segment.pids[i].pid);
531 p_ctx->segment_info.nb_pids++;
532 break;
533 case DVR_RECORD_PID_CLOSE:
534 DVR_DEBUG(1, "%s close pid:%d", __func__, params->segment.pids[i].pid);
535 ret = record_device_remove_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800536 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800537 break;
538 default:
539 DVR_DEBUG(1, "%s wrong action pid:%d", __func__, params->segment.pids[i].pid);
540 return DVR_FAILURE;
541 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800542 }
543
Pengfei Liub4734232020-01-17 18:25:10 +0800544 //ret = record_device_start(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800545 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800546
547 pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx);
548 p_ctx->state = DVR_RECORD_STATE_STARTED;
549 return DVR_SUCCESS;
550}
551
552int dvr_record_stop_segment(DVR_RecordHandle_t handle, DVR_RecordSegmentInfo_t *p_info)
553{
554 DVR_RecordContext_t *p_ctx;
555 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800556 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800557
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800558 p_ctx = (DVR_RecordContext_t *)handle;
559 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
560 if (p_ctx == &record_ctx[i])
561 break;
562 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800563 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800564
Pengfei Liub4734232020-01-17 18:25:10 +0800565 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800566 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STOPPED);
567 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800568 DVR_RETURN_IF_FALSE(p_info);/*should support NULL*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800569
Pengfei Liuc181a982020-01-07 19:27:13 +0800570 p_ctx->state = DVR_RECORD_STATE_STOPPED;
pengfei.liuab5a2262020-02-14 17:33:40 +0800571 if (p_ctx->is_vod) {
pengfei.liu8b563292020-02-26 15:49:02 +0800572 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
pengfei.liuab5a2262020-02-14 17:33:40 +0800573 p_ctx->segment_info.duration = 10*1000; //debug, should delete it
574 } else {
575 ret = record_device_stop(p_ctx->dev_handle);
576 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
577 //p_ctx->state = DVR_RECORD_STATE_STOPPED;
578 pthread_join(p_ctx->thread, NULL);
579 }
580
Pengfei Liuc181a982020-01-07 19:27:13 +0800581
Pengfei Liub4734232020-01-17 18:25:10 +0800582 /*Update segment info*/
583 memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
584
585 ret = segment_store_info(p_ctx->segment_handle, p_info);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800586 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800587
588 DVR_DEBUG(1, "%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d",
589 __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets);
590
Pengfei Liuc181a982020-01-07 19:27:13 +0800591 ret = segment_close(p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800592 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800593 return DVR_SUCCESS;
594}
Pengfei Liuc181a982020-01-07 19:27:13 +0800595
Pengfei Liub4734232020-01-17 18:25:10 +0800596int dvr_record_resume_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, uint64_t *p_resume_size)
597{
598 DVR_RecordContext_t *p_ctx;
599 uint32_t i;
pengfei.liuab5a2262020-02-14 17:33:40 +0800600 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800601
602 p_ctx = (DVR_RecordContext_t *)handle;
603 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
604 if (p_ctx == &record_ctx[i])
605 break;
Pengfei Liuc181a982020-01-07 19:27:13 +0800606 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800607 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
608 DVR_RETURN_IF_FALSE(params);
609 DVR_RETURN_IF_FALSE(p_resume_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800610
pengfei.liuab5a2262020-02-14 17:33:40 +0800611 DVR_DEBUG(1, "%s , current state:%d, resume size:%lld", __func__, p_ctx->state, *p_resume_size);
612 ret = dvr_record_start_segment(handle, params);
613 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
614
615 p_ctx->segment_info.size = *p_resume_size;
616
617 return DVR_SUCCESS;
618}
619
620int dvr_record_get_status(DVR_RecordHandle_t handle, DVR_RecordStatus_t *p_status)
621{
622 DVR_RecordContext_t *p_ctx;
623 int i;
624
625 p_ctx = (DVR_RecordContext_t *)handle;
626 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
627 if (p_ctx == &record_ctx[i])
628 break;
629 }
630 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
631 DVR_RETURN_IF_FALSE(p_status);
632
633 //lock
634 p_status->state = p_ctx->state;
635 p_status->info.id = p_ctx->segment_info.id;
636 p_status->info.duration = p_ctx->segment_info.duration;
637 p_status->info.size = p_ctx->segment_info.size;
638 p_status->info.nb_packets = p_ctx->segment_info.size/188;
639
640 return DVR_SUCCESS;
641}
642
643int dvr_record_write(DVR_RecordHandle_t handle, void *buffer, uint32_t len)
644{
645 DVR_RecordContext_t *p_ctx;
646 uint32_t i;
647 off_t pos = 0;
648 int ret;
649 int has_pcr;
650
651 p_ctx = (DVR_RecordContext_t *)handle;
652 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
653 if (p_ctx == &record_ctx[i])
654 break;
655 }
656 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
657 DVR_RETURN_IF_FALSE(buffer);
658 DVR_RETURN_IF_FALSE(len);
659
660 pos = segment_tell_position(p_ctx->segment_handle);
661 has_pcr = record_do_pcr_index(p_ctx, buffer, len);
662 if (has_pcr == 0) {
663 /* Pull VOD record shoud use PCR time index */
664 DVR_DEBUG(1, "%s has no pcr, can NOT do time index", __func__);
665 }
666 ret = segment_write(p_ctx->segment_handle, buffer, len);
667 p_ctx->segment_info.size += len;
668 p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188;
669
Pengfei Liuc181a982020-01-07 19:27:13 +0800670 return DVR_SUCCESS;
671}
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800672
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800673int dvr_record_set_encrypt_callback(DVR_RecordHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800674{
675 DVR_RecordContext_t *p_ctx;
676 uint32_t i;
677
678 p_ctx = (DVR_RecordContext_t *)handle;
679 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
680 if (p_ctx == &record_ctx[i])
681 break;
682 }
683 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
684 DVR_RETURN_IF_FALSE(func);
685
686 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
687 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
688 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
689
690 p_ctx->enc_func = func;
691 p_ctx->enc_userdata = userdata;
692 return DVR_SUCCESS;
693}
694
695int dvr_record_set_secure_buffer(DVR_RecordHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
696{
697 DVR_RecordContext_t *p_ctx;
698 uint32_t i;
699 int ret;
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(p_secure_buf);
708 DVR_RETURN_IF_FALSE(len);
709
710 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
711 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
712 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
713
714 ret = record_device_set_secure_buffer(p_ctx->dev_handle, p_secure_buf, len);
715 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
716
717 p_ctx->is_secure_mode = 1;
718 return ret;
719}