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" |
Gong Ke | 2a0ebbe | 2021-05-25 15:22:50 +0800 | [diff] [blame] | 9 | #include "dvb_utils.h" |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 10 | #include "record_device.h" |
| 11 | #include "segment.h" |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 12 | #include <sys/time.h> |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 13 | #include "am_crypt.h" |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 14 | |
hualing chen | d241c7a | 2021-06-22 13:34:27 +0800 | [diff] [blame] | 15 | #define CONTROL_SPEED_ENABLE 0 |
| 16 | |
hualing chen | c7aa4c8 | 2021-02-03 15:41:37 +0800 | [diff] [blame] | 17 | //#define DEBUG_PERFORMANCE |
Yahui Han | 1099389 | 2021-11-02 14:27:33 +0800 | [diff] [blame] | 18 | #define MAX_DVR_RECORD_SESSION_COUNT 4 |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 19 | #define RECORD_BLOCK_SIZE (256 * 1024) |
Yahui Han | ce15e9c | 2020-12-08 18:08:32 +0800 | [diff] [blame] | 20 | #define NEW_DEVICE_RECORD_BLOCK_SIZE (1024 * 188) |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 21 | |
| 22 | /**\brief DVR index file type*/ |
| 23 | typedef enum { |
| 24 | DVR_INDEX_TYPE_PCR, /**< DVR index file use pcr*/ |
| 25 | DVR_INDEX_TYPE_LOCAL_CLOCK, /**< DVR index file use local clock*/ |
| 26 | DVR_INDEX_TYPE_INVALID /**< DVR index file type invalid type*/ |
| 27 | } DVR_IndexType_t; |
| 28 | |
| 29 | /**\brief DVR VOD context*/ |
| 30 | typedef struct { |
| 31 | pthread_mutex_t mutex; /**< VOD mutex lock*/ |
| 32 | pthread_cond_t cond; /**< VOD condition*/ |
| 33 | void *buffer; /**< VOD buffer*/ |
| 34 | uint32_t buf_len; /**< VOD buffer len*/ |
| 35 | } DVR_VodContext_t; |
| 36 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 37 | /**\brief DVR record secure mode buffer*/ |
| 38 | typedef struct { |
| 39 | uint32_t addr; /**< Secure mode record buffer address*/ |
| 40 | uint32_t len; /**< Secure mode record buffer length*/ |
| 41 | } DVR_SecureBuffer_t; |
| 42 | |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 43 | /**\brief DVR record new dmx secure mode buffer*/ |
| 44 | typedef struct { |
| 45 | uint32_t buf_start; /**< Secure mode record buffer address*/ |
| 46 | uint32_t buf_end; /**< Secure mode record buffer length*/ |
| 47 | uint32_t data_start; /**< Secure mode record buffer address*/ |
| 48 | uint32_t data_end; /**< Secure mode record buffer length*/ |
| 49 | } DVR_NewDmxSecureBuffer_t; |
| 50 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 51 | /**\brief DVR record context*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 52 | typedef struct { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 53 | pthread_t thread; /**< DVR thread handle*/ |
| 54 | Record_DeviceHandle_t dev_handle; /**< DVR device handle*/ |
| 55 | Segment_Handle_t segment_handle; /**< DVR segment handle*/ |
| 56 | DVR_RecordState_t state; /**< DVR record state*/ |
| 57 | char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/ |
| 58 | DVR_RecordSegmentStartParams_t segment_params; /**< DVR record start parameters*/ |
| 59 | DVR_RecordSegmentInfo_t segment_info; /**< DVR record current segment info*/ |
| 60 | size_t notification_size; /**< DVR record nogification size*/ |
| 61 | DVR_RecordEventFunction_t event_notify_fn; /**< DVR record event notify function*/ |
| 62 | void *event_userdata; /**< DVR record event userdata*/ |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 63 | //DVR_VodContext_t vod; /**< DVR record vod context*/ |
| 64 | int is_vod; /**< Indicate current mode is VOD record mode*/ |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 65 | DVR_CryptoFunction_t enc_func; /**< Encrypt function*/ |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 66 | void *enc_userdata; /**< Encrypt userdata*/ |
| 67 | int is_secure_mode; /**< Record session run in secure pipeline */ |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 68 | void *cryptor; /**< Cryptor for encrypted PVR on FTA.*/ |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 69 | size_t last_send_size; /**< Last send notify segment size */ |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 70 | uint32_t block_size; /**< DVR record block size */ |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 71 | DVR_Bool_t is_new_dmx; /**< DVR is used new dmx driver */ |
hualing chen | 40accc3 | 2021-04-21 15:58:09 +0800 | [diff] [blame] | 72 | int index_type; /**< DVR is used pcr or local time */ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 73 | } DVR_RecordContext_t; |
| 74 | |
Gong Ke | 2a0ebbe | 2021-05-25 15:22:50 +0800 | [diff] [blame] | 75 | extern ssize_t record_device_read_ext(Record_DeviceHandle_t handle, size_t *buf, size_t *len); |
| 76 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 77 | static DVR_RecordContext_t record_ctx[MAX_DVR_RECORD_SESSION_COUNT] = { |
| 78 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 79 | .state = DVR_RECORD_STATE_CLOSED |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 80 | }, |
| 81 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 82 | .state = DVR_RECORD_STATE_CLOSED |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 83 | } |
| 84 | }; |
| 85 | |
Zhiqiang Han | 51646a0 | 2020-04-05 18:43:22 +0800 | [diff] [blame] | 86 | static int record_is_valid_pid(DVR_RecordContext_t *p_ctx, int pid) |
| 87 | { |
| 88 | int i; |
| 89 | |
| 90 | for (i = 0; i < p_ctx->segment_info.nb_pids; i++) { |
| 91 | if (pid == p_ctx->segment_info.pids[i].pid) |
| 92 | return 1; |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 97 | static int record_save_pcr(DVR_RecordContext_t *p_ctx, uint8_t *buf, loff_t pos) |
| 98 | { |
| 99 | uint8_t *p = buf; |
| 100 | int len; |
| 101 | uint8_t afc; |
| 102 | uint64_t pcr = 0; |
| 103 | int has_pcr = 0; |
| 104 | int pid; |
| 105 | int adp_field_len; |
| 106 | |
| 107 | pid = ((p[1] & 0x1f) << 8) | p[2]; |
Zhiqiang Han | 51646a0 | 2020-04-05 18:43:22 +0800 | [diff] [blame] | 108 | if (pid == 0x1fff || !record_is_valid_pid(p_ctx, pid)) |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 109 | return has_pcr; |
| 110 | |
| 111 | //scramble = p[3] >> 6; |
| 112 | //cc = p[3] & 0x0f; |
| 113 | afc = (p[3] >> 4) & 0x03; |
| 114 | |
| 115 | p += 4; |
| 116 | len = 184; |
| 117 | |
| 118 | if (afc & 2) { |
| 119 | adp_field_len = p[0]; |
| 120 | /* Skip adaptation len */ |
| 121 | p++; |
| 122 | len--; |
| 123 | /* Parse pcr field, see 13818 spec table I-2-6,adaptation_field */ |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 124 | if (p[0] & 0x10 && len >= 6 && adp_field_len >= 6) { |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 125 | /* get pcr value,pcr is 33bit value */ |
| 126 | pcr = (((uint64_t)(p[1])) << 25) |
| 127 | | (((uint64_t)p[2]) << 17) |
| 128 | | (((uint64_t)(p[3])) << 9) |
| 129 | | (((uint64_t)p[4]) << 1) |
| 130 | | ((((uint64_t)p[5]) & 0x80) >> 7); |
| 131 | has_pcr = 1; |
| 132 | } |
| 133 | |
| 134 | p += adp_field_len; |
| 135 | len -= adp_field_len; |
| 136 | |
| 137 | if (len < 0) { |
| 138 | DVR_DEBUG(1, "parser pcr: illegal adaptation field length"); |
| 139 | return 0; |
| 140 | } |
| 141 | } |
| 142 | |
hualing chen | 40accc3 | 2021-04-21 15:58:09 +0800 | [diff] [blame] | 143 | if (has_pcr && p_ctx->index_type == DVR_INDEX_TYPE_PCR) { |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 144 | segment_update_pts(p_ctx->segment_handle, pcr/90, pos); |
| 145 | } |
| 146 | return has_pcr; |
| 147 | } |
| 148 | |
| 149 | static int record_do_pcr_index(DVR_RecordContext_t *p_ctx, uint8_t *buf, int len) |
| 150 | { |
| 151 | uint8_t *p = buf; |
| 152 | int left = len; |
| 153 | loff_t pos; |
| 154 | int has_pcr = 0; |
| 155 | |
| 156 | pos = segment_tell_position(p_ctx->segment_handle); |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame^] | 157 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 158 | while (left >= 188) { |
| 159 | if (*p == 0x47) { |
| 160 | has_pcr |= record_save_pcr(p_ctx, p, pos); |
| 161 | p += 188; |
| 162 | left -= 188; |
| 163 | pos += 188; |
| 164 | } else { |
| 165 | p++; |
| 166 | left --; |
| 167 | pos++; |
| 168 | } |
| 169 | } |
| 170 | return has_pcr; |
| 171 | } |
| 172 | |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 173 | static int get_diff_time(struct timeval start_tv, struct timeval end_tv) |
| 174 | { |
| 175 | return end_tv.tv_sec * 1000 + end_tv.tv_usec / 1000 - start_tv.tv_sec * 1000 - start_tv.tv_usec / 1000; |
| 176 | } |
| 177 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 178 | void *record_thread(void *arg) |
| 179 | { |
| 180 | DVR_RecordContext_t *p_ctx = (DVR_RecordContext_t *)arg; |
| 181 | ssize_t len; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 182 | uint8_t *buf, *buf_out; |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 183 | uint32_t block_size = p_ctx->block_size; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 184 | loff_t pos = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 185 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 186 | struct timespec start_ts, end_ts; |
| 187 | DVR_RecordStatus_t record_status; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 188 | int has_pcr; |
hualing chen | 40accc3 | 2021-04-21 15:58:09 +0800 | [diff] [blame] | 189 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 190 | time_t pre_time = 0; |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 191 | #define DVR_STORE_INFO_TIME (400) |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 192 | DVR_SecureBuffer_t secure_buf; |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 193 | DVR_NewDmxSecureBuffer_t new_dmx_secure_buf; |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame^] | 194 | int first_read = 0; |
hualing chen | d241c7a | 2021-06-22 13:34:27 +0800 | [diff] [blame] | 195 | if (CONTROL_SPEED_ENABLE == 0) |
| 196 | p_ctx->index_type = DVR_INDEX_TYPE_INVALID; |
| 197 | else |
| 198 | p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 199 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 200 | buf = (uint8_t *)malloc(block_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 201 | if (!buf) { |
| 202 | DVR_DEBUG(1, "%s, malloc failed", __func__); |
| 203 | return NULL; |
| 204 | } |
| 205 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 206 | buf_out = (uint8_t *)malloc(block_size + 188); |
| 207 | if (!buf_out) { |
| 208 | DVR_DEBUG(1, "%s, malloc failed", __func__); |
Pengfei Liu | faf38e4 | 2020-05-22 00:28:02 +0800 | [diff] [blame] | 209 | free(buf); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 210 | return NULL; |
| 211 | } |
| 212 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 213 | memset(&record_status, 0, sizeof(record_status)); |
| 214 | record_status.state = DVR_RECORD_STATE_STARTED; |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 215 | if (p_ctx->event_notify_fn) { |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 216 | record_status.info.id = p_ctx->segment_info.id; |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 217 | 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] | 218 | DVR_DEBUG(1, "%s line %d notify record status, state:%d id=%lld", |
| 219 | __func__,__LINE__, record_status.state, p_ctx->segment_info.id); |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 220 | } |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 221 | DVR_DEBUG(1, "%s, secure_mode:%d, block_size:%d, cryptor:%p", |
| 222 | __func__, p_ctx->is_secure_mode, |
| 223 | block_size, p_ctx->cryptor); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 224 | clock_gettime(CLOCK_MONOTONIC, &start_ts); |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 225 | |
| 226 | struct timeval t1, t2, t3, t4, t5, t6, t7; |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 227 | while (p_ctx->state == DVR_RECORD_STATE_STARTED || |
| 228 | p_ctx->state == DVR_RECORD_STATE_PAUSE) { |
| 229 | |
| 230 | if (p_ctx->state == DVR_RECORD_STATE_PAUSE) { |
| 231 | //wait resume record |
| 232 | usleep(20*1000); |
| 233 | continue; |
| 234 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 235 | gettimeofday(&t1, NULL); |
hualing chen | c70a8df | 2020-05-12 19:23:11 +0800 | [diff] [blame] | 236 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 237 | /* data from dmx, normal dvr case */ |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 238 | if (p_ctx->is_secure_mode) { |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 239 | if (p_ctx->is_new_dmx) { |
Yahui Han | ce15e9c | 2020-12-08 18:08:32 +0800 | [diff] [blame] | 240 | |
| 241 | /* We resolve the below invoke for dvbcore to be under safety status */ |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 242 | memset(&new_dmx_secure_buf, 0, sizeof(new_dmx_secure_buf)); |
Yahui Han | ce15e9c | 2020-12-08 18:08:32 +0800 | [diff] [blame] | 243 | len = record_device_read(p_ctx->dev_handle, &new_dmx_secure_buf, sizeof(new_dmx_secure_buf), 10); |
| 244 | if (len == DVR_FAILURE) { |
Gong Ke | 2a0ebbe | 2021-05-25 15:22:50 +0800 | [diff] [blame] | 245 | DVR_DEBUG(1, "handle[%p] ret:%d\n", p_ctx->dev_handle, ret); |
Yahui Han | ce15e9c | 2020-12-08 18:08:32 +0800 | [diff] [blame] | 246 | /*For the second recording, poll always failed which we should check |
| 247 | * dvbcore further. For now, Just ignore the fack poll fail, I think |
| 248 | * it won't influce anything. But we need adjust the poll timeout |
| 249 | * from 1000ms to 10ms. |
| 250 | */ |
| 251 | //continue; |
| 252 | } |
| 253 | |
| 254 | /* Read data from secure demux TA */ |
| 255 | len = record_device_read_ext(p_ctx->dev_handle, &secure_buf.addr, |
| 256 | &secure_buf.len); |
| 257 | |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 258 | } else { |
| 259 | memset(&secure_buf, 0, sizeof(secure_buf)); |
| 260 | len = record_device_read(p_ctx->dev_handle, &secure_buf, sizeof(secure_buf), 1000); |
| 261 | } |
| 262 | if (len != DVR_FAILURE) { |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame] | 263 | //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] | 264 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 265 | } else { |
| 266 | len = record_device_read(p_ctx->dev_handle, buf, block_size, 1000); |
| 267 | } |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 268 | if (len == DVR_FAILURE) { |
hualing chen | 6c12638 | 2020-04-13 15:47:51 +0800 | [diff] [blame] | 269 | //usleep(10*1000); |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 270 | DVR_DEBUG(1, "%s, start_read error", __func__); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 271 | continue; |
| 272 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 273 | gettimeofday(&t2, NULL); |
hualing chen | c70a8df | 2020-05-12 19:23:11 +0800 | [diff] [blame] | 274 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 275 | /* Got data from device, record it */ |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 276 | if (p_ctx->is_secure_mode && p_ctx->enc_func) { |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 277 | /* Encrypt record data */ |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 278 | DVR_CryptoParams_t crypto_params; |
| 279 | |
| 280 | memset(&crypto_params, 0, sizeof(crypto_params)); |
| 281 | crypto_params.type = DVR_CRYPTO_TYPE_ENCRYPT; |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 282 | memcpy(crypto_params.location, p_ctx->location, sizeof(p_ctx->location)); |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 283 | crypto_params.segment_id = p_ctx->segment_info.id; |
| 284 | crypto_params.offset = p_ctx->segment_info.size; |
| 285 | |
| 286 | if (p_ctx->is_secure_mode) { |
| 287 | crypto_params.input_buffer.type = DVR_BUFFER_TYPE_SECURE; |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 288 | crypto_params.input_buffer.addr = secure_buf.addr; |
| 289 | crypto_params.input_buffer.size = secure_buf.len; |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 290 | } else { |
| 291 | crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL; |
| 292 | crypto_params.input_buffer.addr = (size_t)buf; |
| 293 | crypto_params.input_buffer.size = len; |
| 294 | } |
| 295 | |
| 296 | crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL; |
| 297 | crypto_params.output_buffer.addr = (size_t)buf_out; |
| 298 | crypto_params.output_buffer.size = block_size + 188; |
hualing chen | 9811b21 | 2020-10-29 11:21:44 +0800 | [diff] [blame] | 299 | |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 300 | p_ctx->enc_func(&crypto_params, p_ctx->enc_userdata); |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 301 | gettimeofday(&t3, NULL); |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 302 | /* Out buffer length may not equal in buffer length */ |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 303 | if (crypto_params.output_size > 0) { |
| 304 | ret = segment_write(p_ctx->segment_handle, buf_out, crypto_params.output_size); |
| 305 | len = crypto_params.output_size; |
hualing chen | bafc62d | 2020-11-02 15:44:05 +0800 | [diff] [blame] | 306 | } else { |
| 307 | len = 0; |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 308 | } |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 309 | } else if (p_ctx->cryptor) { |
| 310 | /* Encrypt with clear key */ |
| 311 | am_crypt_des_crypt(p_ctx->cryptor, buf_out, buf, (uint32_t *)&len, 0); |
| 312 | gettimeofday(&t3, NULL); |
| 313 | ret = segment_write(p_ctx->segment_handle, buf_out, len); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 314 | } else { |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame^] | 315 | if (first_read == 0) { |
| 316 | first_read = 1; |
| 317 | DVR_DEBUG(1, "%s:%d,first read ts", __func__,__LINE__); |
| 318 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 319 | gettimeofday(&t3, NULL); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 320 | ret = segment_write(p_ctx->segment_handle, buf, len); |
| 321 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 322 | gettimeofday(&t4, NULL); |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 323 | //add DVR_RECORD_EVENT_WRITE_ERROR event if write error |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 324 | if (ret == -1 && len > 0 && p_ctx->event_notify_fn) { |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 325 | //send write event |
hualing chen | 9b434f0 | 2020-06-10 15:06:54 +0800 | [diff] [blame] | 326 | if (p_ctx->event_notify_fn) { |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 327 | memset(&record_status, 0, sizeof(record_status)); |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame] | 328 | DVR_DEBUG(1, "%s:%d,send event write error", __func__,__LINE__); |
hualing chen | 9ce7d94 | 2021-06-30 13:31:01 +0800 | [diff] [blame] | 329 | record_status.info.id = p_ctx->segment_info.id; |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 330 | p_ctx->event_notify_fn(DVR_RECORD_EVENT_WRITE_ERROR, &record_status, p_ctx->event_userdata); |
| 331 | } |
hualing chen | c70a8df | 2020-05-12 19:23:11 +0800 | [diff] [blame] | 332 | DVR_DEBUG(1, "%s,write error %d", __func__,__LINE__); |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 333 | goto end; |
| 334 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 335 | /* Do time index */ |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 336 | uint8_t *index_buf = (p_ctx->enc_func || p_ctx->cryptor)? buf_out : buf; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 337 | pos = segment_tell_position(p_ctx->segment_handle); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 338 | has_pcr = record_do_pcr_index(p_ctx, index_buf, len); |
hualing chen | 40accc3 | 2021-04-21 15:58:09 +0800 | [diff] [blame] | 339 | if (has_pcr == 0 && p_ctx->index_type == DVR_INDEX_TYPE_INVALID) { |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 340 | clock_gettime(CLOCK_MONOTONIC, &end_ts); |
| 341 | if ((end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) - |
| 342 | (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) > 40) { |
| 343 | /* PCR interval threshlod > 40 ms*/ |
| 344 | DVR_DEBUG(1, "%s use local clock time index", __func__); |
hualing chen | 40accc3 | 2021-04-21 15:58:09 +0800 | [diff] [blame] | 345 | p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 346 | } |
hualing chen | 40accc3 | 2021-04-21 15:58:09 +0800 | [diff] [blame] | 347 | } else if (has_pcr && p_ctx->index_type == DVR_INDEX_TYPE_INVALID){ |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 348 | DVR_DEBUG(1, "%s use pcr time index", __func__); |
hualing chen | 40accc3 | 2021-04-21 15:58:09 +0800 | [diff] [blame] | 349 | p_ctx->index_type = DVR_INDEX_TYPE_PCR; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 350 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 351 | gettimeofday(&t5, NULL); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 352 | |
| 353 | /* Update segment info */ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 354 | p_ctx->segment_info.size += len; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 355 | /*Duration need use pcr to calculate, todo...*/ |
hualing chen | 40accc3 | 2021-04-21 15:58:09 +0800 | [diff] [blame] | 356 | if (p_ctx->index_type == DVR_INDEX_TYPE_PCR) { |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 357 | 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] | 358 | if (pre_time == 0) |
| 359 | pre_time = p_ctx->segment_info.duration; |
hualing chen | 40accc3 | 2021-04-21 15:58:09 +0800 | [diff] [blame] | 360 | } else if (p_ctx->index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) { |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 361 | clock_gettime(CLOCK_MONOTONIC, &end_ts); |
| 362 | p_ctx->segment_info.duration = (end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) - |
| 363 | (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 364 | if (pre_time == 0) |
| 365 | pre_time = p_ctx->segment_info.duration; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 366 | segment_update_pts(p_ctx->segment_handle, p_ctx->segment_info.duration, pos); |
| 367 | } else { |
| 368 | DVR_DEBUG(1, "%s can NOT do time index", __func__); |
| 369 | } |
| 370 | p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 371 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 372 | if (p_ctx->segment_info.duration - pre_time > DVR_STORE_INFO_TIME) { |
| 373 | pre_time = p_ctx->segment_info.duration + DVR_STORE_INFO_TIME; |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 374 | segment_store_info(p_ctx->segment_handle, &(p_ctx->segment_info)); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 375 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 376 | gettimeofday(&t6, NULL); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 377 | /*Event notification*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 378 | if (p_ctx->notification_size && |
| 379 | p_ctx->event_notify_fn && |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 380 | /*!(p_ctx->segment_info.size % p_ctx->notification_size)*/ |
| 381 | (p_ctx->segment_info.size -p_ctx->last_send_size) >= p_ctx->notification_size&& |
hualing chen | be68d64 | 2021-09-09 13:06:36 +0800 | [diff] [blame] | 382 | p_ctx->segment_info.duration > 0 && |
| 383 | p_ctx->state == DVR_RECORD_STATE_STARTED) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 384 | memset(&record_status, 0, sizeof(record_status)); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 385 | //clock_gettime(CLOCK_MONOTONIC, &end_ts); |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 386 | p_ctx->last_send_size = p_ctx->segment_info.size; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 387 | record_status.state = p_ctx->state; |
| 388 | record_status.info.id = p_ctx->segment_info.id; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 389 | record_status.info.duration = p_ctx->segment_info.duration; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 390 | record_status.info.size = p_ctx->segment_info.size; |
| 391 | record_status.info.nb_packets = p_ctx->segment_info.size/188; |
| 392 | 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] | 393 | DVR_DEBUG(1, "%s notify record status, state:%d, id:%lld, duration:%ld ms, size:%zu loc[%s]", |
| 394 | __func__, record_status.state, |
| 395 | record_status.info.id, record_status.info.duration, |
| 396 | record_status.info.size, p_ctx->location); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 397 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 398 | gettimeofday(&t7, NULL); |
| 399 | #ifdef DEBUG_PERFORMANCE |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 400 | 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] | 401 | 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] | 402 | 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] | 403 | #endif |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 404 | } |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 405 | end: |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 406 | free((void *)buf); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 407 | free((void *)buf_out); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 408 | DVR_DEBUG(1, "exit %s", __func__); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | int dvr_record_open(DVR_RecordHandle_t *p_handle, DVR_RecordOpenParams_t *params) |
| 413 | { |
| 414 | DVR_RecordContext_t *p_ctx; |
| 415 | Record_DeviceOpenParams_t dev_open_params; |
| 416 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 417 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 418 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 419 | DVR_RETURN_IF_FALSE(p_handle); |
| 420 | DVR_RETURN_IF_FALSE(params); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 421 | |
| 422 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 423 | if (record_ctx[i].state == DVR_RECORD_STATE_CLOSED) { |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 424 | break; |
| 425 | } |
| 426 | } |
Pengfei Liu | faf38e4 | 2020-05-22 00:28:02 +0800 | [diff] [blame] | 427 | DVR_RETURN_IF_FALSE(i < MAX_DVR_RECORD_SESSION_COUNT); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 428 | DVR_RETURN_IF_FALSE(record_ctx[i].state == DVR_RECORD_STATE_CLOSED); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 429 | p_ctx = &record_ctx[i]; |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 430 | DVR_DEBUG(1, "%s , current state:%d, dmx_id:%d, notification_size:%zu, flags:%d, keylen:%d ", |
| 431 | __func__, p_ctx->state, params->dmx_dev_id, |
| 432 | params->notification_size, |
| 433 | params->flags, params->keylen); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 434 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 435 | /*Process event params*/ |
| 436 | p_ctx->notification_size = params->notification_size; |
| 437 | p_ctx->event_notify_fn = params->event_fn; |
| 438 | p_ctx->event_userdata = params->event_userdata; |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 439 | p_ctx->last_send_size = 0; |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 440 | |
| 441 | if (params->keylen > 0) { |
| 442 | p_ctx->cryptor = am_crypt_des_open(params->clearkey, |
| 443 | params->cleariv, |
| 444 | params->keylen * 8); |
| 445 | if (!p_ctx->cryptor) |
| 446 | DVR_DEBUG(1, "%s , open des cryptor failed!!!\n", __func__); |
| 447 | } else { |
| 448 | p_ctx->cryptor = NULL; |
| 449 | } |
| 450 | |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 451 | //check is new driver |
| 452 | p_ctx->is_new_dmx = dvr_check_dmx_isNew(); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 453 | /*Process crypto params, todo*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 454 | memset((void *)&dev_open_params, 0, sizeof(dev_open_params)); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 455 | if (params->data_from_memory) { |
| 456 | /* data from memory, VOD case */ |
| 457 | p_ctx->is_vod = 1; |
| 458 | } else { |
| 459 | p_ctx->is_vod = 0; |
| 460 | /* data from dmx, normal dvr case */ |
hualing chen | 958fe4c | 2020-03-24 17:35:07 +0800 | [diff] [blame] | 461 | dev_open_params.dmx_dev_id = params->dmx_dev_id; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 462 | dev_open_params.buf_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE); |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 463 | dev_open_params.ringbuf_size = params->ringbuf_size; |
hualing chen | 9811b21 | 2020-10-29 11:21:44 +0800 | [diff] [blame] | 464 | if (p_ctx->is_new_dmx) |
hualing chen | eceb37e | 2021-01-18 16:07:48 +0800 | [diff] [blame] | 465 | dev_open_params.buf_size = NEW_DEVICE_RECORD_BLOCK_SIZE * 30; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 466 | ret = record_device_open(&p_ctx->dev_handle, &dev_open_params); |
| 467 | if (ret != DVR_SUCCESS) { |
| 468 | DVR_DEBUG(1, "%s, open record devices failed", __func__); |
| 469 | return DVR_FAILURE; |
| 470 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 471 | } |
| 472 | |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 473 | p_ctx->block_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE); |
hualing chen | 9811b21 | 2020-10-29 11:21:44 +0800 | [diff] [blame] | 474 | if (p_ctx->is_new_dmx) |
Yahui Han | ce15e9c | 2020-12-08 18:08:32 +0800 | [diff] [blame] | 475 | p_ctx->block_size = NEW_DEVICE_RECORD_BLOCK_SIZE * 30; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 476 | p_ctx->enc_func = NULL; |
| 477 | p_ctx->enc_userdata = NULL; |
| 478 | p_ctx->is_secure_mode = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 479 | p_ctx->state = DVR_RECORD_STATE_OPENED; |
hualing chen | 9811b21 | 2020-10-29 11:21:44 +0800 | [diff] [blame] | 480 | DVR_DEBUG(1, "%s, block_size:%d is_new:%d", __func__, p_ctx->block_size, p_ctx->is_new_dmx); |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 481 | *p_handle = p_ctx; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 482 | return DVR_SUCCESS; |
| 483 | } |
| 484 | |
| 485 | int dvr_record_close(DVR_RecordHandle_t handle) |
| 486 | { |
| 487 | DVR_RecordContext_t *p_ctx; |
| 488 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 489 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 490 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 491 | p_ctx = (DVR_RecordContext_t *)handle; |
| 492 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 493 | if (p_ctx == &record_ctx[i]) |
| 494 | break; |
| 495 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 496 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 497 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 498 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 499 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 500 | if (p_ctx->cryptor) { |
| 501 | am_crypt_des_close(p_ctx->cryptor); |
| 502 | p_ctx->cryptor = NULL; |
| 503 | } |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 504 | if (p_ctx->is_vod) { |
| 505 | ret = DVR_SUCCESS; |
| 506 | } else { |
| 507 | ret = record_device_close(p_ctx->dev_handle); |
| 508 | if (ret != DVR_SUCCESS) { |
| 509 | DVR_DEBUG(1, "%s, failed", __func__); |
| 510 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 511 | } |
| 512 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 513 | p_ctx->state = DVR_RECORD_STATE_CLOSED; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 514 | return ret; |
| 515 | } |
| 516 | |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 517 | int dvr_record_pause(DVR_RecordHandle_t handle) |
| 518 | { |
| 519 | DVR_RecordContext_t *p_ctx; |
| 520 | int ret; |
| 521 | uint32_t i; |
| 522 | |
| 523 | p_ctx = (DVR_RecordContext_t *)handle; |
| 524 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 525 | if (p_ctx == &record_ctx[i]) |
| 526 | break; |
| 527 | } |
| 528 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 529 | |
| 530 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
| 531 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 532 | |
| 533 | if (p_ctx->is_vod) { |
| 534 | ret = DVR_SUCCESS; |
| 535 | } |
| 536 | //set pause state,will not store ts into segment |
| 537 | p_ctx->state = DVR_RECORD_STATE_PAUSE; |
| 538 | return ret; |
| 539 | } |
| 540 | |
| 541 | int dvr_record_resume(DVR_RecordHandle_t handle) |
| 542 | { |
| 543 | DVR_RecordContext_t *p_ctx; |
| 544 | int ret; |
| 545 | uint32_t i; |
| 546 | |
| 547 | p_ctx = (DVR_RecordContext_t *)handle; |
| 548 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 549 | if (p_ctx == &record_ctx[i]) |
| 550 | break; |
| 551 | } |
| 552 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 553 | |
| 554 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
| 555 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 556 | |
| 557 | if (p_ctx->is_vod) { |
| 558 | ret = DVR_SUCCESS; |
| 559 | } |
| 560 | //set stated state,will resume store ts into segment |
| 561 | p_ctx->state = DVR_RECORD_STATE_STARTED; |
| 562 | return ret; |
| 563 | } |
| 564 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 565 | #if 0 |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 566 | int dvr_record_register_encryption(DVR_RecordHandle_t handle, |
| 567 | DVR_CryptoFunction_t cb, |
| 568 | DVR_CryptoParams_t params, |
| 569 | void *userdata) |
| 570 | { |
| 571 | return DVR_SUCCESS; |
| 572 | } |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 573 | #endif |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 574 | |
| 575 | int dvr_record_start_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params) |
| 576 | { |
| 577 | DVR_RecordContext_t *p_ctx; |
| 578 | Segment_OpenParams_t open_params; |
| 579 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 580 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 581 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 582 | p_ctx = (DVR_RecordContext_t *)handle; |
| 583 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 584 | if (p_ctx == &record_ctx[i]) |
| 585 | break; |
| 586 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 587 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 588 | |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame] | 589 | 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] | 590 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 591 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 592 | DVR_RETURN_IF_FALSE(params); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 593 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 594 | 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] | 595 | memset(&open_params, 0, sizeof(open_params)); |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 596 | memcpy(open_params.location, params->location, sizeof(params->location)); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 597 | open_params.segment_id = params->segment.segment_id; |
| 598 | open_params.mode = SEGMENT_MODE_WRITE; |
| 599 | |
| 600 | ret = segment_open(&open_params, &p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 601 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 602 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 603 | /*process params*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 604 | { |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 605 | memcpy(p_ctx->location, params->location, sizeof(params->location)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 606 | //need all params?? |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 607 | memcpy(&p_ctx->segment_params, ¶ms->segment, sizeof(params->segment)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 608 | /*save current segment info*/ |
| 609 | memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info)); |
| 610 | p_ctx->segment_info.id = params->segment.segment_id; |
| 611 | p_ctx->segment_info.nb_pids = params->segment.nb_pids; |
| 612 | 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] | 613 | } |
| 614 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 615 | if (!p_ctx->is_vod) { |
| 616 | /* normal dvr case */ |
| 617 | for (i = 0; i < params->segment.nb_pids; i++) { |
| 618 | ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid); |
| 619 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 620 | } |
| 621 | ret = record_device_start(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 622 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 623 | } |
| 624 | |
Zhiqiang Han | 5ebad99 | 2020-04-28 18:19:59 +0800 | [diff] [blame] | 625 | ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info); |
| 626 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 627 | p_ctx->state = DVR_RECORD_STATE_STARTED; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 628 | if (!p_ctx->is_vod) |
| 629 | pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 630 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 631 | return DVR_SUCCESS; |
| 632 | } |
| 633 | |
| 634 | int dvr_record_next_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, DVR_RecordSegmentInfo_t *p_info) |
| 635 | { |
| 636 | DVR_RecordContext_t *p_ctx; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 637 | Segment_OpenParams_t open_params; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 638 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 639 | uint32_t i; |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 640 | loff_t pos; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 641 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 642 | p_ctx = (DVR_RecordContext_t *)handle; |
| 643 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 644 | if (p_ctx == &record_ctx[i]) |
| 645 | break; |
| 646 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 647 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 648 | |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame] | 649 | 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] | 650 | DVR_RETURN_IF_FALSE(p_ctx->state == DVR_RECORD_STATE_STARTED); |
| 651 | //DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 652 | DVR_RETURN_IF_FALSE(params); |
| 653 | DVR_RETURN_IF_FALSE(p_info); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 654 | DVR_RETURN_IF_FALSE(!p_ctx->is_vod); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 655 | |
| 656 | /*Stop the on going record segment*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 657 | //ret = record_device_stop(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 658 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 659 | p_ctx->state = DVR_RECORD_STATE_STOPPED; |
| 660 | pthread_join(p_ctx->thread, NULL); |
| 661 | |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 662 | //add index file store |
| 663 | pos = segment_tell_position(p_ctx->segment_handle); |
| 664 | segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos); |
| 665 | |
| 666 | 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] | 667 | /*Update segment info*/ |
| 668 | memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info)); |
| 669 | |
| 670 | ret = segment_store_info(p_ctx->segment_handle, p_info); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 671 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 672 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 673 | 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", |
| 674 | __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] | 675 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 676 | /*Close current segment*/ |
| 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 | /*Open the new record segment*/ |
| 680 | memset(&open_params, 0, sizeof(open_params)); |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 681 | memcpy(open_params.location, p_ctx->location, sizeof(p_ctx->location)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 682 | open_params.segment_id = params->segment.segment_id; |
| 683 | open_params.mode = SEGMENT_MODE_WRITE; |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 684 | 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] | 685 | |
| 686 | ret = segment_open(&open_params, &p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 687 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 688 | /*process params*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 689 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 690 | //need all params?? |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 691 | memcpy(&p_ctx->segment_params, ¶ms->segment, sizeof(params->segment)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 692 | /*save current segment info*/ |
| 693 | memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info)); |
| 694 | p_ctx->segment_info.id = params->segment.segment_id; |
| 695 | 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] | 696 | } |
| 697 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 698 | p_ctx->segment_info.nb_pids = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 699 | for (i = 0; i < params->segment.nb_pids; i++) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 700 | switch (params->segment.pid_action[i]) { |
| 701 | case DVR_RECORD_PID_CREATE: |
| 702 | DVR_DEBUG(1, "%s create pid:%d", __func__, params->segment.pids[i].pid); |
| 703 | ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid); |
| 704 | p_ctx->segment_info.nb_pids++; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 705 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 706 | break; |
| 707 | case DVR_RECORD_PID_KEEP: |
| 708 | DVR_DEBUG(1, "%s keep pid:%d", __func__, params->segment.pids[i].pid); |
| 709 | p_ctx->segment_info.nb_pids++; |
| 710 | break; |
| 711 | case DVR_RECORD_PID_CLOSE: |
| 712 | DVR_DEBUG(1, "%s close pid:%d", __func__, params->segment.pids[i].pid); |
| 713 | 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] | 714 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 715 | break; |
| 716 | default: |
| 717 | DVR_DEBUG(1, "%s wrong action pid:%d", __func__, params->segment.pids[i].pid); |
| 718 | return DVR_FAILURE; |
| 719 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 720 | } |
| 721 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 722 | //ret = record_device_start(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 723 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 724 | /*Update segment info*/ |
| 725 | ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 726 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 727 | p_ctx->state = DVR_RECORD_STATE_STARTED; |
Zhiqiang Han | 0d60f2b | 2020-05-26 14:39:54 +0800 | [diff] [blame] | 728 | pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 729 | return DVR_SUCCESS; |
| 730 | } |
| 731 | |
| 732 | int dvr_record_stop_segment(DVR_RecordHandle_t handle, DVR_RecordSegmentInfo_t *p_info) |
| 733 | { |
| 734 | DVR_RecordContext_t *p_ctx; |
| 735 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 736 | uint32_t i; |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 737 | loff_t pos; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 738 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 739 | p_ctx = (DVR_RecordContext_t *)handle; |
| 740 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 741 | if (p_ctx == &record_ctx[i]) |
| 742 | break; |
| 743 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 744 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 745 | |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame] | 746 | 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] | 747 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STOPPED); |
| 748 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 749 | DVR_RETURN_IF_FALSE(p_info);/*should support NULL*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 750 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 751 | p_ctx->state = DVR_RECORD_STATE_STOPPED; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 752 | if (p_ctx->is_vod) { |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 753 | 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] | 754 | p_ctx->segment_info.duration = 10*1000; //debug, should delete it |
| 755 | } else { |
| 756 | ret = record_device_stop(p_ctx->dev_handle); |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 757 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 758 | if (ret != DVR_SUCCESS) |
| 759 | goto end; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 760 | //p_ctx->state = DVR_RECORD_STATE_STOPPED; |
| 761 | pthread_join(p_ctx->thread, NULL); |
| 762 | } |
| 763 | |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 764 | //add index file store |
| 765 | pos = segment_tell_position(p_ctx->segment_handle); |
| 766 | 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] | 767 | 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] | 768 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 769 | /*Update segment info*/ |
| 770 | memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info)); |
| 771 | |
| 772 | ret = segment_store_info(p_ctx->segment_handle, p_info); |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 773 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 774 | if (ret != DVR_SUCCESS) |
| 775 | goto end; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 776 | |
| 777 | DVR_DEBUG(1, "%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d", |
| 778 | __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets); |
| 779 | |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 780 | end: |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 781 | ret = segment_close(p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 782 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 783 | return DVR_SUCCESS; |
| 784 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 785 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 786 | int dvr_record_resume_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, uint64_t *p_resume_size) |
| 787 | { |
| 788 | DVR_RecordContext_t *p_ctx; |
| 789 | uint32_t i; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 790 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 791 | |
| 792 | p_ctx = (DVR_RecordContext_t *)handle; |
| 793 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 794 | if (p_ctx == &record_ctx[i]) |
| 795 | break; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 796 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 797 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 798 | DVR_RETURN_IF_FALSE(params); |
| 799 | DVR_RETURN_IF_FALSE(p_resume_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 800 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 801 | DVR_DEBUG(1, "%s , current state:%d, resume size:%lld", __func__, p_ctx->state, *p_resume_size); |
| 802 | ret = dvr_record_start_segment(handle, params); |
| 803 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 804 | |
| 805 | p_ctx->segment_info.size = *p_resume_size; |
| 806 | |
| 807 | return DVR_SUCCESS; |
| 808 | } |
| 809 | |
| 810 | int dvr_record_get_status(DVR_RecordHandle_t handle, DVR_RecordStatus_t *p_status) |
| 811 | { |
| 812 | DVR_RecordContext_t *p_ctx; |
| 813 | int i; |
| 814 | |
| 815 | p_ctx = (DVR_RecordContext_t *)handle; |
| 816 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 817 | if (p_ctx == &record_ctx[i]) |
| 818 | break; |
| 819 | } |
| 820 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 821 | DVR_RETURN_IF_FALSE(p_status); |
| 822 | |
| 823 | //lock |
| 824 | p_status->state = p_ctx->state; |
| 825 | p_status->info.id = p_ctx->segment_info.id; |
| 826 | p_status->info.duration = p_ctx->segment_info.duration; |
| 827 | p_status->info.size = p_ctx->segment_info.size; |
| 828 | p_status->info.nb_packets = p_ctx->segment_info.size/188; |
| 829 | |
| 830 | return DVR_SUCCESS; |
| 831 | } |
| 832 | |
| 833 | int dvr_record_write(DVR_RecordHandle_t handle, void *buffer, uint32_t len) |
| 834 | { |
| 835 | DVR_RecordContext_t *p_ctx; |
| 836 | uint32_t i; |
| 837 | off_t pos = 0; |
| 838 | int ret; |
| 839 | int has_pcr; |
| 840 | |
| 841 | p_ctx = (DVR_RecordContext_t *)handle; |
| 842 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 843 | if (p_ctx == &record_ctx[i]) |
| 844 | break; |
| 845 | } |
| 846 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 847 | DVR_RETURN_IF_FALSE(buffer); |
| 848 | DVR_RETURN_IF_FALSE(len); |
| 849 | |
| 850 | pos = segment_tell_position(p_ctx->segment_handle); |
| 851 | has_pcr = record_do_pcr_index(p_ctx, buffer, len); |
| 852 | if (has_pcr == 0) { |
| 853 | /* Pull VOD record shoud use PCR time index */ |
| 854 | DVR_DEBUG(1, "%s has no pcr, can NOT do time index", __func__); |
| 855 | } |
| 856 | ret = segment_write(p_ctx->segment_handle, buffer, len); |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 857 | if (ret != len) { |
| 858 | DVR_DEBUG(1, "%s write error ret:%d len:%d", __func__, ret, len); |
| 859 | } |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 860 | p_ctx->segment_info.size += len; |
| 861 | p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188; |
| 862 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 863 | return DVR_SUCCESS; |
| 864 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 865 | |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 866 | 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] | 867 | { |
| 868 | DVR_RecordContext_t *p_ctx; |
| 869 | uint32_t i; |
| 870 | |
| 871 | p_ctx = (DVR_RecordContext_t *)handle; |
| 872 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 873 | if (p_ctx == &record_ctx[i]) |
| 874 | break; |
| 875 | } |
| 876 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 877 | DVR_RETURN_IF_FALSE(func); |
| 878 | |
| 879 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
| 880 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 881 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 882 | |
| 883 | p_ctx->enc_func = func; |
| 884 | p_ctx->enc_userdata = userdata; |
| 885 | return DVR_SUCCESS; |
| 886 | } |
| 887 | |
| 888 | int dvr_record_set_secure_buffer(DVR_RecordHandle_t handle, uint8_t *p_secure_buf, uint32_t len) |
| 889 | { |
| 890 | DVR_RecordContext_t *p_ctx; |
| 891 | uint32_t i; |
| 892 | int ret; |
| 893 | |
| 894 | p_ctx = (DVR_RecordContext_t *)handle; |
| 895 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 896 | if (p_ctx == &record_ctx[i]) |
| 897 | break; |
| 898 | } |
| 899 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 900 | DVR_RETURN_IF_FALSE(p_secure_buf); |
| 901 | DVR_RETURN_IF_FALSE(len); |
| 902 | |
| 903 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
| 904 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 905 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 906 | |
| 907 | ret = record_device_set_secure_buffer(p_ctx->dev_handle, p_secure_buf, len); |
| 908 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 909 | |
| 910 | p_ctx->is_secure_mode = 1; |
| 911 | return ret; |
| 912 | } |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame] | 913 | |
| 914 | int dvr_record_is_secure_mode(DVR_RecordHandle_t handle) |
| 915 | { |
| 916 | DVR_RecordContext_t *p_ctx; |
| 917 | uint32_t i; |
| 918 | int ret; |
| 919 | |
| 920 | p_ctx = (DVR_RecordContext_t *)handle; |
| 921 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 922 | if (p_ctx == &record_ctx[i]) |
| 923 | break; |
| 924 | } |
| 925 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 926 | |
| 927 | if (p_ctx->is_secure_mode == 1) |
| 928 | ret = 1; |
| 929 | else |
| 930 | ret = 0; |
| 931 | return ret; |
Yahui Han | ce15e9c | 2020-12-08 18:08:32 +0800 | [diff] [blame] | 932 | } |