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