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