blob: e571554dfd7065de71872f1f54a2ef8a25b031c3 [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 chenc7aa4c82021-02-03 15:41:37 +080017//#define DEBUG_PERFORMANCE
Yahui Han10993892021-11-02 14:27:33 +080018#define MAX_DVR_RECORD_SESSION_COUNT 4
hualing chen266b9502020-04-04 17:39:39 +080019#define RECORD_BLOCK_SIZE (256 * 1024)
Yahui Hance15e9c2020-12-08 18:08:32 +080020#define NEW_DEVICE_RECORD_BLOCK_SIZE (1024 * 188)
pengfei.liuab5a2262020-02-14 17:33:40 +080021
22/**\brief DVR index file type*/
23typedef enum {
24 DVR_INDEX_TYPE_PCR, /**< DVR index file use pcr*/
25 DVR_INDEX_TYPE_LOCAL_CLOCK, /**< DVR index file use local clock*/
26 DVR_INDEX_TYPE_INVALID /**< DVR index file type invalid type*/
27} DVR_IndexType_t;
28
29/**\brief DVR VOD context*/
30typedef struct {
31 pthread_mutex_t mutex; /**< VOD mutex lock*/
32 pthread_cond_t cond; /**< VOD condition*/
33 void *buffer; /**< VOD buffer*/
34 uint32_t buf_len; /**< VOD buffer len*/
35} DVR_VodContext_t;
36
pengfei.liu07ddc8a2020-03-24 23:36:53 +080037/**\brief DVR record secure mode buffer*/
38typedef struct {
39 uint32_t addr; /**< Secure mode record buffer address*/
40 uint32_t len; /**< Secure mode record buffer length*/
41} DVR_SecureBuffer_t;
42
hualing chenf9867402020-09-23 17:06:20 +080043/**\brief DVR record new dmx secure mode buffer*/
44typedef struct {
45 uint32_t buf_start; /**< Secure mode record buffer address*/
46 uint32_t buf_end; /**< Secure mode record buffer length*/
47 uint32_t data_start; /**< Secure mode record buffer address*/
48 uint32_t data_end; /**< Secure mode record buffer length*/
49} DVR_NewDmxSecureBuffer_t;
50
Pengfei Liub4734232020-01-17 18:25:10 +080051/**\brief DVR record context*/
Pengfei Liuc181a982020-01-07 19:27:13 +080052typedef struct {
Pengfei Liub4734232020-01-17 18:25:10 +080053 pthread_t thread; /**< DVR thread handle*/
54 Record_DeviceHandle_t dev_handle; /**< DVR device handle*/
55 Segment_Handle_t segment_handle; /**< DVR segment handle*/
56 DVR_RecordState_t state; /**< DVR record state*/
57 char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/
58 DVR_RecordSegmentStartParams_t segment_params; /**< DVR record start parameters*/
59 DVR_RecordSegmentInfo_t segment_info; /**< DVR record current segment info*/
60 size_t notification_size; /**< DVR record nogification size*/
61 DVR_RecordEventFunction_t event_notify_fn; /**< DVR record event notify function*/
62 void *event_userdata; /**< DVR record event userdata*/
pengfei.liuab5a2262020-02-14 17:33:40 +080063 //DVR_VodContext_t vod; /**< DVR record vod context*/
64 int is_vod; /**< Indicate current mode is VOD record mode*/
pengfei.liu27cc4ec2020-04-03 16:28:16 +080065 DVR_CryptoFunction_t enc_func; /**< Encrypt function*/
pengfei.liu07ddc8a2020-03-24 23:36:53 +080066 void *enc_userdata; /**< Encrypt userdata*/
67 int is_secure_mode; /**< Record session run in secure pipeline */
Yahui Han1fbf3292021-11-08 18:17:19 +080068 void *cryptor; /**< Cryptor for encrypted PVR on FTA.*/
hualing chen2615aa82020-04-02 21:32:51 +080069 size_t last_send_size; /**< Last send notify segment size */
pengfei.liufda2a972020-04-09 14:47:15 +080070 uint32_t block_size; /**< DVR record block size */
hualing chenf9867402020-09-23 17:06:20 +080071 DVR_Bool_t is_new_dmx; /**< DVR is used new dmx driver */
hualing chen40accc32021-04-21 15:58:09 +080072 int index_type; /**< DVR is used pcr or local time */
Pengfei Liuc181a982020-01-07 19:27:13 +080073} DVR_RecordContext_t;
74
Gong Ke2a0ebbe2021-05-25 15:22:50 +080075extern ssize_t record_device_read_ext(Record_DeviceHandle_t handle, size_t *buf, size_t *len);
76
Pengfei Liuc181a982020-01-07 19:27:13 +080077static DVR_RecordContext_t record_ctx[MAX_DVR_RECORD_SESSION_COUNT] = {
78 {
Pengfei Liub4734232020-01-17 18:25:10 +080079 .state = DVR_RECORD_STATE_CLOSED
Pengfei Liuc181a982020-01-07 19:27:13 +080080 },
81 {
Pengfei Liub4734232020-01-17 18:25:10 +080082 .state = DVR_RECORD_STATE_CLOSED
Pengfei Liuc181a982020-01-07 19:27:13 +080083 }
84};
85
Zhiqiang Han51646a02020-04-05 18:43:22 +080086static int record_is_valid_pid(DVR_RecordContext_t *p_ctx, int pid)
87{
88 int i;
89
90 for (i = 0; i < p_ctx->segment_info.nb_pids; i++) {
91 if (pid == p_ctx->segment_info.pids[i].pid)
92 return 1;
93 }
94 return 0;
95}
96
pengfei.liuab5a2262020-02-14 17:33:40 +080097static int record_save_pcr(DVR_RecordContext_t *p_ctx, uint8_t *buf, loff_t pos)
98{
99 uint8_t *p = buf;
100 int len;
101 uint8_t afc;
102 uint64_t pcr = 0;
103 int has_pcr = 0;
104 int pid;
105 int adp_field_len;
106
107 pid = ((p[1] & 0x1f) << 8) | p[2];
Zhiqiang Han51646a02020-04-05 18:43:22 +0800108 if (pid == 0x1fff || !record_is_valid_pid(p_ctx, pid))
pengfei.liuab5a2262020-02-14 17:33:40 +0800109 return has_pcr;
110
111 //scramble = p[3] >> 6;
112 //cc = p[3] & 0x0f;
113 afc = (p[3] >> 4) & 0x03;
114
115 p += 4;
116 len = 184;
117
118 if (afc & 2) {
119 adp_field_len = p[0];
120 /* Skip adaptation len */
121 p++;
122 len--;
123 /* Parse pcr field, see 13818 spec table I-2-6,adaptation_field */
hualing chen5605eed2020-05-26 18:18:06 +0800124 if (p[0] & 0x10 && len >= 6 && adp_field_len >= 6) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800125 /* get pcr value,pcr is 33bit value */
126 pcr = (((uint64_t)(p[1])) << 25)
127 | (((uint64_t)p[2]) << 17)
128 | (((uint64_t)(p[3])) << 9)
129 | (((uint64_t)p[4]) << 1)
130 | ((((uint64_t)p[5]) & 0x80) >> 7);
131 has_pcr = 1;
132 }
133
134 p += adp_field_len;
135 len -= adp_field_len;
136
137 if (len < 0) {
138 DVR_DEBUG(1, "parser pcr: illegal adaptation field length");
139 return 0;
140 }
141 }
142
hualing chen40accc32021-04-21 15:58:09 +0800143 if (has_pcr && p_ctx->index_type == DVR_INDEX_TYPE_PCR) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800144 segment_update_pts(p_ctx->segment_handle, pcr/90, pos);
145 }
146 return has_pcr;
147}
148
149static int record_do_pcr_index(DVR_RecordContext_t *p_ctx, uint8_t *buf, int len)
150{
151 uint8_t *p = buf;
152 int left = len;
153 loff_t pos;
154 int has_pcr = 0;
155
156 pos = segment_tell_position(p_ctx->segment_handle);
157 while (left >= 188) {
158 if (*p == 0x47) {
159 has_pcr |= record_save_pcr(p_ctx, p, pos);
160 p += 188;
161 left -= 188;
162 pos += 188;
163 } else {
164 p++;
165 left --;
166 pos++;
167 }
168 }
169 return has_pcr;
170}
171
pengfei.liu567d6d82020-04-17 16:48:59 +0800172static int get_diff_time(struct timeval start_tv, struct timeval end_tv)
173{
174 return end_tv.tv_sec * 1000 + end_tv.tv_usec / 1000 - start_tv.tv_sec * 1000 - start_tv.tv_usec / 1000;
175}
176
Pengfei Liuc181a982020-01-07 19:27:13 +0800177void *record_thread(void *arg)
178{
179 DVR_RecordContext_t *p_ctx = (DVR_RecordContext_t *)arg;
180 ssize_t len;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800181 uint8_t *buf, *buf_out;
pengfei.liufda2a972020-04-09 14:47:15 +0800182 uint32_t block_size = p_ctx->block_size;
pengfei.liuab5a2262020-02-14 17:33:40 +0800183 loff_t pos = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800184 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800185 struct timespec start_ts, end_ts;
186 DVR_RecordStatus_t record_status;
pengfei.liuab5a2262020-02-14 17:33:40 +0800187 int has_pcr;
hualing chen40accc32021-04-21 15:58:09 +0800188
hualing chen87072a82020-03-12 16:20:12 +0800189 time_t pre_time = 0;
hualing chen2932d372020-04-29 13:44:00 +0800190 #define DVR_STORE_INFO_TIME (400)
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800191 DVR_SecureBuffer_t secure_buf;
hualing chenf9867402020-09-23 17:06:20 +0800192 DVR_NewDmxSecureBuffer_t new_dmx_secure_buf;
hualing chend241c7a2021-06-22 13:34:27 +0800193
194 if (CONTROL_SPEED_ENABLE == 0)
195 p_ctx->index_type = DVR_INDEX_TYPE_INVALID;
196 else
197 p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
Pengfei Liuc181a982020-01-07 19:27:13 +0800198
Pengfei Liub4734232020-01-17 18:25:10 +0800199 buf = (uint8_t *)malloc(block_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800200 if (!buf) {
201 DVR_DEBUG(1, "%s, malloc failed", __func__);
202 return NULL;
203 }
204
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800205 buf_out = (uint8_t *)malloc(block_size + 188);
206 if (!buf_out) {
207 DVR_DEBUG(1, "%s, malloc failed", __func__);
Pengfei Liufaf38e42020-05-22 00:28:02 +0800208 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800209 return NULL;
210 }
211
hualing chen266b9502020-04-04 17:39:39 +0800212 memset(&record_status, 0, sizeof(record_status));
213 record_status.state = DVR_RECORD_STATE_STARTED;
pengfei.liufda2a972020-04-09 14:47:15 +0800214 if (p_ctx->event_notify_fn) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800215 record_status.info.id = p_ctx->segment_info.id;
pengfei.liufda2a972020-04-09 14:47:15 +0800216 p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata);
hualing chen4b7c15d2020-04-07 16:13:48 +0800217 DVR_DEBUG(1, "%s line %d notify record status, state:%d id=%lld",
218 __func__,__LINE__, record_status.state, p_ctx->segment_info.id);
pengfei.liufda2a972020-04-09 14:47:15 +0800219 }
Yahui Han1fbf3292021-11-08 18:17:19 +0800220 DVR_DEBUG(1, "%s, secure_mode:%d, block_size:%d, cryptor:%p",
221 __func__, p_ctx->is_secure_mode,
222 block_size, p_ctx->cryptor);
Pengfei Liub4734232020-01-17 18:25:10 +0800223 clock_gettime(CLOCK_MONOTONIC, &start_ts);
pengfei.liu567d6d82020-04-17 16:48:59 +0800224
225 struct timeval t1, t2, t3, t4, t5, t6, t7;
hualing chen03fd4942021-07-15 15:56:41 +0800226 while (p_ctx->state == DVR_RECORD_STATE_STARTED ||
227 p_ctx->state == DVR_RECORD_STATE_PAUSE) {
228
229 if (p_ctx->state == DVR_RECORD_STATE_PAUSE) {
230 //wait resume record
231 usleep(20*1000);
232 continue;
233 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800234 gettimeofday(&t1, NULL);
hualing chenc70a8df2020-05-12 19:23:11 +0800235
pengfei.liuab5a2262020-02-14 17:33:40 +0800236 /* data from dmx, normal dvr case */
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800237 if (p_ctx->is_secure_mode) {
hualing chenf9867402020-09-23 17:06:20 +0800238 if (p_ctx->is_new_dmx) {
Yahui Hance15e9c2020-12-08 18:08:32 +0800239
240 /* We resolve the below invoke for dvbcore to be under safety status */
hualing chenf9867402020-09-23 17:06:20 +0800241 memset(&new_dmx_secure_buf, 0, sizeof(new_dmx_secure_buf));
Yahui Hance15e9c2020-12-08 18:08:32 +0800242 len = record_device_read(p_ctx->dev_handle, &new_dmx_secure_buf, sizeof(new_dmx_secure_buf), 10);
243 if (len == DVR_FAILURE) {
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800244 DVR_DEBUG(1, "handle[%p] ret:%d\n", p_ctx->dev_handle, ret);
Yahui Hance15e9c2020-12-08 18:08:32 +0800245 /*For the second recording, poll always failed which we should check
246 * dvbcore further. For now, Just ignore the fack poll fail, I think
247 * it won't influce anything. But we need adjust the poll timeout
248 * from 1000ms to 10ms.
249 */
250 //continue;
251 }
252
253 /* Read data from secure demux TA */
254 len = record_device_read_ext(p_ctx->dev_handle, &secure_buf.addr,
255 &secure_buf.len);
256
hualing chenf9867402020-09-23 17:06:20 +0800257 } else {
258 memset(&secure_buf, 0, sizeof(secure_buf));
259 len = record_device_read(p_ctx->dev_handle, &secure_buf, sizeof(secure_buf), 1000);
260 }
261 if (len != DVR_FAILURE) {
hualing chen4fe3bee2020-10-23 13:58:52 +0800262 //DVR_DEBUG(1, "%s, secure_buf:%#x, size:%#x", __func__, secure_buf.addr, secure_buf.len);
hualing chenf9867402020-09-23 17:06:20 +0800263 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800264 } else {
265 len = record_device_read(p_ctx->dev_handle, buf, block_size, 1000);
266 }
Pengfei Liub4734232020-01-17 18:25:10 +0800267 if (len == DVR_FAILURE) {
hualing chen6c126382020-04-13 15:47:51 +0800268 //usleep(10*1000);
hualing chen2932d372020-04-29 13:44:00 +0800269 DVR_DEBUG(1, "%s, start_read error", __func__);
Pengfei Liub4734232020-01-17 18:25:10 +0800270 continue;
271 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800272 gettimeofday(&t2, NULL);
hualing chenc70a8df2020-05-12 19:23:11 +0800273
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800274 /* Got data from device, record it */
Yahui Han1fbf3292021-11-08 18:17:19 +0800275 if (p_ctx->is_secure_mode && p_ctx->enc_func) {
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800276 /* Encrypt record data */
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800277 DVR_CryptoParams_t crypto_params;
278
279 memset(&crypto_params, 0, sizeof(crypto_params));
280 crypto_params.type = DVR_CRYPTO_TYPE_ENCRYPT;
hualing chen7a56cba2020-04-14 14:09:27 +0800281 memcpy(crypto_params.location, p_ctx->location, sizeof(p_ctx->location));
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800282 crypto_params.segment_id = p_ctx->segment_info.id;
283 crypto_params.offset = p_ctx->segment_info.size;
284
285 if (p_ctx->is_secure_mode) {
286 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_SECURE;
Yahui Han1fbf3292021-11-08 18:17:19 +0800287 crypto_params.input_buffer.addr = secure_buf.addr;
288 crypto_params.input_buffer.size = secure_buf.len;
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800289 } else {
290 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
291 crypto_params.input_buffer.addr = (size_t)buf;
292 crypto_params.input_buffer.size = len;
293 }
294
295 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL;
296 crypto_params.output_buffer.addr = (size_t)buf_out;
297 crypto_params.output_buffer.size = block_size + 188;
hualing chen9811b212020-10-29 11:21:44 +0800298
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800299 p_ctx->enc_func(&crypto_params, p_ctx->enc_userdata);
pengfei.liu567d6d82020-04-17 16:48:59 +0800300 gettimeofday(&t3, NULL);
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800301 /* Out buffer length may not equal in buffer length */
hualing chenf9867402020-09-23 17:06:20 +0800302 if (crypto_params.output_size > 0) {
303 ret = segment_write(p_ctx->segment_handle, buf_out, crypto_params.output_size);
304 len = crypto_params.output_size;
hualing chenbafc62d2020-11-02 15:44:05 +0800305 } else {
306 len = 0;
hualing chenf9867402020-09-23 17:06:20 +0800307 }
Yahui Han1fbf3292021-11-08 18:17:19 +0800308 } else if (p_ctx->cryptor) {
309 /* Encrypt with clear key */
Yahui Han63b23b42021-12-07 15:37:46 +0800310 int crypt_len = len;
311 am_crypt_des_crypt(p_ctx->cryptor, buf_out, buf, &crypt_len, 0);
312 len = crypt_len;
Yahui Han1fbf3292021-11-08 18:17:19 +0800313 gettimeofday(&t3, NULL);
314 ret = segment_write(p_ctx->segment_handle, buf_out, len);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800315 } else {
pengfei.liu567d6d82020-04-17 16:48:59 +0800316 gettimeofday(&t3, NULL);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800317 ret = segment_write(p_ctx->segment_handle, buf, len);
318 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800319 gettimeofday(&t4, NULL);
hualing chen626204e2020-04-07 11:55:08 +0800320 //add DVR_RECORD_EVENT_WRITE_ERROR event if write error
pengfei.liufda2a972020-04-09 14:47:15 +0800321 if (ret == -1 && len > 0 && p_ctx->event_notify_fn) {
hualing chen626204e2020-04-07 11:55:08 +0800322 //send write event
hualing chen9b434f02020-06-10 15:06:54 +0800323 if (p_ctx->event_notify_fn) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800324 memset(&record_status, 0, sizeof(record_status));
hualing chen4fe3bee2020-10-23 13:58:52 +0800325 DVR_DEBUG(1, "%s:%d,send event write error", __func__,__LINE__);
hualing chen9ce7d942021-06-30 13:31:01 +0800326 record_status.info.id = p_ctx->segment_info.id;
hualing chen4b7c15d2020-04-07 16:13:48 +0800327 p_ctx->event_notify_fn(DVR_RECORD_EVENT_WRITE_ERROR, &record_status, p_ctx->event_userdata);
328 }
hualing chenc70a8df2020-05-12 19:23:11 +0800329 DVR_DEBUG(1, "%s,write error %d", __func__,__LINE__);
hualing chen626204e2020-04-07 11:55:08 +0800330 goto end;
331 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800332 /* Do time index */
Yahui Han1fbf3292021-11-08 18:17:19 +0800333 uint8_t *index_buf = (p_ctx->enc_func || p_ctx->cryptor)? buf_out : buf;
pengfei.liuab5a2262020-02-14 17:33:40 +0800334 pos = segment_tell_position(p_ctx->segment_handle);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800335 has_pcr = record_do_pcr_index(p_ctx, index_buf, len);
hualing chen40accc32021-04-21 15:58:09 +0800336 if (has_pcr == 0 && p_ctx->index_type == DVR_INDEX_TYPE_INVALID) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800337 clock_gettime(CLOCK_MONOTONIC, &end_ts);
338 if ((end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) -
339 (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) > 40) {
340 /* PCR interval threshlod > 40 ms*/
341 DVR_DEBUG(1, "%s use local clock time index", __func__);
hualing chen40accc32021-04-21 15:58:09 +0800342 p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
pengfei.liuab5a2262020-02-14 17:33:40 +0800343 }
hualing chen40accc32021-04-21 15:58:09 +0800344 } else if (has_pcr && p_ctx->index_type == DVR_INDEX_TYPE_INVALID){
pengfei.liuab5a2262020-02-14 17:33:40 +0800345 DVR_DEBUG(1, "%s use pcr time index", __func__);
hualing chen40accc32021-04-21 15:58:09 +0800346 p_ctx->index_type = DVR_INDEX_TYPE_PCR;
Pengfei Liuc181a982020-01-07 19:27:13 +0800347 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800348 gettimeofday(&t5, NULL);
pengfei.liuab5a2262020-02-14 17:33:40 +0800349
350 /* Update segment info */
Pengfei Liub4734232020-01-17 18:25:10 +0800351 p_ctx->segment_info.size += len;
pengfei.liuab5a2262020-02-14 17:33:40 +0800352 /*Duration need use pcr to calculate, todo...*/
hualing chen40accc32021-04-21 15:58:09 +0800353 if (p_ctx->index_type == DVR_INDEX_TYPE_PCR) {
pengfei.liu8b563292020-02-26 15:49:02 +0800354 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
hualing chen87072a82020-03-12 16:20:12 +0800355 if (pre_time == 0)
356 pre_time = p_ctx->segment_info.duration;
hualing chen40accc32021-04-21 15:58:09 +0800357 } else if (p_ctx->index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800358 clock_gettime(CLOCK_MONOTONIC, &end_ts);
359 p_ctx->segment_info.duration = (end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) -
360 (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000);
hualing chen87072a82020-03-12 16:20:12 +0800361 if (pre_time == 0)
362 pre_time = p_ctx->segment_info.duration;
pengfei.liuab5a2262020-02-14 17:33:40 +0800363 segment_update_pts(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
364 } else {
365 DVR_DEBUG(1, "%s can NOT do time index", __func__);
366 }
367 p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188;
Pengfei Liub4734232020-01-17 18:25:10 +0800368
hualing chen87072a82020-03-12 16:20:12 +0800369 if (p_ctx->segment_info.duration - pre_time > DVR_STORE_INFO_TIME) {
370 pre_time = p_ctx->segment_info.duration + DVR_STORE_INFO_TIME;
hualing chenf9867402020-09-23 17:06:20 +0800371 segment_store_info(p_ctx->segment_handle, &(p_ctx->segment_info));
hualing chen87072a82020-03-12 16:20:12 +0800372 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800373 gettimeofday(&t6, NULL);
hualing chen87072a82020-03-12 16:20:12 +0800374 /*Event notification*/
Pengfei Liub4734232020-01-17 18:25:10 +0800375 if (p_ctx->notification_size &&
376 p_ctx->event_notify_fn &&
hualing chen2615aa82020-04-02 21:32:51 +0800377 /*!(p_ctx->segment_info.size % p_ctx->notification_size)*/
378 (p_ctx->segment_info.size -p_ctx->last_send_size) >= p_ctx->notification_size&&
hualing chenbe68d642021-09-09 13:06:36 +0800379 p_ctx->segment_info.duration > 0 &&
380 p_ctx->state == DVR_RECORD_STATE_STARTED) {
Pengfei Liub4734232020-01-17 18:25:10 +0800381 memset(&record_status, 0, sizeof(record_status));
pengfei.liuab5a2262020-02-14 17:33:40 +0800382 //clock_gettime(CLOCK_MONOTONIC, &end_ts);
hualing chen2615aa82020-04-02 21:32:51 +0800383 p_ctx->last_send_size = p_ctx->segment_info.size;
Pengfei Liub4734232020-01-17 18:25:10 +0800384 record_status.state = p_ctx->state;
385 record_status.info.id = p_ctx->segment_info.id;
pengfei.liuab5a2262020-02-14 17:33:40 +0800386 record_status.info.duration = p_ctx->segment_info.duration;
Pengfei Liub4734232020-01-17 18:25:10 +0800387 record_status.info.size = p_ctx->segment_info.size;
388 record_status.info.nb_packets = p_ctx->segment_info.size/188;
389 p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata);
hualing chen4fe3bee2020-10-23 13:58:52 +0800390 DVR_DEBUG(1, "%s notify record status, state:%d, id:%lld, duration:%ld ms, size:%zu loc[%s]",
391 __func__, record_status.state,
392 record_status.info.id, record_status.info.duration,
393 record_status.info.size, p_ctx->location);
Pengfei Liub4734232020-01-17 18:25:10 +0800394 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800395 gettimeofday(&t7, NULL);
396#ifdef DEBUG_PERFORMANCE
hualing chen2932d372020-04-29 13:44:00 +0800397 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 +0800398 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 +0800399 get_diff_time(t5, t6), get_diff_time(t6, t7), get_diff_time(t1, t5), len);
pengfei.liu567d6d82020-04-17 16:48:59 +0800400#endif
Pengfei Liuc181a982020-01-07 19:27:13 +0800401 }
hualing chen626204e2020-04-07 11:55:08 +0800402end:
Pengfei Liub4734232020-01-17 18:25:10 +0800403 free((void *)buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800404 free((void *)buf_out);
Pengfei Liub4734232020-01-17 18:25:10 +0800405 DVR_DEBUG(1, "exit %s", __func__);
Pengfei Liuc181a982020-01-07 19:27:13 +0800406 return NULL;
407}
408
409int dvr_record_open(DVR_RecordHandle_t *p_handle, DVR_RecordOpenParams_t *params)
410{
411 DVR_RecordContext_t *p_ctx;
412 Record_DeviceOpenParams_t dev_open_params;
413 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800414 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800415
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800416 DVR_RETURN_IF_FALSE(p_handle);
417 DVR_RETURN_IF_FALSE(params);
Pengfei Liuc181a982020-01-07 19:27:13 +0800418
419 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
Pengfei Liub4734232020-01-17 18:25:10 +0800420 if (record_ctx[i].state == DVR_RECORD_STATE_CLOSED) {
Pengfei Liuc181a982020-01-07 19:27:13 +0800421 break;
422 }
423 }
Pengfei Liufaf38e42020-05-22 00:28:02 +0800424 DVR_RETURN_IF_FALSE(i < MAX_DVR_RECORD_SESSION_COUNT);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800425 DVR_RETURN_IF_FALSE(record_ctx[i].state == DVR_RECORD_STATE_CLOSED);
Pengfei Liuc181a982020-01-07 19:27:13 +0800426 p_ctx = &record_ctx[i];
Yahui Han1fbf3292021-11-08 18:17:19 +0800427 DVR_DEBUG(1, "%s , current state:%d, dmx_id:%d, notification_size:%zu, flags:%d, keylen:%d ",
428 __func__, p_ctx->state, params->dmx_dev_id,
429 params->notification_size,
430 params->flags, params->keylen);
Pengfei Liuc181a982020-01-07 19:27:13 +0800431
Pengfei Liub4734232020-01-17 18:25:10 +0800432 /*Process event params*/
433 p_ctx->notification_size = params->notification_size;
434 p_ctx->event_notify_fn = params->event_fn;
435 p_ctx->event_userdata = params->event_userdata;
hualing chen2615aa82020-04-02 21:32:51 +0800436 p_ctx->last_send_size = 0;
Yahui Han1fbf3292021-11-08 18:17:19 +0800437
438 if (params->keylen > 0) {
439 p_ctx->cryptor = am_crypt_des_open(params->clearkey,
440 params->cleariv,
441 params->keylen * 8);
442 if (!p_ctx->cryptor)
443 DVR_DEBUG(1, "%s , open des cryptor failed!!!\n", __func__);
444 } else {
445 p_ctx->cryptor = NULL;
446 }
447
hualing chenf9867402020-09-23 17:06:20 +0800448 //check is new driver
449 p_ctx->is_new_dmx = dvr_check_dmx_isNew();
Pengfei Liub4734232020-01-17 18:25:10 +0800450 /*Process crypto params, todo*/
Pengfei Liub4734232020-01-17 18:25:10 +0800451 memset((void *)&dev_open_params, 0, sizeof(dev_open_params));
pengfei.liuab5a2262020-02-14 17:33:40 +0800452 if (params->data_from_memory) {
453 /* data from memory, VOD case */
454 p_ctx->is_vod = 1;
455 } else {
456 p_ctx->is_vod = 0;
457 /* data from dmx, normal dvr case */
hualing chen958fe4c2020-03-24 17:35:07 +0800458 dev_open_params.dmx_dev_id = params->dmx_dev_id;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800459 dev_open_params.buf_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE);
hualing chen03fd4942021-07-15 15:56:41 +0800460 dev_open_params.ringbuf_size = params->ringbuf_size;
hualing chen9811b212020-10-29 11:21:44 +0800461 if (p_ctx->is_new_dmx)
hualing cheneceb37e2021-01-18 16:07:48 +0800462 dev_open_params.buf_size = NEW_DEVICE_RECORD_BLOCK_SIZE * 30;
pengfei.liuab5a2262020-02-14 17:33:40 +0800463 ret = record_device_open(&p_ctx->dev_handle, &dev_open_params);
464 if (ret != DVR_SUCCESS) {
465 DVR_DEBUG(1, "%s, open record devices failed", __func__);
466 return DVR_FAILURE;
467 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800468 }
469
pengfei.liufda2a972020-04-09 14:47:15 +0800470 p_ctx->block_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE);
hualing chen9811b212020-10-29 11:21:44 +0800471 if (p_ctx->is_new_dmx)
Yahui Hance15e9c2020-12-08 18:08:32 +0800472 p_ctx->block_size = NEW_DEVICE_RECORD_BLOCK_SIZE * 30;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800473 p_ctx->enc_func = NULL;
474 p_ctx->enc_userdata = NULL;
475 p_ctx->is_secure_mode = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800476 p_ctx->state = DVR_RECORD_STATE_OPENED;
hualing chen9811b212020-10-29 11:21:44 +0800477 DVR_DEBUG(1, "%s, block_size:%d is_new:%d", __func__, p_ctx->block_size, p_ctx->is_new_dmx);
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800478 *p_handle = p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800479 return DVR_SUCCESS;
480}
481
482int dvr_record_close(DVR_RecordHandle_t handle)
483{
484 DVR_RecordContext_t *p_ctx;
485 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800486 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800487
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800488 p_ctx = (DVR_RecordContext_t *)handle;
489 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
490 if (p_ctx == &record_ctx[i])
491 break;
492 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800493 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800494
Pengfei Liub4734232020-01-17 18:25:10 +0800495 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800496 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
Yahui Han1fbf3292021-11-08 18:17:19 +0800497 if (p_ctx->cryptor) {
498 am_crypt_des_close(p_ctx->cryptor);
499 p_ctx->cryptor = NULL;
500 }
pengfei.liuab5a2262020-02-14 17:33:40 +0800501 if (p_ctx->is_vod) {
502 ret = DVR_SUCCESS;
503 } else {
504 ret = record_device_close(p_ctx->dev_handle);
505 if (ret != DVR_SUCCESS) {
506 DVR_DEBUG(1, "%s, failed", __func__);
507 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800508 }
509
Pengfei Liub4734232020-01-17 18:25:10 +0800510 p_ctx->state = DVR_RECORD_STATE_CLOSED;
Pengfei Liuc181a982020-01-07 19:27:13 +0800511 return ret;
512}
513
hualing chen03fd4942021-07-15 15:56:41 +0800514int dvr_record_pause(DVR_RecordHandle_t handle)
515{
516 DVR_RecordContext_t *p_ctx;
517 int ret;
518 uint32_t i;
519
520 p_ctx = (DVR_RecordContext_t *)handle;
521 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
522 if (p_ctx == &record_ctx[i])
523 break;
524 }
525 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
526
527 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
528 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
529
530 if (p_ctx->is_vod) {
531 ret = DVR_SUCCESS;
532 }
533 //set pause state,will not store ts into segment
534 p_ctx->state = DVR_RECORD_STATE_PAUSE;
535 return ret;
536}
537
538int dvr_record_resume(DVR_RecordHandle_t handle)
539{
540 DVR_RecordContext_t *p_ctx;
541 int ret;
542 uint32_t i;
543
544 p_ctx = (DVR_RecordContext_t *)handle;
545 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
546 if (p_ctx == &record_ctx[i])
547 break;
548 }
549 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
550
551 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
552 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
553
554 if (p_ctx->is_vod) {
555 ret = DVR_SUCCESS;
556 }
557 //set stated state,will resume store ts into segment
558 p_ctx->state = DVR_RECORD_STATE_STARTED;
559 return ret;
560}
561
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800562#if 0
Pengfei Liuc181a982020-01-07 19:27:13 +0800563int dvr_record_register_encryption(DVR_RecordHandle_t handle,
564 DVR_CryptoFunction_t cb,
565 DVR_CryptoParams_t params,
566 void *userdata)
567{
568 return DVR_SUCCESS;
569}
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800570#endif
Pengfei Liuc181a982020-01-07 19:27:13 +0800571
572int dvr_record_start_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params)
573{
574 DVR_RecordContext_t *p_ctx;
575 Segment_OpenParams_t open_params;
576 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800577 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800578
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800579 p_ctx = (DVR_RecordContext_t *)handle;
580 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
581 if (p_ctx == &record_ctx[i])
582 break;
583 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800584 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800585
hualing chen4fe3bee2020-10-23 13:58:52 +0800586 DVR_DEBUG(1, "%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 +0800587 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
588 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
589 DVR_RETURN_IF_FALSE(params);
Pengfei Liuc181a982020-01-07 19:27:13 +0800590
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800591 DVR_RETURN_IF_FALSE(strlen((const char *)params->location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liuc181a982020-01-07 19:27:13 +0800592 memset(&open_params, 0, sizeof(open_params));
hualing chen7a56cba2020-04-14 14:09:27 +0800593 memcpy(open_params.location, params->location, sizeof(params->location));
Pengfei Liuc181a982020-01-07 19:27:13 +0800594 open_params.segment_id = params->segment.segment_id;
595 open_params.mode = SEGMENT_MODE_WRITE;
596
597 ret = segment_open(&open_params, &p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800598 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800599
Pengfei Liub4734232020-01-17 18:25:10 +0800600 /*process params*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800601 {
hualing chen7a56cba2020-04-14 14:09:27 +0800602 memcpy(p_ctx->location, params->location, sizeof(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800603 //need all params??
Pengfei Liuc181a982020-01-07 19:27:13 +0800604 memcpy(&p_ctx->segment_params, &params->segment, sizeof(params->segment));
Pengfei Liub4734232020-01-17 18:25:10 +0800605 /*save current segment info*/
606 memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info));
607 p_ctx->segment_info.id = params->segment.segment_id;
608 p_ctx->segment_info.nb_pids = params->segment.nb_pids;
609 memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800610 }
611
pengfei.liuab5a2262020-02-14 17:33:40 +0800612 if (!p_ctx->is_vod) {
613 /* normal dvr case */
614 for (i = 0; i < params->segment.nb_pids; i++) {
615 ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
616 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
617 }
618 ret = record_device_start(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800619 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800620 }
621
Zhiqiang Han5ebad992020-04-28 18:19:59 +0800622 ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info);
623
Pengfei Liuc181a982020-01-07 19:27:13 +0800624 p_ctx->state = DVR_RECORD_STATE_STARTED;
pengfei.liuab5a2262020-02-14 17:33:40 +0800625 if (!p_ctx->is_vod)
626 pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +0800627
Pengfei Liuc181a982020-01-07 19:27:13 +0800628 return DVR_SUCCESS;
629}
630
631int dvr_record_next_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, DVR_RecordSegmentInfo_t *p_info)
632{
633 DVR_RecordContext_t *p_ctx;
Pengfei Liub4734232020-01-17 18:25:10 +0800634 Segment_OpenParams_t open_params;
Pengfei Liuc181a982020-01-07 19:27:13 +0800635 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800636 uint32_t i;
hualing chen2932d372020-04-29 13:44:00 +0800637 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800638
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800639 p_ctx = (DVR_RecordContext_t *)handle;
640 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
641 if (p_ctx == &record_ctx[i])
642 break;
643 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800644 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800645
hualing chen4fe3bee2020-10-23 13:58:52 +0800646 DVR_DEBUG(1, "%s , current state:%d p_ctx->location:%s", __func__, p_ctx->state, p_ctx->location);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800647 DVR_RETURN_IF_FALSE(p_ctx->state == DVR_RECORD_STATE_STARTED);
648 //DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
649 DVR_RETURN_IF_FALSE(params);
650 DVR_RETURN_IF_FALSE(p_info);
pengfei.liuab5a2262020-02-14 17:33:40 +0800651 DVR_RETURN_IF_FALSE(!p_ctx->is_vod);
Pengfei Liuc181a982020-01-07 19:27:13 +0800652
653 /*Stop the on going record segment*/
Pengfei Liub4734232020-01-17 18:25:10 +0800654 //ret = record_device_stop(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800655 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800656 p_ctx->state = DVR_RECORD_STATE_STOPPED;
657 pthread_join(p_ctx->thread, NULL);
658
hualing chen2932d372020-04-29 13:44:00 +0800659 //add index file store
660 pos = segment_tell_position(p_ctx->segment_handle);
661 segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
662
663 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
Pengfei Liub4734232020-01-17 18:25:10 +0800664 /*Update segment info*/
665 memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
666
667 ret = segment_store_info(p_ctx->segment_handle, p_info);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800668 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800669
hualing chena540a7e2020-03-27 16:44:05 +0800670 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",
671 __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 +0800672
Pengfei Liub4734232020-01-17 18:25:10 +0800673 /*Close current segment*/
674 ret = segment_close(p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800675 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800676 /*Open the new record segment*/
677 memset(&open_params, 0, sizeof(open_params));
hualing chen7a56cba2020-04-14 14:09:27 +0800678 memcpy(open_params.location, p_ctx->location, sizeof(p_ctx->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800679 open_params.segment_id = params->segment.segment_id;
680 open_params.mode = SEGMENT_MODE_WRITE;
hualing chen7a56cba2020-04-14 14:09:27 +0800681 DVR_DEBUG(1, "%s: p_ctx->location:%s params->location:%s", __func__, p_ctx->location,params->location);
Pengfei Liub4734232020-01-17 18:25:10 +0800682
683 ret = segment_open(&open_params, &p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800684 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800685 /*process params*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800686 {
Pengfei Liub4734232020-01-17 18:25:10 +0800687 //need all params??
Pengfei Liuc181a982020-01-07 19:27:13 +0800688 memcpy(&p_ctx->segment_params, &params->segment, sizeof(params->segment));
Pengfei Liub4734232020-01-17 18:25:10 +0800689 /*save current segment info*/
690 memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info));
691 p_ctx->segment_info.id = params->segment.segment_id;
692 memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800693 }
694
Pengfei Liub4734232020-01-17 18:25:10 +0800695 p_ctx->segment_info.nb_pids = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800696 for (i = 0; i < params->segment.nb_pids; i++) {
Pengfei Liub4734232020-01-17 18:25:10 +0800697 switch (params->segment.pid_action[i]) {
698 case DVR_RECORD_PID_CREATE:
699 DVR_DEBUG(1, "%s create pid:%d", __func__, params->segment.pids[i].pid);
700 ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
701 p_ctx->segment_info.nb_pids++;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800702 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800703 break;
704 case DVR_RECORD_PID_KEEP:
705 DVR_DEBUG(1, "%s keep pid:%d", __func__, params->segment.pids[i].pid);
706 p_ctx->segment_info.nb_pids++;
707 break;
708 case DVR_RECORD_PID_CLOSE:
709 DVR_DEBUG(1, "%s close pid:%d", __func__, params->segment.pids[i].pid);
710 ret = record_device_remove_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800711 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800712 break;
713 default:
714 DVR_DEBUG(1, "%s wrong action pid:%d", __func__, params->segment.pids[i].pid);
715 return DVR_FAILURE;
716 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800717 }
718
Pengfei Liub4734232020-01-17 18:25:10 +0800719 //ret = record_device_start(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800720 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
hualing chen4b7c15d2020-04-07 16:13:48 +0800721 /*Update segment info*/
722 ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info);
Pengfei Liuc181a982020-01-07 19:27:13 +0800723
Pengfei Liuc181a982020-01-07 19:27:13 +0800724 p_ctx->state = DVR_RECORD_STATE_STARTED;
Zhiqiang Han0d60f2b2020-05-26 14:39:54 +0800725 pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800726 return DVR_SUCCESS;
727}
728
729int dvr_record_stop_segment(DVR_RecordHandle_t handle, DVR_RecordSegmentInfo_t *p_info)
730{
731 DVR_RecordContext_t *p_ctx;
732 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800733 uint32_t i;
hualing chen2932d372020-04-29 13:44:00 +0800734 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800735
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800736 p_ctx = (DVR_RecordContext_t *)handle;
737 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
738 if (p_ctx == &record_ctx[i])
739 break;
740 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800741 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800742
hualing chen4fe3bee2020-10-23 13:58:52 +0800743 DVR_DEBUG(1, "%s , current state:%d p_ctx->location:%s", __func__, p_ctx->state, p_ctx->location);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800744 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STOPPED);
745 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800746 DVR_RETURN_IF_FALSE(p_info);/*should support NULL*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800747
Pengfei Liuc181a982020-01-07 19:27:13 +0800748 p_ctx->state = DVR_RECORD_STATE_STOPPED;
pengfei.liuab5a2262020-02-14 17:33:40 +0800749 if (p_ctx->is_vod) {
pengfei.liu8b563292020-02-26 15:49:02 +0800750 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
pengfei.liuab5a2262020-02-14 17:33:40 +0800751 p_ctx->segment_info.duration = 10*1000; //debug, should delete it
752 } else {
753 ret = record_device_stop(p_ctx->dev_handle);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800754 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
755 if (ret != DVR_SUCCESS)
756 goto end;
pengfei.liuab5a2262020-02-14 17:33:40 +0800757 //p_ctx->state = DVR_RECORD_STATE_STOPPED;
758 pthread_join(p_ctx->thread, NULL);
759 }
760
hualing chen2932d372020-04-29 13:44:00 +0800761 //add index file store
762 pos = segment_tell_position(p_ctx->segment_handle);
763 segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
hualing chene41f4372020-06-06 16:29:17 +0800764 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +0800765
Pengfei Liub4734232020-01-17 18:25:10 +0800766 /*Update segment info*/
767 memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
768
769 ret = segment_store_info(p_ctx->segment_handle, p_info);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800770 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
771 if (ret != DVR_SUCCESS)
772 goto end;
Pengfei Liub4734232020-01-17 18:25:10 +0800773
774 DVR_DEBUG(1, "%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d",
775 __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets);
776
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800777end:
Pengfei Liuc181a982020-01-07 19:27:13 +0800778 ret = segment_close(p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800779 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800780 return DVR_SUCCESS;
781}
Pengfei Liuc181a982020-01-07 19:27:13 +0800782
Pengfei Liub4734232020-01-17 18:25:10 +0800783int dvr_record_resume_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, uint64_t *p_resume_size)
784{
785 DVR_RecordContext_t *p_ctx;
786 uint32_t i;
pengfei.liuab5a2262020-02-14 17:33:40 +0800787 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800788
789 p_ctx = (DVR_RecordContext_t *)handle;
790 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
791 if (p_ctx == &record_ctx[i])
792 break;
Pengfei Liuc181a982020-01-07 19:27:13 +0800793 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800794 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
795 DVR_RETURN_IF_FALSE(params);
796 DVR_RETURN_IF_FALSE(p_resume_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800797
pengfei.liuab5a2262020-02-14 17:33:40 +0800798 DVR_DEBUG(1, "%s , current state:%d, resume size:%lld", __func__, p_ctx->state, *p_resume_size);
799 ret = dvr_record_start_segment(handle, params);
800 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
801
802 p_ctx->segment_info.size = *p_resume_size;
803
804 return DVR_SUCCESS;
805}
806
807int dvr_record_get_status(DVR_RecordHandle_t handle, DVR_RecordStatus_t *p_status)
808{
809 DVR_RecordContext_t *p_ctx;
810 int i;
811
812 p_ctx = (DVR_RecordContext_t *)handle;
813 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
814 if (p_ctx == &record_ctx[i])
815 break;
816 }
817 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
818 DVR_RETURN_IF_FALSE(p_status);
819
820 //lock
821 p_status->state = p_ctx->state;
822 p_status->info.id = p_ctx->segment_info.id;
823 p_status->info.duration = p_ctx->segment_info.duration;
824 p_status->info.size = p_ctx->segment_info.size;
825 p_status->info.nb_packets = p_ctx->segment_info.size/188;
826
827 return DVR_SUCCESS;
828}
829
830int dvr_record_write(DVR_RecordHandle_t handle, void *buffer, uint32_t len)
831{
832 DVR_RecordContext_t *p_ctx;
833 uint32_t i;
834 off_t pos = 0;
835 int ret;
836 int has_pcr;
837
838 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 }
843 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
844 DVR_RETURN_IF_FALSE(buffer);
845 DVR_RETURN_IF_FALSE(len);
846
847 pos = segment_tell_position(p_ctx->segment_handle);
848 has_pcr = record_do_pcr_index(p_ctx, buffer, len);
849 if (has_pcr == 0) {
850 /* Pull VOD record shoud use PCR time index */
851 DVR_DEBUG(1, "%s has no pcr, can NOT do time index", __func__);
852 }
853 ret = segment_write(p_ctx->segment_handle, buffer, len);
hualing chen2932d372020-04-29 13:44:00 +0800854 if (ret != len) {
855 DVR_DEBUG(1, "%s write error ret:%d len:%d", __func__, ret, len);
856 }
pengfei.liuab5a2262020-02-14 17:33:40 +0800857 p_ctx->segment_info.size += len;
858 p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188;
859
Pengfei Liuc181a982020-01-07 19:27:13 +0800860 return DVR_SUCCESS;
861}
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800862
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800863int dvr_record_set_encrypt_callback(DVR_RecordHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800864{
865 DVR_RecordContext_t *p_ctx;
866 uint32_t i;
867
868 p_ctx = (DVR_RecordContext_t *)handle;
869 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
870 if (p_ctx == &record_ctx[i])
871 break;
872 }
873 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
874 DVR_RETURN_IF_FALSE(func);
875
876 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
877 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
878 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
879
880 p_ctx->enc_func = func;
881 p_ctx->enc_userdata = userdata;
882 return DVR_SUCCESS;
883}
884
885int dvr_record_set_secure_buffer(DVR_RecordHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
886{
887 DVR_RecordContext_t *p_ctx;
888 uint32_t i;
889 int ret;
890
891 p_ctx = (DVR_RecordContext_t *)handle;
892 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
893 if (p_ctx == &record_ctx[i])
894 break;
895 }
896 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
897 DVR_RETURN_IF_FALSE(p_secure_buf);
898 DVR_RETURN_IF_FALSE(len);
899
900 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
901 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
902 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
903
904 ret = record_device_set_secure_buffer(p_ctx->dev_handle, p_secure_buf, len);
905 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
906
907 p_ctx->is_secure_mode = 1;
908 return ret;
909}
hualing chen4fe3bee2020-10-23 13:58:52 +0800910
911int dvr_record_is_secure_mode(DVR_RecordHandle_t handle)
912{
913 DVR_RecordContext_t *p_ctx;
914 uint32_t i;
915 int ret;
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
924 if (p_ctx->is_secure_mode == 1)
925 ret = 1;
926 else
927 ret = 0;
928 return ret;
Yahui Hance15e9c2020-12-08 18:08:32 +0800929}