blob: aba21982d2c67fc3106588be475d5b8643825826 [file] [log] [blame]
Pengfei Liuc181a982020-01-07 19:27:13 +08001#include <stdio.h>
Pengfei Liub4734232020-01-17 18:25:10 +08002#include <unistd.h>
3#include <stdlib.h>
Pengfei Liuc181a982020-01-07 19:27:13 +08004#include <pthread.h>
Pengfei Liub4734232020-01-17 18:25:10 +08005#include <string.h>
Pengfei Liu47ed6c92020-01-17 11:23:41 +08006#include "dvr_types.h"
Pengfei Liuc181a982020-01-07 19:27:13 +08007#include "dvr_record.h"
8#include "dvr_crypto.h"
9#include "record_device.h"
10#include "segment.h"
pengfei.liu567d6d82020-04-17 16:48:59 +080011#include <sys/time.h>
Pengfei Liuc181a982020-01-07 19:27:13 +080012
hualing chen5605eed2020-05-26 18:18:06 +080013//#define DEBUG_PERFORMANCE
Pengfei Liuc181a982020-01-07 19:27:13 +080014#define MAX_DVR_RECORD_SESSION_COUNT 2
hualing chen266b9502020-04-04 17:39:39 +080015#define RECORD_BLOCK_SIZE (256 * 1024)
pengfei.liuab5a2262020-02-14 17:33:40 +080016
17/**\brief DVR index file type*/
18typedef enum {
19 DVR_INDEX_TYPE_PCR, /**< DVR index file use pcr*/
20 DVR_INDEX_TYPE_LOCAL_CLOCK, /**< DVR index file use local clock*/
21 DVR_INDEX_TYPE_INVALID /**< DVR index file type invalid type*/
22} DVR_IndexType_t;
23
24/**\brief DVR VOD context*/
25typedef struct {
26 pthread_mutex_t mutex; /**< VOD mutex lock*/
27 pthread_cond_t cond; /**< VOD condition*/
28 void *buffer; /**< VOD buffer*/
29 uint32_t buf_len; /**< VOD buffer len*/
30} DVR_VodContext_t;
31
pengfei.liu07ddc8a2020-03-24 23:36:53 +080032/**\brief DVR record secure mode buffer*/
33typedef struct {
34 uint32_t addr; /**< Secure mode record buffer address*/
35 uint32_t len; /**< Secure mode record buffer length*/
36} DVR_SecureBuffer_t;
37
hualing chenf9867402020-09-23 17:06:20 +080038/**\brief DVR record new dmx secure mode buffer*/
39typedef struct {
40 uint32_t buf_start; /**< Secure mode record buffer address*/
41 uint32_t buf_end; /**< Secure mode record buffer length*/
42 uint32_t data_start; /**< Secure mode record buffer address*/
43 uint32_t data_end; /**< Secure mode record buffer length*/
44} DVR_NewDmxSecureBuffer_t;
45
Pengfei Liub4734232020-01-17 18:25:10 +080046/**\brief DVR record context*/
Pengfei Liuc181a982020-01-07 19:27:13 +080047typedef struct {
Pengfei Liub4734232020-01-17 18:25:10 +080048 pthread_t thread; /**< DVR thread handle*/
49 Record_DeviceHandle_t dev_handle; /**< DVR device handle*/
50 Segment_Handle_t segment_handle; /**< DVR segment handle*/
51 DVR_RecordState_t state; /**< DVR record state*/
52 char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/
53 DVR_RecordSegmentStartParams_t segment_params; /**< DVR record start parameters*/
54 DVR_RecordSegmentInfo_t segment_info; /**< DVR record current segment info*/
55 size_t notification_size; /**< DVR record nogification size*/
56 DVR_RecordEventFunction_t event_notify_fn; /**< DVR record event notify function*/
57 void *event_userdata; /**< DVR record event userdata*/
pengfei.liuab5a2262020-02-14 17:33:40 +080058 //DVR_VodContext_t vod; /**< DVR record vod context*/
59 int is_vod; /**< Indicate current mode is VOD record mode*/
pengfei.liu27cc4ec2020-04-03 16:28:16 +080060 DVR_CryptoFunction_t enc_func; /**< Encrypt function*/
pengfei.liu07ddc8a2020-03-24 23:36:53 +080061 void *enc_userdata; /**< Encrypt userdata*/
62 int is_secure_mode; /**< Record session run in secure pipeline */
hualing chen2615aa82020-04-02 21:32:51 +080063 size_t last_send_size; /**< Last send notify segment size */
pengfei.liufda2a972020-04-09 14:47:15 +080064 uint32_t block_size; /**< DVR record block size */
hualing chenf9867402020-09-23 17:06:20 +080065 DVR_Bool_t is_new_dmx; /**< DVR is used new dmx driver */
Pengfei Liuc181a982020-01-07 19:27:13 +080066} DVR_RecordContext_t;
67
68static DVR_RecordContext_t record_ctx[MAX_DVR_RECORD_SESSION_COUNT] = {
69 {
Pengfei Liub4734232020-01-17 18:25:10 +080070 .state = DVR_RECORD_STATE_CLOSED
Pengfei Liuc181a982020-01-07 19:27:13 +080071 },
72 {
Pengfei Liub4734232020-01-17 18:25:10 +080073 .state = DVR_RECORD_STATE_CLOSED
Pengfei Liuc181a982020-01-07 19:27:13 +080074 }
75};
76
Zhiqiang Han51646a02020-04-05 18:43:22 +080077static int record_is_valid_pid(DVR_RecordContext_t *p_ctx, int pid)
78{
79 int i;
80
81 for (i = 0; i < p_ctx->segment_info.nb_pids; i++) {
82 if (pid == p_ctx->segment_info.pids[i].pid)
83 return 1;
84 }
85 return 0;
86}
87
pengfei.liuab5a2262020-02-14 17:33:40 +080088static int record_save_pcr(DVR_RecordContext_t *p_ctx, uint8_t *buf, loff_t pos)
89{
90 uint8_t *p = buf;
91 int len;
92 uint8_t afc;
93 uint64_t pcr = 0;
94 int has_pcr = 0;
95 int pid;
96 int adp_field_len;
97
98 pid = ((p[1] & 0x1f) << 8) | p[2];
Zhiqiang Han51646a02020-04-05 18:43:22 +080099 if (pid == 0x1fff || !record_is_valid_pid(p_ctx, pid))
pengfei.liuab5a2262020-02-14 17:33:40 +0800100 return has_pcr;
101
102 //scramble = p[3] >> 6;
103 //cc = p[3] & 0x0f;
104 afc = (p[3] >> 4) & 0x03;
105
106 p += 4;
107 len = 184;
108
109 if (afc & 2) {
110 adp_field_len = p[0];
111 /* Skip adaptation len */
112 p++;
113 len--;
114 /* Parse pcr field, see 13818 spec table I-2-6,adaptation_field */
hualing chen5605eed2020-05-26 18:18:06 +0800115 if (p[0] & 0x10 && len >= 6 && adp_field_len >= 6) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800116 /* get pcr value,pcr is 33bit value */
117 pcr = (((uint64_t)(p[1])) << 25)
118 | (((uint64_t)p[2]) << 17)
119 | (((uint64_t)(p[3])) << 9)
120 | (((uint64_t)p[4]) << 1)
121 | ((((uint64_t)p[5]) & 0x80) >> 7);
122 has_pcr = 1;
123 }
124
125 p += adp_field_len;
126 len -= adp_field_len;
127
128 if (len < 0) {
129 DVR_DEBUG(1, "parser pcr: illegal adaptation field length");
130 return 0;
131 }
132 }
133
134 if (has_pcr) {
135 segment_update_pts(p_ctx->segment_handle, pcr/90, pos);
136 }
137 return has_pcr;
138}
139
140static int record_do_pcr_index(DVR_RecordContext_t *p_ctx, uint8_t *buf, int len)
141{
142 uint8_t *p = buf;
143 int left = len;
144 loff_t pos;
145 int has_pcr = 0;
146
147 pos = segment_tell_position(p_ctx->segment_handle);
148 while (left >= 188) {
149 if (*p == 0x47) {
150 has_pcr |= record_save_pcr(p_ctx, p, pos);
151 p += 188;
152 left -= 188;
153 pos += 188;
154 } else {
155 p++;
156 left --;
157 pos++;
158 }
159 }
160 return has_pcr;
161}
162
pengfei.liu567d6d82020-04-17 16:48:59 +0800163static int get_diff_time(struct timeval start_tv, struct timeval end_tv)
164{
165 return end_tv.tv_sec * 1000 + end_tv.tv_usec / 1000 - start_tv.tv_sec * 1000 - start_tv.tv_usec / 1000;
166}
167
Pengfei Liuc181a982020-01-07 19:27:13 +0800168void *record_thread(void *arg)
169{
170 DVR_RecordContext_t *p_ctx = (DVR_RecordContext_t *)arg;
171 ssize_t len;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800172 uint8_t *buf, *buf_out;
pengfei.liufda2a972020-04-09 14:47:15 +0800173 uint32_t block_size = p_ctx->block_size;
pengfei.liuab5a2262020-02-14 17:33:40 +0800174 loff_t pos = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800175 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800176 struct timespec start_ts, end_ts;
177 DVR_RecordStatus_t record_status;
pengfei.liuab5a2262020-02-14 17:33:40 +0800178 int has_pcr;
179 int index_type = DVR_INDEX_TYPE_INVALID;
hualing chen87072a82020-03-12 16:20:12 +0800180 time_t pre_time = 0;
hualing chen2932d372020-04-29 13:44:00 +0800181 #define DVR_STORE_INFO_TIME (400)
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800182 DVR_SecureBuffer_t secure_buf;
hualing chenf9867402020-09-23 17:06:20 +0800183 DVR_NewDmxSecureBuffer_t new_dmx_secure_buf;
Pengfei Liuc181a982020-01-07 19:27:13 +0800184
Pengfei Liub4734232020-01-17 18:25:10 +0800185 buf = (uint8_t *)malloc(block_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800186 if (!buf) {
187 DVR_DEBUG(1, "%s, malloc failed", __func__);
188 return NULL;
189 }
190
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800191 buf_out = (uint8_t *)malloc(block_size + 188);
192 if (!buf_out) {
193 DVR_DEBUG(1, "%s, malloc failed", __func__);
Pengfei Liufaf38e42020-05-22 00:28:02 +0800194 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800195 return NULL;
196 }
197
hualing chen266b9502020-04-04 17:39:39 +0800198 memset(&record_status, 0, sizeof(record_status));
199 record_status.state = DVR_RECORD_STATE_STARTED;
pengfei.liufda2a972020-04-09 14:47:15 +0800200 if (p_ctx->event_notify_fn) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800201 record_status.info.id = p_ctx->segment_info.id;
pengfei.liufda2a972020-04-09 14:47:15 +0800202 p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata);
hualing chen4b7c15d2020-04-07 16:13:48 +0800203 DVR_DEBUG(1, "%s line %d notify record status, state:%d id=%lld",
204 __func__,__LINE__, record_status.state, p_ctx->segment_info.id);
pengfei.liufda2a972020-04-09 14:47:15 +0800205 }
206 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 +0800207
Pengfei Liub4734232020-01-17 18:25:10 +0800208 clock_gettime(CLOCK_MONOTONIC, &start_ts);
pengfei.liu567d6d82020-04-17 16:48:59 +0800209
210 struct timeval t1, t2, t3, t4, t5, t6, t7;
Pengfei Liuc181a982020-01-07 19:27:13 +0800211 while (p_ctx->state == DVR_RECORD_STATE_STARTED) {
pengfei.liu567d6d82020-04-17 16:48:59 +0800212 gettimeofday(&t1, NULL);
hualing chenc70a8df2020-05-12 19:23:11 +0800213
pengfei.liuab5a2262020-02-14 17:33:40 +0800214 /* data from dmx, normal dvr case */
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800215 if (p_ctx->is_secure_mode) {
hualing chenf9867402020-09-23 17:06:20 +0800216 if (p_ctx->is_new_dmx) {
217 memset(&new_dmx_secure_buf, 0, sizeof(new_dmx_secure_buf));
218 len = record_device_read(p_ctx->dev_handle, &new_dmx_secure_buf, sizeof(new_dmx_secure_buf), 1000);
219 } else {
220 memset(&secure_buf, 0, sizeof(secure_buf));
221 len = record_device_read(p_ctx->dev_handle, &secure_buf, sizeof(secure_buf), 1000);
222 }
223 if (len != DVR_FAILURE) {
224 DVR_DEBUG(1, "%s, secure_buf:%#x, size:%#x", __func__, secure_buf.addr, secure_buf.len);
225 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800226 } else {
227 len = record_device_read(p_ctx->dev_handle, buf, block_size, 1000);
228 }
Pengfei Liub4734232020-01-17 18:25:10 +0800229 if (len == DVR_FAILURE) {
hualing chen6c126382020-04-13 15:47:51 +0800230 //usleep(10*1000);
hualing chen2932d372020-04-29 13:44:00 +0800231 DVR_DEBUG(1, "%s, start_read error", __func__);
Pengfei Liub4734232020-01-17 18:25:10 +0800232 continue;
233 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800234 gettimeofday(&t2, NULL);
hualing chenc70a8df2020-05-12 19:23:11 +0800235
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800236 /* Got data from device, record it */
237 if (p_ctx->enc_func) {
238 /* Encrypt record data */
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800239 DVR_CryptoParams_t crypto_params;
240
241 memset(&crypto_params, 0, sizeof(crypto_params));
242 crypto_params.type = DVR_CRYPTO_TYPE_ENCRYPT;
hualing chen7a56cba2020-04-14 14:09:27 +0800243 memcpy(crypto_params.location, p_ctx->location, sizeof(p_ctx->location));
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800244 crypto_params.segment_id = p_ctx->segment_info.id;
245 crypto_params.offset = p_ctx->segment_info.size;
246
247 if (p_ctx->is_secure_mode) {
248 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_SECURE;
hualing chenf9867402020-09-23 17:06:20 +0800249 if (p_ctx->is_new_dmx) {
250 crypto_params.input_buffer.addr = new_dmx_secure_buf.data_start;
251 crypto_params.input_buffer.size = new_dmx_secure_buf.data_end - new_dmx_secure_buf.data_start;
252 } else {
253 crypto_params.input_buffer.addr = secure_buf.addr;
254 crypto_params.input_buffer.size = secure_buf.len;
255 }
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800256 } else {
257 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
258 crypto_params.input_buffer.addr = (size_t)buf;
259 crypto_params.input_buffer.size = len;
260 }
261
262 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL;
263 crypto_params.output_buffer.addr = (size_t)buf_out;
264 crypto_params.output_buffer.size = block_size + 188;
265
266 p_ctx->enc_func(&crypto_params, p_ctx->enc_userdata);
pengfei.liu567d6d82020-04-17 16:48:59 +0800267 gettimeofday(&t3, NULL);
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800268 /* Out buffer length may not equal in buffer length */
hualing chenf9867402020-09-23 17:06:20 +0800269 if (crypto_params.output_size > 0) {
270 ret = segment_write(p_ctx->segment_handle, buf_out, crypto_params.output_size);
271 len = crypto_params.output_size;
272 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800273 } else {
pengfei.liu567d6d82020-04-17 16:48:59 +0800274 gettimeofday(&t3, NULL);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800275 ret = segment_write(p_ctx->segment_handle, buf, len);
276 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800277 gettimeofday(&t4, NULL);
hualing chen626204e2020-04-07 11:55:08 +0800278 //add DVR_RECORD_EVENT_WRITE_ERROR event if write error
pengfei.liufda2a972020-04-09 14:47:15 +0800279 if (ret == -1 && len > 0 && p_ctx->event_notify_fn) {
hualing chen626204e2020-04-07 11:55:08 +0800280 //send write event
hualing chen9b434f02020-06-10 15:06:54 +0800281 if (p_ctx->event_notify_fn) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800282 memset(&record_status, 0, sizeof(record_status));
hualing chenfbf8e022020-06-15 13:43:11 +0800283 DVR_DEBUG(1, "%s,send event write error", __func__,__LINE__);
hualing chen4b7c15d2020-04-07 16:13:48 +0800284 p_ctx->event_notify_fn(DVR_RECORD_EVENT_WRITE_ERROR, &record_status, p_ctx->event_userdata);
285 }
hualing chenc70a8df2020-05-12 19:23:11 +0800286 DVR_DEBUG(1, "%s,write error %d", __func__,__LINE__);
hualing chen626204e2020-04-07 11:55:08 +0800287 goto end;
288 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800289 /* Do time index */
290 uint8_t *index_buf = p_ctx->enc_func ? buf_out : buf;
pengfei.liuab5a2262020-02-14 17:33:40 +0800291 pos = segment_tell_position(p_ctx->segment_handle);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800292 has_pcr = record_do_pcr_index(p_ctx, index_buf, len);
pengfei.liuab5a2262020-02-14 17:33:40 +0800293 if (has_pcr == 0 && index_type == DVR_INDEX_TYPE_INVALID) {
294 clock_gettime(CLOCK_MONOTONIC, &end_ts);
295 if ((end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) -
296 (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) > 40) {
297 /* PCR interval threshlod > 40 ms*/
298 DVR_DEBUG(1, "%s use local clock time index", __func__);
299 index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
300 }
301 } else if (has_pcr && index_type == DVR_INDEX_TYPE_INVALID){
302 DVR_DEBUG(1, "%s use pcr time index", __func__);
303 index_type = DVR_INDEX_TYPE_PCR;
Pengfei Liuc181a982020-01-07 19:27:13 +0800304 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800305 gettimeofday(&t5, NULL);
pengfei.liuab5a2262020-02-14 17:33:40 +0800306
307 /* Update segment info */
Pengfei Liub4734232020-01-17 18:25:10 +0800308 p_ctx->segment_info.size += len;
pengfei.liuab5a2262020-02-14 17:33:40 +0800309 /*Duration need use pcr to calculate, todo...*/
310 if (index_type == DVR_INDEX_TYPE_PCR) {
pengfei.liu8b563292020-02-26 15:49:02 +0800311 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
hualing chen87072a82020-03-12 16:20:12 +0800312 if (pre_time == 0)
313 pre_time = p_ctx->segment_info.duration;
pengfei.liuab5a2262020-02-14 17:33:40 +0800314 } else if (index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) {
315 clock_gettime(CLOCK_MONOTONIC, &end_ts);
316 p_ctx->segment_info.duration = (end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) -
317 (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000);
hualing chen87072a82020-03-12 16:20:12 +0800318 if (pre_time == 0)
319 pre_time = p_ctx->segment_info.duration;
pengfei.liuab5a2262020-02-14 17:33:40 +0800320 segment_update_pts(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
321 } else {
322 DVR_DEBUG(1, "%s can NOT do time index", __func__);
323 }
324 p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188;
Pengfei Liub4734232020-01-17 18:25:10 +0800325
hualing chen87072a82020-03-12 16:20:12 +0800326 if (p_ctx->segment_info.duration - pre_time > DVR_STORE_INFO_TIME) {
327 pre_time = p_ctx->segment_info.duration + DVR_STORE_INFO_TIME;
hualing chenf9867402020-09-23 17:06:20 +0800328 segment_store_info(p_ctx->segment_handle, &(p_ctx->segment_info));
hualing chen87072a82020-03-12 16:20:12 +0800329 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800330 gettimeofday(&t6, NULL);
hualing chen87072a82020-03-12 16:20:12 +0800331 /*Event notification*/
Pengfei Liub4734232020-01-17 18:25:10 +0800332 if (p_ctx->notification_size &&
333 p_ctx->event_notify_fn &&
hualing chen2615aa82020-04-02 21:32:51 +0800334 /*!(p_ctx->segment_info.size % p_ctx->notification_size)*/
335 (p_ctx->segment_info.size -p_ctx->last_send_size) >= p_ctx->notification_size&&
pengfei.liuab5a2262020-02-14 17:33:40 +0800336 p_ctx->segment_info.duration > 0) {
Pengfei Liub4734232020-01-17 18:25:10 +0800337 memset(&record_status, 0, sizeof(record_status));
pengfei.liuab5a2262020-02-14 17:33:40 +0800338 //clock_gettime(CLOCK_MONOTONIC, &end_ts);
hualing chen2615aa82020-04-02 21:32:51 +0800339 p_ctx->last_send_size = p_ctx->segment_info.size;
Pengfei Liub4734232020-01-17 18:25:10 +0800340 record_status.state = p_ctx->state;
341 record_status.info.id = p_ctx->segment_info.id;
pengfei.liuab5a2262020-02-14 17:33:40 +0800342 record_status.info.duration = p_ctx->segment_info.duration;
Pengfei Liub4734232020-01-17 18:25:10 +0800343 record_status.info.size = p_ctx->segment_info.size;
344 record_status.info.nb_packets = p_ctx->segment_info.size/188;
345 p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata);
346 DVR_DEBUG(1, "%s notify record status, state:%d, id:%lld, duration:%ld ms, size:%zu",
347 __func__, record_status.state, record_status.info.id, record_status.info.duration, record_status.info.size);
348 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800349 gettimeofday(&t7, NULL);
350#ifdef DEBUG_PERFORMANCE
hualing chen2932d372020-04-29 13:44:00 +0800351 DVR_DEBUG(1, "record count, read:%dms, encrypt:%dms, write:%dms, index:%dms, store:%dms, notify:%dms total:%dms read len:%zd ",
pengfei.liu567d6d82020-04-17 16:48:59 +0800352 get_diff_time(t1, t2), get_diff_time(t2, t3), get_diff_time(t3, t4), get_diff_time(t4, t5),
hualing chen2932d372020-04-29 13:44:00 +0800353 get_diff_time(t5, t6), get_diff_time(t6, t7), get_diff_time(t1, t5), len);
pengfei.liu567d6d82020-04-17 16:48:59 +0800354#endif
Pengfei Liuc181a982020-01-07 19:27:13 +0800355 }
hualing chen626204e2020-04-07 11:55:08 +0800356end:
Pengfei Liub4734232020-01-17 18:25:10 +0800357 free((void *)buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800358 free((void *)buf_out);
Pengfei Liub4734232020-01-17 18:25:10 +0800359 DVR_DEBUG(1, "exit %s", __func__);
Pengfei Liuc181a982020-01-07 19:27:13 +0800360 return NULL;
361}
362
363int dvr_record_open(DVR_RecordHandle_t *p_handle, DVR_RecordOpenParams_t *params)
364{
365 DVR_RecordContext_t *p_ctx;
366 Record_DeviceOpenParams_t dev_open_params;
367 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800368 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800369
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800370 DVR_RETURN_IF_FALSE(p_handle);
371 DVR_RETURN_IF_FALSE(params);
Pengfei Liuc181a982020-01-07 19:27:13 +0800372
373 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
Pengfei Liub4734232020-01-17 18:25:10 +0800374 if (record_ctx[i].state == DVR_RECORD_STATE_CLOSED) {
Pengfei Liuc181a982020-01-07 19:27:13 +0800375 break;
376 }
377 }
Pengfei Liufaf38e42020-05-22 00:28:02 +0800378 DVR_RETURN_IF_FALSE(i < MAX_DVR_RECORD_SESSION_COUNT);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800379 DVR_RETURN_IF_FALSE(record_ctx[i].state == DVR_RECORD_STATE_CLOSED);
Pengfei Liuc181a982020-01-07 19:27:13 +0800380 p_ctx = &record_ctx[i];
hualing chenfbf8e022020-06-15 13:43:11 +0800381 DVR_DEBUG(1, "%s , current state:%d, dmx_id:%d, notification_size:%zu ", __func__,
Pengfei Liub4734232020-01-17 18:25:10 +0800382 p_ctx->state, params->dmx_dev_id, params->notification_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800383
Pengfei Liub4734232020-01-17 18:25:10 +0800384 /*Process event params*/
385 p_ctx->notification_size = params->notification_size;
386 p_ctx->event_notify_fn = params->event_fn;
387 p_ctx->event_userdata = params->event_userdata;
hualing chen2615aa82020-04-02 21:32:51 +0800388 p_ctx->last_send_size = 0;
hualing chenf9867402020-09-23 17:06:20 +0800389 //check is new driver
390 p_ctx->is_new_dmx = dvr_check_dmx_isNew();
Pengfei Liub4734232020-01-17 18:25:10 +0800391 /*Process crypto params, todo*/
Pengfei Liub4734232020-01-17 18:25:10 +0800392 memset((void *)&dev_open_params, 0, sizeof(dev_open_params));
pengfei.liuab5a2262020-02-14 17:33:40 +0800393 if (params->data_from_memory) {
394 /* data from memory, VOD case */
395 p_ctx->is_vod = 1;
396 } else {
397 p_ctx->is_vod = 0;
398 /* data from dmx, normal dvr case */
hualing chen958fe4c2020-03-24 17:35:07 +0800399 dev_open_params.dmx_dev_id = params->dmx_dev_id;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800400 dev_open_params.buf_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE);
Pengfei Liub4734232020-01-17 18:25:10 +0800401
pengfei.liuab5a2262020-02-14 17:33:40 +0800402 ret = record_device_open(&p_ctx->dev_handle, &dev_open_params);
403 if (ret != DVR_SUCCESS) {
404 DVR_DEBUG(1, "%s, open record devices failed", __func__);
405 return DVR_FAILURE;
406 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800407 }
408
pengfei.liufda2a972020-04-09 14:47:15 +0800409 p_ctx->block_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800410 p_ctx->enc_func = NULL;
411 p_ctx->enc_userdata = NULL;
412 p_ctx->is_secure_mode = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800413 p_ctx->state = DVR_RECORD_STATE_OPENED;
414
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800415 *p_handle = p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800416 return DVR_SUCCESS;
417}
418
419int dvr_record_close(DVR_RecordHandle_t handle)
420{
421 DVR_RecordContext_t *p_ctx;
422 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800423 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800424
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800425 p_ctx = (DVR_RecordContext_t *)handle;
426 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
427 if (p_ctx == &record_ctx[i])
428 break;
429 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800430 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800431
Pengfei Liub4734232020-01-17 18:25:10 +0800432 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800433 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
Pengfei Liuc181a982020-01-07 19:27:13 +0800434
pengfei.liuab5a2262020-02-14 17:33:40 +0800435 if (p_ctx->is_vod) {
436 ret = DVR_SUCCESS;
437 } else {
438 ret = record_device_close(p_ctx->dev_handle);
439 if (ret != DVR_SUCCESS) {
440 DVR_DEBUG(1, "%s, failed", __func__);
441 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800442 }
443
Pengfei Liub4734232020-01-17 18:25:10 +0800444 p_ctx->state = DVR_RECORD_STATE_CLOSED;
Pengfei Liuc181a982020-01-07 19:27:13 +0800445 return ret;
446}
447
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800448#if 0
Pengfei Liuc181a982020-01-07 19:27:13 +0800449int dvr_record_register_encryption(DVR_RecordHandle_t handle,
450 DVR_CryptoFunction_t cb,
451 DVR_CryptoParams_t params,
452 void *userdata)
453{
454 return DVR_SUCCESS;
455}
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800456#endif
Pengfei Liuc181a982020-01-07 19:27:13 +0800457
458int dvr_record_start_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params)
459{
460 DVR_RecordContext_t *p_ctx;
461 Segment_OpenParams_t open_params;
462 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800463 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800464
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800465 p_ctx = (DVR_RecordContext_t *)handle;
466 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
467 if (p_ctx == &record_ctx[i])
468 break;
469 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800470 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800471
Pengfei Liub4734232020-01-17 18:25:10 +0800472 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800473 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
474 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
475 DVR_RETURN_IF_FALSE(params);
Pengfei Liuc181a982020-01-07 19:27:13 +0800476
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800477 DVR_RETURN_IF_FALSE(strlen((const char *)params->location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liuc181a982020-01-07 19:27:13 +0800478 memset(&open_params, 0, sizeof(open_params));
hualing chen7a56cba2020-04-14 14:09:27 +0800479 memcpy(open_params.location, params->location, sizeof(params->location));
Pengfei Liuc181a982020-01-07 19:27:13 +0800480 open_params.segment_id = params->segment.segment_id;
481 open_params.mode = SEGMENT_MODE_WRITE;
482
483 ret = segment_open(&open_params, &p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800484 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800485
Pengfei Liub4734232020-01-17 18:25:10 +0800486 /*process params*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800487 {
hualing chen7a56cba2020-04-14 14:09:27 +0800488 memcpy(p_ctx->location, params->location, sizeof(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800489 //need all params??
Pengfei Liuc181a982020-01-07 19:27:13 +0800490 memcpy(&p_ctx->segment_params, &params->segment, sizeof(params->segment));
Pengfei Liub4734232020-01-17 18:25:10 +0800491 /*save current segment info*/
492 memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info));
493 p_ctx->segment_info.id = params->segment.segment_id;
494 p_ctx->segment_info.nb_pids = params->segment.nb_pids;
495 memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800496 }
497
pengfei.liuab5a2262020-02-14 17:33:40 +0800498 if (!p_ctx->is_vod) {
499 /* normal dvr case */
500 for (i = 0; i < params->segment.nb_pids; i++) {
501 ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
502 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
503 }
504 ret = record_device_start(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800505 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800506 }
507
Zhiqiang Han5ebad992020-04-28 18:19:59 +0800508 ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info);
509
Pengfei Liuc181a982020-01-07 19:27:13 +0800510 p_ctx->state = DVR_RECORD_STATE_STARTED;
pengfei.liuab5a2262020-02-14 17:33:40 +0800511 if (!p_ctx->is_vod)
512 pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +0800513
Pengfei Liuc181a982020-01-07 19:27:13 +0800514 return DVR_SUCCESS;
515}
516
517int dvr_record_next_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, DVR_RecordSegmentInfo_t *p_info)
518{
519 DVR_RecordContext_t *p_ctx;
Pengfei Liub4734232020-01-17 18:25:10 +0800520 Segment_OpenParams_t open_params;
Pengfei Liuc181a982020-01-07 19:27:13 +0800521 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800522 uint32_t i;
hualing chen2932d372020-04-29 13:44:00 +0800523 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800524
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800525 p_ctx = (DVR_RecordContext_t *)handle;
526 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
527 if (p_ctx == &record_ctx[i])
528 break;
529 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800530 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800531
Pengfei Liub4734232020-01-17 18:25:10 +0800532 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800533 DVR_RETURN_IF_FALSE(p_ctx->state == DVR_RECORD_STATE_STARTED);
534 //DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
535 DVR_RETURN_IF_FALSE(params);
536 DVR_RETURN_IF_FALSE(p_info);
pengfei.liuab5a2262020-02-14 17:33:40 +0800537 DVR_RETURN_IF_FALSE(!p_ctx->is_vod);
Pengfei Liuc181a982020-01-07 19:27:13 +0800538
539 /*Stop the on going record segment*/
Pengfei Liub4734232020-01-17 18:25:10 +0800540 //ret = record_device_stop(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800541 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800542 p_ctx->state = DVR_RECORD_STATE_STOPPED;
543 pthread_join(p_ctx->thread, NULL);
544
hualing chen2932d372020-04-29 13:44:00 +0800545 //add index file store
546 pos = segment_tell_position(p_ctx->segment_handle);
547 segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
548
549 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
Pengfei Liub4734232020-01-17 18:25:10 +0800550 /*Update segment info*/
551 memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
552
553 ret = segment_store_info(p_ctx->segment_handle, p_info);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800554 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800555
hualing chena540a7e2020-03-27 16:44:05 +0800556 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",
557 __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 +0800558
Pengfei Liub4734232020-01-17 18:25:10 +0800559 /*Close current segment*/
560 ret = segment_close(p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800561 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800562 /*Open the new record segment*/
563 memset(&open_params, 0, sizeof(open_params));
hualing chen7a56cba2020-04-14 14:09:27 +0800564 memcpy(open_params.location, p_ctx->location, sizeof(p_ctx->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800565 open_params.segment_id = params->segment.segment_id;
566 open_params.mode = SEGMENT_MODE_WRITE;
hualing chen7a56cba2020-04-14 14:09:27 +0800567 DVR_DEBUG(1, "%s: p_ctx->location:%s params->location:%s", __func__, p_ctx->location,params->location);
Pengfei Liub4734232020-01-17 18:25:10 +0800568
569 ret = segment_open(&open_params, &p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800570 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800571 /*process params*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800572 {
Pengfei Liub4734232020-01-17 18:25:10 +0800573 //need all params??
Pengfei Liuc181a982020-01-07 19:27:13 +0800574 memcpy(&p_ctx->segment_params, &params->segment, sizeof(params->segment));
Pengfei Liub4734232020-01-17 18:25:10 +0800575 /*save current segment info*/
576 memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info));
577 p_ctx->segment_info.id = params->segment.segment_id;
578 memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800579 }
580
Pengfei Liub4734232020-01-17 18:25:10 +0800581 p_ctx->segment_info.nb_pids = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800582 for (i = 0; i < params->segment.nb_pids; i++) {
Pengfei Liub4734232020-01-17 18:25:10 +0800583 switch (params->segment.pid_action[i]) {
584 case DVR_RECORD_PID_CREATE:
585 DVR_DEBUG(1, "%s create pid:%d", __func__, params->segment.pids[i].pid);
586 ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
587 p_ctx->segment_info.nb_pids++;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800588 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800589 break;
590 case DVR_RECORD_PID_KEEP:
591 DVR_DEBUG(1, "%s keep pid:%d", __func__, params->segment.pids[i].pid);
592 p_ctx->segment_info.nb_pids++;
593 break;
594 case DVR_RECORD_PID_CLOSE:
595 DVR_DEBUG(1, "%s close pid:%d", __func__, params->segment.pids[i].pid);
596 ret = record_device_remove_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800597 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800598 break;
599 default:
600 DVR_DEBUG(1, "%s wrong action pid:%d", __func__, params->segment.pids[i].pid);
601 return DVR_FAILURE;
602 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800603 }
604
Pengfei Liub4734232020-01-17 18:25:10 +0800605 //ret = record_device_start(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800606 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
hualing chen4b7c15d2020-04-07 16:13:48 +0800607 /*Update segment info*/
608 ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info);
Pengfei Liuc181a982020-01-07 19:27:13 +0800609
Pengfei Liuc181a982020-01-07 19:27:13 +0800610 p_ctx->state = DVR_RECORD_STATE_STARTED;
Zhiqiang Han0d60f2b2020-05-26 14:39:54 +0800611 pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800612 return DVR_SUCCESS;
613}
614
615int dvr_record_stop_segment(DVR_RecordHandle_t handle, DVR_RecordSegmentInfo_t *p_info)
616{
617 DVR_RecordContext_t *p_ctx;
618 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800619 uint32_t i;
hualing chen2932d372020-04-29 13:44:00 +0800620 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800621
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800622 p_ctx = (DVR_RecordContext_t *)handle;
623 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
624 if (p_ctx == &record_ctx[i])
625 break;
626 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800627 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800628
Pengfei Liub4734232020-01-17 18:25:10 +0800629 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800630 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STOPPED);
631 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800632 DVR_RETURN_IF_FALSE(p_info);/*should support NULL*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800633
Pengfei Liuc181a982020-01-07 19:27:13 +0800634 p_ctx->state = DVR_RECORD_STATE_STOPPED;
pengfei.liuab5a2262020-02-14 17:33:40 +0800635 if (p_ctx->is_vod) {
pengfei.liu8b563292020-02-26 15:49:02 +0800636 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
pengfei.liuab5a2262020-02-14 17:33:40 +0800637 p_ctx->segment_info.duration = 10*1000; //debug, should delete it
638 } else {
639 ret = record_device_stop(p_ctx->dev_handle);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800640 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
641 if (ret != DVR_SUCCESS)
642 goto end;
pengfei.liuab5a2262020-02-14 17:33:40 +0800643 //p_ctx->state = DVR_RECORD_STATE_STOPPED;
644 pthread_join(p_ctx->thread, NULL);
645 }
646
hualing chen2932d372020-04-29 13:44:00 +0800647 //add index file store
648 pos = segment_tell_position(p_ctx->segment_handle);
649 segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
hualing chene41f4372020-06-06 16:29:17 +0800650 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +0800651
Pengfei Liub4734232020-01-17 18:25:10 +0800652 /*Update segment info*/
653 memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
654
655 ret = segment_store_info(p_ctx->segment_handle, p_info);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800656 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
657 if (ret != DVR_SUCCESS)
658 goto end;
Pengfei Liub4734232020-01-17 18:25:10 +0800659
660 DVR_DEBUG(1, "%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d",
661 __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets);
662
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800663end:
Pengfei Liuc181a982020-01-07 19:27:13 +0800664 ret = segment_close(p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800665 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800666 return DVR_SUCCESS;
667}
Pengfei Liuc181a982020-01-07 19:27:13 +0800668
Pengfei Liub4734232020-01-17 18:25:10 +0800669int dvr_record_resume_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, uint64_t *p_resume_size)
670{
671 DVR_RecordContext_t *p_ctx;
672 uint32_t i;
pengfei.liuab5a2262020-02-14 17:33:40 +0800673 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800674
675 p_ctx = (DVR_RecordContext_t *)handle;
676 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
677 if (p_ctx == &record_ctx[i])
678 break;
Pengfei Liuc181a982020-01-07 19:27:13 +0800679 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800680 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
681 DVR_RETURN_IF_FALSE(params);
682 DVR_RETURN_IF_FALSE(p_resume_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800683
pengfei.liuab5a2262020-02-14 17:33:40 +0800684 DVR_DEBUG(1, "%s , current state:%d, resume size:%lld", __func__, p_ctx->state, *p_resume_size);
685 ret = dvr_record_start_segment(handle, params);
686 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
687
688 p_ctx->segment_info.size = *p_resume_size;
689
690 return DVR_SUCCESS;
691}
692
693int dvr_record_get_status(DVR_RecordHandle_t handle, DVR_RecordStatus_t *p_status)
694{
695 DVR_RecordContext_t *p_ctx;
696 int i;
697
698 p_ctx = (DVR_RecordContext_t *)handle;
699 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
700 if (p_ctx == &record_ctx[i])
701 break;
702 }
703 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
704 DVR_RETURN_IF_FALSE(p_status);
705
706 //lock
707 p_status->state = p_ctx->state;
708 p_status->info.id = p_ctx->segment_info.id;
709 p_status->info.duration = p_ctx->segment_info.duration;
710 p_status->info.size = p_ctx->segment_info.size;
711 p_status->info.nb_packets = p_ctx->segment_info.size/188;
712
713 return DVR_SUCCESS;
714}
715
716int dvr_record_write(DVR_RecordHandle_t handle, void *buffer, uint32_t len)
717{
718 DVR_RecordContext_t *p_ctx;
719 uint32_t i;
720 off_t pos = 0;
721 int ret;
722 int has_pcr;
723
724 p_ctx = (DVR_RecordContext_t *)handle;
725 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
726 if (p_ctx == &record_ctx[i])
727 break;
728 }
729 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
730 DVR_RETURN_IF_FALSE(buffer);
731 DVR_RETURN_IF_FALSE(len);
732
733 pos = segment_tell_position(p_ctx->segment_handle);
734 has_pcr = record_do_pcr_index(p_ctx, buffer, len);
735 if (has_pcr == 0) {
736 /* Pull VOD record shoud use PCR time index */
737 DVR_DEBUG(1, "%s has no pcr, can NOT do time index", __func__);
738 }
739 ret = segment_write(p_ctx->segment_handle, buffer, len);
hualing chen2932d372020-04-29 13:44:00 +0800740 if (ret != len) {
741 DVR_DEBUG(1, "%s write error ret:%d len:%d", __func__, ret, len);
742 }
pengfei.liuab5a2262020-02-14 17:33:40 +0800743 p_ctx->segment_info.size += len;
744 p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188;
745
Pengfei Liuc181a982020-01-07 19:27:13 +0800746 return DVR_SUCCESS;
747}
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800748
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800749int dvr_record_set_encrypt_callback(DVR_RecordHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800750{
751 DVR_RecordContext_t *p_ctx;
752 uint32_t i;
753
754 p_ctx = (DVR_RecordContext_t *)handle;
755 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
756 if (p_ctx == &record_ctx[i])
757 break;
758 }
759 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
760 DVR_RETURN_IF_FALSE(func);
761
762 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
763 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
764 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
765
766 p_ctx->enc_func = func;
767 p_ctx->enc_userdata = userdata;
768 return DVR_SUCCESS;
769}
770
771int dvr_record_set_secure_buffer(DVR_RecordHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
772{
773 DVR_RecordContext_t *p_ctx;
774 uint32_t i;
775 int ret;
776
777 p_ctx = (DVR_RecordContext_t *)handle;
778 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
779 if (p_ctx == &record_ctx[i])
780 break;
781 }
782 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
783 DVR_RETURN_IF_FALSE(p_secure_buf);
784 DVR_RETURN_IF_FALSE(len);
785
786 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
787 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
788 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
789
790 ret = record_device_set_secure_buffer(p_ctx->dev_handle, p_secure_buf, len);
791 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
792
793 p_ctx->is_secure_mode = 1;
794 return ret;
795}