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 | } |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame^] | 255 | //add DVR_RECORD_EVENT_WRITE_ERROR event if write error |
| 256 | if (ret == -1 && len > 0) { |
| 257 | //send write event |
| 258 | p_ctx->event_notify_fn(DVR_RECORD_EVENT_WRITE_ERROR, NULL, p_ctx->event_userdata); |
| 259 | DVR_DEBUG(1, "%s notify DVR_RECORD_EVENT_WRITE_ERROR, exit record thread"); |
| 260 | goto end; |
| 261 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 262 | /* Do time index */ |
| 263 | uint8_t *index_buf = p_ctx->enc_func ? buf_out : buf; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 264 | pos = segment_tell_position(p_ctx->segment_handle); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 265 | has_pcr = record_do_pcr_index(p_ctx, index_buf, len); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 266 | if (has_pcr == 0 && index_type == DVR_INDEX_TYPE_INVALID) { |
| 267 | clock_gettime(CLOCK_MONOTONIC, &end_ts); |
| 268 | if ((end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) - |
| 269 | (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) > 40) { |
| 270 | /* PCR interval threshlod > 40 ms*/ |
| 271 | DVR_DEBUG(1, "%s use local clock time index", __func__); |
| 272 | index_type = DVR_INDEX_TYPE_LOCAL_CLOCK; |
| 273 | } |
| 274 | } else if (has_pcr && index_type == DVR_INDEX_TYPE_INVALID){ |
| 275 | DVR_DEBUG(1, "%s use pcr time index", __func__); |
| 276 | index_type = DVR_INDEX_TYPE_PCR; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 277 | } |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 278 | |
| 279 | /* Update segment info */ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 280 | p_ctx->segment_info.size += len; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 281 | /*Duration need use pcr to calculate, todo...*/ |
| 282 | if (index_type == DVR_INDEX_TYPE_PCR) { |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 283 | 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] | 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 | } else if (index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) { |
| 287 | clock_gettime(CLOCK_MONOTONIC, &end_ts); |
| 288 | p_ctx->segment_info.duration = (end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) - |
| 289 | (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 290 | if (pre_time == 0) |
| 291 | pre_time = p_ctx->segment_info.duration; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 292 | segment_update_pts(p_ctx->segment_handle, p_ctx->segment_info.duration, pos); |
| 293 | } else { |
| 294 | DVR_DEBUG(1, "%s can NOT do time index", __func__); |
| 295 | } |
| 296 | p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 297 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 298 | if (p_ctx->segment_info.duration - pre_time > DVR_STORE_INFO_TIME) { |
| 299 | pre_time = p_ctx->segment_info.duration + DVR_STORE_INFO_TIME; |
| 300 | segment_store_info(p_ctx->segment_handle, &(p_ctx->segment_info)); |
| 301 | } |
| 302 | /*Event notification*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 303 | if (p_ctx->notification_size && |
| 304 | p_ctx->event_notify_fn && |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 305 | /*!(p_ctx->segment_info.size % p_ctx->notification_size)*/ |
| 306 | (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] | 307 | p_ctx->segment_info.duration > 0) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 308 | memset(&record_status, 0, sizeof(record_status)); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 309 | //clock_gettime(CLOCK_MONOTONIC, &end_ts); |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 310 | p_ctx->last_send_size = p_ctx->segment_info.size; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 311 | record_status.state = p_ctx->state; |
| 312 | record_status.info.id = p_ctx->segment_info.id; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 313 | record_status.info.duration = p_ctx->segment_info.duration; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 314 | record_status.info.size = p_ctx->segment_info.size; |
| 315 | record_status.info.nb_packets = p_ctx->segment_info.size/188; |
| 316 | p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata); |
| 317 | DVR_DEBUG(1, "%s notify record status, state:%d, id:%lld, duration:%ld ms, size:%zu", |
| 318 | __func__, record_status.state, record_status.info.id, record_status.info.duration, record_status.info.size); |
| 319 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 320 | } |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame^] | 321 | end: |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 322 | free((void *)buf); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 323 | free((void *)buf_out); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 324 | DVR_DEBUG(1, "exit %s", __func__); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 325 | return NULL; |
| 326 | } |
| 327 | |
| 328 | int dvr_record_open(DVR_RecordHandle_t *p_handle, DVR_RecordOpenParams_t *params) |
| 329 | { |
| 330 | DVR_RecordContext_t *p_ctx; |
| 331 | Record_DeviceOpenParams_t dev_open_params; |
| 332 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 333 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 334 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 335 | DVR_RETURN_IF_FALSE(p_handle); |
| 336 | DVR_RETURN_IF_FALSE(params); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 337 | |
| 338 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 339 | if (record_ctx[i].state == DVR_RECORD_STATE_CLOSED) { |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 340 | break; |
| 341 | } |
| 342 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 343 | DVR_RETURN_IF_FALSE(record_ctx[i].state == DVR_RECORD_STATE_CLOSED); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 344 | p_ctx = &record_ctx[i]; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 345 | DVR_DEBUG(1, "%s , current state:%d, dmx_id:%d, notification_size:%zu", __func__, |
| 346 | p_ctx->state, params->dmx_dev_id, params->notification_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 347 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 348 | /*Process event params*/ |
| 349 | p_ctx->notification_size = params->notification_size; |
| 350 | p_ctx->event_notify_fn = params->event_fn; |
| 351 | p_ctx->event_userdata = params->event_userdata; |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 352 | p_ctx->last_send_size = 0; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 353 | /*Process crypto params, todo*/ |
| 354 | #if 0 |
| 355 | DVR_CryptoPeriod_t crypto_period; /**< DVR crypto period information*/ |
| 356 | DVR_CryptoFunction_t crypto_fn; /**< DVR crypto callback function*/ |
| 357 | void *crypto_data; /**< DVR crypto userdata*/ |
| 358 | #endif |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 359 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 360 | memset((void *)&dev_open_params, 0, sizeof(dev_open_params)); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 361 | if (params->data_from_memory) { |
| 362 | /* data from memory, VOD case */ |
| 363 | p_ctx->is_vod = 1; |
| 364 | } else { |
| 365 | p_ctx->is_vod = 0; |
| 366 | /* data from dmx, normal dvr case */ |
hualing chen | 958fe4c | 2020-03-24 17:35:07 +0800 | [diff] [blame] | 367 | dev_open_params.dmx_dev_id = params->dmx_dev_id; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 368 | 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] | 369 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 370 | ret = record_device_open(&p_ctx->dev_handle, &dev_open_params); |
| 371 | if (ret != DVR_SUCCESS) { |
| 372 | DVR_DEBUG(1, "%s, open record devices failed", __func__); |
| 373 | return DVR_FAILURE; |
| 374 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 375 | } |
| 376 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 377 | p_ctx->enc_func = NULL; |
| 378 | p_ctx->enc_userdata = NULL; |
| 379 | p_ctx->is_secure_mode = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 380 | p_ctx->state = DVR_RECORD_STATE_OPENED; |
| 381 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 382 | *p_handle = p_ctx; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 383 | return DVR_SUCCESS; |
| 384 | } |
| 385 | |
| 386 | int dvr_record_close(DVR_RecordHandle_t handle) |
| 387 | { |
| 388 | DVR_RecordContext_t *p_ctx; |
| 389 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 390 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 391 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 392 | p_ctx = (DVR_RecordContext_t *)handle; |
| 393 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 394 | if (p_ctx == &record_ctx[i]) |
| 395 | break; |
| 396 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 397 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 398 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 399 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 400 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 401 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 402 | if (p_ctx->is_vod) { |
| 403 | ret = DVR_SUCCESS; |
| 404 | } else { |
| 405 | ret = record_device_close(p_ctx->dev_handle); |
| 406 | if (ret != DVR_SUCCESS) { |
| 407 | DVR_DEBUG(1, "%s, failed", __func__); |
| 408 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 409 | } |
| 410 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 411 | p_ctx->state = DVR_RECORD_STATE_CLOSED; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 412 | return ret; |
| 413 | } |
| 414 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 415 | #if 0 |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 416 | int dvr_record_register_encryption(DVR_RecordHandle_t handle, |
| 417 | DVR_CryptoFunction_t cb, |
| 418 | DVR_CryptoParams_t params, |
| 419 | void *userdata) |
| 420 | { |
| 421 | return DVR_SUCCESS; |
| 422 | } |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 423 | #endif |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 424 | |
| 425 | int dvr_record_start_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params) |
| 426 | { |
| 427 | DVR_RecordContext_t *p_ctx; |
| 428 | Segment_OpenParams_t open_params; |
| 429 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 430 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 431 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 432 | p_ctx = (DVR_RecordContext_t *)handle; |
| 433 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 434 | if (p_ctx == &record_ctx[i]) |
| 435 | break; |
| 436 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 437 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 438 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 439 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 440 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 441 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 442 | DVR_RETURN_IF_FALSE(params); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 443 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 444 | 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] | 445 | memset(&open_params, 0, sizeof(open_params)); |
| 446 | memcpy(open_params.location, params->location, strlen(params->location)); |
| 447 | open_params.segment_id = params->segment.segment_id; |
| 448 | open_params.mode = SEGMENT_MODE_WRITE; |
| 449 | |
| 450 | ret = segment_open(&open_params, &p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 451 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 452 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 453 | /*process params*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 454 | { |
| 455 | memcpy(p_ctx->location, params->location, strlen(params->location)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 456 | //need all params?? |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 457 | memcpy(&p_ctx->segment_params, ¶ms->segment, sizeof(params->segment)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 458 | /*save current segment info*/ |
| 459 | memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info)); |
| 460 | p_ctx->segment_info.id = params->segment.segment_id; |
| 461 | p_ctx->segment_info.nb_pids = params->segment.nb_pids; |
| 462 | 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] | 463 | } |
| 464 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 465 | if (!p_ctx->is_vod) { |
| 466 | /* normal dvr case */ |
| 467 | for (i = 0; i < params->segment.nb_pids; i++) { |
| 468 | ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid); |
| 469 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 470 | } |
| 471 | ret = record_device_start(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 472 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 473 | } |
| 474 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 475 | p_ctx->state = DVR_RECORD_STATE_STARTED; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 476 | if (!p_ctx->is_vod) |
| 477 | pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 478 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 479 | return DVR_SUCCESS; |
| 480 | } |
| 481 | |
| 482 | int dvr_record_next_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, DVR_RecordSegmentInfo_t *p_info) |
| 483 | { |
| 484 | DVR_RecordContext_t *p_ctx; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 485 | Segment_OpenParams_t open_params; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 486 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 487 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 488 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 489 | p_ctx = (DVR_RecordContext_t *)handle; |
| 490 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 491 | if (p_ctx == &record_ctx[i]) |
| 492 | break; |
| 493 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 494 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 495 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 496 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 497 | DVR_RETURN_IF_FALSE(p_ctx->state == DVR_RECORD_STATE_STARTED); |
| 498 | //DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 499 | DVR_RETURN_IF_FALSE(params); |
| 500 | DVR_RETURN_IF_FALSE(p_info); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 501 | DVR_RETURN_IF_FALSE(!p_ctx->is_vod); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 502 | |
| 503 | /*Stop the on going record segment*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 504 | //ret = record_device_stop(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 505 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 506 | |
| 507 | p_ctx->state = DVR_RECORD_STATE_STOPPED; |
| 508 | pthread_join(p_ctx->thread, NULL); |
| 509 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 510 | /*Update segment info*/ |
| 511 | memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info)); |
| 512 | |
| 513 | ret = segment_store_info(p_ctx->segment_handle, p_info); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 514 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 515 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 516 | 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", |
| 517 | __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] | 518 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 519 | /*Close current segment*/ |
| 520 | ret = segment_close(p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 521 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 522 | /*Open the new record segment*/ |
| 523 | memset(&open_params, 0, sizeof(open_params)); |
| 524 | memcpy(open_params.location, p_ctx->location, strlen(p_ctx->location)); |
| 525 | open_params.segment_id = params->segment.segment_id; |
| 526 | open_params.mode = SEGMENT_MODE_WRITE; |
| 527 | |
| 528 | ret = segment_open(&open_params, &p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 529 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 530 | /*process params*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 531 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 532 | //need all params?? |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 533 | memcpy(&p_ctx->segment_params, ¶ms->segment, sizeof(params->segment)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 534 | /*save current segment info*/ |
| 535 | memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info)); |
| 536 | p_ctx->segment_info.id = params->segment.segment_id; |
| 537 | 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] | 538 | } |
| 539 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 540 | p_ctx->segment_info.nb_pids = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 541 | for (i = 0; i < params->segment.nb_pids; i++) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 542 | switch (params->segment.pid_action[i]) { |
| 543 | case DVR_RECORD_PID_CREATE: |
| 544 | DVR_DEBUG(1, "%s create pid:%d", __func__, params->segment.pids[i].pid); |
| 545 | ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid); |
| 546 | p_ctx->segment_info.nb_pids++; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 547 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 548 | break; |
| 549 | case DVR_RECORD_PID_KEEP: |
| 550 | DVR_DEBUG(1, "%s keep pid:%d", __func__, params->segment.pids[i].pid); |
| 551 | p_ctx->segment_info.nb_pids++; |
| 552 | break; |
| 553 | case DVR_RECORD_PID_CLOSE: |
| 554 | DVR_DEBUG(1, "%s close pid:%d", __func__, params->segment.pids[i].pid); |
| 555 | 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] | 556 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 557 | break; |
| 558 | default: |
| 559 | DVR_DEBUG(1, "%s wrong action pid:%d", __func__, params->segment.pids[i].pid); |
| 560 | return DVR_FAILURE; |
| 561 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 562 | } |
| 563 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 564 | //ret = record_device_start(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 565 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 566 | |
| 567 | pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx); |
| 568 | p_ctx->state = DVR_RECORD_STATE_STARTED; |
| 569 | return DVR_SUCCESS; |
| 570 | } |
| 571 | |
| 572 | int dvr_record_stop_segment(DVR_RecordHandle_t handle, DVR_RecordSegmentInfo_t *p_info) |
| 573 | { |
| 574 | DVR_RecordContext_t *p_ctx; |
| 575 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 576 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 577 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 578 | p_ctx = (DVR_RecordContext_t *)handle; |
| 579 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 580 | if (p_ctx == &record_ctx[i]) |
| 581 | break; |
| 582 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 583 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 584 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 585 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 586 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STOPPED); |
| 587 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 588 | DVR_RETURN_IF_FALSE(p_info);/*should support NULL*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 589 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 590 | p_ctx->state = DVR_RECORD_STATE_STOPPED; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 591 | if (p_ctx->is_vod) { |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 592 | 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] | 593 | p_ctx->segment_info.duration = 10*1000; //debug, should delete it |
| 594 | } else { |
| 595 | ret = record_device_stop(p_ctx->dev_handle); |
| 596 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 597 | //p_ctx->state = DVR_RECORD_STATE_STOPPED; |
| 598 | pthread_join(p_ctx->thread, NULL); |
| 599 | } |
| 600 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 601 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 602 | /*Update segment info*/ |
| 603 | memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info)); |
| 604 | |
| 605 | ret = segment_store_info(p_ctx->segment_handle, p_info); |
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 | |
| 608 | DVR_DEBUG(1, "%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d", |
| 609 | __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets); |
| 610 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 611 | ret = segment_close(p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 612 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 613 | return DVR_SUCCESS; |
| 614 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 615 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 616 | int dvr_record_resume_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, uint64_t *p_resume_size) |
| 617 | { |
| 618 | DVR_RecordContext_t *p_ctx; |
| 619 | uint32_t i; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 620 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 621 | |
| 622 | p_ctx = (DVR_RecordContext_t *)handle; |
| 623 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 624 | if (p_ctx == &record_ctx[i]) |
| 625 | break; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 626 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 627 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 628 | DVR_RETURN_IF_FALSE(params); |
| 629 | DVR_RETURN_IF_FALSE(p_resume_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 630 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 631 | DVR_DEBUG(1, "%s , current state:%d, resume size:%lld", __func__, p_ctx->state, *p_resume_size); |
| 632 | ret = dvr_record_start_segment(handle, params); |
| 633 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 634 | |
| 635 | p_ctx->segment_info.size = *p_resume_size; |
| 636 | |
| 637 | return DVR_SUCCESS; |
| 638 | } |
| 639 | |
| 640 | int dvr_record_get_status(DVR_RecordHandle_t handle, DVR_RecordStatus_t *p_status) |
| 641 | { |
| 642 | DVR_RecordContext_t *p_ctx; |
| 643 | int i; |
| 644 | |
| 645 | p_ctx = (DVR_RecordContext_t *)handle; |
| 646 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 647 | if (p_ctx == &record_ctx[i]) |
| 648 | break; |
| 649 | } |
| 650 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 651 | DVR_RETURN_IF_FALSE(p_status); |
| 652 | |
| 653 | //lock |
| 654 | p_status->state = p_ctx->state; |
| 655 | p_status->info.id = p_ctx->segment_info.id; |
| 656 | p_status->info.duration = p_ctx->segment_info.duration; |
| 657 | p_status->info.size = p_ctx->segment_info.size; |
| 658 | p_status->info.nb_packets = p_ctx->segment_info.size/188; |
| 659 | |
| 660 | return DVR_SUCCESS; |
| 661 | } |
| 662 | |
| 663 | int dvr_record_write(DVR_RecordHandle_t handle, void *buffer, uint32_t len) |
| 664 | { |
| 665 | DVR_RecordContext_t *p_ctx; |
| 666 | uint32_t i; |
| 667 | off_t pos = 0; |
| 668 | int ret; |
| 669 | int has_pcr; |
| 670 | |
| 671 | p_ctx = (DVR_RecordContext_t *)handle; |
| 672 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 673 | if (p_ctx == &record_ctx[i]) |
| 674 | break; |
| 675 | } |
| 676 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 677 | DVR_RETURN_IF_FALSE(buffer); |
| 678 | DVR_RETURN_IF_FALSE(len); |
| 679 | |
| 680 | pos = segment_tell_position(p_ctx->segment_handle); |
| 681 | has_pcr = record_do_pcr_index(p_ctx, buffer, len); |
| 682 | if (has_pcr == 0) { |
| 683 | /* Pull VOD record shoud use PCR time index */ |
| 684 | DVR_DEBUG(1, "%s has no pcr, can NOT do time index", __func__); |
| 685 | } |
| 686 | ret = segment_write(p_ctx->segment_handle, buffer, len); |
| 687 | p_ctx->segment_info.size += len; |
| 688 | p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188; |
| 689 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 690 | return DVR_SUCCESS; |
| 691 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 692 | |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 693 | 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] | 694 | { |
| 695 | DVR_RecordContext_t *p_ctx; |
| 696 | uint32_t i; |
| 697 | |
| 698 | p_ctx = (DVR_RecordContext_t *)handle; |
| 699 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 700 | if (p_ctx == &record_ctx[i]) |
| 701 | break; |
| 702 | } |
| 703 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 704 | DVR_RETURN_IF_FALSE(func); |
| 705 | |
| 706 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
| 707 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 708 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 709 | |
| 710 | p_ctx->enc_func = func; |
| 711 | p_ctx->enc_userdata = userdata; |
| 712 | return DVR_SUCCESS; |
| 713 | } |
| 714 | |
| 715 | int dvr_record_set_secure_buffer(DVR_RecordHandle_t handle, uint8_t *p_secure_buf, uint32_t len) |
| 716 | { |
| 717 | DVR_RecordContext_t *p_ctx; |
| 718 | uint32_t i; |
| 719 | int ret; |
| 720 | |
| 721 | p_ctx = (DVR_RecordContext_t *)handle; |
| 722 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 723 | if (p_ctx == &record_ctx[i]) |
| 724 | break; |
| 725 | } |
| 726 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 727 | DVR_RETURN_IF_FALSE(p_secure_buf); |
| 728 | DVR_RETURN_IF_FALSE(len); |
| 729 | |
| 730 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
| 731 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 732 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 733 | |
| 734 | ret = record_device_set_secure_buffer(p_ctx->dev_handle, p_secure_buf, len); |
| 735 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 736 | |
| 737 | p_ctx->is_secure_mode = 1; |
| 738 | return ret; |
| 739 | } |