blob: f62839f876d9a00d213ecf0ddcbb187fd1cd8ad3 [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>
Pengfei Liuc181a982020-01-07 19:27:13 +080013
hualing chend241c7a2021-06-22 13:34:27 +080014#define CONTROL_SPEED_ENABLE 0
15
hualing chenc7aa4c82021-02-03 15:41:37 +080016//#define DEBUG_PERFORMANCE
Pengfei Liuc181a982020-01-07 19:27:13 +080017#define MAX_DVR_RECORD_SESSION_COUNT 2
hualing chen266b9502020-04-04 17:39:39 +080018#define RECORD_BLOCK_SIZE (256 * 1024)
Yahui Hance15e9c2020-12-08 18:08:32 +080019#define NEW_DEVICE_RECORD_BLOCK_SIZE (1024 * 188)
pengfei.liuab5a2262020-02-14 17:33:40 +080020
21/**\brief DVR index file type*/
22typedef enum {
23 DVR_INDEX_TYPE_PCR, /**< DVR index file use pcr*/
24 DVR_INDEX_TYPE_LOCAL_CLOCK, /**< DVR index file use local clock*/
25 DVR_INDEX_TYPE_INVALID /**< DVR index file type invalid type*/
26} DVR_IndexType_t;
27
28/**\brief DVR VOD context*/
29typedef struct {
30 pthread_mutex_t mutex; /**< VOD mutex lock*/
31 pthread_cond_t cond; /**< VOD condition*/
32 void *buffer; /**< VOD buffer*/
33 uint32_t buf_len; /**< VOD buffer len*/
34} DVR_VodContext_t;
35
pengfei.liu07ddc8a2020-03-24 23:36:53 +080036/**\brief DVR record secure mode buffer*/
37typedef struct {
38 uint32_t addr; /**< Secure mode record buffer address*/
39 uint32_t len; /**< Secure mode record buffer length*/
40} DVR_SecureBuffer_t;
41
hualing chenf9867402020-09-23 17:06:20 +080042/**\brief DVR record new dmx secure mode buffer*/
43typedef struct {
44 uint32_t buf_start; /**< Secure mode record buffer address*/
45 uint32_t buf_end; /**< Secure mode record buffer length*/
46 uint32_t data_start; /**< Secure mode record buffer address*/
47 uint32_t data_end; /**< Secure mode record buffer length*/
48} DVR_NewDmxSecureBuffer_t;
49
Pengfei Liub4734232020-01-17 18:25:10 +080050/**\brief DVR record context*/
Pengfei Liuc181a982020-01-07 19:27:13 +080051typedef struct {
Pengfei Liub4734232020-01-17 18:25:10 +080052 pthread_t thread; /**< DVR thread handle*/
53 Record_DeviceHandle_t dev_handle; /**< DVR device handle*/
54 Segment_Handle_t segment_handle; /**< DVR segment handle*/
55 DVR_RecordState_t state; /**< DVR record state*/
56 char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/
57 DVR_RecordSegmentStartParams_t segment_params; /**< DVR record start parameters*/
58 DVR_RecordSegmentInfo_t segment_info; /**< DVR record current segment info*/
59 size_t notification_size; /**< DVR record nogification size*/
60 DVR_RecordEventFunction_t event_notify_fn; /**< DVR record event notify function*/
61 void *event_userdata; /**< DVR record event userdata*/
pengfei.liuab5a2262020-02-14 17:33:40 +080062 //DVR_VodContext_t vod; /**< DVR record vod context*/
63 int is_vod; /**< Indicate current mode is VOD record mode*/
pengfei.liu27cc4ec2020-04-03 16:28:16 +080064 DVR_CryptoFunction_t enc_func; /**< Encrypt function*/
pengfei.liu07ddc8a2020-03-24 23:36:53 +080065 void *enc_userdata; /**< Encrypt userdata*/
66 int is_secure_mode; /**< Record session run in secure pipeline */
hualing chen2615aa82020-04-02 21:32:51 +080067 size_t last_send_size; /**< Last send notify segment size */
pengfei.liufda2a972020-04-09 14:47:15 +080068 uint32_t block_size; /**< DVR record block size */
hualing chenf9867402020-09-23 17:06:20 +080069 DVR_Bool_t is_new_dmx; /**< DVR is used new dmx driver */
hualing chen40accc32021-04-21 15:58:09 +080070 int index_type; /**< DVR is used pcr or local time */
Pengfei Liuc181a982020-01-07 19:27:13 +080071} DVR_RecordContext_t;
72
Gong Ke2a0ebbe2021-05-25 15:22:50 +080073extern ssize_t record_device_read_ext(Record_DeviceHandle_t handle, size_t *buf, size_t *len);
74
Pengfei Liuc181a982020-01-07 19:27:13 +080075static DVR_RecordContext_t record_ctx[MAX_DVR_RECORD_SESSION_COUNT] = {
76 {
Pengfei Liub4734232020-01-17 18:25:10 +080077 .state = DVR_RECORD_STATE_CLOSED
Pengfei Liuc181a982020-01-07 19:27:13 +080078 },
79 {
Pengfei Liub4734232020-01-17 18:25:10 +080080 .state = DVR_RECORD_STATE_CLOSED
Pengfei Liuc181a982020-01-07 19:27:13 +080081 }
82};
83
Zhiqiang Han51646a02020-04-05 18:43:22 +080084static int record_is_valid_pid(DVR_RecordContext_t *p_ctx, int pid)
85{
86 int i;
87
88 for (i = 0; i < p_ctx->segment_info.nb_pids; i++) {
89 if (pid == p_ctx->segment_info.pids[i].pid)
90 return 1;
91 }
92 return 0;
93}
94
pengfei.liuab5a2262020-02-14 17:33:40 +080095static int record_save_pcr(DVR_RecordContext_t *p_ctx, uint8_t *buf, loff_t pos)
96{
97 uint8_t *p = buf;
98 int len;
99 uint8_t afc;
100 uint64_t pcr = 0;
101 int has_pcr = 0;
102 int pid;
103 int adp_field_len;
104
105 pid = ((p[1] & 0x1f) << 8) | p[2];
Zhiqiang Han51646a02020-04-05 18:43:22 +0800106 if (pid == 0x1fff || !record_is_valid_pid(p_ctx, pid))
pengfei.liuab5a2262020-02-14 17:33:40 +0800107 return has_pcr;
108
109 //scramble = p[3] >> 6;
110 //cc = p[3] & 0x0f;
111 afc = (p[3] >> 4) & 0x03;
112
113 p += 4;
114 len = 184;
115
116 if (afc & 2) {
117 adp_field_len = p[0];
118 /* Skip adaptation len */
119 p++;
120 len--;
121 /* Parse pcr field, see 13818 spec table I-2-6,adaptation_field */
hualing chen5605eed2020-05-26 18:18:06 +0800122 if (p[0] & 0x10 && len >= 6 && adp_field_len >= 6) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800123 /* get pcr value,pcr is 33bit value */
124 pcr = (((uint64_t)(p[1])) << 25)
125 | (((uint64_t)p[2]) << 17)
126 | (((uint64_t)(p[3])) << 9)
127 | (((uint64_t)p[4]) << 1)
128 | ((((uint64_t)p[5]) & 0x80) >> 7);
129 has_pcr = 1;
130 }
131
132 p += adp_field_len;
133 len -= adp_field_len;
134
135 if (len < 0) {
136 DVR_DEBUG(1, "parser pcr: illegal adaptation field length");
137 return 0;
138 }
139 }
140
hualing chen40accc32021-04-21 15:58:09 +0800141 if (has_pcr && p_ctx->index_type == DVR_INDEX_TYPE_PCR) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800142 segment_update_pts(p_ctx->segment_handle, pcr/90, pos);
143 }
144 return has_pcr;
145}
146
147static int record_do_pcr_index(DVR_RecordContext_t *p_ctx, uint8_t *buf, int len)
148{
149 uint8_t *p = buf;
150 int left = len;
151 loff_t pos;
152 int has_pcr = 0;
153
154 pos = segment_tell_position(p_ctx->segment_handle);
155 while (left >= 188) {
156 if (*p == 0x47) {
157 has_pcr |= record_save_pcr(p_ctx, p, pos);
158 p += 188;
159 left -= 188;
160 pos += 188;
161 } else {
162 p++;
163 left --;
164 pos++;
165 }
166 }
167 return has_pcr;
168}
169
pengfei.liu567d6d82020-04-17 16:48:59 +0800170static int get_diff_time(struct timeval start_tv, struct timeval end_tv)
171{
172 return end_tv.tv_sec * 1000 + end_tv.tv_usec / 1000 - start_tv.tv_sec * 1000 - start_tv.tv_usec / 1000;
173}
174
Pengfei Liuc181a982020-01-07 19:27:13 +0800175void *record_thread(void *arg)
176{
177 DVR_RecordContext_t *p_ctx = (DVR_RecordContext_t *)arg;
178 ssize_t len;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800179 uint8_t *buf, *buf_out;
pengfei.liufda2a972020-04-09 14:47:15 +0800180 uint32_t block_size = p_ctx->block_size;
pengfei.liuab5a2262020-02-14 17:33:40 +0800181 loff_t pos = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800182 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800183 struct timespec start_ts, end_ts;
184 DVR_RecordStatus_t record_status;
pengfei.liuab5a2262020-02-14 17:33:40 +0800185 int has_pcr;
hualing chen40accc32021-04-21 15:58:09 +0800186
hualing chen87072a82020-03-12 16:20:12 +0800187 time_t pre_time = 0;
hualing chen2932d372020-04-29 13:44:00 +0800188 #define DVR_STORE_INFO_TIME (400)
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800189 DVR_SecureBuffer_t secure_buf;
hualing chenf9867402020-09-23 17:06:20 +0800190 DVR_NewDmxSecureBuffer_t new_dmx_secure_buf;
hualing chend241c7a2021-06-22 13:34:27 +0800191
192 if (CONTROL_SPEED_ENABLE == 0)
193 p_ctx->index_type = DVR_INDEX_TYPE_INVALID;
194 else
195 p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
Pengfei Liuc181a982020-01-07 19:27:13 +0800196
Pengfei Liub4734232020-01-17 18:25:10 +0800197 buf = (uint8_t *)malloc(block_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800198 if (!buf) {
199 DVR_DEBUG(1, "%s, malloc failed", __func__);
200 return NULL;
201 }
202
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800203 buf_out = (uint8_t *)malloc(block_size + 188);
204 if (!buf_out) {
205 DVR_DEBUG(1, "%s, malloc failed", __func__);
Pengfei Liufaf38e42020-05-22 00:28:02 +0800206 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800207 return NULL;
208 }
209
hualing chen266b9502020-04-04 17:39:39 +0800210 memset(&record_status, 0, sizeof(record_status));
211 record_status.state = DVR_RECORD_STATE_STARTED;
pengfei.liufda2a972020-04-09 14:47:15 +0800212 if (p_ctx->event_notify_fn) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800213 record_status.info.id = p_ctx->segment_info.id;
pengfei.liufda2a972020-04-09 14:47:15 +0800214 p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata);
hualing chen4b7c15d2020-04-07 16:13:48 +0800215 DVR_DEBUG(1, "%s line %d notify record status, state:%d id=%lld",
216 __func__,__LINE__, record_status.state, p_ctx->segment_info.id);
pengfei.liufda2a972020-04-09 14:47:15 +0800217 }
218 DVR_DEBUG(1, "%s, secure_mode:%d, block_size:%d", __func__, p_ctx->is_secure_mode, block_size);
Pengfei Liub4734232020-01-17 18:25:10 +0800219 clock_gettime(CLOCK_MONOTONIC, &start_ts);
pengfei.liu567d6d82020-04-17 16:48:59 +0800220
221 struct timeval t1, t2, t3, t4, t5, t6, t7;
Pengfei Liuc181a982020-01-07 19:27:13 +0800222 while (p_ctx->state == DVR_RECORD_STATE_STARTED) {
pengfei.liu567d6d82020-04-17 16:48:59 +0800223 gettimeofday(&t1, NULL);
hualing chenc70a8df2020-05-12 19:23:11 +0800224
pengfei.liuab5a2262020-02-14 17:33:40 +0800225 /* data from dmx, normal dvr case */
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800226 if (p_ctx->is_secure_mode) {
hualing chenf9867402020-09-23 17:06:20 +0800227 if (p_ctx->is_new_dmx) {
Yahui Hance15e9c2020-12-08 18:08:32 +0800228
229 /* We resolve the below invoke for dvbcore to be under safety status */
hualing chenf9867402020-09-23 17:06:20 +0800230 memset(&new_dmx_secure_buf, 0, sizeof(new_dmx_secure_buf));
Yahui Hance15e9c2020-12-08 18:08:32 +0800231 len = record_device_read(p_ctx->dev_handle, &new_dmx_secure_buf, sizeof(new_dmx_secure_buf), 10);
232 if (len == DVR_FAILURE) {
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800233 DVR_DEBUG(1, "handle[%p] ret:%d\n", p_ctx->dev_handle, ret);
Yahui Hance15e9c2020-12-08 18:08:32 +0800234 /*For the second recording, poll always failed which we should check
235 * dvbcore further. For now, Just ignore the fack poll fail, I think
236 * it won't influce anything. But we need adjust the poll timeout
237 * from 1000ms to 10ms.
238 */
239 //continue;
240 }
241
242 /* Read data from secure demux TA */
243 len = record_device_read_ext(p_ctx->dev_handle, &secure_buf.addr,
244 &secure_buf.len);
245
hualing chenf9867402020-09-23 17:06:20 +0800246 } else {
247 memset(&secure_buf, 0, sizeof(secure_buf));
248 len = record_device_read(p_ctx->dev_handle, &secure_buf, sizeof(secure_buf), 1000);
249 }
250 if (len != DVR_FAILURE) {
hualing chen4fe3bee2020-10-23 13:58:52 +0800251 //DVR_DEBUG(1, "%s, secure_buf:%#x, size:%#x", __func__, secure_buf.addr, secure_buf.len);
hualing chenf9867402020-09-23 17:06:20 +0800252 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800253 } else {
254 len = record_device_read(p_ctx->dev_handle, buf, block_size, 1000);
255 }
Pengfei Liub4734232020-01-17 18:25:10 +0800256 if (len == DVR_FAILURE) {
hualing chen6c126382020-04-13 15:47:51 +0800257 //usleep(10*1000);
hualing chen2932d372020-04-29 13:44:00 +0800258 DVR_DEBUG(1, "%s, start_read error", __func__);
Pengfei Liub4734232020-01-17 18:25:10 +0800259 continue;
260 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800261 gettimeofday(&t2, NULL);
hualing chenc70a8df2020-05-12 19:23:11 +0800262
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800263 /* Got data from device, record it */
264 if (p_ctx->enc_func) {
265 /* Encrypt record data */
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800266 DVR_CryptoParams_t crypto_params;
267
268 memset(&crypto_params, 0, sizeof(crypto_params));
269 crypto_params.type = DVR_CRYPTO_TYPE_ENCRYPT;
hualing chen7a56cba2020-04-14 14:09:27 +0800270 memcpy(crypto_params.location, p_ctx->location, sizeof(p_ctx->location));
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800271 crypto_params.segment_id = p_ctx->segment_info.id;
272 crypto_params.offset = p_ctx->segment_info.size;
273
274 if (p_ctx->is_secure_mode) {
275 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_SECURE;
Yahui Hance15e9c2020-12-08 18:08:32 +0800276#if 0
hualing chenf9867402020-09-23 17:06:20 +0800277 if (p_ctx->is_new_dmx) {
278 crypto_params.input_buffer.addr = new_dmx_secure_buf.data_start;
279 crypto_params.input_buffer.size = new_dmx_secure_buf.data_end - new_dmx_secure_buf.data_start;
Yahui Hance15e9c2020-12-08 18:08:32 +0800280 } else
281#endif
282 {
hualing chenf9867402020-09-23 17:06:20 +0800283 crypto_params.input_buffer.addr = secure_buf.addr;
284 crypto_params.input_buffer.size = secure_buf.len;
285 }
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800286 } else {
287 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
288 crypto_params.input_buffer.addr = (size_t)buf;
289 crypto_params.input_buffer.size = len;
290 }
291
292 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL;
293 crypto_params.output_buffer.addr = (size_t)buf_out;
294 crypto_params.output_buffer.size = block_size + 188;
hualing chen9811b212020-10-29 11:21:44 +0800295
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800296 p_ctx->enc_func(&crypto_params, p_ctx->enc_userdata);
pengfei.liu567d6d82020-04-17 16:48:59 +0800297 gettimeofday(&t3, NULL);
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800298 /* Out buffer length may not equal in buffer length */
hualing chenf9867402020-09-23 17:06:20 +0800299 if (crypto_params.output_size > 0) {
300 ret = segment_write(p_ctx->segment_handle, buf_out, crypto_params.output_size);
301 len = crypto_params.output_size;
hualing chenbafc62d2020-11-02 15:44:05 +0800302 } else {
303 len = 0;
hualing chenf9867402020-09-23 17:06:20 +0800304 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800305 } else {
pengfei.liu567d6d82020-04-17 16:48:59 +0800306 gettimeofday(&t3, NULL);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800307 ret = segment_write(p_ctx->segment_handle, buf, len);
308 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800309 gettimeofday(&t4, NULL);
hualing chen626204e2020-04-07 11:55:08 +0800310 //add DVR_RECORD_EVENT_WRITE_ERROR event if write error
pengfei.liufda2a972020-04-09 14:47:15 +0800311 if (ret == -1 && len > 0 && p_ctx->event_notify_fn) {
hualing chen626204e2020-04-07 11:55:08 +0800312 //send write event
hualing chen9b434f02020-06-10 15:06:54 +0800313 if (p_ctx->event_notify_fn) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800314 memset(&record_status, 0, sizeof(record_status));
hualing chen4fe3bee2020-10-23 13:58:52 +0800315 DVR_DEBUG(1, "%s:%d,send event write error", __func__,__LINE__);
hualing chen9ce7d942021-06-30 13:31:01 +0800316 record_status.info.id = p_ctx->segment_info.id;
hualing chen4b7c15d2020-04-07 16:13:48 +0800317 p_ctx->event_notify_fn(DVR_RECORD_EVENT_WRITE_ERROR, &record_status, p_ctx->event_userdata);
318 }
hualing chenc70a8df2020-05-12 19:23:11 +0800319 DVR_DEBUG(1, "%s,write error %d", __func__,__LINE__);
hualing chen626204e2020-04-07 11:55:08 +0800320 goto end;
321 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800322 /* Do time index */
323 uint8_t *index_buf = p_ctx->enc_func ? buf_out : buf;
pengfei.liuab5a2262020-02-14 17:33:40 +0800324 pos = segment_tell_position(p_ctx->segment_handle);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800325 has_pcr = record_do_pcr_index(p_ctx, index_buf, len);
hualing chen40accc32021-04-21 15:58:09 +0800326 if (has_pcr == 0 && p_ctx->index_type == DVR_INDEX_TYPE_INVALID) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800327 clock_gettime(CLOCK_MONOTONIC, &end_ts);
328 if ((end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) -
329 (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) > 40) {
330 /* PCR interval threshlod > 40 ms*/
331 DVR_DEBUG(1, "%s use local clock time index", __func__);
hualing chen40accc32021-04-21 15:58:09 +0800332 p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
pengfei.liuab5a2262020-02-14 17:33:40 +0800333 }
hualing chen40accc32021-04-21 15:58:09 +0800334 } else if (has_pcr && p_ctx->index_type == DVR_INDEX_TYPE_INVALID){
pengfei.liuab5a2262020-02-14 17:33:40 +0800335 DVR_DEBUG(1, "%s use pcr time index", __func__);
hualing chen40accc32021-04-21 15:58:09 +0800336 p_ctx->index_type = DVR_INDEX_TYPE_PCR;
Pengfei Liuc181a982020-01-07 19:27:13 +0800337 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800338 gettimeofday(&t5, NULL);
pengfei.liuab5a2262020-02-14 17:33:40 +0800339
340 /* Update segment info */
Pengfei Liub4734232020-01-17 18:25:10 +0800341 p_ctx->segment_info.size += len;
pengfei.liuab5a2262020-02-14 17:33:40 +0800342 /*Duration need use pcr to calculate, todo...*/
hualing chen40accc32021-04-21 15:58:09 +0800343 if (p_ctx->index_type == DVR_INDEX_TYPE_PCR) {
pengfei.liu8b563292020-02-26 15:49:02 +0800344 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
hualing chen87072a82020-03-12 16:20:12 +0800345 if (pre_time == 0)
346 pre_time = p_ctx->segment_info.duration;
hualing chen40accc32021-04-21 15:58:09 +0800347 } else if (p_ctx->index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) {
pengfei.liuab5a2262020-02-14 17:33:40 +0800348 clock_gettime(CLOCK_MONOTONIC, &end_ts);
349 p_ctx->segment_info.duration = (end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) -
350 (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000);
hualing chen87072a82020-03-12 16:20:12 +0800351 if (pre_time == 0)
352 pre_time = p_ctx->segment_info.duration;
pengfei.liuab5a2262020-02-14 17:33:40 +0800353 segment_update_pts(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
354 } else {
355 DVR_DEBUG(1, "%s can NOT do time index", __func__);
356 }
357 p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188;
Pengfei Liub4734232020-01-17 18:25:10 +0800358
hualing chen87072a82020-03-12 16:20:12 +0800359 if (p_ctx->segment_info.duration - pre_time > DVR_STORE_INFO_TIME) {
360 pre_time = p_ctx->segment_info.duration + DVR_STORE_INFO_TIME;
hualing chenf9867402020-09-23 17:06:20 +0800361 segment_store_info(p_ctx->segment_handle, &(p_ctx->segment_info));
hualing chen87072a82020-03-12 16:20:12 +0800362 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800363 gettimeofday(&t6, NULL);
hualing chen87072a82020-03-12 16:20:12 +0800364 /*Event notification*/
Pengfei Liub4734232020-01-17 18:25:10 +0800365 if (p_ctx->notification_size &&
366 p_ctx->event_notify_fn &&
hualing chen2615aa82020-04-02 21:32:51 +0800367 /*!(p_ctx->segment_info.size % p_ctx->notification_size)*/
368 (p_ctx->segment_info.size -p_ctx->last_send_size) >= p_ctx->notification_size&&
pengfei.liuab5a2262020-02-14 17:33:40 +0800369 p_ctx->segment_info.duration > 0) {
Pengfei Liub4734232020-01-17 18:25:10 +0800370 memset(&record_status, 0, sizeof(record_status));
pengfei.liuab5a2262020-02-14 17:33:40 +0800371 //clock_gettime(CLOCK_MONOTONIC, &end_ts);
hualing chen2615aa82020-04-02 21:32:51 +0800372 p_ctx->last_send_size = p_ctx->segment_info.size;
Pengfei Liub4734232020-01-17 18:25:10 +0800373 record_status.state = p_ctx->state;
374 record_status.info.id = p_ctx->segment_info.id;
pengfei.liuab5a2262020-02-14 17:33:40 +0800375 record_status.info.duration = p_ctx->segment_info.duration;
Pengfei Liub4734232020-01-17 18:25:10 +0800376 record_status.info.size = p_ctx->segment_info.size;
377 record_status.info.nb_packets = p_ctx->segment_info.size/188;
378 p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata);
hualing chen4fe3bee2020-10-23 13:58:52 +0800379 DVR_DEBUG(1, "%s notify record status, state:%d, id:%lld, duration:%ld ms, size:%zu loc[%s]",
380 __func__, record_status.state,
381 record_status.info.id, record_status.info.duration,
382 record_status.info.size, p_ctx->location);
Pengfei Liub4734232020-01-17 18:25:10 +0800383 }
pengfei.liu567d6d82020-04-17 16:48:59 +0800384 gettimeofday(&t7, NULL);
385#ifdef DEBUG_PERFORMANCE
hualing chen2932d372020-04-29 13:44:00 +0800386 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 +0800387 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 +0800388 get_diff_time(t5, t6), get_diff_time(t6, t7), get_diff_time(t1, t5), len);
pengfei.liu567d6d82020-04-17 16:48:59 +0800389#endif
Pengfei Liuc181a982020-01-07 19:27:13 +0800390 }
hualing chen626204e2020-04-07 11:55:08 +0800391end:
Pengfei Liub4734232020-01-17 18:25:10 +0800392 free((void *)buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800393 free((void *)buf_out);
Pengfei Liub4734232020-01-17 18:25:10 +0800394 DVR_DEBUG(1, "exit %s", __func__);
Pengfei Liuc181a982020-01-07 19:27:13 +0800395 return NULL;
396}
397
398int dvr_record_open(DVR_RecordHandle_t *p_handle, DVR_RecordOpenParams_t *params)
399{
400 DVR_RecordContext_t *p_ctx;
401 Record_DeviceOpenParams_t dev_open_params;
402 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800403 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800404
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800405 DVR_RETURN_IF_FALSE(p_handle);
406 DVR_RETURN_IF_FALSE(params);
Pengfei Liuc181a982020-01-07 19:27:13 +0800407
408 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
Pengfei Liub4734232020-01-17 18:25:10 +0800409 if (record_ctx[i].state == DVR_RECORD_STATE_CLOSED) {
Pengfei Liuc181a982020-01-07 19:27:13 +0800410 break;
411 }
412 }
Pengfei Liufaf38e42020-05-22 00:28:02 +0800413 DVR_RETURN_IF_FALSE(i < MAX_DVR_RECORD_SESSION_COUNT);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800414 DVR_RETURN_IF_FALSE(record_ctx[i].state == DVR_RECORD_STATE_CLOSED);
Pengfei Liuc181a982020-01-07 19:27:13 +0800415 p_ctx = &record_ctx[i];
hualing chenfbf8e022020-06-15 13:43:11 +0800416 DVR_DEBUG(1, "%s , current state:%d, dmx_id:%d, notification_size:%zu ", __func__,
Pengfei Liub4734232020-01-17 18:25:10 +0800417 p_ctx->state, params->dmx_dev_id, params->notification_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800418
Pengfei Liub4734232020-01-17 18:25:10 +0800419 /*Process event params*/
420 p_ctx->notification_size = params->notification_size;
421 p_ctx->event_notify_fn = params->event_fn;
422 p_ctx->event_userdata = params->event_userdata;
hualing chen2615aa82020-04-02 21:32:51 +0800423 p_ctx->last_send_size = 0;
hualing chenf9867402020-09-23 17:06:20 +0800424 //check is new driver
425 p_ctx->is_new_dmx = dvr_check_dmx_isNew();
Pengfei Liub4734232020-01-17 18:25:10 +0800426 /*Process crypto params, todo*/
Pengfei Liub4734232020-01-17 18:25:10 +0800427 memset((void *)&dev_open_params, 0, sizeof(dev_open_params));
pengfei.liuab5a2262020-02-14 17:33:40 +0800428 if (params->data_from_memory) {
429 /* data from memory, VOD case */
430 p_ctx->is_vod = 1;
431 } else {
432 p_ctx->is_vod = 0;
433 /* data from dmx, normal dvr case */
hualing chen958fe4c2020-03-24 17:35:07 +0800434 dev_open_params.dmx_dev_id = params->dmx_dev_id;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800435 dev_open_params.buf_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE);
hualing chen9811b212020-10-29 11:21:44 +0800436 if (p_ctx->is_new_dmx)
hualing cheneceb37e2021-01-18 16:07:48 +0800437 dev_open_params.buf_size = NEW_DEVICE_RECORD_BLOCK_SIZE * 30;
pengfei.liuab5a2262020-02-14 17:33:40 +0800438 ret = record_device_open(&p_ctx->dev_handle, &dev_open_params);
439 if (ret != DVR_SUCCESS) {
440 DVR_DEBUG(1, "%s, open record devices failed", __func__);
441 return DVR_FAILURE;
442 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800443 }
444
pengfei.liufda2a972020-04-09 14:47:15 +0800445 p_ctx->block_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE);
hualing chen9811b212020-10-29 11:21:44 +0800446 if (p_ctx->is_new_dmx)
Yahui Hance15e9c2020-12-08 18:08:32 +0800447 p_ctx->block_size = NEW_DEVICE_RECORD_BLOCK_SIZE * 30;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800448 p_ctx->enc_func = NULL;
449 p_ctx->enc_userdata = NULL;
450 p_ctx->is_secure_mode = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800451 p_ctx->state = DVR_RECORD_STATE_OPENED;
hualing chen9811b212020-10-29 11:21:44 +0800452 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 +0800453 *p_handle = p_ctx;
Pengfei Liuc181a982020-01-07 19:27:13 +0800454 return DVR_SUCCESS;
455}
456
457int dvr_record_close(DVR_RecordHandle_t handle)
458{
459 DVR_RecordContext_t *p_ctx;
460 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800461 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800462
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800463 p_ctx = (DVR_RecordContext_t *)handle;
464 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
465 if (p_ctx == &record_ctx[i])
466 break;
467 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800468 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800469
Pengfei Liub4734232020-01-17 18:25:10 +0800470 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800471 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
Pengfei Liuc181a982020-01-07 19:27:13 +0800472
pengfei.liuab5a2262020-02-14 17:33:40 +0800473 if (p_ctx->is_vod) {
474 ret = DVR_SUCCESS;
475 } else {
476 ret = record_device_close(p_ctx->dev_handle);
477 if (ret != DVR_SUCCESS) {
478 DVR_DEBUG(1, "%s, failed", __func__);
479 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800480 }
481
Pengfei Liub4734232020-01-17 18:25:10 +0800482 p_ctx->state = DVR_RECORD_STATE_CLOSED;
Pengfei Liuc181a982020-01-07 19:27:13 +0800483 return ret;
484}
485
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800486#if 0
Pengfei Liuc181a982020-01-07 19:27:13 +0800487int dvr_record_register_encryption(DVR_RecordHandle_t handle,
488 DVR_CryptoFunction_t cb,
489 DVR_CryptoParams_t params,
490 void *userdata)
491{
492 return DVR_SUCCESS;
493}
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800494#endif
Pengfei Liuc181a982020-01-07 19:27:13 +0800495
496int dvr_record_start_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params)
497{
498 DVR_RecordContext_t *p_ctx;
499 Segment_OpenParams_t open_params;
500 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800501 uint32_t i;
Pengfei Liuc181a982020-01-07 19:27:13 +0800502
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800503 p_ctx = (DVR_RecordContext_t *)handle;
504 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
505 if (p_ctx == &record_ctx[i])
506 break;
507 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800508 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800509
hualing chen4fe3bee2020-10-23 13:58:52 +0800510 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 +0800511 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
512 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
513 DVR_RETURN_IF_FALSE(params);
Pengfei Liuc181a982020-01-07 19:27:13 +0800514
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800515 DVR_RETURN_IF_FALSE(strlen((const char *)params->location) < DVR_MAX_LOCATION_SIZE);
Pengfei Liuc181a982020-01-07 19:27:13 +0800516 memset(&open_params, 0, sizeof(open_params));
hualing chen7a56cba2020-04-14 14:09:27 +0800517 memcpy(open_params.location, params->location, sizeof(params->location));
Pengfei Liuc181a982020-01-07 19:27:13 +0800518 open_params.segment_id = params->segment.segment_id;
519 open_params.mode = SEGMENT_MODE_WRITE;
520
521 ret = segment_open(&open_params, &p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800522 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800523
Pengfei Liub4734232020-01-17 18:25:10 +0800524 /*process params*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800525 {
hualing chen7a56cba2020-04-14 14:09:27 +0800526 memcpy(p_ctx->location, params->location, sizeof(params->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800527 //need all params??
Pengfei Liuc181a982020-01-07 19:27:13 +0800528 memcpy(&p_ctx->segment_params, &params->segment, sizeof(params->segment));
Pengfei Liub4734232020-01-17 18:25:10 +0800529 /*save current segment info*/
530 memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info));
531 p_ctx->segment_info.id = params->segment.segment_id;
532 p_ctx->segment_info.nb_pids = params->segment.nb_pids;
533 memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800534 }
535
pengfei.liuab5a2262020-02-14 17:33:40 +0800536 if (!p_ctx->is_vod) {
537 /* normal dvr case */
538 for (i = 0; i < params->segment.nb_pids; i++) {
539 ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
540 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
541 }
542 ret = record_device_start(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800543 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800544 }
545
Zhiqiang Han5ebad992020-04-28 18:19:59 +0800546 ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info);
547
Pengfei Liuc181a982020-01-07 19:27:13 +0800548 p_ctx->state = DVR_RECORD_STATE_STARTED;
pengfei.liuab5a2262020-02-14 17:33:40 +0800549 if (!p_ctx->is_vod)
550 pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx);
Pengfei Liub4734232020-01-17 18:25:10 +0800551
Pengfei Liuc181a982020-01-07 19:27:13 +0800552 return DVR_SUCCESS;
553}
554
555int dvr_record_next_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, DVR_RecordSegmentInfo_t *p_info)
556{
557 DVR_RecordContext_t *p_ctx;
Pengfei Liub4734232020-01-17 18:25:10 +0800558 Segment_OpenParams_t open_params;
Pengfei Liuc181a982020-01-07 19:27:13 +0800559 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800560 uint32_t i;
hualing chen2932d372020-04-29 13:44:00 +0800561 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800562
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800563 p_ctx = (DVR_RecordContext_t *)handle;
564 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
565 if (p_ctx == &record_ctx[i])
566 break;
567 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800568 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800569
hualing chen4fe3bee2020-10-23 13:58:52 +0800570 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 +0800571 DVR_RETURN_IF_FALSE(p_ctx->state == DVR_RECORD_STATE_STARTED);
572 //DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
573 DVR_RETURN_IF_FALSE(params);
574 DVR_RETURN_IF_FALSE(p_info);
pengfei.liuab5a2262020-02-14 17:33:40 +0800575 DVR_RETURN_IF_FALSE(!p_ctx->is_vod);
Pengfei Liuc181a982020-01-07 19:27:13 +0800576
577 /*Stop the on going record segment*/
Pengfei Liub4734232020-01-17 18:25:10 +0800578 //ret = record_device_stop(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800579 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800580 p_ctx->state = DVR_RECORD_STATE_STOPPED;
581 pthread_join(p_ctx->thread, NULL);
582
hualing chen2932d372020-04-29 13:44:00 +0800583 //add index file store
584 pos = segment_tell_position(p_ctx->segment_handle);
585 segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
586
587 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
Pengfei Liub4734232020-01-17 18:25:10 +0800588 /*Update segment info*/
589 memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
590
591 ret = segment_store_info(p_ctx->segment_handle, p_info);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800592 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liuc181a982020-01-07 19:27:13 +0800593
hualing chena540a7e2020-03-27 16:44:05 +0800594 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",
595 __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 +0800596
Pengfei Liub4734232020-01-17 18:25:10 +0800597 /*Close current segment*/
598 ret = segment_close(p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800599 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800600 /*Open the new record segment*/
601 memset(&open_params, 0, sizeof(open_params));
hualing chen7a56cba2020-04-14 14:09:27 +0800602 memcpy(open_params.location, p_ctx->location, sizeof(p_ctx->location));
Pengfei Liub4734232020-01-17 18:25:10 +0800603 open_params.segment_id = params->segment.segment_id;
604 open_params.mode = SEGMENT_MODE_WRITE;
hualing chen7a56cba2020-04-14 14:09:27 +0800605 DVR_DEBUG(1, "%s: p_ctx->location:%s params->location:%s", __func__, p_ctx->location,params->location);
Pengfei Liub4734232020-01-17 18:25:10 +0800606
607 ret = segment_open(&open_params, &p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800608 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800609 /*process params*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800610 {
Pengfei Liub4734232020-01-17 18:25:10 +0800611 //need all params??
Pengfei Liuc181a982020-01-07 19:27:13 +0800612 memcpy(&p_ctx->segment_params, &params->segment, sizeof(params->segment));
Pengfei Liub4734232020-01-17 18:25:10 +0800613 /*save current segment info*/
614 memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info));
615 p_ctx->segment_info.id = params->segment.segment_id;
616 memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t));
Pengfei Liuc181a982020-01-07 19:27:13 +0800617 }
618
Pengfei Liub4734232020-01-17 18:25:10 +0800619 p_ctx->segment_info.nb_pids = 0;
Pengfei Liuc181a982020-01-07 19:27:13 +0800620 for (i = 0; i < params->segment.nb_pids; i++) {
Pengfei Liub4734232020-01-17 18:25:10 +0800621 switch (params->segment.pid_action[i]) {
622 case DVR_RECORD_PID_CREATE:
623 DVR_DEBUG(1, "%s create pid:%d", __func__, params->segment.pids[i].pid);
624 ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
625 p_ctx->segment_info.nb_pids++;
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800626 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800627 break;
628 case DVR_RECORD_PID_KEEP:
629 DVR_DEBUG(1, "%s keep pid:%d", __func__, params->segment.pids[i].pid);
630 p_ctx->segment_info.nb_pids++;
631 break;
632 case DVR_RECORD_PID_CLOSE:
633 DVR_DEBUG(1, "%s close pid:%d", __func__, params->segment.pids[i].pid);
634 ret = record_device_remove_pid(p_ctx->dev_handle, params->segment.pids[i].pid);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800635 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800636 break;
637 default:
638 DVR_DEBUG(1, "%s wrong action pid:%d", __func__, params->segment.pids[i].pid);
639 return DVR_FAILURE;
640 }
Pengfei Liuc181a982020-01-07 19:27:13 +0800641 }
642
Pengfei Liub4734232020-01-17 18:25:10 +0800643 //ret = record_device_start(p_ctx->dev_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800644 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
hualing chen4b7c15d2020-04-07 16:13:48 +0800645 /*Update segment info*/
646 ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info);
Pengfei Liuc181a982020-01-07 19:27:13 +0800647
Pengfei Liuc181a982020-01-07 19:27:13 +0800648 p_ctx->state = DVR_RECORD_STATE_STARTED;
Zhiqiang Han0d60f2b2020-05-26 14:39:54 +0800649 pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx);
Pengfei Liuc181a982020-01-07 19:27:13 +0800650 return DVR_SUCCESS;
651}
652
653int dvr_record_stop_segment(DVR_RecordHandle_t handle, DVR_RecordSegmentInfo_t *p_info)
654{
655 DVR_RecordContext_t *p_ctx;
656 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800657 uint32_t i;
hualing chen2932d372020-04-29 13:44:00 +0800658 loff_t pos;
Pengfei Liuc181a982020-01-07 19:27:13 +0800659
Pengfei Liu47ed6c92020-01-17 11:23:41 +0800660 p_ctx = (DVR_RecordContext_t *)handle;
661 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
662 if (p_ctx == &record_ctx[i])
663 break;
664 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800665 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
Pengfei Liuc181a982020-01-07 19:27:13 +0800666
hualing chen4fe3bee2020-10-23 13:58:52 +0800667 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 +0800668 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STOPPED);
669 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800670 DVR_RETURN_IF_FALSE(p_info);/*should support NULL*/
Pengfei Liuc181a982020-01-07 19:27:13 +0800671
Pengfei Liuc181a982020-01-07 19:27:13 +0800672 p_ctx->state = DVR_RECORD_STATE_STOPPED;
pengfei.liuab5a2262020-02-14 17:33:40 +0800673 if (p_ctx->is_vod) {
pengfei.liu8b563292020-02-26 15:49:02 +0800674 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
pengfei.liuab5a2262020-02-14 17:33:40 +0800675 p_ctx->segment_info.duration = 10*1000; //debug, should delete it
676 } else {
677 ret = record_device_stop(p_ctx->dev_handle);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800678 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
679 if (ret != DVR_SUCCESS)
680 goto end;
pengfei.liuab5a2262020-02-14 17:33:40 +0800681 //p_ctx->state = DVR_RECORD_STATE_STOPPED;
682 pthread_join(p_ctx->thread, NULL);
683 }
684
hualing chen2932d372020-04-29 13:44:00 +0800685 //add index file store
686 pos = segment_tell_position(p_ctx->segment_handle);
687 segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos);
hualing chene41f4372020-06-06 16:29:17 +0800688 p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle);
Pengfei Liuc181a982020-01-07 19:27:13 +0800689
Pengfei Liub4734232020-01-17 18:25:10 +0800690 /*Update segment info*/
691 memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
692
693 ret = segment_store_info(p_ctx->segment_handle, p_info);
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800694 //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
695 if (ret != DVR_SUCCESS)
696 goto end;
Pengfei Liub4734232020-01-17 18:25:10 +0800697
698 DVR_DEBUG(1, "%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d",
699 __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets);
700
Zhiqiang Han5c805cf2020-05-09 16:51:08 +0800701end:
Pengfei Liuc181a982020-01-07 19:27:13 +0800702 ret = segment_close(p_ctx->segment_handle);
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800703 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
Pengfei Liub4734232020-01-17 18:25:10 +0800704 return DVR_SUCCESS;
705}
Pengfei Liuc181a982020-01-07 19:27:13 +0800706
Pengfei Liub4734232020-01-17 18:25:10 +0800707int dvr_record_resume_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, uint64_t *p_resume_size)
708{
709 DVR_RecordContext_t *p_ctx;
710 uint32_t i;
pengfei.liuab5a2262020-02-14 17:33:40 +0800711 int ret;
Pengfei Liub4734232020-01-17 18:25:10 +0800712
713 p_ctx = (DVR_RecordContext_t *)handle;
714 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
715 if (p_ctx == &record_ctx[i])
716 break;
Pengfei Liuc181a982020-01-07 19:27:13 +0800717 }
Pengfei Liu3b1a8202020-02-12 23:04:21 +0800718 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
719 DVR_RETURN_IF_FALSE(params);
720 DVR_RETURN_IF_FALSE(p_resume_size);
Pengfei Liuc181a982020-01-07 19:27:13 +0800721
pengfei.liuab5a2262020-02-14 17:33:40 +0800722 DVR_DEBUG(1, "%s , current state:%d, resume size:%lld", __func__, p_ctx->state, *p_resume_size);
723 ret = dvr_record_start_segment(handle, params);
724 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
725
726 p_ctx->segment_info.size = *p_resume_size;
727
728 return DVR_SUCCESS;
729}
730
731int dvr_record_get_status(DVR_RecordHandle_t handle, DVR_RecordStatus_t *p_status)
732{
733 DVR_RecordContext_t *p_ctx;
734 int i;
735
736 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 }
741 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
742 DVR_RETURN_IF_FALSE(p_status);
743
744 //lock
745 p_status->state = p_ctx->state;
746 p_status->info.id = p_ctx->segment_info.id;
747 p_status->info.duration = p_ctx->segment_info.duration;
748 p_status->info.size = p_ctx->segment_info.size;
749 p_status->info.nb_packets = p_ctx->segment_info.size/188;
750
751 return DVR_SUCCESS;
752}
753
754int dvr_record_write(DVR_RecordHandle_t handle, void *buffer, uint32_t len)
755{
756 DVR_RecordContext_t *p_ctx;
757 uint32_t i;
758 off_t pos = 0;
759 int ret;
760 int has_pcr;
761
762 p_ctx = (DVR_RecordContext_t *)handle;
763 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
764 if (p_ctx == &record_ctx[i])
765 break;
766 }
767 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
768 DVR_RETURN_IF_FALSE(buffer);
769 DVR_RETURN_IF_FALSE(len);
770
771 pos = segment_tell_position(p_ctx->segment_handle);
772 has_pcr = record_do_pcr_index(p_ctx, buffer, len);
773 if (has_pcr == 0) {
774 /* Pull VOD record shoud use PCR time index */
775 DVR_DEBUG(1, "%s has no pcr, can NOT do time index", __func__);
776 }
777 ret = segment_write(p_ctx->segment_handle, buffer, len);
hualing chen2932d372020-04-29 13:44:00 +0800778 if (ret != len) {
779 DVR_DEBUG(1, "%s write error ret:%d len:%d", __func__, ret, len);
780 }
pengfei.liuab5a2262020-02-14 17:33:40 +0800781 p_ctx->segment_info.size += len;
782 p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188;
783
Pengfei Liuc181a982020-01-07 19:27:13 +0800784 return DVR_SUCCESS;
785}
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800786
pengfei.liu27cc4ec2020-04-03 16:28:16 +0800787int dvr_record_set_encrypt_callback(DVR_RecordHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800788{
789 DVR_RecordContext_t *p_ctx;
790 uint32_t i;
791
792 p_ctx = (DVR_RecordContext_t *)handle;
793 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
794 if (p_ctx == &record_ctx[i])
795 break;
796 }
797 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
798 DVR_RETURN_IF_FALSE(func);
799
800 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
801 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
802 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
803
804 p_ctx->enc_func = func;
805 p_ctx->enc_userdata = userdata;
806 return DVR_SUCCESS;
807}
808
809int dvr_record_set_secure_buffer(DVR_RecordHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
810{
811 DVR_RecordContext_t *p_ctx;
812 uint32_t i;
813 int ret;
814
815 p_ctx = (DVR_RecordContext_t *)handle;
816 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
817 if (p_ctx == &record_ctx[i])
818 break;
819 }
820 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
821 DVR_RETURN_IF_FALSE(p_secure_buf);
822 DVR_RETURN_IF_FALSE(len);
823
824 DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state);
825 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED);
826 DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED);
827
828 ret = record_device_set_secure_buffer(p_ctx->dev_handle, p_secure_buf, len);
829 DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
830
831 p_ctx->is_secure_mode = 1;
832 return ret;
833}
hualing chen4fe3bee2020-10-23 13:58:52 +0800834
835int dvr_record_is_secure_mode(DVR_RecordHandle_t handle)
836{
837 DVR_RecordContext_t *p_ctx;
838 uint32_t i;
839 int ret;
840
841 p_ctx = (DVR_RecordContext_t *)handle;
842 for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) {
843 if (p_ctx == &record_ctx[i])
844 break;
845 }
846 DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]);
847
848 if (p_ctx->is_secure_mode == 1)
849 ret = 1;
850 else
851 ret = 0;
852 return ret;
Yahui Hance15e9c2020-12-08 18:08:32 +0800853}