Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 2 | #include <unistd.h> |
| 3 | #include <stdlib.h> |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 4 | #include <pthread.h> |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 5 | #include <string.h> |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 6 | #include "dvr_types.h" |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 7 | #include "dvr_record.h" |
| 8 | #include "dvr_crypto.h" |
| 9 | #include "record_device.h" |
| 10 | #include "segment.h" |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 11 | #include <sys/time.h> |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 12 | |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 13 | //#define DEBUG_PERFORMANCE |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 14 | #define MAX_DVR_RECORD_SESSION_COUNT 2 |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 15 | #define RECORD_BLOCK_SIZE (256 * 1024) |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 16 | |
| 17 | /**\brief DVR index file type*/ |
| 18 | typedef 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*/ |
| 25 | typedef 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.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 32 | /**\brief DVR record secure mode buffer*/ |
| 33 | typedef 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 chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 38 | /**\brief DVR record new dmx secure mode buffer*/ |
| 39 | typedef 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 Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 46 | /**\brief DVR record context*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 47 | typedef struct { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 48 | 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.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 58 | //DVR_VodContext_t vod; /**< DVR record vod context*/ |
| 59 | int is_vod; /**< Indicate current mode is VOD record mode*/ |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 60 | DVR_CryptoFunction_t enc_func; /**< Encrypt function*/ |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 61 | void *enc_userdata; /**< Encrypt userdata*/ |
| 62 | int is_secure_mode; /**< Record session run in secure pipeline */ |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 63 | size_t last_send_size; /**< Last send notify segment size */ |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 64 | uint32_t block_size; /**< DVR record block size */ |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 65 | DVR_Bool_t is_new_dmx; /**< DVR is used new dmx driver */ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 66 | } DVR_RecordContext_t; |
| 67 | |
| 68 | static DVR_RecordContext_t record_ctx[MAX_DVR_RECORD_SESSION_COUNT] = { |
| 69 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 70 | .state = DVR_RECORD_STATE_CLOSED |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 71 | }, |
| 72 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 73 | .state = DVR_RECORD_STATE_CLOSED |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 74 | } |
| 75 | }; |
| 76 | |
Zhiqiang Han | 51646a0 | 2020-04-05 18:43:22 +0800 | [diff] [blame] | 77 | static 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.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 88 | static 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 Han | 51646a0 | 2020-04-05 18:43:22 +0800 | [diff] [blame] | 99 | if (pid == 0x1fff || !record_is_valid_pid(p_ctx, pid)) |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 100 | 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 chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 115 | if (p[0] & 0x10 && len >= 6 && adp_field_len >= 6) { |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 116 | /* 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 | |
| 140 | static 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.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 163 | static 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 Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 168 | void *record_thread(void *arg) |
| 169 | { |
| 170 | DVR_RecordContext_t *p_ctx = (DVR_RecordContext_t *)arg; |
| 171 | ssize_t len; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 172 | uint8_t *buf, *buf_out; |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 173 | uint32_t block_size = p_ctx->block_size; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 174 | loff_t pos = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 175 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 176 | struct timespec start_ts, end_ts; |
| 177 | DVR_RecordStatus_t record_status; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 178 | int has_pcr; |
| 179 | int index_type = DVR_INDEX_TYPE_INVALID; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 180 | time_t pre_time = 0; |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 181 | #define DVR_STORE_INFO_TIME (400) |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 182 | DVR_SecureBuffer_t secure_buf; |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 183 | DVR_NewDmxSecureBuffer_t new_dmx_secure_buf; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 184 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 185 | buf = (uint8_t *)malloc(block_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 186 | if (!buf) { |
| 187 | DVR_DEBUG(1, "%s, malloc failed", __func__); |
| 188 | return NULL; |
| 189 | } |
| 190 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 191 | buf_out = (uint8_t *)malloc(block_size + 188); |
| 192 | if (!buf_out) { |
| 193 | DVR_DEBUG(1, "%s, malloc failed", __func__); |
Pengfei Liu | faf38e4 | 2020-05-22 00:28:02 +0800 | [diff] [blame] | 194 | free(buf); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 195 | return NULL; |
| 196 | } |
| 197 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 198 | memset(&record_status, 0, sizeof(record_status)); |
| 199 | record_status.state = DVR_RECORD_STATE_STARTED; |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 200 | if (p_ctx->event_notify_fn) { |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 201 | record_status.info.id = p_ctx->segment_info.id; |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 202 | p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata); |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 203 | 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.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 205 | } |
| 206 | DVR_DEBUG(1, "%s, secure_mode:%d, block_size:%d", __func__, p_ctx->is_secure_mode, block_size); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 207 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 208 | clock_gettime(CLOCK_MONOTONIC, &start_ts); |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 209 | |
| 210 | struct timeval t1, t2, t3, t4, t5, t6, t7; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 211 | while (p_ctx->state == DVR_RECORD_STATE_STARTED) { |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 212 | gettimeofday(&t1, NULL); |
hualing chen | c70a8df | 2020-05-12 19:23:11 +0800 | [diff] [blame] | 213 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 214 | /* data from dmx, normal dvr case */ |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 215 | if (p_ctx->is_secure_mode) { |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 216 | 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) { |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame^] | 224 | //DVR_DEBUG(1, "%s, secure_buf:%#x, size:%#x", __func__, secure_buf.addr, secure_buf.len); |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 225 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 226 | } else { |
| 227 | len = record_device_read(p_ctx->dev_handle, buf, block_size, 1000); |
| 228 | } |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 229 | if (len == DVR_FAILURE) { |
hualing chen | 6c12638 | 2020-04-13 15:47:51 +0800 | [diff] [blame] | 230 | //usleep(10*1000); |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 231 | DVR_DEBUG(1, "%s, start_read error", __func__); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 232 | continue; |
| 233 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 234 | gettimeofday(&t2, NULL); |
hualing chen | c70a8df | 2020-05-12 19:23:11 +0800 | [diff] [blame] | 235 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 236 | /* Got data from device, record it */ |
| 237 | if (p_ctx->enc_func) { |
| 238 | /* Encrypt record data */ |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 239 | DVR_CryptoParams_t crypto_params; |
| 240 | |
| 241 | memset(&crypto_params, 0, sizeof(crypto_params)); |
| 242 | crypto_params.type = DVR_CRYPTO_TYPE_ENCRYPT; |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 243 | memcpy(crypto_params.location, p_ctx->location, sizeof(p_ctx->location)); |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 244 | 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 chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 249 | 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.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 256 | } 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; |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame^] | 265 | if ((p_ctx->segment_info.size % (256 * 1024)) != 0) { |
| 266 | DVR_DEBUG(1, "%s:%d,offset is not 256k error", __func__,__LINE__); |
| 267 | } |
| 268 | if (p_ctx->segment_info.size == 0) { |
| 269 | DVR_DEBUG(1, "%s:%d,offset is 0k success", __func__,__LINE__); |
| 270 | } |
| 271 | if (p_ctx->is_secure_mode && secure_buf.len != (256 * 1024)) { |
| 272 | DVR_DEBUG(1, "%s:%d,secure buf len is not 256k [0x%x] continue", __func__,__LINE__, secure_buf.len); |
| 273 | continue; |
| 274 | } else { |
| 275 | DVR_DEBUG(1, "%s:%d,read is 256k success", __func__,__LINE__); |
| 276 | } |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 277 | p_ctx->enc_func(&crypto_params, p_ctx->enc_userdata); |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 278 | gettimeofday(&t3, NULL); |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 279 | /* Out buffer length may not equal in buffer length */ |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 280 | if (crypto_params.output_size > 0) { |
| 281 | ret = segment_write(p_ctx->segment_handle, buf_out, crypto_params.output_size); |
| 282 | len = crypto_params.output_size; |
| 283 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 284 | } else { |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 285 | gettimeofday(&t3, NULL); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 286 | ret = segment_write(p_ctx->segment_handle, buf, len); |
| 287 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 288 | gettimeofday(&t4, NULL); |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 289 | //add DVR_RECORD_EVENT_WRITE_ERROR event if write error |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 290 | if (ret == -1 && len > 0 && p_ctx->event_notify_fn) { |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 291 | //send write event |
hualing chen | 9b434f0 | 2020-06-10 15:06:54 +0800 | [diff] [blame] | 292 | if (p_ctx->event_notify_fn) { |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 293 | memset(&record_status, 0, sizeof(record_status)); |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame^] | 294 | DVR_DEBUG(1, "%s:%d,send event write error", __func__,__LINE__); |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 295 | p_ctx->event_notify_fn(DVR_RECORD_EVENT_WRITE_ERROR, &record_status, p_ctx->event_userdata); |
| 296 | } |
hualing chen | c70a8df | 2020-05-12 19:23:11 +0800 | [diff] [blame] | 297 | DVR_DEBUG(1, "%s,write error %d", __func__,__LINE__); |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 298 | goto end; |
| 299 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 300 | /* Do time index */ |
| 301 | uint8_t *index_buf = p_ctx->enc_func ? buf_out : buf; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 302 | pos = segment_tell_position(p_ctx->segment_handle); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 303 | has_pcr = record_do_pcr_index(p_ctx, index_buf, len); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 304 | if (has_pcr == 0 && index_type == DVR_INDEX_TYPE_INVALID) { |
| 305 | clock_gettime(CLOCK_MONOTONIC, &end_ts); |
| 306 | if ((end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) - |
| 307 | (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) > 40) { |
| 308 | /* PCR interval threshlod > 40 ms*/ |
| 309 | DVR_DEBUG(1, "%s use local clock time index", __func__); |
| 310 | index_type = DVR_INDEX_TYPE_LOCAL_CLOCK; |
| 311 | } |
| 312 | } else if (has_pcr && index_type == DVR_INDEX_TYPE_INVALID){ |
| 313 | DVR_DEBUG(1, "%s use pcr time index", __func__); |
| 314 | index_type = DVR_INDEX_TYPE_PCR; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 315 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 316 | gettimeofday(&t5, NULL); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 317 | |
| 318 | /* Update segment info */ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 319 | p_ctx->segment_info.size += len; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 320 | /*Duration need use pcr to calculate, todo...*/ |
| 321 | if (index_type == DVR_INDEX_TYPE_PCR) { |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 322 | p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 323 | if (pre_time == 0) |
| 324 | pre_time = p_ctx->segment_info.duration; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 325 | } else if (index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) { |
| 326 | clock_gettime(CLOCK_MONOTONIC, &end_ts); |
| 327 | p_ctx->segment_info.duration = (end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) - |
| 328 | (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 329 | if (pre_time == 0) |
| 330 | pre_time = p_ctx->segment_info.duration; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 331 | segment_update_pts(p_ctx->segment_handle, p_ctx->segment_info.duration, pos); |
| 332 | } else { |
| 333 | DVR_DEBUG(1, "%s can NOT do time index", __func__); |
| 334 | } |
| 335 | p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 336 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 337 | if (p_ctx->segment_info.duration - pre_time > DVR_STORE_INFO_TIME) { |
| 338 | pre_time = p_ctx->segment_info.duration + DVR_STORE_INFO_TIME; |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 339 | segment_store_info(p_ctx->segment_handle, &(p_ctx->segment_info)); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 340 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 341 | gettimeofday(&t6, NULL); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 342 | /*Event notification*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 343 | if (p_ctx->notification_size && |
| 344 | p_ctx->event_notify_fn && |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 345 | /*!(p_ctx->segment_info.size % p_ctx->notification_size)*/ |
| 346 | (p_ctx->segment_info.size -p_ctx->last_send_size) >= p_ctx->notification_size&& |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 347 | p_ctx->segment_info.duration > 0) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 348 | memset(&record_status, 0, sizeof(record_status)); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 349 | //clock_gettime(CLOCK_MONOTONIC, &end_ts); |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 350 | p_ctx->last_send_size = p_ctx->segment_info.size; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 351 | record_status.state = p_ctx->state; |
| 352 | record_status.info.id = p_ctx->segment_info.id; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 353 | record_status.info.duration = p_ctx->segment_info.duration; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 354 | record_status.info.size = p_ctx->segment_info.size; |
| 355 | record_status.info.nb_packets = p_ctx->segment_info.size/188; |
| 356 | p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata); |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame^] | 357 | DVR_DEBUG(1, "%s notify record status, state:%d, id:%lld, duration:%ld ms, size:%zu loc[%s]", |
| 358 | __func__, record_status.state, |
| 359 | record_status.info.id, record_status.info.duration, |
| 360 | record_status.info.size, p_ctx->location); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 361 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 362 | gettimeofday(&t7, NULL); |
| 363 | #ifdef DEBUG_PERFORMANCE |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 364 | DVR_DEBUG(1, "record count, read:%dms, encrypt:%dms, write:%dms, index:%dms, store:%dms, notify:%dms total:%dms read len:%zd ", |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 365 | get_diff_time(t1, t2), get_diff_time(t2, t3), get_diff_time(t3, t4), get_diff_time(t4, t5), |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 366 | get_diff_time(t5, t6), get_diff_time(t6, t7), get_diff_time(t1, t5), len); |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 367 | #endif |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 368 | } |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 369 | end: |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 370 | free((void *)buf); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 371 | free((void *)buf_out); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 372 | DVR_DEBUG(1, "exit %s", __func__); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 373 | return NULL; |
| 374 | } |
| 375 | |
| 376 | int dvr_record_open(DVR_RecordHandle_t *p_handle, DVR_RecordOpenParams_t *params) |
| 377 | { |
| 378 | DVR_RecordContext_t *p_ctx; |
| 379 | Record_DeviceOpenParams_t dev_open_params; |
| 380 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 381 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 382 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 383 | DVR_RETURN_IF_FALSE(p_handle); |
| 384 | DVR_RETURN_IF_FALSE(params); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 385 | |
| 386 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 387 | if (record_ctx[i].state == DVR_RECORD_STATE_CLOSED) { |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 388 | break; |
| 389 | } |
| 390 | } |
Pengfei Liu | faf38e4 | 2020-05-22 00:28:02 +0800 | [diff] [blame] | 391 | DVR_RETURN_IF_FALSE(i < MAX_DVR_RECORD_SESSION_COUNT); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 392 | DVR_RETURN_IF_FALSE(record_ctx[i].state == DVR_RECORD_STATE_CLOSED); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 393 | p_ctx = &record_ctx[i]; |
hualing chen | fbf8e02 | 2020-06-15 13:43:11 +0800 | [diff] [blame] | 394 | DVR_DEBUG(1, "%s , current state:%d, dmx_id:%d, notification_size:%zu ", __func__, |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 395 | p_ctx->state, params->dmx_dev_id, params->notification_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 396 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 397 | /*Process event params*/ |
| 398 | p_ctx->notification_size = params->notification_size; |
| 399 | p_ctx->event_notify_fn = params->event_fn; |
| 400 | p_ctx->event_userdata = params->event_userdata; |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 401 | p_ctx->last_send_size = 0; |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 402 | //check is new driver |
| 403 | p_ctx->is_new_dmx = dvr_check_dmx_isNew(); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 404 | /*Process crypto params, todo*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 405 | memset((void *)&dev_open_params, 0, sizeof(dev_open_params)); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 406 | if (params->data_from_memory) { |
| 407 | /* data from memory, VOD case */ |
| 408 | p_ctx->is_vod = 1; |
| 409 | } else { |
| 410 | p_ctx->is_vod = 0; |
| 411 | /* data from dmx, normal dvr case */ |
hualing chen | 958fe4c | 2020-03-24 17:35:07 +0800 | [diff] [blame] | 412 | dev_open_params.dmx_dev_id = params->dmx_dev_id; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 413 | dev_open_params.buf_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 414 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 415 | ret = record_device_open(&p_ctx->dev_handle, &dev_open_params); |
| 416 | if (ret != DVR_SUCCESS) { |
| 417 | DVR_DEBUG(1, "%s, open record devices failed", __func__); |
| 418 | return DVR_FAILURE; |
| 419 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 420 | } |
| 421 | |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 422 | p_ctx->block_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 423 | p_ctx->enc_func = NULL; |
| 424 | p_ctx->enc_userdata = NULL; |
| 425 | p_ctx->is_secure_mode = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 426 | p_ctx->state = DVR_RECORD_STATE_OPENED; |
| 427 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 428 | *p_handle = p_ctx; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 429 | return DVR_SUCCESS; |
| 430 | } |
| 431 | |
| 432 | int dvr_record_close(DVR_RecordHandle_t handle) |
| 433 | { |
| 434 | DVR_RecordContext_t *p_ctx; |
| 435 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 436 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 437 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 438 | p_ctx = (DVR_RecordContext_t *)handle; |
| 439 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 440 | if (p_ctx == &record_ctx[i]) |
| 441 | break; |
| 442 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 443 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 444 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 445 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 446 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 447 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 448 | if (p_ctx->is_vod) { |
| 449 | ret = DVR_SUCCESS; |
| 450 | } else { |
| 451 | ret = record_device_close(p_ctx->dev_handle); |
| 452 | if (ret != DVR_SUCCESS) { |
| 453 | DVR_DEBUG(1, "%s, failed", __func__); |
| 454 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 455 | } |
| 456 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 457 | p_ctx->state = DVR_RECORD_STATE_CLOSED; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 458 | return ret; |
| 459 | } |
| 460 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 461 | #if 0 |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 462 | int dvr_record_register_encryption(DVR_RecordHandle_t handle, |
| 463 | DVR_CryptoFunction_t cb, |
| 464 | DVR_CryptoParams_t params, |
| 465 | void *userdata) |
| 466 | { |
| 467 | return DVR_SUCCESS; |
| 468 | } |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 469 | #endif |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 470 | |
| 471 | int dvr_record_start_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params) |
| 472 | { |
| 473 | DVR_RecordContext_t *p_ctx; |
| 474 | Segment_OpenParams_t open_params; |
| 475 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 476 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 477 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 478 | p_ctx = (DVR_RecordContext_t *)handle; |
| 479 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 480 | if (p_ctx == &record_ctx[i]) |
| 481 | break; |
| 482 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 483 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 484 | |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame^] | 485 | DVR_DEBUG(1, "%s , current state:%d pids:%d params->location:%s", __func__, p_ctx->state, params->segment.nb_pids, params->location); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 486 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 487 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 488 | DVR_RETURN_IF_FALSE(params); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 489 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 490 | DVR_RETURN_IF_FALSE(strlen((const char *)params->location) < DVR_MAX_LOCATION_SIZE); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 491 | memset(&open_params, 0, sizeof(open_params)); |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 492 | memcpy(open_params.location, params->location, sizeof(params->location)); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 493 | open_params.segment_id = params->segment.segment_id; |
| 494 | open_params.mode = SEGMENT_MODE_WRITE; |
| 495 | |
| 496 | ret = segment_open(&open_params, &p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 497 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 498 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 499 | /*process params*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 500 | { |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 501 | memcpy(p_ctx->location, params->location, sizeof(params->location)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 502 | //need all params?? |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 503 | memcpy(&p_ctx->segment_params, ¶ms->segment, sizeof(params->segment)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 504 | /*save current segment info*/ |
| 505 | memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info)); |
| 506 | p_ctx->segment_info.id = params->segment.segment_id; |
| 507 | p_ctx->segment_info.nb_pids = params->segment.nb_pids; |
| 508 | memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t)); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 509 | } |
| 510 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 511 | if (!p_ctx->is_vod) { |
| 512 | /* normal dvr case */ |
| 513 | for (i = 0; i < params->segment.nb_pids; i++) { |
| 514 | ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid); |
| 515 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 516 | } |
| 517 | ret = record_device_start(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 518 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 519 | } |
| 520 | |
Zhiqiang Han | 5ebad99 | 2020-04-28 18:19:59 +0800 | [diff] [blame] | 521 | ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info); |
| 522 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 523 | p_ctx->state = DVR_RECORD_STATE_STARTED; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 524 | if (!p_ctx->is_vod) |
| 525 | pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 526 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 527 | return DVR_SUCCESS; |
| 528 | } |
| 529 | |
| 530 | int dvr_record_next_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, DVR_RecordSegmentInfo_t *p_info) |
| 531 | { |
| 532 | DVR_RecordContext_t *p_ctx; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 533 | Segment_OpenParams_t open_params; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 534 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 535 | uint32_t i; |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 536 | loff_t pos; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 537 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 538 | p_ctx = (DVR_RecordContext_t *)handle; |
| 539 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 540 | if (p_ctx == &record_ctx[i]) |
| 541 | break; |
| 542 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 543 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 544 | |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame^] | 545 | DVR_DEBUG(1, "%s , current state:%d p_ctx->location:%s", __func__, p_ctx->state, p_ctx->location); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 546 | DVR_RETURN_IF_FALSE(p_ctx->state == DVR_RECORD_STATE_STARTED); |
| 547 | //DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 548 | DVR_RETURN_IF_FALSE(params); |
| 549 | DVR_RETURN_IF_FALSE(p_info); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 550 | DVR_RETURN_IF_FALSE(!p_ctx->is_vod); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 551 | |
| 552 | /*Stop the on going record segment*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 553 | //ret = record_device_stop(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 554 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 555 | p_ctx->state = DVR_RECORD_STATE_STOPPED; |
| 556 | pthread_join(p_ctx->thread, NULL); |
| 557 | |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 558 | //add index file store |
| 559 | pos = segment_tell_position(p_ctx->segment_handle); |
| 560 | segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos); |
| 561 | |
| 562 | p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 563 | /*Update segment info*/ |
| 564 | memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info)); |
| 565 | |
| 566 | ret = segment_store_info(p_ctx->segment_handle, p_info); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 567 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 568 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 569 | 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", |
| 570 | __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets, params->segment.nb_pids); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 571 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 572 | /*Close current segment*/ |
| 573 | ret = segment_close(p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 574 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 575 | /*Open the new record segment*/ |
| 576 | memset(&open_params, 0, sizeof(open_params)); |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 577 | memcpy(open_params.location, p_ctx->location, sizeof(p_ctx->location)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 578 | open_params.segment_id = params->segment.segment_id; |
| 579 | open_params.mode = SEGMENT_MODE_WRITE; |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 580 | DVR_DEBUG(1, "%s: p_ctx->location:%s params->location:%s", __func__, p_ctx->location,params->location); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 581 | |
| 582 | ret = segment_open(&open_params, &p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 583 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 584 | /*process params*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 585 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 586 | //need all params?? |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 587 | memcpy(&p_ctx->segment_params, ¶ms->segment, sizeof(params->segment)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 588 | /*save current segment info*/ |
| 589 | memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info)); |
| 590 | p_ctx->segment_info.id = params->segment.segment_id; |
| 591 | memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t)); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 592 | } |
| 593 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 594 | p_ctx->segment_info.nb_pids = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 595 | for (i = 0; i < params->segment.nb_pids; i++) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 596 | switch (params->segment.pid_action[i]) { |
| 597 | case DVR_RECORD_PID_CREATE: |
| 598 | DVR_DEBUG(1, "%s create pid:%d", __func__, params->segment.pids[i].pid); |
| 599 | ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid); |
| 600 | p_ctx->segment_info.nb_pids++; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 601 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 602 | break; |
| 603 | case DVR_RECORD_PID_KEEP: |
| 604 | DVR_DEBUG(1, "%s keep pid:%d", __func__, params->segment.pids[i].pid); |
| 605 | p_ctx->segment_info.nb_pids++; |
| 606 | break; |
| 607 | case DVR_RECORD_PID_CLOSE: |
| 608 | DVR_DEBUG(1, "%s close pid:%d", __func__, params->segment.pids[i].pid); |
| 609 | ret = record_device_remove_pid(p_ctx->dev_handle, params->segment.pids[i].pid); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 610 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 611 | break; |
| 612 | default: |
| 613 | DVR_DEBUG(1, "%s wrong action pid:%d", __func__, params->segment.pids[i].pid); |
| 614 | return DVR_FAILURE; |
| 615 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 616 | } |
| 617 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 618 | //ret = record_device_start(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 619 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 620 | /*Update segment info*/ |
| 621 | ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 622 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 623 | p_ctx->state = DVR_RECORD_STATE_STARTED; |
Zhiqiang Han | 0d60f2b | 2020-05-26 14:39:54 +0800 | [diff] [blame] | 624 | pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 625 | return DVR_SUCCESS; |
| 626 | } |
| 627 | |
| 628 | int dvr_record_stop_segment(DVR_RecordHandle_t handle, DVR_RecordSegmentInfo_t *p_info) |
| 629 | { |
| 630 | DVR_RecordContext_t *p_ctx; |
| 631 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 632 | uint32_t i; |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 633 | loff_t pos; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 634 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 635 | p_ctx = (DVR_RecordContext_t *)handle; |
| 636 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 637 | if (p_ctx == &record_ctx[i]) |
| 638 | break; |
| 639 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 640 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 641 | |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame^] | 642 | DVR_DEBUG(1, "%s , current state:%d p_ctx->location:%s", __func__, p_ctx->state, p_ctx->location); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 643 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STOPPED); |
| 644 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 645 | DVR_RETURN_IF_FALSE(p_info);/*should support NULL*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 646 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 647 | p_ctx->state = DVR_RECORD_STATE_STOPPED; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 648 | if (p_ctx->is_vod) { |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 649 | p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 650 | p_ctx->segment_info.duration = 10*1000; //debug, should delete it |
| 651 | } else { |
| 652 | ret = record_device_stop(p_ctx->dev_handle); |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 653 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 654 | if (ret != DVR_SUCCESS) |
| 655 | goto end; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 656 | //p_ctx->state = DVR_RECORD_STATE_STOPPED; |
| 657 | pthread_join(p_ctx->thread, NULL); |
| 658 | } |
| 659 | |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 660 | //add index file store |
| 661 | pos = segment_tell_position(p_ctx->segment_handle); |
| 662 | segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos); |
hualing chen | e41f437 | 2020-06-06 16:29:17 +0800 | [diff] [blame] | 663 | p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 664 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 665 | /*Update segment info*/ |
| 666 | memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info)); |
| 667 | |
| 668 | ret = segment_store_info(p_ctx->segment_handle, p_info); |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 669 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 670 | if (ret != DVR_SUCCESS) |
| 671 | goto end; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 672 | |
| 673 | DVR_DEBUG(1, "%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d", |
| 674 | __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets); |
| 675 | |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 676 | end: |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 677 | ret = segment_close(p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 678 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 679 | return DVR_SUCCESS; |
| 680 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 681 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 682 | int dvr_record_resume_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, uint64_t *p_resume_size) |
| 683 | { |
| 684 | DVR_RecordContext_t *p_ctx; |
| 685 | uint32_t i; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 686 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 687 | |
| 688 | p_ctx = (DVR_RecordContext_t *)handle; |
| 689 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 690 | if (p_ctx == &record_ctx[i]) |
| 691 | break; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 692 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 693 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 694 | DVR_RETURN_IF_FALSE(params); |
| 695 | DVR_RETURN_IF_FALSE(p_resume_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 696 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 697 | DVR_DEBUG(1, "%s , current state:%d, resume size:%lld", __func__, p_ctx->state, *p_resume_size); |
| 698 | ret = dvr_record_start_segment(handle, params); |
| 699 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 700 | |
| 701 | p_ctx->segment_info.size = *p_resume_size; |
| 702 | |
| 703 | return DVR_SUCCESS; |
| 704 | } |
| 705 | |
| 706 | int dvr_record_get_status(DVR_RecordHandle_t handle, DVR_RecordStatus_t *p_status) |
| 707 | { |
| 708 | DVR_RecordContext_t *p_ctx; |
| 709 | int i; |
| 710 | |
| 711 | p_ctx = (DVR_RecordContext_t *)handle; |
| 712 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 713 | if (p_ctx == &record_ctx[i]) |
| 714 | break; |
| 715 | } |
| 716 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 717 | DVR_RETURN_IF_FALSE(p_status); |
| 718 | |
| 719 | //lock |
| 720 | p_status->state = p_ctx->state; |
| 721 | p_status->info.id = p_ctx->segment_info.id; |
| 722 | p_status->info.duration = p_ctx->segment_info.duration; |
| 723 | p_status->info.size = p_ctx->segment_info.size; |
| 724 | p_status->info.nb_packets = p_ctx->segment_info.size/188; |
| 725 | |
| 726 | return DVR_SUCCESS; |
| 727 | } |
| 728 | |
| 729 | int dvr_record_write(DVR_RecordHandle_t handle, void *buffer, uint32_t len) |
| 730 | { |
| 731 | DVR_RecordContext_t *p_ctx; |
| 732 | uint32_t i; |
| 733 | off_t pos = 0; |
| 734 | int ret; |
| 735 | int has_pcr; |
| 736 | |
| 737 | p_ctx = (DVR_RecordContext_t *)handle; |
| 738 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 739 | if (p_ctx == &record_ctx[i]) |
| 740 | break; |
| 741 | } |
| 742 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 743 | DVR_RETURN_IF_FALSE(buffer); |
| 744 | DVR_RETURN_IF_FALSE(len); |
| 745 | |
| 746 | pos = segment_tell_position(p_ctx->segment_handle); |
| 747 | has_pcr = record_do_pcr_index(p_ctx, buffer, len); |
| 748 | if (has_pcr == 0) { |
| 749 | /* Pull VOD record shoud use PCR time index */ |
| 750 | DVR_DEBUG(1, "%s has no pcr, can NOT do time index", __func__); |
| 751 | } |
| 752 | ret = segment_write(p_ctx->segment_handle, buffer, len); |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 753 | if (ret != len) { |
| 754 | DVR_DEBUG(1, "%s write error ret:%d len:%d", __func__, ret, len); |
| 755 | } |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 756 | p_ctx->segment_info.size += len; |
| 757 | p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188; |
| 758 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 759 | return DVR_SUCCESS; |
| 760 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 761 | |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 762 | int dvr_record_set_encrypt_callback(DVR_RecordHandle_t handle, DVR_CryptoFunction_t func, void *userdata) |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 763 | { |
| 764 | DVR_RecordContext_t *p_ctx; |
| 765 | uint32_t i; |
| 766 | |
| 767 | p_ctx = (DVR_RecordContext_t *)handle; |
| 768 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 769 | if (p_ctx == &record_ctx[i]) |
| 770 | break; |
| 771 | } |
| 772 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 773 | DVR_RETURN_IF_FALSE(func); |
| 774 | |
| 775 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
| 776 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 777 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 778 | |
| 779 | p_ctx->enc_func = func; |
| 780 | p_ctx->enc_userdata = userdata; |
| 781 | return DVR_SUCCESS; |
| 782 | } |
| 783 | |
| 784 | int dvr_record_set_secure_buffer(DVR_RecordHandle_t handle, uint8_t *p_secure_buf, uint32_t len) |
| 785 | { |
| 786 | DVR_RecordContext_t *p_ctx; |
| 787 | uint32_t i; |
| 788 | int ret; |
| 789 | |
| 790 | p_ctx = (DVR_RecordContext_t *)handle; |
| 791 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 792 | if (p_ctx == &record_ctx[i]) |
| 793 | break; |
| 794 | } |
| 795 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 796 | DVR_RETURN_IF_FALSE(p_secure_buf); |
| 797 | DVR_RETURN_IF_FALSE(len); |
| 798 | |
| 799 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
| 800 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 801 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 802 | |
| 803 | ret = record_device_set_secure_buffer(p_ctx->dev_handle, p_secure_buf, len); |
| 804 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 805 | |
| 806 | p_ctx->is_secure_mode = 1; |
| 807 | return ret; |
| 808 | } |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame^] | 809 | |
| 810 | int dvr_record_is_secure_mode(DVR_RecordHandle_t handle) |
| 811 | { |
| 812 | DVR_RecordContext_t *p_ctx; |
| 813 | uint32_t i; |
| 814 | int ret; |
| 815 | |
| 816 | p_ctx = (DVR_RecordContext_t *)handle; |
| 817 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 818 | if (p_ctx == &record_ctx[i]) |
| 819 | break; |
| 820 | } |
| 821 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 822 | |
| 823 | if (p_ctx->is_secure_mode == 1) |
| 824 | ret = 1; |
| 825 | else |
| 826 | ret = 0; |
| 827 | return ret; |
| 828 | } |