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 | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 11 | |
| 12 | #define MAX_DVR_RECORD_SESSION_COUNT 2 |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 13 | //#define RECORD_BLOCK_SIZE (64 * 1024) |
| 14 | #define RECORD_BLOCK_SIZE (256 * 1024) |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 15 | |
| 16 | /**\brief DVR index file type*/ |
| 17 | typedef enum { |
| 18 | DVR_INDEX_TYPE_PCR, /**< DVR index file use pcr*/ |
| 19 | DVR_INDEX_TYPE_LOCAL_CLOCK, /**< DVR index file use local clock*/ |
| 20 | DVR_INDEX_TYPE_INVALID /**< DVR index file type invalid type*/ |
| 21 | } DVR_IndexType_t; |
| 22 | |
| 23 | /**\brief DVR VOD context*/ |
| 24 | typedef struct { |
| 25 | pthread_mutex_t mutex; /**< VOD mutex lock*/ |
| 26 | pthread_cond_t cond; /**< VOD condition*/ |
| 27 | void *buffer; /**< VOD buffer*/ |
| 28 | uint32_t buf_len; /**< VOD buffer len*/ |
| 29 | } DVR_VodContext_t; |
| 30 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 31 | /**\brief DVR record secure mode buffer*/ |
| 32 | typedef struct { |
| 33 | uint32_t addr; /**< Secure mode record buffer address*/ |
| 34 | uint32_t len; /**< Secure mode record buffer length*/ |
| 35 | } DVR_SecureBuffer_t; |
| 36 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 37 | /**\brief DVR record context*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 38 | typedef struct { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 39 | pthread_t thread; /**< DVR thread handle*/ |
| 40 | Record_DeviceHandle_t dev_handle; /**< DVR device handle*/ |
| 41 | Segment_Handle_t segment_handle; /**< DVR segment handle*/ |
| 42 | DVR_RecordState_t state; /**< DVR record state*/ |
| 43 | char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/ |
| 44 | DVR_RecordSegmentStartParams_t segment_params; /**< DVR record start parameters*/ |
| 45 | DVR_RecordSegmentInfo_t segment_info; /**< DVR record current segment info*/ |
| 46 | size_t notification_size; /**< DVR record nogification size*/ |
| 47 | DVR_RecordEventFunction_t event_notify_fn; /**< DVR record event notify function*/ |
| 48 | void *event_userdata; /**< DVR record event userdata*/ |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 49 | //DVR_VodContext_t vod; /**< DVR record vod context*/ |
| 50 | int is_vod; /**< Indicate current mode is VOD record mode*/ |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 51 | DVR_CryptoFunction_t enc_func; /**< Encrypt function*/ |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 52 | void *enc_userdata; /**< Encrypt userdata*/ |
| 53 | int is_secure_mode; /**< Record session run in secure pipeline */ |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 54 | size_t last_send_size; /**< Last send notify segment size */ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 55 | } DVR_RecordContext_t; |
| 56 | |
| 57 | static DVR_RecordContext_t record_ctx[MAX_DVR_RECORD_SESSION_COUNT] = { |
| 58 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 59 | .state = DVR_RECORD_STATE_CLOSED |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 60 | }, |
| 61 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 62 | .state = DVR_RECORD_STATE_CLOSED |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | |
Zhiqiang Han | 51646a0 | 2020-04-05 18:43:22 +0800 | [diff] [blame^] | 66 | static int record_is_valid_pid(DVR_RecordContext_t *p_ctx, int pid) |
| 67 | { |
| 68 | int i; |
| 69 | |
| 70 | for (i = 0; i < p_ctx->segment_info.nb_pids; i++) { |
| 71 | if (pid == p_ctx->segment_info.pids[i].pid) |
| 72 | return 1; |
| 73 | } |
| 74 | return 0; |
| 75 | } |
| 76 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 77 | static int record_save_pcr(DVR_RecordContext_t *p_ctx, uint8_t *buf, loff_t pos) |
| 78 | { |
| 79 | uint8_t *p = buf; |
| 80 | int len; |
| 81 | uint8_t afc; |
| 82 | uint64_t pcr = 0; |
| 83 | int has_pcr = 0; |
| 84 | int pid; |
| 85 | int adp_field_len; |
| 86 | |
| 87 | pid = ((p[1] & 0x1f) << 8) | p[2]; |
Zhiqiang Han | 51646a0 | 2020-04-05 18:43:22 +0800 | [diff] [blame^] | 88 | if (pid == 0x1fff || !record_is_valid_pid(p_ctx, pid)) |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 89 | return has_pcr; |
| 90 | |
| 91 | //scramble = p[3] >> 6; |
| 92 | //cc = p[3] & 0x0f; |
| 93 | afc = (p[3] >> 4) & 0x03; |
| 94 | |
| 95 | p += 4; |
| 96 | len = 184; |
| 97 | |
| 98 | if (afc & 2) { |
| 99 | adp_field_len = p[0]; |
| 100 | /* Skip adaptation len */ |
| 101 | p++; |
| 102 | len--; |
| 103 | /* Parse pcr field, see 13818 spec table I-2-6,adaptation_field */ |
| 104 | if (p[0] & 0x10 && len >= 6) { |
| 105 | /* get pcr value,pcr is 33bit value */ |
| 106 | pcr = (((uint64_t)(p[1])) << 25) |
| 107 | | (((uint64_t)p[2]) << 17) |
| 108 | | (((uint64_t)(p[3])) << 9) |
| 109 | | (((uint64_t)p[4]) << 1) |
| 110 | | ((((uint64_t)p[5]) & 0x80) >> 7); |
| 111 | has_pcr = 1; |
| 112 | } |
| 113 | |
| 114 | p += adp_field_len; |
| 115 | len -= adp_field_len; |
| 116 | |
| 117 | if (len < 0) { |
| 118 | DVR_DEBUG(1, "parser pcr: illegal adaptation field length"); |
| 119 | return 0; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if (has_pcr) { |
| 124 | segment_update_pts(p_ctx->segment_handle, pcr/90, pos); |
| 125 | } |
| 126 | return has_pcr; |
| 127 | } |
| 128 | |
| 129 | static int record_do_pcr_index(DVR_RecordContext_t *p_ctx, uint8_t *buf, int len) |
| 130 | { |
| 131 | uint8_t *p = buf; |
| 132 | int left = len; |
| 133 | loff_t pos; |
| 134 | int has_pcr = 0; |
| 135 | |
| 136 | pos = segment_tell_position(p_ctx->segment_handle); |
| 137 | while (left >= 188) { |
| 138 | if (*p == 0x47) { |
| 139 | has_pcr |= record_save_pcr(p_ctx, p, pos); |
| 140 | p += 188; |
| 141 | left -= 188; |
| 142 | pos += 188; |
| 143 | } else { |
| 144 | p++; |
| 145 | left --; |
| 146 | pos++; |
| 147 | } |
| 148 | } |
| 149 | return has_pcr; |
| 150 | } |
| 151 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 152 | void *record_thread(void *arg) |
| 153 | { |
| 154 | DVR_RecordContext_t *p_ctx = (DVR_RecordContext_t *)arg; |
| 155 | ssize_t len; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 156 | uint8_t *buf, *buf_out; |
| 157 | int block_size = RECORD_BLOCK_SIZE;//256*1024; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 158 | loff_t pos = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 159 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 160 | struct timespec start_ts, end_ts; |
| 161 | DVR_RecordStatus_t record_status; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 162 | int has_pcr; |
| 163 | int index_type = DVR_INDEX_TYPE_INVALID; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 164 | time_t pre_time = 0; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 165 | uint32_t out_len; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 166 | #define DVR_STORE_INFO_TIME (1000) |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 167 | DVR_SecureBuffer_t secure_buf; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 168 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 169 | buf = (uint8_t *)malloc(block_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 170 | if (!buf) { |
| 171 | DVR_DEBUG(1, "%s, malloc failed", __func__); |
| 172 | return NULL; |
| 173 | } |
| 174 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 175 | buf_out = (uint8_t *)malloc(block_size + 188); |
| 176 | if (!buf_out) { |
| 177 | DVR_DEBUG(1, "%s, malloc failed", __func__); |
| 178 | return NULL; |
| 179 | } |
| 180 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 181 | memset(&record_status, 0, sizeof(record_status)); |
| 182 | record_status.state = DVR_RECORD_STATE_STARTED; |
| 183 | p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata); |
| 184 | DVR_DEBUG(1, "%s notify record status, state:%d", |
| 185 | __func__, record_status.state); |
| 186 | DVR_DEBUG(1, "%s, secure_mode:%d", __func__, p_ctx->is_secure_mode); |
| 187 | |
| 188 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 189 | clock_gettime(CLOCK_MONOTONIC, &start_ts); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 190 | while (p_ctx->state == DVR_RECORD_STATE_STARTED) { |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 191 | /* data from dmx, normal dvr case */ |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 192 | if (p_ctx->is_secure_mode) { |
| 193 | memset(&secure_buf, 0, sizeof(secure_buf)); |
| 194 | len = record_device_read(p_ctx->dev_handle, &secure_buf, sizeof(secure_buf), 1000); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 195 | if (len != DVR_FAILURE) { |
| 196 | DVR_DEBUG(1, "%s, secure_buf:%#x, size:%#x", __func__, secure_buf.addr, secure_buf.len); |
| 197 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 198 | } else { |
| 199 | len = record_device_read(p_ctx->dev_handle, buf, block_size, 1000); |
| 200 | } |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 201 | if (len == DVR_FAILURE) { |
| 202 | usleep(10*1000); |
| 203 | continue; |
| 204 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 205 | |
| 206 | /* Got data from device, record it */ |
| 207 | if (p_ctx->enc_func) { |
| 208 | /* Encrypt record data */ |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 209 | #if 0 |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 210 | uint8_t *input_buf; |
| 211 | uint32_t input_len; |
| 212 | /* Out buffer length may not equal in buffer length */ |
| 213 | if (p_ctx->is_secure_mode) { |
| 214 | input_buf = (uint8_t *)secure_buf.addr; |
| 215 | input_len = secure_buf.len; |
| 216 | } else { |
| 217 | input_buf = buf; |
| 218 | input_len = len; |
| 219 | } |
| 220 | out_len = block_size + 188; |
| 221 | p_ctx->enc_func(input_buf, input_len, buf_out, &out_len, p_ctx->enc_userdata); |
| 222 | ret = segment_write(p_ctx->segment_handle, buf_out, out_len); |
| 223 | len = out_len; |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 224 | #else |
| 225 | DVR_CryptoParams_t crypto_params; |
| 226 | |
| 227 | memset(&crypto_params, 0, sizeof(crypto_params)); |
| 228 | crypto_params.type = DVR_CRYPTO_TYPE_ENCRYPT; |
| 229 | memcpy(crypto_params.location, p_ctx->location, strlen(p_ctx->location)); |
| 230 | crypto_params.segment_id = p_ctx->segment_info.id; |
| 231 | crypto_params.offset = p_ctx->segment_info.size; |
| 232 | |
| 233 | if (p_ctx->is_secure_mode) { |
| 234 | crypto_params.input_buffer.type = DVR_BUFFER_TYPE_SECURE; |
| 235 | crypto_params.input_buffer.addr = secure_buf.addr; |
| 236 | crypto_params.input_buffer.size = secure_buf.len; |
| 237 | } else { |
| 238 | crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL; |
| 239 | crypto_params.input_buffer.addr = (size_t)buf; |
| 240 | crypto_params.input_buffer.size = len; |
| 241 | } |
| 242 | |
| 243 | crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL; |
| 244 | crypto_params.output_buffer.addr = (size_t)buf_out; |
| 245 | crypto_params.output_buffer.size = block_size + 188; |
| 246 | |
| 247 | p_ctx->enc_func(&crypto_params, p_ctx->enc_userdata); |
| 248 | /* Out buffer length may not equal in buffer length */ |
| 249 | ret = segment_write(p_ctx->segment_handle, buf_out, crypto_params.output_size); |
| 250 | len = crypto_params.output_size; |
| 251 | #endif |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 252 | } else { |
| 253 | ret = segment_write(p_ctx->segment_handle, buf, len); |
| 254 | } |
| 255 | |
| 256 | /* Do time index */ |
| 257 | uint8_t *index_buf = p_ctx->enc_func ? buf_out : buf; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 258 | pos = segment_tell_position(p_ctx->segment_handle); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 259 | has_pcr = record_do_pcr_index(p_ctx, index_buf, len); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 260 | if (has_pcr == 0 && index_type == DVR_INDEX_TYPE_INVALID) { |
| 261 | clock_gettime(CLOCK_MONOTONIC, &end_ts); |
| 262 | if ((end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) - |
| 263 | (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) > 40) { |
| 264 | /* PCR interval threshlod > 40 ms*/ |
| 265 | DVR_DEBUG(1, "%s use local clock time index", __func__); |
| 266 | index_type = DVR_INDEX_TYPE_LOCAL_CLOCK; |
| 267 | } |
| 268 | } else if (has_pcr && index_type == DVR_INDEX_TYPE_INVALID){ |
| 269 | DVR_DEBUG(1, "%s use pcr time index", __func__); |
| 270 | index_type = DVR_INDEX_TYPE_PCR; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 271 | } |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 272 | |
| 273 | /* Update segment info */ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 274 | p_ctx->segment_info.size += len; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 275 | /*Duration need use pcr to calculate, todo...*/ |
| 276 | if (index_type == DVR_INDEX_TYPE_PCR) { |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 277 | 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] | 278 | if (pre_time == 0) |
| 279 | pre_time = p_ctx->segment_info.duration; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 280 | } else if (index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) { |
| 281 | clock_gettime(CLOCK_MONOTONIC, &end_ts); |
| 282 | p_ctx->segment_info.duration = (end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) - |
| 283 | (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 284 | if (pre_time == 0) |
| 285 | pre_time = p_ctx->segment_info.duration; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 286 | segment_update_pts(p_ctx->segment_handle, p_ctx->segment_info.duration, pos); |
| 287 | } else { |
| 288 | DVR_DEBUG(1, "%s can NOT do time index", __func__); |
| 289 | } |
| 290 | p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 291 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 292 | if (p_ctx->segment_info.duration - pre_time > DVR_STORE_INFO_TIME) { |
| 293 | pre_time = p_ctx->segment_info.duration + DVR_STORE_INFO_TIME; |
| 294 | segment_store_info(p_ctx->segment_handle, &(p_ctx->segment_info)); |
| 295 | } |
| 296 | /*Event notification*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 297 | if (p_ctx->notification_size && |
| 298 | p_ctx->event_notify_fn && |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 299 | /*!(p_ctx->segment_info.size % p_ctx->notification_size)*/ |
| 300 | (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] | 301 | p_ctx->segment_info.duration > 0) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 302 | memset(&record_status, 0, sizeof(record_status)); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 303 | //clock_gettime(CLOCK_MONOTONIC, &end_ts); |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 304 | p_ctx->last_send_size = p_ctx->segment_info.size; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 305 | record_status.state = p_ctx->state; |
| 306 | record_status.info.id = p_ctx->segment_info.id; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 307 | record_status.info.duration = p_ctx->segment_info.duration; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 308 | record_status.info.size = p_ctx->segment_info.size; |
| 309 | record_status.info.nb_packets = p_ctx->segment_info.size/188; |
| 310 | p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata); |
| 311 | DVR_DEBUG(1, "%s notify record status, state:%d, id:%lld, duration:%ld ms, size:%zu", |
| 312 | __func__, record_status.state, record_status.info.id, record_status.info.duration, record_status.info.size); |
| 313 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 314 | } |
| 315 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 316 | free((void *)buf); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 317 | free((void *)buf_out); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 318 | DVR_DEBUG(1, "exit %s", __func__); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 319 | return NULL; |
| 320 | } |
| 321 | |
| 322 | int dvr_record_open(DVR_RecordHandle_t *p_handle, DVR_RecordOpenParams_t *params) |
| 323 | { |
| 324 | DVR_RecordContext_t *p_ctx; |
| 325 | Record_DeviceOpenParams_t dev_open_params; |
| 326 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 327 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 328 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 329 | DVR_RETURN_IF_FALSE(p_handle); |
| 330 | DVR_RETURN_IF_FALSE(params); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 331 | |
| 332 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 333 | if (record_ctx[i].state == DVR_RECORD_STATE_CLOSED) { |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 334 | break; |
| 335 | } |
| 336 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 337 | DVR_RETURN_IF_FALSE(record_ctx[i].state == DVR_RECORD_STATE_CLOSED); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 338 | p_ctx = &record_ctx[i]; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 339 | DVR_DEBUG(1, "%s , current state:%d, dmx_id:%d, notification_size:%zu", __func__, |
| 340 | p_ctx->state, params->dmx_dev_id, params->notification_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 341 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 342 | /*Process event params*/ |
| 343 | p_ctx->notification_size = params->notification_size; |
| 344 | p_ctx->event_notify_fn = params->event_fn; |
| 345 | p_ctx->event_userdata = params->event_userdata; |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 346 | p_ctx->last_send_size = 0; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 347 | /*Process crypto params, todo*/ |
| 348 | #if 0 |
| 349 | DVR_CryptoPeriod_t crypto_period; /**< DVR crypto period information*/ |
| 350 | DVR_CryptoFunction_t crypto_fn; /**< DVR crypto callback function*/ |
| 351 | void *crypto_data; /**< DVR crypto userdata*/ |
| 352 | #endif |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 353 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 354 | memset((void *)&dev_open_params, 0, sizeof(dev_open_params)); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 355 | if (params->data_from_memory) { |
| 356 | /* data from memory, VOD case */ |
| 357 | p_ctx->is_vod = 1; |
| 358 | } else { |
| 359 | p_ctx->is_vod = 0; |
| 360 | /* data from dmx, normal dvr case */ |
hualing chen | 958fe4c | 2020-03-24 17:35:07 +0800 | [diff] [blame] | 361 | dev_open_params.dmx_dev_id = params->dmx_dev_id; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 362 | 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] | 363 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 364 | ret = record_device_open(&p_ctx->dev_handle, &dev_open_params); |
| 365 | if (ret != DVR_SUCCESS) { |
| 366 | DVR_DEBUG(1, "%s, open record devices failed", __func__); |
| 367 | return DVR_FAILURE; |
| 368 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 369 | } |
| 370 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 371 | p_ctx->enc_func = NULL; |
| 372 | p_ctx->enc_userdata = NULL; |
| 373 | p_ctx->is_secure_mode = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 374 | p_ctx->state = DVR_RECORD_STATE_OPENED; |
| 375 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 376 | *p_handle = p_ctx; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 377 | return DVR_SUCCESS; |
| 378 | } |
| 379 | |
| 380 | int dvr_record_close(DVR_RecordHandle_t handle) |
| 381 | { |
| 382 | DVR_RecordContext_t *p_ctx; |
| 383 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 384 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 385 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 386 | p_ctx = (DVR_RecordContext_t *)handle; |
| 387 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 388 | if (p_ctx == &record_ctx[i]) |
| 389 | break; |
| 390 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 391 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 392 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 393 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 394 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 395 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 396 | if (p_ctx->is_vod) { |
| 397 | ret = DVR_SUCCESS; |
| 398 | } else { |
| 399 | ret = record_device_close(p_ctx->dev_handle); |
| 400 | if (ret != DVR_SUCCESS) { |
| 401 | DVR_DEBUG(1, "%s, failed", __func__); |
| 402 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 403 | } |
| 404 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 405 | p_ctx->state = DVR_RECORD_STATE_CLOSED; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 406 | return ret; |
| 407 | } |
| 408 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 409 | #if 0 |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 410 | int dvr_record_register_encryption(DVR_RecordHandle_t handle, |
| 411 | DVR_CryptoFunction_t cb, |
| 412 | DVR_CryptoParams_t params, |
| 413 | void *userdata) |
| 414 | { |
| 415 | return DVR_SUCCESS; |
| 416 | } |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 417 | #endif |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 418 | |
| 419 | int dvr_record_start_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params) |
| 420 | { |
| 421 | DVR_RecordContext_t *p_ctx; |
| 422 | Segment_OpenParams_t open_params; |
| 423 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 424 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 425 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 426 | p_ctx = (DVR_RecordContext_t *)handle; |
| 427 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 428 | if (p_ctx == &record_ctx[i]) |
| 429 | break; |
| 430 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 431 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 432 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 433 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 434 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 435 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 436 | DVR_RETURN_IF_FALSE(params); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 437 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 438 | 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] | 439 | memset(&open_params, 0, sizeof(open_params)); |
| 440 | memcpy(open_params.location, params->location, strlen(params->location)); |
| 441 | open_params.segment_id = params->segment.segment_id; |
| 442 | open_params.mode = SEGMENT_MODE_WRITE; |
| 443 | |
| 444 | ret = segment_open(&open_params, &p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 445 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 446 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 447 | /*process params*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 448 | { |
| 449 | memcpy(p_ctx->location, params->location, strlen(params->location)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 450 | //need all params?? |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 451 | memcpy(&p_ctx->segment_params, ¶ms->segment, sizeof(params->segment)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 452 | /*save current segment info*/ |
| 453 | memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info)); |
| 454 | p_ctx->segment_info.id = params->segment.segment_id; |
| 455 | p_ctx->segment_info.nb_pids = params->segment.nb_pids; |
| 456 | 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] | 457 | } |
| 458 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 459 | if (!p_ctx->is_vod) { |
| 460 | /* normal dvr case */ |
| 461 | for (i = 0; i < params->segment.nb_pids; i++) { |
| 462 | ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid); |
| 463 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 464 | } |
| 465 | ret = record_device_start(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 466 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 467 | } |
| 468 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 469 | p_ctx->state = DVR_RECORD_STATE_STARTED; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 470 | if (!p_ctx->is_vod) |
| 471 | pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 472 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 473 | return DVR_SUCCESS; |
| 474 | } |
| 475 | |
| 476 | int dvr_record_next_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, DVR_RecordSegmentInfo_t *p_info) |
| 477 | { |
| 478 | DVR_RecordContext_t *p_ctx; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 479 | Segment_OpenParams_t open_params; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 480 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 481 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 482 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 483 | p_ctx = (DVR_RecordContext_t *)handle; |
| 484 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 485 | if (p_ctx == &record_ctx[i]) |
| 486 | break; |
| 487 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 488 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 489 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 490 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 491 | DVR_RETURN_IF_FALSE(p_ctx->state == DVR_RECORD_STATE_STARTED); |
| 492 | //DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 493 | DVR_RETURN_IF_FALSE(params); |
| 494 | DVR_RETURN_IF_FALSE(p_info); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 495 | DVR_RETURN_IF_FALSE(!p_ctx->is_vod); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 496 | |
| 497 | /*Stop the on going record segment*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 498 | //ret = record_device_stop(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 499 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 500 | |
| 501 | p_ctx->state = DVR_RECORD_STATE_STOPPED; |
| 502 | pthread_join(p_ctx->thread, NULL); |
| 503 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 504 | /*Update segment info*/ |
| 505 | memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info)); |
| 506 | |
| 507 | ret = segment_store_info(p_ctx->segment_handle, p_info); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 508 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 509 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 510 | 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", |
| 511 | __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] | 512 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 513 | /*Close current segment*/ |
| 514 | ret = segment_close(p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 515 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 516 | /*Open the new record segment*/ |
| 517 | memset(&open_params, 0, sizeof(open_params)); |
| 518 | memcpy(open_params.location, p_ctx->location, strlen(p_ctx->location)); |
| 519 | open_params.segment_id = params->segment.segment_id; |
| 520 | open_params.mode = SEGMENT_MODE_WRITE; |
| 521 | |
| 522 | ret = segment_open(&open_params, &p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 523 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 524 | /*process params*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 525 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 526 | //need all params?? |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 527 | memcpy(&p_ctx->segment_params, ¶ms->segment, sizeof(params->segment)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 528 | /*save current segment info*/ |
| 529 | memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info)); |
| 530 | p_ctx->segment_info.id = params->segment.segment_id; |
| 531 | 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] | 532 | } |
| 533 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 534 | p_ctx->segment_info.nb_pids = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 535 | for (i = 0; i < params->segment.nb_pids; i++) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 536 | switch (params->segment.pid_action[i]) { |
| 537 | case DVR_RECORD_PID_CREATE: |
| 538 | DVR_DEBUG(1, "%s create pid:%d", __func__, params->segment.pids[i].pid); |
| 539 | ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid); |
| 540 | p_ctx->segment_info.nb_pids++; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 541 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 542 | break; |
| 543 | case DVR_RECORD_PID_KEEP: |
| 544 | DVR_DEBUG(1, "%s keep pid:%d", __func__, params->segment.pids[i].pid); |
| 545 | p_ctx->segment_info.nb_pids++; |
| 546 | break; |
| 547 | case DVR_RECORD_PID_CLOSE: |
| 548 | DVR_DEBUG(1, "%s close pid:%d", __func__, params->segment.pids[i].pid); |
| 549 | 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] | 550 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 551 | break; |
| 552 | default: |
| 553 | DVR_DEBUG(1, "%s wrong action pid:%d", __func__, params->segment.pids[i].pid); |
| 554 | return DVR_FAILURE; |
| 555 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 556 | } |
| 557 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 558 | //ret = record_device_start(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 559 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 560 | |
| 561 | pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx); |
| 562 | p_ctx->state = DVR_RECORD_STATE_STARTED; |
| 563 | return DVR_SUCCESS; |
| 564 | } |
| 565 | |
| 566 | int dvr_record_stop_segment(DVR_RecordHandle_t handle, DVR_RecordSegmentInfo_t *p_info) |
| 567 | { |
| 568 | DVR_RecordContext_t *p_ctx; |
| 569 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 570 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 571 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 572 | p_ctx = (DVR_RecordContext_t *)handle; |
| 573 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 574 | if (p_ctx == &record_ctx[i]) |
| 575 | break; |
| 576 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 577 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 578 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 579 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 580 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STOPPED); |
| 581 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 582 | DVR_RETURN_IF_FALSE(p_info);/*should support NULL*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 583 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 584 | p_ctx->state = DVR_RECORD_STATE_STOPPED; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 585 | if (p_ctx->is_vod) { |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 586 | 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] | 587 | p_ctx->segment_info.duration = 10*1000; //debug, should delete it |
| 588 | } else { |
| 589 | ret = record_device_stop(p_ctx->dev_handle); |
| 590 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 591 | //p_ctx->state = DVR_RECORD_STATE_STOPPED; |
| 592 | pthread_join(p_ctx->thread, NULL); |
| 593 | } |
| 594 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 595 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 596 | /*Update segment info*/ |
| 597 | memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info)); |
| 598 | |
| 599 | ret = segment_store_info(p_ctx->segment_handle, p_info); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 600 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 601 | |
| 602 | DVR_DEBUG(1, "%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d", |
| 603 | __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets); |
| 604 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 605 | ret = segment_close(p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 606 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 607 | return DVR_SUCCESS; |
| 608 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 609 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 610 | int dvr_record_resume_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, uint64_t *p_resume_size) |
| 611 | { |
| 612 | DVR_RecordContext_t *p_ctx; |
| 613 | uint32_t i; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 614 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 615 | |
| 616 | p_ctx = (DVR_RecordContext_t *)handle; |
| 617 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 618 | if (p_ctx == &record_ctx[i]) |
| 619 | break; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 620 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 621 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 622 | DVR_RETURN_IF_FALSE(params); |
| 623 | DVR_RETURN_IF_FALSE(p_resume_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 624 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 625 | DVR_DEBUG(1, "%s , current state:%d, resume size:%lld", __func__, p_ctx->state, *p_resume_size); |
| 626 | ret = dvr_record_start_segment(handle, params); |
| 627 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 628 | |
| 629 | p_ctx->segment_info.size = *p_resume_size; |
| 630 | |
| 631 | return DVR_SUCCESS; |
| 632 | } |
| 633 | |
| 634 | int dvr_record_get_status(DVR_RecordHandle_t handle, DVR_RecordStatus_t *p_status) |
| 635 | { |
| 636 | DVR_RecordContext_t *p_ctx; |
| 637 | int i; |
| 638 | |
| 639 | p_ctx = (DVR_RecordContext_t *)handle; |
| 640 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 641 | if (p_ctx == &record_ctx[i]) |
| 642 | break; |
| 643 | } |
| 644 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 645 | DVR_RETURN_IF_FALSE(p_status); |
| 646 | |
| 647 | //lock |
| 648 | p_status->state = p_ctx->state; |
| 649 | p_status->info.id = p_ctx->segment_info.id; |
| 650 | p_status->info.duration = p_ctx->segment_info.duration; |
| 651 | p_status->info.size = p_ctx->segment_info.size; |
| 652 | p_status->info.nb_packets = p_ctx->segment_info.size/188; |
| 653 | |
| 654 | return DVR_SUCCESS; |
| 655 | } |
| 656 | |
| 657 | int dvr_record_write(DVR_RecordHandle_t handle, void *buffer, uint32_t len) |
| 658 | { |
| 659 | DVR_RecordContext_t *p_ctx; |
| 660 | uint32_t i; |
| 661 | off_t pos = 0; |
| 662 | int ret; |
| 663 | int has_pcr; |
| 664 | |
| 665 | p_ctx = (DVR_RecordContext_t *)handle; |
| 666 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 667 | if (p_ctx == &record_ctx[i]) |
| 668 | break; |
| 669 | } |
| 670 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 671 | DVR_RETURN_IF_FALSE(buffer); |
| 672 | DVR_RETURN_IF_FALSE(len); |
| 673 | |
| 674 | pos = segment_tell_position(p_ctx->segment_handle); |
| 675 | has_pcr = record_do_pcr_index(p_ctx, buffer, len); |
| 676 | if (has_pcr == 0) { |
| 677 | /* Pull VOD record shoud use PCR time index */ |
| 678 | DVR_DEBUG(1, "%s has no pcr, can NOT do time index", __func__); |
| 679 | } |
| 680 | ret = segment_write(p_ctx->segment_handle, buffer, len); |
| 681 | p_ctx->segment_info.size += len; |
| 682 | p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188; |
| 683 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 684 | return DVR_SUCCESS; |
| 685 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 686 | |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 687 | 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] | 688 | { |
| 689 | DVR_RecordContext_t *p_ctx; |
| 690 | uint32_t i; |
| 691 | |
| 692 | p_ctx = (DVR_RecordContext_t *)handle; |
| 693 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 694 | if (p_ctx == &record_ctx[i]) |
| 695 | break; |
| 696 | } |
| 697 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 698 | DVR_RETURN_IF_FALSE(func); |
| 699 | |
| 700 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
| 701 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 702 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 703 | |
| 704 | p_ctx->enc_func = func; |
| 705 | p_ctx->enc_userdata = userdata; |
| 706 | return DVR_SUCCESS; |
| 707 | } |
| 708 | |
| 709 | int dvr_record_set_secure_buffer(DVR_RecordHandle_t handle, uint8_t *p_secure_buf, uint32_t len) |
| 710 | { |
| 711 | DVR_RecordContext_t *p_ctx; |
| 712 | uint32_t i; |
| 713 | int ret; |
| 714 | |
| 715 | p_ctx = (DVR_RecordContext_t *)handle; |
| 716 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 717 | if (p_ctx == &record_ctx[i]) |
| 718 | break; |
| 719 | } |
| 720 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 721 | DVR_RETURN_IF_FALSE(p_secure_buf); |
| 722 | DVR_RETURN_IF_FALSE(len); |
| 723 | |
| 724 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
| 725 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 726 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 727 | |
| 728 | ret = record_device_set_secure_buffer(p_ctx->dev_handle, p_secure_buf, len); |
| 729 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 730 | |
| 731 | p_ctx->is_secure_mode = 1; |
| 732 | return ret; |
| 733 | } |